0
votes

I am implementing a memory pool - type class. One of the methods allocates B bytes of memory and returns a void pointer to it, while internally handling buffers and moving around older memory to ensure all memory managed by the object during its lifetime is contiguous (similar to how a std::vector will have a pre allocated buffer and allocate extra space once the buffer runs out, copying the information from the old buffer to the new one to ensure all memory is contiguous). My question is, how do I ensure, or check for, all the allocated memory being continuous? If I wish to jump from object to object manually, using

static_cast<desired_type*>(buffer_pointer + N)

This method is naturally going to fail if the location for an object is offset by some amount that isn't just the sum of the sizes of the previous objects. I am new to writing custom memory pools, so I am just wondering, how do I either ensure allocated memory is non fragmented, or access the location of the new fragment so that I can still manually index through a block of malloc()-ed memory? Thank you.

1
the only solution is to allocate a single block with malloc or new and manage it manually. Or use a pre-allocated vector. - Jean-François Fabre
Write your own allocator? - P.W
seems that you have to manage all information related to objects (size, location...) - duong_dajgja
@duong_dajgja yeah, the class tracks the amount of bytes already used up, and the max byte capacity. What I am unsure of how to do is track location. The allocation method returns a tuple of a pointer to the beginning of the contiguous memory block (in case it changed from the previous allocation because of buffer overflow), and a pointer to the start of the new location. When new memory is added to it, however, I want to be able to take the beginning pointer and increment by size to get where each object ends and another one starts, which is why I want to know how i can test for continuity - Saswat Mishra
@Jean-FrançoisFabre Yes, the method allocates a buffer from a single malloc until the buffer overflows (N+1 bytes requested from a buffer of size N). In the case of overflow, the class moves the data onto a new buffer with enough size. I want to be able to retrieve the position of each object from just the size of each object and the starting position, which should be straightforward - just add the cumulative preceding sizes to the start. However, not sure if this works for fragmented memory, so is there a way I can check for fragmentation, or is that not a problem with a single malloc buffer? - Saswat Mishra

1 Answers

0
votes

If I understand your question, you're asking if you can have multiple calls to malloc return contiguous memory.

The answer is no, memory will not be contiguous across multiple mallocs as most memory managers will put head/tail data around the allocated memory for both their own management and to put protection markers around the edges to detect overruns - the details are heavily implementation dependent.

for your own memory management you need to allocate a big enough block with malloc and then split it and manage the internals yourself.

You can look at this github proj as an example of the management required: https://github.com/bcorriveau/memblock