Skip to content

Commit 5a54fc1

Browse files
committed
REVIEWED: Prioritize calloc() calls than malloc() on some cases
1 parent 8a1468c commit 5a54fc1

File tree

4 files changed

+19
-19
lines changed

4 files changed

+19
-19
lines changed

src/platforms/rcore_desktop_glfw.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ static void CursorEnterCallback(GLFWwindow *window, int enter);
148148
static void JoystickCallback(int jid, int event); // GLFW3 Joystick Connected/Disconnected Callback
149149

150150
// Wrappers used by glfwInitAllocator
151-
static void *AllocateWrapper(size_t size, void *user); // GLFW3 GLFWallocatefun, wrapps around RL_MALLOC macro
152-
static void *ReallocateWrapper(void *block, size_t size, void *user); // GLFW3 GLFWreallocatefun, wrapps around RL_MALLOC macro
151+
static void *AllocateWrapper(size_t size, void *user); // GLFW3 GLFWallocatefun, wrapps around RL_CALLOC macro
152+
static void *ReallocateWrapper(void *block, size_t size, void *user); // GLFW3 GLFWreallocatefun, wrapps around RL_REALLOC macro
153153
static void DeallocateWrapper(void *block, void *user); // GLFW3 GLFWdeallocatefun, wraps around RL_FREE macro
154154

