remove use of calloc

This commit is contained in:
CK Tan 2020-10-27 16:59:43 -07:00
parent bc6f2fa6b8
commit 5be06807ad
2 changed files with 19 additions and 13 deletions

29
toml.c
View File

@ -38,21 +38,28 @@
static void* (*ppmalloc)(size_t) = malloc; static void* (*ppmalloc)(size_t) = malloc;
static void (*ppfree)(void*) = free; static void (*ppfree)(void*) = free;
static void* (*ppcalloc)(size_t, size_t) = calloc;
void toml_set_memutil(void* (*xxmalloc)(size_t), void toml_set_memutil(void* (*xxmalloc)(size_t),
void (*xxfree)(void*), void (*xxfree)(void*))
void* (*xxcalloc)(size_t, size_t))
{ {
if (xxmalloc) ppmalloc = xxmalloc; if (xxmalloc) ppmalloc = xxmalloc;
if (xxfree) ppfree = xxfree; if (xxfree) ppfree = xxfree;
if (xxcalloc) ppcalloc = xxcalloc;
} }
#define MALLOC(a) ppmalloc(a) #define MALLOC(a) ppmalloc(a)
#define FREE(a) ppfree(a) #define FREE(a) ppfree(a)
#define CALLOC(a,b) ppcalloc(a,b)
static void* CALLOC(size_t nmemb, size_t sz)
{
int nb = sz * nmemb;
void* p = MALLOC(nb);
if (p) {
memset(p, 0, nb);
}
return p;
}
static char* STRDUP(const char* s) static char* STRDUP(const char* s)
{ {
@ -382,22 +389,22 @@ static int e_keyexists(context_t* ctx, int lineno)
static void* expand(void* p, int sz, int newsz) static void* expand(void* p, int sz, int newsz)
{ {
void* s = malloc(newsz); void* s = MALLOC(newsz);
if (!s) return 0; if (!s) return 0;
memcpy(s, p, sz); memcpy(s, p, sz);
free(p); FREE(p);
return s; return s;
} }
static void** expand_ptrarr(void** p, int n) static void** expand_ptrarr(void** p, int n)
{ {
void** s = malloc((n+1) * sizeof(void*)); void** s = MALLOC((n+1) * sizeof(void*));
if (!s) return 0; if (!s) return 0;
s[n] = 0; s[n] = 0;
memcpy(s, p, n * sizeof(void*)); memcpy(s, p, n * sizeof(void*));
free(p); FREE(p);
return s; return s;
} }
@ -1047,8 +1054,8 @@ static int parse_keyval(context_t* ctx, toml_table_t* tab)
token_t key = ctx->tok; token_t key = ctx->tok;
if (eat_token(ctx, STRING, 1, FLINE)) return -1; if (eat_token(ctx, STRING, 1, FLINE)) return -1;
if (ctx->tok.tok == DOT) { if (ctx->tok.tok == DOT) {
/* handle inline dotted key. /* handle inline dotted key.
e.g. e.g.
physical.color = "orange" physical.color = "orange"
physical.shape = "round" physical.shape = "round"

3
toml.h
View File

@ -143,7 +143,6 @@ TOML_EXTERN int toml_rtots(toml_raw_t s, toml_timestamp_t* ret);
TOML_EXTERN int toml_utf8_to_ucs(const char* orig, int len, int64_t* ret); TOML_EXTERN int toml_utf8_to_ucs(const char* orig, int len, int64_t* ret);
TOML_EXTERN int toml_ucs_to_utf8(int64_t code, char buf[6]); TOML_EXTERN int toml_ucs_to_utf8(int64_t code, char buf[6]);
TOML_EXTERN void toml_set_memutil(void* (*xxmalloc)(size_t), TOML_EXTERN void toml_set_memutil(void* (*xxmalloc)(size_t),
void (*xxfree)(void*), void (*xxfree)(void*));
void* (*xxcalloc)(size_t, size_t));
#endif /* TOML_H */ #endif /* TOML_H */