Memory functions

This commit is contained in:
2024-10-17 09:30:36 -07:00
parent 5fb36aad35
commit a74f285cb2
22 changed files with 225 additions and 63 deletions

View File

@ -7,6 +7,7 @@
#include "assetjson.h"
#include "assert/assert.h"
#include "util/memory.h"
size_t assetJsonParse(const char_t *json, assetjson_t **out) {
size_t offset = assetJsonParseSub(json, out);
@ -84,7 +85,7 @@ size_t assetJsonParseAsNull(
const char_t *json,
assetjson_t **out
) {
assetjson_t *obj = malloc(sizeof(assetjson_t));
assetjson_t *obj = (assetjson_t*)memoryAllocate(sizeof(assetjson_t));
// Read "null"
assertTrue(json[0] == 'n', "Expected NULL data type. (n0)");
assertTrue(json[1] == 'u', "Expected NULL data type. (u0)");
@ -102,7 +103,7 @@ size_t assetJsonParseAsBoolean(
const char_t *json,
assetjson_t **out
) {
assetjson_t *obj = malloc(sizeof(assetjson_t));
assetjson_t *obj = (assetjson_t*)memoryAllocate(sizeof(assetjson_t));
obj->type = ASSET_JSON_DATA_TYPE_BOOLEAN;
*out = obj;
@ -131,7 +132,7 @@ size_t assetJsonParseAsString(
const char_t *json,
assetjson_t **out
) {
assetjson_t *obj = malloc(sizeof(assetjson_t));
assetjson_t *obj = memoryAllocate(sizeof(assetjson_t));
obj->type = ASSET_JSON_DATA_TYPE_STRING;
@ -141,7 +142,7 @@ size_t assetJsonParseAsString(
char c;
bool_t inEscape = false;
size_t bufferSize = 2;
char_t *string = (char_t*)malloc(bufferSize * sizeof(char_t));
char_t *string = (char_t*)memoryAllocate(bufferSize * sizeof(char_t));
while(true) {
c = json[offset];
if(c == '\0') assertUnreachable("Unexpected end of string.");
@ -178,7 +179,7 @@ size_t assetJsonParseAsString(
if(outOffset >= bufferSize) {
bufferSize *= 2;
string = realloc(string, bufferSize * sizeof(char_t));
string = memoryReallocate(string, bufferSize * sizeof(char_t));
}
string[outOffset] = c;
offset++;
@ -193,7 +194,7 @@ size_t assetJsonParseAsString(
if(c == '"') break;
if(outOffset >= bufferSize) {
bufferSize *= 2;
string = realloc(string, bufferSize * sizeof(char_t));
string = memoryReallocate(string, bufferSize * sizeof(char_t));
}
string[outOffset] = c;
offset++;
@ -212,13 +213,13 @@ size_t assetJsonParseAsObject(
const char_t *json,
assetjson_t **out
) {
assetjson_t *obj = malloc(sizeof(assetjson_t));
assetjson_t *obj = memoryAllocate(sizeof(assetjson_t));
obj->type = ASSET_JSON_DATA_TYPE_OBJECT;
size_t bufferSize = 2;
char_t **keys = malloc(bufferSize * sizeof(char_t*));
assetjson_t **values = malloc(bufferSize * sizeof(assetjson_t*));
char_t **keys = memoryAllocate(bufferSize * sizeof(char_t*));
assetjson_t **values = memoryAllocate(bufferSize * sizeof(assetjson_t*));
size_t length = 0;
// Skip whitespace
@ -264,8 +265,8 @@ size_t assetJsonParseAsObject(
// Need to resize?
if(length >= bufferSize) {
bufferSize *= 2;
keys = realloc(keys, bufferSize * sizeof(char_t*));
values = realloc(values, bufferSize * sizeof(assetjson_t*));
keys = memoryReallocate(keys, bufferSize * sizeof(char_t*));
values = memoryReallocate(values, bufferSize * sizeof(assetjson_t*));
}
keys[length] = bufferKey;
@ -279,7 +280,10 @@ size_t assetJsonParseAsObject(
}
// Expect either comma or closing bracket
assertTrue(c == ',' || c == '}', "Expected comma or closing bracket after JSON object value.");
assertTrue(
c == ',' || c == '}',
"Expected comma or closing bracket after JSON object value."
);
if(c == '}') break;
offset++;
@ -297,7 +301,7 @@ size_t assetJsonParseAsArray(
const char_t *json,
assetjson_t **out
) {
assetjson_t *obj = malloc(sizeof(assetjson_t));
assetjson_t *obj = (assetjson_t*)memoryAllocate(sizeof(assetjson_t));
obj->type = ASSET_JSON_DATA_TYPE_ARRAY;
@ -306,7 +310,9 @@ size_t assetJsonParseAsArray(
// Create array
size_t arraySize = 2;
obj->array.value = malloc(arraySize * sizeof(assetjson_t*));
obj->array.value = (assetjson_t**)memoryAllocate(
arraySize * sizeof(assetjson_t*)
);
obj->array.length = 0;
// Until closing bracket
@ -326,7 +332,7 @@ size_t assetJsonParseAsArray(
// Need to expand?
if(obj->array.length >= arraySize) {
arraySize *= 2;
obj->array.value = realloc(
obj->array.value = memoryReallocate(
obj->array.value, arraySize * sizeof(assetjson_t*)
);
}
@ -365,7 +371,7 @@ size_t assetJsonParseAsNumber(
const char_t *json,
assetjson_t **out
) {
assetjson_t *obj = malloc(sizeof(assetjson_t));
assetjson_t *obj = (assetjson_t*)memoryAllocate(sizeof(assetjson_t));
obj->type = ASSET_JSON_DATA_TYPE_NUMBER;
@ -374,7 +380,7 @@ size_t assetJsonParseAsNumber(
size_t outOffset = 0;
char_t c;
size_t bufferSize = 2;
char_t *buffer = (char_t*)malloc(bufferSize * sizeof(char_t));
char_t *buffer = (char_t*)memoryAllocate(bufferSize * sizeof(char_t));
bool_t hasDecimal = false;
bool_t hasNumber = false;
@ -395,7 +401,7 @@ size_t assetJsonParseAsNumber(
// If no number before decimal, add a 0
if(outOffset >= bufferSize) {
bufferSize *= 2;
buffer = realloc(buffer, bufferSize * sizeof(char_t));
buffer = memoryReallocate(buffer, bufferSize * sizeof(char_t));
}
buffer[outOffset] = '0';
outOffset++;
@ -409,7 +415,7 @@ size_t assetJsonParseAsNumber(
// Need to expand?
if(outOffset >= bufferSize) {
bufferSize *= 2;
buffer = realloc(buffer, bufferSize * sizeof(char_t));
buffer = memoryReallocate(buffer, bufferSize * sizeof(char_t));
}
buffer[outOffset] = c;
@ -420,7 +426,7 @@ size_t assetJsonParseAsNumber(
// Seal the buffer, parse and cleanup
buffer[outOffset] = '\0';
obj->number = strtod(buffer, NULL);
free(buffer);
memoryFree(buffer);
*out = obj;
return offset;
@ -434,7 +440,7 @@ size_t assetJsonReadString(
size_t outOffset = 0;
char_t c;
size_t bufferSize = 32;
char_t *string = (char_t*)malloc(sizeof(char_t) * bufferSize);
char_t *string = (char_t*)memoryAllocate(sizeof(char_t) * bufferSize);
bool_t inEscape = false;
// For each char
@ -475,7 +481,7 @@ size_t assetJsonReadString(
if(outOffset >= bufferSize) {
bufferSize *= 2;
string = realloc(string, bufferSize);
string = memoryReallocate(string, bufferSize);
}
string[outOffset] = c;
offset++;
@ -493,7 +499,7 @@ size_t assetJsonReadString(
if(outOffset >= bufferSize) {
bufferSize *= 2;
string = realloc(string, bufferSize);
string = memoryReallocate(string, bufferSize);
}
string[outOffset] = c;
offset++;
@ -522,22 +528,22 @@ void assetJsonDispose(assetjson_t *json) {
switch(json->type) {
case ASSET_JSON_DATA_TYPE_OBJECT:
for(size_t i = 0; i < json->object.length; i++) {
free(json->object.keys[i]);
memoryFree(json->object.keys[i]);
assetJsonDispose(json->object.values[i]);
}
free(json->object.keys);
free(json->object.values);
memoryFree(json->object.keys);
memoryFree(json->object.values);
break;
case ASSET_JSON_DATA_TYPE_ARRAY:
for(size_t i = 0; i < json->array.length; i++) {
assetJsonDispose(json->array.value[i]);
}
free(json->array.value);
memoryFree(json->array.value);
break;
case ASSET_JSON_DATA_TYPE_STRING:
free(json->string);
memoryFree(json->string);
break;
case ASSET_JSON_DATA_TYPE_NUMBER:
@ -550,5 +556,5 @@ void assetJsonDispose(assetjson_t *json) {
break;
}
free(json);
memoryFree(json);
}

View File

@ -9,6 +9,7 @@
#include "asset/asset.h"
#include "assert/assert.h"
#include "locale/language.h"
#include "util/memory.h"
void assetLanguageObjectLoad(
const char_t *key,
@ -48,7 +49,7 @@ void assetLanguageLoad(const char_t *path) {
assetOpen(path);
size_t length = assetGetSize();
char_t *buffer = malloc(sizeof(char_t) * (length + 1));
char_t *buffer = memoryAllocate(sizeof(char_t) * (length + 1));
buffer[length] = '\0';
size_t read = assetRead((uint8_t*)buffer, length);
assertTrue(read == length, "Failed to read language file!");
@ -56,7 +57,7 @@ void assetLanguageLoad(const char_t *path) {
assetjson_t *json;
read = assetJsonParse(buffer, &json);
free(buffer);
memoryFree(buffer);
assertTrue(
json->type == ASSET_JSON_DATA_TYPE_OBJECT,
@ -65,4 +66,5 @@ void assetLanguageLoad(const char_t *path) {
languageInit();
assetLanguageObjectLoad("", json);
assetJsonDispose(json);
}

View File

@ -8,6 +8,7 @@
#include "assetmap.h"
#include "asset/asset.h"
#include "assert/assert.h"
#include "util/memory.h"
void assetMapLoadEntity(
assetjson_t *jEnt,
@ -104,7 +105,7 @@ void assetMapLoad(
// Read in the string data.
assetOpen(path);
size_t length = assetGetSize();
char_t *buffer = malloc(sizeof(char_t) * (length + 1));
char_t *buffer = memoryAllocate(sizeof(char_t) * (length + 1));
size_t read = assetRead((uint8_t*)buffer, length);
buffer[length] = '\0';
assertTrue(read == length, "assetMapLoad: Failed to read map data!");
@ -113,7 +114,7 @@ void assetMapLoad(
// Begin parsing JSON data.
assetjson_t *json;
read = assetJsonParse(buffer, &json);
free(buffer);
memoryFree(buffer);
assertTrue(
json->type == ASSET_JSON_DATA_TYPE_OBJECT,