Commit 60a57804912063a6e5d2dc3f61da73a1b40c0dfc

Thomas de Grivel 2024-12-26T08:35:10

wip alloc map/unmap alloc_free

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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
diff --git a/libkc3/alloc.c b/libkc3/alloc.c
index 257af83..e35f0b8 100644
--- a/libkc3/alloc.c
+++ b/libkc3/alloc.c
@@ -17,12 +17,13 @@
 #include <sys/mman.h>
 #include "alloc.h"
 #include "assert.h"
+#include "compare.h"
 #include "skiplist__alloc.h"
 #include "skiplist_node__alloc.h"
 
-static sw                g_alloc_init = 0;
-static s_skiplist__alloc g_alloc_skiplist_mapped_size = {0};
-static s_skiplist__alloc g_alloc_skiplist_size_mapped = {0};
+static sw                 g_alloc_init = 0;
+static s_skiplist__alloc *g_alloc_skiplist_mapped_size = NULL;
+static s_skiplist__alloc *g_alloc_skiplist_size_mapped = NULL;
 
 void * alloc (uw size)
 {
@@ -45,8 +46,8 @@ void * alloc (uw size)
     assert(! "alloc: alloc_map(size)");
     return NULL;
   }
-  skiplist_insert__alloc(&g_alloc_skiplist_mapped_size, a);
-  skiplist_insert__alloc(&g_alloc_skiplist_size_mapped, a);
+  skiplist_insert__alloc(g_alloc_skiplist_mapped_size, a);
+  skiplist_insert__alloc(g_alloc_skiplist_size_mapped, a);
   return a->mapped;
 }
 
@@ -54,8 +55,8 @@ void alloc_clean (void)
 {
   g_alloc_init--;
   if (! g_alloc_init) {
-    skiplist_clean__alloc(&g_alloc_skiplist_mapped_size);
-    skiplist_clean__alloc(&g_alloc_skiplist_size_mapped);
+    skiplist_delete__alloc(g_alloc_skiplist_mapped_size);
+    skiplist_delete__alloc(g_alloc_skiplist_size_mapped);
   }
 }
 
@@ -65,16 +66,18 @@ s8 alloc_init (void)
     g_alloc_init++;
     return 0;
   }
