[Git][ghc/ghc][wip/stgMalloc] rts: Consistently use stgMallocBytes instead of malloc

Ben Gamari gitlab at gitlab.haskell.org
Tue Aug 25 01:28:11 UTC 2020



Ben Gamari pushed to branch wip/stgMalloc at Glasgow Haskell Compiler / GHC


Commits:
1d1d21b2 by GHC GitLab CI at 2020-08-24T21:28:04-04:00
rts: Consistently use stgMallocBytes instead of malloc

This can help in debugging RTS memory leaks since all allocations go
through the same interface.

- - - - -


5 changed files:

- rts/linker/PEi386.c
- rts/win32/IOManager.c
- rts/win32/OSThreads.c
- rts/win32/WorkQueue.c
- rts/xxhash.c


Changes:

=====================================
rts/linker/PEi386.c
=====================================
@@ -735,7 +735,7 @@ addDLL_PEi386( pathchar *dll_name, HINSTANCE *loaded )
 error:
     stgFree(buf);
 
-    char* errormsg = malloc(sizeof(char) * 80);
+    char* errormsg = stgMallocBytes(sizeof(char) * 80, "addDLL_PEi386");
     snprintf(errormsg, 80, "addDLL: %" PATH_FMT " or dependencies not loaded. (Win32 error %lu)", dll_name, GetLastError());
     /* LoadLibrary failed; return a ptr to the error msg. */
     return errormsg;
@@ -745,7 +745,7 @@ pathchar* findSystemLibrary_PEi386( pathchar* dll_name )
 {
     const unsigned int init_buf_size = 1024;
     unsigned int bufsize             = init_buf_size;
-    wchar_t* result = malloc(sizeof(wchar_t) * bufsize);
+    wchar_t* result = stgMallocBytes(sizeof(wchar_t) * bufsize, "findSystemLibrary_PEi386");
     DWORD wResult   = SearchPathW(NULL, dll_name, NULL, bufsize, result, NULL);
 
     if (wResult > bufsize) {
@@ -755,7 +755,7 @@ pathchar* findSystemLibrary_PEi386( pathchar* dll_name )
 
 
     if (!wResult) {
-        free(result);
+        stgFree(result);
         return NULL;
     }
 
@@ -773,7 +773,7 @@ HsPtr addLibrarySearchPath_PEi386(pathchar* dll_path)
     int bufsize                      = init_buf_size;
 
     // Make sure the path is an absolute path
-    WCHAR* abs_path = malloc(sizeof(WCHAR) * init_buf_size);
+    WCHAR* abs_path = stgMallocBytes(sizeof(WCHAR) * init_buf_size, "addLibrarySearchPath_PEi386(1)");
     DWORD wResult = GetFullPathNameW(dll_path, bufsize, abs_path, NULL);
     if (!wResult){
         IF_DEBUG(linker, debugBelch("addLibrarySearchPath[GetFullPathNameW]: %" PATH_FMT " (Win32 error %lu)", dll_path, GetLastError()));
@@ -791,7 +791,7 @@ HsPtr addLibrarySearchPath_PEi386(pathchar* dll_path)
     else
     {
         warnMissingKBLibraryPaths();
-        WCHAR* str = malloc(sizeof(WCHAR) * init_buf_size);
+        WCHAR* str = stgMallocBytes(sizeof(WCHAR) * init_buf_size, "addLibrarySearchPath_PEi386(2)");
         wResult = GetEnvironmentVariableW(L"PATH", str, bufsize);
 
         if (wResult > init_buf_size) {
@@ -804,7 +804,7 @@ HsPtr addLibrarySearchPath_PEi386(pathchar* dll_path)
         }
 
         bufsize = wResult + 2 + pathlen(abs_path);
-        wchar_t* newPath = malloc(sizeof(wchar_t) * bufsize);
+        wchar_t* newPath = stgMallocBytes(sizeof(wchar_t) * bufsize, "addLibrarySearchPath_PEi386(3)");
 
         wcscpy(newPath, abs_path);
         wcscat(newPath, L";");
@@ -813,19 +813,19 @@ HsPtr addLibrarySearchPath_PEi386(pathchar* dll_path)
             sysErrorBelch("addLibrarySearchPath[SetEnvironmentVariableW]: %" PATH_FMT " (Win32 error %lu)", abs_path, GetLastError());
         }
 
-        free(newPath);
-        free(abs_path);
+        stgFree(newPath);
+        stgFree(abs_path);
 
         return str;
     }
 
     if (!result) {
         sysErrorBelch("addLibrarySearchPath: %" PATH_FMT " (Win32 error %lu)", abs_path, GetLastError());
-        free(abs_path);
+        stgFree(abs_path);
         return NULL;
     }
 
-    free(abs_path);
+    stgFree(abs_path);
     return result;
 }
 


=====================================
rts/win32/IOManager.c
=====================================
@@ -265,7 +265,7 @@ IOWorkerProc(PVOID param)
                 }
                 // Free the WorkItem
                 DeregisterWorkItem(iom,work);
