2 hours ago, lawnjelly said:The point of your handles is presumably to deal with a block of memory that may be relocatable.
Yes!
QuoteAs far as I can see if you want to use a standard vector you either need to:
- Override the mechanism for determining the start of the vector array to go through the handle each time it is accessed (pretty inefficient) or
- Update the start of array pointer in the vector on relocating the memory (and watch for dangling pointers)
Yes! Good analysis of where this is going though specific to vector, which I agree.
QuoteAfaik, underneath it all, although the implementation isn't specified, a vector is typically just a fancy dynamic array. If you want the vector to use your handle you would presumably have to provide it with the array address, either on the fly, or when it changes (preferable).
Yes, so far I agree.
QuoteIt may be an idea to write your own template wrapper if you intend to bandit an STL implementation, to emphasise it is working differently under the hood (as relocatable code may break the 'expected contract').
This I don't understand a bit. Template wrapper as in, I concretely wrap the vector?
QuoteI must say I still strongly suspect this is something that could be achieved much better with a different paradigm, however, without knowing the details we can only answer what you have asked directly.
Okay just trying to summarize, please correct me if I misinterpret. So according to you, it's impossible to sneak my object handle in through the custom STL allocator in any way unless I forge the STL implementation to work for my will, in case I'm still holding up with the paradigm of this relocatable memory for such things.
So up to this point I conclude that it is impossible, with the case I gave, to provide a custom STL allocator through the indirect memory access (the object handle)?
1 hour ago, lawnjelly said:Actually thinking about it you may be able to force an allocation through your relocation code without modifying the STL code .. by clever use of reserve and resize. Depends on the STL implementation though and how it is about shrinking vectors.
i.e. You want to move array from A to B, array size is 10, max size is 16.
resize(10) according to this (http://www.cplusplus.com/reference/vector/vector/resize/) should result in reallocation enabling you to relocate in your custom allocator.
In the case where array size == max size, you might need to force an allocation with a resize(11) then delete the last element .. which might be costly depending on the object. There may be a better way, just an idea.
I get your idea I think, but in general this does not matter. If the vector chunk shrinks/grows due to resize, then what it does is, as you said (also according to the "Complexity" section in the link you gave), the chunk should be "updated" with new size info (as in capacity) where it means that it is reallocated to a new chunk with new size. In short basically it performs allocation/deallocation (if it will depending on the conditions). This is what this thread is about; I want my memory allocator to capture this moment, which I think (so far) should be done by intercepting the default allocator using custom STL allocator and inject it with my memory allocator that handing object handle, which as you see in the picture, I couldn't able to.
If you see figure 2 in my picture, there's an unused block (colored in grey). Whenever the vector grows beyond its current capacity or do what you proposed due to resize, if it uses the custom memory allocation I provided, the vector array will abandon its position 0x00 (a.k.a deallocate) and then "move to" (a.k.a allocate new chunk to) the unused block, whether with bigger size array or resized smaller array. It will be given a new object handle upon allocation. The abandoned (freed) 0x00 will be defragmented sometime later. This keeps going on the same with other objects too whenever there's any allocation/deallocation. So in short it should be done automatically anyway, again, if the STL allocator is successfully customized with my memory allocator with the object handle thingy.