public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Iain Buclaw <ibuclaw@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc(refs/users/ibuclaw/heads/darwin)] libphobos: Add more uses of getTargetInfo(osxVersionMin). Implement posix_memalign if missing
Date: Sun,  7 Mar 2021 17:01:45 +0000 (GMT)	[thread overview]
Message-ID: <20210307170145.0C66C393A43D@sourceware.org> (raw)

https://gcc.gnu.org/g:a161f87230064cc01cf8de535f58f87503eec405

commit a161f87230064cc01cf8de535f58f87503eec405
Author: Iain Buclaw <ibuclaw@gdcproject.org>
Date:   Sun Dec 6 00:19:39 2020 +0100

    libphobos: Add more uses of getTargetInfo(osxVersionMin).  Implement posix_memalign if missing

Diff:
---
 libphobos/libdruntime/Makefile.am                  |  25 ++--
 libphobos/libdruntime/Makefile.in                  |  35 ++---
 libphobos/libdruntime/core/stdc/math.d             |  20 +--
 libphobos/libdruntime/core/sys/darwin/config.d     |  61 +++++++++
 libphobos/libdruntime/core/sys/darwin/mach/dyld.d  |  18 ++-
 libphobos/libdruntime/core/sys/darwin/pthread.d    |  35 +++--
 libphobos/libdruntime/core/sys/darwin/string.d     |   8 +-
 libphobos/libdruntime/core/sys/posix/stdio.d       |   4 +-
 libphobos/libdruntime/core/sys/posix/stdlib.d      |   8 +-
 .../src/std/experimental/allocator/mallocator.d    | 141 ++++++++++++++++++---
 10 files changed, 278 insertions(+), 77 deletions(-)

diff --git a/libphobos/libdruntime/Makefile.am b/libphobos/libdruntime/Makefile.am
index c332d1787df..9d66f07c19a 100644
--- a/libphobos/libdruntime/Makefile.am
+++ b/libphobos/libdruntime/Makefile.am
@@ -206,18 +206,19 @@ DRUNTIME_DSOURCES_BIONIC = core/sys/bionic/err.d \
 	core/sys/bionic/fcntl.d core/sys/bionic/stdlib.d \
 	core/sys/bionic/string.d core/sys/bionic/unistd.d
 
-DRUNTIME_DSOURCES_DARWIN = core/sys/darwin/crt_externs.d \
-	core/sys/darwin/dlfcn.d core/sys/darwin/err.d \
-	core/sys/darwin/execinfo.d core/sys/darwin/ifaddrs.d \
-	core/sys/darwin/mach/dyld.d core/sys/darwin/mach/getsect.d \
-	core/sys/darwin/mach/kern_return.d core/sys/darwin/mach/loader.d \
-	core/sys/darwin/mach/nlist.d core/sys/darwin/mach/port.d \
-	core/sys/darwin/mach/semaphore.d core/sys/darwin/mach/stab.d \
-	core/sys/darwin/mach/thread_act.d core/sys/darwin/netinet/in_.d \
-	core/sys/darwin/pthread.d core/sys/darwin/stdlib.d \
-	core/sys/darwin/string.d core/sys/darwin/sys/attr.d \
-	core/sys/darwin/sys/cdefs.d core/sys/darwin/sys/event.d \
-	core/sys/darwin/sys/mman.d core/sys/darwin/sys/sysctl.d
+DRUNTIME_DSOURCES_DARWIN = core/sys/darwin/config.d \
+	core/sys/darwin/crt_externs.d core/sys/darwin/dlfcn.d \
+	core/sys/darwin/err.d core/sys/darwin/execinfo.d \
+	core/sys/darwin/ifaddrs.d core/sys/darwin/mach/dyld.d \
+	core/sys/darwin/mach/getsect.d core/sys/darwin/mach/kern_return.d \
+	core/sys/darwin/mach/loader.d core/sys/darwin/mach/nlist.d \
+	core/sys/darwin/mach/port.d core/sys/darwin/mach/semaphore.d \
+	core/sys/darwin/mach/stab.d core/sys/darwin/mach/thread_act.d \
+	core/sys/darwin/netinet/in_.d core/sys/darwin/pthread.d \
+	core/sys/darwin/stdlib.d core/sys/darwin/string.d \
+	core/sys/darwin/sys/attr.d core/sys/darwin/sys/cdefs.d \
+	core/sys/darwin/sys/event.d core/sys/darwin/sys/mman.d \
+	core/sys/darwin/sys/sysctl.d
 
 DRUNTIME_DSOURCES_DRAGONFLYBSD = core/sys/dragonflybsd/dlfcn.d \
 	core/sys/dragonflybsd/err.d core/sys/dragonflybsd/execinfo.d \
