From 32b12154933124a899dae65c1e2a993e814e60c1 Mon Sep 17 00:00:00 2001 From: Ivan Maidanski Date: Sun, 15 Dec 2013 20:36:45 +0400 Subject: [PATCH] Remove 'opp' local variable in GC_malloc_X (code refactoring) * alloc.c: Update comment (about free lists). * fnlz_mlc.c (GC_finalized_malloc): Remove "opp" local variable (replace *opp with freelist[lg]). * gcj_mlc.c (GC_gcj_malloc, GC_gcj_malloc_ignore_off_page): Likewise. * malloc.c (GC_malloc_atomic, GC_malloc, GC_malloc_uncollectable): Likewise. * mallocx.c (GC_malloc_atomic_uncollectable): Likewise. * typd_mlc.c (GC_malloc_explicitly_typed, GC_malloc_explicitly_typed_ignore_off_page, GC_calloc_explicitly_typed): Likewise. --- alloc.c | 13 ++++++++----- fnlz_mlc.c | 6 ++---- gcj_mlc.c | 14 +++++--------- malloc.c | 26 +++++++++++--------------- mallocx.c | 10 ++++------ typd_mlc.c | 20 +++++++------------- 6 files changed, 37 insertions(+), 52 deletions(-) diff --git a/alloc.c b/alloc.c index b9039e09..f0b1c11f 100644 --- a/alloc.c +++ b/alloc.c @@ -32,12 +32,15 @@ * kind k objects of size i points to a non-empty * free list. It returns a pointer to the first entry on the free list. * In a single-threaded world, GC_allocobj may be called to allocate - * an object of (small) size i as follows: + * an object of (small) size lb as follows: * - * opp = &(GC_objfreelist[i]); - * if (*opp == 0) GC_allocobj(i, NORMAL); - * ptr = *opp; - * *opp = obj_link(ptr); + * lg = GC_size_map[lb]; + * op = GC_objfreelist[lg]; + * if (NULL == op) { + * op = GENERAL_MALLOC(lb, NORMAL); + * } else { + * GC_objfreelist[lg] = obj_link(op); + * } * * Note that this is very fast if the free list is non-empty; it should * only involve the execution of 4 or 5 simple instructions. diff --git a/fnlz_mlc.c b/fnlz_mlc.c index 0dad28a6..923810c8 100644 --- a/fnlz_mlc.c +++ b/fnlz_mlc.c @@ -86,7 +86,6 @@ GC_API void GC_CALL GC_register_disclaim_proc(int kind, GC_disclaim_proc proc, #endif { ptr_t op; - ptr_t *opp; word lg; DCL_LOCK_STATE; @@ -95,9 +94,8 @@ GC_API void GC_CALL GC_register_disclaim_proc(int kind, GC_disclaim_proc proc, if (SMALL_OBJ(lb)) { GC_DBG_COLLECT_AT_MALLOC(lb); lg = GC_size_map[lb]; - opp = &GC_finalized_objfreelist[lg]; LOCK(); - op = *opp; + op = GC_finalized_objfreelist[lg]; if (EXPECT(0 == op, FALSE)) { UNLOCK(); op = GC_generic_malloc((word)lb, GC_finalized_kind); @@ -106,7 +104,7 @@ GC_API void GC_CALL GC_register_disclaim_proc(int kind, GC_disclaim_proc proc, /* GC_generic_malloc has extended the size map for us. */ lg = GC_size_map[lb]; } else { - *opp = obj_link(op); + GC_finalized_objfreelist[lg] = obj_link(op); obj_link(op) = 0; GC_bytes_allocd += GRANULES_TO_BYTES(lg); UNLOCK(); diff --git a/gcj_mlc.c b/gcj_mlc.c index 78950e25..c2aee253 100644 --- a/gcj_mlc.c +++ b/gcj_mlc.c @@ -164,17 +164,15 @@ static void maybe_finalize(void) #endif { ptr_t op; - ptr_t * opp; word lg; DCL_LOCK_STATE; GC_DBG_COLLECT_AT_MALLOC(lb); if(SMALL_OBJ(lb)) { lg = GC_size_map[lb]; - opp = &(GC_gcjobjfreelist[lg]); LOCK(); - op = *opp; - if(EXPECT(op == 0, FALSE)) { + op = GC_gcjobjfreelist[lg]; + if(EXPECT(0 == op, FALSE)) { maybe_finalize(); op = (ptr_t)GENERAL_MALLOC_INNER((word)lb, GC_gcj_kind); if (0 == op) { @@ -183,7 +181,7 @@ static void maybe_finalize(void) return((*oom_fn)(lb)); } } else { - *opp = obj_link(op); + GC_gcjobjfreelist[lg] = obj_link(op); GC_bytes_allocd += GRANULES_TO_BYTES(lg); } *(void **)op = ptr_to_struct_containing_descr; @@ -238,16 +236,14 @@ GC_API void * GC_CALL GC_gcj_malloc_ignore_off_page(size_t lb, void * ptr_to_struct_containing_descr) { ptr_t op; - ptr_t * opp; word lg; DCL_LOCK_STATE; GC_DBG_COLLECT_AT_MALLOC(lb); if(SMALL_OBJ(lb)) { lg = GC_size_map[lb]; - opp = &(GC_gcjobjfreelist[lg]); LOCK(); - op = *opp; + op = GC_gcjobjfreelist[lg]; if (EXPECT(0 == op, FALSE)) { maybe_finalize(); op = (ptr_t)GENERAL_MALLOC_INNER_IOP(lb, GC_gcj_kind); @@ -257,7 +253,7 @@ GC_API void * GC_CALL GC_gcj_malloc_ignore_off_page(size_t lb, return((*oom_fn)(lb)); } } else { - *opp = obj_link(op); + GC_gcjobjfreelist[lg] = obj_link(op); GC_bytes_allocd += GRANULES_TO_BYTES(lg); } } else { diff --git a/malloc.c b/malloc.c index 4983cdfe..11bc8ce8 100644 --- a/malloc.c +++ b/malloc.c @@ -230,25 +230,24 @@ GC_API void * GC_CALL GC_generic_malloc(size_t lb, int k) #endif { void *op; - void ** opp; size_t lg; DCL_LOCK_STATE; if(SMALL_OBJ(lb)) { GC_DBG_COLLECT_AT_MALLOC(lb); lg = GC_size_map[lb]; - opp = &(GC_aobjfreelist[lg]); LOCK(); - if (EXPECT((op = *opp) == 0, FALSE)) { + op = GC_aobjfreelist[lg]; + if (EXPECT(0 == op, FALSE)) { UNLOCK(); return(GENERAL_MALLOC((word)lb, PTRFREE)); } - *opp = obj_link(op); + GC_aobjfreelist[lg] = obj_link(op); GC_bytes_allocd += GRANULES_TO_BYTES(lg); UNLOCK(); return((void *) op); } else { - return(GENERAL_MALLOC((word)lb, PTRFREE)); + return(GENERAL_MALLOC((word)lb, PTRFREE)); } } @@ -260,16 +259,15 @@ GC_API void * GC_CALL GC_generic_malloc(size_t lb, int k) #endif { void *op; - void **opp; size_t lg; DCL_LOCK_STATE; if(SMALL_OBJ(lb)) { GC_DBG_COLLECT_AT_MALLOC(lb); lg = GC_size_map[lb]; - opp = (void **)&(GC_objfreelist[lg]); LOCK(); - if (EXPECT((op = *opp) == 0, FALSE)) { + op = GC_objfreelist[lg]; + if (EXPECT(0 == op, FALSE)) { UNLOCK(); return (GENERAL_MALLOC((word)lb, NORMAL)); } @@ -278,7 +276,7 @@ GC_API void * GC_CALL GC_generic_malloc(size_t lb, int k) <= (word)GC_greatest_plausible_heap_addr && (word)obj_link(op) >= (word)GC_least_plausible_heap_addr)); - *opp = obj_link(op); + GC_objfreelist[lg] = obj_link(op); obj_link(op) = 0; GC_bytes_allocd += GRANULES_TO_BYTES(lg); UNLOCK(); @@ -292,21 +290,19 @@ GC_API void * GC_CALL GC_generic_malloc(size_t lb, int k) GC_API void * GC_CALL GC_malloc_uncollectable(size_t lb) { void *op; - void **opp; size_t lg; DCL_LOCK_STATE; - if( SMALL_OBJ(lb) ) { + if (SMALL_OBJ(lb)) { GC_DBG_COLLECT_AT_MALLOC(lb); if (EXTRA_BYTES != 0 && lb != 0) lb--; /* We don't need the extra byte, since this won't be */ /* collected anyway. */ lg = GC_size_map[lb]; - opp = &(GC_uobjfreelist[lg]); LOCK(); - op = *opp; - if (EXPECT(0 != op, TRUE)) { - *opp = obj_link(op); + op = GC_uobjfreelist[lg]; + if (EXPECT(op != 0, TRUE)) { + GC_uobjfreelist[lg] = obj_link(op); obj_link(op) = 0; GC_bytes_allocd += GRANULES_TO_BYTES(lg); /* Mark bit ws already set on free list. It will be */ diff --git a/mallocx.c b/mallocx.c index 7fcaa5c2..eb96124c 100644 --- a/mallocx.c +++ b/mallocx.c @@ -508,21 +508,19 @@ GC_API int GC_CALL GC_posix_memalign(void **memptr, size_t align, size_t lb) GC_API void * GC_CALL GC_malloc_atomic_uncollectable(size_t lb) { void *op; - void **opp; size_t lg; DCL_LOCK_STATE; - if( SMALL_OBJ(lb) ) { + if (SMALL_OBJ(lb)) { GC_DBG_COLLECT_AT_MALLOC(lb); if (EXTRA_BYTES != 0 && lb != 0) lb--; /* We don't need the extra byte, since this won't be */ /* collected anyway. */ lg = GC_size_map[lb]; - opp = &(GC_auobjfreelist[lg]); LOCK(); - op = *opp; - if (EXPECT(0 != op, TRUE)) { - *opp = obj_link(op); + op = GC_auobjfreelist[lg]; + if (EXPECT(op != 0, TRUE)) { + GC_auobjfreelist[lg] = obj_link(op); obj_link(op) = 0; GC_bytes_allocd += GRANULES_TO_BYTES(lg); /* Mark bit was already set while object was on free list. */ diff --git a/typd_mlc.c b/typd_mlc.c index 00f78ce5..1c546ed7 100644 --- a/typd_mlc.c +++ b/typd_mlc.c @@ -584,7 +584,6 @@ GC_API GC_descr GC_CALL GC_make_descriptor(const GC_word * bm, size_t len) GC_API void * GC_CALL GC_malloc_explicitly_typed(size_t lb, GC_descr d) { ptr_t op; - ptr_t * opp; size_t lg; DCL_LOCK_STATE; @@ -592,16 +591,15 @@ GC_API void * GC_CALL GC_malloc_explicitly_typed(size_t lb, GC_descr d) if(SMALL_OBJ(lb)) { GC_DBG_COLLECT_AT_MALLOC(lb); lg = GC_size_map[lb]; - opp = &(GC_eobjfreelist[lg]); LOCK(); - op = *opp; + op = GC_eobjfreelist[lg]; if (EXPECT(0 == op, FALSE)) { UNLOCK(); op = (ptr_t)GENERAL_MALLOC((word)lb, GC_explicit_kind); if (0 == op) return 0; lg = GC_size_map[lb]; /* May have been uninitialized. */ } else { - *opp = obj_link(op); + GC_eobjfreelist[lg] = obj_link(op); obj_link(op) = 0; GC_bytes_allocd += GRANULES_TO_BYTES(lg); UNLOCK(); @@ -621,24 +619,22 @@ GC_API void * GC_CALL GC_malloc_explicitly_typed_ignore_off_page(size_t lb, GC_descr d) { ptr_t op; - ptr_t * opp; size_t lg; DCL_LOCK_STATE; lb += TYPD_EXTRA_BYTES; - if( SMALL_OBJ(lb) ) { + if (SMALL_OBJ(lb)) { GC_DBG_COLLECT_AT_MALLOC(lb); lg = GC_size_map[lb]; - opp = &(GC_eobjfreelist[lg]); LOCK(); - op = *opp; + op = GC_eobjfreelist[lg]; if (EXPECT(0 == op, FALSE)) { UNLOCK(); op = (ptr_t)GENERAL_MALLOC_IOP(lb, GC_explicit_kind); if (0 == op) return 0; lg = GC_size_map[lb]; /* May have been uninitialized. */ } else { - *opp = obj_link(op); + GC_eobjfreelist[lg] = obj_link(op); obj_link(op) = 0; GC_bytes_allocd += GRANULES_TO_BYTES(lg); UNLOCK(); @@ -658,7 +654,6 @@ GC_API void * GC_CALL GC_calloc_explicitly_typed(size_t n, size_t lb, GC_descr d) { ptr_t op; - ptr_t * opp; size_t lg; GC_descr simple_descr; complex_descriptor *complex_descr; @@ -682,16 +677,15 @@ GC_API void * GC_CALL GC_calloc_explicitly_typed(size_t n, size_t lb, } if( SMALL_OBJ(lb) ) { lg = GC_size_map[lb]; - opp = &(GC_arobjfreelist[lg]); LOCK(); - op = *opp; + op = GC_arobjfreelist[lg]; if (EXPECT(0 == op, FALSE)) { UNLOCK(); op = (ptr_t)GENERAL_MALLOC((word)lb, GC_array_kind); if (0 == op) return(0); lg = GC_size_map[lb]; /* May have been uninitialized. */ } else { - *opp = obj_link(op); + GC_arobjfreelist[lg] = obj_link(op); obj_link(op) = 0; GC_bytes_allocd += GRANULES_TO_BYTES(lg); UNLOCK(); -- 2.40.0