-  if (! skiplist_init__alloc(&g_alloc_skiplist_mapped_size, 32, 2.5)) {
-    err_puts("alloc_init: skiplist_init__alloc 1");
-    assert(! "alloc_init: skiplist_init__alloc 1");
+  if (! (g_alloc_skiplist_mapped_size = skiplist_new__alloc(32, 2.5))) {
+    err_puts("alloc_init: skiplist_new__alloc 1");
+    assert(! "alloc_init: skiplist_new__alloc 1");
     return 1;
   }
-  if (! skiplist_init__alloc(&g_alloc_skiplist_size_mapped, 32, 2.5)) {
-    err_puts("alloc_init: skiplist_init__alloc 2");
-    assert(! "alloc_init: skiplist_init__alloc 2");
+  g_alloc_skiplist_mapped_size->compare = compare_alloc_mapped_size;
+  if (! (g_alloc_skiplist_size_mapped = skiplist_new__alloc(32, 2.5))) {
+    err_puts("alloc_init: skiplist_new__alloc 2");
+    assert(! "alloc_init: skiplist_new__alloc 2");
     return 1;
   }
+  g_alloc_skiplist_size_mapped->compare = compare_alloc_size_mapped;
   g_alloc_init = 1;
   return 0;
 }
@@ -86,7 +89,7 @@ void alloc_free (void *allocated)
   s_skiplist_node__alloc *pred;
   s_alloc                 pred_alloc = {0};
   pred_alloc.mapped = allocated;
-  if (! (pred = skiplist_pred__alloc(&g_alloc_skiplist_mapped_size,
+  if (! (pred = skiplist_pred__alloc(g_alloc_skiplist_mapped_size,
                                      &pred_alloc)))
     goto ko;
   if (! (node = SKIPLIST_NODE_NEXT__alloc(pred, 0)))
@@ -95,8 +98,8 @@ void alloc_free (void *allocated)
   if (al->mapped != allocated)
     goto ko;
   alloc_unmap(allocated, al->size);
-  skiplist_remove__alloc(&g_alloc_skiplist_mapped_size, al);
-  skiplist_remove__alloc(&g_alloc_skiplist_size_mapped, al);
+  skiplist_remove__alloc(g_alloc_skiplist_mapped_size, al);
+  skiplist_remove__alloc(g_alloc_skiplist_size_mapped, al);
   alloc_unmap(al, sizeof(s_alloc));
  ko:
   err_puts("free: invalid argument");
diff --git a/libkc3/array.c b/libkc3/array.c
index c879640..aba0059 100644
--- a/libkc3/array.c
+++ b/libkc3/array.c
@@ -56,7 +56,7 @@ void array_clean (s_array *a)
   bool must_clean;
   uw size;
   assert(a);
-  free(a->dimensions);
+  alloc_free(a->dimensions);
   if (a->data &&
       sym_must_clean(a->element_type, &must_clean) &&
       must_clean &&
@@ -71,14 +71,14 @@ void array_clean (s_array *a)
     }
   }
   if (a->free_data)
-    free(a->free_data);
+    alloc_free(a->free_data);
   if (a->tags) {
     i = 0;
     while (i < a->count) {
       tag_clean(a->tags + i);
       i++;
     }
-    free(a->tags);
+    alloc_free(a->tags);
   }
 }
 
@@ -159,7 +159,7 @@ s_array * array_free (s_array *a)
 {
   a->data = NULL;
   if (a->free_data) {
-    free(a->free_data);
+    alloc_free(a->free_data);
     a->free_data = NULL;
   }
   return a;
@@ -196,13 +196,13 @@ s_array * array_init (s_array *a, const s_sym *array_type, uw dimension,
     }
     i--;
     if (! sym_type_size(&tmp.element_type, &item_size)) {
-      free(tmp.dimensions);
+      alloc_free(tmp.dimensions);
       return NULL;
     }
     if (! item_size) {
       err_puts("array_init: zero item size");
       assert(! "array_init: zero item size");
-      free(tmp.dimensions);
+      alloc_free(tmp.dimensions);
       return NULL;
     }
     tmp.dimensions[i].item_size = item_size;
@@ -291,7 +291,7 @@ s_array * array_init_copy (s_array *a, const s_array *src)
     if (src->data) {
       tmp.data = tmp.free_data = alloc(tmp.size);
       if (! tmp.data) {
-        free(tmp.dimensions);
+        alloc_free(tmp.dimensions);
         return NULL;
       }
       data_tmp = tmp.data;
@@ -309,7 +309,7 @@ s_array * array_init_copy (s_array *a, const s_array *src)
     else if (src->tags) {
       tmp.tags = alloc(src->count * sizeof(s_tag));
       if (! tmp.tags) {
-        free(tmp.dimensions);
+        alloc_free(tmp.dimensions);
         return NULL;
       }
       i = 0;
@@ -331,8 +331,8 @@ s_array * array_init_copy (s_array *a, const s_array *src)
       data_clean(src->element_type, data_tmp);
     }
   }
-  free(tmp.data);
-  free(tmp.dimensions);
+  alloc_free(tmp.data);
+  alloc_free(tmp.dimensions);
   return NULL;
  ko_tags:
   if (i)
diff --git a/libkc3/block.c b/libkc3/block.c
index 544dd85..70f73cf 100644
--- a/libkc3/block.c
+++ b/libkc3/block.c
@@ -28,13 +28,13 @@ void block_clean (s_block *block)
   i = block->count;
   while (i--)
     tag_clean(block->tag + i);
-  free(block->tag);
+  alloc_unmap(block->tag, block->count * sizeof(s_tag));
 }
 
 void block_delete (s_block *block)
 {
   block_clean(block);
-  free(block);
+  alloc_unmap(block, sizeof(s_block));
 }
 
 s_block * block_init (s_block *block, uw count)
@@ -43,7 +43,7 @@ s_block * block_init (s_block *block, uw count)
   assert(block);
   tmp.count = count;
   if (count) {
-    tmp.tag = alloc(count * sizeof(s_tag));
+    tmp.tag = alloc_map(count * sizeof(s_tag));
     if (! tmp.tag)
       return NULL;
   }
@@ -131,11 +131,11 @@ s_block * block_init_from_list (s_block *block,
 s_block * block_new (uw count)
 {
   s_block *block;
-  block = alloc(sizeof(s_block));
+  block = alloc_map(sizeof(s_block));
   if (! block)
     return NULL;
   if (! block_init(block, count)) {
-    free(block);
+    alloc_unmap(block, sizeof(s_block));
     return NULL;
   }
   return block;
@@ -144,11 +144,11 @@ s_block * block_new (uw count)
 s_block * block_new_1 (const char *p)
 {
   s_block *block;
-  block = alloc(sizeof(s_block));
+  block = alloc_map(sizeof(s_block));
   if (! block)
     return NULL;
   if (! block_init_1(block, p)) {
-    free(block);
+    alloc_unmap(block, sizeof(s_block));
     return NULL;
   }
   return block;
diff --git a/libkc3/buf.h b/libkc3/buf.h
index c3e60a9..6bed7fe 100644
--- a/libkc3/buf.h
+++ b/libkc3/buf.h
@@ -20,7 +20,6 @@
 #define LIBKC3_BUF_H
 
 #include <stdarg.h>
-#include <stdlib.h>
 #include "types.h"
 
 #define BUF_SIZE 65536
diff --git a/libkc3/buf_fd.c b/libkc3/buf_fd.c
index b9d02af..d468598 100644
--- a/libkc3/buf_fd.c
+++ b/libkc3/buf_fd.c
@@ -36,7 +36,7 @@ void buf_fd_close (s_buf *buf)
   buf_flush(buf);
   buf->flush = NULL;
   buf->refill = NULL;
-  free(buf->user_ptr);
+  alloc_unmap(buf->user_ptr, sizeof(s_buf_fd));
   buf->user_ptr = NULL;
 }
 
@@ -45,7 +45,7 @@ s_buf * buf_fd_open_r (s_buf *buf, s32 fd)
   s_buf_fd *buf_fd;
   assert(buf);
   assert(fd);
-  buf_fd = alloc(sizeof(s_buf_fd));
+  buf_fd = alloc_map(sizeof(s_buf_fd));
   if (! buf_fd)
     return NULL;
   buf_fd->fd = fd;
diff --git a/libkc3/buf_file.c b/libkc3/buf_file.c
index 378d420..3c8ae7b 100644
--- a/libkc3/buf_file.c
+++ b/libkc3/buf_file.c
@@ -12,15 +12,10 @@
  */
 #include "alloc.h"
 #include "assert.h"
-#include <stdio.h>
 #include "buf.h"
 #include "buf_file.h"
 #include "buf_save.h"
 
-typedef struct buf_file {
-  FILE *fp;
-} s_buf_file;
-
 sw buf_file_open_r_refill (s_buf *buf);
 sw buf_file_open_w_flush (s_buf *buf);
 sw buf_file_open_w_seek (s_buf *buf, sw offset, u8 whence);
@@ -31,7 +26,7 @@ void buf_file_close (s_buf *buf)
   buf_flush(buf);
   buf->flush = NULL;
   buf->refill = NULL;
-  free(buf->user_ptr);
+  alloc_unmap(buf->user_ptr, sizeof(s_buf_file));
   buf->user_ptr = NULL;
 }
 
@@ -62,7 +57,7 @@ s_buf * buf_file_open_r (s_buf *buf, FILE *fp)
   s_buf_file *buf_file;
   assert(buf);
   assert(fp);
-  buf_file = alloc(sizeof(s_buf_file));
+  buf_file = alloc_map(sizeof(s_buf_file));
   if (! buf_file)
     return NULL;
   buf_file->fp = fp;
@@ -105,7 +100,7 @@ s_buf * buf_file_open_w (s_buf *buf, FILE *fp)
   s_buf_file *buf_file;
   assert(buf);
   assert(fp);
-  buf_file = alloc(sizeof(s_buf_file));
+  buf_file = alloc_map(sizeof(s_buf_file));
   if (! buf_file)
     return NULL;
   buf_file->fp = fp;
diff --git a/libkc3/buf_inspect.c b/libkc3/buf_inspect.c
index 0d44337..67d2599 100644
--- a/libkc3/buf_inspect.c
+++ b/libkc3/buf_inspect.c
@@ -87,6 +87,7 @@ sw buf_inspect_array (s_buf *buf, const s_array *array)
 sw buf_inspect_array_data (s_buf *buf, const s_array *array)
 {
   uw *address;
+  uw  address_size;
   u8 *data = NULL;
   s_tag *tag = NULL;
   sw r;
@@ -98,12 +99,13 @@ sw buf_inspect_array_data (s_buf *buf, const s_array *array)
     data = array->data;
   else 
     tag = array->tags;
-  address = alloc(array->dimension * sizeof(uw));
+  address_size = array->dimension * sizeof(uw);
+  address = alloc_map(address_size);
   if (! address)
     return -1;
   r = buf_inspect_array_data_rec(buf, array, (const u8 **) &data,
                                  (const s_tag **) &tag, address, 0);
-  free(address);
+  alloc_unmap(address, address_size);
   return r;
 }
 
@@ -167,6 +169,7 @@ sw buf_inspect_array_data_rec (s_buf *buf, const s_array *array,
 sw buf_inspect_array_data_size (s_pretty *pretty, const s_array *array)
 {
   uw *address;
+  uw  address_size;
   const s_sym *buf_inspect_type_save;
   u8 *data = NULL;
   s_tag *tag = NULL;
@@ -176,7 +179,8 @@ sw buf_inspect_array_data_size (s_pretty *pretty, const s_array *array)
     data = array->data;
   else 
     tag = array->tags;
-  address = alloc(array->dimension * sizeof(uw));
+  address_size = array->dimension * sizeof(uw);
+  address = alloc_map(address_size);
   if (! address)
     return -1;
   buf_inspect_type_save = g_buf_inspect_type;
@@ -186,7 +190,7 @@ sw buf_inspect_array_data_size (s_pretty *pretty, const s_array *array)
                                       (const s_tag **) &tag,
                                       address, 0);
   g_buf_inspect_type = buf_inspect_type_save;
-  free(address);
+  alloc_unmap(address, address_size);
   return r;
 }
 
diff --git a/libkc3/buf_parse.c b/libkc3/buf_parse.c
index 8f29e2a..de6cf58 100644
--- a/libkc3/buf_parse.c
+++ b/libkc3/buf_parse.c
@@ -94,6 +94,7 @@ sw buf_parse_array (s_buf *buf, s_array *dest)
 sw buf_parse_array_data (s_buf *buf, s_array *dest)
 {
   uw *address = NULL;
+  uw  address_size;
   uw i;
   sw r = 0;
   sw result = 0;
@@ -114,7 +115,8 @@ sw buf_parse_array_data (s_buf *buf, s_array *dest)
     return result;
   }
   tmp = *dest;
-  address = alloc(tmp.dimension * sizeof(sw));
+  address_size = tmp.dimension * sizeof(sw);
+  address = alloc(address_size);
   if (! address)
     return -1;
   tmp.count = 1;
@@ -126,7 +128,7 @@ sw buf_parse_array_data (s_buf *buf, s_array *dest)
   tmp.size = tmp.dimensions[0].count * tmp.dimensions[0].item_size;
   tmp.tags = alloc(tmp.count * sizeof(s_tag));
   if (! tmp.tags) {
-    free(address);
+    alloc_unmap(address, address_size);
     return -1;
   }
   tag = tmp.tags;
@@ -140,9 +142,9 @@ sw buf_parse_array_data (s_buf *buf, s_array *dest)
   *dest = tmp;
   goto clean;
  restore:
-  free(tmp.tags);
+  alloc_free(tmp.tags);
  clean:
-  free(address);
+  alloc_unmap(address, address_size);
   return r;
 }
 
@@ -287,6 +289,7 @@ sw buf_parse_array_dimension_count (s_buf *buf, s_array *dest)
 sw buf_parse_array_dimensions (s_buf *buf, s_array *dest)
 {
   uw *address;
+  uw  address_size;
   sw r;
   uw size;
   s_array tmp;
@@ -300,7 +303,8 @@ sw buf_parse_array_dimensions (s_buf *buf, s_array *dest)
     assert(! "buf_parse_array_dimensions: zero item size");
     return -1;
   }
-  address = alloc(tmp.dimension * sizeof(sw));
+  address_size = tmp.dimension * sizeof(sw);
+  address = alloc(address_size);
   if (! address)
     return -1;
   tmp.dimensions[tmp.dimension - 1].item_size = size;
@@ -314,7 +318,7 @@ sw buf_parse_array_dimensions (s_buf *buf, s_array *dest)
   }
   *dest = tmp;
  clean:
-  free(address);
+  alloc_unmap(address, address_size);
   return r;
 }
 
