我有一个用于Windows Mobile 6.x的Visual Studio 2008 C++应用程序,其中我在计算给定进程可用的可用虚拟内存量。 (我意识到这并没有考虑到碎片问题。)我的代码基本上是这样的:

MEMORY_BASIC_INFORMATION mbi = { 0 };

/// total free memory available to the process
DWORD free = 0;

/// base memory address for the given process index (2-33).
DWORD slot_base_addr = process_index * 0x02000000;

/// look at each memory region for the process.
for( DWORD offset = 0x10000;
     offset < 0x02000000;
     offset += mbi.RegionSize )
{
    ::VirtualQuery( ( void* )( slot_base_addr + offset ),
                    &mbi,
                    sizeof( MEMORY_BASIC_INFORMATION ) );

    if( mbi.State == MEM_FREE )
    {
        free += ( mbi.RegionSize - ( ( ~( DWORD )mbi.BaseAddress + 1 ) & 0xffff ) ) & 0xffff0000;
    }
}
NKDbgPrintfW( L"%d bytes free\r\n", free );

我可以与其他API确认,这似乎可以很好地工作。我的问题是这条线在做什么:
free += ( mbi.RegionSize - ( ( ~( DWORD )mbi.BaseAddress + 1 ) & 0xffff ) ) & 0xffff0000;

为什么这不仅是:
free += mbi.RegionSize;

我在MSFT员工Ross Jordan的Usenet帖子中找到了前一行。

谢谢,
保罗·H

编辑:

例如。对于进程插槽2,这是每个可用内存块的列表,其中包含罗斯·乔丹(RS)算法和仅RegionSize(RS)给出的可用内存量。
Slot: 2. Range: 0x04000000 - 0x06000000
    RS:    16,384 bytes RJ:         0 bytes diff: 16384
    RS:     4,096 bytes RJ:         0 bytes diff: 4096
    RS:     4,096 bytes RJ:         0 bytes diff: 4096
    RS:     4,096 bytes RJ:         0 bytes diff: 4096
    RS:     4,096 bytes RJ:         0 bytes diff: 4096
    RS:     4,096 bytes RJ:         0 bytes diff: 4096
    RS:    36,864 bytes RJ:         0 bytes diff: 36864
    RS:    65,536 bytes RJ:    65,536 bytes diff: 0
    RS:    53,248 bytes RJ:         0 bytes diff: 53248
    RS:     4,096 bytes RJ:         0 bytes diff: 4096
    RS:     4,096 bytes RJ:         0 bytes diff: 4096
    RS:     4,096 bytes RJ:         0 bytes diff: 4096
    RS:     4,096 bytes RJ:         0 bytes diff: 4096
    RS:     4,096 bytes RJ:         0 bytes diff: 4096
    RS: 7,671,808 bytes RJ: 7,667,712 bytes diff: 4096
    RS: 1,921,024 bytes RJ: 1,900,544 bytes diff: 20480
    RS: 7,491,584 bytes RJ: 7,471,104 bytes diff: 20480
    RS: 3,252,224 bytes RJ: 3,211,264 bytes diff: 40960
    RS:   262,144 bytes RJ:   262,144 bytes diff: 0

RS: Total VM Free: 20,811,776 bytes.
RJ: Total VM Free: 20,578,304 bytes.

编辑2:

汉斯引导我找到答案。这只是执行此操作的一种理想方式,但是假设分配大小为64KB。
SYSTEM_INFO si = { 0 };
::GetSystemInfo( &si );

free += mbi.RegionSize - mbi.RegionSize % si.dwAllocationGranularity;

最佳答案

VirtualAlloc的分配粒度通常为64KB。如果AllocationBase不是64KB的倍数,他将尝试做一些有意义的事情。我认为这完全没有意义,他的位掩码仍然假定为64KB的粒度,并且他不使用SYSTEM_INFO.dwAllocationGranularity。里面有这个评论:



在不是64KB的极少数情况下,此代码将生成垃圾值。只需换一下,按RegionSize即可。

关于c++ - 解释计算自由虚拟内存的一行二进制补码数学,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4069686/

10-11 21:07