This thread from a few years ago says it's not possible. I assume that is still the case. True?
I probably don't need it at the moment. I wrote something that uses persistent data structures to model rich text documents. I like how it works - undo and redo are trivial. But there is a potential for a lot of allocation of the same few "document node" types over and over, as the user types and edits a document. So I was wondering about this.
You can "relatively" easily write a custom allocator for structs. It will have to rely on pointers, which are extremely clunky in Swift, but this is doable.
However, even if you do it, you'll hit a big discrepancy with that Swift is designed to work with references overwhelmingly as classes; there's no idiomatic way currently to "just" pass a pointer to your allocated struct and/or prevent your carefully constructed allocation from getting frivolously copied around, since structs are supposed to behave this way. There are inout references, but otherwise you're at the mercy of the compiler being able to prove that you don't need to copy a value.
For classes, I'm not aware of an even remotely adequate method to query an object's storage size, especially that (if I'm not mistaking) Swift objects carry a field 8 bytes above the respective allocation to point to the protocol table.
As such, I would argue, if you ever need to have some custom allocation logic in your program, you should stay away from Swift altogether.