diff --git a/libc3/fn_clause.c b/libc3/fn_clause.c
index 02c1002..1f0d147 100644
--- a/libc3/fn_clause.c
+++ b/libc3/fn_clause.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 "arg.h"
#include "binding.h"
#include "block.h"
@@ -69,11 +69,8 @@ s_fn_clause * fn_clause_init (s_fn_clause *fn_clause, s_fn_clause *next_clause)
s_fn_clause * fn_clause_new (s_fn_clause *next_clause)
{
s_fn_clause *fn_clause;
- fn_clause = calloc(1, sizeof(s_fn_clause));
- if (! fn_clause) {
- err_puts("fn_clause_new: failed to allocate memory");
- assert(! "fn_clause_new: failed to allocate memory");
+ fn_clause = alloc(sizeof(s_fn_clause));
+ if (! fn_clause)
return NULL;
- }
return fn_clause_init(fn_clause, next_clause);
}
diff --git a/libc3/frame.c b/libc3/frame.c
index 8a4f3fd..1eb0ca7 100644
--- a/libc3/frame.c
+++ b/libc3/frame.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 "binding.h"
#include "frame.h"
#include "list.h"
@@ -69,11 +69,8 @@ s_frame * frame_init (s_frame *frame, s_frame *next)
s_frame * frame_new (s_frame *next)
{
s_frame *frame;
- frame = calloc(1, sizeof(s_frame));
- if (! frame) {
- err_puts("frame_new: failed to allocate memory");
- assert(! "frame_new: failed to allocate memory");
+ frame = alloc(sizeof(s_frame));
+ if (! frame)
return NULL;
- }
return frame_init(frame, next);
}
diff --git a/libc3/integer.c b/libc3/integer.c
index 0f6254c..3da7596 100644
--- a/libc3/integer.c
+++ b/libc3/integer.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_parse.h"
#include "compare.h"
@@ -468,12 +468,9 @@ s_integer * integer_neg (const s_integer *a, s_integer *dest)
s_integer * integer_new (void)
{
s_integer *a = NULL;
- a = calloc(1, sizeof(s_integer));
- if (! a) {
- err_puts("integer_new: failed to allocate memory");
- assert(! "integer_new: failed to allocate memory");
+ a = alloc(sizeof(s_integer));
+ if (! a)
return NULL;
- }
if (! integer_init(a)) {
free(a);
return NULL;
@@ -485,12 +482,9 @@ s_integer * integer_new_copy (const s_integer *src)
{
s_integer *a;
assert(src);
- a = calloc(1, sizeof(s_integer));
- if (! a) {
- err_puts("integer_new_copy: failed to allocate memory");
- assert(! "integer_new_copy: failed to allocate memory");
+ a = alloc(sizeof(s_integer));
+ if (! a)
return NULL;
- }
if (! integer_init_copy(a, src)) {
free(a);
return NULL;
diff --git a/libc3/log.c b/libc3/log.c
index ff0ab04..c0a07a6 100644
--- a/libc3/log.c
+++ b/libc3/log.c
@@ -10,6 +10,7 @@
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
+#include "alloc.h"
#include "assert.h"
#include "buf.h"
#include "buf_file.h"
@@ -47,11 +48,9 @@ s_log * log_init (s_log *log)
s_log * log_new (void)
{
s_log *log;
- log = malloc(sizeof(s_log));
- if (! log) {
- err_puts("log_new: failed to allocate memory");
+ log = alloc(sizeof(s_log));
+ if (! log)
return NULL;
- }
if (! log_init(log)) {
free(log);
return NULL;
diff --git a/libc3/map.c b/libc3/map.c
index 7626f44..152471a 100644
--- a/libc3/map.c
+++ b/libc3/map.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 "buf.h"
#include "buf_parse.h"
#include "compare.h"
@@ -78,10 +78,20 @@ s_tag * map_get (const s_map *map, const s_tag *key, s_tag *value)
s_map * map_init (s_map *map, uw count)
{
+ s_map tmp = {0};
assert(map);
- map->count = count;
- map->key = calloc(count, sizeof(s_tag));
- map->value = calloc(count, sizeof(s_tag));
+ if (count) {
+ tmp.count = count;
+ tmp.key = alloc(count * sizeof(s_tag));
+ if (! tmp.key)
+ return NULL;
+ tmp.value = alloc(count * sizeof(s_tag));
+ if (! tmp.value) {
+ free(tmp.key);
+ return NULL;
+ }
+ }
+ *map = tmp;
return map;
}
@@ -113,15 +123,21 @@ s_map * map_init_1 (s_map *map, const char *p)
s_map * map_init_copy (s_map *map, const s_map *src)
{
uw i = 0;
+ s_map tmp = {0};
assert(src);
assert(map);
- map_init(map, src->count);
- while (i < src->count) {
- tag_init_copy(map->key + i, src->key + i);
- tag_init_copy(map->value + i, src->value + i);
+ if (! map_init(&tmp, src->count))
+ return NULL;
+ while (i < tmp.count) {
+ if (! tag_init_copy(tmp.key + i, src->key + i) ||
+ ! tag_init_copy(tmp.value + i, src->value + i))
+ goto ko;
i++;
}
return map;
+ ko:
+ map_clean(&tmp);
+ return NULL;
}
s_map * map_init_from_lists (s_map *map, const s_list *keys,
@@ -130,6 +146,7 @@ s_map * map_init_from_lists (s_map *map, const s_list *keys,
sw i = 0;
const s_list *k;
sw len;
+ s_map tmp = {0};
const s_list *v;
assert(map);
if ((len = list_length(keys)) != list_length(values)) {
@@ -139,12 +156,12 @@ s_map * map_init_from_lists (s_map *map, const s_list *keys,
" keys and values length do not match");
return NULL;
}
- map_init(map, len);
+ map_init(&tmp, len);
k = keys;
v = values;
while (i < len) {
- if (! tag_init_copy(map->key + i, &k->tag) ||
- ! tag_init_copy(map->value + i, &v->tag))
+ if (! tag_init_copy(tmp.key + i, &k->tag) ||
+ ! tag_init_copy(tmp.value + i, &v->tag))
goto ko;
k = list_next(k);
v = list_next(v);
@@ -189,12 +206,9 @@ s_list ** map_map (const s_map *map, const s_fn *fn, s_list **result)
s_map * map_new (uw count)
{
s_map *map;
- map = calloc(1, sizeof(s_map));
- if (! map) {
- err_puts("map_new: failed to allocate memory");
- assert(! "map_new: failed to allocate memory");
+ map = alloc(sizeof(s_map));
+ if (! map)
return NULL;
- }
if (! map_init(map, count)) {
free(map);
return NULL;
@@ -205,12 +219,9 @@ s_map * map_new (uw count)
s_map * map_new_1 (const char *p)
{
s_map *map;
- map = calloc(1, sizeof(s_map));
- if (! map) {
- err_puts("map_new_1: failed to allocate memory");
- assert(! "map_new_1: failed to allocate memory");
+ map = alloc(sizeof(s_map));
+ if (! map)
return NULL;
- }
if (! map_init_1(map, p)) {
free(map);
return NULL;
@@ -221,12 +232,9 @@ s_map * map_new_1 (const char *p)
s_map * map_new_from_lists (const s_list *keys, const s_list *values)
{
s_map *map;
- map = calloc(1, sizeof(s_map));
- if (! map) {
- err_puts("map_new_from_lists: failed to allocate memory");
- assert(! "map_new_from_lists: failed to allocate memory");
+ map = alloc(sizeof(s_map));
+ if (! map)
return NULL;
- }
if (! map_init_from_lists(map, keys, values)) {
free(map);
return NULL;
diff --git a/libc3/ptr.c b/libc3/ptr.c
index ee82b04..688209a 100644
--- a/libc3/ptr.c
+++ b/libc3/ptr.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 "integer.h"
#include "ptr.h"
#include "tag_type.h"
@@ -75,12 +75,9 @@ u_ptr_w * ptr_init_copy (u_ptr_w *ptr, const u_ptr_w *src)
u_ptr_w * ptr_new (void *p)
{
u_ptr_w *ptr;
- ptr = calloc(1, sizeof(u_ptr_w));
- if (! ptr) {
- err_puts("ptr_new: failed to allocate memory");
- assert(! "ptr_new: failed to allocate memory");
+ ptr = alloc(sizeof(u_ptr_w));
+ if (! ptr)
return NULL;
- }
if (! ptr_init(ptr, p)) {
free(ptr);
return NULL;
@@ -92,12 +89,9 @@ u_ptr_w * ptr_new_copy (const u_ptr_w *src)
{
u_ptr_w *ptr;
assert(src);
- ptr = calloc(1, sizeof(u_ptr_w));
- if (! ptr) {
- err_puts("ptr_new_copy: failed to allocate memory");
- assert(! "ptr_new_copy: failed to allocate memory");
+ ptr = alloc(sizeof(u_ptr_w));
+ if (! ptr)
return NULL;
- }
if (! ptr_init_copy(ptr, src)) {
free(ptr);
return NULL;
diff --git a/libc3/ptr_free.c b/libc3/ptr_free.c
index bacfc67..81e9890 100644
--- a/libc3/ptr_free.c
+++ b/libc3/ptr_free.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 "integer.h"
#include "ptr_free.h"
#include "tag_type.h"
@@ -81,12 +81,9 @@ u_ptr_w * ptr_free_init_copy (u_ptr_w *ptr_free, const u_ptr_w *src)
u_ptr_w * ptr_free_new (void *p)
{
u_ptr_w *ptr_free;
- ptr_free = calloc(1, sizeof(u_ptr_w));
- if (! ptr_free) {
- err_puts("ptr_free_new: failed to allocate memory");
- assert(! "ptr_free_new: failed to allocate memory");
+ ptr_free = alloc(sizeof(u_ptr_w));
+ if (! ptr_free)
return NULL;
- }
if (! ptr_free_init(ptr_free, p)) {
free(ptr_free);
return NULL;
@@ -98,12 +95,9 @@ u_ptr_w * ptr_free_new_copy (const u_ptr_w *src)
{
u_ptr_w *ptr_free;
assert(src);
- ptr_free = calloc(1, sizeof(u_ptr_w));
- if (! ptr_free) {
- err_puts("ptr_free_new_copy: failed to allocate memory");
- assert(! "ptr_free_new_copy: failed to allocate memory");
+ ptr_free = alloc(sizeof(u_ptr_w));
+ if (! ptr_free)
return NULL;
- }
if (! ptr_free_init_copy(ptr_free, src)) {
free(ptr_free);
return NULL;
diff --git a/libc3/skiplist.c.in b/libc3/skiplist.c.in
index f14e70e..f39a420 100644
--- a/libc3/skiplist.c.in
+++ b/libc3/skiplist.c.in
@@ -184,13 +184,15 @@ skiplist_random_height___NAME$ (s_skiplist___NAME$ *skiplist)
{
u8 height;
const t_skiplist_height *height_table;
- sw max;
- u32 k;
sw i;
+ u32 j;
+ sw max;
+ t_skiplist_height k;
assert(skiplist);
height_table = SKIPLIST_HEIGHT_TABLE___NAME$(skiplist);
max = height_table[skiplist->max_height - 1];
- u32_random_uniform(&k, max);
+ u32_random_uniform(&j, max);
+ k = j;
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 2be48fc..6387867 100644
--- a/libc3/skiplist__fact.c
+++ b/libc3/skiplist__fact.c
@@ -184,13 +184,15 @@ skiplist_random_height__fact (s_skiplist__fact *skiplist)
{
u8 height;
const t_skiplist_height *height_table;
- sw max;
- u32 k;
sw i;
+ u32 j;
+ sw max;
+ t_skiplist_height k;
assert(skiplist);
height_table = SKIPLIST_HEIGHT_TABLE__fact(skiplist);
max = height_table[skiplist->max_height - 1];
- u32_random_uniform(&k, max);
+ u32_random_uniform(&j, max);
+ k = j;
for (i = 0; i < skiplist->max_height && k > height_table[i]; i++)
;
height = skiplist->max_height - i;