libBLOC
Barry Restructuring object core + portable locking 22e0342 (2 years, 11 months ago)
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;
+}