The MASM Forum Archive 2004 to 2012

General Forums => The Workshop => Topic started by: TNick on November 10, 2006, 06:00:45 PM

Title: GetSystemTime \ HeapRealloc \ Bitmap size
Post by: TNick on November 10, 2006, 06:00:45 PM
Hi all!

I would like to know if there is a way of getting the BIOS time in Windows, because GetSystemTime and GetLocalTime return the time selected by user in <Date and Time Properties>. I need this to track some events.

Second, just want to be shure that, if a call to HeapRealloc fail, the pointer passed to it is still valid. Do you know something about this?

And, finally: if I select a bitmap in a memory DC, can I resize it, or I must select another bitmap, resize my bitmap and reselect it in DC?

Thanks for help in advance!
Nick
Title: Re: GetSystemTime \ HeapRealloc \ Bitmap size
Post by: Manos on November 10, 2006, 07:37:19 PM
About HeapRealloc,I think that in WinXP it fails some times,
mainly if you call this repeatedly.
I don't know why,but I have bad experience using this.

About bitmap,you must select another bitmap in the DC.

Manos.
Title: Re: GetSystemTime \ HeapRealloc \ Bitmap size
Post by: TNick on November 10, 2006, 07:40:23 PM
No, on my system works ok. Just want to know if that pointer that I get using a HeapAlloc is still valid
Title: Re: GetSystemTime \ HeapRealloc \ Bitmap size
Post by: Manos on November 10, 2006, 07:46:50 PM
The old pointer is invalid.

Manos.
Title: Re: GetSystemTime \ HeapRealloc \ Bitmap size
Post by: TNick on November 10, 2006, 07:57:14 PM
Oh,I understand now. Not very good behavior. So, if it fails, you can't free that block of memory.
Thanks for your answer!

Nick
Title: Re: GetSystemTime \ HeapRealloc \ Bitmap size
Post by: Relvinian on November 10, 2006, 08:36:35 PM
TNick,

This is from the SDK for HeapReAlloc:

If HeapReAlloc succeeds, it allocates at least the amount of memory requested. To determine the actual size of the reallocated block, use the HeapSize function.

If HeapReAlloc fails, the original memory is not freed, and the original handle and pointer are still valid.

