]> 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   return 0;
75 }
76
77 void
78 Name_space::free_dynamic_entry(Names::Entry *n)
79 {
80   assert (!n->next_link());
81   delete static_cast<Moe::Entry*>(n);
82 }
83
84 int
85 Name_space::get_capability(L4::Ipc::Snd_fpage const &cap_fp, L4::Cap<void> *cap,
86                            L4::Server_object **lo)
87 {
88   L4::Cap<void> rcv_cap(Rcv_cap << L4_CAP_SHIFT);
89
90   if (cap_fp.id_received())
91     {
92       L4::Server_object *o = object_pool.find(cap_fp.data());
93       if (!o)
94         return -L4_EINVAL;
95
96       if (lo)
97         *lo = o;
98
99       *cap = o->obj_cap();
100       return 0;
101     }
102
103   if (cap_fp.cap_received())
104     {
105       *cap = rcv_cap;
106       return 0;
107     }
108
109   return -L4_EINVAL;
110 }
111
112 int
113 Name_space::save_capability(L4::Cap<void> *cap)
114 {
115   L4::Cap<void> rcv_cap(Rcv_cap << L4_CAP_SHIFT);
116   if (*cap != rcv_cap)
117     return 0;
118
119   L4::Cap<void> nc = object_pool.cap_alloc()->alloc<void>();
120   if (!nc.is_valid())
121     return -L4_ENOMEM;
122
123   nc.move(rcv_cap);
124   *cap = nc;
125   return 0;
126 }
127
128 void
129 Name_space::free_capability(L4::Cap<void> cap)
130 {
131   object_pool.cap_alloc()->free(cap);
132 }
133
134 }
135
136 namespace L4Re { namespace Util { namespace Names {
137
138 void
139 Name_space::dump(bool rec, int indent) const
140 {
141   Name_space const *n;
142   //L4::cout << "MOE: Name space dump (" << obj_cap() << ")\n";
143   for (Const_iterator i = begin(); i != end(); ++i)
144     {
145       for (int x = 0; x < indent; ++x)
146         L4::cout << "  ";
147
148       L4::cout << "  " << i->name()  << " -> " << i->obj()->cap()
149                << " o=" << (void*)i->obj()->obj()  << " f="
150                << i->obj()->flags() << '\n';
151       if (rec && i->obj()->is_valid() 
152           && (n = dynamic_cast<Name_space const *>(i->obj()->obj())))
153         n->dump(rec, indent + 1);
154     }
155 }
156
157
158 }}}