The MASM Forum

General => The Campus => Topic started by: nidud on June 28, 2014, 07:12:42 AM

Title: GlobalAlloc, HeapAlloc, and LocalAlloc
Post by: nidud on June 28, 2014, 07:12:42 AM
From Wayback Machine, this thread from before nidud's mass deletion of all of his posts
 niduds deleted posts from this thread (https://web.archive.org/web/20140821094624/https://masm32.com/board/index.php?topic=3341.0)
Unfortunately the zip files were not archived (by Wayback Machine) and will not work.
Title: Re: GlobalAlloc, HeapAlloc, and LocalAlloc
Post by: dedndave on June 28, 2014, 08:14:31 AM
XP MCE2005 (pro) SP3, prescott w/htt
Intel(R) Pentium(R) 4 CPU 3.00GHz (SSE3)
------------------------------------------------------
475699  cycles - LocalAlloc
477340  cycles - LocalAlloc
477033  cycles - LocalAlloc

14662124        cycles - HeapAlloc
14741803        cycles - HeapAlloc
14678539        cycles - HeapAlloc

15614816        cycles - GlobalAlloc
15559706        cycles - GlobalAlloc
15559654        cycles - GlobalAlloc
Title: Re: GlobalAlloc, HeapAlloc, and LocalAlloc
Post by: jj2007 on June 28, 2014, 08:46:12 AM
Interesting :t
So the question is, why do the others need so many cycles?
BTW the thread title is misleading; LocalAlloc is a Win32 function, you are creating something different...
Title: Re: GlobalAlloc, HeapAlloc, and LocalAlloc
Post by: dedndave on June 28, 2014, 08:54:18 AM
you don't have to use a linker option - you can probe the stack down
it doesn't take many cycles because you get 4 Kb per loop pass
and - in some cases, the stack has been previously probed down part of the way

by the way - it does no good to time it more than one call per process instance  :biggrin:
once the stack is probed down to some address - subsequent calls don't need to loop

;*****************************************************************************************************

        OPTION  PROLOGUE:None
        OPTION  EPILOGUE:None

ProbeStack PROC    uSize:UINT

;Probe Stack to Bytes Requested
;Copyright Dedndave 6-2012
;
;  Returns: EAX = probed-to stack pointer
;           ECX = bytes requested
;           EDX = top of stack

;---------------------------------------------------

        ASSUME  FS:Nothing

        mov     eax,esp
        mov     ecx,[esp+4]
        add     eax,8
        push    ebp
        sub     eax,ecx
        mov     ebp,esp
        and     al,-16                    ;must be 4-aligned, 16-aligned optional (shown)

Probe0: push    ecx
        mov     esp,fs:[8]
        cmp     eax,esp
        jb      Probe0

        mov     edx,esp
        leave
        ret     4

        ASSUME  FS:ERROR

ProbeStack ENDP

        OPTION  PROLOGUE:PrologueDef
        OPTION  EPILOGUE:EpilogueDef

;*****************************************************************************************************


that's an older version - don't know if i have an updated one   :P
Title: Re: GlobalAlloc, HeapAlloc, and LocalAlloc
Post by: nidud on June 28, 2014, 09:03:03 AM
deleted
Title: Re: GlobalAlloc, HeapAlloc, and LocalAlloc
Post by: hutch-- on June 28, 2014, 10:45:16 AM
Different strategies do different things, a high count of small allocations is always better done by allocating a single block and allocating pointers to parts of it as it is always a lot faster. It has to do with how the system allocates blocks of memory and what is the minimum size when a small block is allocated. For variable length strings the system has OLE strings which are part of the OLE string pool, if you need large blocks that can be swapped out to virtual memory, VirtualAlloc() is the right technique, most of the others interface with the same lower level function to provide memory so its more a choice of interface than performance.

LOCAL and GLOBAL Alloc are the same function as LocalAlloc() is a compatibility left over from Win16, GlobalAlloc() is required for a few old functions, the clipboard is one of them but it is only useful as a general purpose allocation strategy if you use the GMEM_FIXED flag and forget about GlobalLock() as it has assumptions from the Win16 days in its design.
Title: Re: GlobalAlloc, HeapAlloc, and LocalAlloc
Post by: Gunther on June 28, 2014, 06:53:19 PM
Hi nidud,


Intel(R) Core(TM) i7-3770 CPU @ 3.40GHz (SSE4)
------------------------------------------------------
1824805 cycles - GlobalAlloc
1872796 cycles - GlobalAlloc
1863379 cycles - GlobalAlloc

1483367 cycles - HeapAlloc
1558823 cycles - HeapAlloc
1545215 cycles - HeapAlloc

227101  cycles - LocalAlloc
228995  cycles - LocalAlloc
227467  cycles - LocalAlloc


Gunther
Title: Re: GlobalAlloc, HeapAlloc, and LocalAlloc
Post by: nidud on June 28, 2014, 11:45:48 PM
deleted
Title: Re: GlobalAlloc, HeapAlloc, and LocalAlloc
Post by: RuiLoureiro on June 29, 2014, 12:49:29 AM
nidud,
           something seems to be wrong, it doesnt work.
           
Title: Re: GlobalAlloc, HeapAlloc, and LocalAlloc
Post by: nidud on June 29, 2014, 01:14:02 AM
deleted
Title: Re: GlobalAlloc, HeapAlloc, and LocalAlloc
Post by: jj2007 on June 29, 2014, 01:16:26 AM
It works (XP SP3):
memory: 67108840
memory: 66708832
memory: 40308832
Title: Re: GlobalAlloc, HeapAlloc, and LocalAlloc
Post by: dedndave on June 29, 2014, 02:55:32 AM
XP SP3 prescott w/htt
memory: 67108840
memory: 66708832
memory: 40308832


i'm confused   :biggrin:
Title: Re: GlobalAlloc, HeapAlloc, and LocalAlloc
Post by: nidud on June 30, 2014, 11:36:30 PM
deleted
Title: Re: GlobalAlloc, HeapAlloc, and LocalAlloc
Post by: dedndave on July 01, 2014, 12:03:43 AM
i know - the registers should be preserved before EBP is set, not after
that's because of the way LEAVE works on exit
i often write my own stack frame to get what i want...

http://masm32.com/board/index.php?topic=3326.msg35052#msg35052 (http://masm32.com/board/index.php?topic=3326.msg35052#msg35052)
Title: Re: GlobalAlloc, HeapAlloc, and LocalAlloc
Post by: nidud on July 01, 2014, 02:17:46 AM
deleted
Title: Re: GlobalAlloc, HeapAlloc, and LocalAlloc
Post by: dedndave on July 01, 2014, 03:32:22 AM
there might be a way to write your own Prologue and Epilogue to do it
maybe even add the stack probe thing to make it crash-proof   :biggrin:
Title: Re: GlobalAlloc, HeapAlloc, and LocalAlloc
Post by: nidud on July 01, 2014, 08:27:31 AM
deleted