I am working on a plugin for an application, where the memory should be allocated by the Application and keep track of it. Hence, memory handles should be obtained from the host application in the form of buffers and later on give them back to the application. Now, I am planning on using STL Vectors and I am wondering what sort of memory allocation does it use internally.
Does it use ‘new’ and ‘delete’ functions internally? If so, can I just overload ‘new’ and ‘delete’ with my own functions? Or should I create my own template allocator which looks like a difficult job for me since I am not that experienced in creating custom templates.
Any suggestions/sample code are welcome. Memory handles can be obtained from the application like this
void* bufferH = NULL;
bufferH = MemReg()->New_Mem_Handle(size_of_buffer);
MemReg()->Dispose_Mem_Handle(bufferH); //Dispose it
vectorusesstd::allocatorby default, andstd::allocatoris required to use global operator new (that is,::operator new(size_t)) to obtain the memory (20.4.1.1). However, it isn’t required to call it exactly once per call toallocator::allocate.So yes, if you replace global operator new then
vectorwill use it, although not necessarily in a way that really allows your implementation to manage memory “efficiently”. Any special tricks you want to use could, in principle, be made completely irrelevant bystd::allocatorgrabbing memory in 10MB chunks and sub-allocating.If you have a particular implementation in mind, you can look at how its
vectorbehaves, which is probably good enough if your planned allocation strategy is inherently platform-specific.