Nucleus
Barry Basic object manager 5201ba5 (3 years, 3 months ago)
diff --git a/include/nucleus/object.h b/include/nucleus/object.h
new file mode 100644
index 0000000..bcd7c9a
--- /dev/null
+++ b/include/nucleus/object.h
@@ -0,0 +1,25 @@
+#ifndef _NUCLEUS_OBJECT_H
+#define _NUCLEUS_OBJECT_H
+
+typedef struct ObjectType ObjectType;
+typedef struct Object Object;
+
+/* Object Type */
+struct ObjectType {
+ unsigned int count;
+ unsigned int usage;
+ void *(*new)(void);
+ void (*delete)(Object *);
+};
+
+/* Object */
+struct Object {
+ ObjectType *type;
+ unsigned int usage;
+};
+
+void *get(void *addr);
+void put(void *addr);
+void *new(ObjectType *type);
+
+#endif
diff --git a/object/manager.c b/object/manager.c
new file mode 100644
index 0000000..a0b0c8b
--- /dev/null
+++ b/object/manager.c
@@ -0,0 +1,41 @@
+/*
+ * This is the Object Manager. It implements the basic operations each object
+ * needs and leaves the rest up to the respective subsystem that implements that
+ * object. The object manager is a resource manager which should help improve
+ * memory safety within the kernel. It reference counts each object and
+ * controls their instantiation and deletion.
+ */
+
+#include "object.h"
+
+/* Obtain a reference to an object */
+void *
+get(void *addr)
+{
+ Object *obj = addr;
+ obj->type->usage++;
+ obj->usage++;
+ return addr;
+}
+
+/* Release a reference to an object */
+void
+put(void *addr)
+{
+ Object *obj = addr;
+ obj->type->usage--;
+ if (--obj->usage)
+ return;
+ obj->type->count--;
+ obj->type->delete(obj);
+}
+
+/* Create a new instance of an object */
+void *
+new(ObjectType *type)
+{
+ Object *obj = type->new();
+ obj->type = type;
+ type->count++;
+ return get(obj);
+}