1
0
mirror of https://github.com/janet-lang/janet synced 2025-01-26 07:06:51 +00:00

Merge pull request #236 from andrewchambers/scratch_calloc

Add scratch calloc.
This commit is contained in:
Calvin Rose 2020-01-02 20:29:10 -06:00 committed by GitHub
commit e93e237c67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 0 deletions

View File

@ -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;

View File

@ -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);