@@ -5942,9 +5942,9 @@ void *MetadataAllocator::Allocate(size_t size, size_t alignment) {
5942
5942
static OnceToken_t getenvToken;
5943
5943
SWIFT_ONCE_F (getenvToken, checkAllocatorDebugEnvironmentVariables, nullptr );
5944
5944
5945
- // If the size is larger than the maximum, just use malloc .
5945
+ // If the size is larger than the maximum, just do a normal heap allocation .
5946
5946
if (size > PoolRange::MaxPoolAllocationSize) {
5947
- void *allocation = malloc (size);
5947
+ void *allocation = swift_slowAlloc (size, alignment - 1 );
5948
5948
memsetScribble (allocation, size);
5949
5949
return allocation;
5950
5950
}
@@ -5984,6 +5984,21 @@ void *MetadataAllocator::Allocate(size_t size, size_t alignment) {
5984
5984
__asan_poison_memory_region (allocation, newState.Remaining );
5985
5985
}
5986
5986
5987
+ // NULL should be impossible, but check anyway in case of bugs or corruption
5988
+ if (SWIFT_UNLIKELY (!allocation)) {
5989
+ PoolRange curStateReRead = AllocationPool.load (std::memory_order_relaxed);
5990
+ swift::fatalError (
5991
+ 0 ,
5992
+ " Metadata allocator corruption: allocation is NULL. "
5993
+ " curState: {%p, %zu} - curStateReRead: {%p, %zu} - "
5994
+ " newState: {%p, %zu} - allocatedNewPage: %s - requested size: %zu - "
5995
+ " sizeWithHeader: %zu - alignment: %zu - Tag: %d\n " ,
5996
+ curState.Begin , curState.Remaining , curStateReRead.Begin ,
5997
+ curStateReRead.Remaining , newState.Begin , newState.Remaining ,
5998
+ allocatedNewPage ? " true" : " false" , size, sizeWithHeader, alignment,
5999
+ Tag);
6000
+ }
6001
+
5987
6002
// Swap in the new state.
5988
6003
if (std::atomic_compare_exchange_weak_explicit (&AllocationPool,
5989
6004
&curState, newState,
@@ -6024,7 +6039,7 @@ void MetadataAllocator::Deallocate(const void *allocation, size_t size,
6024
6039
__asan_poison_memory_region (allocation, size);
6025
6040
6026
6041
if (size > PoolRange::MaxPoolAllocationSize) {
6027
- free (const_cast <void *>(allocation));
6042
+ swift_slowDealloc (const_cast <void *>(allocation), size, Alignment - 1 );
6028
6043
return ;
6029
6044
}
6030
6045
0 commit comments