Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You say that allocating such large blocks <em>starts</em> to return null pointers. To me, this implies that it works for a while, that you're allocating and freeing quite a few of these over time, and that after having done some MANY times, you start getting nulls.</p> <p>If that is the case, you're seeing a classic case of memory fragmentation. (Pardon the following novel if my assumption is incorrect.) In the past, I've handled such issues by writing my own memory manager that allows me to relocate objects in memory to make room for the allocation of new blocks. If your code has to be able to hold pointer into the allocated blocks, your memory manager is going to have to involve a pointer implementation that will make the moves transparent. This becomes difficult to impossible if you're relying on third party software.</p> <p>You might look into preallocating a pool of each of the block sizes that you'll be using. This ensures that you'll never fragment your memory in a way that a large block will only be unavailable if you have used the entire pool of large blocks, as opposed to having plenty of free memory, but only in small fragments. Check up on a method called <a href="http://en.wikipedia.org/wiki/Slab_allocation" rel="nofollow">Slab Allocation</a>.</p> <p>My implementation of relocatable memory worked something like this: At the beginning of program execution, I allocated the largest single block of memory available. (mmap is your friend.) If I could not find a free fragment where I could satisfy an allocation request, I find some block with a chunk of free space before and after it, and move it to the location of the preceding free space. I'd plan this move so that the resulting free block would be large enough for the newly requested allocation.</p> <p>This is not a good general solution. It can be very slow and I wouldn't use it in a system that required real-time performance. (Think about what happens if you have to shuffle a few dozen blocks for the sake of one allocation. Worst case, you have to copy every allocated byte for the sake of satisfying one request.) It also requires that you can tolerate the shuffling of every pointer that refers into this memory. But it does provide more efficient use of memory than a slab allocator, if it is absolutely necessary.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload