21
21
#include " ../SwiftShims/RuntimeShims.h"
22
22
#include < algorithm>
23
23
#include < stdlib.h>
24
+ #if defined(__APPLE__)
25
+ #include " swift/Basic/Lazy.h"
26
+ #include < malloc/malloc.h>
27
+ #endif
24
28
25
29
using namespace swift ;
26
30
@@ -57,6 +61,13 @@ using namespace swift;
57
61
static_assert (_swift_MinAllocationAlignment > MALLOC_ALIGN_MASK,
58
62
" Swift's default alignment must exceed platform malloc mask." );
59
63
64
+ #if defined(__APPLE__)
65
+ static inline malloc_zone_t *DEFAULT_ZONE () {
66
+ static malloc_zone_t *z = SWIFT_LAZY_CONSTANT (malloc_default_zone ());
67
+ return z;
68
+ }
69
+ #endif
70
+
60
71
// When alignMask == ~(size_t(0)), allocation uses the "default"
61
72
// _swift_MinAllocationAlignment. This is different than calling swift_slowAlloc
62
73
// with `alignMask == _swift_MinAllocationAlignment - 1` because it forces
@@ -77,7 +88,11 @@ void *swift::swift_slowAlloc(size_t size, size_t alignMask) {
77
88
void *p;
78
89
// This check also forces "default" alignment to use AlignedAlloc.
79
90
if (alignMask <= MALLOC_ALIGN_MASK) {
91
+ #if defined(__APPLE__)
92
+ p = malloc_zone_malloc (DEFAULT_ZONE (), size);
93
+ #else
80
94
p = malloc (size);
95
+ #endif
81
96
} else {
82
97
size_t alignment = (alignMask == ~(size_t (0 )))
83
98
? _swift_MinAllocationAlignment
@@ -106,7 +121,11 @@ void *swift::swift_slowAlloc(size_t size, size_t alignMask) {
106
121
// consistent with allocation with the same alignment.
107
122
void swift::swift_slowDealloc (void *ptr, size_t bytes, size_t alignMask) {
108
123
if (alignMask <= MALLOC_ALIGN_MASK) {
124
+ #if defined(__APPLE__)
125
+ malloc_zone_free (DEFAULT_ZONE (), ptr);
126
+ #else
109
127
free (ptr);
128
+ #endif
110
129
} else {
111
130
AlignedFree (ptr);
112
131
}
0 commit comments