36 lines
998 B
C
36 lines
998 B
C
// ob_object.c - executive object manager
|
|
#include <string.h>
|
|
#include <intrin.h>
|
|
#include <kernel/ob_object.h>
|
|
#include <kernel/mm_virt.h>
|
|
|
|
__declspec(dllexport) OBJECT* ObAllocateObject(OBJECT_TYPE type, size_t size) {
|
|
if (size < sizeof(OBJECT)) size = sizeof(OBJECT);
|
|
|
|
OBJECT* obj = (OBJECT *)MmAllocatePool(size);
|
|
if (!obj) return (OBJECT *)0;
|
|
|
|
memset(obj, 0, size);
|
|
obj->Type = type;
|
|
obj->RefCount = 1;
|
|
obj->Destroy = (void *)0; // caller fills this in
|
|
return obj;
|
|
}
|
|
|
|
__declspec(dllexport) void ObReferenceObject(OBJECT* object) {
|
|
if (!object) return;
|
|
_InterlockedIncrement(&object->RefCount);
|
|
}
|
|
|
|
__declspec(dllexport) void ObDereferenceObject(OBJECT* object) {
|
|
if (!object) return;
|
|
long prev = _InterlockedDecrement(&object->RefCount);
|
|
if (prev == 0) {
|
|
// Reference count hit zero - run the destructor then free.
|
|
if (object->Destroy) {
|
|
object->Destroy(object);
|
|
}
|
|
MmFreePool(object);
|
|
}
|
|
}
|