155155
//----------------------------------------------------------------------------------
@@ -1342,7 +1342,7 @@ static void SetDimensionsFromMonitor(GLFWmonitor *monitor)
13421342
static void *AllocateWrapper(size_t size, void *user)
13431343
{
13441344
(void)user;
1345-
return RL_MALLOC(size);
1345+
return RL_CALLOC(size, 1);
13461346
}
13471347
static void *ReallocateWrapper(void *block, size_t size, void *user)
13481348
{

src/rcore.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2318,8 +2318,8 @@ FilePathList LoadDirectoryFiles(const char *dirPath)
23182318

23192319
// Memory allocation for dirFileCount
23202320
files.capacity = fileCounter;
2321-
files.paths = (char **)RL_MALLOC(files.capacity*sizeof(char *));
2322-
for (unsigned int i = 0; i < files.capacity; i++) files.paths[i] = (char *)RL_MALLOC(MAX_FILEPATH_LENGTH*sizeof(char));
2321+
files.paths = (char **)RL_CALLOC(files.capacity, sizeof(char *));
2322+
for (unsigned int i = 0; i < files.capacity; i++) files.paths[i] = (char *)RL_CALLOC(MAX_FILEPATH_LENGTH, sizeof(char));
23232323

23242324
closedir(dir);
23252325

src/rlgl.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ RLAPI void rlLoadDrawQuad(void); // Load and draw a quad
889889
#endif
890890
#endif
891891

892-
#include <stdlib.h> // Required for: malloc(), free()
892+
#include <stdlib.h> // Required for: calloc(), free()
893893
#include <string.h> // Required for: strcmp(), strlen() [Used in rlglInit(), on extensions loading]
894894
#include <math.h> // Required for: sqrtf(), sinf(), cosf(), floor(), log()
895895

@@ -2429,7 +2429,7 @@ void rlLoadExtensions(void *loader)
24292429

24302430
// Get supported extensions list
24312431
GLint numExt = 0;
2432-
const char **extList = (const char **)RL_MALLOC(512*sizeof(const char *)); // Allocate 512 strings pointers (2 KB)
2432+
const char **extList = (const char **)RL_CALLOC(512, sizeof(const char *)); // Allocate 512 strings pointers (2 KB)
24332433
const char *extensions = (const char *)glGetString(GL_EXTENSIONS); // One big const string
24342434

24352435
// NOTE: We have to duplicate string because glGetString() returns a const string
@@ -2741,21 +2741,21 @@ rlRenderBatch rlLoadRenderBatch(int numBuffers, int bufferElements)
27412741
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
27422742
// Initialize CPU (RAM) vertex buffers (position, texcoord, color data and indexes)
27432743
//--------------------------------------------------------------------------------------------
2744-
batch.vertexBuffer = (rlVertexBuffer *)RL_MALLOC(numBuffers*sizeof(rlVertexBuffer));
2744+
batch.vertexBuffer = (rlVertexBuffer *)RL_CALLOC(numBuffers, sizeof(rlVertexBuffer));
27452745

27462746
for (int i = 0; i < numBuffers; i++)
27472747
{
27482748
batch.vertexBuffer[i].elementCount = bufferElements;
27492749

2750-
batch.vertexBuffer[i].vertices = (float *)RL_MALLOC(bufferElements*3*4*sizeof(float)); // 3 float by vertex, 4 vertex by quad
2751-
batch.vertexBuffer[i].texcoords = (float *)RL_MALLOC(bufferElements*2*4*sizeof(float)); // 2 float by texcoord, 4 texcoord by quad
2752-
batch.vertexBuffer[i].normals = (float *)RL_MALLOC(bufferElements*3*4*sizeof(float)); // 3 float by vertex, 4 vertex by quad
2753-
batch.vertexBuffer[i].colors = (unsigned char *)RL_MALLOC(bufferElements*4*4*sizeof(unsigned char)); // 4 float by color, 4 colors by quad
2750+
batch.vertexBuffer[i].vertices = (float *)RL_CALLOC(bufferElements*3*4, sizeof(float)); // 3 float by vertex, 4 vertex by quad
2751+
batch.vertexBuffer[i].texcoords = (float *)RL_CALLOC(bufferElements*2*4, sizeof(float)); // 2 float by texcoord, 4 texcoord by quad
2752+
batch.vertexBuffer[i].normals = (float *)RL_CALLOC(bufferElements*3*4, sizeof(float)); // 3 float by vertex, 4 vertex by quad
2753+
batch.vertexBuffer[i].colors = (unsigned char *)RL_CALLOC(bufferElements*4*4, sizeof(unsigned char)); // 4 float by color, 4 colors by quad
27542754
#if defined(GRAPHICS_API_OPENGL_33)
2755-
batch.vertexBuffer[i].indices = (unsigned int *)RL_MALLOC(bufferElements*6*sizeof(unsigned int)); // 6 int by quad (indices)
2755+
batch.vertexBuffer[i].indices = (unsigned int *)RL_CALLOC(bufferElements*6, sizeof(unsigned int)); // 6 int by quad (indices)
27562756
#endif
27572757
#if defined(GRAPHICS_API_OPENGL_ES2)
2758-
batch.vertexBuffer[i].indices = (unsigned short *)RL_MALLOC(bufferElements*6*sizeof(unsigned short)); // 6 int by quad (indices)
2758+
batch.vertexBuffer[i].indices = (unsigned short *)RL_CALLOC(bufferElements*6, sizeof(unsigned short)); // 6 int by quad (indices)
27592759
#endif
27602760

27612761
for (int j = 0; j < (3*4*bufferElements); j++) batch.vertexBuffer[i].vertices[j] = 0.0f;
@@ -2843,7 +2843,7 @@ rlRenderBatch rlLoadRenderBatch(int numBuffers, int bufferElements)
28432843

28442844
// Init draw calls tracking system
28452845
//--------------------------------------------------------------------------------------------
2846-
batch.draws = (rlDrawCall *)RL_MALLOC(RL_DEFAULT_BATCH_DRAWCALLS*sizeof(rlDrawCall));
2846+
batch.draws = (rlDrawCall *)RL_CALLOC(RL_DEFAULT_BATCH_DRAWCALLS, sizeof(rlDrawCall));
28472847

28482848
for (int i = 0; i < RL_DEFAULT_BATCH_DRAWCALLS; i++)
28492849
{
@@ -3649,7 +3649,7 @@ void *rlReadTexturePixels(unsigned int id, int width, int height, int format)
36493649

36503650
if ((glInternalFormat != 0) && (format < RL_PIXELFORMAT_COMPRESSED_DXT1_RGB))
36513651
{
3652-
pixels = RL_MALLOC(size);
3652+
pixels = RL_CALLOC(size, 1);
36533653
glGetTexImage(GL_TEXTURE_2D, 0, glFormat, glType, pixels);
36543654
}
36553655
else TRACELOG(RL_LOG_WARNING, "TEXTURE: [ID %i] Data retrieval not suported for pixel format (%i)", id, format);
@@ -3674,7 +3674,7 @@ void *rlReadTexturePixels(unsigned int id, int width, int height, int format)
36743674
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, id, 0);
36753675

36763676
// We read data as RGBA because FBO texture is configured as RGBA, despite binding another texture format
3677-
pixels = (unsigned char *)RL_MALLOC(rlGetPixelDataSize(width, height, RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8A8));
3677+
pixels = RL_CALLOC(rlGetPixelDataSize(width, height, RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8A8), 1);
36783678
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
36793679

36803680
glBindFramebuffer(GL_FRAMEBUFFER, 0);

src/utils.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ unsigned char *LoadFileData(const char *fileName, int *dataSize)
203203

204204
if (size > 0)
205205
{
206-
data = (unsigned char *)RL_MALLOC(size*sizeof(unsigned char));
206+
data = (unsigned char *)RL_CALLOC(size, sizeof(unsigned char));
207207

208208
if (data != NULL)
209209
{
@@ -366,7 +366,7 @@ char *LoadFileText(const char *fileName)
366366

367367
if (size > 0)
368368
{
369-
text = (char *)RL_MALLOC((size + 1)*sizeof(char));
369+
text = (char *)RL_CALLOC(size + 1, sizeof(char));
370370

371371
if (text != NULL)
372372
{

0 commit comments

Comments
 (0)