L4Re Operating System Framework
Interface and Usage Documentation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
item_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/cxx/bitmap>
18
19namespace L4Re { namespace Util {
20
22using cxx::Bitmap;
23
28{
29private:
30 long _capacity;
31 long _free_hint;
32 Bitmap_base _bits;
33
34 void hint(long hint)
35 { __atomic_store_n(&_free_hint, hint, __ATOMIC_RELAXED); }
36
37public:
38 bool is_allocated(long item) const noexcept
39 { return _bits[item]; }
40
41 long hint() const { return __atomic_load_n(&_free_hint, __ATOMIC_RELAXED); }
42
43 bool alloc(long item) noexcept
44 {
45 return !_bits.atomic_get_and_set(item);
46 }
47
48 void free(long item) noexcept
49 {
50 if (item < hint())
51 hint(item);
52
53 _bits.atomic_clear_bit(item);
54 }
55
56 Item_alloc_base(long size, void *mem) noexcept
57 : _capacity(size), _free_hint(0), _bits(mem)
58 {}
59
60 long alloc() noexcept
61 {
62 long free_hint = hint();
63
64 for (long i = free_hint; i < _capacity; ++i)
65 if (alloc(i))
66 {
67 hint(i + 1);
68 return i;
69 }
70
71 // _free_hint is not necessarily correct in case of multi-threading! Make
72 // sure we don't miss any potentially free slots.
73 for (long i = 0; i < free_hint && i < _capacity; ++i)
74 if (alloc(i))
75 {
76 hint(i + 1);
77 return i;
78 }
79
80 return -1;
81 }
82
83 long size() const noexcept
84 {
85 return _capacity;
86 }
87};
88
89template< long Bits >
90class Item_alloc : public Item_alloc_base
91{
92private:
93 typename Bitmap_base::Word<Bits>::Type _bits[Bitmap_base::Word<Bits>::Size];
94
95public:
96 Item_alloc() noexcept : Item_alloc_base(Bits, _bits) {}
97};
98
99}}
Helper abstraction for a word contained in the bitmap.
Definition bitmap:80
Basic bitmap abstraction.
Definition bitmap:19
void atomic_clear_bit(long bit) noexcept
Clear bit bit atomically.
Definition bitmap:269
word_type atomic_get_and_set(long bit) noexcept
Set bit bit atomically and return old state.
Definition bitmap:308
A static bitmap.
Definition bitmap:221
L4Re C++ Interfaces.
Definition cmd_control:14