Commit 286a29516851a673baf2ca23e1a8d19199646b33

Thomas de Grivel 2024-03-02T16:48:31

alloc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
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;