commit 0cc44f6f0dfb641edbbc962bc76bf2360ede2d2a
parent a845bc8f0d6846291122dc2e52f78025eab5a38b
Author: Drew DeVault <sir@cmpwn.com>
Date: Thu, 24 Dec 2020 18:46:48 -0500
s/calloc/xcalloc/g
Diffstat:
10 files changed, 96 insertions(+), 75 deletions(-)
diff --git a/include/util.h b/include/util.h
@@ -5,5 +5,7 @@
unsigned long djb2(unsigned long hash, char c);
unsigned long djb2_s(unsigned long hash, const char *str);
+void *xcalloc(size_t n, size_t s);
+void *xrealloc(void *p, size_t s);
#endif
diff --git a/src/check.c b/src/check.c
@@ -9,6 +9,7 @@
#include "trace.h"
#include "type_store.h"
#include "types.h"
+#include "util.h"
struct context {
struct type_store store;
@@ -62,8 +63,8 @@ check_expr_assign(struct context *ctx,
expr->type = EXPR_ASSIGN;
expr->result = &builtin_type_void;
expr->assign.indirect = aexpr->assign.indirect;
- struct expression *object = calloc(1, sizeof(struct expression));
- struct expression *value = calloc(1, sizeof(struct expression));
+ struct expression *object = xcalloc(1, sizeof(struct expression));
+ struct expression *value = xcalloc(1, sizeof(struct expression));
check_expression(ctx, aexpr->assign.object, object);
check_expression(ctx, aexpr->assign.value, value);
@@ -96,8 +97,8 @@ check_expr_binarithm(struct context *ctx,
expr->type = EXPR_BINARITHM;
expr->binarithm.op = aexpr->binarithm.op;
- struct expression *lvalue = calloc(1, sizeof(struct expression)),
- *rvalue = calloc(1, sizeof(struct expression));
+ struct expression *lvalue = xcalloc(1, sizeof(struct expression)),
+ *rvalue = xcalloc(1, sizeof(struct expression));
check_expression(ctx, aexpr->binarithm.lvalue, lvalue);
check_expression(ctx, aexpr->binarithm.rvalue, rvalue);
expr->binarithm.lvalue = lvalue;
@@ -154,7 +155,7 @@ check_expr_binding(struct context *ctx,
.name = abinding->name,
};
struct expression *initializer =
- calloc(1, sizeof(struct expression));
+ xcalloc(1, sizeof(struct expression));
check_expression(ctx, abinding->initializer, initializer);
const struct type *type;
@@ -174,7 +175,7 @@ check_expr_binding(struct context *ctx,
if (abinding->next) {
binding = *next =
- calloc(1, sizeof(struct expression_binding));
+ xcalloc(1, sizeof(struct expression_binding));
next = &binding->next;
}
@@ -190,7 +191,7 @@ check_expr_call(struct context *ctx,
trenter(TR_CHECK, "call");
expr->type = EXPR_CALL;
- struct expression *lvalue = calloc(1, sizeof(struct expression));
+ struct expression *lvalue = xcalloc(1, sizeof(struct expression));
check_expression(ctx, aexpr->call.lvalue, lvalue);
expr->call.lvalue = lvalue;
@@ -206,8 +207,8 @@ check_expr_call(struct context *ctx,
while (param && aarg) {
trenter(TR_CHECK, "arg");
assert(!aarg->variadic); // TODO
- arg = *next = calloc(1, sizeof(struct call_argument));
- arg->value = calloc(1, sizeof(struct expression));
+ arg = *next = xcalloc(1, sizeof(struct call_argument));
+ arg->value = xcalloc(1, sizeof(struct expression));
check_expression(ctx, aarg->value, arg->value);
// TODO: Test for assignability
@@ -291,13 +292,13 @@ check_expr_list(struct context *ctx,
const struct ast_expression_list *alist = &aexpr->list;
while (alist) {
- struct expression *lexpr = calloc(1, sizeof(struct expression));
+ struct expression *lexpr = xcalloc(1, sizeof(struct expression));
check_expression(ctx, alist->expr, lexpr);
list->expr = lexpr;
alist = alist->next;
if (alist) {
- *next = calloc(1, sizeof(struct expressions));
+ *next = xcalloc(1, sizeof(struct expressions));
list = *next;
next = &list->next;
} else {
@@ -321,7 +322,7 @@ check_expr_measure(struct context *ctx,
switch (expr->measure.op) {
case M_LEN:
- expr->measure.value = calloc(1, sizeof(struct expression));
+ expr->measure.value = xcalloc(1, sizeof(struct expression));
check_expression(ctx, aexpr->measure.value, expr->measure.value);
enum type_storage vstor = expr->measure.value->result->storage;
expect(vstor == TYPE_STORAGE_ARRAY || vstor == TYPE_STORAGE_SLICE,
@@ -348,7 +349,7 @@ check_expr_return(struct context *ctx,
expr->terminates = true;
if (aexpr->_return.value) {
- struct expression *rval = calloc(1, sizeof(struct expression));
+ struct expression *rval = xcalloc(1, sizeof(struct expression));
check_expression(ctx, aexpr->_return.value, rval);
expr->_return.value = rval;
// TODO: Test assignability with function's return type
@@ -365,7 +366,7 @@ check_expr_unarithm(struct context *ctx,
trenter(TR_CHECK, "unarithm");
expr->type = EXPR_UNARITHM;
- struct expression *operand = calloc(1, sizeof(struct expression));
+ struct expression *operand = xcalloc(1, sizeof(struct expression));
check_expression(ctx, aexpr->unarithm.operand, operand);
expr->unarithm.operand = operand;
expr->unarithm.op = aexpr->unarithm.op;
@@ -490,7 +491,7 @@ check_function(struct context *ctx,
&ctx->store, &fn_atype);
assert(fntype); // Invariant
- struct declaration *decl = calloc(1, sizeof(struct declaration));
+ struct declaration *decl = xcalloc(1, sizeof(struct declaration));
decl->type = DECL_FUNC;
decl->func.type = fntype;
// TODO: Rewrite ident to be a member of the unit's namespace
@@ -509,7 +510,7 @@ check_function(struct context *ctx,
params = params->next;
}
- struct expression *body = calloc(1, sizeof(struct expression));
+ struct expression *body = xcalloc(1, sizeof(struct expression));
check_expression(ctx, afndecl->body, body);
decl->func.body = body;
@@ -555,7 +556,7 @@ check_declarations(struct context *ctx,
if (decl) {
struct declarations *decls = *next =
- calloc(1, sizeof(struct declarations));
+ xcalloc(1, sizeof(struct declarations));
decl->exported = adecl->exported;
decls->decl = decl;
next = &decls->next;
@@ -636,7 +637,7 @@ check(const struct ast_unit *aunit, struct unit *unit)
assert(!su->imports); // TODO
scan_declarations(&ctx, &su->decls);
- *next = calloc(1, sizeof(struct scopes));
+ *next = xcalloc(1, sizeof(struct scopes));
(*next)->scope = scope_pop(&ctx.scope, TR_SCAN);
next = &(*next)->next;
}
diff --git a/src/gen.c b/src/gen.c
@@ -12,6 +12,7 @@
#include "scope.h"
#include "trace.h"
#include "types.h"
+#include "util.h"
static char *
ident_to_sym(const struct identifier *ident)
@@ -22,8 +23,7 @@ ident_to_sym(const struct identifier *ident)
return NULL;
}
int n = snprintf(NULL, 0, "%s.%s", ns, ident->name);
- char *str = calloc(1, n + 1);
- assert(str);
+ char *str = xcalloc(1, n + 1);
snprintf(str, n + 1, "%s.%s", ns, ident->name);
free(ns);
return str;
@@ -39,7 +39,7 @@ gen_temp(struct gen_context *ctx, struct qbe_value *val,
val->type = type;
int n = snprintf(NULL, 0, fmt, ctx->id);
- char *str = calloc(1, n + 1);
+ char *str = xcalloc(1, n + 1);
snprintf(str, n + 1, fmt, ctx->id);
++ctx->id;
@@ -66,7 +66,7 @@ binding_alloc(struct gen_context *ctx, const struct scope_object *obj,
alloc_temp(ctx, val, obj->type, fmt);
val->indirect = true;
- struct gen_binding *binding = calloc(1, sizeof(struct gen_binding));
+ struct gen_binding *binding = xcalloc(1, sizeof(struct gen_binding));
binding->name = strdup(val->name);
binding->object = obj;
binding->next = ctx->bindings;
@@ -299,14 +299,14 @@ gen_call(struct gen_context *ctx,
struct qbe_arguments *arg, **next = &call.args;
struct call_argument *carg = expr->call.args;
- arg = *next = calloc(1, sizeof(struct qbe_arguments));
+ arg = *next = xcalloc(1, sizeof(struct qbe_arguments));
gen_temp(ctx, &arg->value, &qbe_long, "func.%d");
gen_expression(ctx, expr->call.lvalue, &arg->value);
next = &arg->next;
while (carg) {
assert(!carg->variadic); // TODO
- arg = *next = calloc(1, sizeof(struct qbe_arguments));
+ arg = *next = xcalloc(1, sizeof(struct qbe_arguments));
gen_temp(ctx, &arg->value,
qtype_for_type(ctx, carg->value->result, false),
"arg.%d");
@@ -527,7 +527,7 @@ gen_function_decl(struct gen_context *ctx, const struct declaration *decl)
const struct type *fntype = func->type;
assert(func->flags == 0); // TODO
- struct qbe_def *qdef = calloc(1, sizeof(struct qbe_def));
+ struct qbe_def *qdef = xcalloc(1, sizeof(struct qbe_def));
qdef->type = Q_FUNC;
qdef->exported = decl->exported;
qdef->name = func->symbol ? strdup(func->symbol)
@@ -540,7 +540,7 @@ gen_function_decl(struct gen_context *ctx, const struct declaration *decl)
struct qbe_func_param *param, **next = &qdef->func.params;
struct scope_object *obj = decl->func.scope->objects;
while (obj) {
- param = *next = calloc(1, sizeof(struct qbe_func_param));
+ param = *next = xcalloc(1, sizeof(struct qbe_func_param));
assert(!obj->ident.ns); // Invariant
param->name = strdup(obj->ident.name);
param->type = qtype_for_type(ctx, obj->type, true);
diff --git a/src/identifier.c b/src/identifier.c
@@ -6,6 +6,7 @@
#include <stdlib.h>
#include <string.h>
#include "identifier.h"
+#include "util.h"
static int
_asprintf(char **strp, const char *fmt, ...)
@@ -15,11 +16,7 @@ _asprintf(char **strp, const char *fmt, ...)
int n = vsnprintf(NULL, 0, fmt, ap);
va_end(ap);
- *strp = calloc(n + 1, 1);
- if (!*strp) {
- errno = ENOMEM;
- return -1;
- }
+ *strp = xcalloc(n + 1, 1);
va_start(ap, fmt);
n = vsnprintf(*strp, n + 1, fmt, ap);
@@ -69,7 +66,7 @@ identifier_dup(struct identifier *new, const struct identifier *ident)
assert(ident && new);
new->name = strdup(ident->name);
if (ident->ns) {
- new->ns = calloc(1, sizeof(struct identifier));
+ new->ns = xcalloc(1, sizeof(struct identifier));
identifier_dup(new->ns, ident->ns);
}
}
diff --git a/src/lex.c b/src/lex.c
@@ -11,6 +11,7 @@
#include "lex.h"
#include "trace.h"
#include "utf8.h"
+#include "util.h"
static const char *tokens[] = {
// Must be alpha sorted
@@ -126,7 +127,7 @@ lex_init(struct lexer *lexer, FILE *f)
memset(lexer, 0, sizeof(*lexer));
lexer->in = f;
lexer->bufsz = 256;
- lexer->buf = calloc(1, lexer->bufsz);
+ lexer->buf = xcalloc(1, lexer->bufsz);
lexer->un.token = T_ERROR;
lexer->loc.lineno = 1;
lexer->loc.colno = 0;
@@ -178,8 +179,7 @@ next(struct lexer *lexer, struct location *loc, bool buffer)
}
if (lexer->buflen + utf8_chsize(c) >= lexer->bufsz) {
lexer->bufsz *= 2;
- lexer->buf = realloc(lexer->buf, lexer->bufsz);
- assert(lexer->buf);
+ lexer->buf = xrealloc(lexer->buf, lexer->bufsz);
}
char buf[UTF8_MAX_SIZE];
size_t sz = utf8_encode(&buf[0], c);
diff --git a/src/parse.c b/src/parse.c
@@ -12,6 +12,7 @@
#include "trace.h"
#include "types.h"
#include "utf8.h"
+#include "util.h"
struct parser {
struct lexer *lex;
@@ -80,7 +81,7 @@ parse_identifier(struct parser *par, struct identifier *ident)
struct identifier *ns;
switch (lex(par->lex, &tok)) {
case T_DOUBLE_COLON:
- ns = calloc(1, sizeof(struct identifier));
+ ns = xcalloc(1, sizeof(struct identifier));
*ns = *i;
i->ns = ns;
i->name = NULL;
@@ -133,7 +134,7 @@ parse_imports(struct parser *par, struct ast_subunit *subunit)
struct ast_imports *imports;
switch (lex(par->lex, &tok)) {
case T_USE:
- imports = calloc(1, sizeof(struct ast_imports));
+ imports = xcalloc(1, sizeof(struct ast_imports));
parse_import(par, imports);
*next = imports;
next = &imports->next;
@@ -163,8 +164,8 @@ parse_parameter_list(struct parser *par, struct ast_function_type *type)
bool more = true;
struct ast_function_parameters **next = &type->params;
while (more) {
- *next = calloc(1, sizeof(struct ast_function_parameters));
- (*next)->type = calloc(1, sizeof(struct ast_type));
+ *next = xcalloc(1, sizeof(struct ast_function_parameters));
+ (*next)->type = xcalloc(1, sizeof(struct ast_type));
want(par, T_NAME, &tok);
(*next)->name = tok.name;
want(par, T_COLON, NULL);
@@ -215,7 +216,7 @@ parse_prototype(struct parser *par, struct ast_function_type *type)
parse_parameter_list(par, type);
want(par, T_RPAREN, NULL);
}
- type->result = calloc(1, sizeof(struct ast_type));
+ type->result = xcalloc(1, sizeof(struct ast_type));
parse_type(par, type->result);
size_t ctr = 0;
for (struct ast_function_parameters *param = type->params;
@@ -296,7 +297,7 @@ parse_enum_type(struct parser *par, struct ast_enum_type *type)
want(par, T_LBRACE, NULL);
struct ast_enum_field **next = &type->values;
while (tok.token != T_RBRACE) {
- *next = calloc(1, sizeof(struct ast_enum_field));
+ *next = xcalloc(1, sizeof(struct ast_enum_field));
want(par, T_NAME, &tok);
(*next)->name = tok.name;
if (lex(par->lex, &tok) == T_EQUAL) {
@@ -380,7 +381,7 @@ parse_type(struct parser *par, struct ast_type *type)
/* fallthrough */
case T_TIMES:
type->storage = TYPE_STORAGE_POINTER;
- type->pointer.referent = calloc(1, sizeof(struct ast_type));
+ type->pointer.referent = xcalloc(1, sizeof(struct ast_type));
parse_type(par, type->pointer.referent);
break;
case T_STRUCT:
@@ -417,7 +418,7 @@ static struct ast_expression *
parse_access(struct parser *par)
{
trace(TR_PARSE, "access");
- struct ast_expression *exp = calloc(1, sizeof(struct ast_expression));
+ struct ast_expression *exp = xcalloc(1, sizeof(struct ast_expression));
exp->type = EXPR_ACCESS;
parse_identifier(par, &exp->access.ident);
return exp;
@@ -431,7 +432,7 @@ parse_constant(struct parser *par)
struct token tok = {0};
want(par, T_LITERAL, &tok);
- struct ast_expression *exp = calloc(1, sizeof(struct ast_expression));
+ struct ast_expression *exp = xcalloc(1, sizeof(struct ast_expression));
exp->type = EXPR_CONSTANT;
exp->constant.storage = tok.storage;
@@ -498,7 +499,7 @@ parse_measurement_expression(struct parser *par)
{
trace(TR_PARSE, "measurement");
- struct ast_expression *exp = calloc(1, sizeof(struct ast_expression));
+ struct ast_expression *exp = xcalloc(1, sizeof(struct ast_expression));
exp->type = EXPR_MEASURE;
struct token tok;
@@ -508,7 +509,7 @@ parse_measurement_expression(struct parser *par)
switch (tok.token) {
case T_SIZE:
exp->measure.op = M_SIZE;
- exp->measure.type = calloc(1, sizeof(struct ast_type));
+ exp->measure.type = xcalloc(1, sizeof(struct ast_type));
parse_type(par, exp->measure.type);
break;
case T_LEN:
@@ -531,7 +532,7 @@ parse_call_expression(struct parser *par, struct ast_expression *lvalue)
{
trenter(TR_PARSE, "call");
- struct ast_expression *expr = calloc(1, sizeof(struct ast_expression));
+ struct ast_expression *expr = xcalloc(1, sizeof(struct ast_expression));
expr->type = EXPR_CALL;
expr->call.lvalue = lvalue;
@@ -541,7 +542,7 @@ parse_call_expression(struct parser *par, struct ast_expression *lvalue)
unlex(par->lex, &tok);
trenter(TR_PARSE, "arg");
- arg = *next = calloc(1, sizeof(struct ast_call_argument));
+ arg = *next = xcalloc(1, sizeof(struct ast_call_argument));
arg->value = parse_complex_expression(par);
if (lex(par->lex, &tok) == T_ELLIPSIS) {
@@ -658,7 +659,7 @@ parse_unary_expression(struct parser *par)
case T_LNOT: // !
case T_TIMES: // *
case T_BAND: // &
- exp = calloc(1, sizeof(struct ast_expression));
+ exp = xcalloc(1, sizeof(struct ast_expression));
exp->type = EXPR_UNARITHM;
exp->unarithm.op = unop_for_token(tok.token);
if (tok.token == T_BAND) {
@@ -802,7 +803,7 @@ parse_bin_expression(struct parser *par, struct ast_expression *lvalue, int i)
lex(par->lex, &tok);
}
- struct ast_expression *e = calloc(1, sizeof(struct ast_expression));
+ struct ast_expression *e = xcalloc(1, sizeof(struct ast_expression));
e->type = EXPR_BINARITHM;
e->binarithm.op = op;
e->binarithm.lvalue = lvalue;
@@ -840,7 +841,7 @@ static struct ast_expression *
parse_binding_list(struct parser *par)
{
trenter(TR_PARSE, "binding-list");
- struct ast_expression *exp = calloc(1, sizeof(struct ast_expression));
+ struct ast_expression *exp = xcalloc(1, sizeof(struct ast_expression));
exp->type = EXPR_BINDING;
unsigned int flags = 0;
@@ -865,12 +866,12 @@ parse_binding_list(struct parser *par)
while (more) {
want(par, T_NAME, &tok);
binding->name = tok.name;
- binding->initializer = calloc(1, sizeof(struct ast_expression));
+ binding->initializer = xcalloc(1, sizeof(struct ast_expression));
binding->flags = flags;
switch (lex(par->lex, &tok)) {
case T_COLON:
- binding->type = calloc(1, sizeof(struct ast_type));
+ binding->type = xcalloc(1, sizeof(struct ast_type));
parse_type(par, binding->type);
binding->type->flags |= flags;
want(par, T_EQUAL, &tok);
@@ -885,7 +886,7 @@ parse_binding_list(struct parser *par)
switch (lex(par->lex, &tok)) {
case T_COMMA:
- *next = calloc(1, sizeof(struct ast_expression_binding));
+ *next = xcalloc(1, sizeof(struct ast_expression_binding));
binding = *next;
next = &binding->next;
break;
@@ -905,7 +906,7 @@ parse_assignment(struct parser *par, struct ast_expression *object, bool indirec
{
trenter(TR_PARSE, "assign");
struct ast_expression *value = parse_complex_expression(par);
- struct ast_expression *expr = calloc(1, sizeof(struct ast_expression));
+ struct ast_expression *expr = xcalloc(1, sizeof(struct ast_expression));
expr->type = EXPR_ASSIGN;
expr->assign.object = object;
expr->assign.value = value;
@@ -977,7 +978,7 @@ parse_control_statement(struct parser *par)
{
trenter(TR_PARSE, "control-expression");
- struct ast_expression *exp = calloc(1, sizeof(struct ast_expression));
+ struct ast_expression *exp = xcalloc(1, sizeof(struct ast_expression));
struct token tok;
switch (lex(par->lex, &tok)) {
@@ -1013,7 +1014,7 @@ parse_expression_list(struct parser *par)
trenter(TR_PARSE, "expression-list");
want(par, T_LBRACE, NULL);
- struct ast_expression *exp = calloc(1, sizeof(struct ast_expression));
+ struct ast_expression *exp = xcalloc(1, sizeof(struct ast_expression));
struct ast_expression_list *cur = &exp->list;
struct ast_expression_list **next = &cur->next;
exp->type = EXPR_LIST;
@@ -1043,7 +1044,7 @@ parse_expression_list(struct parser *par)
more = false;
} else {
unlex(par->lex, &tok);
- *next = calloc(1, sizeof(struct ast_expression_list));
+ *next = xcalloc(1, sizeof(struct ast_expression_list));
cur = *next;
next = &cur->next;
}
@@ -1121,7 +1122,7 @@ parse_global_decl(struct parser *par, enum lexical_token mode,
case T_COMMA:
lex(par->lex, &tok);
if (tok.token == T_NAME || tok.token == T_ATTR_SYMBOL) {
- i->next = calloc(1, sizeof(struct ast_global_decl));
+ i->next = xcalloc(1, sizeof(struct ast_global_decl));
i = i->next;
unlex(par->lex, &tok);
break;
@@ -1163,7 +1164,7 @@ parse_type_decl(struct parser *par, struct ast_type_decl *decl)
case T_COMMA:
lex(par->lex, &tok);
if (lex(par->lex, &tok) == T_NAME) {
- i->next = calloc(1, sizeof(struct ast_type_decl));
+ i->next = xcalloc(1, sizeof(struct ast_type_decl));
i = i->next;
unlex(par->lex, &tok);
break;
@@ -1288,7 +1289,7 @@ parse_decls(struct parser *par, struct ast_decls *decls)
}
parse_decl(par, &(*next)->decl);
next = &(*next)->next;
- *next = calloc(1, sizeof(struct ast_decls));
+ *next = xcalloc(1, sizeof(struct ast_decls));
want(par, T_SEMICOLON, NULL);
if (lex(par->lex, &tok) != T_EOF) {
unlex(par->lex, &tok);
diff --git a/src/qbe.c b/src/qbe.c
@@ -4,6 +4,7 @@
#include <stdlib.h>
#include <string.h>
#include "qbe.h"
+#include "util.h"
// Simple type singletons
const struct qbe_type
@@ -156,7 +157,7 @@ qbe_append_def(struct qbe_program *prog, struct qbe_def *def)
struct qbe_value *
qval_dup(const struct qbe_value *val)
{
- struct qbe_value *new = calloc(1, sizeof(struct qbe_value));
+ struct qbe_value *new = xcalloc(1, sizeof(struct qbe_value));
*new = *val;
if (val->kind != QV_CONST) {
new->name = strdup(val->name);
@@ -178,7 +179,7 @@ va_geni(struct qbe_statement *stmt, enum qbe_instr instr,
struct qbe_arguments **next = &stmt->args;
struct qbe_value *val;
while ((val = va_arg(ap, struct qbe_value *))) {
- struct qbe_arguments *arg = calloc(1, sizeof(struct qbe_arguments));
+ struct qbe_arguments *arg = xcalloc(1, sizeof(struct qbe_arguments));
arg->value = *val;
*next = arg;
next = &arg->next;
@@ -200,7 +201,7 @@ genl(struct qbe_statement *stmt, uint64_t *id, const char *fmt)
{
stmt->type = Q_LABEL;
int n = snprintf(NULL, 0, fmt, *id);
- char *l = calloc(1, n + 1);
+ char *l = xcalloc(1, n + 1);
snprintf(l, n + 1, fmt, *id);
stmt->label = l;
*id = *id + 1;
@@ -213,14 +214,11 @@ push(struct qbe_func *func, struct qbe_statement *stmt)
if (!func->body) {
func->bsiz = 256;
func->blen = 0;
- func->body = calloc(1, sizeof(struct qbe_statement) * func->bsiz);
- assert(func->body);
+ func->body = xcalloc(1, sizeof(struct qbe_statement) * func->bsiz);
}
if (func->blen + 1 < func->bsiz) {
func->bsiz *= 2;
- struct qbe_statement *new = realloc(func->body, func->bsiz);
- func->body = new;
- assert(func->body);
+ func->body = xrealloc(func->body, func->bsiz);
}
func->body[func->blen++] = *stmt;
}
@@ -256,7 +254,7 @@ pushc(struct qbe_func *func, const char *fmt, ...)
int n = vsnprintf(NULL, 0, fmt, ap);
va_end(ap);
- char *str = calloc(1, n + 1);
+ char *str = xcalloc(1, n + 1);
va_start(ap, fmt);
vsnprintf(str, n + 1, fmt, ap);
va_end(ap);
diff --git a/src/scope.c b/src/scope.c
@@ -3,11 +3,12 @@
#include "identifier.h"
#include "scope.h"
#include "trace.h"
+#include "util.h"
struct scope *
scope_push(struct scope **stack, enum trace_sys sys)
{
- struct scope *new = calloc(1, sizeof(struct scope));
+ struct scope *new = xcalloc(1, sizeof(struct scope));
new->next = &new->objects;
if (*stack) {
new->parent = *stack;
@@ -65,7 +66,7 @@ scope_insert(struct scope *scope,
const struct identifier *ident,
const struct type *type)
{
- struct scope_object *o = calloc(1, sizeof(struct scope_object));
+ struct scope_object *o = xcalloc(1, sizeof(struct scope_object));
identifier_dup(&o->ident, ident);
o->otype = otype;
o->type = type;
diff --git a/src/type_store.c b/src/type_store.c
@@ -349,7 +349,7 @@ type_init_from_atype(struct type_store *store,
struct type_func_param *param, **next = &type->func.params;
for (struct ast_function_parameters *aparam = atype->func.params;
aparam; aparam = aparam->next) {
- param = *next = calloc(1, sizeof(struct type_func_param));
+ param = *next = xcalloc(1, sizeof(struct type_func_param));
param->type = type_store_lookup_atype(store, aparam->type);
next = ¶m->next;
}
@@ -437,7 +437,7 @@ type_store_lookup_atype(struct type_store *store, const struct ast_type *atype)
next = &bucket->next;
}
- bucket = *next = calloc(1, sizeof(struct type_bucket));
+ bucket = *next = xcalloc(1, sizeof(struct type_bucket));
type_init_from_atype(store, &bucket->type, atype);
return &bucket->type;
}
@@ -463,7 +463,7 @@ type_store_lookup_type(struct type_store *store, const struct type *type)
next = &bucket->next;
}
- bucket = *next = calloc(1, sizeof(struct type_bucket));
+ bucket = *next = xcalloc(1, sizeof(struct type_bucket));
type_init_from_type(store, &bucket->type, type);
return &bucket->type;
}
diff --git a/src/util.c b/src/util.c
@@ -1,3 +1,4 @@
+#include <stdlib.h>
#include "util.h"
unsigned long
@@ -15,3 +16,23 @@ djb2_s(unsigned long hash, const char *str)
}
return hash;
}
+
+void *
+xcalloc(size_t n, size_t s)
+{
+ void *p = calloc(n, s);
+ if (!p) {
+ abort();
+ }
+ return p;
+}
+
+void *
+xrealloc(void *p, size_t s)
+{
+ p = realloc(p, s);
+ if (!p) {
+ abort();
+ }
+ return p;
+}