2023-06-16 14:25:45 -07:00
|
|
|
#include "capability/capability_table.h"
|
|
|
|
|
2023-06-26 15:01:55 -07:00
|
|
|
#include "debug/debug.h"
|
|
|
|
|
2023-06-16 14:25:45 -07:00
|
|
|
CapabilityTable::CapabilityTable() {}
|
|
|
|
|
2023-06-21 15:07:40 -07:00
|
|
|
uint64_t CapabilityTable::AddExistingCapability(
|
|
|
|
const glcr::RefPtr<Capability>& cap) {
|
2023-06-16 14:25:45 -07:00
|
|
|
MutexHolder h(lock_);
|
2023-06-16 15:27:09 -07:00
|
|
|
uint64_t id = next_cap_id_++;
|
2023-11-16 22:12:00 -08:00
|
|
|
if (capabilities_.Insert(id, cap) != glcr::OK) {
|
|
|
|
panic("Reusing capability id.");
|
|
|
|
}
|
2023-06-16 15:27:09 -07:00
|
|
|
return id;
|
2023-06-16 14:25:45 -07:00
|
|
|
}
|
|
|
|
|
2023-06-21 15:07:40 -07:00
|
|
|
glcr::RefPtr<Capability> CapabilityTable::GetCapability(uint64_t id) {
|
2023-06-16 14:25:45 -07:00
|
|
|
MutexHolder h(lock_);
|
2023-11-16 22:12:00 -08:00
|
|
|
if (!capabilities_.Contains(id)) {
|
2023-11-19 17:53:56 -08:00
|
|
|
dbgln("Bad cap access {}", id);
|
|
|
|
return {};
|
2023-06-16 14:25:45 -07:00
|
|
|
}
|
2023-11-16 22:12:00 -08:00
|
|
|
return capabilities_.at(id);
|
2023-06-16 14:25:45 -07:00
|
|
|
}
|
|
|
|
|
2023-06-21 15:07:40 -07:00
|
|
|
glcr::RefPtr<Capability> CapabilityTable::ReleaseCapability(uint64_t id) {
|
2023-06-16 14:25:45 -07:00
|
|
|
MutexHolder h(lock_);
|
2023-11-16 22:12:00 -08:00
|
|
|
if (!capabilities_.Contains(id)) {
|
2023-11-19 17:53:56 -08:00
|
|
|
dbgln("Bad cap release {}", id);
|
|
|
|
return {};
|
2023-06-16 14:25:45 -07:00
|
|
|
}
|
2023-11-16 22:12:00 -08:00
|
|
|
auto cap = capabilities_.at(id);
|
|
|
|
(void)capabilities_.Delete(id);
|
|
|
|
return cap;
|
2023-06-16 14:25:45 -07:00
|
|
|
}
|
2023-11-24 17:05:11 -08:00
|
|
|
|
|
|
|
void CapabilityTable::ReleaseAll() {
|
|
|
|
for (uint64_t i = 0; i < next_cap_id_; i++) {
|
|
|
|
(void)capabilities_.Delete(i);
|
|
|
|
}
|
|
|
|
if (capabilities_.size() != 0) {
|
|
|
|
dbgln("Capabilities still remaining after clear: {x}",
|
|
|
|
capabilities_.size());
|
|
|
|
}
|
|
|
|
}
|