From acdbf8911cb55183821f7ae866cc55f58b3123aa Mon Sep 17 00:00:00 2001 From: Andrew Chambers Date: Fri, 3 Jan 2020 12:10:17 +1300 Subject: [PATCH] Add scratch calloc. --- src/core/gc.c | 10 ++++++++++ src/include/janet.h | 1 + 2 files changed, 11 insertions(+) diff --git a/src/core/gc.c b/src/core/gc.c index 65e16d32..2e91cbd1 100644 --- a/src/core/gc.c +++ b/src/core/gc.c @@ -491,6 +491,16 @@ void *janet_smalloc(size_t size) { return (char *)mem + SCRATCH_HDR_SIZE; } +void *janet_scalloc(size_t nmemb, size_t size) { + if (nmemb && size > (size_t)-1/size) { + JANET_OUT_OF_MEMORY; + } + size_t n = nmemb * size; + void *p = janet_smalloc(n); + memset(p, 0, n); + return p; +} + void *janet_srealloc(void *mem, size_t size) { if (NULL == mem) return janet_smalloc(size); mem = (char *)mem - SCRATCH_HDR_SIZE; diff --git a/src/include/janet.h b/src/include/janet.h index 664b803f..27b46e28 100644 --- a/src/include/janet.h +++ b/src/include/janet.h @@ -1355,6 +1355,7 @@ JANET_API void janet_stacktrace(JanetFiber *fiber, Janet err); typedef void (*ScratchFinalizer)(void *); JANET_API void *janet_smalloc(size_t size); JANET_API void *janet_srealloc(void *mem, size_t size); +JANET_API void *janet_scalloc(size_t nmemb, size_t size); JANET_API void janet_sfinalizer(void *mem, ScratchFinalizer finalizer); JANET_API void janet_sfree(void *mem);