diff --git a/libc3/skiplist.c.in b/libc3/skiplist.c.in
index f653350..f14e70e 100644
--- a/libc3/skiplist.c.in
+++ b/libc3/skiplist.c.in
@@ -10,12 +10,13 @@
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
-#include <assert.h>
-#include <stdlib.h>
+#include "alloc.h"
+#include "assert.h"
#include "compare.h"
#include "_NAME$.h"
#include "skiplist_node___NAME$.h"
#include "skiplist___NAME$.h"
+#include "u32.h"
/*
Random height
@@ -112,6 +113,8 @@ skiplist_init___NAME$ (s_skiplist___NAME$ *skiplist, u8 max_height, f64 spacing)
{
assert(skiplist);
skiplist->head = skiplist_node_new___NAME$(NULL, max_height);
+ if (! skiplist->head)
+ return NULL;
skiplist->compare = compare__NAME$;
skiplist->length = 0;
skiplist->max_height = max_height;
@@ -142,9 +145,14 @@ skiplist_insert___NAME$ (s_skiplist___NAME$ *skiplist, _TYPE$ _NAME$)
s_skiplist___NAME$ *
skiplist_new___NAME$ (u8 max_height, f64 spacing)
{
- s_skiplist___NAME$ *skiplist = malloc(SKIPLIST_SIZE___NAME$(max_height));
- if (skiplist)
- skiplist_init___NAME$(skiplist, max_height, spacing);
+ s_skiplist___NAME$ *skiplist;
+ skiplist = alloc(SKIPLIST_SIZE___NAME$(max_height));
+ if (! skiplist)
+ return NULL;
+ if (! skiplist_init___NAME$(skiplist, max_height, spacing)) {
+ free(skiplist);
+ return NULL;
+ }
return skiplist;
}
@@ -177,12 +185,12 @@ skiplist_random_height___NAME$ (s_skiplist___NAME$ *skiplist)
u8 height;
const t_skiplist_height *height_table;
sw max;
- t_skiplist_height k;
+ u32 k;
sw i;
assert(skiplist);
height_table = SKIPLIST_HEIGHT_TABLE___NAME$(skiplist);
max = height_table[skiplist->max_height - 1];
- k = arc4random_uniform(max);
+ u32_random_uniform(&k, max);
for (i = 0; i < skiplist->max_height && k > height_table[i]; i++)
;
height = skiplist->max_height - i;
diff --git a/libc3/skiplist__fact.c b/libc3/skiplist__fact.c
index b84381b..2be48fc 100644
--- a/libc3/skiplist__fact.c
+++ b/libc3/skiplist__fact.c
@@ -10,12 +10,13 @@
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
-#include <assert.h>
-#include <stdlib.h>
+#include "alloc.h"
+#include "assert.h"
#include "compare.h"
#include "fact.h"
#include "skiplist_node__fact.h"
#include "skiplist__fact.h"
+#include "u32.h"
/*
Random height
@@ -112,6 +113,8 @@ skiplist_init__fact (s_skiplist__fact *skiplist, u8 max_height, f64 spacing)
{
assert(skiplist);
skiplist->head = skiplist_node_new__fact(NULL, max_height);
+ if (! skiplist->head)
+ return NULL;
skiplist->compare = compare_fact;
skiplist->length = 0;
skiplist->max_height = max_height;
@@ -142,9 +145,14 @@ skiplist_insert__fact (s_skiplist__fact *skiplist, s_fact * fact)
s_skiplist__fact *
skiplist_new__fact (u8 max_height, f64 spacing)
{
- s_skiplist__fact *skiplist = malloc(SKIPLIST_SIZE__fact(max_height));
- if (skiplist)
- skiplist_init__fact(skiplist, max_height, spacing);
+ s_skiplist__fact *skiplist;
+ skiplist = alloc(SKIPLIST_SIZE__fact(max_height));
+ if (! skiplist)
+ return NULL;
+ if (! skiplist_init__fact(skiplist, max_height, spacing)) {
+ free(skiplist);
+ return NULL;
+ }
return skiplist;
}
@@ -177,12 +185,12 @@ skiplist_random_height__fact (s_skiplist__fact *skiplist)
u8 height;
const t_skiplist_height *height_table;
sw max;
- t_skiplist_height k;
+ u32 k;
sw i;
assert(skiplist);
height_table = SKIPLIST_HEIGHT_TABLE__fact(skiplist);
max = height_table[skiplist->max_height - 1];
- k = arc4random_uniform(max);
+ u32_random_uniform(&k, max);
for (i = 0; i < skiplist->max_height && k > height_table[i]; i++)
;
height = skiplist->max_height - i;
diff --git a/libc3/str.c b/libc3/str.c
index 3218392..0ba003f 100644
--- a/libc3/str.c
+++ b/libc3/str.c
@@ -10,11 +10,10 @@
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
-#include "assert.h"
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
+//#include <stdarg.h>
#include <string.h>
+#include "alloc.h"
+#include "assert.h"
#include "buf.h"
#include "buf_inspect.h"
#include "buf_parse.h"
@@ -139,12 +138,9 @@ s_str * str_init_alloc (s_str *str, uw size, const char *p)
{
s_str tmp;
assert(str);
- tmp.free.p = calloc(size + 1, 1);
- if (! tmp.free.p) {
- err_puts("str_init_alloc: failed to allocate memory.");
- assert(! "str_init_alloc: failed to allocate memory.");
+ tmp.free.p = alloc(size + 1);
+ if (! tmp.free.p)
return NULL;
- }
tmp.size = size;
tmp.ptr.p = tmp.free.p;
memcpy(tmp.free.p, p, size);
@@ -198,12 +194,9 @@ s_str * str_init_cat (s_str *str, const s_str *a, const s_str *b)
assert(a);
assert(b);
tmp.size = a->size + b->size;
- tmp.free.p = calloc(tmp.size + 1, 1);
- if (! tmp.free.p) {
- err_puts("str_init_cat: failed to allocate memory.");
- assert(! "str_init_cat: failed to allocate memory.");
+ tmp.free.p = alloc(tmp.size + 1);
+ if (! tmp.free.p)
return NULL;
- }
tmp.ptr.p = tmp.free.p;
if (a->size)
memcpy(tmp.free.ps8, a->ptr.p, a->size);
@@ -218,12 +211,9 @@ s_str * str_init_copy (s_str *str, const s_str *src)
s_str tmp = {0};
assert(str);
assert(src);
- tmp.free.p = calloc(src->size + 1, 1);
- if (! tmp.free.p) {
- err_puts("str_init_copy: failed to allocate memory.");
- assert(! "str_init_copy: failed to allocate memory.");
+ tmp.free.p = alloc(src->size + 1);
+ if (! tmp.free.p)
return NULL;
- }
tmp.size = src->size;
tmp.ptr.p = tmp.free.p;
memcpy(tmp.free.p, src->ptr.p, tmp.size);
@@ -238,12 +228,9 @@ s_str * str_init_copy_1 (s_str *str, const char *src)
assert(str);
assert(src);
len = strlen(src);
- tmp.free.p = calloc(len + 1, 1);
- if (! tmp.free.p) {
- err_puts("str_init_copy_1: failed to allocate memory.");
- assert(! "str_init_copy_1: failed to allocate memory.");
+ tmp.free.p = alloc(len + 1);
+ if (! tmp.free.p)
return NULL;
- }
tmp.size = len;
tmp.ptr.p = tmp.free.p;
memcpy(tmp.free.p, src, len + 1);
@@ -353,19 +340,16 @@ sw str_length_utf8 (const s_str *str)
s_str * str_new (char *free, uw size, const char *p)
{
s_str *str;
- str = calloc(1, sizeof(s_str));
- if (! str) {
- err_puts("str_new: failed to allocate memory");
- assert(! "str_new: failed to allocate memory");
+ str = alloc(sizeof(s_str));
+ if (! str)
return NULL;
- }
str_init(str, free, size, p);
return str;
}
s_str * str_new_1 (char *free, const char *s)
{
- size_t len = strlen(s);
+ uw len = strlen(s);
s_str *str = str_new(free, len, s);
return str;
}
@@ -374,11 +358,9 @@ s_str * str_new_cpy (const char *p, uw size)
{
char *a;
s_str *str;
- if (! (a = malloc(size))) {
- err_puts("str_new_cpy: failed to allocate memory");
- assert(! "str_new_cpy: failed to allocate memory");
+ a = alloc(size);
+ if (! a)
return NULL;
- }
memcpy(a, p, size);
str = str_new(a, size, a);
if (! str) {
@@ -393,11 +375,9 @@ s_str * str_new_copy (const s_str *src)
char *a;
s_str *dest;
assert(src);
- if (! (a = malloc(src->size))) {
- err_puts("str_new_copy: failed to allocate memory");
- assert(! "str_new_copy: failed to allocate memory");
+ a = alloc(src->size);
+ if (! a)
return NULL;
- }
memcpy(a, src->ptr.p, src->size);
dest = str_new(a, src->size, a);
if (! dest) {
diff --git a/libc3/struct.c b/libc3/struct.c
index c9379c6..01f34ad 100644
--- a/libc3/struct.c
+++ b/libc3/struct.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 "data.h"
#include "env.h"
#include "list.h"
@@ -30,12 +30,9 @@ s_struct * struct_allocate (s_struct *s)
assert(! s->data);
tmp = *s;
tmp.free_data = true;
- tmp.data = calloc(1, tmp.type->size);
- if (! tmp.data) {
- err_puts("struct_allocate: failed to allocate memory");
- assert(! "struct_allocate: failed to allocate memory");
+ tmp.data = alloc(tmp.type->size);
+ if (! tmp.data)
return NULL;
- }
*s = tmp;
return s;
}
@@ -144,12 +141,9 @@ s_struct * struct_init_copy (s_struct *s, const s_struct *src)
tmp.type = src->type;
if (src->data) {
tmp.free_data = true;
- tmp.data = calloc(1, tmp.type->size);
- if (! tmp.data) {
- err_puts("struct_init_copy: failed to allocate memory for data");
- assert(! "struct_init_copy: failed to allocate memory for data");
+ tmp.data = alloc(tmp.type->size);
+ if (! tmp.data)
return NULL;
- }
i = 0;
while (i < tmp.type->map.count) {
if (! tag_type(tmp.type->map.value + i, &sym) ||
@@ -160,14 +154,9 @@ s_struct * struct_init_copy (s_struct *s, const s_struct *src)
}
}
else if (src->tag) {
- tmp.tag = calloc(tmp.type->map.count, sizeof(s_tag));
- if (! tmp.tag) {
- err_puts("struct_init_copy:"
- " failed to allocate memory for tags (1)");
- assert(! "struct_init_copy:"
- " failed to allocate memory for tags (1)");
+ tmp.tag = alloc(tmp.type->map.count * sizeof(s_tag));
+ if (! tmp.tag)
return NULL;
- }
i = 0;
while (i < tmp.type->map.count) {
if (! tag_init_copy(tmp.tag + i, src->tag + i))
@@ -195,12 +184,9 @@ s_struct * struct_init_from_lists (s_struct *s, const s_sym *module,
assert(list_length(keys) == list_length(values));
if (! struct_init(&tmp, module))
return NULL;
- tmp.tag = calloc(tmp.type->map.count, sizeof(s_tag));
- if (! tmp.tag) {
- err_puts("struct_init_from_lists: failed to allocate memory (tag)");
- assert(! "struct_init_from_lists: failed to allocate memory (tag)");
+ tmp.tag = alloc(tmp.type->map.count * sizeof(s_tag));
+ if (! tmp.tag)
return NULL;
- }
k = keys;
v = values;
while (k && v) {
@@ -256,12 +242,9 @@ s_struct * struct_new (const s_sym *module)
{
s_struct *s;
assert(module);
- s = calloc(1, sizeof(s_struct));
- if (! s) {
- err_puts("struct_new: failed to allocate memory");
- assert(! "struct_new: failed to allocate memory");
+ s = alloc(sizeof(s_struct));
+ if (! s)
return NULL;
- }
if (! struct_init(s, module)) {
free(s);
return NULL;
@@ -273,12 +256,9 @@ s_struct * struct_new_1 (const s8 *p)
{
s_struct *s;
assert(p);
- s = calloc(1, sizeof(s_struct));
- if (! s) {
- err_puts("struct_new_1: failed to allocate memory");
- assert(! "struct_new_1: failed to allocate memory");
+ s = alloc(sizeof(s_struct));
+ if (! s)
return NULL;
- }
if (! struct_init_1(s, p)) {
free(s);
return NULL;
@@ -290,12 +270,9 @@ s_struct * struct_new_copy (const s_struct *src)
{
s_struct *s;
assert(src);
- s = calloc(1, sizeof(s_struct));
- if (! s) {
- err_puts("struct_new_copy: failed to allocate memory");
- assert(! "struct_new_copy: failed to allocate memory");
+ s = alloc(sizeof(s_struct));
+ if (! s)
return NULL;
- }
if (! struct_init_copy(s, src)) {
free(s);
return NULL;
diff --git a/libc3/struct_type.c b/libc3/struct_type.c
index 9837d79..000a818 100644
--- a/libc3/struct_type.c
+++ b/libc3/struct_type.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 "env.h"
#include "list.h"
#include "map.h"
@@ -66,12 +66,8 @@ s_struct_type * struct_type_init (s_struct_type *st,
st->module = module;
if (! map_init(&st->map, count))
return NULL;
- st->offset = calloc(count, sizeof(uw));
+ st->offset = alloc(count * sizeof(uw));
if (! st->offset) {
- err_puts("struct_type_init: failed to allocate memory:"
- " offset array");
- assert(! "struct_type_init: failed to allocate memory:"
- " offset array");
map_clean(&st->map);
return NULL;
}
@@ -124,12 +120,8 @@ s_struct_type * struct_type_init_copy (s_struct_type *s,
tmp.module = src->module;
if (! map_init_copy(&tmp.map, &src->map))
return NULL;
- tmp.offset = calloc(tmp.map.count, sizeof(uw));
+ tmp.offset = alloc(tmp.map.count * sizeof(uw));
if (! tmp.offset) {
- err_puts("struct_type_init_copy: failed to allocate memory:"
- " offset array");
- assert(! "struct_type_init_copy: failed to allocate memory:"
- " offset array");
map_clean(&tmp.map);
return NULL;
}
@@ -161,12 +153,9 @@ s_struct_type * struct_type_new (const s_sym *module,
{
s_struct_type *st;
assert(module);
- st = calloc(1, sizeof(s_struct_type));
- if (! st) {
- err_puts("struct_type_new: failed to allocate memory");
- assert(! "struct_type_new: failed to allocate memory");
+ st = alloc(sizeof(s_struct_type));
+ if (! st)
return NULL;
- }
if (! struct_type_init(st, module, spec)) {
free(st);
return NULL;