Relvinian
Title: Re: GetSystemTime \ HeapRealloc \ Bitmap size
Post by: Manos on November 10, 2006, 09:22:59 PM
About HeapReAlloc have a look here (http://www.masm32.com/board/index.php?topic=1940.msg15652#msg15652).

Manos.
Title: Re: GetSystemTime \ HeapRealloc \ Bitmap size
Post by: TNick on November 11, 2006, 04:10:39 PM
Manos,

I see this is a real problem. Anyway, even if I'm not shure, I don't think the way proposed in that topic is a safe one. You can't be shure that the new allocated portion will be right there. I guess that I'll have to change all my HeapRealloc to HeapAlloc - copy - HeapFree.

Thanks for bitmap related info.

What about time. Is there a way to find the BIOS time?

Regards,
Nick
Title: Re: GetSystemTime \ HeapRealloc \ Bitmap size
Post by: ToutEnMasm on November 11, 2006, 06:04:54 PM

I use heapalloc and then Heaprealloc in an intensive maner,in XP and have never seen one problem.
     ToutEnMasm
Title: Re: GetSystemTime \ HeapRealloc \ Bitmap size
Post by: Manos on November 11, 2006, 06:06:07 PM
Quote from: TNick on November 11, 2006, 04:10:39 PM
I guess that I'll have to change all my HeapRealloc to HeapAlloc - copy - HeapFree.

Yes.
The same I have done in my projects.You can implement your own HeapReAlloc.
Inside your HeapReAlloc,you can check the returned pointer from the new HeapAlloc
and if this is 0,you can preserve the old pointer.

Manos.
Title: Re: GetSystemTime \ HeapRealloc \ Bitmap size
Post by: ToutEnMasm on November 11, 2006, 06:13:15 PM
a sample is better

http://www.masm32.com/board/index.php?topic=2253.msg17776#msg17776
Title: Re: GetSystemTime \ HeapRealloc \ Bitmap size
Post by: TNick on November 11, 2006, 06:55:03 PM
Hello, ToutEnMasm!

I used HeapRealloc a lot, too, and had no problems! This question had pop up when thinking about where to test a failure of HeapRealloc:

INVOKE  HeapRealloc,hHeap,0,pMem,NewSize
or eax, eax
jz ErrorLabel
mov  pMem, eax


or

mov  pMem, eax
or eax, eax
jz ErrorLabel


Anyway, reading that topic that Manos said, seem like some people had serious problems with it. I will look at that code a little more, but if I can't find another cause, I would say that safest thing to do is <HeapAlloc - copy - HeapFree.>. Even if there can be a speed decrease...

Regards,
Nick



Title: Re: GetSystemTime \ HeapRealloc \ Bitmap size
Post by: TNick on November 11, 2006, 07:23:06 PM
Ok, reading is done. In that post, if I understand correctly, they said that, eventually, HeapRealloc will fail. This is OK. I just want to know if the pointer used in this function will still be valid, and my bytes intact. There is no reference about this in that post.
Let's think it over.

IF requested size aviable in place
        just increase the allowed size
        mov  eax, OldPointer
ELSE
        find a place with enough space
        IF There_is_a_place
            xor ecx, ecx
            .WHILE ecx<oldsize
               .
               .    ;copy old content
               .
            .ENDW
            mov  eax, NewPointer
        ELSE
          xor eax, eax
        .ENDIF
.ENDIF

Well, if this is correct, there is no place where our pointer or content may be affected....

Am I missing something here?

Manos, a snippet, maybe?


Nick
Title: Re: GetSystemTime \ HeapRealloc \ Bitmap size
Post by: Manos on November 11, 2006, 09:29:54 PM
A snippet you will find here (http://www.manoscoder.gr/mbbs/forums/thread-view.asp?tid=56&posts=1#M139).

Manos.
Title: Re: GetSystemTime \ HeapRealloc \ Bitmap size
Post by: Tedd on November 11, 2006, 10:36:56 PM
testing seems to show that the old pointer is valid if re-alloc fails..
(though obviously this only calls once -- calling repeatedly could - but shouldn't! - have other side effects.)


.586
.model flat, stdcall
option casemap:none
include windows.inc
include kernel32.inc
includelib kernel32.lib
include user32.inc
includelib user32.lib

.data
before      db "before",0
succs       db "succeeded",0
failed      db "failed",0
strung      db "this is a debugging string",0
sizer       db "%lu bytes",0

.data?
hHeap       HANDLE  ?
numbuff     db 128 dup (?)

.code
start:
    push esi
    invoke GetProcessHeap
    mov hHeap,eax

    invoke HeapAlloc, hHeap,NULL,64     ;allocate a small block - 64 bytes
    mov esi,eax
    invoke lstrcpy, esi,ADDR strung     ;store some data into it
    invoke MessageBox, NULL,esi,ADDR before,MB_OK   ;show the data is there

    invoke HeapReAlloc, hHeap,NULL,esi,07fffffffh    ;request a huge chunk - it should fail ;)
    .IF (eax)
        ;***succeeded -- we get a new pointer to the new memory
        mov esi,eax
        invoke HeapSize, hHeap,NULL,esi
        invoke wsprintf, ADDR numbuff,ADDR sizer,eax
        invoke MessageBox, NULL,esi,ADDR numbuff,MB_OK
    .ELSE
        ;***failed -- the 'old' pointer is still valid
        invoke HeapSize, hHeap,NULL,esi
        invoke wsprintf, ADDR numbuff,ADDR sizer,eax
        invoke MessageBox, NULL,esi,ADDR numbuff,MB_OK
    .ENDIF
    invoke HeapFree, hHeap,NULL,esi
    pop esi
    invoke ExitProcess, NULL
end start
Title: Re: GetSystemTime \ HeapRealloc \ Bitmap size
Post by: TNick on November 13, 2006, 07:02:46 PM
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
Title: Re: GetSystemTime \ HeapRealloc \ Bitmap size
Post by: u on November 13, 2006, 07:37:33 PM
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.
Title: Re: GetSystemTime \ HeapRealloc \ Bitmap size
Post by: TNick on November 13, 2006, 07:45:06 PM
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