Correct layout for memory allocator / memory? (which algorithm)

Hello, I’m thinking of trying to expand my skills by trying some things that I have never done before. One thing that puzzled me a bit was memory allocators and memory pools. I want to make a memory block and only select memory from the system once. I have what is currently configured, the memory is an array of bytes (or characters) that is 65535 for my testing purpose.

I have two algorithms that I have examined using.

First, this is an algorithm in which the entire data block is added with the remaining memory and a pointer (or rather, an offset) to the first allocated block (or block header), then each selection is preceded by the size of the selection, and the previous and next distribution, so I can easy to release selection. Then I can generate the largest and smallest blocks by looking at the space after the current allocations.

Another option I have is to add a second offset in front of the allocated memory and make this point the first unoccupied block, then each unallocated block also has a “Previous and Next” distribution along with the size so that I can easily find a place to place the next distribution.

The problem is that I can’t say what is “right”. Suppose that we will have a distribution of variables by size (but most of them will have a size that will not be so overhead.) The first will be slower to receive the largest and smallest possible blocks, but I can store and manipulate them, if necessary, to avoid their regeneration. However, the second one will take longer to release (due to the fact that you need to find which deactivator is next to the dispenser) and does not necessarily provide any benefits for distribution. In fact, this will require a more specialized code for the case when less than 6 bytes are left (2 bytes for size, 2 for pre-offset, 2 for next offset).

, , - . ? ?

+3
1

(, , ). : , ...

+1

Source: https://habr.com/ru/post/1775592/


All Articles