]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/loader/server/src/name_space.cc
update
[l4.git] / l4 / pkg / loader / 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 "obj_reg.h"
13 #include "global.h"
14
15 #include <l4/cxx/minmax>
16 #include <l4/re/consts>
17
18 #include <cstring>
19 #include <cstdlib>
20 #include <cassert>
21
22 namespace Ldr {
23
24 static Dbg dbg(Dbg::Name_space, "ns");
25
26 Name_space::Name_space()
27 : L4Re::Util::Names::Name_space(dbg, Err())
28 {
29   Gate_alloc::registry.register_obj(this);
30 }
31
32 Name_space::~Name_space()
33 {
34   Gate_alloc::registry.unregister_obj(this);
35 }
36
37
38 Entry *
39 Name_space::alloc_dynamic_entry(Names::Name const &name, unsigned flags)
40 {
41   char *na = (char*)malloc(name.len());
42   if (!na)
43     return 0;
44
45   memcpy(na, name.start(), name.len());
46   Names::Name new_name(na, name.len());
47   Entry *e = new Ldr::Entry(new_name, Names::Obj(flags), true);
48   if (e)
49     return e;
50
51   free(na);
52
53   return 0;
54 }
55
56 void
57 Name_space::free_dynamic_entry(Names::Entry *n)
58 {
59   free(const_cast<char*>(n->name().start()));
60   assert (!n->next_link());
61   delete static_cast<Ldr::Entry*>(n);
62 }
63
64 int
65 Name_space::get_capability(L4::Ipc::Snd_fpage const &cap_fp, L4::Cap<void> *cap,
66                            L4::Server_object **so)
67 {
68   if (cap_fp.id_received())
69     {
70       L4::Server_object *o = L4::Basic_registry::find(cap_fp.data());
71       if (!o)
72         return -L4_EINVAL;
73
74       if (so)
75         *so = o;
76
77       *cap = o->obj_cap();
78       return 0;
79     }
80
81   if (cap_fp.cap_received())
82     {
83       *cap = Glbl::rcv_cap;
84       return 0;
85     }
86
87   return -L4_EINVAL;
88 }
89
90 int
91 Name_space::save_capability(L4::Cap<void> *cap)
92 {
93   if (*cap != Glbl::rcv_cap)
94     return 0;
95
96   L4::Cap<void> nc = L4Re::Util::cap_alloc.alloc<void>();
97
98   if (!nc.is_valid())
99     return -L4_ENOMEM;
100
101   nc.move(Glbl::rcv_cap);
102   *cap = nc;
103   return 0;
104 }
105
106 void
107 Name_space::free_capability(L4::Cap<void> cap)
108 {
109   L4Re::Util::cap_alloc.free(cap);
110 }
111
112 }
113
114
115 // dump stuff
116 #include <l4/cxx/iostream>
117
118 namespace L4Re { namespace Util { namespace Names {
119
120 void
121 Name_space::dump(bool rec, int indent) const
122 {
123   Name_space const *n;
124   //L4::cout << "loader: Name space dump (" << obj_cap() << ")\n";
125   for (Const_iterator i = begin(); i != end(); ++i)
126     {
127       for (int x = 0; x < indent; ++x)
128         L4::cout << "  ";
129
130       L4::cout << "  " << i->name() << " -> "
131                << i->obj()->cap().cap()
132                << " o=" << (void*)i->obj()->obj()  << " f="
133                << i->obj()->flags() << '\n';
134       if (rec && i->obj()->is_valid() 
135           && (n = dynamic_cast<Name_space const *>(i->obj()->obj())))
136         n->dump(rec, indent + 1);
137     }
138 }
139
140
141 }}}
142