4 * \file bitmap_cap_alloc
5 * \brief Bitmap capability allocator
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)
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.
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.
28 #include <l4/re/util/item_alloc>
29 #include <l4/sys/capability>
30 #include <l4/sys/task.h>
32 namespace L4Re { namespace Util {
35 * \brief Capability allocator.
36 * \ingroup api_l4re_util
42 Item_alloc_base _items;
45 enum State { Free = 0, Allocated, Unknown };
46 Cap_alloc_base(long max, void *mem, long bias = 0)
47 throw() : _bias(bias), _items(max, mem) {}
49 L4::Cap<void> alloc() throw()
51 long cap = _items.alloc();
53 return L4::Cap<void>::Invalid;
55 return L4::Cap<void>((cap + _bias) << L4_CAP_SHIFT);
58 long hint() const { return _items.hint(); }
61 * \brief Allocate a capability slot.
63 template< typename T >
64 L4::Cap<T> alloc() throw()
65 { return L4::Cap<T>(alloc().cap()); }
67 State is_allocated(L4::Cap<void> c) const throw()
69 long idx = (c.cap() >> L4_CAP_SHIFT);
75 return _items.is_allocated(idx) ? Allocated : Free;
79 * \brief Free a capability slot.
82 void free(L4::Cap<T> const &cap, l4_cap_idx_t task = -1UL,
83 l4_umword_t unmap_flags = L4_FP_ALL_SPACES) throw()
85 long idx = (cap.cap() >> L4_CAP_SHIFT);
94 l4_task_unmap(task, cap.fpage(), unmap_flags | 2);
97 // since we hav no counters assume couter always > 0
98 void take(L4::Cap<void>) throw() {}
99 bool release(L4::Cap<void>, l4_cap_idx_t task = L4_INVALID_CAP,
100 unsigned unmap_flags = L4_FP_ALL_SPACES) throw()
101 { (void)task; (void)unmap_flags; return false; }
105 return _items.size() + _bias - 1;
109 template< long Size >
110 class Cap_alloc : public Cap_alloc_base
113 typename Bitmap_base::Word<Size>::Type _bits[Bitmap_base::Word<Size>::Size];
116 explicit Cap_alloc(long bias = 0) throw()
117 : Cap_alloc_base(Size, _bits, bias) {}