[Bf-blender-cvs] [3ecba657bb7] blender2.8: GPU: Replace malloc/calloc/realloc/free with MEM_* counterpart

Clément Foucault noreply at git.blender.org
Tue Jul 31 18:29:36 CEST 2018


Commit: 3ecba657bb79e010cffae4110ee74a063429f7ae
Author: Clément Foucault
Date:   Tue Jul 31 16:54:58 2018 +0200
Branches: blender2.8
https://developer.blender.org/rB3ecba657bb79e010cffae4110ee74a063429f7ae

GPU: Replace malloc/calloc/realloc/free with MEM_* counterpart

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

M	source/blender/gpu/intern/gpu_batch.c
M	source/blender/gpu/intern/gpu_element.c
M	source/blender/gpu/intern/gpu_shader_interface.c
M	source/blender/gpu/intern/gpu_vertex_buffer.c

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

diff --git a/source/blender/gpu/intern/gpu_batch.c b/source/blender/gpu/intern/gpu_batch.c
index 9b433a37a72..87ea112148c 100644
--- a/source/blender/gpu/intern/gpu_batch.c
+++ b/source/blender/gpu/intern/gpu_batch.c
@@ -30,6 +30,8 @@
  * Contains VAOs + VBOs + Shader representing a drawable entity.
  */
 
+#include "MEM_guardedalloc.h"
+
 #include "GPU_batch.h"
 #include "GPU_batch_presets.h"
 #include "GPU_matrix.h"
@@ -59,8 +61,8 @@ void GPU_batch_vao_cache_clear(GPUBatch *batch)
 				GPU_shaderinterface_remove_batch_ref((GPUShaderInterface *)batch->dynamic_vaos.interfaces[i], batch);
 			}
 		}
-		free(batch->dynamic_vaos.interfaces);
-		free(batch->dynamic_vaos.vao_ids);
+		MEM_freeN(batch->dynamic_vaos.interfaces);
+		MEM_freeN(batch->dynamic_vaos.vao_ids);
 	}
 	else {
 		for (int i = 0; i < GPU_BATCH_VAO_STATIC_LEN; ++i) {
@@ -85,7 +87,7 @@ GPUBatch *GPU_batch_create_ex(
         GPUPrimType prim_type, GPUVertBuf *verts, GPUIndexBuf *elem,
         uint owns_flag)
 {
-	GPUBatch *batch = calloc(1, sizeof(GPUBatch));
+	GPUBatch *batch = MEM_callocN(sizeof(GPUBatch), "GPUBatch");
 	GPU_batch_init_ex(batch, prim_type, verts, elem, owns_flag);
 	return batch;
 }
@@ -146,7 +148,7 @@ void GPU_batch_discard(GPUBatch *batch)
 	if (batch->free_callback) {
 		batch->free_callback(batch, batch->callback_data);
 	}
-	free(batch);
+	MEM_freeN(batch);
 }
 
 void GPU_batch_callback_free_set(GPUBatch *batch, void (*callback)(GPUBatch *, void *), void *user_data)
@@ -255,8 +257,8 @@ static GLuint batch_vao_get(GPUBatch *batch)
 			}
 			/* Init dynamic arrays and let the branch below set the values. */
 			batch->dynamic_vaos.count = GPU_BATCH_VAO_DYN_ALLOC_COUNT;
-			batch->dynamic_vaos.interfaces = calloc(batch->dynamic_vaos.count, sizeof(GPUShaderInterface *));
-			batch->dynamic_vaos.vao_ids = calloc(batch->dynamic_vaos.count, sizeof(GLuint));
+			batch->dynamic_vaos.interfaces = MEM_callocN(batch->dynamic_vaos.count * sizeof(GPUShaderInterface *), "dyn vaos interfaces");
+			batch->dynamic_vaos.vao_ids = MEM_callocN(batch->dynamic_vaos.count * sizeof(GLuint), "dyn vaos ids");
 		}
 	}
 
@@ -270,10 +272,8 @@ static GLuint batch_vao_get(GPUBatch *batch)
 			/* Not enough place, realloc the array. */
 			i = batch->dynamic_vaos.count;
 			batch->dynamic_vaos.count += GPU_BATCH_VAO_DYN_ALLOC_COUNT;
-			batch->dynamic_vaos.interfaces = realloc(batch->dynamic_vaos.interfaces, sizeof(GPUShaderInterface *) * batch->dynamic_vaos.count);
-			batch->dynamic_vaos.vao_ids = realloc(batch->dynamic_vaos.vao_ids, sizeof(GLuint) * batch->dynamic_vaos.count);
-			memset(batch->dynamic_vaos.interfaces + i, 0, sizeof(GPUShaderInterface *) * GPU_BATCH_VAO_DYN_ALLOC_COUNT);
-			memset(batch->dynamic_vaos.vao_ids + i, 0, sizeof(GLuint) * GPU_BATCH_VAO_DYN_ALLOC_COUNT);
+			batch->dynamic_vaos.interfaces = MEM_recallocN(batch->dynamic_vaos.interfaces, sizeof(GPUShaderInterface *) * batch->dynamic_vaos.count);
+			batch->dynamic_vaos.vao_ids = MEM_recallocN(batch->dynamic_vaos.vao_ids, sizeof(GLuint) * batch->dynamic_vaos.count);
 		}
 		batch->dynamic_vaos.interfaces[i] = batch->interface;
 		batch->dynamic_vaos.vao_ids[i] = new_vao = GPU_vao_alloc();
