void *tort_object_alloc(tort_mtable *mtable, size_t size) { void *ptr; size_t alloc_size = sizeof(tort_header) + size; /* Save the instance size in the mtable. */ if ( mtable ) { if ( ! mtable->instance_size ) mtable->instance_size = size; assert(size >= mtable->instance_size); } else { // fprintf(stderr, " tort_object_alloc: no mtable for %lu\n", (unsigned long) alloc_size); } ptr = _tort_object_alloc(mtable, alloc_size); if ( ptr ) { extern unsigned long _tort_alloc_id; if ( ! _tort_alloc_bzero ) bzero(ptr, alloc_size); ptr += sizeof(tort_header); tort_h_ref(ptr)->mtable = mtable; tort_h_ref(ptr)->applyf = _tort_m_object___cannot_apply; ++ _tort_alloc_id; // fprintf(stderr, " alloc %p[%llu] %s %lu\n", ptr, (unsigned long long) size, tort_object_name(mtable), _tort_alloc_id); TORT_GC_STAT(object_alloc_n ++); TORT_GC_STAT(object_alloc_bytes += alloc_size); } return ptr; }
tort_mtable* tort_mtable_set_class_delegate(tort_mtable *obj_mt, tort_v delegate) { tort_mtable *cls_mt; tort_v cls_delegate; if ( delegate == 0 && tort_nil != 0 ) delegate = tort_nil; cls_mt = tort_h_ref(obj_mt)->mtable; cls_delegate = delegate != tort_nil ? tort_h_ref(delegate)->mtable : tort_nil; _tort_m_mtable__delegateSET(tort_ta obj_mt, delegate); _tort_m_mtable__delegateSET(tort_ta cls_mt, cls_delegate); return obj_mt; }
tort_v _tort_allocate(tort_tp tort_v mtable, size_t size #if TORT_ALLOC_DEBUG ,const char *alloc_file, int alloc_line #endif ) { tort_v val = _tort_M_object___allocate(tort_ta mtable, tort_i(size)); #if TORT_ALLOC_DEBUG tort_h_ref(val)->alloc_file = alloc_file; tort_h_ref(val)->alloc_line = alloc_line; tort_h_ref(val)->alloc_id = _tort_alloc_id; #endif return val; }
tort_mtable* tort_mtable_new_class(tort_v delegate) { tort_mtable *obj_mt = tort_mtable_new_0(delegate); tort_mtable *cls_mt = tort_mtable_new_0(0); tort_h_ref(obj_mt)->mtable = cls_mt; tort_mtable_set_class_delegate(obj_mt, delegate); return obj_mt; }
tort_v tort_add_class_method(tort_v mtable, const char *name, void *applyf) { return tort_add_method(tort_h_ref(mtable)->mtable, name, applyf); }