Click here to Skip to main content
15,889,862 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have decided to reinvent the wheel for the millionth time and write my own memory pool. My only question is about page size boundaries.

Let's say GetSystemInfo() call tells me that the page size is 4096 bytes. Now, I want to preallocate a memory area of 1MB (could be smaller, or larger), and divide this area into 128 byte blocks. HeapAlloc()/VirtualAlloc() will have an overhead between 8 and 16 bytes I guess. Might be some more, I've read posts talking about 60 bytes.

Question is, do I need to pay attention to not to have one of my 128 byte blocks across page boundaries?

Do I simply allocate 1MB in one chunk and divide it into my block size?

Or should I allocate many blocks of, say, 4000 bytes (to take into account HeapAlloc() overhead), and sub-divide this 4000 bytes into 128 byte blocks (4000 / 128 = 31 blocks, 128 bytes each) and not use the remaining bytes at all (4000 - 31x128 = 32 bytes in this example)?
Posted
Updated 3-Feb-14 8:04am
v2
Comments
Sergey Alexandrovich Kryukov 3-Feb-14 15:10pm    
Not quite clear. You should understand that if you do something in one chunk, sub-chunks may not make any sense.
Perhaps you need to put more detail in your explanation of the problem, starting with the explanation of the goal of this activity.
—SA
tablespoon 3-Feb-14 16:05pm    
I want to implement a memory pool. I want to allocate a large chunk with a system call, like HeapAlloc() or malloc(). Then I want to write my own routines to allocate/free memory blocks from this large chunk, using my own memory manager.

Question: Do I need to pay attention to the Page Size of the system when I return these small memory chunks to the caller of my routines?

Example: My initial chunk is 8000 bytes. This chunk fits into two pages of 4KB each. I suballocate blocks of 128 bytes to the caller of my routines from this chunk. I start allocating from the beginning of the chunk. At some point, one of my blocks would reside in two pages because 8000 is not divisible by 128.

Is it a bad idea to have my blocks spread to two pages? Would that be bad for performance?
[no name] 5-Feb-14 12:02pm    
The page size isn't 4k, it is 4096. 4096 and 8192 are divisible by 128.
tablespoon 6-Feb-14 17:15pm    
bling, in the example I allocated 4000 bytes because of the HeapAlloc()/malloc() memory header. I thought if I allocated a little less than the page size, I'd guarantee that my block would entirely reside in one page, and wouldn't cross the boundaries. I was wrong.

1 solution

I've discovered that what I really needed was _aligned_malloc(), or some formulation to ensure that I'd always have my blocks on page boundaries.

HeapAlloc()/malloc() doesn't guarantee that any allocation will fit in a page. According to the documentation, I have blocks alinged on 8 bytes/16 bytes on 32 bit/64 bit systems, and that's all the alignement I get.

If I want to ensure page alignement, I'll have to accept some memory remaining unused and unaccessible.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900