diff --git a/libphobos/libdruntime/Makefile.in b/libphobos/libdruntime/Makefile.in
index 23c161f96d0..1bb20559e20 100644
--- a/libphobos/libdruntime/Makefile.in
+++ b/libphobos/libdruntime/Makefile.in
@@ -255,10 +255,11 @@ am__objects_3 = core/sys/posix/aio.lo core/sys/posix/arpa/inet.lo \
 	core/sys/posix/time.lo core/sys/posix/ucontext.lo \
 	core/sys/posix/unistd.lo core/sys/posix/utime.lo
 @DRUNTIME_OS_POSIX_TRUE@am__objects_4 = $(am__objects_3)
-am__objects_5 = core/sys/darwin/crt_externs.lo \
-	core/sys/darwin/dlfcn.lo core/sys/darwin/err.lo \
-	core/sys/darwin/execinfo.lo core/sys/darwin/ifaddrs.lo \
-	core/sys/darwin/mach/dyld.lo core/sys/darwin/mach/getsect.lo \
+am__objects_5 = core/sys/darwin/config.lo \
+	core/sys/darwin/crt_externs.lo core/sys/darwin/dlfcn.lo \
+	core/sys/darwin/err.lo core/sys/darwin/execinfo.lo \
+	core/sys/darwin/ifaddrs.lo core/sys/darwin/mach/dyld.lo \
+	core/sys/darwin/mach/getsect.lo \
 	core/sys/darwin/mach/kern_return.lo \
 	core/sys/darwin/mach/loader.lo core/sys/darwin/mach/nlist.lo \
 	core/sys/darwin/mach/port.lo core/sys/darwin/mach/semaphore.lo \
@@ -833,18 +834,19 @@ DRUNTIME_DSOURCES_BIONIC = core/sys/bionic/err.d \
 	core/sys/bionic/fcntl.d core/sys/bionic/stdlib.d \
 	core/sys/bionic/string.d core/sys/bionic/unistd.d
 
-DRUNTIME_DSOURCES_DARWIN = core/sys/darwin/crt_externs.d \
-	core/sys/darwin/dlfcn.d core/sys/darwin/err.d \
-	core/sys/darwin/execinfo.d core/sys/darwin/ifaddrs.d \
-	core/sys/darwin/mach/dyld.d core/sys/darwin/mach/getsect.d \
-	core/sys/darwin/mach/kern_return.d core/sys/darwin/mach/loader.d \
-	core/sys/darwin/mach/nlist.d core/sys/darwin/mach/port.d \
-	core/sys/darwin/mach/semaphore.d core/sys/darwin/mach/stab.d \
-	core/sys/darwin/mach/thread_act.d core/sys/darwin/netinet/in_.d \
-	core/sys/darwin/pthread.d core/sys/darwin/stdlib.d \
-	core/sys/darwin/string.d core/sys/darwin/sys/attr.d \
-	core/sys/darwin/sys/cdefs.d core/sys/darwin/sys/event.d \
-	core/sys/darwin/sys/mman.d core/sys/darwin/sys/sysctl.d
+DRUNTIME_DSOURCES_DARWIN = core/sys/darwin/config.d \
+	core/sys/darwin/crt_externs.d core/sys/darwin/dlfcn.d \
+	core/sys/darwin/err.d core/sys/darwin/execinfo.d \
+	core/sys/darwin/ifaddrs.d core/sys/darwin/mach/dyld.d \
+	core/sys/darwin/mach/getsect.d core/sys/darwin/mach/kern_return.d \
+	core/sys/darwin/mach/loader.d core/sys/darwin/mach/nlist.d \
+	core/sys/darwin/mach/port.d core/sys/darwin/mach/semaphore.d \
+	core/sys/darwin/mach/stab.d core/sys/darwin/mach/thread_act.d \
+	core/sys/darwin/netinet/in_.d core/sys/darwin/pthread.d \
+	core/sys/darwin/stdlib.d core/sys/darwin/string.d \
+	core/sys/darwin/sys/attr.d core/sys/darwin/sys/cdefs.d \
+	core/sys/darwin/sys/event.d core/sys/darwin/sys/mman.d \
+	core/sys/darwin/sys/sysctl.d
 
 DRUNTIME_DSOURCES_DRAGONFLYBSD = core/sys/dragonflybsd/dlfcn.d \
 	core/sys/dragonflybsd/err.d core/sys/dragonflybsd/execinfo.d \
