BarryServer : Git

All the code for all my projects
// BarryServer : Git / Nucleus / commit / 7a0d3efda0953d2296a36652babef51441b5ef20 / object / manager.c

// Related

Nucleus

Barry Doubly-linked object lists and copying 7a0d3ef (3 years, 3 months ago)
diff --git a/object/manager.c b/object/manager.c
index c50d45d..74b833d 100644
--- a/object/manager.c
+++ b/object/manager.c
@@ -32,7 +32,6 @@ put(void *addr)
 	__atomic_sub_fetch(&obj->type->usage, 1, __ATOMIC_RELAXED);
 	if (__atomic_sub_fetch(&obj->usage, 1, __ATOMIC_RELAXED))
 		return;
-	__atomic_sub_fetch(&obj->type->count, 1, __ATOMIC_RELAXED);
 	if (obj->type->delete)
 		obj->type->delete(obj);
 	remove(obj->type->objects, obj);
@@ -47,19 +46,32 @@ void *
 new(ObjectType *type)
 {
 	Object *obj;
-	if (type->alloc)
+	if (type->alloc && type->free)
 		obj = type->alloc();
 	else
 		obj = kmalloc(type->size);
+	init_lock(&obj->lock);
+	obj->type = type;
 	if (type->new)
 		type->new(obj);
 	if (!type->objects)
 		type->objects = create_list(type);
 	add(type->objects, obj);
-	init_lock(&obj->lock);
-	obj->type = type;
-	__atomic_add_fetch(&type->count, 1, __ATOMIC_RELAXED);
-	return get(obj);
+	/* No need to get() since it's in the global list */
+	return obj;
+}
+
+/* Copy an instance of an object */
+void *
+copy(void *addr)
+{
+	Object *parent = addr;
+	Object *child = NULL;
+	if (parent->type->copy) {
+		child = new(parent->type);
+		parent->type->copy(parent, child);
+	}
+	return child;
 }
 
 /* Lock an object */