News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Heap requested = HeapSize??

Started by jj2007, June 03, 2013, 10:28:09 PM

Previous topic - Next topic

jj2007

I vaguely remember that HeapAlloc gives you more memory than requested, and that you can check the real availability with HeapSize. But they seem to be identical, at least on Win7. Where is my error?

Raymond Chen has the answer (16 Mar 2012, so that probably means Win7...):
QuoteBonus chatter: It appears that at some point, the kernel folks decided that these "bonus bytes" were more hassle than they were worth, and now they spend extra effort remembering not only the actual size of the memory block but also the requested size. When you ask, "How big is this memory block?" they lie and return the requested size rather than the actual size. In other words, the free bonus bytes are no longer exposed to applications by the kernel heap functions.
:icon_eek:

include \masm32\include\masm32rt.inc
deb Macro dummy, txt, var
  if usedeb
   pushad
   print str$(var), 9, &txt, 13, 10
   popad
  endif
ENDM

.code
start:
   m2m ebx, 1
   mov edi, rv(GetProcessHeap)
   usedeb=0
   print "request", 9, "received", 13, 10
   .Repeat
      inc ebx
      invoke HeapAlloc,  edi, HEAP_GENERATE_EXCEPTIONS, ebx
      deb 20, "Alloc", eax
      xchg eax, esi
      invoke HeapSize, edi, 0, esi
      xchg eax, ecx
      deb 20, "Size", ecx
      print str$(ecx), 9
      print str$(ebx), 13, 10
      invoke HeapFree, edi, 0, esi
      deb 20, "Free", eax
      dec ebx
      add ebx, ebx
   .Until ebx>65536
   inkey " "
   exit 0
end start

dedndave

it may be something that MS changed along the way
... that it returns the requested size from a table, rather than the actual allocation
in theory, making that change shouldn't break any code
and, it prevents the issue mentioned in the article

it may have been changed in xp sp2 or sp3, for example
then, MS doesn't tell anyone - lol

jj2007

Quote from: dedndave on June 04, 2013, 03:03:37 AM
in theory, making that change shouldn't break any code

There could be scenarios, though. Such as "new" code relying on the correct size and running under XP SP2 instead of SP3.

dedndave

wouldn't that be "forward compatibility" ?   :P

maybe that's why they don't tell us about it - lol

at any rate, i ran the test on xp sp3, and HeapSize returns the same as requested
i wonder if the issue mentioned in that article was affected by the change
(or the change was affected by the article)
i.e., can you reallocate with HEAP_ZERO_MEMORY and get zero'ed bytes

Gunther

Jochen,

heap test results:

request received
2       2
3       3
5       5
9       9
17      17
33      33
65      65
129     129
257     257
513     513
1025    1025
2049    2049
4097    4097
8193    8193
16385   16385
32769   32769
65537   65537


Gunther
You have to know the facts before you can distort them.

FORTRANS

Hi,

   A sample of my systems.  Do you have the request / received
reversed?

Windows 2000

request   received
2   2
3   3
5   5
9   9
17   17
33   33
65   65
129   129
257   257
513   513
1025   1025
2049   2049
4097   4097
8193   8193
16385   16385
32769   32769
65537   65537

Windows 98

request   received
12   2
12   3
12   5
12   9
20   17
36   33
68   65
132   129
260   257
516   513
1028   1025
2052   2049
4100   4097
8196   8193
16388   16385
32772   32769
65540   65537

Windows XP

request   received
2   2
3   3
5   5
9   9
17   17
33   33
65   65
129   129
257   257
513   513
1025   1025
2049   2049
4097   4097
8193   8193
16385   16385
32769   32769
65537   65537



HTH,

Steve N.

jj2007

Quote from: FORTRANS on June 04, 2013, 05:08:57 AM
Hi,

   A sample of my systems.  Do you have the request / received
reversed?

Oops, it seems so :redface:

So it seems Win98 was the last version that did not use the table trick...