Skip to content

Commit 5b07acd

Browse files
committed
fixes and tweaks raymob/helper.c
use raylib malloc/free macros remove useless pointer casts fix missing free..
1 parent 1c079b8 commit 5b07acd

1 file changed

Lines changed: 29 additions & 24 deletions

File tree

app/src/main/cpp/deps/raymob/helper.c

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ char* GetCacheDir(void)
106106

107107
// Allocate memory for the cache path
108108
size_t len = strlen(pathChars) + 1; // NOTE: +1 for the null terminator
109-
char* cachePath = malloc(len);
109+
char* cachePath = RL_MALLOC(len);
110110

111111
// Copy the string to the allocated memory
112112
if (cachePath)
@@ -138,7 +138,7 @@ char* LoadCacheFile(const char* fileName)
138138
size_t len1 = strlen(cacheDir);
139139
size_t len2 = strlen(fileName);
140140
size_t len = len1 + len2 + 1;
141-
char *filePath = malloc(len);
141+
char *filePath = RL_MALLOC(len);
142142
strncpy(filePath, cacheDir, len1);
143143
filePath[len1] = '/';
144144
strncpy(filePath + len1 + 1, fileName, len2);
@@ -179,6 +179,9 @@ char* LoadCacheFile(const char* fileName)
179179
}
180180
else TraceLog(LOG_WARNING, "FILEIO: File name provided is not valid");
181181

182+
RL_FREE(filePath);
183+
RL_FREE(cacheDir);
184+
182185
return text;
183186
}
184187

@@ -237,7 +240,7 @@ char* GetL10NString(const char* value)
237240

238241
// Allocate memory for returned string
239242
size_t len = strlen(strReturn) + 1;
240-
char* stringValue = malloc(len);
243+
char* stringValue = RL_MALLOC(len);
241244

242245
if (stringValue) {
243246
strncpy(stringValue, strReturn, len);
@@ -304,9 +307,9 @@ void* ReadFromAppStorage(const char *filepath, int *dataSize){
304307

305308
char *appStoragePath = GetAppStoragePath();
306309

307-
int path_len = strlen(appStoragePath) + strlen(filepath) + 2;
308-
char *path = (char*)malloc(sizeof(char)*path_len);
309-
snprintf(path, path_len, "%s/%s", appStoragePath, filepath);
310+
size_t pathLen = strlen(appStoragePath) + strlen(filepath) + 2;
311+
char *path = RL_MALLOC(sizeof(char)*pathLen);
312+
snprintf(path, pathLen, "%s/%s", appStoragePath, filepath);
310313

311314
unsigned char *data = NULL;
312315
*dataSize = 0;
@@ -315,6 +318,8 @@ void* ReadFromAppStorage(const char *filepath, int *dataSize){
315318

316319
if (file == NULL){
317320
TraceLog(LOG_WARNING, "FILEIO: [%s] Failed to open file", path);
321+
RL_FREE(appStoragePath);
322+
RL_FREE(path);
318323
return NULL;
319324
}
320325

@@ -326,7 +331,7 @@ void* ReadFromAppStorage(const char *filepath, int *dataSize){
326331

327332
if (size > 0)
328333
{
329-
data = (unsigned char *)RL_MALLOC(size*sizeof(unsigned char));
334+
data = RL_MALLOC(size*sizeof(unsigned char));
330335

331336
if (data != NULL)
332337
{
@@ -356,8 +361,8 @@ void* ReadFromAppStorage(const char *filepath, int *dataSize){
356361

357362
fclose(file);
358363

359-
free((void*)appStoragePath);
360-
free((void*)path);
364+
RL_FREE(appStoragePath);
365+
RL_FREE(path);
361366

362367
return data;
363368
}
@@ -366,9 +371,9 @@ bool WriteToAppStorage(const char *filepath, void *data, unsigned int dataSize){
366371

367372
char *appStoragePath = GetAppStoragePath();
368373

369-
int path_len = strlen(appStoragePath) + strlen(filepath) + 2;
370-
char *path = (char*)malloc(sizeof(char)*path_len);
371-
snprintf(path, path_len, "%s/%s", appStoragePath, filepath);
374+
size_t pathLen = strlen(appStoragePath) + strlen(filepath) + 2;
375+
char *path = RL_MALLOC(sizeof(char)*pathLen);
376+
snprintf(path, pathLen, "%s/%s", appStoragePath, filepath);
372377

373378
bool success = false;
374379

@@ -389,8 +394,8 @@ bool WriteToAppStorage(const char *filepath, void *data, unsigned int dataSize){
389394
}
390395
else TraceLog(LOG_WARNING, "FILEIO: [%s] Failed to open file", path);
391396

392-
free((void*)appStoragePath);
393-
free((void*)path);
397+
RL_FREE(appStoragePath);
398+
RL_FREE(path);
394399

395400
return success;
396401
}
@@ -399,14 +404,14 @@ bool IsFileExistsInAppStorage(const char *filepath){
399404

400405
char *appStoragePath = GetAppStoragePath();
401406

402-
int path_len = strlen(appStoragePath) + strlen(filepath) + 2;
403-
char *path = (char*)malloc(sizeof(char)*path_len);
404-
snprintf(path, path_len, "%s/%s", appStoragePath, filepath);
407+
size_t pathLen = strlen(appStoragePath) + strlen(filepath) + 2;
408+
char *path = RL_MALLOC(sizeof(char)*pathLen);
409+
snprintf(path, pathLen, "%s/%s", appStoragePath, filepath);
405410

406411
bool success = (access(path, F_OK) != -1);
407412

408-
free((void*)appStoragePath);
409-
free((void*)path);
413+
RL_FREE(appStoragePath);
414+
RL_FREE(path);
410415

411416
return success;
412417
}
@@ -415,12 +420,12 @@ void RemoveFileInAppStorage(const char *filepath){
415420

416421
char *appStoragePath = GetAppStoragePath();
417422

418-
int path_len = strlen(appStoragePath) + strlen(filepath) + 2;
419-
char *path = (char*)malloc(sizeof(char)*path_len);
420-
snprintf(path, path_len, "%s/%s", appStoragePath, filepath);
423+
size_t pathLen = strlen(appStoragePath) + strlen(filepath) + 2;
424+
char *path = RL_MALLOC(sizeof(char)*pathLen);
425+
snprintf(path, pathLen, "%s/%s", appStoragePath, filepath);
421426

422427
remove(path);
423428

424-
free((void*)appStoragePath);
425-
free((void*)path);
429+
RL_FREE(appStoragePath);
430+
RL_FREE(path);
426431
}

0 commit comments

Comments
 (0)