Ergonomic improvement: xcalloc -> new(), xrealloc -> grow()

This commit is contained in:
Bruce Hill 2021-07-30 14:44:00 -07:00
parent ad640caac6
commit 3445982b16
4 changed files with 14 additions and 14 deletions

15
files.c
View File

@ -30,13 +30,13 @@ static void populate_lines(file_t *f, size_t len)
{
// Calculate line numbers:
size_t linecap = 10;
f->lines = xcalloc(sizeof(const char*), linecap);
f->lines = new(const char*[linecap]);
f->nlines = 0;
char *p = f->memory;
for (size_t n = 0; p && p < &f->memory[len]; ++n) {
++f->nlines;
if (n >= linecap)
f->lines = xrealloc(f->lines, sizeof(const char*)*(linecap *= 2));
f->lines = grow(f->lines, linecap *= 2);
f->lines[n] = p;
do {
char *nl = strchr(p, '\n');
@ -107,13 +107,14 @@ file_t *load_file(file_t **files, const char *filename)
{
size_t capacity = 1000;
length = 0;
f->memory = xcalloc(sizeof(char), capacity);
f->memory = new(char[capacity]);
ssize_t just_read;
while ((just_read=read(fd, &f->memory[length], capacity - length)) > 0) {
while ((just_read=read(fd, &f->memory[length], (capacity-1) - length)) > 0) {
length += (size_t)just_read;
if (length >= capacity)
f->memory = xrealloc(f->memory, sizeof(char)*(capacity *= 2) + 1);
if (length >= capacity-1)
f->memory = grow(f->memory, capacity *= 2);
}
f->memory[length] = '\0';
}
finished_loading:
@ -150,7 +151,7 @@ file_t *spoof_file(file_t **files, const char *filename, const char *text, ssize
file_t *f = new(file_t);
size_t len = _len == -1 ? strlen(text) : (size_t)_len;
f->filename = memcheck(strdup(filename));
f->memory = xcalloc(len+1, sizeof(char));
f->memory = new(char[len+1]);
memcpy(f->memory, text, len);
f->start = &f->memory[0];
f->end = &f->memory[len];

View File

@ -136,7 +136,7 @@ static void cache_save(match_t *m)
} else {
cache_t old_cache = cache;
cache.size = old_cache.size == 0 ? 16 : 2*old_cache.size;
cache.matches = xcalloc(cache.size, sizeof(match_t*));
cache.matches = new(match_t*[cache.size]);
// Rehash:
if (old_cache.matches) {
@ -377,7 +377,7 @@ static match_t *match(def_t *defs, file_t *f, const char *str, pat_t *pat, bool
if (s != NULL) {
str = s->end;
if (nchildren+2 >= child_cap) {
m->children = xrealloc(m->children, sizeof(match_t*)*(child_cap += 5));
m->children = grow(m->children, child_cap += 5);
for (size_t i = nchildren; i < child_cap; i++) m->children[i] = NULL;
}
add_owner(&m->children[nchildren++], s);
@ -432,14 +432,14 @@ static match_t *match(def_t *defs, file_t *f, const char *str, pat_t *pat, bool
}
if (msep) {
if (nchildren+2 >= child_cap) {
m->children = xrealloc(m->children, sizeof(match_t*)*(child_cap += 5));
m->children = grow(m->children, child_cap += 5);
for (size_t i = nchildren; i < child_cap; i++) m->children[i] = NULL;
}
add_owner(&m->children[nchildren++], msep);
}
if (nchildren+2 >= child_cap) {
m->children = xrealloc(m->children, sizeof(match_t*)*(child_cap += 5));
m->children = grow(m->children, child_cap += 5);
for (size_t i = nchildren; i < child_cap; i++) m->children[i] = NULL;
}
add_owner(&m->children[nchildren++], mp);

View File

@ -563,7 +563,7 @@ pat_t *bp_replacement(file_t *f, pat_t *replacepat, const char *replacement)
}
}
size_t rlen = (size_t)(p-replacement);
char *rcpy = xcalloc(sizeof(char), rlen + 1);
char *rcpy = new(char[rlen + 1]);
memcpy(rcpy, replacement, rlen);
pat->args.replace.text = rcpy;
pat->args.replace.len = rlen;

View File

@ -14,8 +14,7 @@
#define streq(a, b) (strcmp(a, b) == 0)
#define new(t) memcheck(calloc(1, sizeof(t)))
#define xcalloc(a,b) memcheck(calloc(a,b))
#define xrealloc(a,b) memcheck(realloc(a,b))
#define grow(arr,n) memcheck(realloc(arr,sizeof(arr[0])*(n)))
__attribute__((nonnull(1)))
char unescapechar(const char *escaped, const char **end);