GPU: Replace malloc/calloc/realloc/free with MEM_* counterpart
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -29,6 +29,8 @@
|
||||
* GPU vertex buffer
|
||||
*/
|
||||
|
||||
#include "MEM_guardedalloc.h"
|
||||
|
||||
#include "GPU_vertex_buffer.h"
|
||||
|
||||
#include "gpu_context_private.h"
|
||||
@@ -53,7 +55,7 @@ static GLenum convert_usage_type_to_gl(GPUUsageType type)
|
||||
|
||||
GPUVertBuf *GPU_vertbuf_create(GPUUsageType usage)
|
||||
{
|
||||
GPUVertBuf *verts = malloc(sizeof(GPUVertBuf));
|
||||
GPUVertBuf *verts = MEM_mallocN(sizeof(GPUVertBuf), "GPUVertBuf");
|
||||
GPU_vertbuf_init(verts, usage);
|
||||
return verts;
|
||||
}
|
||||
@@ -96,9 +98,9 @@ void GPU_vertbuf_discard(GPUVertBuf *verts)
|
||||
#endif
|
||||
}
|
||||
if (verts->data) {
|
||||
free(verts->data);
|
||||
MEM_freeN(verts->data);
|
||||
}
|
||||
free(verts);
|
||||
MEM_freeN(verts);
|
||||
}
|
||||
|
||||
uint GPU_vertbuf_size_get(const GPUVertBuf *verts)
|
||||
@@ -123,7 +125,7 @@ void GPU_vertbuf_data_alloc(GPUVertBuf *verts, uint v_len)
|
||||
}
|
||||
/* discard previous data if any */
|
||||
if (verts->data) {
|
||||
free(verts->data);
|
||||
MEM_freeN(verts->data);
|
||||
}
|
||||
#if VRAM_USAGE
|
||||
uint new_size = vertex_buffer_size(&verts->format, v_len);
|
||||
@@ -131,7 +133,7 @@ void GPU_vertbuf_data_alloc(GPUVertBuf *verts, uint v_len)
|
||||
#endif
|
||||
verts->dirty = true;
|
||||
verts->vertex_len = verts->vertex_alloc = v_len;
|
||||
verts->data = malloc(sizeof(GLubyte) * GPU_vertbuf_size_get(verts));
|
||||
verts->data = MEM_mallocN(sizeof(GLubyte) * GPU_vertbuf_size_get(verts), "GPUVertBuf data");
|
||||
}
|
||||
|
||||
/* resize buffer keeping existing data */
|
||||
@@ -148,7 +150,7 @@ void GPU_vertbuf_data_resize(GPUVertBuf *verts, uint v_len)
|
||||
#endif
|
||||
verts->dirty = true;
|
||||
verts->vertex_len = verts->vertex_alloc = v_len;
|
||||
verts->data = realloc(verts->data, sizeof(GLubyte) * GPU_vertbuf_size_get(verts));
|
||||
verts->data = MEM_reallocN(verts->data, sizeof(GLubyte) * GPU_vertbuf_size_get(verts));
|
||||
}
|
||||
|
||||
/* Set vertex count but does not change allocation.
|
||||
@@ -250,7 +252,7 @@ static void VertBuffer_upload_data(GPUVertBuf *verts)
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, buffer_sz, verts->data);
|
||||
|
||||
if (verts->usage == GPU_USAGE_STATIC) {
|
||||
free(verts->data);
|
||||
MEM_freeN(verts->data);
|
||||
verts->data = NULL;
|
||||
}
|
||||
verts->dirty = false;
|
||||
|
||||
Reference in New Issue
Block a user