-                free(work);
+                stgFree(work);
             } else {
                 fprintf(stderr, "unable to fetch work; fatal.\n");
                 fflush(stderr);
@@ -321,7 +321,7 @@ StartIOManager(void)
     wq = NewWorkQueue();
     if ( !wq ) return false;
 
-    ioMan = (IOManagerState*)malloc(sizeof(IOManagerState));
+    ioMan = (IOManagerState*)stgMallocBytes(sizeof(IOManagerState), "StartIOManager");
 
     if (!ioMan) {
         FreeWorkQueue(wq);
@@ -332,7 +332,7 @@ StartIOManager(void)
     hExit = CreateEvent ( NULL, true, false, NULL );
     if ( !hExit ) {
         FreeWorkQueue(wq);
-        free(ioMan);
+        stgFree(ioMan);
         return false;
     }
 
@@ -440,8 +440,7 @@ AddIORequest ( int   fd,
 {
     ASSERT(ioMan);
 
-    WorkItem* wItem    = (WorkItem*)malloc(sizeof(WorkItem));
-    if (!wItem) return 0;
+    WorkItem* wItem    = (WorkItem*)stgMallocBytes(sizeof(WorkItem), "AddIORequest");
 
     unsigned int reqID = ioMan->requestID++;
 
@@ -471,8 +470,7 @@ AddDelayRequest ( HsInt          usecs,
 {
     ASSERT(ioMan);
 
-    WorkItem* wItem = (WorkItem*)malloc(sizeof(WorkItem));
-    if (!wItem) return false;
+    WorkItem* wItem = (WorkItem*)stgMallocBytes(sizeof(WorkItem), "AddDelayRequest");
 
     unsigned int reqID = ioMan->requestID++;
 
@@ -498,7 +496,7 @@ AddProcRequest ( void* proc,
 {
     ASSERT(ioMan);
 
-    WorkItem* wItem = (WorkItem*)malloc(sizeof(WorkItem));
+    WorkItem* wItem = (WorkItem*)stgMallocBytes(sizeof(WorkItem), "AddProcRequest");
     if (!wItem) return false;
 
     unsigned int reqID = ioMan->requestID++;
@@ -542,7 +540,7 @@ void ShutdownIOManager ( bool wait_threads )
             barf("timeEndPeriod failed");
         }
 
-        free(ioMan);
+        stgFree(ioMan);
         ioMan = NULL;
     }
 }


=====================================
rts/win32/OSThreads.c
=====================================
@@ -171,19 +171,19 @@ void freeThreadingResources (void)
 {
     if (cpuGroupCache)
     {
-        free(cpuGroupCache);
+        stgFree(cpuGroupCache);
         cpuGroupCache = NULL;
     }
 
     if (cpuGroupCumulativeCache)
     {
-        free(cpuGroupCumulativeCache);
+        stgFree(cpuGroupCumulativeCache);
         cpuGroupCumulativeCache = NULL;
     }
 
     if (cpuGroupDistCache)
     {
-        free(cpuGroupDistCache);
+        stgFree(cpuGroupDistCache);
         cpuGroupDistCache = NULL;
     }
 }
@@ -240,7 +240,7 @@ getProcessorsDistribution (void)
     if (!cpuGroupDistCache)
     {
         uint8_t n_groups = getNumberOfProcessorsGroups();
-        cpuGroupDistCache = malloc(n_groups * sizeof(uint8_t));
+        cpuGroupDistCache = stgMallocBytes(n_groups * sizeof(uint8_t), "getProcessorsDistribution");
         memset(cpuGroupDistCache, MAXIMUM_PROCESSORS, n_groups * sizeof(uint8_t));
 
         for (int i = 0; i < n_groups; i++)
@@ -265,7 +265,7 @@ getProcessorsCumulativeSum(void)
     if (!cpuGroupCumulativeCache)
     {
         uint8_t n_groups = getNumberOfProcessorsGroups();
-        cpuGroupCumulativeCache = malloc(n_groups * sizeof(uint32_t));
+        cpuGroupCumulativeCache = stgMallocBytes(n_groups * sizeof(uint32_t), "getProcessorsCumulativeSum");
         memset(cpuGroupCumulativeCache, 0, n_groups * sizeof(uint32_t));
 
 #if defined(x86_64_HOST_ARCH)
@@ -306,7 +306,7 @@ createProcessorGroupMap (void)
 
     uint32_t numProcs = getNumberOfProcessors();
 
-    cpuGroupCache = malloc(numProcs * sizeof(uint8_t));
+    cpuGroupCache = stgMallocBytes(numProcs * sizeof(uint8_t), "createProcessorGroupMap");
     /* For 32bit Windows and 64bit older than Windows 7, create a default mapping. */
     memset(cpuGroupCache, 0, numProcs * sizeof(uint8_t));
 
@@ -386,7 +386,7 @@ setThreadAffinity (uint32_t n, uint32_t m) // cap N of M
     ASSERT(n_groups      > 0);
     ASSERT(n_proc        > 0);
 
-    mask = malloc(n_groups * sizeof(DWORD_PTR));
+    mask = stgMallocBytes(n_groups * sizeof(DWORD_PTR), "setThreadAffinity");
     memset(mask, 0, n_groups * sizeof(DWORD_PTR));
 
     /* The mask for the individual groups are all 0 based
@@ -422,14 +422,14 @@ setThreadAffinity (uint32_t n, uint32_t m) // cap N of M
         {
             r = SetThreadAffinityMask(hThread, mask[i]);
             if (r == 0) {
-                free(mask);
+                stgFree(mask);
                 sysErrorBelch("SetThreadAffinity");
                 stg_exit(EXIT_FAILURE);
             }
         }
     }
 
-    free(mask);
+    stgFree(mask);
 }
 
 void


=====================================
rts/win32/WorkQueue.c
=====================================
@@ -41,12 +41,7 @@ newSemaphore(int initCount, int max)
 WorkQueue*
 NewWorkQueue()
 {
-  WorkQueue* wq = (WorkQueue*)malloc(sizeof(WorkQueue));
-
-  if (!wq) {
-    queue_error("NewWorkQueue", "malloc() failed");
-    return wq;
-  }
+  WorkQueue* wq = (WorkQueue*)stgMallocBytes(sizeof(WorkQueue), "NewWorkQueue");
 
   memset(wq, 0, sizeof *wq);
 


=====================================
rts/xxhash.c
=====================================
@@ -98,9 +98,9 @@
 ***************************************/
 /*! Modify the local functions below should you wish to use some other memory routines
 *   for malloc(), free() */
-#include <stdlib.h>
-static void* XXH_malloc(size_t s) { return malloc(s); }
-static void  XXH_free  (void* p)  { free(p); }
+#include "Rts.h"
+static void* XXH_malloc(size_t s) { return stgMallocBytes(s, "XXH_malloc"); }
+static void  XXH_free  (void* p)  { stgFree(p); }
 /*! and for memcpy() */
 #include <string.h>
 static void* XXH_memcpy(void* dest, const void* src, size_t size) { return memcpy(dest,src,size); }



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/1d1d21b2f05c342e5ad0e2acfaf12219a278513b

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/1d1d21b2f05c342e5ad0e2acfaf12219a278513b
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20200824/b0186739/attachment-0001.html>


More information about the ghc-commits mailing list