diff --git a/source/blender/gpu/intern/gpu_element.c b/source/blender/gpu/intern/gpu_element.c
index 901e09443d1..56a0c90d5b5 100644
--- a/source/blender/gpu/intern/gpu_element.c
+++ b/source/blender/gpu/intern/gpu_element.c
@@ -29,6 +29,8 @@
  * GPU element list (AKA index buffer)
  */
 
+#include "MEM_guardedalloc.h"
+
 #include "GPU_element.h"
 
 #include "gpu_context_private.h"
@@ -70,7 +72,7 @@ void GPU_indexbuf_init_ex(
 	builder->max_index_len = index_len;
 	builder->index_len = 0; // start empty
 	builder->prim_type = prim_type;
-	builder->data = calloc(builder->max_index_len, sizeof(uint));
+	builder->data = MEM_callocN(builder->max_index_len * sizeof(uint), "GPUIndexBuf data");
 }
 
 void GPU_indexbuf_init(GPUIndexBufBuilder *builder, GPUPrimType prim_type, uint prim_len, uint vertex_len)
@@ -243,7 +245,7 @@ static void squeeze_indices_short(GPUIndexBufBuilder *builder, GPUIndexBuf *elem
 
 GPUIndexBuf *GPU_indexbuf_build(GPUIndexBufBuilder *builder)
 {
-	GPUIndexBuf *elem = calloc(1, sizeof(GPUIndexBuf));
+	GPUIndexBuf *elem = MEM_callocN(sizeof(GPUIndexBuf), "GPUIndexBuf");
 	GPU_indexbuf_build_in_place(builder, elem);
 	return elem;
 }
@@ -290,7 +292,7 @@ void GPU_indexbuf_build_in_place(GPUIndexBufBuilder *builder, GPUIndexBuf *elem)
 	glBufferData(GL_ARRAY_BUFFER, GPU_indexbuf_size_get(elem), builder->data, GL_STATIC_DRAW);
 
 	/* discard builder (one-time use) */
-	free(builder->data);
+	MEM_freeN(builder->data);
 	builder->data = NULL;
 	/* other fields are safe to leave */
 }
@@ -305,5 +307,5 @@ void GPU_indexbuf_discard(GPUIndexBuf *elem)
 	if (elem->vbo_id) {
 		GPU_buf_free(elem->vbo_id);
 	}
-	free(elem);
+	MEM_freeN(elem);
 }
diff --git a/source/blender/gpu/intern/gpu_shader_interface.c b/source/blender/gpu/intern/gpu_shader_interface.c
index 54c5f41bbd3..f6bbc228ae9 100644
--- a/source/blender/gpu/intern/gpu_shader_interface.c
+++ b/source/blender/gpu/intern/gpu_shader_interface.c
@@ -29,6 +29,8 @@
  * GPU shader interface (C --> GLSL)
  */
 
+#include "MEM_guardedalloc.h"
+
 #include "GPU_shader_interface.h"
 
 #include "gpu_batch_private.h"
