Skip to content

Commit 71b8d03

Browse files
committed
Cache the default malloc zone
1 parent 0f0a908 commit 71b8d03

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

stdlib/public/runtime/Heap.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
#include "../SwiftShims/RuntimeShims.h"
2222
#include <algorithm>
2323
#include <stdlib.h>
24+
#if defined(__APPLE__)
25+
#include "swift/Basic/Lazy.h"
26+
#include <malloc/malloc.h>
27+
#endif
2428

2529
using namespace swift;
2630

@@ -57,6 +61,13 @@ using namespace swift;
5761
static_assert(_swift_MinAllocationAlignment > MALLOC_ALIGN_MASK,
5862
"Swift's default alignment must exceed platform malloc mask.");
5963

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+
6071
// When alignMask == ~(size_t(0)), allocation uses the "default"
6172
// _swift_MinAllocationAlignment. This is different than calling swift_slowAlloc
6273
// with `alignMask == _swift_MinAllocationAlignment - 1` because it forces
@@ -77,7 +88,11 @@ void *swift::swift_slowAlloc(size_t size, size_t alignMask) {
7788
void *p;
7889
// This check also forces "default" alignment to use AlignedAlloc.
7990
if (alignMask <= MALLOC_ALIGN_MASK) {
91+
#if defined(__APPLE__)
92+
p = malloc_zone_malloc(DEFAULT_ZONE(), size);
93+
#else
8094
p = malloc(size);
95+
#endif
8196
} else {
8297
size_t alignment = (alignMask == ~(size_t(0)))
8398
? _swift_MinAllocationAlignment
@@ -106,7 +121,11 @@ void *swift::swift_slowAlloc(size_t size, size_t alignMask) {
106121
// consistent with allocation with the same alignment.
107122
void swift::swift_slowDealloc(void *ptr, size_t bytes, size_t alignMask) {
108123
if (alignMask <= MALLOC_ALIGN_MASK) {
124+
#if defined(__APPLE__)
125+
malloc_zone_free(DEFAULT_ZONE(), ptr);
126+
#else
109127
free(ptr);
128+
#endif
110129
} else {
111130
AlignedFree(ptr);
112131
}

0 commit comments

Comments
 (0)