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);