News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

GetSystemTime \ HeapRealloc \ Bitmap size

Started by TNick, November 10, 2006, 06:00:45 PM

Previous topic - Next topic

TNick

I was thinking about using this code to ask you guys to run it on diffrent machines to see if the pointer is valid after HeapRealloc fails. But, when I try to test this on my machine, it doesn't came to an end. Realloc just take longer, but doesn't fail. I've reallocated about 0x0E000000 bytes.
The idea was that, if HeapRealloc fail and the pointer is invalid, when you try to write to that memory, windows will prompt "app.. has encoured a problem ...". As I said, it just doesn't fail.
I will try this on another machine, but iI think HeapRealloc wins 10 points this time.

Quote
.586
.MODEL FLAT,STDCALL
OPTION CASEMAP:NONE

;
;                  S T A N D A R D    I N C L U D E S
;
   INCLUDE windows.inc
   INCLUDE kernel32.inc
   INCLUDE masm32.inc
   
;
;                     S T A N D A R D    L I B S
;
   INCLUDELIB   kernel32.lib
   INCLUDELIB   masm32.lib
   

.DATA

   hHeapTest      DWORD         0
   pMemoryTest   DWORD         0
   dimMemoryTest   DWORD         0
   
   
   StrDimMemory   BYTE         "At this moment, you have a block of (bytes): 0x"
   StrBuffer      BYTE         40h dup (0)
   
.CONST
   AppStartPoint   BYTE         13,10,"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",13,10
               BYTE         "               APP is running"
               BYTE         13,10,"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",0
   HCreateOk      BYTE         13,10,"The growable heap has been created",0
   pMemCreateOk   BYTE         13,10,"The initial amount of memory has been allocated",0
   AppReady      BYTE         13,10,"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",13,10
               BYTE         "               APP is about to end"
               BYTE         13,10,"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",0
   
.CODE
App_Entry_Point:
   INVOKE   OutputDebugString,   ADDR AppStartPoint
   ;create a growable heap
   INVOKE   HeapCreate,0,5000h,0          
   mov      hHeapTest,         eax
   or      eax,            eax
   jz      App_Ready_To_Exit
   INVOKE   OutputDebugString,   ADDR HCreateOk
   ;alloc initial memory
   INVOKE   HeapAlloc,   hHeapTest,0,1000h   
   mov      pMemoryTest,      eax
   or      eax,            eax
   jz      App_Ready_To_Exit
   INVOKE   OutputDebugString,   ADDR pMemCreateOk
   ;output initial size
   INVOKE   HeapSize,         hHeapTest,0,pMemoryTest
   cmp      eax,            0FFFFFFFFh
   je      App_Ready_To_Exit
   mov      dimMemoryTest,      eax
   INVOKE   dw2hex,            eax,      ADDR StrBuffer
   INVOKE   OutputDebugString,   ADDR StrDimMemory
   
   Loop_Entry:
      ;increase our memory size by 100000h
      mov      eax,            dimMemoryTest
      add      eax,            100000h
      INVOKE   HeapReAlloc,      hHeapTest,0,pMemoryTest,eax
      or      eax,            eax
      jz      Loop_Exit
      mov      pMemoryTest,      eax
      
      INVOKE   HeapSize,         hHeapTest,0,pMemoryTest
      cmp      eax,            0FFFFFFFFh
      je      Loop_Exit
      mov      dimMemoryTest,      eax
      INVOKE   dw2hex,            eax,      ADDR StrBuffer
      INVOKE   OutputDebugString,   ADDR StrDimMemory
      
      jmp      Loop_Entry
      
   Loop_Exit:
   
   ;at this point, HeapReAlloc or HeapSize had failed
   ;let's test if our pointer is ok
   mov      edx,      pMemoryTest
   xor      ecx,      ecx
   mov      eax,      dimMemoryTest
   shr      eax,      2 ;just to make shure that
   shl      eax,      2 ;dimMemoryTest is divisible by 4
   .WHILE   ecx<eax
      ;if this location is not valid, you willsee a message
      ;app encoured a problem and needs to close down ...
      mov      dword ptr [edx+ecx],00FF00FFh
      add      ecx,      4
   .ENDW
   
   
   
App_Ready_To_Exit:
   INVOKE   OutputDebugString,   ADDR AppReady
   .IF      pMemoryTest!=0
      INVOKE   HeapFree,      hHeapTest,0,pMemoryTest
   .ENDIF
   .IF      hHeapTest!=0
      INVOKE   HeapDestroy,      hHeapTest
   .ENDIF
   INVOKE   ExitProcess,   NULL
END App_Entry_Point

u

TNick: tested that code, and I manually stopped the process after it had allocated 2x210MB (total taken: 420MB). win2kSP4 512MB RAM. The swapfile would make it take all day to reach the inherent limit of HeapAlloc :) .

HeapRealloc has never failed me so far... so in my code I usually bluntly presume heapalloc/free/realloc are always successful. But I don't need lots of RAM (256MB+) in my code, usually.
Please use a smaller graphic in your signature.

TNick

Thanks for testing, Ultrano!
Quote from: Ultrano on November 13, 2006, 07:37:33 PM
HeapRealloc has never failed me so far...
Yes, same thing with my code, it never failed. But Manos seem to be convinced that it do fail. I will test this back at my home, too.

Regards,
Nick