void Object_destroy(Object *object) { if(object != NULL) { if(object->immortal == 1) return; switch(object->type) { case tString: bdestroy(object->value.string); break; case tFunction: if (object->native) { NativeMethod_destroy((NativeMethod*)object->value.other); } else { VMMethod_destroy((VMMethod*)object->value.other); } break; case tArray: { DArray *array = (DArray*)object->value.other; int i=0; for(i=0; i < DArray_count(array); i++) { release((Object*)DArray_at(array, i)); } DArray_destroy(array); break; } case tHash: { Hashmap *map = (Hashmap*)object->value.other; Hashmap_traverse(map, delete_object_node); Hashmap_destroy(map); break; } default: break; } if(object->slots) { Hashmap_traverse(object->slots, delete_node); Hashmap_destroy(object->slots); } if(object->parent) { // FIXME: Releasing toplevel Object causes segfault :( /* printf("Me ("); */ /* Object_print(object); */ /* printf(") I am releasing my parent ("); */ /* Object_print(object->parent); */ /* printf(")\\n"); */ /* release(object->parent); */ } free(object); } }
void Object_destroy_immortal(Object *object) { if(object != NULL) { if(object->type == tString) { bdestroy(object->value.string); } if(object->slots) { Hashmap_traverse(object->slots, delete_node); Hashmap_destroy(object->slots); } free(object); } }
void PQueue_destroy(PQueue *p_queue) { size_t i; if(p_queue) { Hashmap_destroy(p_queue->map); for(i = 0; i < p_queue->heap->size; i++) { free(p_queue->heap->items[i]); } BHeap_destroy(p_queue->heap); free(p_queue); } }
Hashmap* Hashmap_create(Hashmap_compare compare, Hashmap_hash hash) { Hashmap* map = calloc(1, sizeof(Hashmap)); check_mem(map); map->compare = compare == NULL ? default_compare : compare; map->hash = hash == NULL ? default_hash : hash; map->buckets = DArray_create(sizeof(DArray*), DEFAULT_NUMBER_OF_BUCKETS); map->buckets->end = map->buckets->max; // fake out expanding it check_mem(map->buckets); return map; error: if(map) Hashmap_destroy(map); return NULL; }
void Object_destroy(Object *object) { switch(object->type) { case tString: bdestroy(object->value.as_string); break; case tArray: { DArray *ary = object->value.as_array; Object *obj = NULL; while((obj = (Object*)DArray_pop(ary)) != NULL) { Object_destroy(obj); } break; } case tHash: { Hashmap_destroy(object->value.as_hash); } default: break; } free(object); }
char *test_destroy() { Hashmap_destroy(map); return NULL; }