library: Cleanup memory allocation

Only call calloc() if the buffer actually needs to be zeroed. For
buffers that are going to be filled right away, malloc() is faster.
This commit is contained in:
Antonio Niño Díaz 2023-05-30 01:47:45 +01:00
parent 039eda9343
commit e15a8402b5
5 changed files with 14 additions and 14 deletions

View File

@ -227,7 +227,7 @@ void NF_Load16bImgData(const char *file, u32 slot, u32 x, u32 y, u32 type)
NF_Error(116, filename, 131072);
// Allocate memory in RAM
NF_BG16B[slot].buffer = calloc(size / 2, sizeof(u16));
NF_BG16B[slot].buffer = malloc(size);
if (NF_BG16B[slot].buffer == NULL)
NF_Error(102, NULL, size); // Not enough free RAM
@ -371,7 +371,7 @@ void NF_Load8bitsBg(const char *file, u32 slot)
NF_Error(116, filename, 65536);
// Allocate space in RAM
NF_BG8B[slot].data = calloc(size, sizeof(u8));
NF_BG8B[slot].data = malloc(size);
if (NF_BG8B[slot].data == NULL) // Not enough memory
NF_Error(102, NULL, size);
@ -397,7 +397,7 @@ void NF_Load8bitsBg(const char *file, u32 slot)
size = 512;
// Allocate space in RAM
NF_BG8B[slot].pal = calloc(size / 2, sizeof(u16));
NF_BG8B[slot].pal = malloc(size);
if (NF_BG8B[slot].pal == NULL) // Not enough free RAM
NF_Error(102, NULL, size);

View File

@ -79,7 +79,7 @@ void NF_LoadRawSound(const char *file, u32 id, u32 freq, u32 format)
NF_Error(116, filename, 256 * 1024);
// Allocate space in RAM
NF_BUFFER_RAWSOUND[id] = calloc(NF_RAWSOUND[id].size, sizeof(char));
NF_BUFFER_RAWSOUND[id] = malloc(NF_RAWSOUND[id].size);
if (NF_BUFFER_RAWSOUND[id] == NULL) // Not enough RAM
NF_Error(102, NULL, NF_RAWSOUND[id].size);

View File

@ -262,7 +262,7 @@ void NF_LoadSpritePal(const char *file, u32 id)
if (NF_SPR256PAL[id].size < 512)
NF_SPR256PAL[id].size = 512;
// Allocate space in RAM
// Allocate space in RAM, but zero the buffer in case the file needs padding
NF_BUFFER_SPR256PAL[id] = calloc(NF_SPR256PAL[id].size, sizeof(char));
if (NF_BUFFER_SPR256PAL[id] == NULL) // Not enough RAM
NF_Error(102, NULL, NF_SPR256PAL[id].size);

View File

@ -79,7 +79,7 @@ void NF_LoadTextFont16(const char *file, const char *name, u32 width, u32 height
// Create an empty map in RAM. Each element in the map is 2 bytes.
NF_TILEDBG[slot].mapsize = ((width / 8) * (height / 8)) * 2;
// Allocate space in RAM and zero it
// Allocate space in RAM and zero it in case the file needs padding
NF_BUFFER_BGMAP[slot] = calloc(NF_TILEDBG[slot].mapsize, sizeof(char));
if (NF_BUFFER_BGMAP[slot] == NULL)
NF_Error(102, NULL, NF_TILEDBG[slot].mapsize);
@ -100,8 +100,8 @@ void NF_LoadTextFont16(const char *file, const char *name, u32 width, u32 height
if (NF_TILEDBG[slot].palsize < 512)
NF_TILEDBG[slot].palsize = 512;
// Allocate space in RAM
NF_BUFFER_BGPAL[slot] = malloc(NF_TILEDBG[slot].palsize);
// Allocate space in RAM and zero it in case the file needs padding
NF_BUFFER_BGPAL[slot] = calloc(NF_TILEDBG[slot].palsize, sizeof(u8));
if (NF_BUFFER_BGPAL[slot] == NULL)
NF_Error(102, NULL, NF_TILEDBG[slot].palsize);

View File

@ -327,8 +327,8 @@ void NF_LoadTilesForBg(const char *file, const char *name, u32 width, u32 height
// Create an empty .MAP file in RAM
NF_TILEDBG[slot].mapsize = ((width / 8) * (height / 8)) * 2;
// Allocate space in RAM and zero it
NF_BUFFER_BGMAP[slot] = malloc(NF_TILEDBG[slot].mapsize);
// Allocate space in RAM and zero it as the initial state of the map
NF_BUFFER_BGMAP[slot] = calloc(NF_TILEDBG[slot].mapsize, sizeof(u8));
if (NF_BUFFER_BGMAP[slot] == NULL)
NF_Error(102, NULL, NF_TILEDBG[slot].mapsize);
@ -349,8 +349,8 @@ void NF_LoadTilesForBg(const char *file, const char *name, u32 width, u32 height
if (NF_TILEDBG[slot].palsize < 512)
NF_TILEDBG[slot].palsize = 512;
// Allocate space in RAM
NF_BUFFER_BGPAL[slot] = malloc(NF_TILEDBG[slot].palsize);
// Allocate space in RAM and zero it in case the file needs padding
NF_BUFFER_BGPAL[slot] = calloc(NF_TILEDBG[slot].palsize, sizeof(u8));
if (NF_BUFFER_BGPAL[slot] == NULL)
NF_Error(102, NULL, NF_TILEDBG[slot].palsize);
@ -1046,8 +1046,8 @@ void NF_LoadExBgPal(const char *file, u32 slot)
if (NF_EXBGPAL[slot].palsize < 512)
NF_EXBGPAL[slot].palsize = 512;
// Allocate memory in RAM
NF_EXBGPAL[slot].buffer = malloc(NF_EXBGPAL[slot].palsize);
// Allocate memory in RAM and zero it in case the file needs padding
NF_EXBGPAL[slot].buffer = calloc(NF_EXBGPAL[slot].palsize, sizeof(u8));
if (NF_EXBGPAL[slot].buffer == NULL)
NF_Error(102, NULL, NF_EXBGPAL[slot].palsize);