News:

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

LocalAlloc memory begin

Started by bomz, June 09, 2011, 02:47:17 PM

Previous topic - Next topic

bomz

Quoteinvoke LocalAlloc,LMEM_FIXED OR LMEM_ZEROINIT,N

memory always begin from multiple for 4 point?
as align 4

hutch--

There is no difference in 32 bit Windows from GlobalAlloc() using the GMEM_FIXED flag and the GMEM_ZEROINIT flag. In 32 bit windows there is no LOCAL alloc at all, its like many other memory strategies, its mapped to NTDLL internal functions.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

qWord

You can rely on that the memory returned by Global/LocalAlloc is to be aligned by 8.
FPU in a trice: SmplMath
It's that simple!

bomz

the function is not problem I need to be sure that memory allways /4 .
I try 10 times - always /4. but there is no about this in msdn

qWord

Quote from: bomz on June 09, 2011, 04:03:26 PMbut there is no about this in msdn
Quote from: msdn: GlobalAlloc FunctionMemory allocated with this function is guaranteed to be aligned on an 8-byte boundary.
GlobalAlloc()

So your on the 'save side' by using this function.
FPU in a trice: SmplMath
It's that simple!

jj2007

Here are some steps to produce align 16, in case you want to work with SSE2.

include \masm32\include\masm32rt.inc
.686
.xmm

.code
start:
mov ebx, alloc(123) ; produce misalignment ;-)
BytesNeeded=555
mov eax, BytesNeeded
add eax, 16
mov esi, alloc(eax)
print hex$(eax), 9, " result of first alloc", 13, 10
mov edi, esi ; keep that pointer for GlobalFree
add esi, 16 ; add the 16 extra bytes
and esi, -16 ; and align them downwards
movaps xmm0, [esi] ; if it's not aligned 16, you will see a GPF
print hex$(esi), 9, " aligned 16 for use with SSE2", 13, 10
nops 5
free edi
print str$(eax), 9, " result of free edi (0=success)", 13, 10
free ebx
inkey str$(eax), 9, " result of free ebx (0=success)", 13, 10
exit
end start

bomz

I think most easy correct begin point from memory begin to 4,8,16...
msdn says that located memory not allways the same size as need - may be greater. the begining is that how windows orginize all memory


qWord

Quote from: bomz on June 09, 2011, 05:59:19 PM
I think most easy correct begin point from memory begin to 4,8,16...
msdn says that located memory not allways the same size as need - may be greater. the begining is that how windows orginize all memory
What are you trying to say?
FPU in a trice: SmplMath
It's that simple!

dedndave

it doesn't seem to be documented anywhere
so - i align it as desired

1) request (N-1) more bytes than you need, where N = alignment
2) save the original address for the handle to Free the allocated block
3) use the N-aligned address for data
   D = (A+N-1) AND (-N)

A = allocated block address
D = aligned data address

bomz

my mathematic english very poor



I mean that windows organize memory using some rule and all part of memory for program, for data, for system, for .... begins from /4
all pages?

qWord

bomz,
when allocating memory, there seems to be no documented warranty that the memory is aligned(this what you mean begin/x) in any form. Only GlobalAlloc() guarantees this - it is documented!
(This does not apply to the Virtual memory functions.)
FPU in a trice: SmplMath
It's that simple!


qWord

follow the link and read the whole text!
FPU in a trice: SmplMath
It's that simple!

bomz

QuoteMemory allocated with this function is guaranteed to be aligned on an 8-byte boundary. To execute dynamically generated code, use the VirtualAlloc function to allocate memory and the VirtualProtect function to grant PAGE_EXECUTE access.

MichaelW

I thought I posted an earlier version of the code in the attachment somewhere here, but I can't seem to find it.
eschew obfuscation