diff options
| author | Franklin Wei <frankhwei536@gmail.com> | 2016-01-31 19:53:45 -0500 |
|---|---|---|
| committer | Franklin Wei <frankhwei536@gmail.com> | 2016-01-31 19:53:45 -0500 |
| commit | 0730fc3924dd4e04efbe51287d1d69850404d05f (patch) | |
| tree | 495d79d0dc26e39c9065c6ceb7d16b9a3e76561d /src/obj.c | |
| parent | 8405274a91e3652ee98a423608a8496ead1edc05 (diff) | |
| download | netcosm-0.5.0-rc1.zip netcosm-0.5.0-rc1.tar.gz netcosm-0.5.0-rc1.tar.bz2 netcosm-0.5.0-rc1.tar.xz | |
bump version to 0.5.0-rc10.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
Diffstat (limited to 'src/obj.c')
| -rw-r--r-- | src/obj.c | 21 |
1 files changed, 12 insertions, 9 deletions
@@ -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) |