44#include < stdlib.h>
55#include < string.h>
66
7+ #include " memory.h"
78#include " platform_compat.h"
89
910namespace fallout {
@@ -13,38 +14,8 @@ namespace fallout {
1314// with a check for this value.
1415#define DICTIONARY_MARKER 0xFEBAFEBA
1516
16- static void * dictionaryMallocDefaultImpl (size_t size);
17- static void * dictionaryReallocDefaultImpl (void * ptr, size_t newSize);
18- static void dictionaryFreeDefaultImpl (void * ptr);
1917static int dictionaryFindIndexForKey (Dictionary* dictionary, const char * key, int * index);
2018
21- // 0x51E408
22- static MallocProc* gDictionaryMallocProc = dictionaryMallocDefaultImpl;
23-
24- // 0x51E40C
25- static ReallocProc* gDictionaryReallocProc = dictionaryReallocDefaultImpl;
26-
27- // 0x51E410
28- static FreeProc* gDictionaryFreeProc = dictionaryFreeDefaultImpl;
29-
30- // 0x4D9B90
31- static void * dictionaryMallocDefaultImpl (size_t size)
32- {
33- return malloc (size);
34- }
35-
36- // 0x4D9B98
37- static void * dictionaryReallocDefaultImpl (void * ptr, size_t newSize)
38- {
39- return realloc (ptr, newSize);
40- }
41-
42- // 0x4D9BA0
43- static void dictionaryFreeDefaultImpl (void * ptr)
44- {
45- free (ptr);
46- }
47-
4819// 0x4D9BA8
4920int dictionaryInit (Dictionary* dictionary, int initialCapacity, size_t valueSize, DictionaryIO* io)
5021{
@@ -64,7 +35,7 @@ int dictionaryInit(Dictionary* dictionary, int initialCapacity, size_t valueSize
6435 int rc = 0 ;
6536
6637 if (initialCapacity != 0 ) {
67- dictionary->entries = (DictionaryEntry*)gDictionaryMallocProc (sizeof (*dictionary->entries ) * initialCapacity);
38+ dictionary->entries = (DictionaryEntry*)internal_malloc (sizeof (*dictionary->entries ) * initialCapacity);
6839 if (dictionary->entries == nullptr ) {
6940 rc = -1 ;
7041 }
@@ -90,7 +61,7 @@ int dictionarySetCapacity(Dictionary* dictionary, int newCapacity)
9061 return -1 ;
9162 }
9263
93- DictionaryEntry* entries = (DictionaryEntry*)gDictionaryReallocProc (dictionary->entries , sizeof (*dictionary->entries ) * newCapacity);
64+ DictionaryEntry* entries = (DictionaryEntry*)internal_realloc (dictionary->entries , sizeof (*dictionary->entries ) * newCapacity);
9465 if (entries == nullptr ) {
9566 return -1 ;
9667 }
@@ -111,16 +82,16 @@ int dictionaryFree(Dictionary* dictionary)
11182 for (int index = 0 ; index < dictionary->entriesLength ; index++) {
11283 DictionaryEntry* entry = &(dictionary->entries [index]);
11384 if (entry->key != nullptr ) {
114- gDictionaryFreeProc (entry->key );
85+ internal_free (entry->key );
11586 }
11687
11788 if (entry->value != nullptr ) {
118- gDictionaryFreeProc (entry->value );
89+ internal_free (entry->value );
11990 }
12091 }
12192
12293 if (dictionary->entries != nullptr ) {
123- gDictionaryFreeProc (dictionary->entries );
94+ internal_free (dictionary->entries );
12495 }
12596
12697 memset (dictionary, 0 , sizeof (*dictionary));
@@ -223,7 +194,7 @@ int dictionaryAddValue(Dictionary* dictionary, const char* key, const void* valu
223194 }
224195
225196 // Make a copy of the key.
226- char * keyCopy = (char *)gDictionaryMallocProc (strlen (key) + 1 );
197+ char * keyCopy = (char *)internal_malloc (strlen (key) + 1 );
227198 if (keyCopy == nullptr ) {
228199 return -1 ;
229200 }
@@ -233,9 +204,9 @@ int dictionaryAddValue(Dictionary* dictionary, const char* key, const void* valu
233204 // Make a copy of the value.
234205 void * valueCopy = nullptr ;
235206 if (value != nullptr && dictionary->valueSize != 0 ) {
236- valueCopy = gDictionaryMallocProc (dictionary->valueSize );
207+ valueCopy = internal_malloc (dictionary->valueSize );
237208 if (valueCopy == nullptr ) {
238- gDictionaryFreeProc (keyCopy);
209+ internal_free (keyCopy);
239210 return -1 ;
240211 }
241212 }
@@ -281,9 +252,9 @@ int dictionaryRemoveValue(Dictionary* dictionary, const char* key)
281252 DictionaryEntry* entry = &(dictionary->entries [indexToRemove]);
282253
283254 // Free key and value (which are copies).
284- gDictionaryFreeProc (entry->key );
255+ internal_free (entry->key );
285256 if (entry->value != nullptr ) {
286- gDictionaryFreeProc (entry->value );
257+ internal_free (entry->value );
287258 }
288259
289260 dictionary->entriesLength --;
@@ -398,16 +369,16 @@ int dictionaryLoad(FILE* stream, Dictionary* dictionary, int a3)
398369 for (int index = 0 ; index < dictionary->entriesLength ; index++) {
399370 DictionaryEntry* entry = &(dictionary->entries [index]);
400371 if (entry->key != nullptr ) {
401- gDictionaryFreeProc (entry->key );
372+ internal_free (entry->key );
402373 }
403374
404375 if (entry->value != nullptr ) {
405- gDictionaryFreeProc (entry->value );
376+ internal_free (entry->value );
406377 }
407378 }
408379
409380 if (dictionary->entries != nullptr ) {
410- gDictionaryFreeProc (dictionary->entries );
381+ internal_free (dictionary->entries );
411382 }
412383
413384 if (dictionaryReadHeader (stream, dictionary) != 0 ) {
@@ -420,7 +391,7 @@ int dictionaryLoad(FILE* stream, Dictionary* dictionary, int a3)
420391 return 0 ;
421392 }
422393
423- dictionary->entries = (DictionaryEntry*)gDictionaryMallocProc (sizeof (*dictionary->entries ) * dictionary->entriesCapacity );
394+ dictionary->entries = (DictionaryEntry*)internal_malloc (sizeof (*dictionary->entries ) * dictionary->entriesCapacity );
424395 if (dictionary->entries == nullptr ) {
425396 return -1 ;
426397 }
@@ -442,7 +413,7 @@ int dictionaryLoad(FILE* stream, Dictionary* dictionary, int a3)
442413 return -1 ;
443414 }
444415
445- entry->key = (char *)gDictionaryMallocProc (keyLength + 1 );
416+ entry->key = (char *)internal_malloc (keyLength + 1 );
446417 if (entry->key == nullptr ) {
447418 return -1 ;
448419 }
@@ -452,7 +423,7 @@ int dictionaryLoad(FILE* stream, Dictionary* dictionary, int a3)
452423 }
453424
454425 if (dictionary->valueSize != 0 ) {
455- entry->value = gDictionaryMallocProc (dictionary->valueSize );
426+ entry->value = internal_malloc (dictionary->valueSize );
456427 if (entry->value == nullptr ) {
457428 return -1 ;
458429 }
@@ -541,18 +512,4 @@ int dictionaryWrite(FILE* stream, Dictionary* dictionary, int a3)
541512 return 0 ;
542513}
543514
544- // 0x4DA498
545- void dictionarySetMemoryProcs (MallocProc* mallocProc, ReallocProc* reallocProc, FreeProc* freeProc)
546- {
547- if (mallocProc != nullptr && reallocProc != nullptr && freeProc != nullptr ) {
548- gDictionaryMallocProc = mallocProc;
549- gDictionaryReallocProc = reallocProc;
550- gDictionaryFreeProc = freeProc;
551- } else {
552- gDictionaryMallocProc = dictionaryMallocDefaultImpl;
553- gDictionaryReallocProc = dictionaryReallocDefaultImpl;
554- gDictionaryFreeProc = dictionaryFreeDefaultImpl;
555- }
556- }
557-
558515} // namespace fallout
0 commit comments