[Bf-blender-cvs] [c297bd9] mesh-transfer-data: Mesh islands: use MemArena to handle memory here as well.

Bastien Montagne noreply at git.blender.org
Wed Oct 15 13:11:21 CEST 2014


Commit: c297bd971f978d670fc86a68efd1974568ae21a8
Author: Bastien Montagne
Date:   Tue Oct 14 16:11:38 2014 +0200
Branches: mesh-transfer-data
https://developer.blender.org/rBc297bd971f978d670fc86a68efd1974568ae21a8

Mesh islands: use MemArena to handle memory here as well.

===================================================================

M	source/blender/blenkernel/BKE_mesh_mapping.h
M	source/blender/blenkernel/intern/mesh_mapping.c

===================================================================

diff --git a/source/blender/blenkernel/BKE_mesh_mapping.h b/source/blender/blenkernel/BKE_mesh_mapping.h
index cd8c800..becdfc6 100644
--- a/source/blender/blenkernel/BKE_mesh_mapping.h
+++ b/source/blender/blenkernel/BKE_mesh_mapping.h
@@ -152,9 +152,10 @@ typedef struct MeshIslands {
 	int *items_to_islands_idx;
 
 	int nbr_islands;
-	MeshElemMap *islands;  /* Array, one item per island. */
+	MeshElemMap **islands;  /* Array of pointers, one item per island. */
 
 	void *mem;  /* Memory handler, internal use only. */
+	size_t allocated_islands;
 } MeshIslands;
 
 void BKE_loop_islands_init(MeshIslands *islands, const short item_type, const int num_items, const short island_type);
diff --git a/source/blender/blenkernel/intern/mesh_mapping.c b/source/blender/blenkernel/intern/mesh_mapping.c
index e52418c..fe966e6 100644
--- a/source/blender/blenkernel/intern/mesh_mapping.c
+++ b/source/blender/blenkernel/intern/mesh_mapping.c
@@ -566,35 +566,30 @@ int *BKE_mesh_calc_smoothgroups(const MEdge *medge, const int totedge,
 
 void BKE_loop_islands_init(MeshIslands *islands, const short item_type, const int num_items, const short island_type)
 {
+	MemArena *mem = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__);
+
 	BLI_assert(ELEM(item_type, MISLAND_TYPE_VERT, MISLAND_TYPE_EDGE, MISLAND_TYPE_POLY, MISLAND_TYPE_LOOP));
 	BLI_assert(ELEM(island_type, MISLAND_TYPE_VERT, MISLAND_TYPE_EDGE, MISLAND_TYPE_POLY, MISLAND_TYPE_LOOP));
 
+	BKE_loop_islands_free(islands);
+
 	islands->item_type = item_type;
 	islands->nbr_items = num_items;
-	islands->items_to_islands_idx = MEM_mallocN(sizeof(*islands->items_to_islands_idx) * (size_t)num_items, __func__);
+	islands->items_to_islands_idx = BLI_memarena_alloc(mem, sizeof(*islands->items_to_islands_idx) * (size_t)num_items);
 
 	islands->island_type = island_type;
-	islands->nbr_islands = 0;
-	islands->islands = NULL;
+	islands->allocated_islands = 64;
+	islands->islands = BLI_memarena_alloc(mem, sizeof(*islands->islands) * islands->allocated_islands);
 
-	islands->mem = NULL;
+	islands->mem = mem;
 }
 
 void BKE_loop_islands_free(MeshIslands *islands)
 {
-	/* For now, we use mere MEM_mallocN, later we'll probably switch to memarena! */
-	int i = islands->nbr_islands;
-
-	if (i) {
-		while (i--) {
-			MeshElemMap *it = &islands->islands[i];
-			if (it->count) {
-				MEM_freeN(it->indices);
-			}
-		}
+	MemArena *mem = islands->mem;
 
-		MEM_freeN(islands->islands);
-		MEM_freeN(islands->items_to_islands_idx);
+	if (mem) {
+		BLI_memarena_free(mem);
 	}
 
 	islands->item_type = 0;
@@ -606,12 +601,15 @@ void BKE_loop_islands_free(MeshIslands *islands)
 	islands->islands = NULL;
 
 	islands->mem = NULL;
+	islands->allocated_islands = 0;
 }
 
 void BKE_loop_islands_add_island(MeshIslands *islands, const int num_items, int *items_indices,
                                  const int num_island_items, int *island_item_indices)
 {
-	MeshElemMap *isl;
+	MemArena *mem = islands->mem;
+
+	MeshElemMap *isld;
 	const int curr_island_idx = islands->nbr_islands++;
 	const size_t curr_num_islands = (size_t)islands->nbr_islands;
 	int i = num_items;
@@ -621,18 +619,20 @@ void BKE_loop_islands_add_island(MeshIslands *islands, const int num_items, int
 		islands->items_to_islands_idx[items_indices[i]] = curr_island_idx;
 	}
 
-	/* XXX TODO UGLY!!! Quick code, to be done better. */
-	isl = MEM_mallocN(sizeof(*isl) * curr_num_islands, __func__);
-	if (curr_island_idx) {
-		memcpy(isl, islands->islands, sizeof(*isl) * (curr_num_islands - 1));
-		MEM_freeN(islands->islands);
+	if (UNLIKELY(curr_num_islands > islands->allocated_islands)) {
+		MeshElemMap **islds;
+
+		islands->allocated_islands *= 2;
+		islds = BLI_memarena_alloc(mem, sizeof(*islds) * islands->allocated_islands);
+		memcpy(islds, islands->islands, sizeof(*islds) * (curr_num_islands - 1));
+		islands->islands = islds;
 	}
-	islands->islands = isl;
 
-	isl = &isl[curr_island_idx];
-	isl->count = num_island_items;
-	isl->indices = MEM_mallocN(sizeof(*isl->indices) * (size_t)num_island_items, __func__);
-	memcpy(isl->indices, island_item_indices, sizeof(*isl->indices) * (size_t)num_island_items);
+	islands->islands[curr_island_idx] = isld = BLI_memarena_alloc(mem, sizeof(*isld));
+
+	isld->count = num_island_items;
+	isld->indices = BLI_memarena_alloc(mem, sizeof(*isld->indices) * (size_t)num_island_items);
+	memcpy(isld->indices, island_item_indices, sizeof(*isld->indices) * (size_t)num_island_items);
 }
 
 /* TODO: I'm not sure edge seam flag is enough to define UV islands? Maybe we should also consider UVmaps values
@@ -1634,7 +1634,7 @@ void BKE_dm2mesh_mapping_loops_compute(
 				BLI_bitmap *verts_active = BLI_BITMAP_NEW((size_t)num_verts_src, __func__);
 
 				for (tidx = 0; tidx < num_trees; tidx++) {
-					MeshElemMap *isld = &islands.islands[tidx];
+					MeshElemMap *isld = islands.islands[tidx];
 					int num_verts_active = 0;
 					BLI_BITMAP_SET_ALL(verts_active, false, (size_t)num_verts_src);
 					for (i = 0; i < isld->count; i++) {




More information about the Bf-blender-cvs mailing list