Remove with_gc functions in darray

We can wrap in DURING_GC_COULD_MALLOC_REGION instead.
This commit is contained in:
Peter Zhu 2024-04-01 14:52:52 -04:00
parent 3c4de946c9
commit 24a7407960
2 changed files with 25 additions and 69 deletions

View File

@ -45,16 +45,12 @@
* void rb_darray_append(rb_darray(T) *ptr_to_ary, T element);
*/
#define rb_darray_append(ptr_to_ary, element) \
rb_darray_append_impl(ptr_to_ary, element, rb_xrealloc_mul_add)
rb_darray_append_impl(ptr_to_ary, element)
#define rb_darray_append_without_gc(ptr_to_ary, element) \
rb_darray_append_impl(ptr_to_ary, element, rb_darray_realloc_mul_add_without_gc)
#define rb_darray_append_impl(ptr_to_ary, element, realloc_func) do { \
#define rb_darray_append_impl(ptr_to_ary, element) do { \
rb_darray_ensure_space((ptr_to_ary), \
sizeof(**(ptr_to_ary)), \
sizeof((*(ptr_to_ary))->data[0]), \
realloc_func); \
sizeof((*(ptr_to_ary))->data[0])); \
rb_darray_set(*(ptr_to_ary), \
(*(ptr_to_ary))->meta.size, \
(element)); \
@ -79,21 +75,15 @@
* void rb_darray_make(rb_darray(T) *ptr_to_ary, size_t size);
*/
#define rb_darray_make(ptr_to_ary, size) \
rb_darray_make_impl((ptr_to_ary), size, sizeof(**(ptr_to_ary)), \
sizeof((*(ptr_to_ary))->data[0]), rb_xcalloc_mul_add)
#define rb_darray_make_without_gc(ptr_to_ary, size) \
rb_darray_make_impl((ptr_to_ary), size, sizeof(**(ptr_to_ary)), \
sizeof((*(ptr_to_ary))->data[0]), rb_darray_calloc_mul_add_without_gc)
rb_darray_make_impl((ptr_to_ary), size, sizeof(**(ptr_to_ary)), sizeof((*(ptr_to_ary))->data[0]))
/* Resize the darray to a new capacity. The new capacity must be greater than
* or equal to the size of the darray.
*
* void rb_darray_resize_capa(rb_darray(T) *ptr_to_ary, size_t capa);
*/
#define rb_darray_resize_capa_without_gc(ptr_to_ary, capa) \
rb_darray_resize_capa_impl((ptr_to_ary), rb_darray_next_power_of_two(capa), sizeof(**(ptr_to_ary)), \
sizeof((*(ptr_to_ary))->data[0]), rb_darray_realloc_mul_add_without_gc)
#define rb_darray_resize_capa(ptr_to_ary, capa) \
rb_darray_resize_capa_impl((ptr_to_ary), capa, sizeof(**(ptr_to_ary)), sizeof((*(ptr_to_ary))->data[0]))
#define rb_darray_data_ptr(ary) ((ary)->data)
@ -139,56 +129,15 @@ rb_darray_free(void *ary)
if (meta) ruby_sized_xfree(ary, meta->capa);
}
static inline void
rb_darray_free_without_gc(void *ary)
{
free(ary);
}
/* Internal function. Like rb_xcalloc_mul_add but does not trigger GC and does
* not check for overflow in arithmetic. */
static inline void *
rb_darray_calloc_mul_add_without_gc(size_t x, size_t y, size_t z)
{
size_t size = (x * y) + z;
void *ptr = calloc(1, size);
if (ptr == NULL) rb_bug("rb_darray_calloc_mul_add_without_gc: failed");
return ptr;
}
/* Internal function. Like rb_xrealloc_mul_add but does not trigger GC and does
* not check for overflow in arithmetic. */
static inline void *
rb_darray_realloc_mul_add_without_gc(const void *orig_ptr, size_t x, size_t y, size_t z)
{
size_t size = (x * y) + z;
void *ptr = realloc((void *)orig_ptr, size);
if (ptr == NULL) rb_bug("rb_darray_realloc_mul_add_without_gc: failed");
return ptr;
}
/* Internal function. Returns the next power of two that is greater than or
* equal to n. */
static inline size_t
rb_darray_next_power_of_two(size_t n)
{
return (size_t)(1 << (64 - nlz_int64(n)));
}
/* Internal function. Resizes the capacity of a darray. The new capacity must
* be greater than or equal to the size of the darray. */
static inline void
rb_darray_resize_capa_impl(void *ptr_to_ary, size_t new_capa, size_t header_size, size_t element_size,
void *(*realloc_mul_add_impl)(const void *, size_t, size_t, size_t))
rb_darray_resize_capa_impl(void *ptr_to_ary, size_t new_capa, size_t header_size, size_t element_size)
{
rb_darray_meta_t **ptr_to_ptr_to_meta = ptr_to_ary;
rb_darray_meta_t *meta = *ptr_to_ptr_to_meta;
rb_darray_meta_t *new_ary = realloc_mul_add_impl(meta, new_capa, element_size, header_size);
rb_darray_meta_t *new_ary = rb_xrealloc_mul_add(meta, new_capa, element_size, header_size);
if (meta == NULL) {
/* First allocation. Initialize size. On subsequence allocations
@ -209,8 +158,7 @@ rb_darray_resize_capa_impl(void *ptr_to_ary, size_t new_capa, size_t header_size
// Ensure there is space for one more element.
// Note: header_size can be bigger than sizeof(rb_darray_meta_t) when T is __int128_t, for example.
static inline void
rb_darray_ensure_space(void *ptr_to_ary, size_t header_size, size_t element_size,
void *(*realloc_mul_add_impl)(const void *, size_t, size_t, size_t))
rb_darray_ensure_space(void *ptr_to_ary, size_t header_size, size_t element_size)
{
rb_darray_meta_t **ptr_to_ptr_to_meta = ptr_to_ary;
rb_darray_meta_t *meta = *ptr_to_ptr_to_meta;
@ -220,12 +168,11 @@ rb_darray_ensure_space(void *ptr_to_ary, size_t header_size, size_t element_size
// Double the capacity
size_t new_capa = current_capa == 0 ? 1 : current_capa * 2;
rb_darray_resize_capa_impl(ptr_to_ary, new_capa, header_size, element_size, realloc_mul_add_impl);
rb_darray_resize_capa_impl(ptr_to_ary, new_capa, header_size, element_size);
}
static inline void
rb_darray_make_impl(void *ptr_to_ary, size_t array_size, size_t header_size, size_t element_size,
void *(*calloc_mul_add_impl)(size_t, size_t, size_t))
rb_darray_make_impl(void *ptr_to_ary, size_t array_size, size_t header_size, size_t element_size)
{
rb_darray_meta_t **ptr_to_ptr_to_meta = ptr_to_ary;
if (array_size == 0) {
@ -233,7 +180,7 @@ rb_darray_make_impl(void *ptr_to_ary, size_t array_size, size_t header_size, siz
return;
}
rb_darray_meta_t *meta = calloc_mul_add_impl(array_size, element_size, header_size);
rb_darray_meta_t *meta = rb_xcalloc_mul_add(array_size, element_size, header_size);
meta->size = array_size;
meta->capa = array_size;

17
gc.c
View File

@ -1908,7 +1908,7 @@ rb_objspace_alloc(void)
ccan_list_head_init(&SIZE_POOL_TOMB_HEAP(size_pool)->pages);
}
rb_darray_make_without_gc(&objspace->weak_references, 0);
rb_darray_make(&objspace->weak_references, 0);
// TODO: debug why on Windows Ruby crashes on boot when GC is on.
#ifdef _WIN32
@ -1955,7 +1955,7 @@ rb_objspace_free(rb_objspace_t *objspace)
free_stack_chunks(&objspace->mark_stack);
mark_stack_free_cache(&objspace->mark_stack);
rb_darray_free_without_gc(objspace->weak_references);
rb_darray_free(objspace->weak_references);
free(objspace);
}
@ -6777,7 +6777,11 @@ rb_gc_mark_weak(VALUE *ptr)
rgengc_check_relation(objspace, obj);
rb_darray_append_without_gc(&objspace->weak_references, ptr);
DURING_GC_COULD_MALLOC_REGION_START();
{
rb_darray_append(&objspace->weak_references, ptr);
}
DURING_GC_COULD_MALLOC_REGION_END();
objspace->profile.weak_references_count++;
}
@ -7942,7 +7946,12 @@ gc_update_weak_references(rb_objspace_t *objspace)
objspace->profile.retained_weak_references_count = retained_weak_references_count;
rb_darray_clear(objspace->weak_references);
rb_darray_resize_capa_without_gc(&objspace->weak_references, retained_weak_references_count);
DURING_GC_COULD_MALLOC_REGION_START();
{
rb_darray_resize_capa(&objspace->weak_references, retained_weak_references_count);
}
DURING_GC_COULD_MALLOC_REGION_END();
}
static void