It turned out memory allocation is a real performance killer for my application, and i need to manage memory myself.
Using STL, have made a custom allocator which should solve the fragmentation problem.
To my surprise, this works just fine for std::vector after some research and trial and error. It could do anything i need, although there some hidden problems, e.g. using std::swap involving different allocators, but i can life with that.
Here's how basic usage looks:
using PagedAlloc = Allocators::PagedAllocator<Allocators::FreeListAllocator, 1024, 4>;
using stdAlloc = Allocators::stdAllocator<int, PagedAlloc>;
PagedAlloc pagedAlloc;
PagedAlloc pagedAlloc2;
stdAlloc alloc(&pagedAlloc);
std::vector<int, stdAlloc > vec(alloc);
stdAlloc alloc2(&pagedAlloc2);
std::vector<int, stdAlloc> vec2(alloc2);
std::vector<int, stdAlloc> test(10, -1, alloc);
test.resize(20);
Basically i need to give the allocator to the constructor of std::vector.
Which is a show stopper, because i can not create an array for example:
std::vector<int, stdAlloc> vecN[10];
I can not call the proper constructor to set the allocator of an array of vectors at this point.
I could loop over the 10 elements to somehow set the allocator afterwards, but it's too late. STL calls the default constructor of vector and allocates one element (for whatever reason), which crashes because the allocator was not yet set.
This means:
To use custom allocators, i'm now forced to remove ALL default constructors for any class that has vector members.
They all need to be constructed given the allocator in parameterized constructors, forwarding that to members during construction in time.
So i can't have an array of any of my data structures, and also i can not create data structures in memory to set them up later for use. It's no longer just data. C++ forces me to treat everything as ‘objects’.
This is nonsense. Bjarne was wrong, and they still haven't fixed it.
Or do i miss something? Are there some hacks to postpone calling constructors until it can be actually done? Maybe using placement new or somethign like that?
Any help welcome. But currently it looks if have to write my own version of STL… <:(