简体   繁体   English

mmap:强制64K对齐

[英]mmap: enforce 64K alignment

I'm porting a project written (by me) for Windows to mobile platforms. 我正在将(由我自己)为Windows编写的项目移植到移动平台。

I need an equivalent of VirtualAlloc (+friends), and the natural one is mmap . 我需要一个等效的VirtualAlloc (+ friends),自然是mmap However there are 2 significant differences. 但是,有2个重大差异。

  1. Addresses returned by VirtualAlloc are guaranteed to be multiples of the so-called allocation granularity ( dwAllocationGranularity ). VirtualAlloc返回的地址保证是所谓的分配粒度dwAllocationGranularity )的dwAllocationGranularity Not to be confused with the page size, this number is arbitrary, and on most Windows system is 64K. 不要与页面大小混淆,该数字是任意的,并且在大多数Windows系统上是64K。 In contrast address returned by mmap is only guaranteed to be page-aligned. 相反, mmap返回的地址只能保证是页面对齐的。

  2. The reserved/allocated region may be freed at once by a call to VirtualFree , and there's no need to pass the allocation size (that is, the size used in VirtualAlloc ). 可以通过调用VirtualFree来一次释放保留/分配的区域,无需传递分配大小(即VirtualAlloc使用的大小)。 In contrast munmap should be given the exact region size to be unmapped, ie it frees the given number of memory pages without any relation about how they were allocated. 相反, munmap应该被赋予要映射的确切区域大小,即,它释放给定数量的内存页面,而与它们的分配方式无关。

This imposes problems for me. 这给我带来了问题。 While I could live with (2), the (1) is a real problem. 虽然我可以接受(2),但(1)是一个真正的问题。 I don't want to get into details, but assuming the much smaller allocation granularity, such as 4K, will lead to a serious efficiency degradation. 我不想详细介绍,但是假设分配粒度小得多,例如4K,将导致严重的效率下降。 This is related to the fact that my code needs to put some information at every granularity boundary within the allocated regions, which impose "gaps" within the contiguous memory region. 这与以下事实有关:我的代码需要在分配的区域内的每个粒度边界处放置一些信息,这会在连续的内存区域内施加“间隙”。

I need to solve this. 我需要解决这个问题。 I can think about pretty naive methods of allocating increased regions, so that they can be 64K-aligned and still have adequate size. 我可以考虑分配增加区域的非常幼稚的方法,以便它们可以64K对齐,并且仍然具有足够的大小。 Or alternatively reserve huge regions of virtual address space, and then allocating properly-aligned memory regions (ie implement a sort of an aligned heap). 或者,保留虚拟地址空间的巨大区域,然后分配适当对齐的内存区域(即,实现某种对齐的堆)。 But I wonder if there are alternatives. 但我想知道是否还有其他选择。 Such as special APIs, maybe some flags, secret system calls or whatever. 例如特殊的API,也许有一些标志,秘密的系统调用等等。

(1) is actually quite easy to solve. (1)实际上很容易解决。 As you note, munmap takes a size parameter, and this is because munmap is capable of partial deallocation. 如您所述, munmap具有size参数,这是因为munmap可以部分释放。 You can thus allocate a chunk of memory that is bigger than what you need, then deallocate the parts that aren't aligned. 因此,您可以分配比您需要的大的内存块,然后取消分配未对齐的部分。

If possible, use posix_memalign (which despite the name allocates rather than aligns memory); 如果可能,请使用posix_memalign (尽管名称已分配posix_memalign分配内存); this allows you to specify an alignment, and the memory allocated can be released using free . 这允许您指定对齐方式,并且可以使用free释放分配的内存。 You would need to check whether posix_memalign is provided by your target platform. 您将需要检查目标平台是否提供了posix_memalign

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM