| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/mem/mem.cpp $ |
| 21 | * $Revision: 56 $ |
| 22 | * $Date: 1/03/02 5:27p $ |
| 23 | * $Author: Matt $ |
| 24 | * |
| 25 | * Memory library |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/mem/mem.cpp $ |
| 28 | * |
| 29 | * 56 1/03/02 5:27p Matt |
| 30 | * Add a longlong cast to prevent math overflow. |
| 31 | * |
| 32 | * 55 4/19/00 5:33p Matt |
| 33 | * From Duane for 1.4 |
| 34 | * Close renderer on malloc fail error exit |
| 35 | * Mac changes |
| 36 | * |
| 37 | * 54 10/21/99 2:25p Kevin |
| 38 | * Mac merge |
| 39 | * |
| 40 | * 53 8/10/99 5:12p Jeff |
| 41 | * added a debug function to dump the current state of the mem library to |
| 42 | * file |
| 43 | * |
| 44 | * 52 7/28/99 2:22p Kevin |
| 45 | * Macintosh Stuff |
| 46 | * |
| 47 | * 51 5/13/99 5:06p Ardussi |
| 48 | * changes for compiling on the Mac |
| 49 | * |
| 50 | * 50 5/02/99 12:24a Jason |
| 51 | * mem was incorrectly choosing lowmem mode on 64 meg machines |
| 52 | * |
| 53 | * 49 4/30/99 5:07p Kevin |
| 54 | * misc dedicated server, networking and low memory enhancements |
| 55 | * |
| 56 | * 48 4/16/99 10:42p Jeff |
| 57 | * moved global variables out of window scope code to all builds for linux |
| 58 | * |
| 59 | * 47 4/15/99 11:09p Jeff |
| 60 | * fixed mem_strdup for linux |
| 61 | * |
| 62 | * 46 4/15/99 9:22p Jeff |
| 63 | * Changes for Linux version |
| 64 | * |
| 65 | * 45 4/12/99 2:23p Kevin |
| 66 | * fixed missing symbol |
| 67 | * |
| 68 | * 44 4/12/99 1:13p Kevin |
| 69 | * Moved call to GetLastError and played with some debugging |
| 70 | * |
| 71 | * 43 3/17/99 11:19a 3dsmax |
| 72 | * Took out debugging |
| 73 | * |
| 74 | * 42 3/16/99 4:49p Kevin |
| 75 | * Fixed realloc |
| 76 | * |
| 77 | * 41 3/15/99 4:32p Jeff |
| 78 | * fixed code so mem library compiles correctly |
| 79 | * |
| 80 | * 40 3/15/99 11:24a Kevin |
| 81 | * Improved memory leak detection for mem_strdup and turned on debugging |
| 82 | * |
| 83 | * 39 3/14/99 2:31p Kevin |
| 84 | * Added mem_heapcheck() function for debugging |
| 85 | * |
| 86 | * 38 3/02/99 5:50p Kevin |
| 87 | * Ouch. Duplicate structures existed and were conflicting. |
| 88 | * |
| 89 | * 37 2/28/99 6:30p Kevin |
| 90 | * Added a call to Error() when unable to alloc memory |
| 91 | * |
| 92 | * 36 1/09/99 4:40p Jeff |
| 93 | * added some ifdefs and fixes to get files to compile under Linux |
| 94 | * |
| 95 | * 35 1/05/99 3:46p Matt |
| 96 | * For Kevin, added call to GetLastError() |
| 97 | * |
| 98 | * 34 11/05/98 2:00p Sean |
| 99 | * |
| 100 | * 33 11/05/98 11:23a Kevin |
| 101 | * fixed bug that caused it not to work under NT |
| 102 | * |
| 103 | * 32 10/30/98 11:23a Samir |
| 104 | * |
| 105 | * 31 10/30/98 11:10a Kevin |
| 106 | * doh! fixed ifdef |
| 107 | * |
| 108 | * 30 10/30/98 11:01a Kevin |
| 109 | * took out mem debug stuff for nt users (temp) |
| 110 | * |
| 111 | * 29 10/28/98 5:01p Kevin |
| 112 | * Improved debugging aids in mem lib |
| 113 | * |
| 114 | * 28 10/21/98 7:18p Matt |
| 115 | * Added quick-exit system to not free individual mem chunks on exit, |
| 116 | * since the whole heap gets freed at the end. |
| 117 | * |
| 118 | * 27 10/18/98 9:11p Matt |
| 119 | * Took the program name out of some error messages. |
| 120 | * |
| 121 | * 26 10/18/98 6:21p Matt |
| 122 | * Changed a Debug_ConsolePrintf() to mprintf() since the former doesn't |
| 123 | * exist in a Release build and the latter compiles out. |
| 124 | * |
| 125 | * 25 10/17/98 12:46p Kevin |
| 126 | * Beta 4 fixes |
| 127 | * |
| 128 | * 24 10/15/98 12:15p Kevin |
| 129 | * put in heap checking code |
| 130 | * |
| 131 | * 23 10/15/98 11:59a Kevin |
| 132 | * removed useage of timer_GetTime and FindArg |
| 133 | * |
| 134 | * 22 10/14/98 11:25p Jeff |
| 135 | * commented out call to HeapDestroy() to keep NT users from crashing on |
| 136 | * exit because it destroys the heap before Sound thread closes |
| 137 | * |
| 138 | * 21 10/14/98 7:20p Kevin |
| 139 | * More dsp changes... |
| 140 | * |
| 141 | * 20 10/14/98 4:39p Matt |
| 142 | * Changed OutrageMessageBox() calls to use either Error() or |
| 143 | * EditorMessageBox() |
| 144 | * |
| 145 | * 19 10/13/98 3:42p Kevin |
| 146 | * bug fixes |
| 147 | * |
| 148 | * 18 10/12/98 8:39p Kevin |
| 149 | * removed mprintf's and fixed some smallish bugs |
| 150 | * |
| 151 | * 17 10/12/98 11:32a Kevin |
| 152 | * doh! heap size was 160 megs |
| 153 | * |
| 154 | * 16 10/12/98 11:25a Kevin |
| 155 | * doh! |
| 156 | * |
| 157 | * 15 10/12/98 10:55a Kevin |
| 158 | * Don't use memory lib for editor currently |
| 159 | * |
| 160 | * 14 10/12/98 10:22a Samir |
| 161 | * made mem_strdup take a const char pointer. |
| 162 | * |
| 163 | * 13 10/09/98 7:40p Kevin |
| 164 | * |
| 165 | * 12 10/09/98 6:55p Kevin |
| 166 | * fized bug with zero byte mallocs and mem_size |
| 167 | * |
| 168 | * 11 10/09/98 3:32p Kevin |
| 169 | * New memory library |
| 170 | * |
| 171 | * 10 10/08/98 4:24p Kevin |
| 172 | * Changed code to comply with memory library usage. Always use mem_malloc |
| 173 | * , mem_free and mem_strdup |
| 174 | * |
| 175 | * 9 10/08/98 2:40p Kevin |
| 176 | * |
| 177 | * 8 10/08/98 12:00p Kevin |
| 178 | * Demo system work |
| 179 | * |
| 180 | * 7 9/22/98 3:55p Samir |
| 181 | * ifdef out stuff for non-debug version. |
| 182 | * |
| 183 | * 6 8/31/98 12:42p Samir |
| 184 | * added some debug code. |
| 185 | * |
| 186 | * 5 7/31/98 5:44p Samir |
| 187 | * improved memory debugging. |
| 188 | * |
| 189 | * $NoKeywords: $ |
| 190 | */ |
| 191 | #if !defined(POSIXcxx . . $ `<built-in>:478:9`!) |
| 192 | #include <new.h> |
| 193 | #endif |
| 194 | #if defined(MACOSX) |
| 195 | |
| 196 | #include <malloc/malloc.h> |
| 197 | #else |
| 198 | #include <malloc.h> |
| 199 | #endif |
| 200 | #include <stdio.h> |
| 201 | #include <string.h> |
| 202 | #include <stdlib.h> |
| 203 | #ifdef WIN32 |
| 204 | // Non-Linux Includes |
| 205 | #include <windows.h> |
| 206 | #include <crtdbg.h> |
| 207 | #else |
| 208 | #endif |
| 209 | #include <cstdint> |
| 210 | |
| 211 | #include "mem.h"cxx . . $ `<file>/mem/mem.h`/ |
| 212 | #include "pserror.h"cxx . . $ `<file>/misc/pserror.h`/ |
| 213 | |
| 214 | // #define MEM_DEBUG |
| 215 | |
| 216 | #ifdef MEM_USE_RTLcxx . . $ `<built-in>:477:9`! |
| 217 | #pragma message("mem.cpp: Compiling For Run-Time Library usage") |
| 218 | #endif |
| 219 | |
| 220 | #ifdef MEM_DEBUG |
| 221 | #pragma message("mem.cpp: Compiling with Debug Settings") |
| 222 | #endif |
| 223 | |
| 224 | #ifdef MEM_LOGFILE |
| 225 | #pragma message("mem.cpp: Compiling with logfile support") |
| 226 | #endif |
| 227 | |
| 228 | int Total_mem_usedcxx . . $ Total_mem_used. = 0; |
| 229 | int Mem_high_water_markcxx . . $ Mem_high_water_mark. = 0; |
| 230 | |
| 231 | #define MEM_NO_MEMORY_PTRcxx . . $ `mem/mem.cpp:231:9`! 0xdeadbeef |
| 232 | #define MEM_MAX_MALLOCScxx . . $ `mem/mem.cpp:232:9`! 199999 |
| 233 | #define MEM_GAURDIAN_SIGcxx . . $ `mem/mem.cpp:233:9`! 0x2bad |
| 234 | #define MEM_MALLOC_TO_SORTcxx . . $ `mem/mem.cpp:234:9`! 10000 |
| 235 | |
| 236 | struct mem_alloc_infocxx . . $ mem_alloc_info# { |
| 237 | int lencxx . . $ mem_alloc_info#len.; |
| 238 | void *ptrcxx . . $ mem_alloc_info#ptr.; |
| 239 | uint16_tcxx . . $ uint16_t# linecxx . . $ mem_alloc_info#line.; |
| 240 | char filecxx . . $ mem_alloc_info#file.[17]; |
| 241 | }; |
| 242 | |
| 243 | static void *Mem_failsafe_blockcxx . . $ Mem_failsafe_block. = nullptr; |
| 244 | |
| 245 | bool Mem_low_memory_modecxx . . $ Mem_low_memory_mode. = false; |
| 246 | bool Mem_superlow_memory_modecxx . . $ Mem_superlow_memory_mode. = false; |
| 247 | // If this is set, the mem library ignores mem_free() calls. All the memory is then freed at once on exit. |
| 248 | bool Mem_quick_exitcxx . . $ Mem_quick_exit. = false; |
| 249 | |
| 250 | #if defined(POSIXcxx . . $ `<built-in>:478:9`!) |
| 251 | // Linux memory management |
| 252 | int LnxTotalMemUsedcxx . . $ LnxTotalMemUsed.; |
| 253 | |
| 254 | void mem_shutdowncxx . . $ mem_shutdown(49f6e7a06ebc5aa8).() {} |
| 255 | |
| 256 | void mem_Initcxx . . $ mem_Init(49f6e7a06ebc5aa8).() { LnxTotalMemUsedcxx . . $ LnxTotalMemUsed. = 0; } |
| 257 | |
| 258 | int mem_GetTotalMemoryUsedcxx . . $ mem_GetTotalMemoryUsed(b126dc7c1de90089).() { return LnxTotalMemUsedcxx . . $ LnxTotalMemUsed.; } |
| 259 | |
| 260 | void *mem_malloc_subcxx . . $ mem_malloc_sub(7b60db397a413c1c).(int size, const char *file, int line) { |
| 261 | void *new_mem = malloccxx . . $ malloc(9f008b2a9698428b).(size); |
| 262 | if (!new_mem) { |
| 263 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Out of memory allocating %d bytes: line %d in %s\n", size, line, file); |
| 264 | Int3cxx . . $ `misc/pserror.h:247:9`!(); |
| 265 | return nullptr; |
| 266 | } |
| 267 | LnxTotalMemUsedcxx . . $ LnxTotalMemUsed. += size; |
| 268 | return new_mem; |
| 269 | } |
| 270 | |
| 271 | void mem_free_subcxx . . $ mem_free_sub(5d22bdacc48458e8).(void *memblock) { |
| 272 | if (memblock) { |
| 273 | #if defined(MACOSX) |
| 274 | LnxTotalMemUsed -= malloc_size(memblock); |
| 275 | #else |
| 276 | LnxTotalMemUsedcxx . . $ LnxTotalMemUsed. -= malloc_usable_size(memblock); |
| 277 | #endif |
| 278 | freecxx . . $ free(bd21765a0afc8e3c).(memblock); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | void mem_error_msgcxx . . $ mem_error_msg(7b85637613228108).(const char *file, int line, int size) { |
| 283 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Memory error (size=%d) line %d in %s\n", size, line, file); |
| 284 | Int3cxx . . $ `misc/pserror.h:247:9`!(); |
| 285 | } |
| 286 | |
| 287 | char *mem_strdup_subcxx . . $ mem_strdup_sub(2e2de56da327d927).(const char *string, const char *file, int line) { |
| 288 | char *ret = strdup(string); |
| 289 | if (!ret) { |
| 290 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Out of memory allocating %d bytes: line %d in %s\n", strlen(string) + 1, line, file); |
| 291 | Int3cxx . . $ `misc/pserror.h:247:9`!(); |
| 292 | return nullptr; |
| 293 | } |
| 294 | return ret; |
| 295 | } |
| 296 | |
| 297 | void *mem_realloc_subcxx . . $ mem_realloc_sub(7f6def848333c872).(void *mem, int size) { return realloccxx . . $ realloc(42b2a9dd8f084bf2).(mem, size); } |
| 298 | |
| 299 | int mem_size_subcxx . . $ mem_size_sub(dedf1e9b1c614a07).(void *memblock) { |
| 300 | #ifdef MACOSX |
| 301 | return malloc_size(memblock); |
| 302 | #else |
| 303 | return malloc_usable_size(memblock); |
| 304 | #endif |
| 305 | } |
| 306 | |
| 307 | bool mem_dumpmallocstofilecxx . . $ mem_dumpmallocstofile(c6b7e35ff6de5671).(char *filename) { return false; } |
| 308 | |
| 309 | #pragma mark - |
| 310 | |
| 311 | #else // defined(POSIX) |
| 312 | // Windows memory management |
| 313 | |
| 314 | // Uncomment this to detect memory leaks and memory overwrites. Slows down mallocs and frees a little. |
| 315 | // #define MEM_DEBUG 1 |
| 316 | // Uncomment this if you want everything written to "memory.out" |
| 317 | // #define MEM_LOGFILE 1 |
| 318 | |
| 319 | #ifdef MEM_USE_RTL |
| 320 | #undef MEM_DEBUG |
| 321 | #endif |
| 322 | |
| 323 | class MemClass { |
| 324 | public: |
| 325 | ~MemClass(); |
| 326 | }; |
| 327 | class MemClass Mymem; |
| 328 | /* modify these lines to establish data type */ |
| 329 | typedef mem_alloc_info *T; /* type of item to be stored */ |
| 330 | typedef int hashTableIndex; /* index into hash table */ |
| 331 | #define compEQ(a, b) ((a)->ptr == (b)->ptr) |
| 332 | struct Node { |
| 333 | struct Node *next; /* next node */ |
| 334 | T data; /* data stored in node */ |
| 335 | }; |
| 336 | Node *findNode(T data); |
| 337 | void deleteNode(T data); |
| 338 | Node *insertNode(T data); |
| 339 | hashTableIndex hash(T data); |
| 340 | Node **hashTable; |
| 341 | int hashTableSize = MEM_MAX_MALLOCS; |
| 342 | #ifdef MEM_DEBUG |
| 343 | mem_alloc_info mem_info[MEM_MAX_MALLOCS]; |
| 344 | int Mem_next_slot = 0; |
| 345 | int Mem_highest_used_slot = 0; |
| 346 | FILE *mem_out = NULL; |
| 347 | int Mem_mallocs_since_last_sort = 0; |
| 348 | /* |
| 349 | #undef new |
| 350 | void *operator new(uint32_t size, char *file, int line) |
| 351 | { |
| 352 | return mem_malloc_sub(size, file, line); |
| 353 | } |
| 354 | void *operator new [](uint32_t size, char *file, int line) |
| 355 | { |
| 356 | return mem_malloc_sub(size, file, line); |
| 357 | } |
| 358 | void *operator new(uint32_t size) |
| 359 | { |
| 360 | return mem_malloc_sub(size,"unknown",0); |
| 361 | } |
| 362 | void operator delete(void *ptr) |
| 363 | { |
| 364 | mem_free_sub(ptr); |
| 365 | } |
| 366 | */ |
| 367 | #endif |
| 368 | int handle_program_memory_depletion(size_t size); |
| 369 | HANDLE Heap; |
| 370 | void mem_Init() { |
| 371 | #if defined(WIN32) || defined(POSIX) |
| 372 | // allocate failsafe block for memory used by any functions after we run out of memory. |
| 373 | // (printf for instance needs heap memory, as well as our error library.) |
| 374 | MEMORYSTATUS ms; |
| 375 | #ifdef MEM_DEBUG |
| 376 | hashTable = (Node **)malloc(hashTableSize * sizeof(Node *)); |
| 377 | for (int a = 0; a < hashTableSize; a++) { |
| 378 | hashTable[a] = NULL; |
| 379 | } |
| 380 | #ifdef MEM_LOGFILE |
| 381 | { |
| 382 | mem_out = fopen("memory.out", "wt"); |
| 383 | ASSERT(mem_out); |
| 384 | } |
| 385 | #endif |
| 386 | for (int i = 0; i < MEM_MAX_MALLOCS; i++) { |
| 387 | mem_info[i].ptr = (void *)MEM_NO_MEMORY_PTR; |
| 388 | } |
| 389 | #endif |
| 390 | |
| 391 | GlobalMemoryStatus(&ms); |
| 392 | Heap = HeapCreate(HEAP_NO_SERIALIZE, 16000000, 0); // GetProcessHeap(); |
| 393 | if (!Heap) { |
| 394 | mprintf(0, "Unable to create memory heap! error: %d\n", GetLastError()); |
| 395 | Error("Unable to create memory heap; your system may not have enough memory to run."); |
| 396 | } |
| 397 | ASSERT(Heap); |
| 398 | HeapCompact(Heap, 0); |
| 399 | mprintf(0, "System Memory Status:\n"); |
| 400 | mprintf(0, "Percent of memory in use: %d\n", ms.dwMemoryLoad); |
| 401 | mprintf(0, "Bytes of physical memory : %d\n", ms.dwTotalPhys); |
| 402 | mprintf(0, "Free physical memory bytes : %d\n", ms.dwAvailPhys); |
| 403 | mprintf(0, "Available virtual memory : %d\n", ms.dwAvailPageFile); |
| 404 | |
| 405 | // See if there is enough memory to run |
| 406 | if (((int64_t)ms.dwAvailPageFile + ms.dwAvailPhys) < (50 * 1024 * 1024)) { |
| 407 | Error("Your system doesn't have enough available memory to continue.\r\n\r\nMemory Statistics:\r\nTotal Physical: " |
| 408 | "%d\r\nAvaliable Physical: %d\r\nAvailable Virtual: %d\r\n\r\nYou may be able to continue by rebooting, or " |
| 409 | "freeing up some disk space.", |
| 410 | ms.dwTotalPhys, ms.dwAvailPhys, ms.dwAvailPageFile); |
| 411 | return; |
| 412 | } else if ((ms.dwTotalPhys) < (62 * 1024 * 1024)) { |
| 413 | mprintf(0, "Using low memory mode!\n"); |
| 414 | Mem_low_memory_mode = true; |
| 415 | return; |
| 416 | } else if ((ms.dwTotalPhys) < (46 * 1024 * 1024)) { |
| 417 | mprintf(0, "Using super low memory mode!\n"); |
| 418 | Mem_low_memory_mode = true; |
| 419 | Mem_superlow_memory_mode = true; |
| 420 | return; |
| 421 | } |
| 422 | #else |
| 423 | #ifdef MEM_DEBUG |
| 424 | hashTable = (Node **)malloc(hashTableSize * sizeof(Node *)); |
| 425 | for (int a = 0; a < hashTableSize; a++) { |
| 426 | hashTable[a] = NULL; |
| 427 | } |
| 428 | #ifdef MEM_LOGFILE |
| 429 | { |
| 430 | mem_out = fopen("memory.out", "wt"); |
| 431 | ASSERT(mem_out); |
| 432 | } |
| 433 | #endif |
| 434 | for (int i = 0; i < MEM_MAX_MALLOCS; i++) { |
| 435 | mem_info[i].ptr = (void *)MEM_NO_MEMORY_PTR; |
| 436 | } |
| 437 | #endif |
| 438 | Heap = NULL; |
| 439 | |
| 440 | Mem_failsafe_block = mem_malloc(128); |
| 441 | if (!Mem_failsafe_block) { |
| 442 | Error("No available heap memory."); |
| 443 | } |
| 444 | return; |
| 445 | #endif |
| 446 | #if defined(WIN32) || defined(POSIX) |
| 447 | Mem_failsafe_block = mem_malloc(128); |
| 448 | if (!Mem_failsafe_block) { |
| 449 | Error("No available heap memory."); |
| 450 | } |
| 451 | _set_new_handler(handle_program_memory_depletion); |
| 452 | int flags = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); |
| 453 | #ifdef _DEBUG |
| 454 | flags |= _CRTDBG_ALLOC_MEM_DF; |
| 455 | #endif |
| 456 | _CrtSetDbgFlag(flags); |
| 457 | // atexit(mem_Free); |
| 458 | #endif |
| 459 | } |
| 460 | // Returns the number of dynamically allocated bytes |
| 461 | int mem_GetTotalMemoryUsed() { return Total_mem_used; } |
| 462 | bool bail_now = false; |
| 463 | // Allocates a block of memory and returns a pointer to it |
| 464 | void *mem_malloc_sub(int size, const char *file, int line) { |
| 465 | // float mstart = timer_GetTime(); |
| 466 | void *retp; |
| 467 | #ifdef MEM_USE_RTL |
| 468 | retp = malloc(size); |
| 469 | return retp; |
| 470 | #else |
| 471 | #if defined(WIN32) |
| 472 | if (!Heap) { |
| 473 | return 0; |
| 474 | } |
| 475 | #endif |
| 476 | if (size < 0) { |
| 477 | mprintf(0, "Some bozo is trying to allocate a negative length of memory!\n"); |
| 478 | mprintf(0, "Offending file: %s line: %d\n", file, line); |
| 479 | Int3(); |
| 480 | return NULL; |
| 481 | } else if (size == 0) { |
| 482 | mprintf(0, "Warning: Zero byte malloc in %s line %d!\n", file, line); |
| 483 | Int3(); |
| 484 | return (void *)MEM_NO_MEMORY_PTR; |
| 485 | } |
| 486 | mem_alloc_info *mi = NULL; |
| 487 | mem_alloc_info no_track_mi; |
| 488 | bool track_node = true; |
| 489 | #ifdef MEM_DEBUG |
| 490 | if (!bail_now) { |
| 491 | if (mem_info[Mem_next_slot].ptr == (void *)MEM_NO_MEMORY_PTR) { |
| 492 | mi = &mem_info[Mem_next_slot]; |
| 493 | Mem_next_slot++; |
| 494 | if (Mem_next_slot > Mem_highest_used_slot) |
| 495 | Mem_highest_used_slot = Mem_next_slot; |
| 496 | } else { |
| 497 | for (int i = 0; i < MEM_MAX_MALLOCS; i++) { |
| 498 | if (mem_info[i].ptr == (void *)MEM_NO_MEMORY_PTR) { |
| 499 | mi = &mem_info[i]; |
| 500 | Mem_next_slot = i + 1; |
| 501 | if (i > Mem_highest_used_slot) |
| 502 | Mem_highest_used_slot = i; |
| 503 | break; |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | if (bail_now || (mi == NULL)) |
| 509 | #else |
| 510 | |
| 511 | #endif |
| 512 | { |
| 513 | |
| 514 | mi = &no_track_mi; |
| 515 | if (bail_now == false) { |
| 516 | track_node = false; |
| 517 | #ifdef MEM_DEBUG |
| 518 | mprintf(0, "Out of memory tracking slots!!!!\n"); |
| 519 | #endif |
| 520 | } |
| 521 | bail_now = true; |
| 522 | } |
| 523 | #ifndef MEM_DEBUG |
| 524 | mi->ptr = HeapAlloc(Heap, HEAP_NO_SERIALIZE, size); |
| 525 | #else |
| 526 | mi->ptr = HeapAlloc(Heap, HEAP_NO_SERIALIZE, size + 2); |
| 527 | mi->len = size; |
| 528 | uint16_t mem_sig = MEM_GAURDIAN_SIG; |
| 529 | memcpy(((char *)mi->ptr) + size, (void *)&mem_sig, 2); |
| 530 | int flen = strlen(file); |
| 531 | int lofs = 0; |
| 532 | // Strip down the filename to be < 16 bytes |
| 533 | if (flen > 16) |
| 534 | lofs = flen - 16; |
| 535 | |
| 536 | strcpy(mi->file, file + lofs); |
| 537 | mi->line = line; |
| 538 | #ifdef MEM_LOGFILE |
| 539 | fprintf(mem_out, "%s,%d,%d,%d\n", mi->file, mi->line, size, HeapSize(Heap, 0, mi->ptr)); |
| 540 | #endif |
| 541 | #endif |
| 542 | int errors; |
| 543 | if (mi->ptr == NULL) { |
| 544 | errors = GetLastError(); |
| 545 | mprintf(0, "Unable to alloc memory in mem_malloc_sub()!\n"); |
| 546 | Int3(); |
| 547 | ASSERT(mi->ptr); |
| 548 | Error("Out of memory, unable to continue."); |
| 549 | mem_error_msg(file, line, size); |
| 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | retp = mi->ptr; |
| 554 | |
| 555 | Total_mem_used += size; |
| 556 | if (Mem_high_water_mark < Total_mem_used) { |
| 557 | Mem_high_water_mark = Total_mem_used; |
| 558 | } |
| 559 | #ifdef MEM_DEBUG |
| 560 | if (track_node) |
| 561 | insertNode(mi); |
| 562 | #endif |
| 563 | return retp; |
| 564 | #endif |
| 565 | } |
| 566 | // Frees a previously allocated block of memory |
| 567 | void mem_free_sub(void *memblock) { |
| 568 | #ifndef MEM_DEBUG |
| 569 | if (Mem_quick_exit) |
| 570 | return; |
| 571 | #endif |
| 572 | #ifdef MEM_USE_RTL |
| 573 | free(memblock); |
| 574 | return; |
| 575 | #else |
| 576 | #if defined(WIN32) |
| 577 | if (!Heap) { |
| 578 | return; |
| 579 | } |
| 580 | #endif |
| 581 | if (memblock == 0) |
| 582 | return; |
| 583 | if ((void *)MEM_NO_MEMORY_PTR == memblock) { |
| 584 | return; |
| 585 | } |
| 586 | #ifdef MEM_DEBUG |
| 587 | mem_alloc_info findmem; |
| 588 | mem_alloc_info *freemem; |
| 589 | findmem.ptr = memblock; |
| 590 | Node *mynode = findNode(&findmem); |
| 591 | if (mynode) { |
| 592 | freemem = mynode->data; |
| 593 | freemem->ptr = (void *)MEM_NO_MEMORY_PTR; |
| 594 | uint16_t mem_sig = MEM_GAURDIAN_SIG; |
| 595 | if (memcmp((char *)memblock + freemem->len, &mem_sig, 2) != 0) { |
| 596 | // Corrupted memory found when we went to free it. |
| 597 | mprintf(0, "Memory block found to be damaged when it was freed!\n"); |
| 598 | Int3(); |
| 599 | } |
| 600 | Total_mem_used -= freemem->len; |
| 601 | HeapFree(Heap, HEAP_NO_SERIALIZE, memblock); |
| 602 | deleteNode(mynode->data); |
| 603 | return; |
| 604 | } else { |
| 605 | mprintf(0, "Warning, hash lookup of memory block failed!\n"); |
| 606 | HeapFree(Heap, HEAP_NO_SERIALIZE, memblock); |
| 607 | return; |
| 608 | } |
| 609 | #endif |
| 610 | HeapFree(Heap, HEAP_NO_SERIALIZE, memblock); |
| 611 | #endif |
| 612 | } |
| 613 | int handle_program_memory_depletion(size_t size) { |
| 614 | // Release character buffer memory. |
| 615 | mem_free(Mem_failsafe_block); |
| 616 | Mem_failsafe_block = NULL; |
| 617 | Error("Unable to allocate %d bytes of memory.", size); |
| 618 | // Tell new to stop allocation attempts. |
| 619 | return 0; |
| 620 | } |
| 621 | // prints out a memory error message |
| 622 | void mem_error_msg(const char *file, int line, int size) { |
| 623 | Error("Attempt to allocate %d bytes of memory in %s line %d failed.", size, file, line); |
| 624 | } |
| 625 | // int strdup_malloc_line = 0; |
| 626 | char *mem_strdup_sub(const char *src, const char *file, int line) { |
| 627 | #if defined(WIN32) |
| 628 | if (!Heap) { |
| 629 | return 0; |
| 630 | } |
| 631 | #endif |
| 632 | int len = strlen(src) + 1; |
| 633 | // strdup_malloc_line = __LINE__+1; |
| 634 | // Leave this line here! |
| 635 | char *dest = (char *)mem_malloc_sub(len, file, line); |
| 636 | strcpy(dest, src); |
| 637 | return dest; |
| 638 | } |
| 639 | void *mem_realloc_sub(void *memblock, int size) { |
| 640 | #ifdef MEM_USE_RTL |
| 641 | void *retp = realloc(memblock, size); |
| 642 | return retp; |
| 643 | #else |
| 644 | #if defined(WIN32) |
| 645 | if (!Heap) { |
| 646 | return 0; |
| 647 | } |
| 648 | #endif |
| 649 | if ((void *)MEM_NO_MEMORY_PTR == memblock) { |
| 650 | return mem_malloc(size); |
| 651 | } |
| 652 | #ifdef MEM_DEBUG |
| 653 | for (int i = 0; i < MEM_MAX_MALLOCS; i++) { |
| 654 | if (mem_info[i].ptr == (void *)memblock) { |
| 655 | mem_info[i].ptr = (void *)MEM_NO_MEMORY_PTR; |
| 656 | Total_mem_used -= mem_info[i].len; |
| 657 | |
| 658 | deleteNode(&mem_info[i]); |
| 659 | |
| 660 | mem_info[i].ptr = HeapReAlloc(Heap, 0, memblock, size + 2); |
| 661 | uint16_t mem_sig = MEM_GAURDIAN_SIG; |
| 662 | memcpy(((char *)mem_info[i].ptr) + size, (void *)&mem_sig, 2); |
| 663 | mem_info[i].len = size; |
| 664 | Total_mem_used += size; |
| 665 | |
| 666 | insertNode(&mem_info[i]); |
| 667 | |
| 668 | return mem_info[i].ptr; |
| 669 | } |
| 670 | } |
| 671 | #endif |
| 672 | void *retp = HeapReAlloc(Heap, 0, memblock, size); |
| 673 | return retp; |
| 674 | #endif |
| 675 | } |
| 676 | |
| 677 | int mem_size_sub(void *memblock) { |
| 678 | #ifdef MEM_USE_RTL |
| 679 | return _msize(memblock); |
| 680 | #else // MEM_USE_RTL |
| 681 | #if defined(WIN32) |
| 682 | if (!Heap) { |
| 683 | return 0; |
| 684 | } |
| 685 | #endif |
| 686 | if ((void *)MEM_NO_MEMORY_PTR == memblock) { |
| 687 | return 0; |
| 688 | } |
| 689 | return HeapSize(Heap, 0, memblock); |
| 690 | #endif |
| 691 | } |
| 692 | |
| 693 | void mem_shutdown(); |
| 694 | MemClass::~MemClass() { mem_shutdown(); } |
| 695 | // memory routines |
| 696 | void mem_shutdown() { |
| 697 | // free failsafe memory block. |
| 698 | mprintf(0, "Shutting down memory system.\n"); |
| 699 | if (Mem_failsafe_block) { |
| 700 | mem_free(Mem_failsafe_block); |
| 701 | Mem_failsafe_block = NULL; |
| 702 | } |
| 703 | #ifdef MEM_DEBUG |
| 704 | free(hashTable); |
| 705 | if (Total_mem_used) |
| 706 | mprintf(0, "%d bytes leaked in mem_malloc heap!\n", Total_mem_used); |
| 707 | #ifdef MEM_LOGFILE |
| 708 | fclose(mem_out); |
| 709 | #endif |
| 710 | mprintf(0, "Looking for memory leaks.\n"); |
| 711 | for (int i = 0; i < MEM_MAX_MALLOCS; i++) { |
| 712 | if (mem_info[i].ptr != (void *)MEM_NO_MEMORY_PTR) { |
| 713 | mprintf(0, "Memory leaked from %s line %d length %d.\n", mem_info[i].file, mem_info[i].line, mem_info[i].len); |
| 714 | /* |
| 715 | //mprintf(0,"%d\n",strdup_malloc_line); |
| 716 | if( (strcmp(mem_info[i].file,"main\\mem\\mem.cpp")==0) && (mem_info[i].line==(strdup_malloc_line+1)) ) |
| 717 | { |
| 718 | |
| 719 | mprintf(0,"Memory leak from mem_strdup() (%s)\n",mem_info[i].ptr); |
| 720 | } |
| 721 | */ |
| 722 | } |
| 723 | } |
| 724 | mprintf(0, "Done looking for memory leaks.\n"); |
| 725 | mprintf(0, "Memory library high water mark: %d\n", Mem_high_water_mark); |
| 726 | Mem_next_slot = 0; |
| 727 | #endif |
| 728 | if (Heap) { |
| 729 | HeapDestroy(Heap); |
| 730 | Heap = NULL; |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | #ifdef MEM_DEBUG |
| 735 | // accessory stuff for mem_dumpallocstofile() |
| 736 | struct allocdumpt { |
| 737 | mem_alloc_info *data; |
| 738 | }; |
| 739 | |
| 740 | static int *sorted_allocs = NULL; |
| 741 | |
| 742 | int mem_allocdump_cmp(allocdumpt *elem1, allocdumpt *elem2) { |
| 743 | int res; |
| 744 | res = strcmp(elem1->data->file, elem2->data->file); |
| 745 | if (res != 0) |
| 746 | return (res < 0) ? -1 : 1; |
| 747 | |
| 748 | if (elem1->data->line < elem2->data->line) |
| 749 | return -1; |
| 750 | else if (elem1->data->line > elem2->data->line) |
| 751 | return 1; |
| 752 | |
| 753 | if (elem1->data->len < elem2->data->len) |
| 754 | return -1; |
| 755 | else if (elem1->data->len > elem2->data->len) |
| 756 | return 1; |
| 757 | |
| 758 | return 0; |
| 759 | } |
| 760 | |
| 761 | #endif |
| 762 | |
| 763 | bool mem_dumpmallocstofile(char *filename) { |
| 764 | #ifdef MEM_DEBUG |
| 765 | FILE *file; |
| 766 | file = fopen(filename, "wt"); |
| 767 | if (!file) |
| 768 | return false; |
| 769 | |
| 770 | int h_table_idx, num_allocs = 0; |
| 771 | mem_alloc_info *alloc_info; |
| 772 | char buffer[384]; |
| 773 | |
| 774 | for (h_table_idx = 0; h_table_idx < MEM_MAX_MALLOCS; h_table_idx++) { |
| 775 | if (mem_info[h_table_idx].ptr != (void *)MEM_NO_MEMORY_PTR) { |
| 776 | num_allocs++; |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | allocdumpt *allocs; |
| 781 | int curr_idx, i; |
| 782 | allocs = (allocdumpt *)malloc(sizeof(allocdumpt) * num_allocs); |
| 783 | sorted_allocs = (int *)malloc(sizeof(int) * num_allocs); |
| 784 | curr_idx = 0; |
| 785 | |
| 786 | for (h_table_idx = 0; h_table_idx < MEM_MAX_MALLOCS; h_table_idx++) { |
| 787 | if (mem_info[h_table_idx].ptr != (void *)MEM_NO_MEMORY_PTR) { |
| 788 | allocs[curr_idx++].data = &mem_info[h_table_idx]; |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | // sort the list of allocs for easier reading |
| 793 | for (i = 0; i < num_allocs; i++) |
| 794 | sorted_allocs[i] = i; |
| 795 | qsort(allocs, num_allocs, sizeof(allocdumpt), (int (*)(const void *, const void *))mem_allocdump_cmp); |
| 796 | |
| 797 | // dump it out |
| 798 | int last_unique; |
| 799 | last_unique = -1; |
| 800 | |
| 801 | for (i = 0; i < num_allocs; i++) { |
| 802 | curr_idx = sorted_allocs[i]; |
| 803 | alloc_info = (mem_alloc_info *)allocs[curr_idx].data; |
| 804 | |
| 805 | if (last_unique != -1) { |
| 806 | // see if we are just repeating |
| 807 | mem_alloc_info *last_alloc; |
| 808 | last_alloc = (mem_alloc_info *)allocs[sorted_allocs[last_unique]].data; |
| 809 | if ((!strcmp(last_alloc->file, alloc_info->file)) && (last_alloc->line == alloc_info->line)) { |
| 810 | // its the same! |
| 811 | continue; |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | last_unique = i; |
| 816 | |
| 817 | if (i < num_allocs - 1) { |
| 818 | // see if we have multiple of the same line |
| 819 | mem_alloc_info *next_alloc; |
| 820 | next_alloc = (mem_alloc_info *)allocs[sorted_allocs[i + 1]].data; |
| 821 | if ((!strcmp(next_alloc->file, alloc_info->file)) && (next_alloc->line == alloc_info->line)) { |
| 822 | // we have multiple of the same line! |
| 823 | // see how many we have of this line |
| 824 | int repeat_count = 1; |
| 825 | int total_size = alloc_info->len; |
| 826 | i++; |
| 827 | while (i < num_allocs) { |
| 828 | next_alloc = (mem_alloc_info *)allocs[sorted_allocs[i]].data; |
| 829 | if ((!strcmp(next_alloc->file, alloc_info->file)) && (next_alloc->line == alloc_info->line)) { |
| 830 | repeat_count++; |
| 831 | total_size += next_alloc->len; |
| 832 | } else { |
| 833 | break; |
| 834 | } |
| 835 | i++; |
| 836 | } |
| 837 | |
| 838 | sprintf(buffer, "\n--Repeated %d Times--\n", repeat_count); |
| 839 | fputs(buffer, file); |
| 840 | |
| 841 | sprintf(buffer, "*[%d bytes total]\tFile %s Line %d\n", total_size, alloc_info->file, alloc_info->line); |
| 842 | fputs(buffer, file); |
| 843 | |
| 844 | continue; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | sprintf(buffer, "[%d bytes]\tFile %s Line %d\n", alloc_info->len, alloc_info->file, alloc_info->line); |
| 849 | fputs(buffer, file); |
| 850 | } |
| 851 | |
| 852 | free(sorted_allocs); |
| 853 | free(allocs); |
| 854 | |
| 855 | fclose(file); |
| 856 | return true; |
| 857 | #else |
| 858 | return false; |
| 859 | #endif |
| 860 | } |
| 861 | |
| 862 | void mem_heapcheck(void) { |
| 863 | #ifdef MEM_DEBUG |
| 864 | for (int i = 0; i < MEM_MAX_MALLOCS; i++) { |
| 865 | mem_alloc_info *freemem; |
| 866 | if (mem_info[i].ptr == (void *)MEM_NO_MEMORY_PTR) |
| 867 | continue; |
| 868 | freemem = &mem_info[i]; |
| 869 | uint16_t mem_sig = MEM_GAURDIAN_SIG; |
| 870 | if (memcmp((char *)freemem->ptr + freemem->len, &mem_sig, 2) != 0) { |
| 871 | mprintf(0, "Memory block found to be damaged in mem_heapcheck()!\n"); |
| 872 | mprintf(0, "Originally allocated from file %s, line %d\n", freemem->file, freemem->line); |
| 873 | Int3(); |
| 874 | } |
| 875 | } |
| 876 | #endif |
| 877 | } |
| 878 | hashTableIndex hash(T data) { |
| 879 | /*********************************** |
| 880 | * hash function applied to data * |
| 881 | ***********************************/ |
| 882 | uint32_t hval = (uint32_t)data->ptr; |
| 883 | return (hval % MEM_MAX_MALLOCS); |
| 884 | } |
| 885 | Node *insertNode(T data) { |
| 886 | Node *p, *p0; |
| 887 | hashTableIndex bucket; |
| 888 | /************************************************ |
| 889 | * allocate node for data and insert in table * |
| 890 | ************************************************/ |
| 891 | /* insert node at beginning of list */ |
| 892 | bucket = hash(data); |
| 893 | if ((p = (Node *)malloc(sizeof(Node))) == 0) { |
| 894 | fprintf(stderr, "out of memory (insertNode)\n"); |
| 895 | exit(1); |
| 896 | } |
| 897 | p0 = hashTable[bucket]; |
| 898 | hashTable[bucket] = p; |
| 899 | p->next = p0; |
| 900 | p->data = data; |
| 901 | return p; |
| 902 | } |
| 903 | void deleteNode(T data) { |
| 904 | Node *p0, *p; |
| 905 | hashTableIndex bucket; |
| 906 | /******************************************** |
| 907 | * delete node containing data from table * |
| 908 | ********************************************/ |
| 909 | /* find node */ |
| 910 | p0 = 0; |
| 911 | bucket = hash(data); |
| 912 | p = hashTable[bucket]; |
| 913 | while (p && !compEQ(p->data, data)) { |
| 914 | p0 = p; |
| 915 | p = p->next; |
| 916 | } |
| 917 | if (!p) |
| 918 | return; |
| 919 | /* p designates node to delete, remove it from list */ |
| 920 | if (p0) |
| 921 | /* not first node, p0 points to previous node */ |
| 922 | p0->next = p->next; |
| 923 | else |
| 924 | /* first node on chain */ |
| 925 | hashTable[bucket] = p->next; |
| 926 | free(p); |
| 927 | } |
| 928 | Node *findNode(T data) { |
| 929 | Node *p; |
| 930 | /******************************* |
| 931 | * find node containing data * |
| 932 | *******************************/ |
| 933 | p = hashTable[hash(data)]; |
| 934 | while (p && !compEQ(p->data, data)) |
| 935 | p = p->next; |
| 936 | return p; |
| 937 | } |
| 938 | #endif |