@@ -3118,7 +3122,7 @@ sw buf_parse_pcomplex (s_buf *buf, s_complex **c)
   s_complex *tmp;
   tmp = complex_new();
   if ((r = buf_parse_complex(buf, tmp)) <= 0) {
-    free(tmp);
+    alloc_unmap(tmp, sizeof(s_complex));
     return r;
   }
   *c = tmp;
@@ -3188,7 +3192,7 @@ sw buf_parse_quote (s_buf *buf, s_quote *dest)
     err_inspect_buf(buf);
     err_write_1("\n");
     assert(! "buf_parse_quote: buf_parse_tag");
-    free(quote.tag);
+    tag_delete(quote.tag);
     goto restore;
   }
   result += r;
@@ -4859,7 +4863,7 @@ sw buf_parse_unquote (s_buf *buf, s_unquote *dest)
     goto restore;
   }
   if ((r = buf_parse_tag(buf, unquote.tag)) <= 0) {
-    free(unquote.tag);
+    tag_delete(unquote.tag);
     goto restore;
   }
   result += r;
diff --git a/libkc3/compare.h b/libkc3/compare.h
index 900844c..19a9b93 100644
--- a/libkc3/compare.h
+++ b/libkc3/compare.h
@@ -24,6 +24,8 @@
   s8 compare_##type (type a, type b)
 
 s8 compare_alloc (const s_alloc *a, const s_alloc *b);
