L4Re - L4 Runtime Environment
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>
28 #include <l4/sys/smart_capability>
29 #include <l4/re/consts>
30 
31 namespace L4Re {
32 
40 class Cap_alloc
41 {
42 private:
43  void operator = (Cap_alloc const &);
44 
45 protected:
46  Cap_alloc(Cap_alloc const &) {}
47  Cap_alloc() {}
48 
49 public:
50 
55  virtual L4::Cap<void> alloc() throw() = 0;
56 
61  template< typename T >
62  L4::Cap<T> alloc() throw()
63  { return L4::cap_cast<T>(alloc()); }
64 
69  virtual void free(L4::Cap<void> cap) throw() = 0;
70 
71  //virtual L4::Cap<void> next_allocated(l4_umword_t *refs, L4::Cap<void> pivot = L4::Cap<void>::Invalid) throw() = 0;
72 
76  virtual ~Cap_alloc() = 0;
77 
83  template< typename CAP_ALLOC >
84  static inline L4Re::Cap_alloc *
85  get_cap_alloc(CAP_ALLOC &ca)
86  {
87  struct CA : public L4Re::Cap_alloc
88  {
89  CAP_ALLOC &_ca;
90  L4::Cap<void> alloc() throw() { return _ca.alloc(); }
91  void free(L4::Cap<void> cap) throw() { _ca.free(cap); }
92  CA(CAP_ALLOC &ca) : _ca(ca) {}
93  };
94 
95  static CA _ca(ca);
96  return &_ca;
97  }
98 };
99 
100 inline
102 {}
103 
104 
109 template< unsigned long Unmap_flags = L4_FP_ALL_SPACES >
111 {
112 private:
113  Cap_alloc *_ca;
114 
115 public:
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  {
123  L4::Cap<L4::Task>(This_task)->unmap(c.fpage(), Unmap_flags);
124  _ca->free(L4::Cap<void>(c.cap()));
125  }
126  invalidate(c);
127  }
128 
129  static void invalidate(L4::Cap_base &c)
130  {
131  if (c.is_valid())
132  c.invalidate();
133  }
134 
135  static L4::Cap_base copy(L4::Cap_base const &src)
136  {
137  L4::Cap_base r = src;
138  invalidate(const_cast<L4::Cap_base &>(src));
139  return r;
140  }
141 };
142 
168 template< typename T >
169 struct Auto_cap
170 {
171  typedef L4::Smart_cap<T, Smart_cap_auto<> > Cap;
172 };
173 
203 template< typename T >
204 struct Auto_del_cap
205 {
207 };
210 }
L4::Cap< T > alloc()
Allocate a capability.
Definition: cap_alloc:62
Smart capability class.
L4::Capability class.
Constants.
Capability allocator interface.
Definition: cap_alloc:40
static L4Re::Cap_alloc * get_cap_alloc(CAP_ALLOC &ca)
Construct an instance of a capability allocator.
Definition: cap_alloc:85
L4Re C++ Interfaces.
Definition: cmd_control:15
Common task related definitions.
void invalidate()
Set this capability to invalid (L4_INVALID_CAP).
Definition: capability.h:134
virtual ~Cap_alloc()=0
Destructor.
Definition: cap_alloc:101
l4_cap_idx_t cap() const
Return capability selector.
Definition: capability.h:51
Helper for Auto_cap and Auto_del_cap.
Definition: cap_alloc:110
l4_fpage_t fpage(unsigned rights=L4_FPAGE_RWX) const
Return flex-page for the capability.
Definition: capability.h:71
Base class for all kinds of capabilities.
Definition: capability.h:25
bool is_valid() const
Test whether the capability is a valid capability index (i.e., not L4_INVALID_CAP).
Definition: capability.h:59
virtual void free(L4::Cap< void > cap)=0
Free a capability.
Cap< T > cap_cast(Cap< F > const &c)
static_cast for capabilities.
Definition: capability.h:376
virtual L4::Cap< void > alloc()=0
Allocate a capability.
C++ interface for capabilities.
Definition: capability.h:13