1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
#include "object-dir.h"
#include <object.h>
#include <jove.h>
#include <syscall.h>
KernelObjectDirectory __rootdir;
KernelObjectDirectory*
jove_object_as_objdir(KernelObjectTyped *typed)
{
if(typed->type == KO_OBJECT_DIRECTORY) return (KernelObjectDirectory*)typed;
return NULL;
}
void
_jove_alloc_objdir_inplace(
KernelObjectDirectory *dir,
KernelObjectDirectory *parent,
uint8_t memb)
{
*dir = (KernelObjectDirectory) {
.typed = (KernelObjectTyped) {
.parent = parent,
.membi = memb,
.type = KO_OBJECT_DIRECTORY
},
.lastmemb = 0,
.firstfree = 0,
.children = { 0 }
};
parent->children[memb] = JOVE_OBJECT_TYPED(dir);
}
KernelObjectDirectory*
_jove_alloc_objdir(
KernelObjectDirectory *parent,
uint8_t memb)
{
if(!_jove_alloc) {
jove_errno = EJOVE_NOALLOC;
return NULL;
}
if(parent->children[memb]) {
jove_errno = EJOVE_FULL;
return NULL;
}
KernelObjectDirectory *dir = _jove_alloc(sizeof(KernelObjectDirectory));
if(dir == NULL) return NULL;
_jove_alloc_objdir_inplace(dir, parent, memb);
jove_errno = 0;
return dir;
}
JoveError
jove_objdir_sync(KernelObjectDirectory *dir)
{
if(dir->lastmemb) return dir->lastmemb;
int kerr = _syscall_invoke_objdir_lastmemb(dir, &dir->lastmemb);
jove_errno = jove_error_from_kerror(kerr);
return dir->lastmemb;
}
extern JoveError
_jove_objdir_sync_at_arch(
KernelObjectDirectory *dir,
uint8_t i,
obj_type_t type,
KernelObjectTyped **memb
);
JoveError
jove_objdir_sync_at(KernelObjectDirectory *dir, uint8_t i, KernelObjectTyped** memb)
{
obj_type_t type;
int err = _syscall_invoke_objdir_getmemb(dir, i, &type);
if(err != 0) return err;
KernelObjectTyped *dirmemb = dir->children[i];
if(dirmemb != NULL) {
if(dirmemb->type == type) {
if(memb) *memb = dirmemb;
return EJOVE_OK;
}
if(_jove_free == NULL) {
return EJOVE_NOFREE;
}
_jove_free(dirmemb);
}
if(_jove_alloc == NULL)
return EJOVE_NOALLOC;
switch(type) {
case KO_NONE:
return EJOVE_OK;
case KO_OBJECT_DIRECTORY:
dirmemb = JOVE_OBJECT_TYPED(_jove_alloc_objdir(dir, i));
if(memb) *memb = dirmemb;
break;
case KO_MEMORY_UNTYPED:
{
dirmemb = JOVE_OBJECT_TYPED(_jove_alloc_untyped(dir, i));
if(memb) *memb = dirmemb;
break;
}
default:
return _jove_objdir_sync_at_arch(dir, i, type, memb);
}
return EJOVE_OK;
}
int
jove_objdir_nmemb(KernelObjectDirectory *dir)
{
int nmemb = 0;
for(unsigned i = 1; i < 256; i++) {
if(dir->children[i] != NULL) nmemb++;
}
return nmemb;
}
int
jove_objdir_lastmemb(KernelObjectDirectory *dir)
{
if(dir->lastmemb) return dir->lastmemb;
JoveError err = jove_error_from_kerror(_syscall_invoke_objdir_lastmemb(dir, &dir->lastmemb));
if(err) {
jove_errno = err;
return 0;
}
return dir->lastmemb;
}
KernelObjectTyped*
jove_objdir_get(KernelObjectDirectory *dir, uint8_t i)
{
return dir->children[i];
}
JoveError
jove_objdir_move(
KernelObjectDirectory *dir,
uint8_t memb,
KernelObjectDirectory *dest_dir,
uint8_t dest_memb)
{
if(!memb)
return EJOVE_BADOBJ;
if(!dir->children[memb])
return EJOVE_BADOBJ;
if(dest_memb)
return EJOVE_BADOBJ;
if(dest_dir->children[dest_memb])
return EJOVE_FULL;
int kerr = _syscall_invoke_objdir_move(dir, memb, dest_dir, dest_memb);
if(kerr) return jove_error_from_kerror(kerr);
KernelObjectTyped *move = dir->children[memb];
dir->children[memb] = NULL;
dest_dir->children[dest_memb] = move;
if(dir->lastmemb == move->membi) {
dir->lastmemb = 0;
}
move->parent = dest_dir;
move->membi = dest_memb;
return EJOVE_OK;
}
JoveError jove_object_move(
KernelObjectTyped *typed,
KernelObjectDirectory *dest_dir,
uint8_t dest_memb)
{
return jove_objdir_move(typed->parent, typed->membi, dest_dir, dest_memb);
}
JoveError jove_object_move_inplace(
KernelObjectTyped *typed,
KernelObjectTyped *dest)
{
return jove_objdir_move(typed->parent, typed->membi, dest->parent, dest->membi);
}
|