Default allocator for Swift

What default allocator does Swift use? Is this allocator used on all platforms? On iOS?

Do I understand correctly that Darwin.malloc does not use the same allocator as UnsafeMutablePointer.allocate, ManagedBuffer, etc. ?

1 Like

It’s not guaranteed to be the same allocator, but on Darwin at least you should always be able to free that memory with free — it would be pretty irresponsible of us to ever add an allocator that didn’t cooperate with malloc zones.

6 Likes

For historical interest: Once upon a time we had hopes of the Swift allocator requiring the size of the allocation to be presented at deallocation time, at least on Apple platforms. There are pros and cons to this, but it was thought to at least potentially be worth it.

We implemented that incorrectly for variably-sized allocations like ManagedBuffer, and now it’ll almost certainly never happen. Sigh.

(None of this has to contradict what John said, because the allocator for classes, closures, and indirect enums doesn’t have to be the same allocator used by UnsafeMutablePointer.)

7 Likes

There is also some information in this thread:

3 Likes