Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove with_gc functions in darray #10424

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
77 changes: 12 additions & 65 deletions darray.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)); \
Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -220,20 +168,19 @@ 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) {
*ptr_to_ptr_to_meta = NULL;
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;
Expand Down
17 changes: 13 additions & 4 deletions gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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++;
}
Expand Down Expand Up @@ -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
Expand Down