commit 4521aafeb01cddae0dcb325174ea89d5d726396c
parent fb18494b2442f3180dca9d868f8bb428f1d1e26f
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date: Mon, 2 Jan 2023 15:57:10 +0100
Decrease building struct memory footprint
Diffstat:
6 files changed, 70 insertions(+), 43 deletions(-)
diff --git a/src/cg_building.h b/src/cg_building.h
@@ -32,6 +32,24 @@ struct scpr_polygon;
struct scad_geometry;
struct mem_allocator;
+/* A type to store the functors of a constructive mode */
+struct constructive_mode_functors {
+ res_T (*init)
+ (struct building* building,
+ struct mem_allocator* allocator,
+ struct parsed_city_building* parsed_data,
+ struct htable_parameter_set* catalog);
+ res_T (*build_cad)
+ (struct building* building, struct mem_allocator* allocator, void** cad);
+ res_T (*build_footprint)
+ (struct building* building, struct mem_allocator* allocator,
+ struct scad_geometry** footprint);
+ res_T (*export_stl)
+ (void* cad, const int binary);
+ res_T (*release_cad)
+ (struct mem_allocator* allocator, void* cad);
+};
+
/* A type to give an ID to constructive modes.
* Add a new entry for each new constructive mode. */
enum constructive_mode_type {
@@ -79,32 +97,18 @@ hash_str(const struct str* a)
/* The type of buildings as described in the city description */
struct building {
+ /* constructive mode */
+ struct constructive_mode_functors* functors;
+ enum constructive_mode_type constructive_mode;
+
/* generic constructive mode data */
+ int name_initialized;
struct str name;
- struct str dataset_name;
- int names_initialized;
- enum constructive_mode_type constructive_mode;
double height;
struct scpr_polygon* pg;
/* specific data depending to the constructive mode */
void* data;
-
- /* functors depending to the constructive mode */
- res_T (*init)
- (struct building* building,
- struct mem_allocator* allocator,
- struct parsed_city_building* parsed_data,
- struct htable_parameter_set* catalog);
- res_T (*build_cad)
- (struct building* building, struct mem_allocator* allocator, void** cad);
- res_T (*build_footprint)
- (struct building* building, struct mem_allocator* allocator,
- struct scad_geometry** footprint);
- res_T (*export_stl)
- (void* cad, const int binary);
- res_T (*release_cad)
- (struct mem_allocator* allocator, void* cad);
};
#endif /* BUILDING_H */
diff --git a/src/cg_parsing.c b/src/cg_catalog_parsing.c
diff --git a/src/cg_parsing.h b/src/cg_catalog_parsing.h
diff --git a/src/cg_city.c b/src/cg_city.c
@@ -114,9 +114,8 @@ city_release(struct city* city)
ERR(scpr_polygon_ref_put(building->pg));
- if(building->names_initialized) {
+ if(building->name_initialized) {
str_release(&building->name);
- str_release(&building->dataset_name);
}
}
@@ -148,10 +147,10 @@ city_cad_build(struct city* city)
struct building* building = city->buildings + i;
struct data_cad_cmode_0* cad = NULL;
/* create building */
- ERR(building->build_cad(building, city->allocator, (void**)&cad));
+ ERR(building->functors->build_cad(building, city->allocator, (void**)&cad));
ERR(scad_scene_mesh());
- ERR(building->export_stl(cad, city->binary_export));
- ERR(building->release_cad(city->allocator, cad));
+ ERR(building->functors->export_stl(cad, city->binary_export));
+ ERR(building->functors->release_cad(city->allocator, cad));
ERR(scad_scene_clear());
}
@@ -184,7 +183,7 @@ city_ground_build(struct city* city)
struct building* building = city->buildings + i;
struct scad_geometry** footprint = ground.footprints + i;
/* create building footprint */
- ERR(building->build_footprint(building, city->allocator, footprint));
+ ERR(building->functors->build_footprint(building, city->allocator, footprint));
}
ERR(ground_build_cad(city, &ground));
diff --git a/src/cg_constructive_mode_0.c b/src/cg_constructive_mode_0.c
@@ -395,26 +395,37 @@ init_model0
{
res_T res = RES_OK;
struct parameter_set* params;
+ struct str dataset_name;
+ int name_initialized = 0;
+ static struct constructive_mode_functors functors_0
+ = {
+ &init_model0,
+ &build_cad_model0,
+ &build_footprint_model0,
+ &export_stl_model0,
+ &release_cad_model0
+ };
(void)parsed_data;
- building->init = &init_model0;
- building->build_cad = &build_cad_model0;
- building->export_stl = &export_stl_model0;
- building->release_cad = &release_cad_model0;
- building->build_footprint = &build_footprint_model0;
+ if(!building || !allocator || !parsed_data || !catalog) {
+ res = RES_BAD_ARG;
+ goto error;
+ }
building->constructive_mode = mode_0;
+ building->functors = &functors_0;
building->height = parsed_data->height;
str_init(allocator, &building->name);
- str_init(allocator, &building->dataset_name);
- building->names_initialized = 1;
+ building->name_initialized = 1;
+ str_init(allocator, &dataset_name);
+ name_initialized = 1;
ERR(str_set(&building->name, parsed_data->name));
- ERR(str_set(&building->dataset_name, parsed_data->dataset_name));
+ ERR(str_set(&dataset_name, parsed_data->dataset_name));
ERR(scpr_polygon_create(allocator, &building->pg));
ERR(scpr_polygon_setup_indexed_vertices(building->pg, 1, get_nverts, get_pos,
parsed_data));
- params = htable_parameter_set_find(catalog, &building->dataset_name);
+ params = htable_parameter_set_find(catalog, &dataset_name);
if (params == NULL) {
res = RES_BAD_ARG;
goto error;
@@ -430,6 +441,7 @@ init_model0
building->data = params->data;
exit:
+ if(name_initialized) str_release(&dataset_name);
return res;
error:
goto exit;
diff --git a/src/cg_constructive_mode_1.c b/src/cg_constructive_mode_1.c
@@ -1209,26 +1209,37 @@ init_model1
{
res_T res = RES_OK;
struct parameter_set* params;
+ struct str dataset_name;
+ int name_initialized = 0;
+ static struct constructive_mode_functors functors_1
+ = {
+ &init_model1,
+ &build_cad_model1,
+ &build_footprint_model1,
+ &export_stl_model1,
+ &release_cad_model1
+ };
(void) parsed_data;
- building->init = &init_model1;
- building->build_cad = &build_cad_model1;
- building->export_stl = &export_stl_model1;
- building->release_cad = &release_cad_model1;
- building->build_footprint = &build_footprint_model1;
+ if(!building || !allocator || !parsed_data ||!catalog) {
+ res = RES_BAD_ARG;
+ goto error;
+ }
building->constructive_mode = mode_1;
+ building->functors = &functors_1;
building->height = parsed_data->height;
str_init(allocator, &building->name);
- str_init(allocator, &building->dataset_name);
- building->names_initialized = 1;
+ building->name_initialized = 1;
+ str_init(allocator, &dataset_name);
+ name_initialized = 1;
ERR(str_set(&building->name, parsed_data->name));
- ERR(str_set(&building->dataset_name, parsed_data->dataset_name));
+ ERR(str_set(&dataset_name, parsed_data->dataset_name));
ERR(scpr_polygon_create(allocator, &building->pg));
ERR(scpr_polygon_setup_indexed_vertices(building->pg, 1, get_nverts, get_pos,
parsed_data));
- params = htable_parameter_set_find(catalog, &building->dataset_name);
+ params = htable_parameter_set_find(catalog, &dataset_name);
if (params == NULL) {
res = RES_BAD_ARG;
goto error;
@@ -1244,6 +1255,7 @@ init_model1
building->data = params->data;
exit:
+ if(name_initialized) str_release(&dataset_name);
return res;
error:
goto exit;