-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjectManager.c
422 lines (369 loc) · 10.1 KB
/
ObjectManager.c
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include "Z-OS.h"
// This is the object manager's internal
// list of objects.
List CurrentObjects = {0};
// This is the internal list of registered
// type managers.
List TypeManagers = {0};
// This list provides the outside world
// (not really, but close) with handle lookup.
List OpenHandles = {0};
// This method (for use only within the object manager) returns an
// interface for the object with the specified index (not handle).
// This is used to call the IGeneric.Destroy function since the handle
// has already been destroyed by that point.
static Int16 GetInterfaceInternal(UInt16 index, UInt16 code, void** interface)
{
UInt16 i = 0;
InternalObject* obj;
TypeRegistration* typeManager;
Int16 ret = ErrorSuccess;
EnterCriticalSection();
obj = GetListItem(&CurrentObjects,index);
ExitCriticalSection();
if (!obj) return ErrorInvalidHandle;
EnterCriticalSection();
for (i = 0; i < TypeManagers.Length; i++)
{
if ((typeManager = ((TypeRegistration*)GetListItem(&TypeManagers,i)))->Type == obj->Type)
{
ret = typeManager->GetInterface(code,interface);
ExitCriticalSection();
return ret;
}
}
ExitCriticalSection();
return ErrorInvalidType;
}
// Returns an interface of the specified code for the
// object with the specified handle.
Int16 GetInterface(UInt16 handle, UInt16 code, void** interface)
{
Int16 ret = 0;
EnterCriticalSection();
ret = GetInterfaceInternal(((UInt16)GetListItem(&OpenHandles,handle-1)),code,interface);
ExitCriticalSection();
return ret;
}
// Registers a type manager with the specified functions.
Int16 RegisterTypeManager(TypeRegistration typeManager)
{
TypeRegistration* reg = zmalloc(sizeof(TypeRegistration));
if (!reg) return ErrorOutOfMemory;
*reg = typeManager;
AddListItem(&TypeManagers,reg);
return ErrorSuccess;
}
// Creates an object of the specified type,
// opens a handle for it and returns the handle.
Int16 CreateObject(UInt16 type, UInt16* handle, char* name)
{
InternalObject* obj = zmalloc(sizeof(InternalObject));
UInt16 index = 0;
UInt16 i;
Bool hasReplaced = False;
IGeneric* gen;
Int16 ret = 0;
if (!obj) return ErrorOutOfMemory;
// Assign the object name if wanted
if (name)
{
if (OpenObject(name, handle) == ErrorSuccess)
{
ReleaseObject(*handle);
zfree(obj);
return ErrorNameInUse;
}
obj->Name = zmalloc(strlen(name)+1);
if (!(obj->Name)) return ErrorOutOfMemory;
strcpy(obj->Name, name);
}
EnterCriticalSection();
for (i = 0; i < CurrentObjects.Length; i++)
{
if (!GetListItem(&CurrentObjects,i))
{
ReplaceListItem(&CurrentObjects,i,obj);
index = i;
hasReplaced = True;
break;
}
}
if (!hasReplaced)
{
index = CurrentObjects.Length;
AddListItem(&CurrentObjects,obj);
}
AddListItem(&OpenHandles,(void*)index);
obj->Type = type;
if (!(ret = GetInterface(OpenHandles.Length, CodeIGeneric, (void**)&gen)) && (gen->Create)) gen->Create(obj);
*handle = OpenHandles.Length;
obj->OpenHandles++;
ExitCriticalSection();
return ErrorSuccess;
}
// Returns if an object has any open handles.
Int16 IsObjectOpen(UInt16 index)
{
UInt16 i = 0;
EnterCriticalSection();
for (i = 0; i < OpenHandles.Length; i++)
{
if ((Int16)GetListItem(&OpenHandles,i) == index) { ExitCriticalSection(); return TRUE; }
}
ExitCriticalSection();
return FALSE;
}
// Returns a handle to an object with the specified name.
Int16 OpenObject(char* name, UInt16* handle)
{
UInt16 i = 0;
UInt16 index;
Int16 firstAvailInd;
EnterCriticalSection();
for (i = 0; i < CurrentObjects.Length; i++)
{
InternalObject* obj = (InternalObject*)GetListItem(&CurrentObjects,i);
if (obj->Name && !strcmp(name,obj->Name))
{
// Follow any symbolic links
while (obj->Flags == ObjectFlagSymLink) obj = obj->Data;
if ((obj->Flags & ObjectFlagNoShare) && IsObjectOpen(i))
{
*handle = 0;
ExitCriticalSection();
return ErrorInUse;
}
index = GetIndexOf(&CurrentObjects,obj);
if ((firstAvailInd = GetIndexOf(&OpenHandles,(void*)0xFFFF)) == -1)
{
AddListItem(&OpenHandles,(void*)index);
*handle = OpenHandles.Length;
}
else
{
ReplaceListItem(&OpenHandles,firstAvailInd,(void*)index);
*handle = firstAvailInd + 1;
}
obj->OpenHandles++;
ExitCriticalSection();
return ErrorSuccess;
}
else if ((strstr(name,obj->Name) == name) && (strlen(name) > strlen(obj->Name)) && (name[strlen(obj->Name)] == '\\'))
{
INamespace* namespace;
Int16 ret;
if (GetInterfaceInternal(i,CodeINamespace,(void**)(&namespace)) == ErrorSuccess)
{
char* truncName = zmalloc(0x50);
char* reparseName;
UInt16 namespaceHandle;
if (!truncName)
{
ExitCriticalSection();
return ErrorOutOfMemory;
}
strcpy(truncName,&(name[strlen(obj->Name)]));
if ((ret = OpenObject(obj->Name,&namespaceHandle)) != ErrorSuccess)
{
zfree(truncName);
ExitCriticalSection();
return ret;
}
if (!(reparseName = zmalloc(0x50)))
{
ReleaseObject(namespaceHandle);
zfree(truncName);
ExitCriticalSection();
return ErrorOutOfMemory;
}
ret = namespace->CreateObject(namespaceHandle,handle,truncName,&reparseName);
ReleaseObject(namespaceHandle);
// If a reparse point was hit, reparseName contains the new location
if (ret == ErrorReparse)
{
// And begin again...
ret = OpenObject(reparseName,handle);
}
zfree(reparseName);
zfree(truncName);
ExitCriticalSection();
return ret;
}
}
}
ExitCriticalSection();
return ErrorNotFound;
}
Int16 DuplicateHandle(UInt16 handle, UInt16* newHandle)
{
UInt16 index;
EnterCriticalSection();
index = (UInt16)GetListItem(&OpenHandles, handle - 1);
AddListItem(&OpenHandles,(void*)index);
*newHandle = OpenHandles.Length;
ExitCriticalSection();
return ErrorSuccess;
}
// Releases an object and destroys it if needed.
Int16 ReleaseObject(UInt16 handle)
{
UInt16 index;
InternalObject* obj = NULL;
IGeneric* gen;
EnterCriticalSection();
index = (UInt16)GetListItem(&OpenHandles,handle-1);
if (handle == OpenHandles.Length)
{
RemoveListItem(&OpenHandles,handle-1);
}
else
{
ReplaceListItem(&OpenHandles,handle-1,(void*)0xFFFF);
}
obj = GetListItem(&CurrentObjects,index);
obj->OpenHandles--;
if (obj->OpenHandles || obj->KernelReferences)
{
ExitCriticalSection();
return ErrorSuccess;
}
// TODO: Remove this:
ExitCriticalSection();
return ErrorSuccess;
// If we have gotten here, the object is no longer
// in use (aka, no open handles exist for it).
if (!obj)
{
ExitCriticalSection();
return ErrorInvalidHandle;
}
if (!(obj->Flags | ObjectFlagPermanent)) // Objects can be made permanent
{
if (!GetInterfaceInternal(index,CodeIGeneric,(void**)&gen))
{
gen->Destroy(obj);
}
if ((obj->Flags | ObjectFlagAutoFree) && obj->Data) zfree(obj->Data); // Objects can be set to be freed by us
if (obj->Name) zfree(obj->Name);
if (index == CurrentObjects.Length - 1)
{
zfree(RemoveListItem(&CurrentObjects,index));
}
else
{
zfree(ReplaceListItem(&CurrentObjects,index,(void*)0));
}
}
ExitCriticalSection();
return ErrorSuccess;
}
// Returns the internal object data structure from a handle.
UInt16 InternalObjectFromHandle(UInt16 handle, InternalObject** obj)
{
EnterCriticalSection();
*obj = GetListItem(&CurrentObjects,((UInt16)GetListItem(&OpenHandles,handle-1)));
ExitCriticalSection();
if (!(*obj)) return ErrorUnknown;
return ErrorSuccess;
}
// Returns true or false.
Int16 IsHandleValid(UInt16 handle)
{
UInt16 length;
EnterCriticalSection();
length = OpenHandles.Length;
ExitCriticalSection();
if (!handle || handle > length) return False;
return True;
}
/*static void zstrcpy(char* dest, char* src)
{
Int8 i = 0;
while ((dest[i] = src[i])) i++;
}*/
Int16 CreateSymbolicLink(char* name, InternalObject* pointedObj)
{
InternalObject* obj = zmalloc(sizeof(InternalObject));
if (!obj) return ErrorOutOfMemory;
// Assign the object name
if (name)
{
obj->Name = zmalloc(strlen(name)+1);
if (!(obj->Name))
{
zfree(obj);
return ErrorOutOfMemory;
}
strcpy(obj->Name, name);
}
else
{
zfree(obj);
return ErrorNullArg;
}
obj->Flags = ObjectFlagSymLink;
obj->Data = pointedObj;
EnterCriticalSection();
if (pointedObj->Flags != ObjectFlagSymLink) pointedObj->KernelReferences++;
AddListItem(&CurrentObjects,obj);
ExitCriticalSection();
return ErrorSuccess;
}
Int16 ChangeSymbolicLink(char* name, InternalObject* newObj)
{
Int16 ret;
InternalObject* symObj;
if ((ret = InternalObjectFromName(name, &symObj))) return ret;
EnterCriticalSection();
if (((InternalObject*)(symObj->Data))->Flags != ObjectFlagSymLink) ((InternalObject*)(symObj->Data))->KernelReferences--;
symObj->Data = newObj;
if (newObj->Flags != ObjectFlagSymLink) newObj->KernelReferences++;
ExitCriticalSection();
return ErrorSuccess;
}
Int16 InternalObjectFromName(char* name, InternalObject** obj)
{
int i;
InternalObject* currObj;
EnterCriticalSection();
for (i = 0; i < CurrentObjects.Length; i++)
{
currObj = GetListItem(&CurrentObjects,i);
if (currObj->Name && !strcmp(currObj->Name,name))
{
*obj = currObj;
ExitCriticalSection();
return ErrorSuccess;
}
}
ExitCriticalSection();
return ErrorInvalidObject;
}
Int16 InternalObjectFromData(void* data,InternalObject** obj)
{
int i;
InternalObject* currObj;
EnterCriticalSection();
for (i = 0; i < CurrentObjects.Length; i++)
{
currObj = GetListItem(&CurrentObjects,i);
if (currObj->Data == data)
{
*obj = currObj;
ExitCriticalSection();
return ErrorSuccess;
}
}
ExitCriticalSection();
return ErrorInvalidObject;
}
Int16 NextAvailableTypeCode = 0x100;
Int16 GetUniqueTypeCode(void)
{
Int16 nextAvailable;
EnterCriticalSection();
nextAvailable = NextAvailableTypeCode++;
ExitCriticalSection();
return nextAvailable;
}