From d934e7d5a1c07167d75e0f56d38c89f77faf46b3 Mon Sep 17 00:00:00 2001 From: Ivan Maidanski Date: Wed, 14 Feb 2024 00:22:46 +0300 Subject: [PATCH] Treat zero requested size in GC_malloc_many same as that in GC_malloc If the size argument is zero, then it is treated as one. * include/gc/gc.h (GC_malloc_many): Move comment from mallocx.c; document the case of zero value of the argument. * mallocx.c (GC_malloc_many): If lb is zero, then set it to 1 (before computing lg). --- include/gc/gc.h | 5 ++++- mallocx.c | 6 +++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/include/gc/gc.h b/include/gc/gc.h index 14876cc50..18bbf02cb 100644 --- a/include/gc/gc.h +++ b/include/gc/gc.h @@ -1951,9 +1951,12 @@ GC_API void GC_CALL GC_debug_ptr_store_and_dirty(void * /* p */, /* This returns a list of objects, linked through their first word. */ /* Its use can greatly reduce lock contention problems, since the */ /* allocator lock can be acquired and released many fewer times. */ +/* Note that there is no "atomic" version of this function, as */ +/* otherwise the links would not be seen by the collector. */ +/* If the argument is zero, then it is treated as one. */ GC_API GC_ATTR_MALLOC void * GC_CALL GC_malloc_many(size_t /* lb */); #define GC_NEXT(p) (*(void * *)(p)) /* Retrieve the next element */ - /* in returned list. */ + /* in the returned list. */ /* A filter function to control the scanning of dynamic libraries. */ /* If implemented, called by GC before registering a dynamic library */ diff --git a/mallocx.c b/mallocx.c index 0c228512a..1a7892108 100644 --- a/mallocx.c +++ b/mallocx.c @@ -444,13 +444,13 @@ GC_API void GC_CALL GC_generic_malloc_many(size_t lb, int k, void **result) (void) GC_clear_stack(0); } -/* Note that the "atomic" version of this would be unsafe, since the */ -/* links would not be seen by the collector. */ GC_API GC_ATTR_MALLOC void * GC_CALL GC_malloc_many(size_t lb) { void *result; - size_t lg = ALLOC_REQUEST_GRANS(lb); + size_t lg; + if (EXPECT(0 == lb, FALSE)) lb = 1; + lg = ALLOC_REQUEST_GRANS(lb); GC_generic_malloc_many(GRANULES_TO_BYTES(lg), NORMAL, &result); return result; }