diff --git a/libc3/array.c b/libc3/array.c
index 1dd34d9..5d05103 100644
--- a/libc3/array.c
+++ b/libc3/array.c
@@ -179,7 +179,9 @@ s_array * array_init (s_array *a, const s_sym *array_type, uw dimension,
}
#endif
tmp.dimension = dimension;
- tmp.dimensions = calloc(dimension, sizeof(s_array_dimension));
+ tmp.dimensions = alloc(dimension * sizeof(s_array_dimension));
+ if (! tmp.dimensions)
+ return NULL;
i = 0;
while (i < dimension) {
tmp.dimensions[i].count = dimensions[i];
@@ -270,10 +272,8 @@ s_array * array_init_copy (s_array *a, const s_array *src)
return NULL;
if (tmp.dimension) {
if (src->data) {
- tmp.data = tmp.free_data = calloc(1, tmp.size);
+ tmp.data = tmp.free_data = alloc(tmp.size);
if (! tmp.data) {
- err_puts("array_init_copy: failed to allocate memory");
- assert(! "array_init_copy: failed to allocate memory");
free(tmp.dimensions);
return NULL;
}
@@ -290,10 +290,8 @@ s_array * array_init_copy (s_array *a, const s_array *src)
}
}
else if (src->tags) {
- tmp.tags = calloc(src->count, sizeof(s_tag));
+ tmp.tags = alloc(src->count * sizeof(s_tag));
if (! tmp.tags) {
- err_puts("array_init_copy: failed to allocate memory");
- assert(! "array_init_copy: failed to allocate memory");
free(tmp.dimensions);
return NULL;
}
diff --git a/libc3/block.c b/libc3/block.c
index 8a71554..cf3a1ff 100644
--- a/libc3/block.c
+++ b/libc3/block.c
@@ -10,9 +10,9 @@
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
-#include "assert.h"
-#include <stdlib.h>
#include <string.h>
+#include "alloc.h"
+#include "assert.h"
#include "block.h"
#include "buf.h"
#include "buf_inspect.h"
@@ -42,12 +42,9 @@ s_block * block_init (s_block *block, uw count)
assert(block);
tmp.count = count;
if (count) {
- tmp.tag = calloc(count, sizeof(s_tag));
- if (! tmp.tag) {
- err_puts("block_init: failed to allocate memory");
- assert(! "block_init: failed to allocate memory");
+ tmp.tag = alloc(count * sizeof(s_tag));
+ if (! tmp.tag)
return NULL;
- }
}
*block = tmp;
return block;
@@ -128,12 +125,9 @@ s_str * block_inspect (const s_block *x, s_str *dest)
s_block * block_new (uw count)
{
s_block *block;
- block = malloc(sizeof(s_block));
- if (! block) {
- err_puts("block_new: failed to allocate memory");
- assert(! "block_new: failed to allocate memory");
+ block = alloc(sizeof(s_block));
+ if (! block)
return NULL;
- }
if (! block_init(block, count)) {
free(block);
return NULL;
@@ -144,12 +138,9 @@ s_block * block_new (uw count)
s_block * block_new_1 (const char *p)
{
s_block *block;
- block = malloc(sizeof(s_block));
- if (! block) {
- err_puts("block_new_1: failed to allocate memory");
- assert(! "block_new_1: failed to allocate memory");
+ block = alloc(sizeof(s_block));
+ if (! block)
return NULL;
- }
if (! block_init_1(block, p)) {
free(block);
return NULL;
diff --git a/libc3/buf_inspect.c b/libc3/buf_inspect.c
index 5fd2f06..17c1907 100644
--- a/libc3/buf_inspect.c
+++ b/libc3/buf_inspect.c
@@ -10,12 +10,12 @@
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
-#include "assert.h"
#include <float.h>
#include <math.h>
-#include <stdlib.h>
#include <string.h>
#include "../libtommath/tommath.h"
+#include "alloc.h"
+#include "assert.h"
#include "array.h"
#include "buf.h"
#include "buf_inspect.h"
@@ -85,7 +85,9 @@ sw buf_inspect_array_data (s_buf *buf, const s_array *array)
data = array->data;
else
tag = array->tags;
- address = calloc(array->dimension, sizeof(uw));
+ address = alloc(array->dimension * sizeof(uw));
+ if (! address)
+ return -1;
r = buf_inspect_array_data_rec(buf, array, (const u8 **) &data,
(const s_tag **) &tag, address, 0);
free(address);
@@ -150,7 +152,9 @@ sw buf_inspect_array_data_size (const s_array *array)
data = array->data;
else
tag = array->tags;
- address = calloc(array->dimension, sizeof(uw));
+ address = alloc(array->dimension * sizeof(uw));
+ if (! address)
+ return -1;
r = buf_inspect_array_data_size_rec(array, (const u8 **) &data,
(const s_tag **) &tag,
address, 0);
diff --git a/libc3/buf_inspect_u.c.in b/libc3/buf_inspect_u.c.in
index a298580..f339ff4 100644
--- a/libc3/buf_inspect_u.c.in
+++ b/libc3/buf_inspect_u.c.in
@@ -11,6 +11,7 @@
* THIS SOFTWARE.
*/
/* Gen from buf_inspect_u.c.in BITS=_BITS$ bits=_bits$ */
+#include "alloc.h"
#include "buf.h"
#include "buf_inspect.h"
#include "buf_save.h"
@@ -43,7 +44,7 @@ sw buf_inspect_u_bits$_base (s_buf *buf,
return buf_write_character_utf8(buf, zero);
}
size = buf_inspect_u_bits$_base_digits(base, u);
- c = calloc(size, sizeof(character));
+ c = alloc(size * sizeof(character));
buf_save_init(buf, &save);
radix = base->size;
i = 0;
diff --git a/libc3/buf_inspect_u16.c b/libc3/buf_inspect_u16.c
index ab19326..be7b2b1 100644
--- a/libc3/buf_inspect_u16.c
+++ b/libc3/buf_inspect_u16.c
@@ -11,6 +11,7 @@
* THIS SOFTWARE.
*/
/* Gen from buf_inspect_u.c.in BITS=16 bits=16 */
+#include "alloc.h"
#include "buf.h"
#include "buf_inspect.h"
#include "buf_save.h"
@@ -43,7 +44,7 @@ sw buf_inspect_u16_base (s_buf *buf,
return buf_write_character_utf8(buf, zero);
}
size = buf_inspect_u16_base_digits(base, u);
- c = calloc(size, sizeof(character));
+ c = alloc(size * sizeof(character));
buf_save_init(buf, &save);
radix = base->size;
i = 0;
diff --git a/libc3/buf_inspect_u32.c b/libc3/buf_inspect_u32.c
index dc46a90..a489b33 100644
--- a/libc3/buf_inspect_u32.c
+++ b/libc3/buf_inspect_u32.c
@@ -11,6 +11,7 @@
* THIS SOFTWARE.
*/
/* Gen from buf_inspect_u.c.in BITS=32 bits=32 */
+#include "alloc.h"
#include "buf.h"
#include "buf_inspect.h"
#include "buf_save.h"
@@ -43,7 +44,7 @@ sw buf_inspect_u32_base (s_buf *buf,
return buf_write_character_utf8(buf, zero);
}
size = buf_inspect_u32_base_digits(base, u);
- c = calloc(size, sizeof(character));
+ c = alloc(size * sizeof(character));
buf_save_init(buf, &save);
radix = base->size;
i = 0;
diff --git a/libc3/buf_inspect_u64.c b/libc3/buf_inspect_u64.c
index 8a4b2fd..10bfcac 100644
--- a/libc3/buf_inspect_u64.c
+++ b/libc3/buf_inspect_u64.c
@@ -11,6 +11,7 @@
* THIS SOFTWARE.
*/
/* Gen from buf_inspect_u.c.in BITS=64 bits=64 */
+#include "alloc.h"
#include "buf.h"
#include "buf_inspect.h"
#include "buf_save.h"
@@ -43,7 +44,7 @@ sw buf_inspect_u64_base (s_buf *buf,
return buf_write_character_utf8(buf, zero);
}
size = buf_inspect_u64_base_digits(base, u);
- c = calloc(size, sizeof(character));
+ c = alloc(size * sizeof(character));
buf_save_init(buf, &save);
radix = base->size;
i = 0;
diff --git a/libc3/buf_inspect_u8.c b/libc3/buf_inspect_u8.c
index 18b8f76..4ffca3f 100644
--- a/libc3/buf_inspect_u8.c
+++ b/libc3/buf_inspect_u8.c
@@ -11,6 +11,7 @@
* THIS SOFTWARE.
*/
/* Gen from buf_inspect_u.c.in BITS=8 bits=8 */
+#include "alloc.h"
#include "buf.h"
#include "buf_inspect.h"
#include "buf_save.h"
@@ -43,7 +44,7 @@ sw buf_inspect_u8_base (s_buf *buf,
return buf_write_character_utf8(buf, zero);
}
size = buf_inspect_u8_base_digits(base, u);
- c = calloc(size, sizeof(character));
+ c = alloc(size * sizeof(character));
buf_save_init(buf, &save);
radix = base->size;
i = 0;
diff --git a/libc3/buf_inspect_uw.c b/libc3/buf_inspect_uw.c
index d1e8466..f1a62cf 100644
--- a/libc3/buf_inspect_uw.c
+++ b/libc3/buf_inspect_uw.c
@@ -11,6 +11,7 @@
* THIS SOFTWARE.
*/
/* Gen from buf_inspect_u.c.in BITS=W bits=w */
+#include "alloc.h"
#include "buf.h"
#include "buf_inspect.h"
#include "buf_save.h"
@@ -43,7 +44,7 @@ sw buf_inspect_uw_base (s_buf *buf,
return buf_write_character_utf8(buf, zero);
}
size = buf_inspect_uw_base_digits(base, u);
- c = calloc(size, sizeof(character));
+ c = alloc(size * sizeof(character));
buf_save_init(buf, &save);
radix = base->size;
i = 0;
diff --git a/libc3/buf_parse.c b/libc3/buf_parse.c
index 560c386..ca7a5cf 100644
--- a/libc3/buf_parse.c
+++ b/libc3/buf_parse.c
@@ -10,12 +10,12 @@
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
-#include "assert.h"
-#include <stdlib.h>
-#include <string.h>
#include <math.h>
+#include <string.h>
#include "../libtommath/tommath.h"
+#include "alloc.h"
#include "array.h"
+#include "assert.h"
#include "block.h"
#include "buf.h"
#include "buf_inspect.h"
@@ -106,10 +106,9 @@ sw buf_parse_array_data (s_buf *buf, s_array *dest)
return result;
}
tmp = *dest;
- if (! (address = calloc(tmp.dimension, sizeof(sw)))) {
- err_puts("buf_parse_array_data: out of memory: address");
+ address = alloc(tmp.dimension * sizeof(sw));
+ if (! address)
return -1;
- }
tmp.count = 1;
i = 0;
while (i < tmp.dimension) {
@@ -117,9 +116,9 @@ sw buf_parse_array_data (s_buf *buf, s_array *dest)
i++;
}
tmp.size = tmp.dimensions[0].count * tmp.dimensions[0].item_size;
- if (! (tmp.tags = calloc(tmp.count, sizeof(s_tag)))) {
+ tmp.tags = alloc(tmp.count * sizeof(s_tag));
+ if (! tmp.tags) {
free(address);
- err_puts("buf_parse_array_data: out of memory: tags");
return -1;
}
tag = tmp.tags;
@@ -263,11 +262,10 @@ sw buf_parse_array_dimension_count (s_buf *buf, s_array *dest)
goto restore;
result += r;
}
- if (! (tmp.dimensions = calloc(tmp.dimension,
- sizeof(s_array_dimension)))) {
- err_puts("tmp.dimensions: failed to allocate memory");
+ tmp.dimensions = alloc(tmp.dimension *
+ sizeof(s_array_dimension));
+ if (! tmp.dimensions)
return -1;
- }
*dest = tmp;
r = result;
goto clean;
@@ -294,7 +292,9 @@ sw buf_parse_array_dimensions (s_buf *buf, s_array *dest)
assert(! "buf_parse_array_dimensions: zero item size");
return -1;
}
- address = calloc(tmp.dimension, sizeof(sw));
+ address = alloc(tmp.dimension * sizeof(sw));
+ if (! address)
+ return -1;
tmp.dimensions[tmp.dimension - 1].item_size = size;
if ((r = buf_parse_array_dimensions_rec(buf, &tmp, address,
0)) < 0) {
diff --git a/libc3/complex.c b/libc3/complex.c
index 2126fda..f514f54 100644
--- a/libc3/complex.c
+++ b/libc3/complex.c
@@ -10,9 +10,9 @@
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
-#include "assert.h"
#include <math.h>
-#include <stdlib.h>
+#include "alloc.h"
+#include "assert.h"
#include "f32.h"
#include "f64.h"
#include "tag.h"
@@ -79,12 +79,9 @@ s_complex * complex_mul (const s_complex *a, const s_complex *b,
s_complex * complex_new (void)
{
s_complex *c;
- c = calloc(1, sizeof(s_complex));
- if (! c) {
- err_puts("complex_new_copy: failed to allocate memory");
- assert(! "complex_new_copy: failed to allocate memory");
+ c = alloc(sizeof(s_complex));
+ if (! c)
return NULL;
- }
if (! complex_init(c)) {
free(c);
return NULL;
@@ -96,12 +93,9 @@ s_complex * complex_new_copy (const s_complex *src)
{
s_complex *c;
assert(src);
- c = calloc(1, sizeof(s_complex));
- if (! c) {
- err_puts("complex_new_copy: failed to allocate memory");
- assert(! "complex_new_copy: failed to allocate memory");
+ c = alloc(sizeof(s_complex));
+ if (! c)
return NULL;
- }
if (! complex_init_copy(c, src)) {
free(c);
return NULL;
diff --git a/libc3/env.c b/libc3/env.c
index 81e0ae7..e00a429 100644
--- a/libc3/env.c
+++ b/libc3/env.c
@@ -10,9 +10,9 @@
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
+#include <unistd.h>
#include "alloc.h"
#include "assert.h"
-#include <unistd.h>
#include "array.h"
#include "binding.h"
#include "block.h"
@@ -913,12 +913,9 @@ bool env_eval_quote_struct (s_env *env, const s_struct *s, s_tag *dest)
return true;
}
t->type = s->type;
- t->tag = calloc(t->type->map.count, sizeof(s_tag));
- if (! t->tag) {
- err_puts("env_eval_quote_struct: failed to allocate memory");
- assert(! "env_eval_quote_struct: failed to allocate memory");
+ t->tag = alloc(t->type->map.count * sizeof(s_tag));
+ if (! t->tag)
return false;
- }
i = 0;
while (i < t->type->map.count) {
if (! env_eval_quote_tag(env, s->tag + i, t->tag + i))
diff --git a/libc3/fact_list.c b/libc3/fact_list.c
index 461516d..477150c 100644
--- a/libc3/fact_list.c
+++ b/libc3/fact_list.c
@@ -10,7 +10,7 @@
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
-#include <stdlib.h>
+#include "alloc.h"
#include "assert.h"
#include "fact_list.h"
@@ -44,12 +44,9 @@ s_fact_list * fact_list_init (s_fact_list *fl, s_fact *fact,
s_fact_list * fact_list_new (s_fact *fact, s_fact_list *next)
{
s_fact_list *fl;
- fl = malloc(sizeof(s_fact_list));
- if (! fl) {
- err_puts("fact_list_new: failed to allocate memory");
- assert(! "fact_list_new: failed to allocate memory");
+ fl = alloc(sizeof(s_fact_list));
+ if (! fl)
return NULL;
- }
if (! fact_list_init(fl, fact, next)) {
free(fl);
return NULL;
diff --git a/libc3/facts.c b/libc3/facts.c
index 74c0f3f..55dfdd1 100644
--- a/libc3/facts.c
+++ b/libc3/facts.c
@@ -590,10 +590,9 @@ void facts_remove_all (s_facts *facts)
count = facts->facts.count;
if (! count)
return;
- f = calloc(count, sizeof(s_fact *));
+ f = alloc(count * sizeof(s_fact *));
if (! f) {
- err_puts("facts_remove_all: failed to allocate memory");
- assert(! "facts_remove_all: failed to allocate memory");
+ abort();
return;
}
i = 0;
diff --git a/libc3/facts_with.c b/libc3/facts_with.c
index 2652e40..c983c67 100644
--- a/libc3/facts_with.c
+++ b/libc3/facts_with.c
@@ -10,8 +10,8 @@
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
+#include "alloc.h"
#include "assert.h"
-#include <stdlib.h>
#include "buf.h"
#include "buf_inspect.h"
#include "env.h"
@@ -32,8 +32,10 @@ s_facts_with_cursor * facts_with (s_facts *facts,
cursor->facts = facts;
cursor->facts_count = facts_count;
if (facts_count > 0) {
- cursor->levels = calloc(facts_count,
- sizeof(s_facts_with_cursor_level));
+ cursor->levels = alloc(facts_count *
+ sizeof(s_facts_with_cursor_level));
+ if (! cursor->levels)
+ return NULL;
cursor->spec = facts_spec_new_expand(spec);
/*
buf_inspect_facts_spec(&g_c3_env.err, spec);
diff --git a/libc3/fn.c b/libc3/fn.c
index 1629ea8..7137f98 100644
--- a/libc3/fn.c
+++ b/libc3/fn.c
@@ -10,9 +10,9 @@
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
-#include "assert.h"
-#include <stdlib.h>
#include <string.h>
+#include "alloc.h"
+#include "assert.h"
#include "arg.h"
#include "binding.h"
#include "buf.h"
@@ -93,12 +93,9 @@ s_fn * fn_init_copy (s_fn *fn, const s_fn *src)
s_fn * fn_new (void)
{
s_fn *fn;
- fn = calloc(1, sizeof(s_fn));
- if (! fn) {
- err_puts("fn_new: failed to allocate memory");
- assert(! "fn_new: failed to allocate memory");
+ fn = alloc(sizeof(s_fn));
+ if (! fn)
return NULL;
- }
fn_init(fn);
return fn;
}
@@ -107,12 +104,9 @@ s_fn * fn_new_copy (const s_fn *src)
{
s_fn *fn;
assert(src);
- fn = calloc(1, sizeof(s_fn));
- if (! fn) {
- err_puts("fn_new_copy: failed to allocate memory");
- assert(! "fn_new_copy: failed to allocate memory");
+ fn = alloc(sizeof(s_fn));
+ if (! fn)
return NULL;
- }
if (! fn_init_copy(fn, src)) {
free(fn);
return NULL;
diff --git a/libc3/list.c b/libc3/list.c
index d66a86c..5aa8e22 100644
--- a/libc3/list.c
+++ b/libc3/list.c
@@ -10,9 +10,9 @@
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
-#include "assert.h"
-#include <stdlib.h>
#include <string.h>
+#include "alloc.h"
+#include "assert.h"
#include "array.h"
#include "buf.h"
#include "buf_inspect.h"
@@ -188,10 +188,9 @@ s_list * list_next (const s_list *list)
s_list * list_new (s_list *next)
{
s_list *dest;
- if (! (dest = calloc(1, sizeof(s_list)))) {
- err_puts("list_new: failed to allocate memory");
+ dest = alloc(sizeof(s_list));
+ if (! dest)
return NULL;
- }
return list_init(dest, next);
}
@@ -201,7 +200,8 @@ s_list * list_new_1 (const char *p)
s_list *list;
buf_init_1(&buf, false, (char *) p);
if (buf_parse_list(&buf, &list) != (sw) strlen(p)) {
- assert(! "invalid list");
+ err_puts("list_new_1: invalid list");
+ assert(! "list_new_1: invalid list");
return NULL;
}
return list;
@@ -275,20 +275,15 @@ s_array * list_to_array (const s_list *list, const s_sym *array_type,
}
if (len) {
tmp.dimension = 1;
- tmp.dimensions = calloc(1, sizeof(s_array_dimension));
- if (! tmp.dimensions) {
- err_puts("list_to_array: out of memory: 1");
- assert(! "list_to_array: out of memory: 1");
+ tmp.dimensions = alloc(sizeof(s_array_dimension));
+ if (! tmp.dimensions)
return NULL;
- }
tmp.count = len;
tmp.dimensions[0].count = len;
tmp.dimensions[0].item_size = size;
tmp.size = len * size;
- tmp.free_data = calloc(len, size);
+ tmp.free_data = alloc(len * size);
if (! tmp.free_data) {
- err_puts("list_to_array: out of memory: 2");
- assert(! "list_to_array: out of memory: 2");
free(tmp.dimensions);
return NULL;
}