L4Re Operating System Framework
Interface and Usage Documentation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
31{
32private:
33 void operator = (Cap_alloc const &);
34
35protected:
36 Cap_alloc(Cap_alloc const &) {}
37 Cap_alloc() {}
38
39public:
40
45 virtual L4::Cap<void> alloc() noexcept = 0;
46 virtual void take(L4::Cap<void> cap) noexcept = 0;
47
52 template< typename T >
53 L4::Cap<T> alloc() noexcept
54 { return L4::cap_cast<T>(alloc()); }
55
63 unsigned unmap_flags = L4_FP_ALL_SPACES) noexcept = 0;
64 virtual bool release(L4::Cap<void> cap, l4_cap_idx_t task = L4_INVALID_CAP,
65 unsigned unmap_flags = L4_FP_ALL_SPACES) noexcept = 0;
66
70 virtual ~Cap_alloc() = 0;
71};
72
73template<typename ALLOC>
74struct Cap_alloc_t : ALLOC, L4Re::Cap_alloc
75{
76 template<typename ...ARGS>
77 Cap_alloc_t(ARGS &&...args) : ALLOC(cxx::forward<ARGS>(args)...) {}
78
79 L4::Cap<void> alloc() noexcept override { return ALLOC::alloc(); }
80 void take(L4::Cap<void> cap) noexcept override { ALLOC::take(cap); }
81
82 template <typename T>
83 L4::Cap<T> alloc() noexcept
84 {
85 return L4::cap_cast<T>(alloc());
86 }
87
88 void free(L4::Cap<void> cap, l4_cap_idx_t task = L4_INVALID_CAP,
89 unsigned unmap_flags = L4_FP_ALL_SPACES) noexcept override
90 { ALLOC::free(cap, task, unmap_flags); }
91
92 bool release(L4::Cap<void> cap, l4_cap_idx_t task,
93 unsigned unmap_flags) noexcept override
94 { return ALLOC::release(cap, task, unmap_flags); }
95
96 void operator delete(void *) {}
97};
98
99inline
102
103extern Cap_alloc *virt_cap_alloc;
104
109template< unsigned long Unmap_flags = L4_FP_ALL_SPACES >
111{
112private:
113 Cap_alloc *_ca;
114
115public:
116 Smart_cap_auto() : _ca(0) {}
117 Smart_cap_auto(Cap_alloc *ca) : _ca(ca) {}
118
119 void free(L4::Cap_base &c)
120 {
121 if (c.is_valid() && _ca)
122 _ca->free(L4::Cap<void>(c.cap()), This_task, Unmap_flags);
123
124 invalidate(c);
125 }
126
127 static void invalidate(L4::Cap_base &c)
128 {
129 if (c.is_valid())
130 c.invalidate();
131 }
132
133};
134
138template< unsigned long Unmap_flags = L4_FP_ALL_SPACES >
140{
141private:
142 Cap_alloc *_ca;
143
144public:
145 Smart_count_cap() : _ca(nullptr) {}
146 Smart_count_cap(Cap_alloc *ca) : _ca(ca) {}
151 void free(L4::Cap_base &c) noexcept
152 {
153 if (c.is_valid())
154 {
155 if (_ca && _ca->release(L4::Cap<void>(c.cap()), This_task, Unmap_flags))
156 c.invalidate();
157 }
158 }
159
163 static void invalidate(L4::Cap_base &c) noexcept
164 {
165 if (c.is_valid())
166 c.invalidate();
167 }
168
173 {
174 if (src.is_valid())
175 _ca->take(L4::Cap<void>(src.cap()));
176 return src;
177 }
178};
181}
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.
Helper for Unique_cap and Unique_del_cap.
Definition cap_alloc:111
Helper for Ref_cap and Ref_del_cap.
Definition cap_alloc:140
L4::Cap_base copy(L4::Cap_base const &src)
Copy operation for L4::Smart_cap (increment ref count).
Definition cap_alloc:172
void free(L4::Cap_base &c) noexcept
Free operation for L4::Smart_cap (decrement ref count and delete if 0).
Definition cap_alloc:151
static void invalidate(L4::Cap_base &c) noexcept
Invalidate operation for L4::Smart_cap.
Definition cap_alloc:163
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:137
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:219
unsigned long l4_cap_idx_t
Capability selector type.
Definition types.h:335
@ L4_INVALID_CAP
Invalid capability selector.
Definition consts.h:157
@ L4_FP_ALL_SPACES
Flag to tell the unmap operation to revoke permissions from all child mappings including the mapping ...
Definition consts.h:187
virtual ~Cap_alloc()=0
Destructor.
Definition cap_alloc:100
L4Re C++ Interfaces.
Definition cmd_control:14
L4 low-level kernel interface.
Constants.
L4::Capability class.
Common task related definitions.