我是否需要与页面边界对齐? [英] Do I need to align to page boundaries?

查看:65
本文介绍了我是否需要与页面边界对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定重新发明轮子第一百万次并编写我自己的内存池。我唯一的问题是关于页面大小的界限。



假设GetSystemInfo()调用告诉我页面大小为4096字节。现在,我想预先分配1MB的内存区域(可能更小或更大),并将此区域划分为128字节块。我猜,HeapAlloc()/ VirtualAlloc()的开销在8到16个字节之间。可能会更多,我读过有关60字节的帖子。



问题是,我是否需要注意不要使用128字节块之一跨越页面边界?



我只需在一个块中分配1MB并将其分成块大小吗?



或者我应该分配多个块,比如4000字节(考虑到HeapAlloc()开销),并将这4000字节细分为128字节块(4000/128 = 31块,每块128字节)而不是完全使用剩余的字节(本例中为4000 - 31x128 = 32字节)?

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)?

推荐答案

我发现我真正需要的是_aligned_malloc(),或者一些公式,以确保我总是在页面边界上有我的块。



HeapAlloc()/ malloc()不保证任何分配都适合于页。根据文档,我在32位/ 64位系统上有8个字节/ 16个字节的块,这就是我得到的所有对象。



如果我想要为了确保页面对齐,我将不得不接受一些未使用且无法访问的内存。
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.


这篇关于我是否需要与页面边界对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