[Mammoth] Add an owned memory capability that cleans itself up.

This commit is contained in:
Drew Galbraith 2023-11-19 17:54:28 -08:00
parent 7d48dd2b8b
commit 3e9923f227
5 changed files with 84 additions and 9 deletions

View file

@ -43,3 +43,41 @@ MappedMemoryRegion MappedMemoryRegion::FromCapability(z_cap_t vmmo_cap) {
// FIXME: get the size here.
return MappedMemoryRegion(vmmo_cap, 0, vaddr, 0);
}
OwnedMemoryRegion::OwnedMemoryRegion(OwnedMemoryRegion&& other)
: OwnedMemoryRegion(other.vmmo_cap_, other.paddr_, other.vaddr_,
other.size_) {
other.vmmo_cap_ = 0;
other.paddr_ = 0;
other.vaddr_ = 0;
other.size_ = 0;
}
OwnedMemoryRegion& OwnedMemoryRegion::operator=(OwnedMemoryRegion&& other) {
if (vmmo_cap_) {
check(ZCapRelease(vmmo_cap_));
}
vmmo_cap_ = other.vmmo_cap_;
paddr_ = other.paddr_;
vaddr_ = other.vaddr_;
size_ = other.size_;
other.vmmo_cap_ = 0;
other.paddr_ = 0;
other.vaddr_ = 0;
other.size_ = 0;
return *this;
}
OwnedMemoryRegion::~OwnedMemoryRegion() {
if (vmmo_cap_ != 0) {
check(ZCapRelease(vmmo_cap_));
}
}
OwnedMemoryRegion OwnedMemoryRegion::FromCapability(z_cap_t vmmo_cap) {
uint64_t vaddr;
check(ZAddressSpaceMap(gSelfVmasCap, 0, vmmo_cap, &vaddr));
// FIXME: get the size here.
return OwnedMemoryRegion(vmmo_cap, 0, vaddr, 0);
}