Revert "Remove with_gc functions in darray"

This reverts commit 24a740796050b72aa2d35339ba2a317d4eda7b75.
This commit is contained in:
Peter Zhu 2024-12-24 15:13:27 -05:00
parent f1096c6ad4
commit f9cd9a1b55
Notes: git 2025-01-02 16:03:27 +00:00
2 changed files with 65 additions and 25 deletions

View File

@ -41,10 +41,17 @@
*
* void rb_darray_append(rb_darray(T) *ptr_to_ary, T element);
*/
#define rb_darray_append(ptr_to_ary, element) do { \
#define rb_darray_append(ptr_to_ary, element) \
rb_darray_append_impl(ptr_to_ary, element, rb_xrealloc_mul_add)
#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 { \
rb_darray_ensure_space((ptr_to_ary), \
sizeof(**(ptr_to_ary)), \
sizeof((*(ptr_to_ary))->data[0])); \
sizeof((*(ptr_to_ary))->data[0]), \
realloc_func); \
rb_darray_set(*(ptr_to_ary), \
(*(ptr_to_ary))->meta.size, \
(element)); \
@ -82,15 +89,21 @@
* 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_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)
/* 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(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_resize_capa_without_gc(ptr_to_ary, capa) \
rb_darray_resize_capa_impl((ptr_to_ary), capa, sizeof(**(ptr_to_ary)), \
sizeof((*(ptr_to_ary))->data[0]), rb_darray_realloc_mul_add_without_gc)
#define rb_darray_data_ptr(ary) ((ary)->data)
@ -143,15 +156,48 @@ rb_darray_free(void *ary)
xfree(ary);
}
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. 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)
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_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 = xrealloc(meta, new_capa * element_size + header_size);
rb_darray_meta_t *new_ary = realloc_mul_add_impl(meta, new_capa, element_size, header_size);
if (meta == NULL) {
/* First allocation. Initialize size. On subsequence allocations
@ -172,7 +218,8 @@ 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)
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_meta_t **ptr_to_ptr_to_meta = ptr_to_ary;
rb_darray_meta_t *meta = *ptr_to_ptr_to_meta;
@ -182,11 +229,12 @@ 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);
rb_darray_resize_capa_impl(ptr_to_ary, new_capa, header_size, element_size, realloc_mul_add_impl);
}
static inline void
rb_darray_make_impl(void *ptr_to_ary, size_t array_size, size_t header_size, size_t element_size)
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_meta_t **ptr_to_ptr_to_meta = ptr_to_ary;
if (array_size == 0) {
@ -194,7 +242,7 @@ rb_darray_make_impl(void *ptr_to_ary, size_t array_size, size_t header_size, siz
return;
}
rb_darray_meta_t *meta = xcalloc(array_size * element_size + header_size, 1);
rb_darray_meta_t *meta = calloc_mul_add_impl(array_size, element_size, header_size);
meta->size = array_size;
meta->capa = array_size;

View File

@ -4562,11 +4562,7 @@ rb_gc_impl_mark_weak(void *objspace_ptr, VALUE *ptr)
rgengc_check_relation(objspace, obj);
DURING_GC_COULD_MALLOC_REGION_START();
{
rb_darray_append(&objspace->weak_references, ptr);
}
DURING_GC_COULD_MALLOC_REGION_END();
rb_darray_append_without_gc(&objspace->weak_references, ptr);
objspace->profile.weak_references_count++;
}
@ -5381,11 +5377,7 @@ gc_update_weak_references(rb_objspace_t *objspace)
objspace->profile.retained_weak_references_count = retained_weak_references_count;
rb_darray_clear(objspace->weak_references);
DURING_GC_COULD_MALLOC_REGION_START();
{
rb_darray_resize_capa(&objspace->weak_references, retained_weak_references_count);
}
DURING_GC_COULD_MALLOC_REGION_END();
rb_darray_resize_capa_without_gc(&objspace->weak_references, retained_weak_references_count);
}
static void
@ -9245,7 +9237,7 @@ rb_gc_impl_objspace_free(void *objspace_ptr)
for (size_t i = 0; i < rb_darray_size(objspace->heap_pages.sorted); i++) {
heap_page_free(objspace, rb_darray_get(objspace->heap_pages.sorted, i));
}
rb_darray_free(objspace->heap_pages.sorted);
rb_darray_free_without_gc(objspace->heap_pages.sorted);
heap_pages_lomem = 0;
heap_pages_himem = 0;
@ -9261,7 +9253,7 @@ rb_gc_impl_objspace_free(void *objspace_ptr)
free_stack_chunks(&objspace->mark_stack);
mark_stack_free_cache(&objspace->mark_stack);
rb_darray_free(objspace->weak_references);
rb_darray_free_without_gc(objspace->weak_references);
free(objspace);
}
@ -9333,8 +9325,8 @@ rb_gc_impl_objspace_init(void *objspace_ptr)
ccan_list_head_init(&heap->pages);
}
rb_darray_make(&objspace->heap_pages.sorted, 0);
rb_darray_make(&objspace->weak_references, 0);
rb_darray_make_without_gc(&objspace->heap_pages.sorted, 0);
rb_darray_make_without_gc(&objspace->weak_references, 0);
// TODO: debug why on Windows Ruby crashes on boot when GC is on.
#ifdef _WIN32