void engineDestroy() { mapDestroy(); guiDestroy(); gfxDestroy(); SDL_DestroyWindow(engine.window); TTF_Quit(); SDL_Quit(); }
// Destroy peer manager object. static void peermgtDestroy(struct s_peermgt *mgt) { int size = mapGetMapSize(&mgt->map); mapDestroy(&mgt->map); nodedbDestroy(&mgt->nodedb); authmgtDestroy(&mgt->authmgt); dfragDestroy(&mgt->dfrag); cryptoDestroy(mgt->ctx, size); free(mgt->ctx); free(mgt->data); }
// Create peer manager object. static int peermgtCreate(struct s_peermgt *mgt, const int peer_slots, const int auth_slots, struct s_nodekey *local_nodekey, struct s_dh_state *dhstate) { int tnow = utilGetTime(); const char *defaultid = "default"; struct s_peermgt_data *data_mem; struct s_crypto *ctx_mem; if((peer_slots > 0) && (auth_slots > 0) && (peermgtSetNetID(mgt, defaultid, 7))) { data_mem = malloc(sizeof(struct s_peermgt_data) * (peer_slots + 1)); if(data_mem != NULL) { ctx_mem = malloc(sizeof(struct s_crypto) * (peer_slots + 1)); if(ctx_mem != NULL) { if(cryptoCreate(ctx_mem, (peer_slots + 1))) { if(dfragCreate(&mgt->dfrag, peermgt_MSGSIZE_MIN, peermgt_FRAGBUF_COUNT)) { if(authmgtCreate(&mgt->authmgt, &mgt->netid, auth_slots, local_nodekey, dhstate)) { if(nodedbCreate(&mgt->nodedb, ((peer_slots * 8) + 1))) { if(mapCreate(&mgt->map, (peer_slots + 1), nodeid_SIZE, 1)) { mgt->nodekey = local_nodekey; mgt->data = data_mem; mgt->ctx = ctx_mem; mgt->lastconnect = tnow; mgt->rrmsg.msg = mgt->rrmsgbuf; if(peermgtInit(mgt)) { return 1; } mgt->nodekey = NULL; mgt->data = NULL; mgt->ctx = NULL; mapDestroy(&mgt->map); } nodedbDestroy(&mgt->nodedb); } authmgtDestroy(&mgt->authmgt); } dfragDestroy(&mgt->dfrag); } cryptoDestroy(ctx_mem, (peer_slots + 1)); } free(ctx_mem); } free(data_mem); } } return 0; }
static char *map_test(){ int map_len = 8; // Initialization test Map *m = mapCreate(sizeof(int), map_len, (unsigned int (*)(void *))djb2, (int(*)(void *a, void *b))strcmp); mu_assert("m == NULL", m != NULL); mu_assert("m->len != 8", m->len == 8); // Insertion tests char *key = "sprite"; int val = 33; mapInsert(m, key, strlen(key) + 1, &val); unsigned int bucket_id = djb2((unsigned char *)key) % map_len; Bucket *b = m->bs[bucket_id]; mu_assert("bucket is empty", b->tail != b->head); mu_assert("bucket->last->data != val", *(int *)b->tail->data == val); // Retrieval tests int x = 12, get_status; get_status = mapGet(m, key, &x); mu_assert("x has the wrong value", x == val); mu_assert("get_status != 0", get_status == 0); // Removal tests int y = 44, pop_status; pop_status = mapPop(m, key, &y, free); mu_assert("c has the wrong value", y == val); mu_assert("pop_status != 0", pop_status == 0); mu_assert("bucket is not empty", b->tail == b->head); mapDestroy(m, free); return 0; }
void idtDestroy(Map *imdt) { mapDestroy(imdt, free); }
/** * Deletes the symbol table and frees memory allocated for it. * @param sym_table Address to the symbol table */ void asmDestroySymTable(Map *sym_table){ mapDestroy(sym_table, free); }
LDMZ_MAP *mapLoad(FILE *fp) { LDMZ_MAP *map; LDMZ_FILE_MAP map_h; LDMZ_FILE_STRTABLE_REF *map_r; LDMZ_FILE_LAYER *map_l; LDMZ_FILE_OBJECT *object; void *buff, *tmp; char *stringdata; int i; fread((unsigned int *) &map_h, sizeof(LDMZ_FILE_MAP), 1, fp); utilBlockToHostEndian((unsigned int *) &map_h, sizeof(LDMZ_FILE_MAP) >> 2); if (map_h.magic != LDMZ_MAGIC || map_h.version != LDMZ_VERSION) { fclose(fp); return NULL; } if ((buff = malloc(map_h.strtable_zlen)) == NULL) { return NULL; } stringdata = malloc(map_h.strtable_len); map = malloc(sizeof(LDMZ_MAP)); map->stringrefs = malloc(sizeof(LDMZ_REF) * (map_h.strtable_refs_len / sizeof(LDMZ_FILE_STRTABLE_REF))); map_r = malloc(map_h.strtable_refs_len); map_l = malloc(sizeof(LDMZ_FILE_LAYER) * map_h.layers); object = malloc(map_h.objects * sizeof(LDMZ_OBJECT)); map->object = malloc(sizeof(LDMZ_OBJECT) * (map_h.objects)); map->layer = malloc(sizeof(LDMZ_LAYER) * map_h.layers); map->stringdata = stringdata; map->cam_x = map->cam_y = 0; map->layers = map_h.layers; map->objects = map_h.objects; if (!map || (!stringdata && map_h.strtable_len > 0) || (!map->stringrefs && map_h.strtable_refs_len > 0) || (!map->object && map_h.objects) || (!map->layer && map_h.layers) || (!map_r && map_h.strtable_refs_len) || (!object && map_h.objects) || (!map_l)) goto error; /* Down at the bottom of the function */ /**** STRINGS ****/ fread(buff, map_h.strtable_zlen, 1, fp); stbi_zlib_decode_buffer(stringdata, map_h.strtable_len, buff, map_h.strtable_zlen); /**** REFS ****/ if ((tmp = realloc(buff, map_h.strtable_refs_zlen)) == NULL) goto error; /* Down at the bottom of the function */ buff = tmp; fread(buff, map_h.strtable_refs_zlen, 1, fp); stbi_zlib_decode_buffer((void *) map_r, map_h.strtable_refs_len, buff, map_h.strtable_refs_zlen); utilBlockToHostEndian((unsigned int *) map_r, map_h.strtable_refs_len / 4); for (i = 0; i < (map_h.strtable_refs_len / sizeof(LDMZ_FILE_STRTABLE_REF)); i++) { if (map_r[i].key != -1) { map->stringrefs[i].key = &stringdata[map_r[i].key]; map->stringrefs[i].value = &stringdata[map_r[i].value]; } else { map->stringrefs[i].key = NULL; map->stringrefs[i].value = NULL; } } free(map_r); map_r = NULL; map->prop = &map->stringrefs[map_h.main_ref]; /**** OBJECTS ****/ if ((tmp = realloc(buff, map_h.object_zlen)) == NULL && map_h.objects > 0) { goto error; /* Down at the bottom of the function */ } buff = tmp; fread(buff, map_h.object_zlen, 1, fp); stbi_zlib_decode_buffer((void *) object, sizeof(LDMZ_OBJECT) * map_h.objects, buff, map_h.object_zlen); utilBlockToHostEndian((unsigned int *) object, (sizeof(LDMZ_OBJECT) * map_h.objects) >> 2); for (i = 0; i < map_h.objects; i++) { map->object[i].ref = &map->stringrefs[object[i].ref]; map->object[i].x = object[i].x; map->object[i].y = object[i].y; map->object[i].l = object[i].l; } free(object); object = NULL; /**** MAP LAYER ****/ if ((tmp = realloc(buff, map_h.layer_zlen)) == NULL) goto error; /* Down at the bottom of the function */ for (i = 0; i < map_h.layers; i++) map->layer[i].tilemap = NULL; buff = tmp; fread(buff, map_h.layer_zlen, 1, fp); stbi_zlib_decode_buffer((void *)map_l, sizeof(LDMZ_FILE_LAYER) * map_h.layers, buff, map_h.layer_zlen); utilBlockToHostEndian((unsigned int *) map_l, (sizeof(LDMZ_FILE_LAYER) * map_h.layers) >> 2); for (i = 0; i < map_h.layers; i++) { map->layer[i].offset_x = map_l[i].layer_offset_x; map->layer[i].offset_y = map_l[i].layer_offset_y; map->layer[i].tile_w = map_l[i].tile_w; map->layer[i].tile_h = map_l[i].tile_h; map->layer[i].ref = &map->stringrefs[map_l[i].prop_ref]; if ((tmp = realloc(buff, map_l[i].layer_zlen)) == NULL) goto error; /* Down at the bottom of the function */ buff = tmp; if ((map->layer[i].tilemap = mapNewTilemap(map_l[i].layer_w, map_l[i].layer_h)) == NULL) goto error; fread(buff, map_l[i].layer_zlen, 1, fp); stbi_zlib_decode_buffer((void *) map->layer[i].tilemap->data, map_l[i].layer_w * map_l[i].layer_h * 4, buff, map_l[i].layer_zlen); utilBlockToHostEndian(map->layer[i].tilemap->data, map_l[i].layer_w * map_l[i].layer_h); } free(map_l); free(buff); return map; error: /* I know... This is my first goto in C. Ever. <.< */ for (i = 0; i < map_h.layers; i++) mapDeleteTilemap(map->layer[i].tilemap); free(map_l); free(map_r); free(object); free(buff); mapDestroy(map); return NULL; }