L4Re - L4 Runtime Environment
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/capability>
30 #include <l4/sys/task.h>
31 
32 namespace L4Re { namespace Util {
33 
39 {
40 private:
41  long _bias;
42  Item_alloc_base _items;
43 
44 public:
45  enum State { Free = 0, Allocated, Unknown };
46  Cap_alloc_base(long max, void *mem, long bias = 0)
47  noexcept : _bias(bias), _items(max, mem) {}
48 
49  L4::Cap<void> alloc() noexcept
50  {
51  long cap = _items.alloc();
52  if (cap < 0)
54 
55  return L4::Cap<void>((cap + _bias) << L4_CAP_SHIFT);
56  }
57 
58  long hint() const { return _items.hint(); }
59 
63  template< typename T >
64  L4::Cap<T> alloc() noexcept
65  { return L4::Cap<T>(alloc().cap()); }
66 
67  State is_allocated(L4::Cap<void> c) const noexcept
68  {
69  long idx = (c.cap() >> L4_CAP_SHIFT);
70 
71  if (idx < _bias)
72  return Unknown;
73 
74  idx -= _bias;
75  return _items.is_allocated(idx) ? Allocated : Free;
76  }
77 
81  template< typename T>
82  void free(L4::Cap<T> const &cap, l4_cap_idx_t task = L4_INVALID_CAP,
83  l4_umword_t unmap_flags = L4_FP_ALL_SPACES) noexcept
84  {
85  long idx = (cap.cap() >> L4_CAP_SHIFT);
86  if (idx < _bias)
87  return;
88 
89  idx -= _bias;
90 
91  _items.free(idx);
92 
93  if (l4_is_valid_cap(task))
94  l4_task_unmap(task, cap.fpage(), unmap_flags | 2);
95  }
96 
97  // since we have no counters assume counter always > 0
98  void take(L4::Cap<void>) noexcept {}
99  bool release(L4::Cap<void>, l4_cap_idx_t task = L4_INVALID_CAP,
100  unsigned unmap_flags = L4_FP_ALL_SPACES) noexcept
101  { (void)task; (void)unmap_flags; return false; }
102 
103  long last() noexcept
104  {
105  return _items.size() + _bias - 1;
106  }
107 };
108 
109 template< long Size >
110 class Cap_alloc : public Cap_alloc_base
111 {
112 private:
113  typename Bitmap_base::Word<Size>::Type _bits[Bitmap_base::Word<Size>::Size];
114 
115 public:
116  explicit Cap_alloc(long bias = 0) noexcept
117  : Cap_alloc_base(Size, _bits, bias) {}
118 
119 };
120 
121 }
122 }
l4_cap_idx_t cap() const noexcept
Return capability selector.
Definition: capability.h:52
Invalid capability selector.
Definition: consts.h:141
Item allocator.
Helper abstraction for a word contained in the bitmap.
Definition: bitmap:87
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:346
unsigned l4_is_valid_cap(l4_cap_idx_t c) L4_NOTHROW
Test if a capability selector is a valid selector.
Definition: types.h:396
unsigned long l4_cap_idx_t
L4 Capability selector Type.
Definition: types.h:342
L4Re C++ Interfaces.
Definition: cmd_control:15
L4::Cap related definitions.
Item allocator.
Definition: item_alloc:38
T1 max(T1 a, T1 b)
Get the maximum of a and b.
Definition: minmax:46
Flag to tell the unmap operation to unmap all child mappings including the mapping in the invoked tas...
Definition: consts.h:165
Capability index shift.
Definition: consts.h:131
L4::Cap< T > alloc() noexcept
Allocate a capability slot.
unsigned long l4_umword_t
Unsigned machine word.
Definition: l4int.h:52
Capability allocator.
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:13