+s8 compare_alloc_mapped_size (const s_alloc *a, const s_alloc *b);
+s8 compare_alloc_size_mapped (const s_alloc *a, const s_alloc *b);
 s8 compare_array (const s_array *a, const s_array *b);
 s8 compare_block (const s_block *a, const s_block *b);
 s8 compare_bool (bool a, bool b);
diff --git a/libkc3/cow.c b/libkc3/cow.c
index 8b1c34e..a089b8f 100644
--- a/libkc3/cow.c
+++ b/libkc3/cow.c
@@ -32,7 +32,7 @@ void cow_delete (s_cow *cow)
 {
   assert(cow);
   cow_clean(cow);
-  free(cow);
+  alloc_unmap(cow, sizeof(s_cow));
 }
 
 s_cow * cow_freeze (s_cow *cow)
@@ -164,11 +164,11 @@ s_cow * cow_init_tag_copy (s_cow *cow, const s_sym *type,
 s_cow * cow_new (const s_sym *type)
 {
   s_cow *cow;
-  cow = alloc(sizeof(s_cow));
+  cow = alloc_map(sizeof(s_cow));
   if (! cow)
     return NULL;
   if (! cow_init(cow, type)) {
-    free(cow);
+    alloc_unmap(cow, sizeof(s_cow));
     return NULL;
   }
   return cow;
@@ -177,11 +177,11 @@ s_cow * cow_new (const s_sym *type)
 s_cow * cow_new_1 (const char *utf8)
 {
   s_cow *cow;
-  cow = alloc(sizeof(s_cow));
+  cow = alloc_map(sizeof(s_cow));
   if (! cow)
     return NULL;
   if (! cow_init_1(cow, utf8)) {
-    free(cow);
+    alloc_unmap(cow, sizeof(s_cow));
     return NULL;
   }
   return cow;
@@ -190,11 +190,11 @@ s_cow * cow_new_1 (const char *utf8)
 s_cow * cow_new_cast (const s_sym * const *type, s_tag *tag)
 {
   s_cow *cow;
-  cow = alloc(sizeof(s_cow));
+  cow = alloc_map(sizeof(s_cow));
   if (! cow)
     return NULL;
   if (! cow_init_cast(cow, type, tag)) {
-    free(cow);
+    alloc_unmap(cow, sizeof(s_cow));
     return NULL;
   }
   return cow;
@@ -203,11 +203,11 @@ s_cow * cow_new_cast (const s_sym * const *type, s_tag *tag)
 s_cow * cow_new_copy (s_cow *src)
 {
   s_cow *cow;
-  cow = alloc(sizeof(s_cow));
+  cow = alloc_map(sizeof(s_cow));
   if (! cow)
     return NULL;
   if (! cow_init_copy(cow, src)) {
-    free(cow);
+    alloc_unmap(cow, sizeof(s_cow));
     return NULL;
   }
   return cow;
@@ -216,11 +216,11 @@ s_cow * cow_new_copy (s_cow *src)
 s_cow * cow_new_tag_copy (const s_sym *type, s_tag *src)
 {
   s_cow *cow;
-  cow = alloc(sizeof(s_cow));
+  cow = alloc_map(sizeof(s_cow));
   if (! cow)
     return NULL;
   if (! cow_init_tag_copy(cow, type, src)) {
-    free(cow);
+    alloc_unmap(cow, sizeof(s_cow));
     return NULL;
   }
   return cow;
diff --git a/libkc3/env.c b/libkc3/env.c
index ea7124c..a9daac3 100644
--- a/libkc3/env.c
+++ b/libkc3/env.c
@@ -12,12 +12,16 @@
  */
 #include <string.h>
 
-#if (! (defined(__APPLE__) || defined(__OpenBSD__)))
+#if (defined(__APPLE__) ||      \
+     defined(__FreeBSD__) ||    \
+     defined(__NetBSD__) ||     \
+     defined(__OpenBSD__))
+#include <sys/sysctl.h>
+#else
 #include <sys/sysinfo.h>
 #endif
 
 #include <sys/types.h>
-#include <sys/sysctl.h>
 #include <unistd.h>
 #include "alloc.h"
 #include "array.h"
diff --git a/libkc3/facts.c b/libkc3/facts.c
index 8baa8c6..251416e 100644
--- a/libkc3/facts.c
+++ b/libkc3/facts.c
@@ -11,6 +11,7 @@
  * THIS SOFTWARE.
  */
 #include <errno.h>
+#include <stdlib.h>
 #include <string.h>
 #include "alloc.h"
 #include "assert.h"
@@ -193,7 +194,7 @@ void facts_delete (s_facts *facts)
 {
   assert(facts);
   facts_clean(facts);
-  free(facts);
+  alloc_unmap(facts, sizeof(s_facts));
 }
 
 sw facts_dump (s_facts *facts, s_buf *buf)
@@ -505,11 +506,11 @@ sw facts_log_remove (s_log *log, const s_fact *fact)
 s_facts * facts_new (void)
 {
   s_facts *facts;
-  facts = alloc(sizeof(s_facts));
+  facts = alloc_map(sizeof(s_facts));
   if (! facts)
     return NULL;
   if (! facts_init(facts)) {
-    free(facts);
+    alloc_unmap(facts, sizeof(s_facts));
     return NULL;
   }
   return facts;
@@ -642,6 +643,7 @@ s_facts * facts_remove_all (s_facts *facts)
   uw count;
   s_set_cursor__fact cursor;
   s_fact **f;
+  uw       f_size;
   uw i;
   uw j;
   s_set_item__fact *item;
@@ -649,7 +651,8 @@ s_facts * facts_remove_all (s_facts *facts)
   count = facts->facts.count;
   if (! count)
     return facts;
-  f = alloc(count * sizeof(s_fact *));
+  f_size = count * sizeof(s_fact *);
+  f = alloc_map(f_size);
   if (! f)
     return NULL;
   i = 0;
@@ -664,12 +667,12 @@ s_facts * facts_remove_all (s_facts *facts)
   while (j < i) {
     if (! facts_remove_fact(facts, f[j], &b) ||
         ! b) {
-      free(f);
+      alloc_unmap(f, f_size);
       return NULL;
     }
     j++;
   }
-  free(f);
+  alloc_unmap(f, f_size);
   return facts;
 }
 
@@ -813,7 +816,7 @@ sw facts_save_file (s_facts *facts, const char *path)
     goto ko;
   result += r;
   buf_flush(&buf);
-  free(buf.user_ptr);
+  alloc_unmap(buf.user_ptr, sizeof(s_buf_file));
   buf.user_ptr = NULL;
   if (! (facts->log = log_new()))
     goto ko;
diff --git a/libkc3/facts_with.c b/libkc3/facts_with.c
index cc7fdf1..25a76dc 100644
--- a/libkc3/facts_with.c
+++ b/libkc3/facts_with.c
@@ -37,8 +37,8 @@ s_facts_with_cursor * facts_with (s_facts *facts,
   tmp.facts = facts;
   tmp.facts_count = facts_count;
   if (facts_count > 0) {
-    tmp.levels = alloc(facts_count *
-                       sizeof(s_facts_with_cursor_level));
+    tmp.levels = alloc_map(facts_count *
+                           sizeof(s_facts_with_cursor_level));
     if (! tmp.levels)
       return NULL;
     tmp.spec = facts_spec_new_expand(spec);
@@ -151,10 +151,10 @@ s_facts_with_cursor * facts_with_list (s_facts *facts,
   }
   if (! facts_with(facts, cursor, facts_spec))
     goto ko;
-  free(facts_spec);
+  alloc_free(facts_spec);
   return cursor;
  ko:
-  free(facts_spec);
+  alloc_free(facts_spec);
   return NULL;
 }
 
diff --git a/libkc3/facts_with_cursor.c b/libkc3/facts_with_cursor.c
index deb270e..7c4799c 100644
--- a/libkc3/facts_with_cursor.c
+++ b/libkc3/facts_with_cursor.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_inspect.h"
@@ -27,13 +28,14 @@ void facts_with_cursor_clean (s_facts_with_cursor *cursor)
   if (cursor->facts_count) {
     while (i < cursor->facts_count) {
       if (cursor->levels[i].spec) {
-        free(cursor->levels[i].spec);
+        alloc_free(cursor->levels[i].spec);
         facts_cursor_clean(&cursor->levels[i].cursor);
       }
       i++;
     }
-    free(cursor->levels);
-    free(cursor->spec);
+    alloc_unmap(cursor->levels, cursor->facts_count *
+                sizeof(s_facts_with_cursor_level));
+    alloc_free(cursor->spec);
 #if HAVE_PTHREAD
     mutex_clean(&cursor->mutex);
 #endif
@@ -79,7 +81,7 @@ s_fact ** facts_with_cursor_next (s_facts_with_cursor *cursor,
 #endif
       return dest;
     }
-    free(level->spec);
+    alloc_free(level->spec);
     level->spec = NULL;
     cursor->level--;
     if (! cursor->level)
@@ -120,7 +122,7 @@ s_fact ** facts_with_cursor_next (s_facts_with_cursor *cursor,
       cursor->level++;
       continue;
     }
-    free(level->spec);
+    alloc_free(level->spec);
     level->spec = NULL;
     if (! cursor->level)
       goto not_found;
@@ -133,8 +135,9 @@ s_fact ** facts_with_cursor_next (s_facts_with_cursor *cursor,
   return dest;
  not_found:
   cursor->facts_count = 0;
-  free(cursor->levels);
-  free(cursor->spec);
+  alloc_unmap(cursor->levels, cursor->facts_count *
+              sizeof(s_facts_with_cursor_level));
+  alloc_free(cursor->spec);
 #if HAVE_PTHREAD
   mutex_unlock(&cursor->mutex);
   mutex_clean(&cursor->mutex);
diff --git a/libkc3/file.c b/libkc3/file.c
index e5db2ea..03d8871 100644
--- a/libkc3/file.c
+++ b/libkc3/file.c
@@ -347,7 +347,7 @@ s_str * file_read_all (const s_str *path, s_str *dest)
     err_write_1("\n");
     return NULL;
   }
-  buf = alloc(sb.st_size);
+  buf = alloc_map(sb.st_size + 1);
   if (! buf) {
     err_puts("file_read_all: failed to allocate buf");
     close(fd);
@@ -391,7 +391,7 @@ s_str * file_read_max (const s_str *path, uw max, s_str *dest)
     err_write_1("\n");
     return NULL;
   }
-  buf = alloc(max);
+  buf = alloc_map(max);
   if (! buf) {
     err_puts("file_read_max: failed to allocate buf");
     close(fd);
@@ -404,7 +404,7 @@ s_str * file_read_max (const s_str *path, uw max, s_str *dest)
     err_write_1(": ");
     err_inspect_str(path);
     err_write_1("\n");
-    free(buf);
+    alloc_unmap(buf, max);
     close(fd);
     return NULL;
   }
@@ -413,11 +413,11 @@ s_str * file_read_max (const s_str *path, uw max, s_str *dest)
   if (! str_init_alloc_copy(&tmp, size, buf)) {
     err_puts("file_read_max: str_init_alloc_copy");
     assert(! "file_read_max: str_init_alloc_copy");
-    free(buf);
+    alloc_unmap(buf, max);
     close(fd);
     return NULL;
   }
-  free(buf);
+  alloc_unmap(buf, max);
   *dest = tmp;
   return dest;
 }
diff --git a/libkc3/fn.c b/libkc3/fn.c
index 75f1d6d..24354df 100644
--- a/libkc3/fn.c
+++ b/libkc3/fn.c
@@ -47,7 +47,7 @@ void fn_clean (s_fn *fn)
 void fn_delete (s_fn *fn)
 {
   fn_clean(fn);
-  free(fn);
+  alloc_unmap(fn, sizeof(s_fn));
 }
 
 s_fn * fn_init (s_fn *fn, const s_sym *module)
@@ -131,7 +131,7 @@ s_fn * fn_init_copy (s_fn *fn, const s_fn *src)
 s_fn * fn_new (const s_sym *module)
 {
   s_fn *fn;
-  fn = alloc(sizeof(s_fn));
+  fn = alloc_map(sizeof(s_fn));
   if (! fn)
     return NULL;
   fn_init(fn, module);
@@ -142,11 +142,11 @@ s_fn * fn_new_copy (const s_fn *src)
 {
   s_fn *fn;
   assert(src);
-  fn = alloc(sizeof(s_fn));
+  fn = alloc_map(sizeof(s_fn));
   if (! fn)
     return NULL;
   if (! fn_init_copy(fn, src)) {
-    free(fn);
+    alloc_unmap(fn, sizeof(s_fn));
     return NULL;
   }
   return fn;
diff --git a/libkc3/integer.c b/libkc3/integer.c
index 6b41654..964f668 100644
--- a/libkc3/integer.c
+++ b/libkc3/integer.c
@@ -490,11 +490,11 @@ s_integer * integer_neg (const s_integer *a, s_integer *dest)
 s_integer * integer_new (void)
 {
   s_integer *a = NULL;
-  a = alloc(sizeof(s_integer));
+  a = alloc_map(sizeof(s_integer));
   if (! a)
     return NULL;
   if (! integer_init(a)) {
-    free(a);
+    alloc_unmap(a, sizeof(s_integer));
     return NULL;
   }
   return a;
@@ -504,11 +504,11 @@ s_integer * integer_new_copy (const s_integer *src)
 {
   s_integer *a;
   assert(src);
-  a = alloc(sizeof(s_integer));
+  a = alloc_map(sizeof(s_integer));
   if (! a)
     return NULL;
   if (! integer_init_copy(a, src)) {
-    free(a);
+    alloc_unmap(a, sizeof(s_integer));
     return NULL;
   }
   return a;
diff --git a/libkc3/kc3.c b/libkc3/kc3.c
index fbc1f67..008ecdb 100644
--- a/libkc3/kc3.c
+++ b/libkc3/kc3.c
@@ -13,6 +13,7 @@
 #include <dlfcn.h>
 #include <errno.h>
 #include <pthread.h>
+#include <stdlib.h>
 #include <string.h>
 
 #ifndef WIN32
diff --git a/libkc3/list.c b/libkc3/list.c
index 55ccc43..bfb52a5 100644
--- a/libkc3/list.c
+++ b/libkc3/list.c
@@ -580,7 +580,7 @@ s_array * list_to_array (s_list *list, const s_sym *array_type,
     tmp.size = len * size;
     tmp.free_data = alloc(len * size);
     if (! tmp.free_data) {
-      free(tmp.dimensions);
+      alloc_free(tmp.dimensions);
       return NULL;
     }
     data = tmp.data = tmp.free_data;
@@ -602,8 +602,8 @@ s_array * list_to_array (s_list *list, const s_sym *array_type,
       data_clean(tmp.element_type, data);
     }
   }
-  free(tmp.data);
-  free(tmp.dimensions);
+  alloc_free(tmp.data);
+  alloc_free(tmp.dimensions);
   return NULL;
 }
 
diff --git a/libkc3/str.c b/libkc3/str.c
index 349376a..3d6d118 100644
--- a/libkc3/str.c
+++ b/libkc3/str.c
@@ -12,6 +12,7 @@
  */
 #include <math.h>
 #include <stdarg.h>
+#include <stdlib.h>
 #include <string.h>
 #include "alloc.h"
 #include "assert.h"
diff --git a/libkc3/types.h b/libkc3/types.h
index 8a7052f..0e99572 100644
--- a/libkc3/types.h
+++ b/libkc3/types.h
@@ -182,6 +182,7 @@ typedef struct binding                 s_binding;
 typedef struct block                   s_block;
 typedef struct buf                     s_buf;
 typedef struct buf_fd                  s_buf_fd;
+typedef struct buf_file                s_buf_file;
 typedef struct buf_rw                  s_buf_rw;
 typedef struct buf_save                s_buf_save;
 typedef struct call                    s_call;
@@ -286,6 +287,10 @@ struct buf_fd {
   s32 fd;
 };
 
+struct buf_file {
+  FILE *fp;
+};
+
 struct buf_save {
   s_buf_save *next;
   uw line;
diff --git a/socket/socket_addr.c b/socket/socket_addr.c
index 3cc6d69..24d20ed 100644
--- a/socket/socket_addr.c
+++ b/socket/socket_addr.c
@@ -43,27 +43,27 @@ s_str * socket_addr_to_str(s_str *str, const struct sockaddr *addr,
 void socket_addr_delete (struct sockaddr *sa)
 {
   assert(sa);
-  free(sa);
+  alloc_free(sa);
 }
 
 struct sockaddr * socket_addr_new (u32 len)
 {
-    struct sockaddr *sa;
-    assert(len);
-    sa = alloc(len);
-    if (! sa)
-      return NULL;
-    return sa;
+  struct sockaddr *sa;
+  assert(len);
+  sa = alloc(len);
+  if (! sa)
+    return NULL;
+  return sa;
 }
 
 struct sockaddr * socket_addr_new_copy (const struct sockaddr *addr,
                                         u32 len)
 {
-    struct sockaddr *sa;
-    assert(len);
-    sa = alloc(len);
-    if (! sa)
-      return NULL;
-    memcpy(sa, addr, len);
-    return sa;
+  struct sockaddr *sa;
+  assert(len);
+  sa = alloc(len);
+  if (! sa)
+    return NULL;
+  memcpy(sa, addr, len);
+  return sa;
 }
diff --git a/test/facts_test.c b/test/facts_test.c
index fa26e1c..e074ead 100644
--- a/test/facts_test.c
+++ b/test/facts_test.c
@@ -11,6 +11,7 @@
  * THIS SOFTWARE.
  */
 #include <stdio.h>
+#include <stdlib.h>
 #include <unistd.h>
 #include "../libkc3/buf.h"
 #include "../libkc3/compare.h"
diff --git a/window/window.c b/window/window.c
index 19f60e8..f0157f9 100644
--- a/window/window.c
+++ b/window/window.c
@@ -53,7 +53,7 @@ void window_clean (s_window *window)
     sequence_clean(window->sequence + i);
     i++;
   }
-  free(window->sequence);
+  alloc_free(window->sequence);
   tag_clean(&window->tag);
   window->unload(window);
 }
@@ -69,7 +69,7 @@ s_window * window_init (s_window *window,
   tmp.y = y;
   tmp.w = w;
   tmp.h = h;
-  tmp.sequence = calloc(sequence_count, sizeof(s_sequence));
+  tmp.sequence = alloc(sequence_count * sizeof(s_sequence));
   tmp.sequence_count = sequence_count;
   tmp.sequence_pos = 0;
   tag_init_void(&tmp.tag);