[Git][ghc/ghc][wip/T22965-9.2] hi

Ben Gamari (@bgamari) gitlab at gitlab.haskell.org
Fri Feb 17 23:43:44 UTC 2023



Ben Gamari pushed to branch wip/T22965-9.2 at Glasgow Haskell Compiler / GHC


Commits:
5a9a7b80 by Ben Gamari at 2023-02-17T18:42:46-05:00
hi

- - - - -


3 changed files:

- rts/Capability.c
- rts/RtsUtils.c
- rts/RtsUtils.h


Changes:

=====================================
rts/Capability.c
=====================================
@@ -1288,7 +1288,7 @@ freeCapabilities (void)
     for (i=0; i < getNumCapabilities(); i++) {
         freeCapability(capabilities[i]);
         if (capabilities[i] != &MainCapability)
-            stgFree(capabilities[i]);
+            stgFreeAligned(capabilities[i]);
     }
 #else
     freeCapability(&MainCapability);


=====================================
rts/RtsUtils.c
=====================================
@@ -83,41 +83,6 @@ stgMallocBytes (size_t n, char *msg)
     return space;
 }
 
-void *
-stgMallocAlignedBytes (size_t n, size_t align, char *msg)
-{
-    void *space;
-
-#if defined(mingw32_HOST_OS)
-    space = _aligned_malloc(n, align);
-#else
-    if (posix_memalign(&space, align, n)) {
-        space = NULL; // Allocation failed
-    }
-#endif
-
-    if (space == NULL) {
-      /* Quoting POSIX.1-2008 (which says more or less the same as ISO C99):
-       *
-       *   "Upon successful completion with size not equal to 0, malloc() shall
-       *   return a pointer to the allocated space. If size is 0, either a null
-       *   pointer or a unique pointer that can be successfully passed to free()
-       *   shall be returned. Otherwise, it shall return a null pointer and set
-       *   errno to indicate the error."
-       *
-       * Consequently, a NULL pointer being returned by `malloc()` for a 0-size
-       * allocation is *not* to be considered an error.
-       */
-      if (n == 0) return NULL;
-
-      /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
-      rtsConfig.mallocFailHook((W_) n, msg);
-      stg_exit(EXIT_INTERNAL_ERROR);
-    }
-    IF_DEBUG(zero_on_gc, memset(space, 0xbb, n));
-    return space;
-}
-
 void *
 stgReallocBytes (void *p, size_t n, char *msg)
 {
@@ -165,6 +130,53 @@ stgFree(void* p)
   free(p);
 }
 
+// N.B. Allocations resulting from this function must be freed by
+// `stgFreeAligned`, not `stgFree`. This is necessary due to the properties of Windows' `_aligned_malloc`
+void *
+stgMallocAlignedBytes (size_t n, size_t align, char *msg)
+{
+    void *space;
+
+#if defined(mingw32_HOST_OS)
+    space = _aligned_malloc(n, align);
+#else
+    if (posix_memalign(&space, align, n)) {
+        space = NULL; // Allocation failed
+    }
+#endif
+
+    if (space == NULL) {
+      /* Quoting POSIX.1-2008 (which says more or less the same as ISO C99):
+       *
+       *   "Upon successful completion with size not equal to 0, malloc() shall
+       *   return a pointer to the allocated space. If size is 0, either a null
+       *   pointer or a unique pointer that can be successfully passed to free()
+       *   shall be returned. Otherwise, it shall return a null pointer and set
+       *   errno to indicate the error."
+       *
+       * Consequently, a NULL pointer being returned by `malloc()` for a 0-size
+       * allocation is *not* to be considered an error.
+       */
+      if (n == 0) return NULL;
+
+      /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
+      rtsConfig.mallocFailHook((W_) n, msg);
+      stg_exit(EXIT_INTERNAL_ERROR);
+    }
+    IF_DEBUG(zero_on_gc, memset(space, 0xbb, n));
+    return space;
+}
+
+void
+stgFreeAligned (void *p)
+{
+#if defined(mingw32_HOST_OS)
+    _aligned_free(p);
+#else
+    free(p);
+#endif
+}
+
 /* -----------------------------------------------------------------------------
    Stack/heap overflow
    -------------------------------------------------------------------------- */


=====================================
rts/RtsUtils.h
=====================================
@@ -20,8 +20,6 @@ void shutdownAllocator(void);
 void *stgMallocBytes(size_t n, char *msg)
     GNUC3_ATTRIBUTE(__malloc__);
 
-void *stgMallocAlignedBytes(size_t n, size_t align, char *msg);
-
 void *stgReallocBytes(void *p, size_t n, char *msg);
 
 void *stgCallocBytes(size_t count, size_t size, char *msg)
@@ -31,6 +29,10 @@ char *stgStrndup(const char *s, size_t n);
 
 void stgFree(void* p);
 
+void *stgMallocAlignedBytes(size_t n, size_t align, char *msg);
+
+void stgFreeAligned(void *p);
+
 /* -----------------------------------------------------------------------------
  * Misc other utilities
  * -------------------------------------------------------------------------- */



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/5a9a7b8093db8913376aa7c9920f1ac3685c3726

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/5a9a7b8093db8913376aa7c9920f1ac3685c3726
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/20230217/535c9255/attachment-0001.html>


More information about the ghc-commits mailing list