libBLOC
Barry Adding object metadata functions c5a8d16 (2 years, 11 months ago)
diff --git a/include/BLOC/object.h b/include/BLOC/object.h
index 9c221f5..64d685c 100644
--- a/include/BLOC/object.h
+++ b/include/BLOC/object.h
@@ -33,10 +33,12 @@ struct ObjectType {
void (*delete)(void *);
};
+int obj_verify(void *addr);
unsigned int obj_usage(void *addr);
void *obj_get(void *addr);
void obj_put(void *addr);
void *obj_new(struct ObjectType *type);
+struct ObjectType *obj_type(void *addr);
#define obj_drop(v) ({ obj_put(v); v = NULL; })
#define obj_swap(v,o) ({ obj_put(v); v = obj_get(o); })
diff --git a/iterator/iterator_relative.c b/iterator/iterator_relative.c
index d719718..0a6b962 100644
--- a/iterator/iterator_relative.c
+++ b/iterator/iterator_relative.c
@@ -34,7 +34,7 @@ iterator_next(Iterator *iter)
return iter->entry->obj;
}
-/* Get the prev iterable element */
+/* Get the previous iterable element */
void *
iterator_prev(Iterator *iter)
{
diff --git a/list/list.c b/list/list.c
index 888ff12..c5ff224 100644
--- a/list/list.c
+++ b/list/list.c
@@ -19,7 +19,7 @@
/*
* This file implements object Lists. Objects can be a part of multiple lists
- * that can be managed automatically by the library. This prevents subsystems
+ * that can be managed automatically by the library. This prevents programs
* from having to implement their own object lists for sub-objects. Lists may
* be created with a specific Object Type, and will not accept objects that are
* not of that type. If a list does not have a type, it will accept objects of
@@ -29,7 +29,6 @@
#include <BLOC/object.h>
#include <BLOC/list.h>
#include "../assert.h"
-#include "../object.h"
#include "list.h"
static void _list_new(void *);
diff --git a/list/list_add.c b/list/list_add.c
index 276a158..ad5ddec 100644
--- a/list/list_add.c
+++ b/list/list_add.c
@@ -20,7 +20,6 @@
#include <BLOC/object.h>
#include <BLOC/list.h>
#include "../assert.h"
-#include "../object.h"
#include "list.h"
/* Add an object to a list */
@@ -29,9 +28,8 @@ list_add(List *list, void *obj)
{
ASSERT(list);
ASSERT(obj);
- struct ObjectHeader *header = object_header(obj);
- ASSERT(header->magic == OBJECT_MAGIC);
- if (list->type && list->type != header->type)
+ ASSERT(obj_verify(obj));
+ if (list->type && list->type != obj_type(obj))
return;
struct IterableEntry *entry;
diff --git a/list/list_remove.c b/list/list_remove.c
index 767fb09..8aeac63 100644
--- a/list/list_remove.c
+++ b/list/list_remove.c
@@ -20,7 +20,6 @@
#include <BLOC/object.h>
#include <BLOC/list.h>
#include "../assert.h"
-#include "../object.h"
#include "list.h"
/* Remove an object from a list */
@@ -29,11 +28,10 @@ list_remove(List *list, void *obj)
{
ASSERT(list);
ASSERT(obj);
- struct ObjectHeader *header = object_header(obj);
- ASSERT(header->magic == OBJECT_MAGIC);
+ ASSERT(obj_verify(obj));
if (!list->header.start)
return;
- if (list->type && list->type != header->type)
+ if (list->type && list->type != obj_type(obj))
return;
obj_lock(list);
diff --git a/object.c b/object.c
index f51dbbd..52fbe89 100644
--- a/object.c
+++ b/object.c
@@ -33,11 +33,20 @@
#include "../assert.h"
#include "object.h"
-/* Count the number of reference an object has */
+/* 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;
}
@@ -89,6 +98,15 @@ obj_new(struct ObjectType *type)
return obj_get(body);
}
+/* 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;
+}
+
/* Lock an object */
void
obj_lock(void *addr)
diff --git a/tree/tree.c b/tree/tree.c
index 1bffd72..6e23c5a 100644
--- a/tree/tree.c
+++ b/tree/tree.c
@@ -28,7 +28,6 @@
#include <BLOC/object.h>
#include <BLOC/tree.h>
#include "../assert.h"
-#include "../object.h"
#include "tree.h"
static void _tree_new(void *);
diff --git a/tree/tree_add.c b/tree/tree_add.c
index 67397ba..bca8c7e 100644
--- a/tree/tree_add.c
+++ b/tree/tree_add.c
@@ -20,7 +20,6 @@
#include <BLOC/object.h>
#include <BLOC/tree.h>
#include "../assert.h"
-#include "../object.h"
#include "tree.h"
/* Add a node to a tree recursively */
@@ -72,9 +71,8 @@ tree_add(Tree *tree, void *obj)
{
ASSERT(tree);
ASSERT(obj);
- struct ObjectHeader *header = object_header(obj);
- ASSERT(header->magic == OBJECT_MAGIC);
- if (tree->type && tree->type != header->type)
+ ASSERT(obj_verify(obj));
+ if (tree->type && tree->type != obj_type(obj))
return;
struct TreeNode *node;
diff --git a/tree/tree_remove.c b/tree/tree_remove.c
index 6510a03..6f1ad90 100644
--- a/tree/tree_remove.c
+++ b/tree/tree_remove.c
@@ -48,7 +48,7 @@ _tree_remove_r(Tree *t, struct TreeNode *parent, struct TreeNode *child)
} else if (t->compare(parent->header.obj, child->header.obj) < 0) {
parent->right = _tree_remove_r(t, parent->right, child);
} else {
- /* Less that two children: adopt child if available */
+ /* Less than two children: adopt child if available */
if (!child->left)
return child->right;
if (!child->right)
@@ -75,11 +75,10 @@ tree_remove(Tree *tree, void *obj)
{
ASSERT(tree);
ASSERT(obj);
- struct ObjectHeader *header = object_header(obj);
- ASSERT(header->magic == OBJECT_MAGIC);
+ ASSERT(obj_verify(obj));
if (!tree->header.start)
return;
- if (tree->type && tree->type != header->type)
+ if (tree->type && tree->type != obj_type(obj))
return;
obj_lock(tree);
@@ -93,7 +92,7 @@ tree_remove(Tree *tree, void *obj)
/* Unlink from tree */
tree->root = _tree_remove_r(tree, tree->root, node);
- /* Unlink from linear list*/
+ /* Unlink from linear list */
if (tree->header.start == &node->header)
tree->header.start = node->header.next;
if (tree->header.end == &node->header)