Commit d33e7f3f937823c717496e7457f5cb65392e1e48

Thomas de Grivel 2024-03-02T17:11:22

alloc

diff --git a/libc3/set.c.in b/libc3/set.c.in
index 335767f..571c6cd 100644
--- a/libc3/set.c.in
+++ b/libc3/set.c.in
@@ -11,8 +11,8 @@
  * THIS SOFTWARE.
  */
 /* Gen from set.c.in NAME=_NAME$ TYPE=_TYPE$ */
-#include <assert.h>
-#include <stdlib.h>
+#include "alloc.h"
+#include "assert.h"
 #include "compare.h"
 #include "_NAME$.h"
 #include "set___NAME$.h"
@@ -120,12 +120,15 @@ set_get_hash_next___NAME$ (const s_set_item___NAME$ *item)
 s_set___NAME$ *
 set_init___NAME$ (s_set___NAME$ *set, uw max)
 {
+  s_set___NAME$ tmp = {0};
   assert(set);
   assert(max > 0);
-  set->max = max;
-  set->items = calloc(max, sizeof(s_set_item___NAME$ *));
-  set->count = 0;
-  set->collisions = 0;
+  tmp.max = max;
+  tmp.items = alloc(max * sizeof(s_set_item___NAME$ *));
+  if (! tmp.items)
+    return NULL;
+  tmp.count = 0;
+  tmp.collisions = 0;
   return set;
 }
 
diff --git a/libc3/set__fact.c b/libc3/set__fact.c
index 20e6fc2..9b30897 100644
--- a/libc3/set__fact.c
+++ b/libc3/set__fact.c
@@ -11,8 +11,8 @@
  * THIS SOFTWARE.
  */
 /* Gen from set.c.in NAME=fact TYPE=s_fact */
-#include <assert.h>
-#include <stdlib.h>
+#include "alloc.h"
+#include "assert.h"
 #include "compare.h"
 #include "fact.h"
 #include "set__fact.h"
@@ -120,12 +120,15 @@ set_get_hash_next__fact (const s_set_item__fact *item)
 s_set__fact *
 set_init__fact (s_set__fact *set, uw max)
 {
+  s_set__fact tmp = {0};
   assert(set);
   assert(max > 0);
-  set->max = max;
-  set->items = calloc(max, sizeof(s_set_item__fact *));
-  set->count = 0;
-  set->collisions = 0;
+  tmp.max = max;
+  tmp.items = alloc(max * sizeof(s_set_item__fact *));
+  if (! tmp.items)
+    return NULL;
+  tmp.count = 0;
+  tmp.collisions = 0;
   return set;
 }
 
diff --git a/libc3/set__tag.c b/libc3/set__tag.c
index cc688df..18a7e35 100644
--- a/libc3/set__tag.c
+++ b/libc3/set__tag.c
@@ -11,8 +11,8 @@
  * THIS SOFTWARE.
  */
 /* Gen from set.c.in NAME=tag TYPE=s_tag */
-#include <assert.h>
-#include <stdlib.h>
+#include "alloc.h"
+#include "assert.h"
 #include "compare.h"
 #include "tag.h"
 #include "set__tag.h"
@@ -120,12 +120,15 @@ set_get_hash_next__tag (const s_set_item__tag *item)
 s_set__tag *
 set_init__tag (s_set__tag *set, uw max)
 {
+  s_set__tag tmp = {0};
   assert(set);
   assert(max > 0);
-  set->max = max;
-  set->items = calloc(max, sizeof(s_set_item__tag *));
-  set->count = 0;
-  set->collisions = 0;
+  tmp.max = max;
+  tmp.items = alloc(max * sizeof(s_set_item__tag *));
+  if (! tmp.items)
+    return NULL;
+  tmp.count = 0;
+  tmp.collisions = 0;
   return set;
 }
 
diff --git a/libc3/set_item.c.in b/libc3/set_item.c.in
index 6b14a57..3431d36 100644
--- a/libc3/set_item.c.in
+++ b/libc3/set_item.c.in
@@ -11,8 +11,8 @@
  * THIS SOFTWARE.
  */
 /* Gen from set_item.c.in NAME=_NAME$ TYPE=_TYPE$ */
+#include "alloc.h"
 #include "assert.h"
-#include <stdlib.h>
 #include "set_item___NAME$.h"
 #include "_NAME$.h"
 
@@ -20,12 +20,9 @@ s_set_item___NAME$ *
 set_item_new___NAME$ (const _TYPE$ *data, uw hash, s_set_item___NAME$ *next)
 {
   s_set_item___NAME$ *item;
-  item = malloc(sizeof(s_set_item___NAME$));
-  if (! item) {
-    err_puts("set_item_new___NAME$: out of memory");
-    assert(! "set_item_new___NAME$: out of memory");
+  item = alloc(sizeof(s_set_item___NAME$));
+  if (! item)
     return NULL;
-  }
   if (! _NAME$_init_copy(&item->data, data)) {
     free(item);
     return NULL;
diff --git a/libc3/set_item__fact.c b/libc3/set_item__fact.c
index 175a407..7857e91 100644
--- a/libc3/set_item__fact.c
+++ b/libc3/set_item__fact.c
@@ -11,8 +11,8 @@
  * THIS SOFTWARE.
  */
 /* Gen from set_item.c.in NAME=fact TYPE=s_fact */
+#include "alloc.h"
 #include "assert.h"
-#include <stdlib.h>
 #include "set_item__fact.h"
 #include "fact.h"
 
@@ -20,12 +20,9 @@ s_set_item__fact *
 set_item_new__fact (const s_fact *data, uw hash, s_set_item__fact *next)
 {
   s_set_item__fact *item;
-  item = malloc(sizeof(s_set_item__fact));
-  if (! item) {
-    err_puts("set_item_new__fact: out of memory");
-    assert(! "set_item_new__fact: out of memory");
+  item = alloc(sizeof(s_set_item__fact));
+  if (! item)
     return NULL;
-  }
   if (! fact_init_copy(&item->data, data)) {
     free(item);
     return NULL;
diff --git a/libc3/set_item__tag.c b/libc3/set_item__tag.c
index 6549740..b2cfd05 100644
--- a/libc3/set_item__tag.c
+++ b/libc3/set_item__tag.c
@@ -11,8 +11,8 @@
  * THIS SOFTWARE.
  */
 /* Gen from set_item.c.in NAME=tag TYPE=s_tag */
+#include "alloc.h"
 #include "assert.h"
-#include <stdlib.h>
 #include "set_item__tag.h"
 #include "tag.h"
 
@@ -20,12 +20,9 @@ s_set_item__tag *
 set_item_new__tag (const s_tag *data, uw hash, s_set_item__tag *next)
 {
   s_set_item__tag *item;
-  item = malloc(sizeof(s_set_item__tag));
-  if (! item) {
-    err_puts("set_item_new__tag: out of memory");
-    assert(! "set_item_new__tag: out of memory");
+  item = alloc(sizeof(s_set_item__tag));
+  if (! item)
     return NULL;
-  }
   if (! tag_init_copy(&item->data, data)) {
     free(item);
     return NULL;