acadia/zion/capability/capability_table.h

45 lines
1.2 KiB
C
Raw Normal View History

#pragma once
2023-06-21 15:07:40 -07:00
#include <glacier/memory/ref_ptr.h>
#include "capability/capability.h"
#include "lib/linked_list.h"
#include "lib/mutex.h"
class CapabilityTable {
public:
CapabilityTable();
CapabilityTable(CapabilityTable&) = delete;
CapabilityTable& operator=(CapabilityTable&) = delete;
template <typename T>
2023-06-21 15:07:40 -07:00
uint64_t AddNewCapability(const glcr::RefPtr<T>& object,
uint64_t permissions);
uint64_t AddExistingCapability(const glcr::RefPtr<Capability>& cap);
2023-06-21 15:07:40 -07:00
glcr::RefPtr<Capability> GetCapability(uint64_t id);
glcr::RefPtr<Capability> ReleaseCapability(uint64_t id);
private:
Mutex lock_{"cap table"};
// TODO: Do some randomization.
uint64_t next_cap_id_ = 0x100;
// FIXME: use a map data structure.
struct CapEntry {
uint64_t id;
2023-06-21 15:07:40 -07:00
glcr::RefPtr<Capability> cap;
};
LinkedList<CapEntry> capabilities_;
};
template <typename T>
2023-06-21 15:07:40 -07:00
uint64_t CapabilityTable::AddNewCapability(const glcr::RefPtr<T>& object,
uint64_t permissions) {
MutexHolder h(lock_);
uint64_t id = next_cap_id_++;
capabilities_.PushBack(
{.id = id, .cap = MakeRefCounted<Capability>(object, permissions)});
return id;
}