flat assembler
Message board for the users of flat assembler.

Index > Windows > winapi, aligned allocator?

Author
Thread Post new topic Reply to topic
vivik



Joined: 29 Oct 2016
Posts: 671
vivik 28 Jun 2017, 16:02
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: 20520
Location: In your JS exploiting you and your system
revolution 28 Jun 2017, 16:57
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: 2596
Furs 29 Jun 2017, 12:08
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: 20520
Location: In your JS exploiting you and your system
revolution 29 Jun 2017, 12:20
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: 2596
Furs 29 Jun 2017, 13:52
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: 20520
Location: In your JS exploiting you and your system
revolution 29 Jun 2017, 14:22
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: 671
vivik 30 Jun 2017, 14:32
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: 80
alexfru 20 Jul 2017, 06: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: 671
vivik 20 Jul 2017, 12:12
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


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.