Clean up allocator defs

- Condense the allocator section in defs.h and make it match the
  surrounding style.

- Use real calloc in c3_calloc. Some guy on the internet says
  calloc can be faster than malloc since the OS may not actually
  need to call memset.

- Replace calloc calls in vere code with c3_calloc.

Note that I left the c3_calloc definition as-is (i.e., not taking
a count argument). I was going to change it, but it seems like
count is 1 at all call sites. If it's ever used with count != 1,
I'd be in support of changing it, fwiw.
This commit is contained in:
Jōshin 2018-12-27 17:22:55 -08:00
parent 991f39deb1
commit 67c0b7b39b
4 changed files with 26 additions and 30 deletions

View File

@ -91,27 +91,23 @@
cnt_w = (cnt_w + 1) % (n); \ cnt_w = (cnt_w + 1) % (n); \
} while (0) } while (0)
/* c3_malloc(): asserting malloc /* Asserting allocators.
*/ */
#define c3_malloc(s) ({ \ # define c3_malloc(s) ({ \
void* rut = malloc(s); \ void* rut = malloc(s); \
if ( 0 == rut ) { \ if ( 0 == rut ) { \
c3_assert(!"memory lost"); \ c3_assert(!"memory lost"); \
} \ } \
rut;}) rut;})
# define c3_calloc(s) ({ \
/* c3_calloc(): asserting calloc void* rut = calloc(1,s); \
*/ if ( 0 == rut ) { \
#define c3_calloc(s) ({ \ c3_assert(!"memory lost"); \
void* rut = c3_malloc(s); \ } \
memset(rut, 0, s); \ rut;})
rut;}) # define c3_realloc(a, b) ({ \
void* rut = realloc(a, b); \
/* c3_realloc(): asserting realloc if ( 0 == rut ) { \
*/ c3_assert(!"memory lost"); \
#define c3_realloc(a, b) ({ \ } \
void* rut = realloc(a, b); \ rut;})
if ( 0 == rut ) { \
c3_assert(!"memory lost"); \
} \
rut;})

View File

@ -75,7 +75,7 @@ _raft_rnam_free(u3_rnam* nam_u)
static u3_rnam* static u3_rnam*
_raft_readname(const c3_c* str_c, c3_w siz_w) _raft_readname(const c3_c* str_c, c3_w siz_w)
{ {
u3_rnam* nam_u = calloc(1, sizeof(*nam_u)); u3_rnam* nam_u = c3_calloc(sizeof(*nam_u));
c3_c* col_c; c3_c* col_c;
c3_w nam_w; c3_w nam_w;
@ -520,8 +520,8 @@ _raft_rmsg_read(const u3_rbuf* buf_u, u3_rmsg* msg_u)
memcpy(&msg_u->rest.apen.ent_d, buf_u->buf_y + red_i, sizeof(c3_d)); memcpy(&msg_u->rest.apen.ent_d, buf_u->buf_y + red_i, sizeof(c3_d));
red_i += sizeof(c3_d); red_i += sizeof(c3_d);
msg_u->rest.apen.ent_u = calloc( msg_u->rest.apen.ent_u = c3_calloc(
1, msg_u->rest.apen.ent_d * sizeof(u3_rent)); msg_u->rest.apen.ent_d * sizeof(u3_rent));
{ {
c3_d i_d; c3_d i_d;
u3_rent* ent_u = msg_u->rest.apen.ent_u; u3_rent* ent_u = msg_u->rest.apen.ent_u;
@ -1152,7 +1152,7 @@ _raft_write_rest(u3_rcon* ron_u, c3_d lai_d, c3_w lat_w, u3_rmsg* msg_u)
msg_u->rest.lai_d = lai_d; msg_u->rest.lai_d = lai_d;
msg_u->rest.lat_w = lat_w; msg_u->rest.lat_w = lat_w;
msg_u->rest.nam_w = 1 + strlen(raf_u->str_c) / 4; msg_u->rest.nam_w = 1 + strlen(raf_u->str_c) / 4;
msg_u->rest.nam_c = calloc(1, 4 * msg_u->rest.nam_w); msg_u->rest.nam_c = c3_calloc(4 * msg_u->rest.nam_w);
strncpy(msg_u->rest.nam_c, raf_u->str_c, 4 * msg_u->rest.nam_w + 1); strncpy(msg_u->rest.nam_c, raf_u->str_c, 4 * msg_u->rest.nam_w + 1);
msg_u->len_d += 4 + msg_u->rest.nam_w; msg_u->len_d += 4 + msg_u->rest.nam_w;
} }

View File

@ -272,7 +272,7 @@ _sist_cask(c3_c* dir_c, u3_noun nun)
{ {
c3_c paw_c[60]; c3_c paw_c[60];
u3_noun key; u3_noun key;
u3_utty* uty_u = calloc(1, sizeof(u3_utty)); u3_utty* uty_u = c3_calloc(sizeof(u3_utty));
uty_u->fid_i = 0; uty_u->fid_i = 0;
uH; uH;

View File

@ -95,7 +95,7 @@ _term_close_cb(uv_handle_t* han_t)
void void
u3_term_io_init() u3_term_io_init()
{ {
u3_utty* uty_u = calloc(1, sizeof(u3_utty)); u3_utty* uty_u = c3_calloc(sizeof(u3_utty));
if ( c3y == u3_Host.ops_u.dem ) { if ( c3y == u3_Host.ops_u.dem ) {
uty_u->fid_i = 1; uty_u->fid_i = 1;