From a70d56d77c0a09d6a6a1dc18ce39e928527a637a Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sat, 2 May 2015 12:27:53 -0400 Subject: [PATCH] Switched to the Windows API heap functions in windows/alloc.c. --- windows/alloc.c | 17 ++++++++++++----- windows/init.c | 3 +++ windows/uipriv_windows.h | 3 +++ 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/windows/alloc.c b/windows/alloc.c index 51d535d6..87e79a88 100644 --- a/windows/alloc.c +++ b/windows/alloc.c @@ -7,16 +7,23 @@ // passing NULL to tableRealloc() acts like tableAlloc() // passing NULL to tableFree() is a no-op +static HANDLE heap; + +BOOL initAlloc(void) +{ + heap = HeapCreate(0, 0, 0); + return heap != NULL; +} + void *uiAlloc(size_t size, const char *type) { void *out; - out = malloc(size); + out = HeapAlloc(heap, HEAP_ZERO_MEMORY, size); if (out == NULL) { fprintf(stderr, "memory exhausted in uiAlloc() allocating %s\n", type); abort(); } - ZeroMemory(out, size); return out; } @@ -26,12 +33,11 @@ void *uiRealloc(void *p, size_t size, const char *type) if (p == NULL) return uiAlloc(size, type); - out = realloc(p, size); + out = HeapReAlloc(heap, HEAP_ZERO_MEMORY, p, size); if (out == NULL) { fprintf(stderr, "memory exhausted in uiRealloc() reallocating %s\n", type); abort(); } - // TODO zero the extra memory return out; } @@ -39,5 +45,6 @@ void uiFree(void *p) { if (p == NULL) return; - free(p); + if (HeapFree(heap, 0, p) == 0) + logLastError("error freeing memory in uiFree()"); } diff --git a/windows/init.c b/windows/init.c index d8fc49bd..d562d3b6 100644 --- a/windows/init.c +++ b/windows/init.c @@ -64,6 +64,9 @@ const char *uiInit(uiInitOptions *o) options = *o; + if (initAlloc() == 0) + return loadLastError("error initializing memory allocations"); + nCmdShow = SW_SHOWDEFAULT; GetStartupInfoW(&si); if ((si.dwFlags & STARTF_USESHOWWINDOW) != 0) diff --git a/windows/uipriv_windows.h b/windows/uipriv_windows.h index 3429379e..499fdcb1 100644 --- a/windows/uipriv_windows.h +++ b/windows/uipriv_windows.h @@ -75,3 +75,6 @@ extern HMENU makeMenubar(void); extern const uiMenuItem *menuIDToItem(UINT_PTR); extern void runMenuEvent(WORD, uiWindow *); extern void freeMenubar(HMENU); + +// alloc.c +extern BOOL initAlloc(void);