From 14a61e98a81998d976b5119d8881de4444e93832 Mon Sep 17 00:00:00 2001 From: Jonathan Chambers Date: Tue, 10 Apr 2018 13:07:41 -0400 Subject: [PATCH] Expose API to control the minimum bytes allocated before a GC occurs (a cherry-pick of commit 4c0e58d from 'unity-release-7_4-incremental') * alloc.c (min_bytes_allocd_minimum): New static variable. * alloc.c (GC_set_min_bytes_allocd, GC_get_min_bytes_allocd): New API function definition (to set/get min_bytes_allocd_minimum). * alloc.c (min_bytes_allocd): Return min_bytes_allocd_minimum if result is less than min_bytes_allocd_minimum. * include/gc.h (GC_set_min_bytes_allocd, GC_get_min_bytes_allocd): New API function declaration. * tests/test.c [GC_PTHREADS] (main): Call GC_set_min_bytes_allocd() and GC_get_min_bytes_allocd(). --- alloc.c | 17 ++++++++++++++++- include/gc.h | 6 ++++++ tests/test.c | 3 +++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/alloc.c b/alloc.c index 14c1c53b..d26178af 100644 --- a/alloc.c +++ b/alloc.c @@ -230,6 +230,20 @@ GC_API GC_stop_func GC_CALL GC_get_stop_func(void) GC_INNER word GC_total_stacksize = 0; /* updated on every push_all_stacks */ #endif +static size_t min_bytes_allocd_minimum = 1; + /* The lowest value returned by min_bytes_allocd(). */ + +GC_API void GC_CALL GC_set_min_bytes_allocd(size_t value) +{ + GC_ASSERT(value > 0); + min_bytes_allocd_minimum = value; +} + +GC_API size_t GC_CALL GC_get_min_bytes_allocd(void) +{ + return min_bytes_allocd_minimum; +} + /* Return the minimum number of bytes that must be allocated between */ /* collections to amortize the collection cost. Should be non-zero. */ static word min_bytes_allocd(void) @@ -270,7 +284,8 @@ static word min_bytes_allocd(void) if (GC_incremental) { result /= 2; } - return result > 0 ? result : 1; + return result > min_bytes_allocd_minimum + ? result : min_bytes_allocd_minimum; } STATIC word GC_non_gc_bytes_at_gc = 0; diff --git a/include/gc.h b/include/gc.h index fbac4c30..32f66f8c 100644 --- a/include/gc.h +++ b/include/gc.h @@ -427,6 +427,12 @@ GC_API void GC_CALL GC_set_pages_executable(int); /* use or need synchronization (i.e. acquiring the allocator lock). */ GC_API int GC_CALL GC_get_pages_executable(void); +/* The setter and getter of the minimum value returned by the internal */ +/* min_bytes_allocd(). The value should not be zero; the default value */ +/* is one. Not synchronized. */ +GC_API void GC_CALL GC_set_min_bytes_allocd(size_t); +GC_API size_t GC_CALL GC_get_min_bytes_allocd(void); + /* Overrides the default handle-fork mode. Non-zero value means GC */ /* should install proper pthread_atfork handlers. Has effect only if */ /* called before GC_INIT. Clients should invoke GC_set_handle_fork */ diff --git a/tests/test.c b/tests/test.c index 64f8f3f6..13fb90ea 100644 --- a/tests/test.c +++ b/tests/test.c @@ -2261,6 +2261,9 @@ int main(void) "Emulating dirty bits with mprotect/signals\n"); } # endif + GC_set_min_bytes_allocd(1); + if (GC_get_min_bytes_allocd() != 1) + FAIL; GC_set_warn_proc(warn_proc); if ((code = pthread_key_create(&fl_key, 0)) != 0) { GC_printf("Key creation failed %d\n", code); -- 2.50.1