5 * \brief 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/cap_alloc_impl.h>
29 #include <l4/sys/smart_capability>
30 #include <l4/sys/task>
31 #include <l4/re/consts>
33 namespace L4Re { namespace Util {
36 * \defgroup l4re_cap_api L4Re Capability API
40 * \brief Capability allocator.
42 * This is the instance of the capability allocator that is used
43 * by usual applications. The actual implementation of the allocator
44 * depends on the configuration of the system.
46 * Per default we use a reference count capability allocator, that
47 * keeps a reference counter for each managed capability selector.
49 * \note This capability allocator is not thread-safe.
51 extern _Cap_alloc &cap_alloc;
54 * \brief Helper for Auto_cap and Auto_del_cap.
56 template< unsigned long Unmap_flags = L4_FP_ALL_SPACES >
61 * \brief free operation for L4::Smart_cap.
63 static void free(L4::Cap_base &c)
67 cap_alloc.free(L4::Cap<void>(c.cap()), This_task, Unmap_flags);
73 * \brief invalidate operation for L4::Smart_cap.
75 static void invalidate(L4::Cap_base &c)
82 * \brief copy operation for L4::Smart_cap.
84 static L4::Cap_base copy(L4::Cap_base const &src)
87 invalidate(const_cast<L4::Cap_base &>(src));
94 * \brief Helper for Ref_cap and Ref_del_cap.
96 template< unsigned long Unmap_flags = L4_FP_ALL_SPACES >
101 * \brief free operation for L4::Smart_cap (decrement ref count and delete
104 static void free(L4::Cap_base &c)
108 if (cap_alloc.release(L4::Cap<void>(c.cap()), This_task, Unmap_flags))
114 * \brief invalidate operation for L4::Smart_cap.
116 static void invalidate(L4::Cap_base &c)
123 * \brief copy operation for L4::Smart_cap (increment ref count).
125 static L4::Cap_base copy(L4::Cap_base const &src)
127 cap_alloc.take(L4::Cap<void>(src.cap()));
134 * \brief Automatic capability that implements automatic free and
135 * unmap of the capability selector.
136 * \param T the type of the object that is referred by the capability.
138 * This kind of automatic capability is useful for capabilities with
139 * that shall have a lifetime that is strictly coupled to one C++ scope.
144 * L4Re::Util::Auto_cap<L4Re::Dataspace>::Cap
145 * ds_cap(L4Re::Util::cap_alloc.alloc<L4Re::Datasapce>));
147 * // use the dataspace cap
148 * L4Re::chksys(mem_alloc->alloc(4096, ds_cap.get()));
152 * // At the end of the scope ds_cap is unmapped and the capability selector
157 template< typename T >
160 typedef L4::Smart_cap<T, Smart_cap_auto< L4_FP_ALL_SPACES> > Cap;
164 * \brief Automatic capability that implements automatic free and
165 * unmap+delete of the capability selector.
166 * \param T the type of the object that is referred by the capability.
168 * This kind of automatic capability is useful for capabilities with
169 * that shall have a lifetime that is strictly coupled to one C++ scope.
170 * The main difference to Auto_cap is that the unmap is done with the
171 * deletion flag enabled and this leads to the deletion of the object
172 * if the current task holds appropriate deletion rights.
177 * L4Re::Util::Auto_del_cap<L4Re::Dataspace>::Cap
178 * ds_cap(L4Re::Util::cap_alloc.alloc<L4Re::Datasapce>));
180 * // use the dataspace cap
181 * L4Re::chksys(mem_alloc->alloc(4096, ds_cap.get()));
185 * // At the end of the scope ds_cap is unmapped and the capability selector
186 * // is freed. Because the deletion flag is set the data space shall be
187 * // also deleted (even if there are other references to this data space).
191 template< typename T >
194 typedef L4::Smart_cap<T, Smart_cap_auto<L4_FP_DELETE_OBJ> > Cap;
198 * \brief Automatic capability that implements automatic free and
199 * unmap of the capability selector.
200 * \param T the type of the object that is referred by the capability.
202 * This kind of automatic capability implements a counted reference to a
203 * capability selector. The capability shall be unmapped and freed
204 * when the reference count in the allocator goes to zero.
208 * L4Re::Util::Ref_cap<L4Re::Dataspace>::Ca global_ds_cap;
211 * L4Re::Util::Ref_cap<L4Re::Dataspace>::Cap
212 * ds_cap(L4Re::Util::cap_alloc.alloc<L4Re::Datasapce>));
213 * // reference count for the allocated cap selector is now 1
215 * // use the dataspace cap
216 * L4Re::chksys(mem_alloc->alloc(4096, ds_cap.get()));
218 * global_ds_cap = ds_cap;
219 * // reference count is now 2
222 * // reference count dropped to 1 (ds_cap is no longer exiting).
225 template< typename T >
228 typedef L4::Smart_cap<T, Smart_count_cap<L4_FP_ALL_SPACES> > Cap;
232 * \brief Automatic capability that implements automatic free and
233 * unmap+delete of the capability selector.
234 * \param T the type of the object that is referred by the capability.
236 * This kind of automatic capability implements a counted reference to a
237 * capability selector. The capability shall be unmapped and freed
238 * when the reference count in the allocator goes to zero.
239 * The main difference to Ref_cap is that the unmap is done with the
240 * deletion flag enabled and this leads to the deletion of the object
241 * if the current task holds appropriate deletion rights.
245 * L4Re::Util::Ref_del_cap<L4Re::Dataspace>::Ca global_ds_cap;
248 * L4Re::Util::Ref_del_cap<L4Re::Dataspace>::Cap
249 * ds_cap(L4Re::Util::cap_alloc.alloc<L4Re::Datasapce>));
250 * // reference count for the allocated cap selector is now 1
252 * // use the dataspace cap
253 * L4Re::chksys(mem_alloc->alloc(4096, ds_cap.get()));
255 * global_ds_cap = ds_cap;
256 * // reference count is now 2
259 * // reference count dropped to 1 (ds_cap is no longer exiting).
261 * global_ds_cap = L4_INVALID_CAP;
262 * // reference count dropped to 0 (data space shall be deleted).
265 template< typename T >
268 typedef L4::Smart_cap<T, Smart_count_cap<L4_FP_DELETE_OBJ> > Cap;