diff options
Diffstat (limited to 'src/osd/windows/winalloc.c')
-rw-r--r-- | src/osd/windows/winalloc.c | 164 |
1 files changed, 95 insertions, 69 deletions
diff --git a/src/osd/windows/winalloc.c b/src/osd/windows/winalloc.c index a355ebca3c8..c54eda5e768 100644 --- a/src/osd/windows/winalloc.c +++ b/src/osd/windows/winalloc.c @@ -30,9 +30,6 @@ #define PAGE_SIZE 4096 #define COOKIE_VAL 0x11335577 -// set this to 0 to not use guard pages -#define USE_GUARD_PAGES 1 - // set this to 1 to align memory blocks to the start of a page; // otherwise, they are aligned to the end, thus catching array // overruns @@ -188,6 +185,20 @@ INLINE void free_entry(memory_entry *entry) } +INLINE int use_malloc_tracking(void) +{ +#ifdef MESS + extern BOOL win_is_gui_application(void); + return !win_is_gui_application(); +#elif defined(WINUI) + return FALSE; +#else + return TRUE; +#endif + +} + + //============================================================ // IMPLEMENTATION @@ -195,12 +206,15 @@ INLINE void free_entry(memory_entry *entry) void *malloc_file_line(size_t size, const char *file, int line) { - UINT8 *page_base, *block_base; - size_t rounded_size; - memory_entry *entry; + UINT8 *block_base; + int id = current_id++; - if (USE_GUARD_PAGES) + if (use_malloc_tracking()) { + UINT8 *page_base; + size_t rounded_size; + memory_entry *entry; + // round the size up to a page boundary rounded_size = ((size + PAGE_SIZE - 1) / PAGE_SIZE) * PAGE_SIZE; @@ -219,29 +233,27 @@ void *malloc_file_line(size_t size, const char *file, int line) block_base = page_base; else block_base = page_base + rounded_size - size; + + // fill in the entry + entry = allocate_entry(); + entry->size = size; + entry->base = block_base; + entry->file = file; + entry->line = line; + entry->id = id; } else { block_base = (UINT8 *)GlobalAlloc(GMEM_FIXED, size); } - // fill in the entry - entry = allocate_entry(); - entry->size = size; - entry->base = block_base; - entry->file = file; - entry->line = line; - entry->id = current_id++; - -//if (entry->size == 72 && IsDebuggerPresent()) DebugBreak(); - #if LOG_CALLS // logging - if (entry->file) - logerror("malloc #%06d size = %d (%s:%d)\n", entry->size, entry->file, entry->line); + if (file != NULL) + logerror("malloc #%06d size = %d (%s:%d)\n", id, size, file, line); else - logerror("malloc #%06d size = %d\n", entry->id, entry->size); -#endif + logerror("malloc #%06d size = %d\n", id, size); +#endif // LOG_CALLS return block_base; } @@ -283,34 +295,41 @@ void *realloc_file_line(void *memory, size_t size, const char *file, int line) { void *newmemory = NULL; - // if size is non-zero, we need to reallocate memory - if (size != 0) + if (use_malloc_tracking()) { - // allocate space for the new amount - newmemory = malloc_file_line(size, file, line); - if (newmemory == NULL) - return NULL; - - // if we have an old pointer, copy it - if (memory != NULL) + // if size is non-zero, we need to reallocate memory + if (size != 0) { - memory_entry *entry = find_entry(memory); - if (entry == NULL) + // allocate space for the new amount + newmemory = malloc_file_line(size, file, line); + if (newmemory == NULL) + return NULL; + + // if we have an old pointer, copy it + if (memory != NULL) { - if (winalloc_in_main_code) + memory_entry *entry = find_entry(memory); + if (entry == NULL) { - fprintf(stderr, "Error: realloc a non-existant block (%s:%d)\n", file, line); - osd_break_into_debugger("Error: realloc a non-existant block\n"); + if (winalloc_in_main_code) + { + fprintf(stderr, "Error: realloc a non-existant block (%s:%d)\n", file, line); + osd_break_into_debugger("Error: realloc a non-existant block\n"); + } } + else + memcpy(newmemory, memory, (size < entry->size) ? size : entry->size); } - else - memcpy(newmemory, memory, (size < entry->size) ? size : entry->size); } - } - // if we have an original pointer, free it - if (memory != NULL) - free(memory); + // if we have an original pointer, free it + if (memory != NULL) + free(memory); + } + else + { + newmemory = (void *) GlobalReAlloc(memory, size, GMEM_MOVEABLE); + } return newmemory; } @@ -330,28 +349,32 @@ void CLIB_DECL free(void *memory) if (memory == NULL) return; - // error if no entry found - entry = find_entry(memory); - if (entry == NULL) + if (use_malloc_tracking()) { - if (winalloc_in_main_code) + // error if no entry found + entry = find_entry(memory); + if (entry == NULL) { - fprintf(stderr, "Error: free a non-existant block\n"); - osd_break_into_debugger("Error: free a non-existant block"); + if (winalloc_in_main_code) + { + fprintf(stderr, "Error: free a non-existant block\n"); + osd_break_into_debugger("Error: free a non-existant block"); + } + return; } - return; - } - free_entry(entry); + free_entry(entry); - // free the memory - if (USE_GUARD_PAGES) + // free the memory VirtualFree((UINT8 *)memory - ((size_t)memory & (PAGE_SIZE-1)) - PAGE_SIZE, 0, MEM_RELEASE); - else - GlobalFree(memory); #if LOG_CALLS - logerror("free #%06d size = %d\n", entry->id, entry->size); -#endif + logerror("free #%06d size = %d\n", entry->id, entry->size); +#endif // LOG_CALLS + } + else + { + GlobalFree(memory); + } } @@ -376,20 +399,23 @@ void check_unfreed_mem(void) memory_entry *entry; int total = 0; - memory_lock_acquire(); + if (use_malloc_tracking()) + { + memory_lock_acquire(); - // check for leaked memory - for (entry = alloc_list; entry; entry = entry->next) - if (entry->file != NULL) - { - if (total == 0) - fprintf(stderr, "--- memory leak warning ---\n"); - total += entry->size; - fprintf(stderr, "allocation #%06d, %d bytes (%s:%d)\n", entry->id, entry->size, entry->file, entry->line); - } + // check for leaked memory + for (entry = alloc_list; entry; entry = entry->next) + if (entry->file != NULL) + { + if (total == 0) + fprintf(stderr, "--- memory leak warning ---\n"); + total += entry->size; + fprintf(stderr, "allocation #%06d, %d bytes (%s:%d)\n", entry->id, entry->size, entry->file, entry->line); + } - memory_lock_release(); + memory_lock_release(); - if (total > 0) - fprintf(stderr, "a total of %d bytes were not free()'d\n", total); + if (total > 0) + fprintf(stderr, "a total of %d bytes were not free()'d\n", total); + } } |