carray 3.0.7 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (275) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -1
  3. data/CHANGELOG.md +7 -1940
  4. data/README.md +38 -32
  5. data/carray.gemspec +16 -4
  6. data/docs/changelogs/CHANGELOG-3.0.md +2464 -0
  7. data/ext/ca_array_pool.c +4 -5
  8. data/ext/ca_axis_descriptor.h +45 -30
  9. data/ext/ca_axis_dispatch.c +7 -4
  10. data/ext/ca_axis_group.c +6 -9
  11. data/ext/ca_bincmp_dispatch.h +16 -38
  12. data/ext/ca_binop_dispatch.c +27 -0
  13. data/ext/ca_binop_dispatch.h +22 -47
  14. data/ext/ca_categorical_iterator.c +36 -34
  15. data/ext/ca_compare.c +0 -1
  16. data/ext/ca_compare.h +8 -1
  17. data/ext/ca_composite_dispatch.h +6 -0
  18. data/ext/ca_for_buffer.h +6 -1
  19. data/ext/ca_group_iter.c +17 -18
  20. data/ext/ca_iter_state_fields.h +196 -0
  21. data/ext/ca_iter_substrate.h +84 -103
  22. data/ext/ca_kernel_iterator.c +125 -89
  23. data/ext/ca_kernel_iterator.h +229 -1506
  24. data/ext/ca_kernel_iterator_internal.h +1167 -0
  25. data/ext/ca_moncmp_dispatch.h +17 -24
  26. data/ext/ca_monop_dispatch.h +25 -74
  27. data/ext/ca_obj_array.c +90 -163
  28. data/ext/ca_obj_bincmp.c +47 -46
  29. data/ext/ca_obj_binop.c +38 -38
  30. data/ext/ca_obj_bitarray.c +5 -17
  31. data/ext/ca_obj_bitfield.c +15 -30
  32. data/ext/ca_obj_block.c +11 -12
  33. data/ext/ca_obj_byte_swap.c +32 -30
  34. data/ext/ca_obj_const_string.c +57 -62
  35. data/ext/ca_obj_face.c +99 -91
  36. data/ext/ca_obj_face.h +32 -170
  37. data/ext/ca_obj_fake.c +15 -21
  38. data/ext/ca_obj_farray.c +7 -9
  39. data/ext/ca_obj_field.c +17 -13
  40. data/ext/ca_obj_fixlen_string.c +8 -19
  41. data/ext/ca_obj_grid.c +137 -106
  42. data/ext/ca_obj_meld.c +23 -21
  43. data/ext/ca_obj_moncmp.c +10 -21
  44. data/ext/ca_obj_monop.c +24 -35
  45. data/ext/ca_obj_object.c +51 -37
  46. data/ext/ca_obj_record.c +74 -22
  47. data/ext/ca_obj_reduce.c +8 -10
  48. data/ext/ca_obj_refer.c +42 -29
  49. data/ext/ca_obj_remap.c +11 -29
  50. data/ext/ca_obj_repeat.c +41 -23
  51. data/ext/ca_obj_roll.c +18 -24
  52. data/ext/ca_obj_select.c +10 -22
  53. data/ext/ca_obj_select_axis.c +27 -36
  54. data/ext/ca_obj_shift.c +22 -42
  55. data/ext/ca_obj_source.c +1 -5
  56. data/ext/ca_obj_stack.c +28 -24
  57. data/ext/ca_obj_stride.c +53 -54
  58. data/ext/ca_obj_string.c +6 -18
  59. data/ext/ca_obj_tile.c +12 -12
  60. data/ext/ca_obj_time.c +12 -23
  61. data/ext/ca_obj_timedelta.c +12 -23
  62. data/ext/ca_obj_transpose.c +8 -10
  63. data/ext/ca_obj_triop.c +35 -39
  64. data/ext/ca_obj_window.c +67 -86
  65. data/ext/ca_op_cmplx64.h +4 -0
  66. data/ext/ca_op_ipower.c +34 -12
  67. data/ext/ca_op_powi.h +4 -0
  68. data/ext/ca_pow5_table.h +2 -1
  69. data/ext/ca_rng_normal.h +4 -0
  70. data/ext/ca_rng_xoshiro256pp.h +3 -0
  71. data/ext/ca_sort_kernels.h +20 -26
  72. data/ext/ca_sweep_engine.c +2 -3
  73. data/ext/ca_sweep_engine.h +17 -9
  74. data/ext/ca_transform_common.c +3 -8
  75. data/ext/ca_triop_dispatch.h +20 -32
  76. data/ext/caframe_csv_reader.c +11 -11
  77. data/ext/carray.h +300 -1011
  78. data/ext/carray_access.c +234 -180
  79. data/ext/carray_address_basis.c +33 -38
  80. data/ext/carray_attribute.c +81 -104
  81. data/ext/carray_bincount.c +12 -14
  82. data/ext/carray_broadcast.c +30 -26
  83. data/ext/carray_call_cfunc.c +169 -277
  84. data/ext/carray_call_cfunc.h +36 -28
  85. data/ext/carray_cast.c +912 -346
  86. data/ext/carray_cast_func.rb +31 -3
  87. data/ext/carray_class.c +6 -11
  88. data/ext/carray_conversion.c +43 -38
  89. data/ext/carray_copy.c +28 -26
  90. data/ext/carray_core.c +217 -126
  91. data/ext/carray_count.c +27 -15
  92. data/ext/carray_element.c +40 -25
  93. data/ext/carray_factorize.c +53 -52
  94. data/ext/carray_generate.c +26 -19
  95. data/ext/carray_histogram.c +8 -10
  96. data/ext/carray_hold.c +7 -9
  97. data/ext/carray_index_classifier.c +123 -228
  98. data/ext/carray_index_classifier.h +11 -10
  99. data/ext/carray_integer_total.c +523 -0
  100. data/ext/carray_internal.h +1274 -160
  101. data/ext/carray_key_windows.c +3 -3
  102. data/ext/carray_lazy.c +21 -36
  103. data/ext/carray_loop.c +19 -22
  104. data/ext/carray_mask.c +123 -145
  105. data/ext/carray_math_kernel.h +23 -47
  106. data/ext/carray_mathfunc.c +10 -3
  107. data/ext/carray_median_percentile.c +95 -94
  108. data/ext/carray_memory_view.c +42 -47
  109. data/ext/carray_operator.c +423 -122
  110. data/ext/carray_order.c +121 -85
  111. data/ext/carray_partition.c +13 -15
  112. data/ext/carray_random.c +47 -41
  113. data/ext/carray_round.c +9 -4
  114. data/ext/carray_scatter.c +54 -40
  115. data/ext/carray_segment.c +41 -45
  116. data/ext/carray_slab.c +64 -47
  117. data/ext/carray_slab.h +44 -33
  118. data/ext/carray_sort.c +32 -29
  119. data/ext/carray_sort_kernel.c +12 -10
  120. data/ext/carray_struct.c +38 -43
  121. data/ext/carray_test.c +47 -43
  122. data/ext/carray_time_text.c +24 -31
  123. data/ext/carray_undef.c +50 -7
  124. data/ext/carray_utils.c +258 -94
  125. data/ext/extconf.rb +24 -17
  126. data/ext/mk_call_cfunc.rb +19 -20
  127. data/ext/mk_pow5_table.rb +2 -1
  128. data/ext/mkkernel.rb +782 -461
  129. data/ext/ruby_carray.c +47 -42
  130. data/ext/version.h +7 -5
  131. data/lib/carray/attribute.rb +19 -18
  132. data/lib/carray/autoload_carray.rb +20 -15
  133. data/lib/carray/autoload_method_extension.rb +64 -57
  134. data/lib/carray/axis_group.rb +264 -180
  135. data/lib/carray/basics.rb +272 -48
  136. data/lib/carray/bincount_nd.rb +33 -16
  137. data/lib/carray/block_iterator.rb +123 -102
  138. data/lib/carray/boolean_reduce.rb +7 -4
  139. data/lib/carray/categorical.rb +63 -51
  140. data/lib/carray/categorical_iterator.rb +89 -67
  141. data/lib/carray/complex.rb +61 -41
  142. data/lib/carray/conditional.rb +49 -22
  143. data/lib/carray/const_string.rb +54 -62
  144. data/lib/carray/core_extensions.rb +1 -1
  145. data/lib/carray/distance_limit.rb +7 -5
  146. data/lib/carray/factories.rb +269 -0
  147. data/lib/carray/fixlen_string.rb +25 -24
  148. data/lib/carray/frame/column_verbs.rb +20 -8
  149. data/lib/carray/frame/concat.rb +3 -3
  150. data/lib/carray/frame/convert.rb +2 -1
  151. data/lib/carray/frame/csv_parser.rb +7 -1
  152. data/lib/carray/frame/frame.rb +35 -25
  153. data/lib/carray/frame/group.rb +183 -150
  154. data/lib/carray/frame/io.rb +26 -7
  155. data/lib/carray/frame/join.rb +37 -12
  156. data/lib/carray/frame/records.rb +1 -1
  157. data/lib/carray/frame/reshape.rb +3 -3
  158. data/lib/carray/frame/sort.rb +10 -8
  159. data/lib/carray/frame/verbs.rb +148 -118
  160. data/lib/carray/frame/windows.rb +232 -21
  161. data/lib/carray/frame.rb +1 -2
  162. data/lib/carray/fuse_source.rb +24 -21
  163. data/lib/carray/fusion.rb +30 -30
  164. data/lib/carray/histogram.rb +35 -16
  165. data/lib/carray/inspect.rb +7 -7
  166. data/lib/carray/iterator.rb +97 -24
  167. data/lib/carray/key_windows.rb +83 -57
  168. data/lib/carray/lazy.rb +196 -94
  169. data/lib/carray/limits.rb +80 -0
  170. data/lib/carray/mask_gap_fill.rb +21 -16
  171. data/lib/carray/math.rb +87 -50
  172. data/lib/carray/meld_reduce.rb +30 -8
  173. data/lib/carray/methods/align_addr.rb +29 -17
  174. data/lib/carray/methods/bin.rb +5 -1
  175. data/lib/carray/methods/bincount.rb +4 -2
  176. data/lib/carray/methods/bit_string.rb +19 -31
  177. data/lib/carray/methods/broadcast.rb +8 -3
  178. data/lib/carray/methods/choose.rb +5 -5
  179. data/lib/carray/methods/composition.rb +72 -71
  180. data/lib/carray/methods/{index.rb → coordinate.rb} +13 -13
  181. data/lib/carray/methods/discovery_along.rb +3 -3
  182. data/lib/carray/methods/gather_nd.rb +106 -106
  183. data/lib/carray/methods/insert_block.rb +2 -2
  184. data/lib/carray/methods/is_between.rb +6 -4
  185. data/lib/carray/methods/is_in.rb +165 -145
  186. data/lib/carray/methods/join.rb +2 -1
  187. data/lib/carray/methods/lag.rb +6 -2
  188. data/lib/carray/methods/locate_addr.rb +150 -138
  189. data/lib/carray/methods/mask_duplicates.rb +1 -1
  190. data/lib/carray/methods/meshgrid.rb +28 -16
  191. data/lib/carray/methods/mode.rb +67 -67
  192. data/lib/carray/methods/nunique.rb +9 -7
  193. data/lib/carray/methods/repeat.rb +12 -12
  194. data/lib/carray/methods/resize.rb +1 -1
  195. data/lib/carray/methods/snap.rb +24 -20
  196. data/lib/carray/methods/string_format.rb +2 -2
  197. data/lib/carray/methods/unique.rb +4 -2
  198. data/lib/carray/methods/value_counts.rb +1 -1
  199. data/lib/carray/mkmf.rb +25 -28
  200. data/lib/carray/percentile_arguments.rb +82 -0
  201. data/lib/carray/record_order.rb +18 -17
  202. data/lib/carray/rng.rb +1 -0
  203. data/lib/carray/runtime.rb +190 -22
  204. data/lib/carray/segment_iterator.rb +251 -129
  205. data/lib/carray/serialize.rb +618 -571
  206. data/lib/carray/slab_iterator.rb +114 -114
  207. data/lib/carray/stack.rb +11 -12
  208. data/lib/carray/string.rb +16 -22
  209. data/lib/carray/string_operation_extension.rb +76 -52
  210. data/lib/carray/struct.rb +122 -164
  211. data/lib/carray/struct_builder.rb +243 -72
  212. data/lib/carray/time.rb +496 -342
  213. data/lib/carray/window_iterator.rb +191 -150
  214. data/lib/carray.rb +6 -11
  215. data/yard-stubs/ca_obj_array.rb +20 -135
  216. data/yard-stubs/ca_obj_bitarray.rb +1 -8
  217. data/yard-stubs/ca_obj_bitfield.rb +4 -3
  218. data/yard-stubs/ca_obj_byte_swap.rb +21 -7
  219. data/yard-stubs/ca_obj_fake.rb +3 -1
  220. data/yard-stubs/ca_obj_farray.rb +1 -1
  221. data/yard-stubs/ca_obj_field.rb +8 -6
  222. data/yard-stubs/ca_obj_refer.rb +7 -5
  223. data/yard-stubs/ca_obj_roll.rb +7 -6
  224. data/yard-stubs/ca_obj_shift.rb +2 -3
  225. data/yard-stubs/ca_obj_stride.rb +5 -7
  226. data/yard-stubs/ca_obj_tile.rb +1 -1
  227. data/yard-stubs/ca_obj_time.rb +43 -0
  228. data/yard-stubs/ca_obj_transpose.rb +1 -1
  229. data/yard-stubs/ca_obj_window.rb +26 -15
  230. data/yard-stubs/carray_access.rb +51 -4
  231. data/yard-stubs/carray_attribute.rb +26 -38
  232. data/yard-stubs/carray_cast.rb +118 -165
  233. data/yard-stubs/carray_class.rb +3 -4
  234. data/yard-stubs/carray_conversion.rb +6 -7
  235. data/yard-stubs/carray_copy.rb +2 -1
  236. data/yard-stubs/carray_count.rb +5 -3
  237. data/yard-stubs/carray_element.rb +4 -3
  238. data/yard-stubs/carray_limits.rb +100 -0
  239. data/yard-stubs/carray_loop.rb +0 -20
  240. data/yard-stubs/carray_mask.rb +59 -41
  241. data/yard-stubs/carray_math.rb +157 -100
  242. data/yard-stubs/carray_mathfunc.rb +415 -3
  243. data/yard-stubs/carray_median_percentile.rb +29 -11
  244. data/yard-stubs/carray_memory_view.rb +2 -2
  245. data/yard-stubs/carray_order.rb +67 -34
  246. data/yard-stubs/carray_random.rb +6 -6
  247. data/yard-stubs/carray_reduce.rb +353 -0
  248. data/yard-stubs/carray_rng.rb +92 -0
  249. data/yard-stubs/carray_scatter.rb +10 -2
  250. data/yard-stubs/carray_slab.rb +7 -4
  251. data/yard-stubs/carray_sort.rb +7 -6
  252. data/yard-stubs/carray_undef.rb +44 -5
  253. data/yard-stubs/ruby_carray.rb +75 -40
  254. metadata +62 -24
  255. data/CHANGELOG.v1.md +0 -126
  256. data/ext/carray_build_flags.h +0 -3
  257. data/ext/carray_config.h +0 -28
  258. data/ext/carray_data_type.c +0 -90
  259. data/ext/carray_kernels_bincmp.c +0 -6457
  260. data/ext/carray_kernels_binop.c +0 -31534
  261. data/ext/carray_kernels_init.c +0 -1734
  262. data/ext/carray_kernels_moncmp.c +0 -2104
  263. data/ext/carray_kernels_monop.c +0 -25337
  264. data/ext/carray_kernels_reduce_aggregate.c +0 -27545
  265. data/ext/carray_kernels_reduce_boolean.c +0 -1924
  266. data/ext/carray_kernels_reduce_cumulative.c +0 -11646
  267. data/ext/carray_kernels_reduce_extreme.c +0 -21631
  268. data/ext/carray_kernels_reduce_variance.c +0 -5981
  269. data/ext/carray_kernels_scan.c +0 -4302
  270. data/ext/carray_kernels_search.c +0 -37776
  271. data/ext/carray_kernels_sort.c +0 -10814
  272. data/ext/carray_kernels_triop.c +0 -1864
  273. data/lib/carray/construct.rb +0 -360
  274. data/lib/carray/data_type_extension.rb +0 -262
  275. data/lib/carray/data_type_limits.rb +0 -91
data/ext/carray_core.c CHANGED
@@ -8,7 +8,7 @@
8
8
  ---------------------------------------------------------------------------- */
9
9
 
10
10
  #include "carray.h"
11
- #include "carray_internal.h" /* ca_lazy_arena_enter / _exit */
11
+ #include "ruby/version.h"
12
12
  #include "ca_iter_substrate.h"
13
13
  #include "ca_obj_face.h"
14
14
  #include <stdarg.h>
@@ -320,7 +320,7 @@ ca_init_obj_type (void)
320
320
  regsters a sub-class of CArray
321
321
  */
322
322
 
323
- int
323
+ int16_t
324
324
  ca_install_obj_type (VALUE klass,
325
325
  const rb_data_type_t *typeddata,
326
326
  VALUE mask_klass,
@@ -328,19 +328,18 @@ ca_install_obj_type (VALUE klass,
328
328
  const ca_operation_function_t *func,
329
329
  size_t func_size)
330
330
  {
331
- int obj_type;
331
+ int16_t obj_type;
332
332
 
333
- /* The obj_type parameters of ca_array_alloc / ca_stride_setup are
334
- int8_t, so a number of 128 or above cannot be passed to them. */
335
- if ( ca_obj_num > INT8_MAX || ca_obj_num + 1 >= CA_OBJ_TYPE_MAX ) {
333
+ if ( ca_obj_num >= CA_OBJ_TYPE_MAX ) {
336
334
  rb_raise(rb_eRuntimeError,
337
- "internal: too many CArray object types installed (an obj_type is numbered below 128)");
335
+ "internal: too many CArray object types installed (an obj_type is numbered below %d)",
336
+ CA_OBJ_TYPE_MAX);
338
337
  }
339
338
 
340
339
  if ( func_size > sizeof(ca_operation_function_t) ) {
341
340
  rb_raise(rb_eRuntimeError,
342
- "operation table is larger than this carray's (%zu > %zu); "
343
- "the caller was built against a newer carray",
341
+ "ca_install_obj_type: operation table is larger than this "
342
+ "carray's (%zu > %zu); the caller was built against a newer carray",
344
343
  func_size, sizeof(ca_operation_function_t));
345
344
  }
346
345
 
@@ -349,7 +348,8 @@ ca_install_obj_type (VALUE klass,
349
348
  if ( func_size < offsetof(ca_operation_function_t, xfer_all) +
350
349
  sizeof(((ca_operation_function_t *) 0)->xfer_all) ) {
351
350
  rb_raise(rb_eRuntimeError,
352
- "operation table is too small to dispatch (%zu < %zu)",
351
+ "ca_install_obj_type: operation table is too small to dispatch "
352
+ "(%zu < %zu)",
353
353
  func_size,
354
354
  (size_t) (offsetof(ca_operation_function_t, xfer_all) +
355
355
  sizeof(((ca_operation_function_t *) 0)->xfer_all)));
@@ -364,7 +364,7 @@ ca_install_obj_type (VALUE klass,
364
364
  }
365
365
 
366
366
  /* A refused table takes no number. */
367
- obj_type = ca_obj_num++;
367
+ obj_type = (int16_t) ca_obj_num++;
368
368
 
369
369
  /* Copy by the caller's length and zero the rest: slots this build knows
370
370
  about but the caller does not are NULL, which every dispatcher already
@@ -381,6 +381,23 @@ ca_install_obj_type (VALUE klass,
381
381
  return obj_type;
382
382
  }
383
383
 
384
+ void
385
+ ca_operation_table_copy (int16_t obj_type, ca_operation_function_t *dst,
386
+ size_t size)
387
+ {
388
+ if ( obj_type < 0 || obj_type >= ca_obj_num ) {
389
+ rb_raise(rb_eArgError,
390
+ "ca_operation_table_copy: obj_type %d is not installed",
391
+ obj_type);
392
+ }
393
+ if ( size > sizeof(ca_operation_function_t) ) {
394
+ memset((char *) dst + sizeof(ca_operation_function_t), 0,
395
+ size - sizeof(ca_operation_function_t));
396
+ size = sizeof(ca_operation_function_t);
397
+ }
398
+ memcpy(dst, &ca_func[obj_type], size);
399
+ }
400
+
384
401
  /* ------------------------------------------------------------------- */
385
402
 
386
403
  /* api: ca_mark
@@ -407,6 +424,7 @@ ca_mark (void *ap)
407
424
  free function for the carray object needs free operation.
408
425
  */
409
426
 
427
+ /* recursion: frees this array only; a free never raises */
410
428
  void
411
429
  ca_free (void *ap)
412
430
  {
@@ -445,6 +463,7 @@ ca_attach_is_alias (void *ap)
445
463
  extern ca_operation_function_t ca_lazy_marker_func;
446
464
  extern int ca_stride_attach_aliases_root (CAStride *ca);
447
465
 
466
+ ca_check_stack_room();
448
467
  if ( ca == NULL ) return 0;
449
468
  if ( ca_is_entity(ca) ) return 1;
450
469
 
@@ -495,13 +514,13 @@ ca_attach_is_alias (void *ap)
495
514
  rather than at the slab walk). Mask is NULL on the output (=
496
515
  reduction kernels populate it themselves if needed). */
497
516
  VALUE
498
- rb_ca_new_reduced_bytes (VALUE self, int8_t *slab_axes, int8_t naxes,
517
+ rb_ca_new_reduced_bytes (volatile VALUE self, int8_t *slab_axes, int8_t naxes,
499
518
  int32_t data_type, ca_size_t bytes, int keep_axis)
500
519
  {
501
520
  CArray *ca;
502
521
  int8_t k;
503
522
 
504
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
523
+ GetCArray(self, ca);
505
524
 
506
525
  if ( naxes <= 0 || naxes > ca->ndim ) {
507
526
  rb_raise(rb_eArgError,
@@ -561,7 +580,7 @@ rb_ca_new_reduced_bytes (VALUE self, int8_t *slab_axes, int8_t naxes,
561
580
  }
562
581
 
563
582
  /* Fixed-element-size wrapper: the byte width is looked up from ca_sizeof,
564
- which rejects CA_FIXLEN / CA_OBJECT (runtime-width / VALUE cells). A
583
+ which rejects CA_FIXLEN (runtime-width cells). A
565
584
  reduction whose output is a runtime-width data_type (fixlen min / max)
566
585
  must call rb_ca_new_reduced_bytes with the source's own byte width. */
567
586
  VALUE
@@ -625,7 +644,7 @@ parse_axes_items (const VALUE *items, int count, CArray *ca,
625
644
  }
626
645
  if ( count > CA_RANK_MAX ) { /* rank-check: a list of axes, no longer than the rank */
627
646
  rb_raise(rb_eArgError,
628
- "%s: too many axes (%d > CA_RANK_MAX=%d)",
647
+ "%s: too many axes (%d > CArray::RANK_MAX=%d)",
629
648
  CA_CTX_NAME(ctx), count, CA_RANK_MAX);
630
649
  }
631
650
  if ( count > ca->ndim ) {
@@ -636,7 +655,7 @@ parse_axes_items (const VALUE *items, int count, CArray *ca,
636
655
 
637
656
  for ( i = 0; i < CA_RANK_MAX; i++ ) seen[i] = 0;
638
657
  for ( i = 0; i < count; i++ ) {
639
- ca_size_t a = ca_normalize_axis_arg(items[i], (int) ca->ndim, ctx);
658
+ ca_size_t a = rb_ca_normalize_axis_arg(items[i], (int) ca->ndim, ctx);
640
659
  if ( seen[a] ) {
641
660
  rb_raise(rb_eArgError,
642
661
  "%s: duplicate axis %ld", CA_CTX_NAME(ctx), (long) a);
@@ -667,7 +686,7 @@ rb_ca_parse_reduce_axes (int argc, VALUE *argv, CArray *ca, int8_t *out_axes)
667
686
  const VALUE *items = (const VALUE *) argv;
668
687
  int count = argc;
669
688
  if ( argc == 1 && TYPE(argv[0]) == T_ARRAY ) {
670
- VALUE arr = argv[0];
689
+ volatile VALUE arr = argv[0];
671
690
  count = (int) RARRAY_LEN(arr);
672
691
  items = (const VALUE *) RARRAY_CONST_PTR(arr);
673
692
  }
@@ -688,7 +707,7 @@ rb_ca_parse_reduce_axes (int argc, VALUE *argv, CArray *ca, int8_t *out_axes)
688
707
  variadic entry. out_axes[] receives axes in input order. Returns
689
708
  the validated naxes. */
690
709
  int8_t
691
- rb_ca_parse_reduce_axes_kw_ctx (VALUE axis_val, CArray *ca, int8_t *out_axes,
710
+ rb_ca_parse_reduce_axes_kw_ctx (volatile VALUE axis_val, CArray *ca, int8_t *out_axes,
692
711
  const char *ctx)
693
712
  {
694
713
  int i;
@@ -707,7 +726,8 @@ rb_ca_parse_reduce_axes_kw_ctx (VALUE axis_val, CArray *ca, int8_t *out_axes,
707
726
  }
708
727
 
709
728
  if ( rb_obj_is_kind_of(axis_val, rb_cInteger) ) {
710
- return parse_axes_items(&axis_val, 1, ca, out_axes, ctx);
729
+ VALUE item = axis_val;
730
+ return parse_axes_items(&item, 1, ca, out_axes, ctx);
711
731
  }
712
732
 
713
733
  rb_raise(rb_eTypeError,
@@ -909,6 +929,7 @@ static VALUE ca_recursion_probe (VALUE key, VALUE arg, int recursive);
909
929
  would come back into that evaluation when it is computed. The walk is
910
930
  bounded; past the bound the value is not refused here, and is caught when
911
931
  it is evaluated, as a deeper expression is. */
932
+ /* recursion: stops when its budget runs out */
912
933
  static int
913
934
  ca_reaches_evaluating_walk (CArray *ca, int *budget)
914
935
  {
@@ -946,7 +967,8 @@ ca_refuse_cells_reaching_evaluation (ca_size_t n, const VALUE *cells)
946
967
  if ( rb_obj_is_carray(cells[i]) ) {
947
968
  CArray *cv;
948
969
  int budget = 1024;
949
- TypedData_Get_Struct(cells[i], CArray, &carray_data_type, cv);
970
+ /* gc: the caller holds the cells */
971
+ GetCArray(cells[i], cv);
950
972
  if ( ! ca_is_entity(cv) && ca_reaches_evaluating_walk(cv, &budget) ) {
951
973
  ca_refuse_cyclic(CA_RECURSION_COMPUTE);
952
974
  }
@@ -1081,7 +1103,7 @@ ca_test_cyclic_check (void *ap, void *ptr)
1081
1103
  volatile VALUE rval = *(VALUE *) ptr;
1082
1104
  if ( rb_obj_is_carray(rval) ) {
1083
1105
  CArray *cv;
1084
- TypedData_Get_Struct(rval, CArray, &carray_data_type, cv);
1106
+ GetCArray(rval, cv);
1085
1107
  if ( cv == ca ||
1086
1108
  RTEST(rb_exec_recursive(ca_recursion_probe,
1087
1109
  ca_recursion_key(cv, CA_RECURSION_READ),
@@ -1108,7 +1130,8 @@ ca_refuse_storing_self (CArray *ca, const void *data, ca_size_t n)
1108
1130
  memcpy(&rval, d + i * sizeof(VALUE), sizeof(VALUE));
1109
1131
  if ( rb_obj_is_carray(rval) ) {
1110
1132
  CArray *cv;
1111
- TypedData_Get_Struct(rval, CArray, &carray_data_type, cv);
1133
+ /* gc: data, the caller's cells, holds it */
1134
+ GetCArray(rval, cv);
1112
1135
  if ( ca_shares_root(ca, cv) ) {
1113
1136
  rb_raise(rb_eArgError, "%s: can not store an object array into itself",
1114
1137
  ca_calling_method_name());
@@ -1136,9 +1159,9 @@ ca_reading_step (VALUE key, VALUE varg, int recursive)
1136
1159
  static char ca_obj_moves_cells[CA_OBJ_TYPE_MAX];
1137
1160
 
1138
1161
  void
1139
- ca_register_cell_moving_view (int8_t obj_type)
1162
+ ca_register_cell_moving_view (int16_t obj_type)
1140
1163
  {
1141
- ca_obj_moves_cells[(uint8_t) obj_type] = 1;
1164
+ ca_obj_moves_cells[obj_type] = 1;
1142
1165
  }
1143
1166
 
1144
1167
  /* Whether the transfers of a Face are the forwarders of ca_obj_face.c, which
@@ -1158,6 +1181,7 @@ ca_face_forwards_transfers (CArray *ca)
1158
1181
  /* Whether a transfer of ca reaches entities through cell-moving views only:
1159
1182
  then it calls no Ruby on the way. An unknown view, one that computes its
1160
1183
  cells (CAObject, a lazy expression) or converts them, answers no. */
1184
+ /* recursion: stops at a depth of 64 */
1161
1185
  static int
1162
1186
  ca_transfer_calls_no_ruby (CArray *ca, int depth)
1163
1187
  {
@@ -1168,7 +1192,7 @@ ca_transfer_calls_no_ruby (CArray *ca, int depth)
1168
1192
  if ( ca_test_flag(ca, CA_FLAG_MULTI_PARENTS) ) {
1169
1193
  CAMultiParent *mp = (CAMultiParent *) ca;
1170
1194
  int32_t k;
1171
- if ( ! ca_obj_moves_cells[(uint8_t) ca->obj_type] ) {
1195
+ if ( ! ca_obj_moves_cells[ca->obj_type] ) {
1172
1196
  return 0;
1173
1197
  }
1174
1198
  for ( k = 0; k < mp->n_parents; k++ ) {
@@ -1181,7 +1205,7 @@ ca_transfer_calls_no_ruby (CArray *ca, int depth)
1181
1205
  }
1182
1206
  if ( ca_is_face(ca) ? ! ca_face_forwards_transfers(ca)
1183
1207
  : ! ( ca_is_stride_family(ca) ||
1184
- ca_obj_moves_cells[(uint8_t) ca->obj_type] ) ) {
1208
+ ca_obj_moves_cells[ca->obj_type] ) ) {
1185
1209
  return 0;
1186
1210
  }
1187
1211
  ca = ((CAView *) ca)->parent;
@@ -1263,6 +1287,7 @@ typedef struct {
1263
1287
  enum { CA_TRIAL_ADDRS, CA_TRIAL_STRIDE, CA_TRIAL_ALL, CA_TRIAL_FILL,
1264
1288
  CA_TRIAL_FILL_STRIDE, CA_TRIAL_FILL_ADDRS, CA_TRIAL_SYNC };
1265
1289
 
1290
+ /* recursion: reached only from the dispatchers, which check the stack */
1266
1291
  static VALUE
1267
1292
  ca_value_trial_put_body (VALUE arg)
1268
1293
  {
@@ -1295,6 +1320,7 @@ ca_value_trial_put_body (VALUE arg)
1295
1320
  } \
1296
1321
  } while (0)
1297
1322
 
1323
+ /* recursion: reached only from ca_xfer_index, which checks the stack */
1298
1324
  static inline void
1299
1325
  ca_xfer_index_dispatch (CArray *ca, ca_size_t *idx, void *data, int dir)
1300
1326
  {
@@ -1329,6 +1355,11 @@ ca_xfer_index (void *ap, ca_size_t *idx, void *data, int dir)
1329
1355
  {
1330
1356
  CArray *ca = (CArray *) ap;
1331
1357
 
1358
+ /* An entity has no parent to recurse into; this runs once per cell. */
1359
+ if ( ! ca_is_entity(ca) ) {
1360
+ ca_check_stack_room();
1361
+ }
1362
+
1332
1363
  if ( dir == CA_XFER_PUT && ca_is_readonly(ca) ) {
1333
1364
  ca_refuse_read_only();
1334
1365
  }
@@ -1397,6 +1428,7 @@ ca_xfer_addrs_is_sequential_run (ca_size_t n, ca_size_t *addrs,
1397
1428
  return 1;
1398
1429
  }
1399
1430
 
1431
+ /* recursion: reached only from ca_xfer_addrs, which checks the stack */
1400
1432
  static void
1401
1433
  ca_xfer_addrs_dispatch (CArray *ca, ca_size_t n, ca_size_t *addrs,
1402
1434
  void *data, int dir)
@@ -1481,6 +1513,11 @@ ca_xfer_addrs (void *ap, ca_size_t n, ca_size_t *addrs, void *data, int dir)
1481
1513
  {
1482
1514
  CArray *ca = (CArray *) ap;
1483
1515
 
1516
+ /* An entity has no parent to recurse into; this runs once per cell. */
1517
+ if ( ! ca_is_entity(ca) ) {
1518
+ ca_check_stack_room();
1519
+ }
1520
+
1484
1521
  if ( dir == CA_XFER_PUT && ca_is_readonly(ca) ) {
1485
1522
  ca_refuse_read_only();
1486
1523
  }
@@ -1847,6 +1884,7 @@ ca_xfer_stride_request_is_axis_box (void *ap, ca_size_t *starts,
1847
1884
  return 1;
1848
1885
  }
1849
1886
 
1887
+ /* recursion: reached only from ca_xfer_stride, which checks the stack */
1850
1888
  static void
1851
1889
  ca_xfer_stride_dispatch (CArray *ca, ca_size_t *starts, ca_size_t *counts,
1852
1890
  ca_size_t *strides, void *data, int dir)
@@ -1940,6 +1978,8 @@ ca_xfer_stride (void *ap, ca_size_t *starts, ca_size_t *counts,
1940
1978
  {
1941
1979
  CArray *ca = (CArray *) ap;
1942
1980
 
1981
+ ca_check_stack_room();
1982
+
1943
1983
  if ( dir == CA_XFER_PUT && ca_is_readonly(ca) ) {
1944
1984
  ca_refuse_read_only();
1945
1985
  }
@@ -1989,6 +2029,7 @@ typedef struct {
1989
2029
  int dir;
1990
2030
  } ca_xfer_all_args_t;
1991
2031
 
2032
+ /* recursion: reached only from ca_xfer_all, which checks the stack */
1992
2033
  static VALUE
1993
2034
  ca_xfer_all_body (VALUE arg)
1994
2035
  {
@@ -2013,6 +2054,8 @@ ca_xfer_all (void *ap, void *data, int dir)
2013
2054
  CArray *ca = (CArray *) ap;
2014
2055
  ca_xfer_all_args_t args;
2015
2056
  int guard = -1;
2057
+
2058
+ ca_check_stack_room();
2016
2059
  if ( ! ca_func[ca->obj_type].xfer_all ) {
2017
2060
  rb_raise(rb_eRuntimeError,
2018
2061
  "[BUG] xfer_all not defined for object type <%i>",
@@ -2081,6 +2124,7 @@ ca_allocate_rest (CArray *ca, CArray *mask0)
2081
2124
 
2082
2125
  /* The view half of ca_allocate, after the attach level is taken. Run
2083
2126
  under rb_protect by ca_allocate. */
2127
+ /* recursion: reached only from ca_allocate, which checks the stack */
2084
2128
  static VALUE
2085
2129
  ca_allocate_view (VALUE arg)
2086
2130
  {
@@ -2097,7 +2141,7 @@ ca_allocate_view (VALUE arg)
2097
2141
 
2098
2142
  /* Steps a view's attach level by d. A development build also counts the
2099
2143
  views whose level is above zero, and the spec compares that count
2100
- before and after every test: a window left open shows as a view that
2144
+ before and after every test: a view left attached shows as a view that
2101
2145
  stays counted. */
2102
2146
  #ifdef CARRAY_DEV_BUILD
2103
2147
  static long ca_attached_views = 0;
@@ -2204,10 +2248,12 @@ ca_shortcuts_off_from_env (void)
2204
2248
  static VALUE
2205
2249
  rb_ca_s_fast_paths_enabled (int argc, VALUE *argv, VALUE klass)
2206
2250
  {
2207
- VALUE rname;
2251
+ VALUE rname_arg;
2252
+ volatile VALUE rname;
2208
2253
  int id;
2209
2254
 
2210
- rb_scan_args(argc, argv, "01", &rname);
2255
+ rb_scan_args(argc, argv, "01", &rname_arg);
2256
+ rname = rname_arg;
2211
2257
  if ( NIL_P(rname) ) {
2212
2258
  return ca_fast_paths_on ? Qtrue : Qfalse;
2213
2259
  }
@@ -2241,6 +2287,8 @@ ca_allocate (void *ap)
2241
2287
  return;
2242
2288
  }
2243
2289
 
2290
+ ca_check_stack_room();
2291
+
2244
2292
  if ( ca_is_view(ca) ) { /* view array */
2245
2293
 
2246
2294
  if ( CAVIEW(ca)->attach >= CA_ATTACH_MAX ) {
@@ -2277,6 +2325,7 @@ ca_allocate (void *ap)
2277
2325
 
2278
2326
  /* The first attach of a view: materialise through the type's slot, then
2279
2327
  its mask. Run under rb_protect by ca_attach. */
2328
+ /* recursion: reached only from ca_attach, which checks the stack */
2280
2329
  static VALUE
2281
2330
  ca_attach_view_first (VALUE arg)
2282
2331
  {
@@ -2321,6 +2370,8 @@ ca_attach (void *ap)
2321
2370
  return;
2322
2371
  }
2323
2372
 
2373
+ ca_check_stack_room();
2374
+
2324
2375
  if ( ca_is_view(ca) ) { /* view array */
2325
2376
 
2326
2377
  if ( CAVIEW(ca)->attach >= CA_ATTACH_MAX ) {
@@ -2451,21 +2502,21 @@ ca_sync_all (CArray **list, int32_t n)
2451
2502
  }
2452
2503
  }
2453
2504
 
2454
- /* An attach window over list[0..n-1] that closes however body leaves.
2505
+ /* Attaches list[0..n-1], runs body, and detaches them however body leaves.
2455
2506
 
2456
2507
  modes holds one letter per array:
2457
- 'r' attach; detach on close
2458
- 'w' attach; sync, then detach on close
2459
- 'a' allocate (write only); sync, then detach on close
2460
-
2461
- Opening takes all of the arrays or none. Closing syncs the 'w' arrays
2462
- even when body raised or broke out -- the cells body wrote reach the
2463
- parent, as they would for an entity, which has no separate buffer. An
2464
- 'a' array is not synced after a raise: allocate did not gather, so its
2465
- cells body never wrote hold nothing faithful to push back. Allocate
2466
- clears the mask as it opens, and an entity (or the parent under a view)
2467
- has no separate mask to fall back on, so the mask of an 'a' array is
2468
- copied before it opens and written back when body raises.
2508
+ 'r' attach; detach at the end
2509
+ 'w' attach; sync, then detach at the end
2510
+ 'a' allocate (write only); sync, then detach at the end
2511
+
2512
+ Attaching takes all of the arrays or none. At the end the 'w' arrays
2513
+ are synced even when body raised or broke out -- the cells body wrote
2514
+ reach the parent, as they would for an entity, which has no separate
2515
+ buffer. An 'a' array is not synced after a raise: allocate did not
2516
+ gather, so its cells body never wrote hold nothing faithful to push
2517
+ back. Allocate clears the mask, and an entity (or the parent under a
2518
+ view) has no separate mask to fall back on, so the mask of an 'a' array
2519
+ is copied before the allocate and written back when body raises.
2469
2520
 
2470
2521
  Every array is detached, in reverse order, whatever happens. body's
2471
2522
  own exception wins over one raised by a sync; when body returned and a
@@ -2500,7 +2551,7 @@ ca_attach_window_save_masks (ca_window_ctx_t *w)
2500
2551
  }
2501
2552
  ca_update_mask(ca);
2502
2553
  {
2503
- VALUE buf = rb_str_new(NULL, (long) ca->elements);
2554
+ volatile VALUE buf = rb_str_new(NULL, (long) ca->elements);
2504
2555
  ca_copy_data(ca->mask, RSTRING_PTR(buf));
2505
2556
  rb_ary_store(w->masks, k, buf);
2506
2557
  }
@@ -2516,7 +2567,10 @@ ca_attach_window_restore_masks (ca_window_ctx_t *w)
2516
2567
  return;
2517
2568
  }
2518
2569
  for ( k = 0; k < w->n; k++ ) {
2519
- VALUE buf = rb_ary_entry(w->masks, k);
2570
+ /* held otherwise only by w->masks, where compaction can move it, and
2571
+ the write may call into Ruby (a CAObject's mask hooks): held on the
2572
+ stack here, which also pins it */
2573
+ volatile VALUE buf = rb_ary_entry(w->masks, k);
2520
2574
  CArray *ca = w->list[k];
2521
2575
  if ( NIL_P(buf) ) {
2522
2576
  continue;
@@ -2526,9 +2580,6 @@ ca_attach_window_restore_masks (ca_window_ctx_t *w)
2526
2580
  ca_create_mask(ca);
2527
2581
  }
2528
2582
  ca_xfer_all(ca->mask, RSTRING_PTR(buf), CA_XFER_PUT);
2529
- /* held only by w->masks, where compaction can move it; the write may
2530
- call into Ruby (a CAObject's mask hooks) */
2531
- RB_GC_GUARD(buf);
2532
2583
  }
2533
2584
  }
2534
2585
 
@@ -2636,21 +2687,21 @@ ca_attach_window (int32_t n, CArray **list, const char *modes,
2636
2687
  return result;
2637
2688
  }
2638
2689
 
2639
- /* ---- A window whose Fiber is abandoned ------------------------------------
2690
+ /* ---- Arrays attached in a Fiber that is abandoned -------------------------
2640
2691
 
2641
- A window that yields to a block is closed by rb_ensure. Ruby does not
2692
+ Arrays attached for a block are detached by rb_ensure. Ruby does not
2642
2693
  run the ensure of a Fiber that is suspended and then collected -- an
2643
- Enumerator stopped with #next and dropped is one -- so such a window
2644
- would stay open for good: a view keeps its attach, and a source that
2694
+ Enumerator stopped with #next and dropped is one -- so such arrays
2695
+ would stay attached for good: a view keeps its attach, and a source that
2645
2696
  publishes its ptr only while held (a CASource) keeps it published and
2646
2697
  stops checking its backend.
2647
2698
 
2648
- The guard closes it from a finalizer instead. The window's state lives
2649
- on the heap, owned by the guard; a sentinel object that only the
2650
- window's frame refers to carries a finalizer that holds the guard. When
2651
- the Fiber is collected the sentinel goes with it, and the finalizer --
2652
- which runs outside GC, where Ruby may be called -- runs `cleanup`. A
2653
- window that closes normally disarms the guard first.
2699
+ The guard detaches them from a finalizer instead. The state of the
2700
+ attach lives on the heap, owned by the guard; a sentinel object that
2701
+ only the attaching frame refers to carries a finalizer that holds the
2702
+ guard. When the Fiber is collected the sentinel goes with it, and the
2703
+ finalizer -- which runs outside GC, where Ruby may be called -- runs
2704
+ `cleanup`. Code that detaches normally disarms the guard first.
2654
2705
 
2655
2706
  `mark` marks the VALUEs `arg` holds, which nothing else does once the
2656
2707
  frame that referred to them is gone. The guard frees `arg` with xfree. */
@@ -2680,7 +2731,7 @@ ca_window_guard_free (void *p)
2680
2731
  }
2681
2732
 
2682
2733
  static const rb_data_type_t ca_window_guard_data_type = {
2683
- .wrap_struct_name = "CArrayWindowGuard",
2734
+ .wrap_struct_name = "CArray::WindowGuard",
2684
2735
  .function = {
2685
2736
  .dmark = ca_window_guard_mark,
2686
2737
  .dfree = ca_window_guard_free,
@@ -2690,7 +2741,7 @@ static const rb_data_type_t ca_window_guard_data_type = {
2690
2741
  };
2691
2742
 
2692
2743
  static VALUE
2693
- ca_window_guard_cleanup_step (VALUE gv)
2744
+ ca_window_guard_cleanup_step (volatile VALUE gv)
2694
2745
  {
2695
2746
  ca_window_guard_t *g;
2696
2747
  TypedData_Get_Struct(gv, ca_window_guard_t, &ca_window_guard_data_type, g);
@@ -2704,7 +2755,7 @@ ca_window_guard_cleanup_step (VALUE gv)
2704
2755
  static VALUE rb_cCAWindowGuard;
2705
2756
 
2706
2757
  static VALUE
2707
- ca_window_guard_call (VALUE gv, VALUE object_id)
2758
+ ca_window_guard_call (volatile VALUE gv, VALUE object_id)
2708
2759
  {
2709
2760
  ca_window_guard_t *g;
2710
2761
  int tag = 0;
@@ -2720,42 +2771,65 @@ ca_window_guard_call (VALUE gv, VALUE object_id)
2720
2771
  return Qnil;
2721
2772
  }
2722
2773
 
2774
+ /* Ruby 3.4 corrupts its finalizer table when Ractors define and undefine
2775
+ finalizers at the same time (a Ruby loop of define_finalizer and
2776
+ undefine_finalizer in eight Ractors crashes it), so there only the main
2777
+ Ractor sets the finalizer. Arrays attached in another Ractor whose
2778
+ Fiber is abandoned stay attached there. */
2779
+ static int
2780
+ ca_window_guard_finalizes (void)
2781
+ {
2782
+ #if RUBY_API_VERSION_MAJOR < 4
2783
+ return ca_in_main_ractor();
2784
+ #else
2785
+ return 1;
2786
+ #endif
2787
+ }
2788
+
2723
2789
  /* Takes ownership of arg (allocated with xmalloc). Returns the guard and
2724
- stores the sentinel; the caller keeps both on its frame (RB_GC_GUARD)
2725
- for as long as the window may be open. */
2790
+ stores the sentinel (nil when no finalizer is set); the caller keeps
2791
+ both on its frame, in volatile variables or in a struct there, for as
2792
+ long as the arrays may be attached. */
2726
2793
  VALUE
2727
2794
  ca_window_guard_arm (void (*cleanup)(void *), void (*mark)(void *), void *arg,
2728
- VALUE *sentinel)
2795
+ volatile VALUE *sentinel)
2729
2796
  {
2730
2797
  ca_window_guard_t *g;
2731
2798
  volatile VALUE gv, sv;
2799
+
2732
2800
  gv = TypedData_Make_Struct(rb_cCAWindowGuard, ca_window_guard_t,
2733
2801
  &ca_window_guard_data_type, g);
2734
2802
  g->cleanup = cleanup;
2735
2803
  g->mark = mark;
2736
2804
  g->arg = arg;
2737
2805
  g->armed = 1;
2738
- sv = rb_class_new_instance(0, NULL, rb_cObject);
2739
- rb_define_finalizer(sv, gv);
2806
+ sv = Qnil;
2807
+ if ( ca_window_guard_finalizes() ) {
2808
+ sv = rb_class_new_instance(0, NULL, rb_cObject);
2809
+ rb_define_finalizer(sv, gv);
2810
+ }
2740
2811
  *sentinel = sv;
2741
2812
  return gv;
2742
2813
  }
2743
2814
 
2744
- /* Called first thing when the window closes normally, so that cleanup
2745
- cannot also run from the finalizer. */
2815
+ /* Called first thing when the arrays are detached normally, so that
2816
+ cleanup cannot also run from the finalizer. */
2746
2817
  void
2747
- ca_window_guard_disarm (VALUE gv, VALUE sentinel)
2818
+ ca_window_guard_disarm (volatile VALUE gv, VALUE sentinel)
2748
2819
  {
2749
2820
  ca_window_guard_t *g;
2750
2821
  TypedData_Get_Struct(gv, ca_window_guard_t, &ca_window_guard_data_type, g);
2751
2822
  g->armed = 0;
2823
+ if ( NIL_P(sentinel) ) {
2824
+ return;
2825
+ }
2752
2826
  /* rb_undefine_finalizer is declared in the headers but not exported by
2753
2827
  every libruby (3.4.1 lacks it), so go through ObjectSpace. */
2754
2828
  rb_funcall(rb_const_get(rb_cObject, rb_intern("ObjectSpace")),
2755
2829
  rb_intern("undefine_finalizer"), 1, sentinel);
2756
2830
  }
2757
2831
 
2758
- /* ca_attach_window for a window that yields to a block, closed by the
2832
+ /* ca_attach_window for a body that yields to a block, detached by the
2759
2833
  guard above if its Fiber is abandoned. The context, the list and the
2760
2834
  modes are copied to the heap, since the caller's frame goes with the
2761
2835
  Fiber; `owners` keeps the arrays in list alive until then. */
@@ -2824,7 +2898,7 @@ ca_attach_window_yielding (VALUE owners, int32_t n, CArray **list,
2824
2898
  gw->w.body = body;
2825
2899
  gw->w.arg = arg;
2826
2900
  gw->w.masks = Qnil;
2827
- guard = ca_window_guard_arm(NULL, ca_guarded_window_mark, gw, (VALUE *) &sentinel);
2901
+ guard = ca_window_guard_arm(NULL, ca_guarded_window_mark, gw, &sentinel);
2828
2902
  ca_attach_window_save_masks(&gw->w);
2829
2903
 
2830
2904
  rb_protect(ca_attach_window_open, (VALUE) &gw->w, &tag);
@@ -2855,17 +2929,14 @@ ca_attach_window_yielding (VALUE owners, int32_t n, CArray **list,
2855
2929
  if ( gw->w.sync_tag && RB_TYPE_P(gw->w.sync_err, T_OBJECT) &&
2856
2930
  RTEST(rb_obj_is_kind_of(gw->w.sync_err, rb_eException)) ) {
2857
2931
  VALUE err = gw->w.sync_err;
2858
- RB_GC_GUARD(guard);
2859
- RB_GC_GUARD(sentinel);
2860
2932
  rb_exc_raise(err);
2861
2933
  }
2862
- RB_GC_GUARD(guard);
2863
- RB_GC_GUARD(sentinel);
2864
2934
  return result;
2865
2935
  }
2866
2936
 
2867
2937
  /* re-gather an attached view's buffer from its parent */
2868
2938
 
2939
+ /* recursion: re-enters once, for the mask */
2869
2940
  void
2870
2941
  ca_update (void *ap)
2871
2942
  {
@@ -2902,6 +2973,8 @@ ca_sync (void *ap)
2902
2973
  return;
2903
2974
  }
2904
2975
 
2976
+ ca_check_stack_room();
2977
+
2905
2978
  if ( ! ca->ptr ) {
2906
2979
  rb_raise(rb_eRuntimeError,
2907
2980
  "[BUG] tried to sync data to detached array");
@@ -2917,10 +2990,10 @@ ca_sync (void *ap)
2917
2990
  }
2918
2991
 
2919
2992
 
2920
- /* Only a mask that exists can hold writes: one made inside the window
2921
- is attached with the view (ca_create_mask). Making one here would
2922
- read the parent's mask while the window closes, and a parent that
2923
- cannot answer would leave the window open. */
2993
+ /* Only a mask that exists can hold writes: one made while the view is
2994
+ attached is attached with the view (ca_create_mask). Making one here
2995
+ would read the parent's mask during the detach, and a parent that
2996
+ cannot answer would leave the view attached. */
2924
2997
  if ( ca->mask ) {
2925
2998
  ca_sync(ca->mask);
2926
2999
  }
@@ -2936,11 +3009,11 @@ ca_sync (void *ap)
2936
3009
 
2937
3010
  }
2938
3011
 
2939
- /* Sync ap and detach it -- the close of a window a frame opened with
3012
+ /* Sync ap and detach it -- the end of an attach a frame began with
2940
3013
  ca_attach or ca_allocate. The detach happens even when the sync raises
2941
3014
  (a read-only array, a backing whose write fails); the sync's exception
2942
3015
  then propagates. Written as two calls, a raising sync skips the detach
2943
- and the window stays open.
3016
+ and the array stays attached.
2944
3017
 
2945
3018
  Not a replacement for every ca_sync: a view's sync slot syncs its parent
2946
3019
  without detaching it, because that attach belongs to the view and its
@@ -2974,6 +3047,7 @@ ca_sync_detach (void *ap)
2974
3047
 
2975
3048
  /* make ca->ptr to be detached */
2976
3049
 
3050
+ /* recursion: closes what ca_attach opened along the same chain, which checked the stack; it must not raise */
2977
3051
  void
2978
3052
  ca_detach (void *ap)
2979
3053
  {
@@ -3143,6 +3217,7 @@ ca_fill_data (void *ap, void *aptr)
3143
3217
  {
3144
3218
  CArray *ca = (CArray *) ap;
3145
3219
  char *ptr = (char *) aptr;
3220
+ ca_check_stack_room();
3146
3221
 
3147
3222
  if ( ca_is_readonly(ca) ) {
3148
3223
  ca_refuse_read_only();
@@ -3202,6 +3277,7 @@ ca_fill_stride (void *ap, ca_size_t base, int8_t ndim,
3202
3277
  ca_size_t *counts, ca_size_t *steps, void *ptr)
3203
3278
  {
3204
3279
  CArray *ca = (CArray *) ap;
3280
+ ca_check_stack_room();
3205
3281
 
3206
3282
  if ( ca_is_readonly(ca) ) {
3207
3283
  ca_refuse_read_only();
@@ -3328,6 +3404,7 @@ void
3328
3404
  ca_fill_addrs (void *ap, ca_size_t n, ca_size_t *addrs, void *ptr)
3329
3405
  {
3330
3406
  CArray *ca = (CArray *) ap;
3407
+ ca_check_stack_room();
3331
3408
 
3332
3409
  if ( ca_is_readonly(ca) ) {
3333
3410
  ca_refuse_read_only();
@@ -3354,6 +3431,7 @@ void *
3354
3431
  ca_clone (void *ap)
3355
3432
  {
3356
3433
  CArray *ca = (CArray *) ap;
3434
+ ca_check_stack_room();
3357
3435
  return ca_func[ca->obj_type].clone(ap);
3358
3436
  }
3359
3437
 
@@ -3377,23 +3455,24 @@ ca_fill (void *ap, void *aptr)
3377
3455
  #ifdef CARRAY_DEV_BUILD
3378
3456
 
3379
3457
  /* The Ruby attach surface is built into development builds only. Ruby has no
3380
- raw pointer, so the only thing a block can do inside an attach window is call
3381
- back into the array API -- and which door that takes is invisible in the
3382
- syntax: v[0] reads the attached buffer while v[] and v[0..1] compose past it
3383
- to the root (R5, see the contract beside ca_attach in carray.h). Per-cell and
3384
- whole-array writes reach the buffer and survive the sync; a write through a
3385
- derived view is discarded. The in-tree tests drive the healthy spellings, so
3386
- the surface stays reachable here and out of released builds. */
3458
+ raw pointer, so the only thing a block can do while an array is attached
3459
+ is call back into the array API, and the syntax does not show which
3460
+ of the two a call takes: v[0] reads the attached buffer while v[] and v[0..1]
3461
+ compose past it to the root (R5, guides/devel/04_attach_lifecycle.md).
3462
+ Per-cell and whole-array writes reach the buffer and survive the sync; a
3463
+ write through a derived view is discarded. The in-tree tests drive the
3464
+ healthy spellings, so the surface stays reachable here and out of
3465
+ released builds. */
3387
3466
 
3388
3467
  /* subroutines for CArray.attach, CArray.attach!
3389
3468
  CArray#attach, CArray#attach! */
3390
3469
 
3391
3470
  static void
3392
- rb_ca_attach_i (VALUE self)
3471
+ rb_ca_attach_i (volatile VALUE self)
3393
3472
  {
3394
3473
  CArray *ca;
3395
3474
  if ( rb_obj_is_carray(self) ) {
3396
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
3475
+ GetCArray(self, ca);
3397
3476
  ca_attach(ca);
3398
3477
  if ( ca_is_view(ca) ) {
3399
3478
  CAVIEW(ca)->nosync += 1;
@@ -3405,11 +3484,11 @@ rb_ca_attach_i (VALUE self)
3405
3484
  }
3406
3485
 
3407
3486
  static void
3408
- rb_ca_sync_i (VALUE self)
3487
+ rb_ca_sync_i (volatile VALUE self)
3409
3488
  {
3410
3489
  CArray *ca;
3411
3490
  if ( rb_obj_is_carray(self) ) {
3412
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
3491
+ GetCArray(self, ca);
3413
3492
  if ( ca_is_view(ca) ) {
3414
3493
  CAVIEW(ca)->nosync -= 1;
3415
3494
  ca_sync(ca);
@@ -3422,11 +3501,11 @@ rb_ca_sync_i (VALUE self)
3422
3501
  }
3423
3502
 
3424
3503
  static void
3425
- rb_ca_detach_i (VALUE self)
3504
+ rb_ca_detach_i (volatile VALUE self)
3426
3505
  {
3427
3506
  CArray *ca;
3428
3507
  if ( rb_obj_is_carray(self) ) {
3429
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
3508
+ GetCArray(self, ca);
3430
3509
  if ( ca_is_view(ca) ) { /* view array */
3431
3510
  CAVIEW(ca)->nosync -= 1;
3432
3511
  ca_detach(ca);
@@ -3468,7 +3547,7 @@ rb_ca_s_attach (int argc, VALUE *argv, VALUE self)
3468
3547
  volatile VALUE list, obj;
3469
3548
  int i;
3470
3549
 
3471
- list = rb_ary_new4(argc, argv);
3550
+ list = rb_ary_new_from_values(argc, argv);
3472
3551
 
3473
3552
  for (i=0; i<RARRAY_LEN(list); i++) {
3474
3553
  obj = rb_ary_entry(list, i);
@@ -3508,7 +3587,7 @@ rb_ca_s_attach_bang (int argc, VALUE *argv, VALUE self)
3508
3587
  volatile VALUE list, obj;
3509
3588
  int i;
3510
3589
 
3511
- list = rb_ary_new4(argc, argv);
3590
+ list = rb_ary_new_from_values(argc, argv);
3512
3591
 
3513
3592
  for (i=0; i<RARRAY_LEN(list); i++) {
3514
3593
  obj = rb_ary_entry(list, i);
@@ -3647,7 +3726,8 @@ rb_ca_members (VALUE self)
3647
3726
  {
3648
3727
  volatile VALUE data_class = rb_ca_data_class(self);
3649
3728
  if ( NIL_P(data_class) ) {
3650
- rb_raise(rb_eTypeError, "carray doesn't have data class");
3729
+ rb_raise(rb_eTypeError, "%s: carray doesn't have data class",
3730
+ ca_calling_method_name());
3651
3731
  }
3652
3732
  else {
3653
3733
  return rb_obj_clone(rb_const_get(data_class, rb_intern("MEMBERS")));
@@ -3661,7 +3741,7 @@ rb_ca_members (VALUE self)
3661
3741
  actual field view is the CAField on the parent. A plain FIXLEN entity
3662
3742
  keeps self == receiver. */
3663
3743
  static VALUE
3664
- ca_face_field_lookup (VALUE self, VALUE sym)
3744
+ ca_face_field_lookup (volatile VALUE self, volatile VALUE sym)
3665
3745
  {
3666
3746
  volatile VALUE data_class = rb_ca_data_class(self);
3667
3747
  volatile VALUE member;
@@ -3670,10 +3750,11 @@ ca_face_field_lookup (VALUE self, VALUE sym)
3670
3750
  CArray *ca;
3671
3751
 
3672
3752
  if ( NIL_P(data_class) ) {
3673
- rb_raise(rb_eTypeError, "carray doesn't have data class");
3753
+ rb_raise(rb_eTypeError, "%s: carray doesn't have data class",
3754
+ ca_calling_method_name());
3674
3755
  }
3675
3756
 
3676
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
3757
+ GetCArray(self, ca);
3677
3758
  receiver = ca_is_face(ca) ? rb_ca_parent(self) : self;
3678
3759
 
3679
3760
  member = rb_ivar_get(self, rb_intern("member"));
@@ -3699,7 +3780,7 @@ ca_face_field_lookup (VALUE self, VALUE sym)
3699
3780
  else {
3700
3781
  obj = rb_hash_aref(member, sym);
3701
3782
  if ( NIL_P(obj) ) {
3702
- sym = rb_funcall(sym, rb_intern("to_s"), 0);
3783
+ sym = rb_obj_as_string(sym);
3703
3784
  obj = rb_hash_aref(member, sym);
3704
3785
  }
3705
3786
  }
@@ -3712,10 +3793,11 @@ ca_face_field_lookup (VALUE self, VALUE sym)
3712
3793
  volatile VALUE info = rb_hash_aref(MEMBER_TABLE, sym);
3713
3794
  if ( NIL_P(info) ) {
3714
3795
  if ( TYPE(sym) != T_STRING ) {
3715
- sym = rb_funcall(sym, rb_intern("to_s"), 0);
3796
+ sym = rb_obj_as_string(sym);
3716
3797
  }
3717
3798
  rb_raise(rb_eArgError,
3718
- "can't find data_member named <%s>", StringValuePtr(sym));
3799
+ "%s: can't find data_member named <%s>",
3800
+ ca_calling_method_name(), StringValuePtr(sym));
3719
3801
  }
3720
3802
  Check_Type(info, T_ARRAY);
3721
3803
  /* Bit-typed members route through a CAField power-of-2 byte
@@ -3747,14 +3829,21 @@ ca_face_field_lookup (VALUE self, VALUE sym)
3747
3829
  else { view_bytes = 8; vtype_name = "uint64"; }
3748
3830
  (void) view_bytes;
3749
3831
  vtype_sym = ID2SYM(rb_intern(vtype_name));
3750
- word_view = rb_funcall(receiver, rb_intern("field"), 2,
3751
- SIZE2NUM(start_byte), vtype_sym);
3832
+ {
3833
+ VALUE args[2];
3834
+ args[0] = SIZE2NUM(start_byte);
3835
+ args[1] = vtype_sym;
3836
+ word_view = rb_ca_field(2, args, receiver);
3837
+ }
3752
3838
  /* little-endian on every host, as the layout numbers the bits */
3753
- word_view = rb_funcall(word_view, rb_intern("endian"), 1,
3754
- ID2SYM(rb_intern("little")));
3839
+ word_view = rb_ca_endian(word_view, ID2SYM(rb_intern("little")));
3755
3840
  range = rb_range_new(LONG2NUM(bit_in_word),
3756
3841
  SIZE2NUM(bit_in_word + bits - 1), 0);
3757
- obj = rb_funcall(word_view, rb_intern("bitfield"), 1, range);
3842
+ {
3843
+ VALUE args[1];
3844
+ args[0] = range;
3845
+ obj = rb_ca_bitfield(1, args, word_view);
3846
+ }
3758
3847
  rb_hash_aset(member, sym, obj);
3759
3848
  return obj;
3760
3849
  }
@@ -3769,22 +3858,19 @@ ca_face_field_lookup (VALUE self, VALUE sym)
3769
3858
  }
3770
3859
  if ( ! NIL_P(endian) ) {
3771
3860
  volatile VALUE field_opts = rb_hash_dup(opts);
3861
+ VALUE args[3];
3772
3862
  rb_hash_delete(field_opts, ID2SYM(rb_intern("endian")));
3773
- if ( RHASH_SIZE(field_opts) > 0 ) {
3774
- obj = rb_funcall(receiver, rb_intern("field"), 3,
3775
- rb_ary_entry(info, 0), rb_ary_entry(info, 1),
3776
- field_opts);
3777
- }
3778
- else {
3779
- obj = rb_funcall(receiver, rb_intern("field"), 2,
3780
- rb_ary_entry(info, 0), rb_ary_entry(info, 1));
3781
- }
3782
- obj = rb_funcall(obj, rb_intern("endian"), 1, endian);
3863
+ args[0] = rb_ary_entry(info, 0);
3864
+ args[1] = rb_ary_entry(info, 1);
3865
+ args[2] = field_opts;
3866
+ obj = rb_ca_field(RHASH_SIZE(field_opts) > 0 ? 3 : 2, args, receiver);
3867
+ obj = rb_ca_endian(obj, endian);
3783
3868
  rb_hash_aset(member, sym, obj);
3784
3869
  return obj;
3785
3870
  }
3786
3871
  }
3787
- obj = rb_apply(receiver, rb_intern("field"), info);
3872
+ obj = rb_ca_field(RARRAY_LENINT(info), (VALUE *) RARRAY_CONST_PTR(info),
3873
+ receiver);
3788
3874
  rb_hash_aset(member, sym, obj);
3789
3875
  return obj;
3790
3876
  }
@@ -3794,13 +3880,13 @@ ca_face_field_lookup (VALUE self, VALUE sym)
3794
3880
  read-only state of the record array (frozen, or a read-only view) is not
3795
3881
  on its parent chain; the field carries it itself. */
3796
3882
  VALUE
3797
- rb_ca_face_field (VALUE self, VALUE sym)
3883
+ rb_ca_face_field (volatile VALUE self, VALUE sym)
3798
3884
  {
3799
3885
  volatile VALUE obj = ca_face_field_lookup(self, sym);
3800
3886
  CArray *ca, *co;
3801
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
3887
+ GetCArray(self, ca);
3802
3888
  if ( ca_is_readonly(ca) && rb_obj_is_carray(obj) ) {
3803
- TypedData_Get_Struct(obj, CArray, &carray_data_type, co);
3889
+ GetCArray(obj, co);
3804
3890
  ca_set_flag(co, CA_FLAG_READ_ONLY);
3805
3891
  }
3806
3892
  return obj;
@@ -3818,10 +3904,11 @@ rb_ca_fields (VALUE self)
3818
3904
  volatile VALUE member_names, list;
3819
3905
  int i;
3820
3906
  if ( NIL_P(data_class) ) {
3821
- rb_raise(rb_eTypeError, "carray doesn't have data class");
3907
+ rb_raise(rb_eTypeError, "%s: carray doesn't have data class",
3908
+ ca_calling_method_name());
3822
3909
  }
3823
3910
  member_names = rb_const_get(data_class, rb_intern("MEMBERS"));
3824
- list = rb_ary_new2(RARRAY_LEN(member_names));
3911
+ list = rb_ary_new_capa(RARRAY_LEN(member_names));
3825
3912
  for (i=0; i<RARRAY_LEN(member_names); i++) {
3826
3913
  VALUE name = rb_ary_entry(member_names, i);
3827
3914
  rb_ary_store(list, i, rb_ca_face_field(self, name));
@@ -3840,11 +3927,13 @@ rb_ca_fields_at (int argc, VALUE *argv, VALUE self)
3840
3927
  volatile VALUE data_class = rb_ca_data_class(self);
3841
3928
  volatile VALUE member_names, list;
3842
3929
  int i;
3930
+ rb_reject_options(rb_pop_options(&argc, &argv));
3843
3931
  if ( NIL_P(data_class) ) {
3844
- rb_raise(rb_eTypeError, "carray doesn't have data class");
3932
+ rb_raise(rb_eTypeError, "%s: carray doesn't have data class",
3933
+ ca_calling_method_name());
3845
3934
  }
3846
- member_names = rb_ary_new4(argc, argv);
3847
- list = rb_ary_new2(RARRAY_LEN(member_names));
3935
+ member_names = rb_ary_new_from_values(argc, argv);
3936
+ list = rb_ary_new_capa(RARRAY_LEN(member_names));
3848
3937
  for (i=0; i<RARRAY_LEN(member_names); i++) {
3849
3938
  VALUE name = rb_ary_entry(member_names, i);
3850
3939
  rb_ary_store(list, i, rb_ca_face_field(self, name));
@@ -3867,6 +3956,8 @@ Init_carray_core (void)
3867
3956
  rb_undef_alloc_func(rb_cCAWindowGuard);
3868
3957
  rb_undef_method(CLASS_OF(rb_cCAWindowGuard), "new");
3869
3958
  rb_define_method(rb_cCAWindowGuard, "call", ca_window_guard_call, 1);
3959
+ rb_funcall(rb_cCArray, rb_intern("private_constant"), 1,
3960
+ ID2SYM(rb_intern("WindowGuard")));
3870
3961
 
3871
3962
  #ifdef CARRAY_DEV_BUILD
3872
3963
  rb_define_singleton_method(rb_cCArray, "attach", rb_ca_s_attach, -1);