From 0730fc3924dd4e04efbe51287d1d69850404d05f Mon Sep 17 00:00:00 2001 From: Franklin Wei Date: Sun, 31 Jan 2016 19:53:45 -0500 Subject: bump version to 0.5.0-rc1 * implements objects using reference counts rather than copying * implements both room-local and global verbs * refactors the world_* functions into a separate module * numerous other changes --- src/obj.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'src/obj.c') diff --git a/src/obj.c b/src/obj.c index 0f9c554..20714f7 100644 --- a/src/obj.c +++ b/src/obj.c @@ -44,6 +44,8 @@ struct object_t *obj_new(const char *class_name) struct object_t *obj = calloc(1, sizeof(struct object_t)); + obj->refcount = 1; + obj->class = hash_lookup(obj_class_map, class_name); if(!obj->class) { @@ -80,23 +82,24 @@ struct object_t *obj_read(int fd) struct object_t *obj_dup(struct object_t *obj) { - struct object_t *new = calloc(1, sizeof(struct object_t)); - memcpy(new, obj, sizeof(*new)); - new->name = strdup(obj->name); - new->userdata = obj->class->hook_clone(obj->userdata); - return new; + ++obj->refcount; + return obj; } void obj_free(void *ptr) { struct object_t *obj = ptr; debugf("Freeing obj %s\n", obj->name); + --obj->refcount; - if(obj->class->hook_destroy) - obj->class->hook_destroy(obj); + if(!obj->refcount) + { + if(obj->class->hook_destroy) + obj->class->hook_destroy(obj); - free(obj->name); - free(obj); + free(obj->name); + free(obj); + } } void obj_shutdown(void) -- cgit v1.1