libBLOC
Barry Restructuring object core + portable locking 22e0342 (2 years, 11 months ago)
diff --git a/object/object.c b/object/object.c
new file mode 100644
index 0000000..39051ee
--- /dev/null
+++ b/object/object.c
@@ -0,0 +1,79 @@
+/*
+ * Copyright (C) 2023 Barry
+ *
+ * This file is part of Barry's Little Objects in C Library (libBLOC).
+ *
+ * libBLOC is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ *
+ * libBLOC is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTIBILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * libBLOC. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+/*
+ * This file contains the core of the object library. It implements the basic
+ * operations each object needs and leaves the rest up to the respective
+ * program that implements the object. The object library is a resource
+ * manager which should help improve memory safety within a program. It
+ * reference counts each object and controls their instantiation and deletion.
+ */
+
+#include <string.h>
+#include <BLOC/object.h>
+#include "../assert.h"
+#include "object.h"
+
+/* Obtain a reference to an object */
+void *
+obj_get(void *addr)
+{
+ struct ObjectHeader *obj = object_header(addr);
+ ASSERT(obj->magic == OBJECT_MAGIC);
+ __atomic_add_fetch(&obj->type->usage, 1, __ATOMIC_RELAXED);
+ __atomic_add_fetch(&obj->usage, 1, __ATOMIC_RELAXED);
+ return addr;
+}
+
+/* Release a reference to an object */
+void
+obj_put(void *addr)
+{
+ struct ObjectHeader *obj = object_header(addr);
+ ASSERT(obj->magic == OBJECT_MAGIC);
+ __atomic_sub_fetch(&obj->type->usage, 1, __ATOMIC_RELAXED);
+ if (__atomic_sub_fetch(&obj->usage, 1, __ATOMIC_RELAXED))
+ return;
+
+ /* Free object */
+ obj_lock(addr);
+ if (obj->type->delete)
+ obj->type->delete(addr);
+ obj_unlock(addr);
+ obj->magic = 0;
+ free(obj);
+}
+
+/* Create a new instance of an object type */
+void *
+obj_new(struct ObjectType *type)
+{
+ void *body;
+ struct ObjectHeader *obj;
+ obj = malloc(sizeof(struct ObjectType) + type->size);
+ body = (void *) (obj + 1);
+ memset(body, 0, type->size);
+
+ obj->magic = OBJECT_MAGIC;
+ obj->type = type;
+ if (type->new)
+ type->new(body);
+
+ return obj_get(body);
+}
diff --git a/object/object.h b/object/object.h
new file mode 100644
index 0000000..4b10de5
--- /dev/null
+++ b/object/object.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2023 Barry
+ *
+ * This file is part of Barry's Little Objects in C Library (libBLOC).
+ *
+ * libBLOC is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ *
+ * libBLOC is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTIBILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * libBLOC. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifndef OBJECT_H
+#define OBJECT_H
+
+typedef _Atomic int spinlock_t;
+
+/* Structure for an object header */
+struct ObjectHeader {
+ unsigned int magic;
+ struct ObjectType *type;
+ refcount_t usage;
+
+ spinlock_t lock;
+ unsigned int owner;
+ refcount_t locks;
+};
+#define OBJECT_MAGIC 0x01020304
+
+/* Get a pointer to an object's header */
+static struct ObjectHeader *
+object_header(void *addr)
+{
+ return (addr - sizeof(struct ObjectHeader));
+}
+
+#endif
diff --git a/object/object_lock.c b/object/object_lock.c
new file mode 100644
index 0000000..0cdc3ad
--- /dev/null
+++ b/object/object_lock.c
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2023 Barry
+ *
+ * This file is part of Barry's Little Objects in C Library (libBLOC).
+ *
+ * libBLOC is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ *
+ * libBLOC is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTIBILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * libBLOC. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifdef __linux__
+# define _GNU_SOURCE
+# include <unistd.h>
+#endif
+
+#include <BLOC/object.h>
+#include "../assert.h"
+#include "object.h"
+
+/* Get a unique identifier for the current thread */
+static unsigned int
+_obj_get_thread_id(void)
+{
+#if defined(__linux__)
+ return (unsigned int) gettid();
+#else
+# error "No _obj_get_thread_id() for target platform"
+#endif
+}
+
+/* Lock an object */
+void
+obj_lock(void *addr)
+{
+ struct ObjectHeader *obj = object_header(addr);
+ ASSERT(obj->magic == OBJECT_MAGIC);
+ unsigned int owner = _obj_get_thread_id();
+ if (obj->owner != owner) {
+ while (__atomic_test_and_set(&(obj->lock), __ATOMIC_ACQUIRE))
+ __builtin_ia32_pause();
+ obj->owner = owner;
+ }
+ __atomic_add_fetch(&obj->locks, 1, __ATOMIC_RELAXED);
+}
+
+/* Unlock an object */
+void
+obj_unlock(void *addr)
+{
+ struct ObjectHeader *obj = object_header(addr);
+ ASSERT(obj->magic == OBJECT_MAGIC);
+ if (__atomic_sub_fetch(&obj->locks, 1, __ATOMIC_RELAXED))
+ return;
+ __atomic_clear(&(obj->lock), __ATOMIC_RELEASE);
+ obj->owner = 0;
+}
diff --git a/object/object_util.c b/object/object_util.c
new file mode 100644
index 0000000..f1949b4
--- /dev/null
+++ b/object/object_util.c
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2023 Barry
+ *
+ * This file is part of Barry's Little Objects in C Library (libBLOC).
+ *
+ * libBLOC is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ *
+ * libBLOC is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTIBILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * libBLOC. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <BLOC/object.h>
+#include "../assert.h"
+#include "object.h"
+
+/* Verify that a pointer points to a valid object */
+int
+obj_verify(void *addr)
+{
+ struct ObjectHeader *obj = object_header(addr);
+ return (int) (obj->magic == OBJECT_MAGIC);
+}
+
+/* Count the number of references an object has */
+unsigned int
+obj_usage(void *addr)
+{
+ struct ObjectHeader *obj = object_header(addr);
+ ASSERT(obj->magic == OBJECT_MAGIC);
+ return (unsigned int) obj->usage;
+}
+
+/* Return the object type of an object */
+struct ObjectType *
+obj_type(void *addr)
+{
+ struct ObjectHeader *obj = object_header(addr);
+ ASSERT(obj->magic == OBJECT_MAGIC);
+ return obj->type;
+}