Note that there are some explanatory texts on larger screens.

plurals
  1. POContiguous VirtualAlloc behaviour on Windows Mobile
    text
    copied!<p>I have been optimising memory performance on a Windows Mobile application and have encountered some differences in behaviour between <strong>VirtualAlloc</strong> <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa366887.aspx" rel="nofollow noreferrer">on Win32</a> and <a href="http://msdn.microsoft.com/en-us/library/ee488564.aspx" rel="nofollow noreferrer">Windows CE</a>. </p> <p>Consider the following test:</p> <pre><code>// Allocate 64k of memory BYTE *a = (BYTE*)VirtualAlloc(0, 65536, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE); // Allocate a second contiguous 64k of memory BYTE *b = (BYTE*)VirtualAlloc(a+65536, 65536, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE); BYTE *c = a + 65528; // Set a pointer near the end of the first allocation BOOL valid1 = !IsBadWritePtr(c, 8); // Expect TRUE BOOL valid2 = !IsBadWritePtr(c+8, 4088); // Expect TRUE BOOL valid3 = !IsBadWritePtr(c, 4096); // TRUE on Win32, FALSE on WinCE </code></pre> <p>The code "allocates" 4096 of data starting at "c". On Win32 this works. I can find no mention in the <strong>VirtualAlloc</strong> documentation whether it is legal or coincidence but there are many examples of code that I have found via google that expect this behaviour.</p> <p>On Windows CE 5.0/5.2 if I use the memory block at "c", in 99% of cases there are no problems, however on some (not all) Windows Mobile 6 devices, <strong>ReadFile</strong> &amp; <strong>WriteFile</strong> will fail with error 87 (The parameter is incorrect.). I assume <strong>ReadFile</strong> is calling <strong>IsBadWritePtr</strong> or similar and return false due to this. If I perform two <strong>ReadFile</strong> calls then everything works fine. (There may of course be other API calls that will also fail.)</p> <p>I am looking for a way to extend the memory returned by <strong>VirtualAlloc</strong> so that I can make the above work. Reserving a large amount of memory on Windows CE is problematic as each process only gets 32MB and due to other items being loaded it is not possible to reserve a large region of memory without causing other problems. (It is possible to reserve a larger amount of memory in the shared region but this also has other problems.)</p> <p>Is there a way to get <strong>VirtualAlloc</strong> to enlarge or combine regions without reserving it up front?</p> <p>I suspect it may be problematic given the following examples:</p> <pre><code>HANDLE hHeap1 = HeapCreate(0, 0, 0); // Heap defaults to 192k BYTE * a1 = (BYTE*)HeapAlloc(hHeap1, 0, 64000); // +96 bytes from start of heap BYTE * b1 = (BYTE*)HeapAlloc(hHeap1, 0, 64000); // +16 bytes from end of a1 BYTE * c1 = (BYTE*)HeapAlloc(hHeap1, 0, 64000); // +16 bytes from end of b1 BYTE * d1 = (BYTE*)HeapAlloc(hHeap1, 0, 64000); // +4528 bytes from end of c1 HANDLE hHeap2 = HeapCreate(0, 4*1024*1024, 4*1024*1024); // 4MB Heap BYTE * a2 = (BYTE*)HeapAlloc(hHeap2, 0, 64000); // +96 bytes from start of heap BYTE * b2 = (BYTE*)HeapAlloc(hHeap2, 0, 64000); // +16 bytes from end of a2 BYTE * c2 = (BYTE*)HeapAlloc(hHeap2, 0, 64000); // +16 bytes from end of b2 BYTE * d2 = (BYTE*)HeapAlloc(hHeap2, 0, 64000); // +16 bytes from end of c2 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload