Recently i have been looking for a pool/allocator mechanism. Boost Pool seems to provide the solution, but there is still things, which it have not been able to deduce from the documentation.
What need to be allocated
- Several small classes (~30 chars)
- std::map (i want to ensure it do not perform dynamic allocator by itself)
- allocation within pugi::xml
- std::strings
How to control of address space for allocation (or just amount)
The object_pool seem the to provide a good way for allocating need 1) However it would like to set a fixed size for the allocator to use. By default it grabs memory be ifself. If possible i would like to give it the address space it can play within.
char * mem_for_class[1024*1024];
boost::object_pool<my_class,mem_for_class> q;
or:
const int max_no_objs=1024;
boost::object_pool<my_class,max_no_objs> q;
Although the UserAllocator is available in Boost::Pool; it seem to defeat the point. I am afraid the control needed would make it too inefficient... and it would be better to start from scratch.
It it possible to set a fixed area for pool_allocator ?
The question is a bit similar to the first. Do boost pool provide any way of limiting how much / where there is allocated memory when giving boost::pool_allocator to a std-type-class (e.g. map)
My scenario
Embedded linux programming. The system must keep running for..ever. So we can not risk any memory segmentation. Currently i mostly either static allocation (stack), but also a few raw "new"s. I would like an allocation scheme that ensure i use the same memory area each time the program loops. Speed /space is important, but safety is still top priority.
I hope StackOverflow is the place to ask. I tried contacting the author of Boost::Pool "Stephen" without luck. I have not found any Boost-specific forum.
Using std::map introduce a lot of extra syntax... which can be confusing... no luck yet I can not easy spot what i am doing wrong. The attempt so far:
I guess the first step rather should be to write a template which forward parameters to the template of std::map
You can always create an allocator that works with STL. If it works with STL, it should work with boost as you are able to pass boost allocators to STL containers..
Considering the above, an allocator that can allocate at a specified memory address AND has a size limitation specified by you can be written as follows:
This allocator makes sure that all memory is allocated at a specified address. No more than the amount you specify can be allocated with the freedom to allocate were you want whether it is on the stack or the heap.
You may create your own pool or use a
std::unique_ptr
as the pool for a single container.EDIT: For strings, you need an offset of
sizeof(_Rep_base)
. See: Why std::string allocating twice?and http://ideone.com/QWtxWg
It is defined as:
So the example becomes: