From 1d8ed2c5bb50d20c8903bb96f017b43a62b32ef2 Mon Sep 17 00:00:00 2001 From: Ivan Maidanski Date: Fri, 24 Mar 2023 08:35:46 +0300 Subject: Avoid code duplication in IGNORE_OFF_PAGE-specific malloc functions (refactoring) * alloc.c: Update comment regarding GC_generic_malloc_inner usage. * alloc.c (GC_collect_or_expand): Replace ignore_off_page argument to flags (check IGNORE_OFF_PAGE bit only in it); update comment. * alloc.c (GC_allocobj): Pass 0 as flags to GC_collect_or_expand. * dbg_mlc.c (GC_debug_generic_malloc, GC_debug_generic_or_special_malloc): Rename knd argument to k. * mallocx.c (GC_generic_or_special_malloc): Likewise. * dbg_mlc.c (GC_debug_generic_malloc): Use GC_generic_malloc_aligned() instead of GC_generic_malloc(). * dbg_mlc.c (GC_debug_generic_malloc_inner): Add flags argument. * gcj_mlc.c [GC_GCJ_SUPPORT] (GC_core_gcj_malloc): Likewise. * include/private/gc_priv.h (C_generic_malloc_aligned, GC_generic_malloc_inner): Likewise. * include/private/gc_priv.h [THREAD_LOCAL_ALLOC && GC_GCJ_SUPPORT] (GC_core_gcj_malloc): Likewise. * include/private/gc_priv.h [DBG_HDRS_ALL] (GC_debug_generic_malloc_inner): Likewise. * malloc.c (GC_generic_malloc_inner, GC_generic_malloc_aligned): Likewise. * dbg_mlc.c (GC_debug_generic_malloc_inner): Use GC_generic_malloc_inner() instead of GC_generic_malloc_inner_ignore_off_page(). * dbg_mlc.c [DBG_HDRS_ALL] (GC_debug_generic_malloc_inner_ignore_off_page): Remove GC_INNER function. * include/private/gc_priv.h [DBG_HDRS_ALL || GC_GCJ_SUPPORT || !GC_NO_FINALIZATION] (GC_generic_malloc_inner_ignore_off_page): Likewise. * include/private/gc_priv.h [DBG_HDRS_ALL] (GC_debug_generic_malloc_inner_ignore_off_page): Likewise. * malloc.c [DBG_HDRS_ALL || GC_GCJ_SUPPORT || !GC_NO_FINALIZATION] (GC_generic_malloc_inner_ignore_off_page): Likewise. * gcj_mlc.c [GC_GCJ_SUPPORT && !THREAD_LOCAL_ALLOC] (GC_gcj_malloc): Define as STATIC GC_core_gcj_malloc. * gcj_mlc.c [GC_GCJ_SUPPORT] (GC_core_gcj_malloc): Reformat comment; pass flags to GC_generic_malloc_inner(). * gcj_mlc.c [GC_GCJ_SUPPORT && !THREAD_LOCAL_ALLOC] (GC_gcj_malloc): Redirect to GC_core_gcj_malloc() passing 0 to flags argument. * thread_local_alloc.c [THREAD_LOCAL_ALLOC && GC_GCJ_SUPPORT] (GC_gcj_malloc): Likewise. * gcj_mlc.c [GC_GCJ_SUPPORT] (GC_gcj_malloc_ignore_off_page): Redirect to GC_core_gcj_malloc() passing IGNORE_OFF_PAGE. * gcj_mlc.c [GC_GCJ_SUPPORT] (GC_debug_gcj_malloc): Pass 0 as flags to GC_generic_malloc_inner(). * include/private/gc_priv.h (GC_generic_malloc_inner): Update comment. * mallocx.c (GC_generic_malloc_many): Likewise. * include/private/gc_priv.h (GC_collect_or_expand): Replace GC_bool ignore_off_page argument to unsigned flags. * include/private/gc_priv.h (GC_INTERNAL_MALLOC, GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE): Specify macro arguments. * include/private/gc_priv.h (GC_INTERNAL_MALLOC): Pass 0 as flags argument to GC_[debug_]generic_malloc_inner(). * include/private/gc_priv.h (GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE): Pass IGNORE_OFF_PAGE to GC_[debug_]generic_malloc_inner(). * malloc.c (GC_alloc_large): Pass flags (instead of flags!=0) to GC_collect_or_expand(). * malloc.c (GC_generic_malloc_inner_small): New STATIC function (move most of code from GC_generic_malloc_inner). * malloc.c (GC_generic_malloc_inner): Move comment to gc_priv.h; call GC_generic_malloc_inner_small(). * malloc.c (GC_generic_malloc_aligned): Call GC_generic_malloc_inner_small() instead of GC_generic_malloc_inner(); pass flags (instead of 0) to GC_alloc_large(); do not cast result of GC_alloc_large() to ptr_t. * malloc.c (GC_generic_malloc): Pass 0 as flags to GC_generic_malloc_aligned(). * malloc.c (GC_memalign): Likewise. * malloc.c (GC_malloc_kind_global, GC_generic_malloc_uncollectable): Call GC_generic_malloc_aligned() instead of GC_generic_malloc(). * mallocx.c (GC_generic_malloc_many): Likewise. * malloc.c (free_internal): Rename knd local variable to k. * mallocx.c (GC_generic_malloc_ignore_off_page, GC_malloc_ignore_off_page, GC_malloc_atomic_ignore_off_page): Redirect to GC_generic_malloc_aligned() passing IGNORE_OFF_PAGE. * typd_mlc.c (GC_generic_malloc_ignore_off_page): Likewise. * tests/gctest.c (run_one_test): Call GC_generic_malloc_ignore_off_page() (with size larger than HBLKSIZE); increment collectable_count. --- gcj_mlc.c | 59 ++++++++++++++++++++++++----------------------------------- 1 file changed, 24 insertions(+), 35 deletions(-) (limited to 'gcj_mlc.c') diff --git a/gcj_mlc.c b/gcj_mlc.c index 9ce81623..988a59c9 100644 --- a/gcj_mlc.c +++ b/gcj_mlc.c @@ -132,15 +132,15 @@ static void maybe_finalize(void) } /* Allocate an object, clear it, and store the pointer to the */ -/* type structure (vtable in gcj). */ -/* This adds a byte at the end of the object if GC_malloc would.*/ +/* type structure (vtable in gcj). This adds a byte at the */ +/* end of the object if GC_malloc would. */ #ifdef THREAD_LOCAL_ALLOC - GC_INNER void * GC_core_gcj_malloc(size_t lb, - void * ptr_to_struct_containing_descr) + GC_INNER #else - GC_API GC_ATTR_MALLOC void * GC_CALL GC_gcj_malloc(size_t lb, - void * ptr_to_struct_containing_descr) + STATIC #endif +void * GC_core_gcj_malloc(size_t lb, void * ptr_to_struct_containing_descr, + unsigned flags) { ptr_t op; size_t lg; @@ -154,7 +154,8 @@ static void maybe_finalize(void) GC_ASSERT(NULL == ((void **)op)[1]); } else { maybe_finalize(); - op = (ptr_t)GC_clear_stack(GC_generic_malloc_inner(lb, GC_gcj_kind)); + op = (ptr_t)GC_clear_stack(GC_generic_malloc_inner(lb, GC_gcj_kind, + flags)); if (NULL == op) { GC_oom_func oom_fn = GC_oom_fn; UNLOCK(); @@ -168,6 +169,21 @@ static void maybe_finalize(void) return (void *)op; } +#ifndef THREAD_LOCAL_ALLOC + GC_API GC_ATTR_MALLOC void * GC_CALL GC_gcj_malloc(size_t lb, + void * ptr_to_struct_containing_descr) + { + return GC_core_gcj_malloc(lb, ptr_to_struct_containing_descr, 0); + } +#endif /* !THREAD_LOCAL_ALLOC */ + +GC_API GC_ATTR_MALLOC void * GC_CALL GC_gcj_malloc_ignore_off_page(size_t lb, + void * ptr_to_struct_containing_descr) +{ + return GC_core_gcj_malloc(lb, ptr_to_struct_containing_descr, + IGNORE_OFF_PAGE); +} + /* Similar to GC_gcj_malloc, but add debug info. This is allocated */ /* with GC_gcj_debug_kind. */ GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_gcj_malloc(size_t lb, @@ -180,7 +196,7 @@ GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_gcj_malloc(size_t lb, LOCK(); maybe_finalize(); result = GC_generic_malloc_inner(SIZET_SAT_ADD(lb, DEBUG_BYTES), - GC_gcj_debug_kind); + GC_gcj_debug_kind, 0 /* flags */); if (NULL == result) { GC_oom_func oom_fn = GC_oom_fn; UNLOCK(); @@ -200,31 +216,4 @@ GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_gcj_malloc(size_t lb, return result; } -GC_API GC_ATTR_MALLOC void * GC_CALL GC_gcj_malloc_ignore_off_page(size_t lb, - void * ptr_to_struct_containing_descr) -{ - ptr_t op; - - /* For small objects, the ignore_off_page variant of */ - /* GC_generic_malloc_inner does not differ from the ordinary one. */ - if (SMALL_OBJ(lb)) - return GC_gcj_malloc(lb, ptr_to_struct_containing_descr); - - GC_DBG_COLLECT_AT_MALLOC(lb); - LOCK(); - maybe_finalize(); - op = (ptr_t)GC_clear_stack(GC_generic_malloc_inner_ignore_off_page(lb, - GC_gcj_kind)); - if (NULL == op) { - GC_oom_func oom_fn = GC_oom_fn; - UNLOCK(); - return (*oom_fn)(lb); - } - *(void **)op = ptr_to_struct_containing_descr; - UNLOCK(); - GC_dirty(op); - REACHABLE_AFTER_DIRTY(ptr_to_struct_containing_descr); - return (void *)op; -} - #endif /* GC_GCJ_SUPPORT */ -- cgit v1.2.1