ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:101153] [Ruby master Feature#17356] Alignment of memory allocated through Fiddle struct's malloc
@ 2020-11-30  8:25 andrea.ribuoli
  2020-12-01  0:29 ` [ruby-core:101169] " shyouhei
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: andrea.ribuoli @ 2020-11-30  8:25 UTC (permalink / raw
  To: ruby-core

Issue #17356 has been reported by AndreaRibuoli (Andrea Ribuoli).

----------------------------------------
Feature #17356: Alignment of memory allocated through Fiddle struct's malloc
https://bugs.ruby-lang.org/issues/17356

* Author: AndreaRibuoli (Andrea Ribuoli)
* Status: Open
* Priority: Normal
----------------------------------------
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](https://github.com/ruby/fiddle/issues/53).

Kind regards,

   Andrea Ribuoli



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:101169] [Ruby master Feature#17356] Alignment of memory allocated through Fiddle struct's malloc
  2020-11-30  8:25 [ruby-core:101153] [Ruby master Feature#17356] Alignment of memory allocated through Fiddle struct's malloc andrea.ribuoli
@ 2020-12-01  0:29 ` shyouhei
  2020-12-01  6:32 ` [ruby-core:101173] " andrea.ribuoli
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: shyouhei @ 2020-12-01  0:29 UTC (permalink / raw
  To: ruby-core

Issue #17356 has been updated by shyouhei (Shyouhei Urabe).


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?

----------------------------------------
Feature #17356: Alignment of memory allocated through Fiddle struct's malloc
https://bugs.ruby-lang.org/issues/17356#change-88855

* Author: AndreaRibuoli (Andrea Ribuoli)
* Status: Open
* Priority: Normal
----------------------------------------
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](https://github.com/ruby/fiddle/issues/53).

Kind regards,

   Andrea Ribuoli



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:101173] [Ruby master Feature#17356] Alignment of memory allocated through Fiddle struct's malloc
  2020-11-30  8:25 [ruby-core:101153] [Ruby master Feature#17356] Alignment of memory allocated through Fiddle struct's malloc andrea.ribuoli
  2020-12-01  0:29 ` [ruby-core:101169] " shyouhei
@ 2020-12-01  6:32 ` andrea.ribuoli
  2020-12-01 12:45 ` [ruby-core:101177] " shyouhei
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: andrea.ribuoli @ 2020-12-01  6:32 UTC (permalink / raw
  To: ruby-core

Issue #17356 has been updated by AndreaRibuoli (Andrea Ribuoli).


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

----------------------------------------
Feature #17356: Alignment of memory allocated through Fiddle struct's malloc
https://bugs.ruby-lang.org/issues/17356#change-88859

* Author: AndreaRibuoli (Andrea Ribuoli)
* Status: Open
* Priority: Normal
----------------------------------------
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](https://github.com/ruby/fiddle/issues/53).

Kind regards,

   Andrea Ribuoli



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:101177] [Ruby master Feature#17356] Alignment of memory allocated through Fiddle struct's malloc
  2020-11-30  8:25 [ruby-core:101153] [Ruby master Feature#17356] Alignment of memory allocated through Fiddle struct's malloc andrea.ribuoli
  2020-12-01  0:29 ` [ruby-core:101169] " shyouhei
  2020-12-01  6:32 ` [ruby-core:101173] " andrea.ribuoli
@ 2020-12-01 12:45 ` shyouhei
  2020-12-01 17:46 ` [ruby-core:101186] " eregontp
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: shyouhei @ 2020-12-01 12:45 UTC (permalink / raw
  To: ruby-core

Issue #17356 has been updated by shyouhei (Shyouhei Urabe).


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.

----------------------------------------
Feature #17356: Alignment of memory allocated through Fiddle struct's malloc
https://bugs.ruby-lang.org/issues/17356#change-88863

* Author: AndreaRibuoli (Andrea Ribuoli)
* Status: Open
* Priority: Normal
----------------------------------------
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](https://github.com/ruby/fiddle/issues/53).

Kind regards,

   Andrea Ribuoli



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:101186] [Ruby master Feature#17356] Alignment of memory allocated through Fiddle struct's malloc
  2020-11-30  8:25 [ruby-core:101153] [Ruby master Feature#17356] Alignment of memory allocated through Fiddle struct's malloc andrea.ribuoli
                   ` (2 preceding siblings ...)
  2020-12-01 12:45 ` [ruby-core:101177] " shyouhei
@ 2020-12-01 17:46 ` eregontp
  2020-12-01 20:11 ` [ruby-core:101193] " kou
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: eregontp @ 2020-12-01 17:46 UTC (permalink / raw
  To: ruby-core

Issue #17356 has been updated by Eregon (Benoit Daloze).


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.

----------------------------------------
Feature #17356: Alignment of memory allocated through Fiddle struct's malloc
https://bugs.ruby-lang.org/issues/17356#change-88867

* Author: AndreaRibuoli (Andrea Ribuoli)
* Status: Open
* Priority: Normal
----------------------------------------
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](https://github.com/ruby/fiddle/issues/53).

Kind regards,

   Andrea Ribuoli



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:101193] [Ruby master Feature#17356] Alignment of memory allocated through Fiddle struct's malloc
  2020-11-30  8:25 [ruby-core:101153] [Ruby master Feature#17356] Alignment of memory allocated through Fiddle struct's malloc andrea.ribuoli
                   ` (3 preceding siblings ...)
  2020-12-01 17:46 ` [ruby-core:101186] " eregontp
@ 2020-12-01 20:11 ` kou
  2020-12-01 22:28 ` [ruby-core:101197] " shyouhei
  2020-12-02 11:06 ` [ruby-core:101199] " andrea.ribuoli
  6 siblings, 0 replies; 8+ messages in thread
From: kou @ 2020-12-01 20:11 UTC (permalink / raw
  To: ruby-core

Issue #17356 has been updated by kou (Kouhei Sutou).


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.

----------------------------------------
Feature #17356: Alignment of memory allocated through Fiddle struct's malloc
https://bugs.ruby-lang.org/issues/17356#change-88874

* Author: AndreaRibuoli (Andrea Ribuoli)
* Status: Open
* Priority: Normal
----------------------------------------
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](https://github.com/ruby/fiddle/issues/53).

Kind regards,

   Andrea Ribuoli



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:101197] [Ruby master Feature#17356] Alignment of memory allocated through Fiddle struct's malloc
  2020-11-30  8:25 [ruby-core:101153] [Ruby master Feature#17356] Alignment of memory allocated through Fiddle struct's malloc andrea.ribuoli
                   ` (4 preceding siblings ...)
  2020-12-01 20:11 ` [ruby-core:101193] " kou
@ 2020-12-01 22:28 ` shyouhei
  2020-12-02 11:06 ` [ruby-core:101199] " andrea.ribuoli
  6 siblings, 0 replies; 8+ messages in thread
From: shyouhei @ 2020-12-01 22:28 UTC (permalink / raw
  To: ruby-core

Issue #17356 has been updated by shyouhei (Shyouhei Urabe).


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.


----------------------------------------
Feature #17356: Alignment of memory allocated through Fiddle struct's malloc
https://bugs.ruby-lang.org/issues/17356#change-88878

* Author: AndreaRibuoli (Andrea Ribuoli)
* Status: Open
* Priority: Normal
----------------------------------------
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](https://github.com/ruby/fiddle/issues/53).

Kind regards,

   Andrea Ribuoli



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:101199] [Ruby master Feature#17356] Alignment of memory allocated through Fiddle struct's malloc
  2020-11-30  8:25 [ruby-core:101153] [Ruby master Feature#17356] Alignment of memory allocated through Fiddle struct's malloc andrea.ribuoli
                   ` (5 preceding siblings ...)
  2020-12-01 22:28 ` [ruby-core:101197] " shyouhei
@ 2020-12-02 11:06 ` andrea.ribuoli
  6 siblings, 0 replies; 8+ messages in thread
From: andrea.ribuoli @ 2020-12-02 11:06 UTC (permalink / raw
  To: ruby-core

Issue #17356 has been updated by AndreaRibuoli (Andrea Ribuoli).


FYI. 
I was informed by IBM that IBM i PASE *malloc* always returns 16-bite 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](https://stackoverflow.com/questions/9078259/does-realloc-keep-the-memory-alignment-of-posix-memalign) very clear. 

Thanks for caring, Andrea 

----------------------------------------
Feature #17356: Alignment of memory allocated through Fiddle struct's malloc
https://bugs.ruby-lang.org/issues/17356#change-88882

* Author: AndreaRibuoli (Andrea Ribuoli)
* Status: Open
* Priority: Normal
----------------------------------------
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](https://github.com/ruby/fiddle/issues/53).

Kind regards,

   Andrea Ribuoli



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2020-12-02 11:06 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-11-30  8:25 [ruby-core:101153] [Ruby master Feature#17356] Alignment of memory allocated through Fiddle struct's malloc andrea.ribuoli
2020-12-01  0:29 ` [ruby-core:101169] " shyouhei
2020-12-01  6:32 ` [ruby-core:101173] " andrea.ribuoli
2020-12-01 12:45 ` [ruby-core:101177] " shyouhei
2020-12-01 17:46 ` [ruby-core:101186] " eregontp
2020-12-01 20:11 ` [ruby-core:101193] " kou
2020-12-01 22:28 ` [ruby-core:101197] " shyouhei
2020-12-02 11:06 ` [ruby-core:101199] " andrea.ribuoli

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).