Remove dictionary mem indirection

This commit is contained in:
Alexander Batalov 2025-02-11 20:26:43 +03:00
parent 52e5611e1b
commit fa1621ff56
3 changed files with 17 additions and 67 deletions

View File

@ -4,6 +4,7 @@
#include <stdlib.h>
#include <string.h>
#include "memory.h"
#include "platform_compat.h"
namespace fallout {
@ -13,38 +14,8 @@ namespace fallout {
// with a check for this value.
#define DICTIONARY_MARKER 0xFEBAFEBA
static void* dictionaryMallocDefaultImpl(size_t size);
static void* dictionaryReallocDefaultImpl(void* ptr, size_t newSize);
static void dictionaryFreeDefaultImpl(void* ptr);
static int dictionaryFindIndexForKey(Dictionary* dictionary, const char* key, int* index);
// 0x51E408
static MallocProc* gDictionaryMallocProc = dictionaryMallocDefaultImpl;
// 0x51E40C
static ReallocProc* gDictionaryReallocProc = dictionaryReallocDefaultImpl;
// 0x51E410
static FreeProc* gDictionaryFreeProc = dictionaryFreeDefaultImpl;
// 0x4D9B90
static void* dictionaryMallocDefaultImpl(size_t size)
{
return malloc(size);
}
// 0x4D9B98
static void* dictionaryReallocDefaultImpl(void* ptr, size_t newSize)
{
return realloc(ptr, newSize);
}
// 0x4D9BA0
static void dictionaryFreeDefaultImpl(void* ptr)
{
free(ptr);
}
// 0x4D9BA8
int dictionaryInit(Dictionary* dictionary, int initialCapacity, size_t valueSize, DictionaryIO* io)
{
@ -64,7 +35,7 @@ int dictionaryInit(Dictionary* dictionary, int initialCapacity, size_t valueSize
int rc = 0;
if (initialCapacity != 0) {
dictionary->entries = (DictionaryEntry*)gDictionaryMallocProc(sizeof(*dictionary->entries) * initialCapacity);
dictionary->entries = (DictionaryEntry*)internal_malloc(sizeof(*dictionary->entries) * initialCapacity);
if (dictionary->entries == nullptr) {
rc = -1;
}
@ -90,7 +61,7 @@ int dictionarySetCapacity(Dictionary* dictionary, int newCapacity)
return -1;
}
DictionaryEntry* entries = (DictionaryEntry*)gDictionaryReallocProc(dictionary->entries, sizeof(*dictionary->entries) * newCapacity);
DictionaryEntry* entries = (DictionaryEntry*)internal_realloc(dictionary->entries, sizeof(*dictionary->entries) * newCapacity);
if (entries == nullptr) {
return -1;
}
@ -111,16 +82,16 @@ int dictionaryFree(Dictionary* dictionary)
for (int index = 0; index < dictionary->entriesLength; index++) {
DictionaryEntry* entry = &(dictionary->entries[index]);
if (entry->key != nullptr) {
gDictionaryFreeProc(entry->key);
internal_free(entry->key);
}
if (entry->value != nullptr) {
gDictionaryFreeProc(entry->value);
internal_free(entry->value);
}
}
if (dictionary->entries != nullptr) {
gDictionaryFreeProc(dictionary->entries);
internal_free(dictionary->entries);
}
memset(dictionary, 0, sizeof(*dictionary));
@ -223,7 +194,7 @@ int dictionaryAddValue(Dictionary* dictionary, const char* key, const void* valu
}
// Make a copy of the key.
char* keyCopy = (char*)gDictionaryMallocProc(strlen(key) + 1);
char* keyCopy = (char*)internal_malloc(strlen(key) + 1);
if (keyCopy == nullptr) {
return -1;
}
@ -233,9 +204,9 @@ int dictionaryAddValue(Dictionary* dictionary, const char* key, const void* valu
// Make a copy of the value.
void* valueCopy = nullptr;
if (value != nullptr && dictionary->valueSize != 0) {
valueCopy = gDictionaryMallocProc(dictionary->valueSize);
valueCopy = internal_malloc(dictionary->valueSize);
if (valueCopy == nullptr) {
gDictionaryFreeProc(keyCopy);
internal_free(keyCopy);
return -1;
}
}
@ -281,9 +252,9 @@ int dictionaryRemoveValue(Dictionary* dictionary, const char* key)
DictionaryEntry* entry = &(dictionary->entries[indexToRemove]);
// Free key and value (which are copies).
gDictionaryFreeProc(entry->key);
internal_free(entry->key);
if (entry->value != nullptr) {
gDictionaryFreeProc(entry->value);
internal_free(entry->value);
}
dictionary->entriesLength--;
@ -398,16 +369,16 @@ int dictionaryLoad(FILE* stream, Dictionary* dictionary, int a3)
for (int index = 0; index < dictionary->entriesLength; index++) {
DictionaryEntry* entry = &(dictionary->entries[index]);
if (entry->key != nullptr) {
gDictionaryFreeProc(entry->key);
internal_free(entry->key);
}
if (entry->value != nullptr) {
gDictionaryFreeProc(entry->value);
internal_free(entry->value);
}
}
if (dictionary->entries != nullptr) {
gDictionaryFreeProc(dictionary->entries);
internal_free(dictionary->entries);
}
if (dictionaryReadHeader(stream, dictionary) != 0) {
@ -420,7 +391,7 @@ int dictionaryLoad(FILE* stream, Dictionary* dictionary, int a3)
return 0;
}
dictionary->entries = (DictionaryEntry*)gDictionaryMallocProc(sizeof(*dictionary->entries) * dictionary->entriesCapacity);
dictionary->entries = (DictionaryEntry*)internal_malloc(sizeof(*dictionary->entries) * dictionary->entriesCapacity);
if (dictionary->entries == nullptr) {
return -1;
}
@ -442,7 +413,7 @@ int dictionaryLoad(FILE* stream, Dictionary* dictionary, int a3)
return -1;
}
entry->key = (char*)gDictionaryMallocProc(keyLength + 1);
entry->key = (char*)internal_malloc(keyLength + 1);
if (entry->key == nullptr) {
return -1;
}
@ -452,7 +423,7 @@ int dictionaryLoad(FILE* stream, Dictionary* dictionary, int a3)
}
if (dictionary->valueSize != 0) {
entry->value = gDictionaryMallocProc(dictionary->valueSize);
entry->value = internal_malloc(dictionary->valueSize);
if (entry->value == nullptr) {
return -1;
}
@ -541,18 +512,4 @@ int dictionaryWrite(FILE* stream, Dictionary* dictionary, int a3)
return 0;
}
// 0x4DA498
void dictionarySetMemoryProcs(MallocProc* mallocProc, ReallocProc* reallocProc, FreeProc* freeProc)
{
if (mallocProc != nullptr && reallocProc != nullptr && freeProc != nullptr) {
gDictionaryMallocProc = mallocProc;
gDictionaryReallocProc = reallocProc;
gDictionaryFreeProc = freeProc;
} else {
gDictionaryMallocProc = dictionaryMallocDefaultImpl;
gDictionaryReallocProc = dictionaryReallocDefaultImpl;
gDictionaryFreeProc = dictionaryFreeDefaultImpl;
}
}
} // namespace fallout

View File

@ -3,8 +3,6 @@
#include <stdio.h>
#include "memory_defs.h"
namespace fallout {
typedef int(DictionaryReadProc)(FILE* stream, void* buffer, unsigned int size, int a4);
@ -63,7 +61,6 @@ int dictionaryLoad(FILE* stream, Dictionary* dictionary, int a3);
int dictionaryWriteInt(FILE* stream, int value);
int dictionaryWriteHeader(FILE* stream, Dictionary* dictionary);
int dictionaryWrite(FILE* stream, Dictionary* dictionary, int a3);
void dictionarySetMemoryProcs(MallocProc* mallocProc, ReallocProc* reallocProc, FreeProc* freeProc);
} // namespace fallout

View File

@ -1,9 +1,6 @@
#include "game_memory.h"
#include "db.h"
#include "dictionary.h"
#include "memory.h"
#include "memory_defs.h"
#include "memory_manager.h"
namespace fallout {
@ -15,7 +12,6 @@ static void gameMemoryFree(void* ptr);
// 0x44B250
int gameMemoryInit()
{
dictionarySetMemoryProcs(internal_malloc, internal_realloc, internal_free);
memoryManagerSetProcs(gameMemoryMalloc, gameMemoryRealloc, gameMemoryFree);
return 0;