flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > Windows > winapi, aligned allocator?

Author
Thread Post new topic Reply to topic
vivik



Joined: 29 Oct 2016
Posts: 174
winapi, aligned allocator?
Anybody have a code for 16-byte (or even better if I can pass required alignment as one of parameters) allocator? Preferrably in pure winapi.
Post 28 Jun 2017, 16:02
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 15168
Location: GW170817
Something like this:

Code:
proc aligned_allocator size,alignment
        mov     edx,[alignment]
        mov     ecx,[size]
        lea     eax,[edx+ecx-1]         ;allocate size + (alignment - 1) bytes
        invoke  winapi_allocation_function,eax,...
        test    eax,eax
        jz      .failed
        mov     edx,[alignment]
        dec     edx
        lea     ecx,[eax+edx]
        not     edx
        and     ecx,edx                 ;return aligned address of allocation in ecx
        ret                             ;return base address of memory block in eax
    .failed:
        ;...
        ret
endp

But be careful when freeing the memory block to use the base address and not the aligned address.
Post 28 Jun 2017, 16:57
View user's profile Send private message Visit poster's website Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 824
VirtualAlloc allocates in chunks of 4k bytes (pages), it may be overkill or not, up to your use case. (alignment is guaranteed to be 4096 bytes obviously)
Post 29 Jun 2017, 12:08
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 15168
Location: GW170817
VirtualAlloc is a little it more complicated than a simple 4kB alignment though.

The page size is not actually guaranteed to be 4kB. While is it probably true that every existing and previous x86/x64 Windows version does use 4kB, it is still prudent to check the actual page size in use by calling GetSystemInfo. Future proofing and all that.
Post 29 Jun 2017, 12:20
View user's profile Send private message Visit poster's website Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 824
But I doubt they'd use smaller pages which aren't even supported by the CPU (likely they'd be larger), which still means 4096 alignment is guaranteed, just not more. Anyway, depending on his use case, it's just an alternative. If he does a lot of allocations then it's overkill and will be inefficient. Still something to keep in mind.
Post 29 Jun 2017, 13:52
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 15168
Location: GW170817
When you mention "doubt" then you should already start to take measures to fix that - Call GetSystemInfo. Then no more doubt needed. It is easy to do. Why assume when you can know?
Post 29 Jun 2017, 14:22
View user's profile Send private message Visit poster's website Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 174
Hoped for an allocator that works on top of VirtualAlloc, maybe somebody made something like that already.

About future proofing, I'll probably just release source code.
Post 30 Jun 2017, 14:32
View user's profile Send private message Reply with quote
alexfru



Joined: 23 Mar 2014
Posts: 32
Look up HeapAlloc(). It isn't as wasteful as VirtualAlloc(). AFAIK, it returns 8-byte aligned addresses.
Post 20 Jul 2017, 06:32
View user's profile Send private message Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 174
SSE requires 16-byte alignment though.

I heard it's actually 16-byte aligned on 64-bit windows, but I'm targeting 32-bit windows for now.
Post 20 Jul 2017, 12:12
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >

Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2005 phpBB Group.

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2016, Tomasz Grysztar.