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 * This file is part of TUD:OS and distributed under the terms of the
12 * GNU General Public License 2.
13 * Please see the COPYING-GPL-2 file for details.
14 *
15 * As a special exception, you may use this file as part of a free software
16 * library without restriction. Specifically, if other files instantiate
17 * templates or use macros or inline functions from this file, or you compile
18 * this file and link it with other files to produce an executable, this
19 * file does not by itself cause the resulting executable to be covered by
20 * the GNU General Public License. This exception does not however
21 * invalidate any other reasons why the executable file might be covered by
22 * the GNU General Public License.
23 */
24
25#pragma once
26
27#include <l4/sys/task>
29#include <l4/re/consts>
30#include <l4/cxx/type_traits>
31
32namespace L4Re {
33
42{
43private:
44 void operator = (Cap_alloc const &);
45
46protected:
47 Cap_alloc(Cap_alloc const &) {}
48 Cap_alloc() {}
49
50public:
51
56 virtual L4::Cap<void> alloc() noexcept = 0;
57 virtual void take(L4::Cap<void> cap) noexcept = 0;
58
63 template< typename T >
64 L4::Cap<T> alloc() noexcept
65 { return L4::cap_cast<T>(alloc()); }
66
74 unsigned unmap_flags = L4_FP_ALL_SPACES) noexcept = 0;
75 virtual bool release(L4::Cap<void> cap, l4_cap_idx_t task = L4_INVALID_CAP,
76 unsigned unmap_flags = L4_FP_ALL_SPACES) noexcept = 0;
77
81 virtual ~Cap_alloc() = 0;
82
88 template< typename CAP_ALLOC >
89 static inline L4Re::Cap_alloc *
90 get_cap_alloc(CAP_ALLOC &ca)
91 {
92 struct CA : public L4Re::Cap_alloc
93 {
94 CAP_ALLOC &_ca;
95 L4::Cap<void> alloc() noexcept override { return _ca.alloc(); }
96 void take(L4::Cap<void> cap) noexcept override { _ca.take(cap); }
97
98 void free(L4::Cap<void> cap, l4_cap_idx_t task = L4_INVALID_CAP,
99 unsigned unmap_flags = L4_FP_ALL_SPACES) noexcept override
100 { _ca.free(cap, task, unmap_flags); }
101
102 bool release(L4::Cap<void> cap, l4_cap_idx_t task,
103 unsigned unmap_flags) noexcept override
104 { return _ca.release(cap, task, unmap_flags); }
105
106 void operator delete(void *) {}
107
108 CA(CAP_ALLOC &ca) : _ca(ca) {}
109 };
110
111 static CA _ca(ca);
112 return &_ca;
113 }
114};
115
116template<typename ALLOC>
117struct Cap_alloc_t : ALLOC, L4Re::Cap_alloc
118{
119 template<typename ...ARGS>
120 Cap_alloc_t(ARGS &&...args) : ALLOC(cxx::forward<ARGS>(args)...) {}
121
122 L4::Cap<void> alloc() noexcept override { return ALLOC::alloc(); }
123 void take(L4::Cap<void> cap) noexcept override { ALLOC::take(cap); }
124
125 void free(L4::Cap<void> cap, l4_cap_idx_t task = L4_INVALID_CAP,
126 unsigned unmap_flags = L4_FP_ALL_SPACES) noexcept override
127 { ALLOC::free(cap, task, unmap_flags); }
128
129 bool release(L4::Cap<void> cap, l4_cap_idx_t task,
130 unsigned unmap_flags) noexcept override
131 { return ALLOC::release(cap, task, unmap_flags); }
132
133 void operator delete(void *) {}
134};
135
136inline
139
140extern Cap_alloc *virt_cap_alloc;
141
146template< unsigned long Unmap_flags = L4_FP_ALL_SPACES >
148{
149private:
150 Cap_alloc *_ca;
151
152public:
153 Smart_cap_auto() : _ca(0) {}
154 Smart_cap_auto(Cap_alloc *ca) : _ca(ca) {}
155
156 void free(L4::Cap_base &c)
157 {
158 if (c.is_valid() && _ca)
159 _ca->free(L4::Cap<void>(c.cap()), This_task, Unmap_flags);
160
161 invalidate(c);
162 }
163
164 static void invalidate(L4::Cap_base &c)
165 {
166 if (c.is_valid())
167 c.invalidate();
168 }
169
170 static L4::Cap_base copy(L4::Cap_base const &src)
171 {
172 L4::Cap_base r = src;
173 invalidate(const_cast<L4::Cap_base &>(src));
174 return r;
175 }
176};
177
181template< unsigned long Unmap_flags = L4_FP_ALL_SPACES >
183{
184private:
185 Cap_alloc *_ca;
186
187public:
188 Smart_count_cap() : _ca(nullptr) {}
189 Smart_count_cap(Cap_alloc *ca) : _ca(ca) {}
194 void free(L4::Cap_base &c) noexcept
195 {
196 if (c.is_valid())
197 {
198 if (_ca && _ca->release(L4::Cap<void>(c.cap()), This_task, Unmap_flags))
199 c.invalidate();
200 }
201 }
202
206 static void invalidate(L4::Cap_base &c) noexcept
207 {
208 if (c.is_valid())
209 c.invalidate();
210 }
211
216 {
217 if (src.is_valid())
218 _ca->take(L4::Cap<void>(src.cap()));
219 return src;
220 }
221};
224}
Capability allocator interface.
Definition cap_alloc:42
virtual L4::Cap< void > alloc() noexcept=0
Allocate a capability.
static L4Re::Cap_alloc * get_cap_alloc(CAP_ALLOC &ca)
Construct an instance of a capability allocator.
Definition cap_alloc:90
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.
Helper for Unique_cap and Unique_del_cap.
Definition cap_alloc:148
Helper for Ref_cap and Ref_del_cap.
Definition cap_alloc:183
L4::Cap_base copy(L4::Cap_base const &src)
Copy operation for L4::Smart_cap (increment ref count).
Definition cap_alloc:215
void free(L4::Cap_base &c) noexcept
Free operation for L4::Smart_cap (decrement ref count and delete if 0).
Definition cap_alloc:194
static void invalidate(L4::Cap_base &c) noexcept
Invalidate operation for L4::Smart_cap.
Definition cap_alloc:206
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:140
l4_cap_idx_t cap() const noexcept
Return capability selector.
Definition capability.h:52
bool is_valid() const noexcept
Test whether the capability is a valid capability index (i.e., not L4_INVALID_CAP).
Definition capability.h:60
C++ interface for capabilities.
Definition capability.h:222
unsigned long l4_cap_idx_t
Capability selector type.
Definition types.h:358
@ L4_INVALID_CAP
Invalid capability selector.
Definition consts.h:168
@ L4_FP_ALL_SPACES
Flag to tell the unmap operation to revoke permissions from all child mappings including the mapping ...
Definition consts.h:198
virtual ~Cap_alloc()=0
Destructor.
Definition cap_alloc:137
L4Re C++ Interfaces.
Definition cmd_control:15
L4 low-level kernel interface.
Our C++ library.
Definition arith:22
Constants.
L4::Capability class.
Common task related definitions.