Commit 7fecb2768b6cbbcca3e0f0fb2b93260c649fe8c0

Thomas de Grivel 2024-03-02T15:19:31

alloc

diff --git a/libc3/sym.c b/libc3/sym.c
index 17c41bf..cefc56c 100644
--- a/libc3/sym.c
+++ b/libc3/sym.c
@@ -10,9 +10,16 @@
  * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
  * THIS SOFTWARE.
  */
-#include <stdlib.h>
-#include <string.h>
-#include "c3.h"
+#include "alloc.h"
+#include "assert.h"
+#include "buf.h"
+#include "buf_inspect.h"
+#include "character.h"
+#include "compare.h"
+#include "str.h"
+#include "struct_type.h"
+#include "sym.h"
+#include "tag_type.h"
 
 const s_sym g_sym__brackets       = {{{NULL},  2, {"[]"}}};
 const s_sym g_sym__paren          = {{{NULL},  2, {"()"}}};
@@ -428,12 +435,9 @@ s_sym_list * sym_list_new (const s_sym *sym, s_sym *free_sym,
                            s_sym_list *next)
 {
   s_sym_list *sym_list;
-  sym_list = malloc(sizeof(s_sym_list));
-  if (! sym_list) {
-    err_puts("sym_list_new: failed to allocate memory");
-    assert(! "sym_list_new: failed to allocate memory");
+  sym_list = alloc(sizeof(s_sym_list));
+  if (! sym_list)
     return NULL;
-  }
   sym_list->sym = sym;
   sym_list->free_sym = free_sym;
   sym_list->next = next;
@@ -565,12 +569,9 @@ const s_sym * sym_new (const s_str *src)
 {
   s_sym *sym;
   s_sym_list *tmp;
-  sym = malloc(sizeof(s_sym));
-  if (! sym) {
-    err_puts("sym_new: failed to allocate memory");
-    assert(! "sym_new: failed to allocate memory");
+  sym = alloc(sizeof(s_sym));
+  if (! sym)
     return NULL;
-  }
   if (! str_init_copy(&sym->str, src)) {
     free(sym);
     return NULL;
diff --git a/libc3/tag.c b/libc3/tag.c
index 2ed68ab..50e85cb 100644
--- a/libc3/tag.c
+++ b/libc3/tag.c
@@ -10,10 +10,10 @@
  * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
  * THIS SOFTWARE.
  */
-#include "assert.h"
-#include <math.h>
 #include <string.h>
+#include "alloc.h"
 #include "array.h"
+#include "assert.h"
 #include "block.h"
 #include "buf.h"
 #include "buf_inspect.h"
@@ -558,24 +558,18 @@ bool * tag_lte (const s_tag *a, const s_tag *b, bool *dest)
 s_tag * tag_new (void)
 {
   s_tag *dest;
-  dest = calloc(1, sizeof(s_tag));
-  if (! dest) {
-    err_puts("tag_new: failed to allocate memory");
-    assert(! "tag_new: failed to allocate memory");
+  dest = alloc(sizeof(s_tag));
+  if (! dest)
     return NULL;
-  }
   return dest;
 }
 
 s_tag * tag_new_1 (const char *p)
 {
   s_tag *dest;
-  dest = calloc(1, sizeof(s_tag));
-  if (! dest) {
-    err_puts("tag_new_1: failed to allocate memory");
-    assert(! "tag_new_1: failed to allocate memory");
+  dest = alloc(sizeof(s_tag));
+  if (! dest)
     return NULL;
-  }
   if (! tag_init_1(dest, p)) {
     free(dest);
     return NULL;
@@ -586,12 +580,9 @@ s_tag * tag_new_1 (const char *p)
 s_tag * tag_new_copy (const s_tag *src)
 {
   s_tag *dest;
-  dest = calloc(1, sizeof(s_tag));
-  if (! dest) {
-    err_puts("tag_new_copy: failed to allocate memory");
-    assert(! "tag_new_copy: failed to allocate memory");
+  dest = alloc(sizeof(s_tag));
+  if (! dest)
     return NULL;
-  }
   if (! tag_init_copy(dest, src)) {
     free(dest);
     return NULL;
diff --git a/libc3/tuple.c b/libc3/tuple.c
index e18d611..8ad0cc6 100644
--- a/libc3/tuple.c
+++ b/libc3/tuple.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_inspect.h"
 #include "buf_parse.h"
@@ -43,12 +43,9 @@ s_tuple * tuple_init (s_tuple *tuple, uw count)
   assert(tuple);
   assert(2 <= count);
   tmp.count = count;
-  tmp.tag = calloc(count, sizeof(s_tag));
-  if (! tmp.tag) {
-    err_puts("tuple_init: failed to allocate memory");
-    assert(! "tuple_init: failed to allocate memory");
+  tmp.tag = alloc(count * sizeof(s_tag));
+  if (! tmp.tag)
     return NULL;
-  }
   i = count;
   while (i--)
     tag_init_void(tmp.tag + i);
@@ -138,12 +135,9 @@ s_str * tuple_inspect (const s_tuple *x, s_str *dest)
 s_tuple * tuple_new (uw count)
 {
   s_tuple *tuple;
-  tuple = malloc(sizeof(s_tuple));
-  if (! tuple) {
-    err_puts("tuple_new: failed to allocate memory");
-    assert(! "tuple_new: failed to allocate memory");
+  tuple = alloc(sizeof(s_tuple));
+  if (! tuple)
     return NULL;
-  }
   if (! tuple_init(tuple, count)) {
     free(tuple);
     return NULL;
@@ -154,12 +148,9 @@ s_tuple * tuple_new (uw count)
 s_tuple * tuple_new_1 (const char *p)
 {
   s_tuple *tuple;
-  tuple = malloc(sizeof(s_tuple));
-  if (! tuple) {
-    err_puts("tuple_new_1: failed to allocate memory");
-    assert(! "tuple_new_1: failed to allocate memory");
+  tuple = alloc(sizeof(s_tuple));
+  if (! tuple)
     return NULL;
-  }
   if (! tuple_init_1(tuple, p)) {
     free(tuple);
     return NULL;