@@ -1341,6 +1343,7 @@ core/sys/posix/utime.lo: core/sys/posix/$(am__dirstamp)
 core/sys/darwin/$(am__dirstamp):
 	@$(MKDIR_P) core/sys/darwin
 	@: > core/sys/darwin/$(am__dirstamp)
+core/sys/darwin/config.lo: core/sys/darwin/$(am__dirstamp)
 core/sys/darwin/crt_externs.lo: core/sys/darwin/$(am__dirstamp)
 core/sys/darwin/dlfcn.lo: core/sys/darwin/$(am__dirstamp)
 core/sys/darwin/err.lo: core/sys/darwin/$(am__dirstamp)
diff --git a/libphobos/libdruntime/core/stdc/math.d b/libphobos/libdruntime/core/stdc/math.d
index e8d1fa8e7b5..19bedd47006 100644
--- a/libphobos/libdruntime/core/stdc/math.d
+++ b/libphobos/libdruntime/core/stdc/math.d
@@ -989,6 +989,8 @@ else version (MinGW)
 }
 else version (Darwin)
 {
+    import core.sys.darwin.config;
+
     enum
     {
         ///
@@ -1035,7 +1037,15 @@ else version (Darwin)
     // Support of OSX < 10.8 needs legacy function names without "l" suffix
     // with exception of __signbitl.  Otherwise could use else version like
     // other Darwins
-    version (OSX)
+    static if (__traits(getTargetInfo, "osxVersionMin") >= __MAC_10_8)
+    {
+        // Available OSX >= 10.8, iOS >= 6.0, all TVOS and WatchOS
+        pure int __fpclassifyl(real x);
+        pure int __isfinitel(real x);
+        pure int __isinfl(real x);
+        pure int __isnanl(real x);
+    }
+    else
     {
         version (AArch64)
         {
@@ -1057,14 +1067,6 @@ else version (Darwin)
             alias __isnanl = __isnan;
         }
     }
-    else
-    {
-        // Available OSX >= 10.8, iOS >= 6.0, all TVOS and WatchOS
-        pure int __fpclassifyl(real x);
-        pure int __isfinitel(real x);
-        pure int __isinfl(real x);
-        pure int __isnanl(real x);
-    }
 
   extern (D)
   {
diff --git a/libphobos/libdruntime/core/sys/darwin/config.d b/libphobos/libdruntime/core/sys/darwin/config.d
new file mode 100644
index 00000000000..347aea41f6b
--- /dev/null
+++ b/libphobos/libdruntime/core/sys/darwin/config.d
@@ -0,0 +1,61 @@
+/**
+ * D header file for Darwin.
+ *
+ * Copyright: Copyright (c) 2020 D Language Foundation
+ * Authors: Iain Buclaw
+ */
+module core.sys.darwin.config;
+
+version (OSX)
+    version = Darwin;
+else version (iOS)
+    version = Darwin;
+else version (TVOS)
+    version = Darwin;
+else version (WatchOS)
+    version = Darwin;
+
+version (Darwin):
+
+public import core.sys.posix.config;
+
+// The compiler exposes `__traits(getTargetInfo, "osxVersionMin")` as a way to
+// obtain the lower bound support OSX version. In some compilers this is
+// controlled by a command-line option:
+//  -mmacosx-version-min=   (on OSX).
+//  -miphoneos-version-min= (on iOS)
+//  -mtvos-version-min=     (on TVOS)
+//  -mwatchos-version-min=  (on WatchOS)
+
+enum __MAC_10_0    = 100000;
+enum __MAC_10_1    = 100100;
+enum __MAC_10_2    = 100200;
+enum __MAC_10_3    = 100300;
+enum __MAC_10_4    = 100400;
+enum __MAC_10_5    = 100500;
+enum __MAC_10_6    = 100600;
+enum __MAC_10_7    = 100700;
+enum __MAC_10_8    = 100800;
+enum __MAC_10_9    = 100900;
+enum __MAC_10_10   = 101000;
+enum __MAC_10_10_2 = 101002;
+enum __MAC_10_10_3 = 101003;
+enum __MAC_10_11   = 101100;
+enum __MAC_10_11_2 = 101102;
+enum __MAC_10_11_3 = 101103;
+enum __MAC_10_11_4 = 101104;
+enum __MAC_10_12   = 101200;
+enum __MAC_10_12_1 = 101201;
+enum __MAC_10_12_2 = 101202;
+enum __MAC_10_12_4 = 101204;
+enum __MAC_10_13   = 101300;
+enum __MAC_10_13_1 = 101301;
+enum __MAC_10_13_2 = 101302;
+enum __MAC_10_13_4 = 101304;
+enum __MAC_10_14   = 101400;
+enum __MAC_10_14_1 = 101401;
+enum __MAC_10_14_4 = 101404;
+enum __MAC_10_15   = 101500;
+enum __MAC_10_15_1 = 101501;
+enum __MAC_10_16   = 101501;
+enum __MAC_11_0    = 110000;
diff --git a/libphobos/libdruntime/core/sys/darwin/mach/dyld.d b/libphobos/libdruntime/core/sys/darwin/mach/dyld.d
index 6e28bfa655b..02d57433429 100644
--- a/libphobos/libdruntime/core/sys/darwin/mach/dyld.d
+++ b/libphobos/libdruntime/core/sys/darwin/mach/dyld.d
@@ -28,14 +28,18 @@ nothrow:
 
 public import core.stdc.stdint; // for intptr_t
 public import core.sys.darwin.mach.loader;
+import core.sys.darwin.config;
 
-uint         _dyld_image_count();
-const(char)* _dyld_get_image_name(uint image_index);
-mach_header* _dyld_get_image_header(uint image_index);
-intptr_t     _dyld_get_image_vmaddr_slide(uint image_index);
-
-void         _dyld_register_func_for_add_image(void function(const scope mach_header* mh, intptr_t vmaddr_slide));
-void         _dyld_register_func_for_remove_image(void function(const scope mach_header* mh, intptr_t vmaddr_slide));
+// __OSX_AVAILABLE_STARTING(__MAC_10_1, __IPHONE_2_0)
+static if (__traits(getTargetInfo, "osxVersionMin") >= __MAC_10_1)
+{
+    uint         _dyld_image_count();
+    const(char)* _dyld_get_image_name(uint image_index);
+    mach_header* _dyld_get_image_header(uint image_index);
+    intptr_t     _dyld_get_image_vmaddr_slide(uint image_index);
+    void         _dyld_register_func_for_add_image(void function(in mach_header* mh, intptr_t vmaddr_slide));
+    void         _dyld_register_func_for_remove_image(void function(in mach_header* mh, intptr_t vmaddr_slide));
+}
 
 int NSVersionOfRunTimeLibrary(const char* libraryPath);
 int NSVersionOfLinkTimeLibrary(const char* libraryPath);
diff --git a/libphobos/libdruntime/core/sys/darwin/pthread.d b/libphobos/libdruntime/core/sys/darwin/pthread.d
index 456cde96cbd..9a1aaafd440 100644
--- a/libphobos/libdruntime/core/sys/darwin/pthread.d
+++ b/libphobos/libdruntime/core/sys/darwin/pthread.d
@@ -29,22 +29,32 @@ nothrow:
 
 public import core.sys.posix.pthread;
 public import core.sys.darwin.mach.port;
+import core.sys.darwin.config;
 
 int pthread_is_threaded_np();
-int pthread_threadid_np(pthread_t, ulong*);
-// ^ __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2)
-int pthread_rwlock_longrdlock_np(pthread_rwlock_t*);
-int pthread_rwlock_yieldwrlock_np(pthread_rwlock_t*);
-// ^ __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_NA);
+// __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2)
+static if (__traits(getTargetInfo, "osxVersionMin") >= __MAC_10_6)
+{
+    int pthread_threadid_np(pthread_t, ulong*);
+}
+// __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_NA)
+static if (__traits(getTargetInfo, "osxVersionMin") >= __MAC_10_7)
+{
+    int pthread_rwlock_longrdlock_np(pthread_rwlock_t*);
+    int pthread_rwlock_yieldwrlock_np(pthread_rwlock_t*);
+}
 int pthread_rwlock_downgrade_np(pthread_rwlock_t*);
 int pthread_rwlock_upgrade_np(pthread_rwlock_t*);
 int pthread_rwlock_tryupgrade_np(pthread_rwlock_t*);
 int pthread_rwlock_held_np(pthread_rwlock_t*);
 int pthread_rwlock_rdheld_np(pthread_rwlock_t*);
 int pthread_rwlock_wrheld_np(pthread_rwlock_t*);
-int pthread_getname_np(pthread_t, char*, size_t);
-int pthread_setname_np(const scope char*);
-// ^ __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2)
+// __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2)
+static if (__traits(getTargetInfo, "osxVersionMin") >= __MAC_10_6)
+{
+    int pthread_getname_np(pthread_t, char*, size_t);
+    int pthread_setname_np(in char*);
+}
 int pthread_main_np();
 mach_port_t pthread_mach_thread_np(pthread_t);
 size_t pthread_get_stacksize_np(pthread_t);
@@ -53,8 +63,11 @@ int pthread_cond_signal_thread_np(pthread_cond_t*, pthread_t);
 int pthread_cond_timedwait_relative_np(pthread_cond_t*, pthread_mutex_t*, const scope timespec*);
 int pthread_create_suspended_np(pthread_t*, const scope pthread_attr_t*, void* function(void*), void*);
 int pthread_kill(pthread_t, int);
-pthread_t pthread_from_mach_thread_np(mach_port_t);
-// ^ __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0)
-int pthread_sigmask(int, const scope sigset_t*, sigset_t*);
+// __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0)
+static if (__traits(getTargetInfo, "osxVersionMin") >= __MAC_10_5)
+{
+    pthread_t pthread_from_mach_thread_np(mach_port_t);
+}
+int pthread_sigmask(int, in sigset_t*, sigset_t*);
 // ^ __DARWIN_ALIAS(pthread_sigmask)
 void pthread_yield_np();
diff --git a/libphobos/libdruntime/core/sys/darwin/string.d b/libphobos/libdruntime/core/sys/darwin/string.d
index bd65fde27bc..20dcd1fdc81 100644
--- a/libphobos/libdruntime/core/sys/darwin/string.d
+++ b/libphobos/libdruntime/core/sys/darwin/string.d
@@ -9,6 +9,7 @@ module core.sys.darwin.string;
 
 public import core.stdc.string;
 import core.sys.darwin.sys.cdefs;
+import core.sys.darwin.config;
 
 version (OSX)
     version = Darwin;
@@ -26,6 +27,9 @@ nothrow:
 
 static if (__DARWIN_C_LEVEL >= __DARWIN_C_FULL)
 {
-    // ^ __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3);
-    pure void* memmem(return const void* haystack, size_t haystacklen, scope const void* needle, size_t needlelen);
+    // __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3)
+    static if (__traits(getTargetInfo, "osxVersionMin") >= __MAC_10_7)
+    {
+        pure void* memmem(return const void* haystack, size_t haystacklen, scope const void* needle, size_t needlelen);
+    }
 }
diff --git a/libphobos/libdruntime/core/sys/posix/stdio.d b/libphobos/libdruntime/core/sys/posix/stdio.d
index b253f86b0ea..4ace0ef8945 100644
--- a/libphobos/libdruntime/core/sys/posix/stdio.d
+++ b/libphobos/libdruntime/core/sys/posix/stdio.d
@@ -373,12 +373,14 @@ else version (CRuntime_Bionic)
 }
 else version (Darwin)
 {
+    import core.sys.darwin.config;
+
     enum L_ctermid = 1024;
 
     int   fseeko(FILE*, off_t, int);
     off_t ftello(FILE*);
 
-    static if (__traits(getTargetInfo, "osxVersionMin") >= 100700)
+    static if (__traits(getTargetInfo, "osxVersionMin") >= __MAC_10_7)
     {
         ssize_t getdelim(char**, size_t*, int, FILE*);
         ssize_t getline(char**, size_t*, FILE*);
diff --git a/libphobos/libdruntime/core/sys/posix/stdlib.d b/libphobos/libdruntime/core/sys/posix/stdlib.d
index 4c10d4e0ed5..57e369d41a5 100644
--- a/libphobos/libdruntime/core/sys/posix/stdlib.d
+++ b/libphobos/libdruntime/core/sys/posix/stdlib.d
@@ -119,7 +119,13 @@ else version (Solaris)
 }
 else version (Darwin)
 {
-    int posix_memalign(void**, size_t, size_t);
+    import core.sys.darwin.config;
+
+    // __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_0)
+    static if (__traits(getTargetInfo, "osxVersionMin") >= __MAC_10_6)
+    {
+        int posix_memalign(void**, size_t, size_t);
+    }
 }
 else version (CRuntime_Bionic)
 {
diff --git a/libphobos/src/std/experimental/allocator/mallocator.d b/libphobos/src/std/experimental/allocator/mallocator.d
index 2d1dec39a74..6e1866bc976 100644
--- a/libphobos/src/std/experimental/allocator/mallocator.d
+++ b/libphobos/src/std/experimental/allocator/mallocator.d
@@ -194,6 +194,84 @@ version (Windows)
     }
 }
 
+version (Posix)
+{
+    // Not all platforms have posix_memalign available, supply own implementation
+    // if this is the case.  Avoid calling it posix_memalign because it needs
+    // special handling for realloc and free cases.
+    static if (!__traits(compiles, { import core.sys.posix.stdlib : posix_memalign; }))
+    {
+        // Helper to cast the infos written before the aligned pointer
+        // this header keeps track of the size (required to realloc) and of
+        // the base ptr (required to free).
+        private struct AlignInfo
+        {
+            void* basePtr;
+            size_t size;
+
+            @nogc nothrow
+            static AlignInfo* opCall(void* ptr)
+            {
+                return cast(AlignInfo*) (ptr - AlignInfo.sizeof);
+            }
+        }
+
+        @nogc nothrow
+        private void* _aligned_malloc(size_t size, size_t alignment)
+        {
+            import core.stdc.stdlib : malloc;
+            size_t offset = alignment + size_t.sizeof * 2 - 1;
+
+            // unaligned chunk
+            void* basePtr = malloc(size + offset);
+            if (!basePtr) return null;
+
+            // get aligned location within the chunk
+            void* alignedPtr = cast(void**)((cast(size_t)(basePtr) + offset)
+                & ~(alignment - 1));
+
+            // write the header before the aligned pointer
+            AlignInfo* head = AlignInfo(alignedPtr);
+            head.basePtr = basePtr;
+            head.size = size;
+
+            return alignedPtr;
+        }
+
+        @nogc nothrow
+        private void* _aligned_realloc(void* ptr, size_t size, size_t alignment)
+        {
+            import core.stdc.stdlib : free;
+            import core.stdc.string : memcpy;
+
+            if (!ptr) return _aligned_malloc(size, alignment);
+
+            // gets the header from the exising pointer
+            AlignInfo* head = AlignInfo(ptr);
+
+            // gets a new aligned pointer
+            void* alignedPtr = _aligned_malloc(size, alignment);
+            if (!alignedPtr)
+                return null;
+
+            // copy exising data
+            memcpy(alignedPtr, ptr, head.size);
+            free(head.basePtr);
+
+            return alignedPtr;
+        }
+
+        @nogc nothrow
+        private void _aligned_free(void *ptr)
+        {
+            import core.stdc.stdlib : free;
+            if (!ptr) return;
+            AlignInfo* head = AlignInfo(ptr);
+            free(head.basePtr);
+        }
+    }
+}
+
 /**
    Aligned allocator using OS-specific primitives, under a uniform API.
  */
@@ -226,24 +304,32 @@ struct AlignedMallocator
     @trusted @nogc nothrow
     void[] alignedAllocate(size_t bytes, uint a) shared
     {
-        import core.stdc.errno : ENOMEM, EINVAL;
-        import core.sys.posix.stdlib : posix_memalign;
-        assert(a.isGoodDynamicAlignment);
-        void* result;
-        auto code = posix_memalign(&result, a, bytes);
-        if (code == ENOMEM)
-            return null;
-
-        else if (code == EINVAL)
+        static if (__traits(compiles, _aligned_malloc))
         {
-            assert(0, "AlignedMallocator.alignment is not a power of two "
-                ~"multiple of (void*).sizeof, according to posix_memalign!");
+            auto result = _aligned_malloc(bytes, a);
+            return result ? result[0 .. bytes] : null;
         }
-        else if (code != 0)
-            assert(0, "posix_memalign returned an unknown code!");
-
         else
-            return result[0 .. bytes];
+        {
+            import core.stdc.errno : ENOMEM, EINVAL;
+            import core.sys.posix.stdlib : posix_memalign;
+            assert(a.isGoodDynamicAlignment);
+            void* result;
+            auto code = posix_memalign(&result, a, bytes);
+            if (code == ENOMEM)
+                return null;
+
+            else if (code == EINVAL)
+            {
+                assert(0, "AlignedMallocator.alignment is not a power of two "
+                    ~"multiple of (void*).sizeof, according to posix_memalign!");
+            }
+            else if (code != 0)
+                assert(0, "posix_memalign returned an unknown code!");
+
+            else
+                return result[0 .. bytes];
+        }
     }
     else version (Windows)
     @trusted @nogc nothrow
@@ -263,8 +349,13 @@ struct AlignedMallocator
     @system @nogc nothrow
     bool deallocate(void[] b) shared
     {
-        import core.stdc.stdlib : free;
-        free(b.ptr);
+        static if (__traits(compiles, _aligned_free))
+            _aligned_free(b.ptr);
+        else
+        {
+            import core.stdc.stdlib : free;
+            free(b.ptr);
+        }
         return true;
     }
     else version (Windows)
@@ -284,7 +375,21 @@ struct AlignedMallocator
     @system @nogc nothrow
     bool reallocate(ref void[] b, size_t newSize) shared
     {
-        return Mallocator.instance.reallocate(b, newSize);
+        static if (__traits(compiles, _aligned_realloc))
+        {
+            if (!newSize)
+            {
+                deallocate(b);
+                b = null;
+                return true;
+            }
+            auto p = cast(ubyte*) _aligned_realloc(b.ptr, newSize, alignment);
+            if (!p) return false;
+            b = p[0 .. newSize];
+            return true;
+        }
+        else
+            return Mallocator.instance.reallocate(b, newSize);
     }
     version (Windows)
     @system @nogc nothrow


             reply	other threads:[~2021-03-07 17:01 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-07 17:01 Iain Buclaw [this message]
  -- strict thread matches above, loose matches on Subject: below --
2021-09-17 14:34 Iain Buclaw
2021-04-19 18:05 Iain Buclaw
2021-04-10 17:00 Iain Buclaw
2021-04-10 15:04 Iain Buclaw
2021-03-14 22:00 Iain Buclaw
2021-01-30 19:08 Iain Buclaw
2021-01-28 17:31 Iain Buclaw
2021-01-11 11:39 Iain Buclaw
2020-12-22 13:41 Iain Buclaw
2020-12-09  9:51 Iain Buclaw
2020-12-05 23:47 Iain Buclaw
2020-12-05 23:38 Iain Buclaw
2020-12-05 23:29 Iain Buclaw
2020-12-05 23:27 Iain Buclaw
2020-12-05 23:20 Iain Buclaw

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210307170145.0C66C393A43D@sourceware.org \
    --to=ibuclaw@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).