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 * This file is part of TUD:OS and distributed under the terms of the
13 * GNU General Public License 2.
14 * Please see the COPYING-GPL-2 file for details.
15 *
16 * As a special exception, you may use this file as part of a free software
17 * library without restriction. Specifically, if other files instantiate
18 * templates or use macros or inline functions from this file, or you compile
19 * this file and link it with other files to produce an executable, this
20 * file does not by itself cause the resulting executable to be covered by
21 * the GNU General Public License. This exception does not however
22 * invalidate any other reasons why the executable file might be covered by
23 * the GNU General Public License.
24 */
25
26#pragma once
27
28#include <l4/re/util/item_alloc>
29#include <l4/sys/assert.h>
30#include <l4/sys/capability>
31#include <l4/sys/task.h>
32
33namespace L4Re { namespace Util {
34
40{
41private:
42 long _bias;
43 Item_alloc_base _items;
44
45public:
46 template <unsigned COUNT>
47 struct Storage
48 {
49 typename Bitmap_base::Word<COUNT>::Type _bits[Bitmap_base::Word<COUNT>::Size];
50 };
51
52 enum State { Free = 0, Allocated, Unknown };
53 Cap_alloc_base(long max, void *mem, long bias = 0, void * = 0)
54 noexcept : _bias(bias), _items(max, mem) {}
55
56 L4::Cap<void> alloc() noexcept
57 {
58 long cap = _items.alloc();
59 if (cap < 0)
61
62 return L4::Cap<void>((cap + _bias) << L4_CAP_SHIFT);
63 }
64
65 long hint() const { return _items.hint(); }
66
70 template< typename T >
71 L4::Cap<T> alloc() noexcept
72 { return L4::Cap<T>(alloc().cap()); }
73
74 State is_allocated(L4::Cap<void> c) const noexcept
75 {
76 long idx = (c.cap() >> L4_CAP_SHIFT);
77
78 if (idx < _bias)
79 return Unknown;
80
81 idx -= _bias;
82 if (idx >= _items.size())
83 return Unknown;
84
85 return _items.is_allocated(idx) ? Allocated : Free;
86 }
87
91 template< typename T>
92 void free(L4::Cap<T> const &cap, l4_cap_idx_t task = L4_INVALID_CAP,
93 l4_umword_t unmap_flags = L4_FP_ALL_SPACES) noexcept
94 {
95 long idx = (cap.cap() >> L4_CAP_SHIFT);
96 if (idx < _bias)
97 return;
98
99 idx -= _bias;
100 if (idx >= _items.size())
101 return;
102
103 l4_assert(_items.is_allocated(idx));
104
105 if (l4_is_valid_cap(task))
106 l4_task_unmap(task, cap.fpage(), unmap_flags | 2);
107
108 _items.free(idx);
109 }
110
111 // since we have no counters assume counter always > 0
112 void take(L4::Cap<void>) noexcept {}
113 bool release(L4::Cap<void>, l4_cap_idx_t /*task*/ = L4_INVALID_CAP,
114 unsigned /*unmap_flags*/ = L4_FP_ALL_SPACES) noexcept
115 { return false; }
116
117 long last() noexcept
118 {
119 return _items.size() + _bias - 1;
120 }
121};
122
123template< long Size >
124class Cap_alloc : public Cap_alloc_base
125{
126private:
127 typename Bitmap_base::Word<Size>::Type _bits[Bitmap_base::Word<Size>::Size];
128
129public:
130 explicit Cap_alloc(long bias = 0) noexcept
131 : Cap_alloc_base(Size, _bits, bias) {}
132
133};
134
135}
136}
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:91
unsigned long l4_umword_t
Unsigned machine word.
Definition l4int.h:51
unsigned long l4_cap_idx_t
Capability selector type.
Definition types.h:359
unsigned l4_is_valid_cap(l4_cap_idx_t c) L4_NOTHROW
Test if a capability selector is a valid selector.
Definition types.h:416
@ L4_CAP_SHIFT
Capability index shift.
Definition consts.h:157
@ L4_INVALID_CAP
Invalid capability selector.
Definition consts.h:168
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:411
@ L4_FP_ALL_SPACES
Flag to tell the unmap operation to revoke permissions from all child mappings including the mapping ...
Definition consts.h:198
Item allocator.
L4Re C++ Interfaces.
Definition l4re.dox:17
Low-level assert implementation.
#define l4_assert(expr)
Low-level assert.
Definition assert.h:43