L4Re Operating System Framework
Interface and Usage Documentation
Loading...
Searching...
No Matches
bitmap_cap_alloc
Go to the documentation of this file.
1// -*- Mode: C++ -*-
2// vim:ft=cpp
7/*
8 * (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
9 * Alexander Warg <warg@os.inf.tu-dresden.de>
10 * economic rights: Technische Universität Dresden (Germany)
11 *
12 * License: see LICENSE.spdx (in this directory or the directories above)
13 */
14
15#pragma once
16
17#include <l4/re/util/item_alloc>
18#include <l4/sys/assert.h>
19#include <l4/sys/capability>
20#include <l4/sys/task.h>
21
22namespace L4Re { namespace Util {
23
29{
30private:
31 long _bias;
32 Item_alloc_base _items;
33
34public:
35 template <unsigned COUNT>
36 struct Storage
37 {
38 typename Bitmap_base::Word<COUNT>::Type _bits[Bitmap_base::Word<COUNT>::Size];
39 };
40
41 enum State { Free = 0, Allocated, Unknown };
42 Cap_alloc_base(long max, void *mem, long bias = 0, void * = 0)
43 noexcept : _bias(bias), _items(max, mem) {}
44
45 L4::Cap<void> alloc() noexcept
46 {
47 long cap = _items.alloc();
48 if (cap < 0)
50
51 return L4::Cap<void>((cap + _bias) << L4_CAP_SHIFT);
52 }
53
54 long hint() const { return _items.hint(); }
55
59 template< typename T >
60 L4::Cap<T> alloc() noexcept
61 { return L4::Cap<T>(alloc().cap()); }
62
63 State is_allocated(L4::Cap<void> c) const noexcept
64 {
65 long idx = (c.cap() >> L4_CAP_SHIFT);
66
67 if (idx < _bias)
68 return Unknown;
69
70 idx -= _bias;
71 if (idx >= _items.size())
72 return Unknown;
73
74 return _items.is_allocated(idx) ? Allocated : Free;
75 }
76
80 template< typename T>
81 void free(L4::Cap<T> const &cap, l4_cap_idx_t task = L4_INVALID_CAP,
82 l4_umword_t unmap_flags = L4_FP_ALL_SPACES) noexcept
83 {
84 long idx = (cap.cap() >> L4_CAP_SHIFT);
85 if (idx < _bias)
86 return;
87
88 idx -= _bias;
89 if (idx >= _items.size())
90 return;
91
92 l4_assert(_items.is_allocated(idx));
93
94 if (l4_is_valid_cap(task))
95 l4_task_unmap(task, cap.fpage(), unmap_flags | 2);
96
97 _items.free(idx);
98 }
99
100 // since we have no counters assume counter always > 0
101 void take(L4::Cap<void>) noexcept {}
102 bool release(L4::Cap<void>, l4_cap_idx_t /*task*/ = L4_INVALID_CAP,
103 unsigned /*unmap_flags*/ = L4_FP_ALL_SPACES) noexcept
104 { return false; }
105
106 long last() noexcept
107 {
108 return _items.size() + _bias - 1;
109 }
110};
111
112template< long Size >
113class Cap_alloc : public Cap_alloc_base
114{
115private:
116 typename Bitmap_base::Word<Size>::Type _bits[Bitmap_base::Word<Size>::Size];
117
118public:
119 explicit Cap_alloc(long bias = 0) noexcept
120 : Cap_alloc_base(Size, _bits, bias) {}
121
122};
123
124}
125}
L4::Cap related definitions.
Capability allocator.
L4::Cap< T > alloc() noexcept
Allocate a capability slot.
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.
C++ interface for capabilities.
Definition capability.h:219
Helper abstraction for a word contained in the bitmap.
Definition bitmap:80
unsigned long l4_umword_t
Unsigned machine word.
Definition l4int.h:40
unsigned long l4_cap_idx_t
Capability selector type.
Definition types.h:335
unsigned l4_is_valid_cap(l4_cap_idx_t c) L4_NOTHROW
Test if a capability selector is a valid selector.
Definition types.h:392
@ L4_CAP_SHIFT
Capability index shift.
Definition consts.h:146
@ L4_INVALID_CAP
Invalid capability selector.
Definition consts.h:157
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:423
@ L4_FP_ALL_SPACES
Flag to tell the unmap operation to revoke permissions from all child mappings including the mapping ...
Definition consts.h:187
Item allocator.
L4Re C++ Interfaces.
Definition cmd_control:14
Low-level assert implementation.
#define l4_assert(expr)
Low-level assert.
Definition assert.h:32