comparison libmlk-rpg/mlk/rpg/map-file.c @ 442:9c3b3935f0aa

core: rework allocations
author David Demelier <markand@malikania.fr>
date Thu, 03 Nov 2022 21:09:25 +0100
parents 25a56ca53ac2
children 773a082f0b91
comparison
equal deleted inserted replaced
441:31c1bbc33813 442:9c3b3935f0aa
64 64
65 /* 65 /*
66 * The next line after a layer declaration is a list of plain integer 66 * The next line after a layer declaration is a list of plain integer
67 * that fill the layer tiles. 67 * that fill the layer tiles.
68 */ 68 */
69 if (!(ctx->mf->layers[layer_type].tiles = mlk_alloc_array0(amount, sizeof (unsigned short)))) 69 if (!(ctx->mf->layers[layer_type].tiles = mlk_alloc_new0(amount, sizeof (unsigned short))))
70 return -1; 70 return -1;
71 71
72 for (int tile; fscanf(ctx->fp, "%d\n", &tile) && current < amount; ++current) 72 for (int tile; fscanf(ctx->fp, "%d\n", &tile) && current < amount; ++current)
73 ctx->mf->layers[layer_type].tiles[current] = tile; 73 ctx->mf->layers[layer_type].tiles[current] = tile;
74 74
273 .map = map, 273 .map = map,
274 }; 274 };
275 int ret = 0; 275 int ret = 0;
276 276
277 memset(map, 0, sizeof (*map)); 277 memset(map, 0, sizeof (*map));
278 mlk_alloc_pool_init(&file->blocks, sizeof (*map->blocks), NULL); 278 mlk_alloc_pool_init(&file->blocks, 16, sizeof (*map->blocks), NULL);
279 279
280 if (!(ctx.fp = fopen(path, "r"))) 280 if (!(ctx.fp = fopen(path, "r")))
281 goto fail; 281 goto fail;
282 if ((ret = parse(&ctx, path)) < 0 || (ret = check(map)) < 0) 282 if ((ret = parse(&ctx, path)) < 0 || (ret = check(map)) < 0)
283 goto fail; 283 goto fail;
299 void 299 void
300 map_file_finish(struct map_file *file) 300 map_file_finish(struct map_file *file)
301 { 301 {
302 assert(file); 302 assert(file);
303 303
304 free(file->layers[0].tiles); 304 mlk_alloc_free(file->layers[0].tiles);
305 free(file->layers[1].tiles); 305 mlk_alloc_free(file->layers[1].tiles);
306 free(file->layers[2].tiles); 306 mlk_alloc_free(file->layers[2].tiles);
307 307
308 tileset_file_finish(&file->tileset_file); 308 tileset_file_finish(&file->tileset_file);
309 mlk_alloc_pool_finish(&file->blocks); 309 mlk_alloc_pool_finish(&file->blocks);
310 310
311 memset(file, 0, sizeof (*file)); 311 memset(file, 0, sizeof (*file));