]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/moe/server/src/name_space.cc
update
[l4.git] / l4 / pkg / moe / server / src / name_space.cc
1 /*
2  * (c) 2008-2009 Alexander Warg <warg@os.inf.tu-dresden.de>
3  *     economic rights: Technische Universität Dresden (Germany)
4  *
5  * This file is part of TUD:OS and distributed under the terms of the
6  * GNU General Public License 2.
7  * Please see the COPYING-GPL-2 file for details.
8  */
9 #include <l4/cxx/iostream>
10 #include "name_space.h"
11 #include "debug.h"
12 #include "globals.h"
13 #include "string.h"
14 #include "server_obj.h"
15
16 #include <l4/cxx/l4iostream>
17 #include <l4/cxx/minmax>
18
19 #include <cstring>
20 #include <cstdlib>
21 #include <cassert>
22
23 Moe::Name_space *root_name_space()
24 {
25   static Moe::Name_space _root;
26   return &_root;
27 }
28
29 namespace Moe {
30
31 static Dbg dbg(Dbg::Name_space, "ns");
32
33 Name_space::Name_space()
34 : L4Re::Util::Names::Name_space(dbg, Err())
35 {
36   object_pool.cap_alloc()->alloc(this);
37 }
38
39 Name_space::~Name_space()
40 {
41   object_pool.cap_alloc()->free(this);
42 }
43
44
45 static Slab_alloc<Name_space> *alloc()
46 {
47   static Slab_alloc<Name_space> a;
48   return &a;
49 }
50
51 void *
52 Name_space::operator new (size_t) throw()
53 {
54   return alloc()->alloc();
55 }
56
57 void
58 Name_space::operator delete (void *p, size_t) throw()
59 { alloc()->free((Name_space*)p); }
60
61 Entry *
62 Name_space::alloc_dynamic_entry(Names::Name const &name, unsigned flags)
63 {
64   char *na = (char*)GC_MALLOC_ATOMIC(name.len());
65   if (!na)
66     return 0;
67
68   memcpy(na, name.start(), name.len());
69   Names::Name new_name(na, name.len());
70   Entry *e = new Moe::Entry(new_name, Names::Obj(flags), true);
71   if (e)
72     return e;
73
74   free(na);
75
76   return 0;
77 }
78
79 void
80 Name_space::free_dynamic_entry(Names::Entry *n)
81 {
82   free(const_cast<char*>(n->name().start()));
83   assert (!n->next_link());
84   delete static_cast<Moe::Entry*>(n);
85 }
86
87 int
88 Name_space::get_capability(L4::Ipc::Snd_fpage const &cap_fp, L4::Cap<void> *cap,
89                            L4::Server_object **lo)
90 {
91   L4::Cap<void> rcv_cap(Rcv_cap << L4_CAP_SHIFT);
92
93   if (cap_fp.id_received())
94     {
95       L4::Server_object *o = object_pool.find(cap_fp.data());
96       if (!o)
97         return -L4_EINVAL;
98
99       if (lo)
100         *lo = o;
101
102       *cap = o->obj_cap();
103       return 0;
104     }
105
106   if (cap_fp.cap_received())
107     {
108       *cap = rcv_cap;
109       return 0;
110     }
111
112   return -L4_EINVAL;
113 }
114
115 int
116 Name_space::save_capability(L4::Cap<void> *cap)
117 {
118   L4::Cap<void> rcv_cap(Rcv_cap << L4_CAP_SHIFT);
119   if (*cap != rcv_cap)
120     return 0;
121
122   L4::Cap<void> nc = object_pool.cap_alloc()->alloc<void>();
123   if (!nc.is_valid())
124     return -L4_ENOMEM;
125
126   nc.move(rcv_cap);
127   *cap = nc;
128   return 0;
129 }
130
131 void
132 Name_space::free_capability(L4::Cap<void> cap)
133 {
134   object_pool.cap_alloc()->free(cap);
135 }
136
137 }
138
139 namespace L4Re { namespace Util { namespace Names {
140
141 void
142 Name_space::dump(bool rec, int indent) const
143 {
144   Name_space const *n;
145   //L4::cout << "MOE: Name space dump (" << obj_cap() << ")\n";
146   for (Const_iterator i = begin(); i != end(); ++i)
147     {
148       for (int x = 0; x < indent; ++x)
149         L4::cout << "  ";
150
151       L4::cout << "  " << i->name()  << " -> " << i->obj()->cap()
152                << " o=" << (void*)i->obj()->obj()  << " f="
153                << i->obj()->flags() << '\n';
154       if (rec && i->obj()->is_valid() 
155           && (n = dynamic_cast<Name_space const *>(i->obj()->obj())))
156         n->dump(rec, indent + 1);
157     }
158 }
159
160
161 }}}