// SPDX-License-Identifier: MIT // Copyright (c) 2023 John Watts and the LuminaSensum contributors #include "object.h" #include "error.h" #include "vm.h" #include <stdatomic.h> #include <stddef.h> #include <stdlib.h> #include <string.h> struct object { struct object_class *class_data; atomic_int ref_count; char priv_data[]; }; Object object_none(void) { return (Object)NULL; } Object object_create(VmState state, struct object_class *class, int priv_size) { (void)state; vm_abort_if(state, priv_size < 1, "object_create: priv_size too small"); size_t size = sizeof(struct object) + priv_size; struct object *obj = calloc(size, 1); vm_abort_if(state, obj == NULL, "object_create: not enough memory for an objects"); obj->class_data = class; obj->ref_count = 1; return (Object)obj; } char *object_priv(VmState state, Object object, struct object_class *class) { (void)state; vm_abort_if( state, object == object_none(), "object_priv: no priv on none"); struct object *obj = (struct object *)object; vm_abort_if(state, obj->class_data != class, "object_priv: incompatible class"); return obj->priv_data; } void object_hold(VmState state, Object obj) { (void)state; if (obj == object_none()) return; atomic_fetch_add_explicit(&obj->ref_count, 1, memory_order_relaxed); } void object_drop(VmState state, Object *objptr) { if (*objptr == object_none()) return; Object obj = *objptr; atomic_int count = atomic_fetch_sub_explicit( &obj->ref_count, 1, memory_order_relaxed); if (count == 1) { // We were the last user of the object, clean it up obj->class_data->cleanup(state, obj); free(obj); } *objptr = object_none(); } void dispatch_call(VmState state, Object obj, const char *name) { if (obj == object_none()) vm_abort_msg(state, "dispatch_call: cannot dispatch on none"); struct object_call *call = obj->class_data->calls; while (call->name != NULL) { if (strcmp(call->name, name) == 0) { call->handler(state, obj, call->priv); return; } ++call; } vm_abort_msg(state, "dispatch_call: no call found to dispatch"); } static struct object_class object_list_class; struct object_list { int length; Object elems[]; }; Object object_list_create(VmState state, int length) { size_t elem_size = sizeof(Object) * length; size_t size = sizeof(struct object_list) + elem_size; Object obj = object_create(state, &object_list_class, size); struct object_list *list = (struct object_list *)object_priv( state, obj, &object_list_class); list->length = length; for (int i = 0; i < length; ++i) list->elems[i] = object_none(); return obj; } int object_list_length(VmState state, Object obj) { struct object_list *list = (struct object_list *)object_priv( state, obj, &object_list_class); return list->length; } void object_list_set(VmState state, Object obj, int index, Object new) { struct object_list *list = (struct object_list *)object_priv( state, obj, &object_list_class); vm_abort_if(state, index < 0, "object_list_get: index too small"); vm_abort_if(state, index >= list->length, "object_list_get: index too large"); object_drop(state, &(list->elems[index])); list->elems[index] = new; } Object object_list_get(VmState state, Object obj, int index) { struct object_list *list = (struct object_list *)object_priv( state, obj, &object_list_class); vm_abort_if(state, index < 0, "object_list_get: index too small"); vm_abort_if(state, index >= list->length, "object_list_get: index too large"); Object new = list->elems[index]; object_hold(state, new); return new; } static void object_list_cleanup(VmState state, Object obj) { struct object_list *list = (struct object_list *)object_priv( state, obj, &object_list_class); for (int i = 0; i < list->length; ++i) object_drop(state, &(list->elems[i])); } static struct object_call object_list_calls[] = {{.name = NULL, /* end */}}; static struct object_class object_list_class = { .cleanup = object_list_cleanup, .calls = &object_list_calls[0], };