36 lines
953 B
C
36 lines
953 B
C
// ob_object.c - executive object manager
|
|
#include <string.h>
|
|
#include <intrin.h>
|
|
#include <ob_object.h>
|
|
#include <mm_virt.h>
|
|
|
|
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;
|
|
}
|
|
|
|
void ObReferenceObject(OBJECT* object) {
|
|
if (!object) return;
|
|
_InterlockedIncrement(&object->RefCount);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|