2023-06-16 14:25:45 -07:00
|
|
|
#pragma once
|
|
|
|
|
2023-06-26 15:01:55 -07:00
|
|
|
#include <glacier/container/linked_list.h>
|
2023-06-21 15:07:40 -07:00
|
|
|
#include <glacier/memory/ref_ptr.h>
|
|
|
|
|
2023-06-16 14:25:45 -07:00
|
|
|
#include "capability/capability.h"
|
|
|
|
#include "lib/mutex.h"
|
|
|
|
|
|
|
|
class CapabilityTable {
|
|
|
|
public:
|
|
|
|
CapabilityTable();
|
|
|
|
|
|
|
|
CapabilityTable(CapabilityTable&) = delete;
|
|
|
|
CapabilityTable& operator=(CapabilityTable&) = delete;
|
|
|
|
|
|
|
|
template <typename T>
|
2023-08-01 18:22:41 -07:00
|
|
|
z_cap_t AddNewCapability(const glcr::RefPtr<T>& object, uint64_t permissions);
|
|
|
|
template <typename T>
|
|
|
|
z_cap_t AddNewCapability(const glcr::RefPtr<T>& object) {
|
|
|
|
return AddNewCapability<T>(object, T::DefaultPermissions());
|
|
|
|
}
|
|
|
|
z_cap_t AddExistingCapability(const glcr::RefPtr<Capability>& cap);
|
2023-06-16 14:25:45 -07:00
|
|
|
|
2023-06-21 15:07:40 -07:00
|
|
|
glcr::RefPtr<Capability> GetCapability(uint64_t id);
|
|
|
|
glcr::RefPtr<Capability> ReleaseCapability(uint64_t id);
|
2023-06-16 14:25:45 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
Mutex lock_{"cap table"};
|
2023-06-19 23:32:49 -07:00
|
|
|
// TODO: Do some randomization.
|
2023-06-16 14:25:45 -07:00
|
|
|
uint64_t next_cap_id_ = 0x100;
|
|
|
|
// FIXME: use a map data structure.
|
2023-06-16 15:27:09 -07:00
|
|
|
struct CapEntry {
|
|
|
|
uint64_t id;
|
2023-06-21 15:07:40 -07:00
|
|
|
glcr::RefPtr<Capability> cap;
|
2023-06-16 15:27:09 -07:00
|
|
|
};
|
2023-06-26 15:01:55 -07:00
|
|
|
glcr::LinkedList<CapEntry> capabilities_;
|
2023-06-16 14:25:45 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
2023-06-21 15:07:40 -07:00
|
|
|
uint64_t CapabilityTable::AddNewCapability(const glcr::RefPtr<T>& object,
|
2023-06-16 14:25:45 -07:00
|
|
|
uint64_t permissions) {
|
|
|
|
MutexHolder h(lock_);
|
|
|
|
uint64_t id = next_cap_id_++;
|
2023-06-16 15:27:09 -07:00
|
|
|
capabilities_.PushBack(
|
|
|
|
{.id = id, .cap = MakeRefCounted<Capability>(object, permissions)});
|
2023-06-16 14:25:45 -07:00
|
|
|
return id;
|
|
|
|
}
|