Skip to content

Commit 20db6c9

Browse files
authored
Merge pull request #58911 from apple/internal-new-delete
[Runtime] Define our own hidden global new/delete on Darwin.
2 parents 65ed38d + 5806a7c commit 20db6c9

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

stdlib/public/runtime/Heap.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ void *swift::swift_slowAlloc(size_t size, size_t alignMask) {
118118
// i.e. 0 < alignment <= _minAllocationAlignment:
119119
// The runtime may use either `free` or AlignedFree as long as it is
120120
// consistent with allocation with the same alignment.
121-
void swift::swift_slowDealloc(void *ptr, size_t bytes, size_t alignMask) {
121+
static void swift_slowDeallocImpl(void *ptr, size_t alignMask) {
122122
if (alignMask <= MALLOC_ALIGN_MASK) {
123123
#if defined(__APPLE__) && SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC
124124
malloc_zone_free(DEFAULT_ZONE(), ptr);
@@ -129,3 +129,34 @@ void swift::swift_slowDealloc(void *ptr, size_t bytes, size_t alignMask) {
129129
AlignedFree(ptr);
130130
}
131131
}
132+
133+
void swift::swift_slowDealloc(void *ptr, size_t bytes, size_t alignMask) {
134+
swift_slowDeallocImpl(ptr, alignMask);
135+
}
136+
137+
#if defined(__APPLE__) && defined(__MACH__) && SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC
138+
// On Darwin, define our own, hidden operator new/delete implementations. We
139+
// don't want to pick up any overrides that come from other code, but we also
140+
// don't want to expose our overrides to any other code. We can't do this
141+
// directly in C++, as the compiler has an implicit prototype with default
142+
// visibility. However, if we implement them as C functions using the C++
143+
// mangled names, the compiler accepts them without complaint, and the linker
144+
// still links all internal uses with these overrides.
145+
146+
__attribute__((visibility(("hidden")))) extern "C" void *_Znwm(size_t size) {
147+
return swift_slowAlloc(size, MALLOC_ALIGN_MASK);
148+
}
149+
150+
__attribute__((visibility(("hidden")))) extern "C" void _ZdlPv(void *ptr) {
151+
swift_slowDeallocImpl(ptr, MALLOC_ALIGN_MASK);
152+
}
153+
154+
__attribute__((visibility(("hidden")))) extern "C" void *_Znam(size_t size) {
155+
return swift_slowAlloc(size, MALLOC_ALIGN_MASK);
156+
}
157+
158+
__attribute__((visibility(("hidden")))) extern "C" void _ZdaPv(void *ptr) {
159+
swift_slowDeallocImpl(ptr, MALLOC_ALIGN_MASK);
160+
}
161+
162+
#endif

0 commit comments

Comments
 (0)