L4Re Operating System Framework
Interface and Usage Documentation
Loading...
Searching...
No Matches
cap_alloc
Go to the documentation of this file.
1// vi:set ft=cpp: -*- Mode: C++ -*-
6/*
7 * (c) 2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
8 * Alexander Warg <warg@os.inf.tu-dresden.de>
9 * economic rights: Technische Universität Dresden (Germany)
10 *
11 * License: see LICENSE.spdx (in this directory or the directories above)
12 */
13
14#pragma once
15
16#include <l4/sys/task>
18#include <l4/re/consts>
19#include <l4/cxx/type_traits>
20
21namespace L4Re {
22
30class Cap_alloc
31{
32public:
33 constexpr Cap_alloc() = default;
34
35 // Attention: do *not* define a destructor. We must keep the class trivially
36 // destructible so that no global destructor is created for
37 // L4Re::Util::cap_alloc.
38 // virtual ~Cap_alloc() = default;
39
40 Cap_alloc(Cap_alloc const &) = delete;
41 Cap_alloc &operator = (Cap_alloc const &) = delete;
42
47 virtual L4::Cap<void> alloc() noexcept = 0;
48 virtual void take(L4::Cap<void> cap) noexcept = 0;
49
54 template< typename T >
55 L4::Cap<T> alloc() noexcept
56 { return L4::cap_cast<T>(alloc()); }
57
65 unsigned unmap_flags = L4_FP_ALL_SPACES) noexcept = 0;
66 virtual bool release(L4::Cap<void> cap, l4_cap_idx_t task = L4_INVALID_CAP,
67 unsigned unmap_flags = L4_FP_ALL_SPACES) noexcept = 0;
68};
69
70template<typename ALLOC>
71struct Cap_alloc_t : ALLOC, L4Re::Cap_alloc
72{
73 template<typename ...ARGS>
74 Cap_alloc_t(ARGS &&...args) : ALLOC(cxx::forward<ARGS>(args)...) {}
75
76 L4::Cap<void> alloc() noexcept override { return ALLOC::alloc(); }
77 void take(L4::Cap<void> cap) noexcept override { ALLOC::take(cap); }
78
79 template <typename T>
80 L4::Cap<T> alloc() noexcept
81 {
82 return L4::cap_cast<T>(alloc());
83 }
84
85 void free(L4::Cap<void> cap, l4_cap_idx_t task = L4_INVALID_CAP,
86 unsigned unmap_flags = L4_FP_ALL_SPACES) noexcept override
87 { ALLOC::free(cap, task, unmap_flags); }
88
89 bool release(L4::Cap<void> cap, l4_cap_idx_t task,
90 unsigned unmap_flags) noexcept override
91 { return ALLOC::release(cap, task, unmap_flags); }
92
93 void operator delete(void *) {}
94};
95
96extern Cap_alloc *virt_cap_alloc;
97
102template< unsigned long Unmap_flags = L4_FP_ALL_SPACES >
103class Smart_cap_auto
104{
105private:
106 Cap_alloc *_ca;
107
108public:
109 Smart_cap_auto() : _ca(0) {}
110 Smart_cap_auto(Cap_alloc *ca) : _ca(ca) {}
111
112 void free(L4::Cap_base &c)
113 {
114 if (c.is_valid() && _ca)
115 _ca->free(L4::Cap<void>(c.cap()), This_task, Unmap_flags);
116
117 invalidate(c);
118 }
119
120 static void invalidate(L4::Cap_base &c)
121 {
122 if (c.is_valid())
123 c.invalidate();
124 }
125
126};
127
131template< unsigned long Unmap_flags = L4_FP_ALL_SPACES >
132class Smart_count_cap
133{
134private:
135 Cap_alloc *_ca;
136
137public:
138 Smart_count_cap() : _ca(nullptr) {}
139 Smart_count_cap(Cap_alloc *ca) : _ca(ca) {}
144 void free(L4::Cap_base &c) noexcept
145 {
146 if (c.is_valid())
147 {
148 if (_ca && _ca->release(L4::Cap<void>(c.cap()), This_task, Unmap_flags))
149 c.invalidate();
150 }
151 }
152
156 static void invalidate(L4::Cap_base &c) noexcept
157 {
158 if (c.is_valid())
159 c.invalidate();
160 }
161
166 {
167 if (src.is_valid())
168 _ca->take(L4::Cap<void>(src.cap()));
169 return src;
170 }
171};
172
173
174}
Capability allocator interface.
Definition cap_alloc:31
virtual L4::Cap< void > alloc() noexcept=0
Allocate a capability.
virtual void free(L4::Cap< void > cap, l4_cap_idx_t task=L4_INVALID_CAP, unsigned unmap_flags=L4_FP_ALL_SPACES) noexcept=0
Free a capability.
L4::Cap_base copy(L4::Cap_base const &src)
Copy operation for L4::Smart_cap (increment ref count).
Definition cap_alloc:165
void free(L4::Cap_base &c) noexcept
Free operation for L4::Smart_cap (decrement ref count and delete if 0).
Definition cap_alloc:144
static void invalidate(L4::Cap_base &c) noexcept
Invalidate operation for L4::Smart_cap.
Definition cap_alloc:156
Base class for all kinds of capabilities.
Definition capability.h:26
void invalidate() noexcept
Set this capability to invalid (L4_INVALID_CAP).
Definition capability.h:142
l4_cap_idx_t cap() const noexcept
Return capability selector.
Definition capability.h:49
bool is_valid() const noexcept
Test whether the capability is a valid capability index (i.e., not L4_INVALID_CAP).
Definition capability.h:57
C++ interface for capabilities.
Definition capability.h:224
unsigned long l4_cap_idx_t
Capability selector type.
Definition types.h:352
#define L4_INVALID_CAP
Invalid capability selector.
Definition consts.h:152
L4Re C++ Interfaces.
Definition cmd_control:14
@ L4_FP_ALL_SPACES
Flag to tell the unmap operation to revoke permissions from all child mappings including the mapping ...
Definition consts.h:181
L4 low-level kernel interface.
Cap< T > cap_cast(Cap< F > const &c) noexcept
static_cast for capabilities.
Definition capability.h:416
Constants.
L4::Capability class.
Common task related definitions.