@@ -154,7 +156,7 @@ GPU_INLINE void buckets_free(GPUShaderInput *buckets[GPU_NUM_SHADERINTERFACE_BUC
 		GPUShaderInput *input = buckets[bucket_index];
 		while (input != NULL) {
 			GPUShaderInput *input_next = input->next;
-			free(input);
+			MEM_freeN(input);
 			input = input_next;
 		}
 	}
@@ -178,12 +180,12 @@ static bool setup_builtin_uniform(GPUShaderInput *input, const char *name)
 
 static const GPUShaderInput *add_uniform(GPUShaderInterface *shaderface, const char *name)
 {
-	GPUShaderInput *input = malloc(sizeof(GPUShaderInput));
+	GPUShaderInput *input = MEM_mallocN(sizeof(GPUShaderInput), "GPUShaderInput Unif");
 
 	input->location = glGetUniformLocation(shaderface->program, name);
 
 	uint name_len = strlen(name);
-	shaderface->name_buffer = realloc(shaderface->name_buffer, shaderface->name_buffer_offset + name_len + 1); /* include NULL terminator */
+	shaderface->name_buffer = MEM_reallocN(shaderface->name_buffer, shaderface->name_buffer_offset + name_len + 1); /* include NULL terminator */
 	char *name_buffer = shaderface->name_buffer + shaderface->name_buffer_offset;
 	strcpy(name_buffer, name);
 
@@ -208,7 +210,7 @@ static const GPUShaderInput *add_uniform(GPUShaderInterface *shaderface, const c
 
 GPUShaderInterface *GPU_shaderinterface_create(int32_t program)
 {
-	GPUShaderInterface *shaderface = calloc(1, sizeof(GPUShaderInterface));
+	GPUShaderInterface *shaderface = MEM_callocN(sizeof(GPUShaderInterface), "GPUShaderInterface");
 	shaderface->program = program;
 
 #if DEBUG_SHADER_INTERFACE
@@ -225,11 +227,11 @@ GPUShaderInterface *GPU_shaderinterface_create(int32_t program)
 	glGetProgramiv(program, GL_ACTIVE_UNIFORM_BLOCKS, &ubo_len);
 
 	const uint32_t name_buffer_len = attr_len * max_attrib_name_len + ubo_len * max_ubo_name_len;
-	shaderface->name_buffer = malloc(name_buffer_len);
+	shaderface->name_buffer = MEM_mallocN(name_buffer_len, "name_buffer");
 
 	/* Attributes */
 	for (uint32_t i = 0; i < attr_len; ++i) {
-		GPUShaderInput *input = malloc(sizeof(GPUShaderInput));
+		GPUShaderInput *input = MEM_mallocN(sizeof(GPUShaderInput), "GPUShaderInput Attr");
 		GLsizei remaining_buffer = name_buffer_len - shaderface->name_buffer_offset;
 		char *name = shaderface->name_buffer + shaderface->name_buffer_offset;
 		GLsizei name_len = 0;
@@ -256,7 +258,7 @@ GPUShaderInterface *GPU_shaderinterface_create(int32_t program)
 	}
 	/* Uniform Blocks */
 	for (uint32_t i = 0; i < ubo_len; ++i) {
-		GPUShaderInput *input = malloc(sizeof(GPUShaderInput));
+		GPUShaderInput *input = MEM_mallocN(sizeof(GPUShaderInput), "GPUShaderInput UBO");
 		GLsizei remaining_buffer = name_buffer_len - shaderface->name_buffer_offset;
 		char *name = shaderface->name_buffer + shaderface->name_buffer_offset;
 		GLsizei name_len = 0;
@@ -282,7 +284,7 @@ GPUShaderInterface *GPU_shaderinterface_create(int32_t program)
 	}
 	/* Batches ref buffer */
 	shaderface->batches_len = GPU_SHADERINTERFACE_REF_ALLOC_COUNT;
-	shaderface->batches = calloc(shaderface->batches_len, sizeof(GPUBatch *));
+	shaderface->batches = MEM_callocN(shaderface->batches_len * sizeof(GPUBatch *), "GPUShaderInterface batches");
 
 	return shaderface;
 }
@@ -294,16 +296,16 @@ void GPU_shaderinterface_discard(GPUShaderInterface *shaderface)
 	buckets_free(shaderface->attrib_buckets);
 	buckets_free(shaderface->ubo_buckets);
 	/* Free memory used by name_buffer. */
-	free(shaderface->name_buffer);
+	MEM_freeN(shaderface->name_buffer);
 	/* Remove this interface from all linked Batches vao cache. */
 	for (int i = 0; i < shaderface->batches_len; ++i) {
 		if (shaderface->batches[i] != NULL) {
 			gpu_batch_remove_interface_ref(shaderface->batches[i], shaderface);
 		}
 	}
-	free(shaderface->batches);
+	MEM_freeN(shaderface->batches);
 	/* Free memory used by shader interface by its self. */
-	free(shaderface);
+	MEM_freeN(shaderface);
 }
 
 const GPUShaderInput *GPU_shaderinterface_uniform(const GPUShaderInterface *shaderface, const char *name)
@@ -350,8 +352,7 @@ void GPU_shaderinterface_add_batch_ref(GPUShaderInterface *shaderface, GPUBatch
 		/* Not enough place, realloc the array. */
 		i = shaderface->batches_len;
 		shaderface->batches_len += GPU_SHADERINTERFACE_REF_ALLOC_COUNT;
-		shaderface->batches = realloc(shaderface->batches, sizeof(GPUBatch *) * shaderface->batches_len);
-		memset(shaderface->batches + i, 0, sizeof(GPUBatch *) * GPU_SHADERINTERFACE_REF_ALLOC_COUNT);
+		shaderface->batches = MEM_recallocN(shaderface->batches, sizeof(GPUBatch *) * shaderface->batches_len);
 	}
 	shaderface->batches[i] = batch;
 }
diff --git a/source/blender/gpu/intern/gpu_vertex_buffer.c b/source/blender/gpu/intern/gpu_vertex_buffer.c
index d605378bf0e..05100b8a23f 100644
--- a/source/blender/gpu/intern/gpu_vertex_buffer.c
+++ b/source/blender/gpu/intern/gpu_vertex_buffer.c
@@ -29,6 +29,8 @@
  * GPU vertex buffer
  */
 
+#include "MEM_guardedalloc.h"
+
 #include "GPU_vertex_buffer.h"
 
 #include "gpu_context_

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list