Feature #17356
open
Alignment of memory allocated through Fiddle struct's malloc
Added by AndreaRibuoli (Andrea Ribuoli) almost 4 years ago.
Updated almost 4 years ago.
Description
I am testing a low-level patch for Ruby 3 that inside gc.c replaces:
TRY_WITH_GC(size, mem = malloc(size));
with:
TRY_WITH_GC(size, mem = aligned_alloc(16,size));
This should allow me to control that Fiddle returns pointers quad-words aligned.
Is it possible to introduce a configure parameter to control this kind of setting?
Refer to issue opened on Fiddle.
Kind regards,
Andrea Ribuoli
You mean the configure
script we ship along with ruby? I don't think it's a good idea to a add parameter there (too much overhead for everything allocated). Instead we could possibly implement a variant of posix_memalign
that interacts with our GC. Would that help you?
Right now the change I have introduced manually is fine for me. I was wondering if there was a simple way to #ifdef the change in the macro invocation in gc.c
so that I can re-introduce it easily in future releases by setting a define. Obviously this make sense if there is someone else that could benefit from using it!
The value to be defined while compiling could be used to determine the alignment.
Rather than the actual alignment the value to be set could be the exponent k, so that alignment is defined as 2^k.
The support of a mechanism consistent with GC sounds complex. Should you decide to introduce it I will obviously be available for testing in my environment.
Thanks for your kindness, Andrea
Sadly that has portability issues. It is not always possible to pass a pointer returned from aligned_alloc to realloc. You are lucky to have an environment with that capability. But we can’t always replace malloc with aligned_alloc.
IMHO, this feature is something that Fiddle or FFI should handle.
They can call malloc/posix_memalign/etc themselves of course.
I don't see a need to change anything so fundamental in the GC.
Fiddle/FFI can use finalizers if automatically freeing the memory is desired.
How about introducing new APIs like ruby_xaligned_malloc()
and ruby_xaligned_free()
that wraps existing rb_aligned_malloc()
and rb_aligned_free()
with objspace_*()
and TRY_WITH_GC()
like objspace_xmalloc0()
and objspace_xfree0()
?
If Fiddle uses posix_memalign()
directly, Fiddle needs to implement TRY_WITH_GC()
like code in Fiddle. And Fiddle also implements portable posix_memalign()
function like rb_aligned_malloc()
. We can implement all of them in Fiddle. But Ruby already has some pieces of them. If Ruby provides new APIs for aligned malloc, Fiddle can just use them instead of implementing them in Fiddle.
My proposal I wrote in #note-1 is basically the same as @kou’s in #note-5. I think it’s quite possible to add such things.
FYI.
I was informed by IBM that IBM i PASE malloc always returns 16-byte aligned memory!
This differs from AIX (that is not my target).
So my patch seems to be
- redundant for my environment and
- unsafe in other platforms.
Nonetheless addressing this topic in general could be useful elsewhere.
I have also found this answer very clear.
Thanks for caring, Andrea
Also available in: Atom
PDF
Like0
Like0Like0Like0Like0Like0Like0Like0