Rebinding in a custom STL allocator with pre-allocated block

后端 未结 1 963
情歌与酒
情歌与酒 2021-02-12 17:40

I\'m going to build a custom allocator, preallocating a big block (array) for storing N elements of some class T, and then just increase an in

1条回答
  •  伪装坚强ぢ
    2021-02-12 18:18

    You are on the right track.

    One irritating details is that copies of allocators must compare equal, even converting (rebound) copies. Comparing equal means they can deallocate each other's pointers. So a container such as std::list will rebind your_alloc to your_alloc> and then construct a your_alloc> using a your_alloc. And technically your your_alloc> has to deallocate a pointer allocated by your_alloc.

    Here is my attempt to meet this requirement. Feel free to copy/modify/abuse this code. My intent is to educate, not become the world's allocator supplier (which wouldn't exactly be lucrative anyway :-)).

    This example takes a slightly different approach to alignment: I happen to know that on my platform of interest (OS X, iOS) that malloc returns 16 byte aligned memory, and so that's all my custom allocator needs to return. You could change that number to whatever is appropriate for your system.

    This hardwiring of alignment means that a single pool can safely supply several allocator and allocator>, as they are all 16-byte aligned (and that's enough). And it also means that copies, even converted copies, can detect when the pointer is pointing into the buffer, since copies all share the same buffer.

    Said slightly differently, the C++ committee has effectively specified that allocators are reference types: copies are equivalent and point to the same memory pool.

    You can get away with cheating, using memory pools actually embedded into the allocator, but only with some containers on some implementations, and you can't back that up with a quote from the standard.

    0 讨论(0)
提交回复
热议问题