]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/l4con/server/src/object_registry_gc
0615d516d9de03324b0ba6847b22537943e41925
[l4.git] / l4 / pkg / l4con / server / src / object_registry_gc
1 // vi:ft=cpp
2 /*
3  * (c) 2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>
4  *     economic rights: Technische Universität Dresden (Germany)
5  *
6  * This file is part of TUD:OS and distributed under the terms of the
7  * GNU General Public License 2.
8  * Please see the COPYING-GPL-2 file for details.
9  */
10
11 /****
12  _______  ______  _____ ____  ___ __  __ _____ _   _ _____  _    _
13 | ____\ \/ /  _ \| ____|  _ \|_ _|  \/  | ____| \ | |_   _|/ \  | |
14 |  _|  \  /| |_) |  _| | |_) || || |\/| |  _| |  \| | | | / _ \ | |
15 | |___ /  \|  __/| |___|  _ < | || |  | | |___| |\  | | |/ ___ \| |___
16 |_____/_/\_\_|   |_____|_| \_\___|_|  |_|_____|_| \_| |_/_/   \_\_____|
17                                                                        
18  ************/
19
20
21 #include <l4/re/util/object_registry>
22
23
24 namespace L4Re { namespace Util {
25
26 class Object_registry_gc : public Object_registry
27 {
28 public:
29   Object_registry_gc(L4::Cap<L4::Thread> server, L4::Cap<L4::Factory> factory)
30    : Object_registry(server, factory)
31   {}
32
33   L4::Cap<void> register_obj_with_gc(L4::Server_object *o, int refcnt)
34   {
35     int idx = get_free_slot();
36     if (idx == -1)
37       return L4::Cap<void>::Invalid;
38
39     L4::Cap<void> c = Object_registry::register_obj(o);
40
41     if (!c.is_valid())
42       return c;
43
44     objs[idx] = o;
45     ref_cnt[idx] = refcnt;
46
47     return c;
48   }
49
50   void gc_run(unsigned int min_delta_in_ms)
51   {
52     l4_cpu_time_t now = l4re_kip()->clock;
53
54     if (last_gc_run + (min_delta_in_ms * 1000) > now)
55       return;
56
57     last_gc_run = now;
58
59     for (int i = 0; i < SLOTS; ++i)
60       if (objs[i])
61         {
62           l4_msgtag_t tag =
63             L4Re::Env::env()->task()->cap_has_child(objs[i]->obj_cap());
64           if (!tag.label() && ref_cnt[i] == 0)
65             {
66               printf("Deleting %d %lx\n", i, objs[i]->obj_cap().cap());
67               delete objs[i]; // XXX: wrong because we do not create the object at open time
68               objs[i] = 0;
69             }
70         }
71   }
72
73   void ref_cnt_add(L4::Server_object *o, int d)
74   {
75     int idx;
76     if ((idx = find_obj(o)) != -1)
77       ref_cnt[idx] += d;
78   }
79
80 private:
81   enum { SLOTS = 100 };
82   L4::Server_object *objs[SLOTS];
83   int ref_cnt[SLOTS];
84   l4_cpu_time_t last_gc_run;
85
86   int get_free_slot()
87   {
88     for (int i = 0; i < SLOTS; ++i)
89       if (!objs[i])
90         return i;
91     return -1;
92   }
93
94   int find_obj(L4::Server_object *o)
95   {
96     for (int i = 0; i < SLOTS; ++i)
97       if (objs[i] == o)
98         return i;
99     return -1;
100   }
101 };
102
103 }}