fix missing locks in the vfs subsystem
[lunaix-os.git] / lunaix-os / kernel / changeling.c
1 #include <lunaix/changeling.h>
2 #include <lunaix/mm/valloc.h>
3
4 #include <klibc/string.h>
5
6 static DEFINE_LLIST(chrysallidis);
7 static unsigned int current_id = 0;
8
9 void
10 changeling_init(morph_t* parent, morph_t* chlg, 
11                 unsigned int id, const char* name)
12 {
13     chlg->sig     = CHLG_ID;
14     chlg->ident   = id;
15     chlg->ref     = 1;
16     chlg->uid     = current_id++;
17     
18     changeling_setname(chlg, name);
19
20     if (!parent) {
21         llist_append(&chrysallidis, &chlg->sibs);
22     } else {
23         llist_append(&parent->subs, &chlg->sibs);
24     }
25
26     llist_init_head(&chlg->subs);
27 }
28
29 morph_t*
30 changeling_spawn(morph_t* parent, const char *name)
31 {
32     morph_t* changeling;
33
34     changeling = valloc(sizeof(morph_t));
35     changeling_init(parent, changeling, chlg_anon, name);
36     
37     return changeling;
38 }
39
40 morph_t*
41 changeling_find(morph_t* parent, struct hstr* str)
42 {
43     morph_t *p, *n;
44     llist_for_each(p, n, &parent->subs, sibs)
45     {
46         if (HSTR_EQ(&p->name, str)) {
47             return p;
48         }
49     }
50
51     return NULL;
52 }
53
54 morph_t*
55 changeling_get_at(morph_t* parent, int index)
56 {
57     morph_t *p, *n;
58     llist_for_each(p, n, &parent->subs, sibs)
59     {
60         if (!(index--)) {
61             return p;
62         }
63     }
64
65     return NULL;
66 }
67
68 morph_t*
69 changeling_setname(morph_t* chlg, const char* name)
70 {
71     if (name) {
72         chlg->name = HSTR(name, strlen(name));
73         hstr_rehash(&chlg->name, HSTR_FULL_HASH);
74     } else {
75         chlg->name = HHSTR(NULL, 0, 0);
76     }
77
78     return chlg;
79 }