aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2021-01-10 00:24:24 -0800
committerBruce Hill <bruce@bruce-hill.com>2021-01-10 00:24:24 -0800
commitf4a7b80b4ff63d6610142b417cbadd6526339ae4 (patch)
tree1da87124f2fa4b935aa17eb25da100475ae49ff3
parent9d1f51c483578c66d401a59f59ad18add0e1a52f (diff)
Updated more things to use xfree(&foo) instead of free(foo)
-rw-r--r--compiler.c13
-rw-r--r--file_loader.c11
-rw-r--r--grammar.c4
-rw-r--r--printing.c2
-rw-r--r--utils.c8
-rw-r--r--utils.h2
-rw-r--r--vm.c2
7 files changed, 22 insertions, 20 deletions
diff --git a/compiler.c b/compiler.c
index 07271c3..b880cf8 100644
--- a/compiler.c
+++ b/compiler.c
@@ -351,7 +351,7 @@ vm_op_t *bp_simplepattern(file_t *f, const char *str)
// Parentheses
case '(': case '{': {
char closing = c == '(' ? ')' : '}';
- free(op);
+ xfree(&op);
op = bp_simplepattern(f, str);
if (!op)
file_err(f, str, str, "There should be a valid pattern after this parenthesis.");
@@ -428,8 +428,8 @@ vm_op_t *bp_simplepattern(file_t *f, const char *str)
op->args.s = strndup(&c, 1);
}
if (matchchar(&str, ':')) { // Don't match definitions
- free((char*)op->args.s);
- free(op);
+ xfree(&op->args.s);
+ xfree(&op);
return NULL;
}
op->op = VM_REF;
@@ -442,14 +442,14 @@ vm_op_t *bp_simplepattern(file_t *f, const char *str)
const char *refname = str;
str = after_name(str);
if (matchchar(&str, ':')) { // Don't match definitions
- free(op);
+ xfree(&op);
return NULL;
}
op->op = VM_REF;
op->args.s = strndup(refname, (size_t)(str - refname));
break;
} else {
- free(op);
+ xfree(&op);
return NULL;
}
}
@@ -535,8 +535,7 @@ vm_op_t *bp_stringpattern(file_t *f, const char *str)
strop->end = str;
if (strop->len == 0) {
- free(strop);
- strop = NULL;
+ xfree(&strop);
} else {
ret = chain_together(ret, strop);
}
diff --git a/file_loader.c b/file_loader.c
index 1f61953..c5dc222 100644
--- a/file_loader.c
+++ b/file_loader.c
@@ -103,30 +103,29 @@ void intern_file(file_t *f)
f->contents = buf;
f->end = buf + size;
f->mmapped = 0;
- free(f->lines);
+ xfree(&f->lines);
populate_lines(f);
}
void destroy_file(file_t **f)
{
if ((*f)->filename) {
- free((char*)(*f)->filename);
+ xfree(&((*f)->filename));
(*f)->filename = NULL;
}
if ((*f)->lines) {
- free((*f)->lines);
+ xfree(&((*f)->lines));
(*f)->lines = NULL;
}
if ((*f)->contents) {
if ((*f)->mmapped) {
munmap((*f)->contents, (size_t)((*f)->end - (*f)->contents));
} else {
- free((*f)->contents);
+ xfree(&((*f)->contents));
}
(*f)->contents = NULL;
}
- free(*f);
- *f = NULL;
+ xfree(f);
}
size_t get_line_number(file_t *f, const char *p)
diff --git a/grammar.c b/grammar.c
index f287f46..b980196 100644
--- a/grammar.c
+++ b/grammar.c
@@ -119,8 +119,8 @@ void pop_backrefs(grammar_t *g, size_t count)
backref_t *b = g->firstbackref;
g->firstbackref = b->next;
check(b, "Attempt to pop %ld more backrefs than there are", count);
- xfree((void**)&b->op);
- xfree((void**)&b);
+ xfree(&b->op);
+ xfree(&b);
}
}
diff --git a/printing.c b/printing.c
index 68faf22..e8cd178 100644
--- a/printing.c
+++ b/printing.c
@@ -132,7 +132,7 @@ static void _visualize_matches(match_node_t *firstmatch, int depth, const char *
for (match_node_t *c = children, *next = NULL; c; c = next) {
next = c->next;
- free(c);
+ xfree(&c);
}
}
diff --git a/utils.c b/utils.c
index c18ef9e..f13db19 100644
--- a/utils.c
+++ b/utils.c
@@ -214,9 +214,13 @@ int memicmp(const void *v1, const void *v2, size_t n)
/*
* Free memory, but also set the pointer to NULL for safety
*/
-void xfree(void **p)
+void xfree(void *p)
{
- free(*p);
+ if (*(void**)p == NULL) {
+ fprintf(stderr, "Attempt to free(NULL)\n");
+ _exit(1);
+ }
+ free(*(void**)p);
p = NULL;
}
diff --git a/utils.h b/utils.h
index 7fde16f..8fcb345 100644
--- a/utils.h
+++ b/utils.h
@@ -34,7 +34,7 @@ void *memcheck(void *p);
__attribute__((nonnull))
int memicmp(const void *s1, const void *s2, size_t n);
__attribute__((nonnull))
-void xfree(void **p);
+void xfree(void *p);
#endif
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1
diff --git a/vm.c b/vm.c
index 55c25fb..78eade4 100644
--- a/vm.c
+++ b/vm.c
@@ -538,7 +538,7 @@ match_t *get_capture(match_t *m, const char **r)
if (end == *r) return NULL;
char *name = strndup(*r, (size_t)(end-*r));
match_t *cap = get_capture_by_name(m, name);
- free(name);
+ xfree(&name);
*r = end;
if (**r == ';') ++(*r);
return cap;