Skip to content

Commit

Permalink
Treat zero requested size in GC_malloc_many same as that in GC_malloc
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
ivmai committed Feb 13, 2024
1 parent ef87ba5 commit d934e7d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
5 changes: 4 additions & 1 deletion include/gc/gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
6 changes: 3 additions & 3 deletions mallocx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit d934e7d

Please sign in to comment.