Loading...
Searching...
No Matches
bitmap_cap_alloc
Go to the documentation of this file.
// -*- Mode: C++ -*-
// vim:ft=cpp
/*
* (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
* Alexander Warg <warg@os.inf.tu-dresden.de>
* economic rights: Technische Universität Dresden (Germany)
*
* License: see LICENSE.spdx (in this directory or the directories above)
*/
#pragma once
#include <l4/re/util/item_alloc>
#include <l4/sys/assert.h>
#include <l4/sys/capability>
#include <l4/sys/task.h>
namespace L4Re { namespace Util {
class Cap_alloc_base
{
private:
long _bias;
Item_alloc_base _items;
public:
template <unsigned COUNT>
struct Storage
{
typename Bitmap_base::Word<COUNT>::Type _bits[Bitmap_base::Word<COUNT>::Size];
};
enum State { Free = 0, Allocated, Unknown };
Cap_alloc_base(long max, void *mem, long bias = 0, void * = 0)
noexcept : _bias(bias), _items(max, mem) {}
L4::Cap<void> alloc() noexcept
{
long cap = _items.alloc();
if (cap < 0)
return L4::Cap<void>::Invalid;
return L4::Cap<void>((cap + _bias) << L4_CAP_SHIFT);
}
long hint() const { return _items.hint(); }
template< typename T >
L4::Cap<T> alloc() noexcept
{ return L4::Cap<T>(alloc().cap()); }
State is_allocated(L4::Cap<void> c) const noexcept
{
long idx = (c.cap() >> L4_CAP_SHIFT);
if (idx < _bias)
return Unknown;
idx -= _bias;
if (idx >= _items.size())
return Unknown;
return _items.is_allocated(idx) ? Allocated : Free;
}
template< typename T>
void free(L4::Cap<T> const &cap, l4_cap_idx_t task = L4_INVALID_CAP,
l4_umword_t unmap_flags = L4_FP_ALL_SPACES) noexcept
{
long idx = (cap.cap() >> L4_CAP_SHIFT);
if (idx < _bias)
return;
idx -= _bias;
if (idx >= _items.size())
return;
l4_assert(_items.is_allocated(idx));
if (l4_is_valid_cap(task))
l4_task_unmap(task, cap.fpage(), unmap_flags | 2);
_items.free(idx);
}
// since we have no counters assume counter always > 0
void take(L4::Cap<void>) noexcept {}
bool release(L4::Cap<void>, l4_cap_idx_t /*task*/ = L4_INVALID_CAP,
unsigned /*unmap_flags*/ = L4_FP_ALL_SPACES) noexcept
{ return false; }
long last() noexcept
{
return _items.size() + _bias - 1;
}
};
template< long Size >
class Cap_alloc : public Cap_alloc_base
{
private:
typename Bitmap_base::Word<Size>::Type _bits[Bitmap_base::Word<Size>::Size];
public:
explicit Cap_alloc(long bias = 0) noexcept
: Cap_alloc_base(Size, _bits, bias) {}
};
}
}
capability
L4::Cap related definitions.
L4Re::Util::Cap_alloc_base
Capability allocator.
Definition bitmap_cap_alloc:29
L4Re::Util::Cap_alloc_base::alloc
L4::Cap< T > alloc() noexcept
Allocate a capability slot.
Definition bitmap_cap_alloc:60
L4Re::Util::Cap_alloc_base::free
void free(L4::Cap< T > const &cap, l4_cap_idx_t task=L4_INVALID_CAP, l4_umword_t unmap_flags=L4_FP_ALL_SPACES) noexcept
Free a capability slot.
Definition bitmap_cap_alloc:81
L4Re::Util::Item_alloc_base
Item allocator.
Definition item_alloc:28
L4::Cap_base::Invalid
@ Invalid
Invalid capability selector.
Definition capability.h:42
L4::Cap
C++ interface for capabilities.
Definition capability.h:224
l4_umword_t
unsigned long l4_umword_t
Unsigned machine word.
Definition l4int.h:40
l4_cap_idx_t
unsigned long l4_cap_idx_t
Capability selector type.
Definition types.h:336
l4_is_valid_cap
unsigned l4_is_valid_cap(l4_cap_idx_t c) L4_NOTHROW
Test if a capability selector is a valid selector.
Definition types.h:393
L4_CAP_SHIFT
@ L4_CAP_SHIFT
Capability index shift.
Definition consts.h:142
L4_INVALID_CAP
@ L4_INVALID_CAP
Invalid capability selector.
Definition consts.h:153
l4_task_unmap
l4_msgtag_t l4_task_unmap(l4_cap_idx_t task, l4_fpage_t fpage, l4_umword_t map_mask) L4_NOTHROW
Revoke rights from the task.
Definition task.h:440
L4_FP_ALL_SPACES
@ L4_FP_ALL_SPACES
Flag to tell the unmap operation to revoke permissions from all child mappings including the mapping ...
Definition consts.h:183
item_alloc
Item allocator.
task.h
Common task related definitions.
L4Re::Util
Documentation of the L4 Runtime Environment utility functionality in C++.
Definition l4re.dox:21
L4Re
L4Re C++ Interfaces.
Definition cmd_control:14
assert.h
Low-level assert implementation.
l4_assert
#define l4_assert(expr)
Low-level assert.
Definition assert.h:32