#Allocators and data alignment
1 messages · Page 1 of 1 (latest)
oh nice!
actually I have no idea what the documentation is smoking, unless there was just a change to how alignedAlloc works
the signature should be pub fn alignedAlloc(self: Allocator, comptime T: type, comptime alignment: ?u29, n: usize)
yeah, looks to just be a bug with autodoc
Reopening it to say - is there any way to use it with runtime known alignment?
Just align to the max it could be
Hmm, it returns an aligned slice, and in align(x) x has to be known at comptime, but it seems like a pretty useless feature
It's a very useful feature for optimizations
I was thinking that I could compare it to the comptime alignment, and throw an error if it's bigger
Where do you get the alignment from?
from a dll, which has a global with an @alignOf of the struct
Well, unless it's using any vector types you can safely assume the alignment is <=8
If it is using vector types you can probably assume it's <=64
If you really want runtime alignment you can use rawAlloc but that's kinda an internal function and it requires some extra work to use
ideally, I wouldn't want to assume anything and have a smallest possible alignment at runtime, but I'm going to do what you say
I also want to use ArrayListAligned, so that's probably out of question
maybe the align(x) feature could have runtime or indeterminate x
as from what I see, the only reason the alignment in alignedAlloc is comptime, is because it returns []align(alignment orelse @alignOf(T)) T
That becomes a bit weird because it means pointer types have part of the type being only runtime-known which also means pointers need extra associated data and stuff so it can't really work
so maybe I could construct the type of the ArrayListAligned at runtime somehow?
No, types cannot exist at runtime
The alignment here has to be comptime-known
Hence squirl's suggestion of just using the max alignment
oh right, but what about indeterminate alloc()?
That returns a byte slice which is by default align(1) so it makes no alignment guarantees
oh, i meant align(), sorry
wdym?
that the pointer would have absolutely no guarantees about alignment at comptime
That's just align(1)
(since any pointer is clearly 1 byte aligned, i.e. every address is a multiple of 1)
right
then there could be an allocRuntimeAligned function which returns a slice with align(1), but is really the runtime alignment
(or align(T) I guess)
This is your only real option for runtime-known alignment atm. Aligning to the max is better for optimization and will at worst use a few bytes more memory so I heavily encourage doing that
just asking if it would make sense, but yeah, I will just use 8 alignment and for extra safety check if the @alignOf from the dll is higher or lower