summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2023-09-08 13:32:54 +0200
committerMitya Selivanov <automainint@guattari.tech>2023-09-08 13:32:54 +0200
commitedad17b52d6558a5559f02cddcb66158193fe164 (patch)
tree5a31dd428c1b81f092bb1fb311368971d4fe5a2b /source
parent0296a6018de8fa4d6fa1496550f84465741dfd26 (diff)
downloadkit-edad17b52d6558a5559f02cddcb66158193fe164.zip
refactor allocs
Diffstat (limited to 'source')
-rw-r--r--source/kit/allocator.c179
-rw-r--r--source/kit/allocator.h32
-rw-r--r--source/kit/dynamic_array.c26
-rw-r--r--source/kit/dynamic_array.h34
-rw-r--r--source/kit/file.c18
-rw-r--r--source/kit/file.h16
-rw-r--r--source/kit/input_buffer.c52
-rw-r--r--source/kit/input_buffer.h7
-rw-r--r--source/kit/input_stream.c22
-rw-r--r--source/kit/input_stream.h11
-rw-r--r--source/kit/thread.posix.c17
-rw-r--r--source/tests/file.test.c32
12 files changed, 290 insertions, 156 deletions
diff --git a/source/kit/allocator.c b/source/kit/allocator.c
index a761407..f39c903 100644
--- a/source/kit/allocator.c
+++ b/source/kit/allocator.c
@@ -1,19 +1,13 @@
#include "allocator.h"
#include <assert.h>
+#include <stdint.h>
#ifndef KIT_DISABLE_SYSTEM_MALLOC
# include <stdlib.h>
# include <string.h>
#endif
-enum {
- KIT_ALLOC_TYPE_NONE,
- KIT_ALLOC_TYPE_DEFAULT,
- KIT_ALLOC_TYPE_BUMP,
- KIT_ALLOC_TYPE_CUSTOM
-};
-
static void *kit_allocate_default_(int request, ptrdiff_t size,
ptrdiff_t previous_size,
void *pointer) {
@@ -21,26 +15,58 @@ static void *kit_allocate_default_(int request, ptrdiff_t size,
switch (request) {
case KIT_ALLOCATE:
case KIT_ALLOCATE_ZERO: {
+ assert(size >= 0);
assert(previous_size == 0);
assert(pointer == NULL);
+
+ if (size <= 0)
+ return NULL;
+
void *p = malloc(size);
- if (request == KIT_ALLOCATE_ZERO && p != NULL)
+
+ if (p != NULL && request == KIT_ALLOCATE_ZERO)
memset(p, 0, size);
+
+ return p;
+ }
+
+ case KIT_REALLOCATE:
+ case KIT_REALLOCATE_ZERO: {
+ assert(size >= 0);
+ assert(previous_size != 0 || pointer == NULL);
+ assert(previous_size == 0 || pointer != NULL);
+
+ if (previous_size == 0 && pointer != NULL)
+ return NULL;
+ if (previous_size != 0 && pointer == NULL)
+ return NULL;
+ if (size == previous_size)
+ return pointer;
+
+ void *p = NULL;
+
+ if (size > 0) {
+ p = malloc(size);
+
+ if (p != NULL) {
+ if (size > 0 && previous_size > 0)
+ memcpy(p, pointer,
+ size < previous_size ? size : previous_size);
+ if (request == KIT_REALLOCATE_ZERO && size > previous_size)
+ memset(p + previous_size, 0, size - previous_size);
+ }
+ }
+
+ free(pointer);
+
return p;
}
case KIT_DEALLOCATE:
assert(size == 0);
assert(pointer != NULL);
- free(pointer);
- return NULL;
-
- case KIT_REALLOCATE:
- case KIT_REALLOCATE_ZERO:
- // FIXME
- // Not implemented.
- //
- assert(0);
+ if (pointer != NULL)
+ free(pointer);
return NULL;
case KIT_DEALLOCATE_ALL:
@@ -48,29 +74,110 @@ static void *kit_allocate_default_(int request, ptrdiff_t size,
//
return NULL;
- default: assert(0);
+ default:;
}
-#else
- assert(0);
#endif
+
+ assert(0);
return NULL;
}
+static void *kit_allocate_from_buffer_(kit_allocator_t *alloc,
+ int request, ptrdiff_t size,
+ ptrdiff_t previous_size,
+ void *pointer) {
+ assert(alloc != NULL);
+ assert(pointer == NULL || pointer < alloc->data);
+
+ if (alloc == NULL)
+ return NULL;
+
+ switch (request) {
+ case KIT_ALLOCATE:
+ case KIT_ALLOCATE_ZERO: {
+ assert(size >= 0);
+ assert(previous_size == 0);
+ assert(pointer == NULL);
+
+ if (size <= 0)
+ return NULL;
+
+ if (alloc->size < size)
+ return NULL;
+
+ void *p = alloc->data;
+ alloc->data += size;
+ alloc->size -= size;
+
+ if (request == KIT_ALLOCATE_ZERO)
+ memset(p, 0, size);
+
+ return p;
+ }
+
+ case KIT_REALLOCATE:
+ case KIT_REALLOCATE_ZERO: {
+ assert(size >= 0);
+ assert(previous_size != 0 || pointer == NULL);
+ assert(previous_size == 0 || pointer != NULL);
+
+ if (size <= 0)
+ return NULL;
+ if (size <= previous_size)
+ return pointer;
+ if (previous_size == 0 && pointer != NULL)
+ return NULL;
+ if (previous_size != 0 && pointer == NULL)
+ return NULL;
+
+ if (pointer + previous_size == alloc->data) {
+ if (alloc->size < size - previous_size)
+ return NULL;
+ alloc->data += size - previous_size;
+ alloc->size -= size - previous_size;
+ return pointer;
+ }
+
+ if (alloc->size < size)
+ return NULL;
+
+ void *p = alloc->data;
+ alloc->data += size;
+ alloc->size -= size;
+
+ if (previous_size > 0)
+ memcpy(p, pointer, previous_size);
+ if (request == KIT_REALLOCATE_ZERO)
+ memset(p + previous_size, 0, size - previous_size);
+
+ return p;
+ }
+
+ case KIT_DEALLOCATE:
+ case KIT_DEALLOCATE_ALL: return NULL;
+
+ default:;
+ }
+
+ assert(0);
+ return NULL;
+}
#ifndef KIT_ENABLE_CUSTOM_ALLOC_DISPATCH
-void *kit_alloc_dispatch(kit_allocator_t alloc, int request,
+void *kit_alloc_dispatch(kit_allocator_t *alloc, int request,
ptrdiff_t size, ptrdiff_t previous_size,
void *pointer) {
- switch (alloc.type) {
+ if (alloc == NULL)
+ return kit_allocate_default_(request, size, previous_size,
+ pointer);
+
+ switch (alloc->type) {
case KIT_ALLOC_TYPE_DEFAULT:
return kit_allocate_default_(request, size, previous_size,
pointer);
- case KIT_ALLOC_TYPE_CUSTOM:
- assert(alloc.allocate != NULL);
- if (alloc.allocate != NULL)
- return alloc.allocate(request, alloc.state, size,
- previous_size, pointer);
- break;
+ case KIT_ALLOC_TYPE_BUFFER:
+ return kit_allocate_from_buffer_(alloc, request, size,
+ previous_size, pointer);
default:;
}
@@ -80,16 +187,16 @@ void *kit_alloc_dispatch(kit_allocator_t alloc, int request,
#endif
kit_allocator_t kit_alloc_default(void) {
- kit_allocator_t alloc = { .state = NULL,
- .type = KIT_ALLOC_TYPE_DEFAULT,
- .allocate = NULL };
+ kit_allocator_t alloc = { .type = KIT_ALLOC_TYPE_DEFAULT,
+ .size = 0,
+ .data = NULL };
return alloc;
}
-kit_allocator_t kit_alloc_bump(ptrdiff_t size, void *buffer,
- kit_allocator_t upstream) {
- // TODO
- //
+kit_allocator_t kit_alloc_buffer(ptrdiff_t size, void *buffer) {
+ kit_allocator_t alloc = { .type = KIT_ALLOC_TYPE_BUFFER,
+ .size = size,
+ .data = buffer };
- return kit_alloc_default();
+ return alloc;
}
diff --git a/source/kit/allocator.h b/source/kit/allocator.h
index f0f5c8d..340883d 100644
--- a/source/kit/allocator.h
+++ b/source/kit/allocator.h
@@ -12,6 +12,12 @@ extern "C" {
#endif
enum {
+ KIT_ALLOC_TYPE_NONE,
+ KIT_ALLOC_TYPE_DEFAULT,
+ KIT_ALLOC_TYPE_BUFFER
+};
+
+enum {
KIT_ALLOCATE,
KIT_ALLOCATE_ZERO,
KIT_DEALLOCATE,
@@ -20,30 +26,24 @@ enum {
KIT_DEALLOCATE_ALL
};
-typedef void *(*kit_allocate_fn)(int request, void *state,
- ptrdiff_t size,
- ptrdiff_t previous_size,
- void *pointer);
-
typedef struct {
- int type;
- void *state;
- kit_allocate_fn allocate;
+ int type;
+ ptrdiff_t size;
+ void *data;
} kit_allocator_t;
-/* Application should implement this function if custom allocator
- * dispatch is enabled.
- *
- * See KIT_ENABLE_CUSTOM_ALLOC_DISPATCH macro.
- */
-void *kit_alloc_dispatch(kit_allocator_t alloc, int request,
+// Application should implement this function if custom allocator
+// dispatch is enabled.
+//
+// See KIT_ENABLE_CUSTOM_ALLOC_DISPATCH macro.
+//
+void *kit_alloc_dispatch(kit_allocator_t *alloc, int request,
ptrdiff_t size, ptrdiff_t previous_size,
void *pointer);
kit_allocator_t kit_alloc_default(void);
-kit_allocator_t kit_alloc_bump(ptrdiff_t size, void *buffer,
- kit_allocator_t upstream);
+kit_allocator_t kit_alloc_buffer(ptrdiff_t size, void *buffer);
#ifdef __cplusplus
}
diff --git a/source/kit/dynamic_array.c b/source/kit/dynamic_array.c
index aece99e..5ba60fc 100644
--- a/source/kit/dynamic_array.c
+++ b/source/kit/dynamic_array.c
@@ -4,7 +4,7 @@
#include <string.h>
void kit_da_init(kit_da_void_t *array, ptrdiff_t element_size,
- ptrdiff_t size, kit_allocator_t alloc) {
+ ptrdiff_t size, kit_allocator_t *alloc) {
assert(array != NULL);
assert(element_size > 0);
assert(size >= 0);
@@ -58,3 +58,27 @@ void kit_da_resize(kit_da_void_t *array, ptrdiff_t element_size,
}
}
}
+
+void kit_da_resize_exact(kit_da_void_t *array, ptrdiff_t element_size,
+ ptrdiff_t capacity) {
+ assert(array != NULL);
+ assert(element_size > 0);
+ assert(capacity >= 0);
+
+ void *bytes = capacity <= 0
+ ? NULL
+ : kit_alloc_dispatch(array->alloc, KIT_ALLOCATE,
+ element_size * capacity, 0,
+ NULL);
+
+ if (bytes != NULL || capacity == 0) {
+ if (array->size > 0 && capacity > 0)
+ memcpy(bytes, array->values, element_size * array->size);
+ if (array->values != NULL)
+ kit_alloc_dispatch(array->alloc, KIT_DEALLOCATE, 0, 0,
+ array->values);
+ array->capacity = capacity;
+ array->size = capacity;
+ array->values = bytes;
+ }
+}
diff --git a/source/kit/dynamic_array.h b/source/kit/dynamic_array.h
index 526fb13..d2f2a6b 100644
--- a/source/kit/dynamic_array.h
+++ b/source/kit/dynamic_array.h
@@ -8,26 +8,29 @@ extern "C" {
#endif
typedef struct {
- ptrdiff_t capacity;
- ptrdiff_t size;
- void *values;
- kit_allocator_t alloc;
+ ptrdiff_t capacity;
+ ptrdiff_t size;
+ void *values;
+ kit_allocator_t *alloc;
} kit_da_void_t;
void kit_da_init(kit_da_void_t *array, ptrdiff_t element_size,
- ptrdiff_t size, kit_allocator_t alloc);
+ ptrdiff_t size, kit_allocator_t *alloc);
void kit_da_resize(kit_da_void_t *array, ptrdiff_t element_size,
ptrdiff_t size);
+void kit_da_resize_exact(kit_da_void_t *array, ptrdiff_t element_size,
+ ptrdiff_t size);
+
/* Dynamic array type definition.
*/
-#define KIT_DA(element_type_) \
- struct { \
- ptrdiff_t capacity; \
- ptrdiff_t size; \
- element_type_ *values; \
- kit_allocator_t alloc; \
+#define KIT_DA(element_type_) \
+ struct { \
+ ptrdiff_t capacity; \
+ ptrdiff_t size; \
+ element_type_ *values; \
+ kit_allocator_t *alloc; \
}
/* Initialize dynamic array.
@@ -40,7 +43,7 @@ void kit_da_resize(kit_da_void_t *array, ptrdiff_t element_size,
*/
#define KIT_DA_CREATE(name_, element_type_, size_) \
KIT_DA(element_type_) name_; \
- KIT_DA_INIT(name_, (size_), kit_alloc_default())
+ KIT_DA_INIT(name_, (size_), NULL)
/* Destroy dynamic array.
*/
@@ -57,6 +60,12 @@ void kit_da_resize(kit_da_void_t *array, ptrdiff_t element_size,
kit_da_resize((kit_da_void_t *) &(array_), \
sizeof((array_).values[0]), size_)
+/* Resize dynamic array with exact capacity.
+ */
+#define KIT_DA_RESIZE_EXACT(array_, capacity_) \
+ kit_da_resize_exact((kit_da_void_t *) &(array_), \
+ sizeof((array_).values[0]), capacity_)
+
/* Append a value to dynamic array.
*/
#define KIT_DA_APPEND(array_, value_) \
@@ -106,6 +115,7 @@ typedef KIT_DA(char) kit_string_t;
# define DA_CREATE KIT_DA_CREATE
# define DA_DESTROY KIT_DA_DESTROY
# define DA_RESIZE KIT_DA_RESIZE
+# define DA_RESIZE_EXACT KIT_DA_RESIZE_EXACT
# define DA_APPEND KIT_DA_APPEND
# define DA_INSERT KIT_DA_INSERT
# define DA_ERASE KIT_DA_ERASE
diff --git a/source/kit/file.c b/source/kit/file.c
index b9fabe2..fd24390 100644
--- a/source/kit/file.c
+++ b/source/kit/file.c
@@ -30,7 +30,7 @@ static int is_delim(char c) {
return c == '/' || c == '\\';
}
-static kit_string_t kit_get_env_(char *name, kit_allocator_t alloc) {
+static kit_string_t kit_get_env_(char *name, kit_allocator_t *alloc) {
char *val = getenv(name);
ptrdiff_t size = val != NULL ? (ptrdiff_t) strlen(val) : 0;
@@ -46,7 +46,7 @@ static kit_string_t kit_get_env_(char *name, kit_allocator_t alloc) {
return result;
}
-kit_string_t kit_path_norm(kit_str_t path, kit_allocator_t alloc) {
+kit_string_t kit_path_norm(kit_str_t path, kit_allocator_t *alloc) {
str_t parent = SZ("..");
ptrdiff_t i, i1, j;
@@ -103,7 +103,7 @@ kit_string_t kit_path_norm(kit_str_t path, kit_allocator_t alloc) {
}
kit_string_t kit_path_join(kit_str_t left, kit_str_t right,
- kit_allocator_t alloc) {
+ kit_allocator_t *alloc) {
ptrdiff_t left_size = left.size;
ptrdiff_t right_size = right.size;
char *right_values = right.values;
@@ -129,7 +129,7 @@ kit_string_t kit_path_join(kit_str_t left, kit_str_t right,
return joined;
}
-kit_string_t kit_path_user(kit_allocator_t alloc) {
+kit_string_t kit_path_user(kit_allocator_t *alloc) {
kit_string_t user = kit_get_env_(KIT_ENV_HOME, alloc);
if (user.size == 0) {
DA_RESIZE(user, 1);
@@ -139,7 +139,7 @@ kit_string_t kit_path_user(kit_allocator_t alloc) {
return user;
}
-kit_string_t kit_path_cache(kit_allocator_t alloc) {
+kit_string_t kit_path_cache(kit_allocator_t *alloc) {
kit_string_t cache, user;
#if defined(_WIN32) && !defined(__CYGWIN__)
@@ -307,8 +307,8 @@ kit_status_t kit_file_remove_folder(kit_str_t path) {
#endif
}
-kit_status_t kit_file_remove_recursive(kit_str_t path,
- kit_allocator_t alloc) {
+kit_status_t kit_file_remove_recursive(kit_str_t path,
+ kit_allocator_t *alloc) {
int type = kit_path_type(path);
ptrdiff_t i;
@@ -409,8 +409,8 @@ kit_file_info_t kit_file_info(kit_str_t path) {
return result;
}
-kit_path_list_t kit_file_enum_folder(kit_str_t path,
- kit_allocator_t alloc) {
+kit_path_list_t kit_file_enum_folder(kit_str_t path,
+ kit_allocator_t *alloc) {
PREPARE_PATH_BUF_;
kit_path_list_t result = { .status = KIT_OK };
diff --git a/source/kit/file.h b/source/kit/file.h
index 52ebe25..491779d 100644
--- a/source/kit/file.h
+++ b/source/kit/file.h
@@ -19,14 +19,14 @@ extern "C" {
# define KIT_ENV_HOME "HOME"
#endif
-kit_string_t kit_path_norm(kit_str_t path, kit_allocator_t alloc);
+kit_string_t kit_path_norm(kit_str_t path, kit_allocator_t *alloc);
kit_string_t kit_path_join(kit_str_t left, kit_str_t right,
- kit_allocator_t alloc);
+ kit_allocator_t *alloc);
-kit_string_t kit_path_user(kit_allocator_t alloc);
+kit_string_t kit_path_user(kit_allocator_t *alloc);
-kit_string_t kit_path_cache(kit_allocator_t alloc);
+kit_string_t kit_path_cache(kit_allocator_t *alloc);
kit_str_t kit_path_index(kit_str_t path, ptrdiff_t index);
@@ -40,8 +40,8 @@ kit_status_t kit_file_remove(kit_str_t path);
kit_status_t kit_file_remove_folder(kit_str_t path);
-kit_status_t kit_file_remove_recursive(kit_str_t path,
- kit_allocator_t alloc);
+kit_status_t kit_file_remove_recursive(kit_str_t path,
+ kit_allocator_t *alloc);
typedef enum {
KIT_PATH_NONE,
@@ -66,8 +66,8 @@ typedef struct {
KIT_DA(kit_string_t) files;
} kit_path_list_t;
-kit_path_list_t kit_file_enum_folder(kit_str_t path,
- kit_allocator_t alloc);
+kit_path_list_t kit_file_enum_folder(kit_str_t path,
+ kit_allocator_t *alloc);
void kit_path_list_destroy(kit_path_list_t list);
diff --git a/source/kit/input_buffer.c b/source/kit/input_buffer.c
index 2e74cad..362b7b8 100644
--- a/source/kit/input_buffer.c
+++ b/source/kit/input_buffer.c
@@ -4,14 +4,14 @@
#include <string.h>
typedef struct {
- ptrdiff_t ref_count;
- kit_is_handle_t upstream;
- kit_allocator_t alloc;
- kit_string_t data;
+ ptrdiff_t ref_count;
+ kit_is_handle_t upstream;
+ kit_allocator_t *alloc;
+ kit_string_t data;
} internal_buffer_t;
-static internal_buffer_t *buf_init(kit_is_handle_t upstream,
- kit_allocator_t alloc) {
+static internal_buffer_t *kit_buf_init_(kit_is_handle_t upstream,
+ kit_allocator_t *alloc) {
internal_buffer_t *buf = kit_alloc_dispatch(alloc, KIT_ALLOCATE,
sizeof *buf, 0, NULL);
@@ -26,19 +26,19 @@ static internal_buffer_t *buf_init(kit_is_handle_t upstream,
return buf;
}
-static kit_allocator_t buf_alloc(void *p) {
+static kit_allocator_t *kit_buf_alloc_(void *p) {
assert(p != NULL);
return ((internal_buffer_t *) p)->alloc;
}
-static void buf_acquire(void *p) {
+static void kit_buf_acquire_(void *p) {
assert(p != NULL);
((internal_buffer_t *) p)->ref_count++;
}
-static void buf_release(void *p) {
+static void kit_buf_release_(void *p) {
assert(p != NULL);
internal_buffer_t *buf = (internal_buffer_t *) p;
@@ -49,7 +49,7 @@ static void buf_release(void *p) {
}
}
-static void buf_adjust(void *p, ptrdiff_t size) {
+static void kit_buf_adjust_(void *p, ptrdiff_t size) {
assert(p != NULL);
assert(size >= 0);
@@ -65,8 +65,8 @@ static void buf_adjust(void *p, ptrdiff_t size) {
}
}
-static ptrdiff_t buf_read(void *p, ptrdiff_t offset,
- kit_str_t destination) {
+static ptrdiff_t kit_buf_read_(void *p, ptrdiff_t offset,
+ kit_str_t destination) {
internal_buffer_t *buf = (internal_buffer_t *) p;
ptrdiff_t n = destination.size < buf->data.size - offset
? destination.size
@@ -75,13 +75,13 @@ static ptrdiff_t buf_read(void *p, ptrdiff_t offset,
return n;
}
-kit_ib_handle_t kit_ib_wrap(kit_is_handle_t upstream,
- kit_allocator_t alloc) {
+kit_ib_handle_t kit_ib_wrap(kit_is_handle_t upstream,
+ kit_allocator_t *alloc) {
kit_ib_handle_t buf;
memset(&buf, 0, sizeof buf);
buf.status = KIT_OK;
DA_INIT(buf.data, 0, alloc);
- buf.internal = buf_init(upstream, alloc);
+ buf.internal = kit_buf_init_(upstream, alloc);
if (buf.internal == NULL)
buf.status = KIT_ERROR_BAD_ALLOC;
return buf;
@@ -96,17 +96,17 @@ kit_ib_handle_t kit_ib_read(kit_ib_handle_t buf, ptrdiff_t size) {
return next;
}
- buf_acquire(buf.internal);
- buf_adjust(buf.internal, buf.offset + size);
+ kit_buf_acquire_(buf.internal);
+ kit_buf_adjust_(buf.internal, buf.offset + size);
- DA_INIT(next.data, size, buf_alloc(buf.internal));
+ DA_INIT(next.data, size, kit_buf_alloc_(buf.internal));
if (next.data.size != size)
next.status = KIT_ERROR_BAD_ALLOC;
kit_str_t destination = { .size = next.data.size,
.values = next.data.values };
- ptrdiff_t n = buf_read(buf.internal, buf.offset, destination);
- next.offset = buf.offset + n;
+ ptrdiff_t n = kit_buf_read_(buf.internal, buf.offset, destination);
+ next.offset = buf.offset + n;
next.internal = buf.internal;
DA_RESIZE(next.data, n);
@@ -126,14 +126,14 @@ kit_ib_handle_t kit_ib_read_while(
return next;
}
- buf_acquire(buf.internal);
+ kit_buf_acquire_(buf.internal);
- DA_INIT(next.data, 0, buf_alloc(buf.internal));
+ DA_INIT(next.data, 0, kit_buf_alloc_(buf.internal));
ptrdiff_t size = 0;
for (;; ++size) {
- buf_adjust(buf.internal, buf.offset + size + 1);
+ kit_buf_adjust_(buf.internal, buf.offset + size + 1);
DA_RESIZE(next.data, size + 1);
if (next.data.size != size + 1)
@@ -141,8 +141,8 @@ kit_ib_handle_t kit_ib_read_while(
kit_str_t destination = { .size = 1,
.values = next.data.values + size };
- ptrdiff_t n = buf_read(buf.internal, buf.offset + size,
- destination);
+ ptrdiff_t n = kit_buf_read_(buf.internal, buf.offset + size,
+ destination);
kit_str_t data = { .size = size + 1, .values = next.data.values };
if (n != 1 || condition == NULL || condition(data) == 0)
@@ -160,6 +160,6 @@ kit_ib_handle_t kit_ib_read_while(
}
void kit_ib_destroy(kit_ib_handle_t buf) {
- buf_release(buf.internal);
+ kit_buf_release_(buf.internal);
DA_DESTROY(buf.data);
}
diff --git a/source/kit/input_buffer.h b/source/kit/input_buffer.h
index 60b4872..0fa1f07 100644
--- a/source/kit/input_buffer.h
+++ b/source/kit/input_buffer.h
@@ -16,8 +16,8 @@ typedef struct {
kit_string_t data;
} kit_ib_handle_t;
-kit_ib_handle_t kit_ib_wrap(kit_is_handle_t upstream,
- kit_allocator_t alloc);
+kit_ib_handle_t kit_ib_wrap(kit_is_handle_t upstream,
+ kit_allocator_t *alloc);
kit_ib_handle_t kit_ib_read(kit_ib_handle_t buf, ptrdiff_t size);
@@ -28,8 +28,7 @@ kit_ib_handle_t kit_ib_read_while(kit_ib_handle_t buf,
void kit_ib_destroy(kit_ib_handle_t buf);
-#define KIT_IB_WRAP(upstream) \
- kit_ib_wrap(upstream, kit_alloc_default())
+#define KIT_IB_WRAP(upstream) kit_ib_wrap(upstream, NULL)
#ifndef KIT_DISABLE_SHORT_NAMES
# define ib_handle_t kit_ib_handle_t
diff --git a/source/kit/input_stream.c b/source/kit/input_stream.c
index f7b17fa..a975fef 100644
--- a/source/kit/input_stream.c
+++ b/source/kit/input_stream.c
@@ -5,20 +5,20 @@
enum { KIT_INPUT_STREAM_STR, KIT_INPUT_STREAM_FILE };
typedef struct {
- ptrdiff_t type;
- kit_allocator_t alloc;
+ ptrdiff_t type;
+ kit_allocator_t *alloc;
} kit_is_state_basic_t;
typedef struct {
- ptrdiff_t type;
- kit_allocator_t alloc;
- kit_str_t string;
+ ptrdiff_t type;
+ kit_allocator_t *alloc;
+ kit_str_t string;
} kit_is_state_str_t;
typedef struct {
- ptrdiff_t type;
- kit_allocator_t alloc;
- FILE *file;
+ ptrdiff_t type;
+ kit_allocator_t *alloc;
+ FILE *file;
} kit_is_state_file_t;
static int kit_is_check_type_(void *state, ptrdiff_t type) {
@@ -58,8 +58,8 @@ static ptrdiff_t kit_read_file_(void *state, kit_str_t destination) {
return size;
}
-kit_is_handle_t kit_is_wrap_string(kit_str_t string,
- kit_allocator_t alloc) {
+kit_is_handle_t kit_is_wrap_string(kit_str_t string,
+ kit_allocator_t *alloc) {
kit_is_handle_t in;
memset(&in, 0, sizeof in);
@@ -77,7 +77,7 @@ kit_is_handle_t kit_is_wrap_string(kit_str_t string,
return in;
}
-kit_is_handle_t kit_is_wrap_file(FILE *f, kit_allocator_t alloc) {
+kit_is_handle_t kit_is_wrap_file(FILE *f, kit_allocator_t *alloc) {
kit_is_handle_t in;
memset(&in, 0, sizeof in);
diff --git a/source/kit/input_stream.h b/source/kit/input_stream.h
index d60e37e..95a6187 100644
--- a/source/kit/input_stream.h
+++ b/source/kit/input_stream.h
@@ -18,17 +18,16 @@ typedef struct {
kit_is_read_fn read;
} kit_is_handle_t;
-kit_is_handle_t kit_is_wrap_string(kit_str_t string,
- kit_allocator_t alloc);
+kit_is_handle_t kit_is_wrap_string(kit_str_t string,
+ kit_allocator_t *alloc);
-kit_is_handle_t kit_is_wrap_file(FILE *f, kit_allocator_t alloc);
+kit_is_handle_t kit_is_wrap_file(FILE *f, kit_allocator_t *alloc);
void kit_is_destroy(kit_is_handle_t in);
-#define KIT_IS_WRAP_STRING(string) \
- kit_is_wrap_string((string), kit_alloc_default())
+#define KIT_IS_WRAP_STRING(string) kit_is_wrap_string((string), NULL)
-#define KIT_IS_WRAP_FILE(f) kit_is_wrap_file((f), kit_alloc_default())
+#define KIT_IS_WRAP_FILE(f) kit_is_wrap_file((f), NULL)
#define KIT_IS_READ(in, destination) \
(in).read((in).state, (destination))
diff --git a/source/kit/thread.posix.c b/source/kit/thread.posix.c
index 79a71e8..bcb089f 100644
--- a/source/kit/thread.posix.c
+++ b/source/kit/thread.posix.c
@@ -37,14 +37,13 @@ Implementation limits:
(see EMULATED_THREADS_USE_NATIVE_TIMEDLOCK macro)
*/
typedef struct {
- thrd_start_t func;
- void *arg;
- kit_allocator_t alloc;
+ thrd_start_t func;
+ void *arg;
} impl_thrd_param_t;
static void *impl_thrd_routine(void *p) {
impl_thrd_param_t pack = *((impl_thrd_param_t *) p);
- kit_alloc_dispatch(pack.alloc, KIT_DEALLOCATE, 0, 0, p);
+ kit_alloc_dispatch(NULL, KIT_DEALLOCATE, 0, 0, p);
return (void *) (intptr_t) pack.func(pack.arg);
}
@@ -213,20 +212,18 @@ int thrd_create_with_stack(thrd_t *thr, thrd_start_t func, void *arg,
return thrd_wrong_stack_size;
attr_p = &attr;
}
- kit_allocator_t alloc = kit_alloc_default();
pack = (impl_thrd_param_t *) kit_alloc_dispatch(
- alloc, KIT_ALLOCATE, sizeof(impl_thrd_param_t), 0, NULL);
+ NULL, KIT_ALLOCATE, sizeof(impl_thrd_param_t), 0, NULL);
if (!pack) {
if (attr_p)
pthread_attr_destroy(attr_p);
return thrd_nomem;
}
- pack->func = func;
- pack->arg = arg;
- pack->alloc = alloc;
+ pack->func = func;
+ pack->arg = arg;
if (pthread_create(thr, attr_p, impl_thrd_routine, pack) != 0) {
- kit_alloc_dispatch(alloc, KIT_DEALLOCATE, 0, 0, pack);
+ kit_alloc_dispatch(NULL, KIT_DEALLOCATE, 0, 0, pack);
if (attr_p)
pthread_attr_destroy(attr_p);
return thrd_error;
diff --git a/source/tests/file.test.c b/source/tests/file.test.c
index e72df83..859de86 100644
--- a/source/tests/file.test.c
+++ b/source/tests/file.test.c
@@ -6,10 +6,8 @@
#include "../kit_test/test.h"
TEST("file path cache") {
- kit_allocator_t alloc = kit_alloc_default();
-
- string_t user = path_user(alloc);
- string_t cache = path_cache(alloc);
+ string_t user = path_user(NULL);
+ string_t cache = path_cache(NULL);
DA_RESIZE(cache, cache.size + 1);
cache.values[cache.size - 1] = '\0';
@@ -18,12 +16,12 @@ TEST("file path cache") {
string_t expected =
#if defined(_WIN32) && !defined(__CYGWIN__)
path_join(WRAP_STR(user), SZ("AppData" PATH_DELIM "Local"),
- alloc);
+ NULL);
#elif defined(__APPLE__)
path_join(WRAP_STR(user), SZ("Library" PATH_DELIM "Caches"),
- alloc);
+ NULL);
#else
- path_join(WRAP_STR(user), SZ(".cache"), alloc);
+ path_join(WRAP_STR(user), SZ(".cache"), NULL);
#endif
REQUIRE(AR_EQUAL(cache, expected));
@@ -37,7 +35,7 @@ TEST("file path normalize one") {
str_t foo = SZ("foo/bar/../baz");
str_t foo_norm = SZ("foo" PATH_DELIM "baz");
- string_t bar = path_norm(foo, kit_alloc_default());
+ string_t bar = path_norm(foo, NULL);
REQUIRE(AR_EQUAL(foo_norm, bar));
@@ -48,7 +46,7 @@ TEST("file path normalize two") {
str_t foo = SZ("foo/bar/../../baz");
str_t foo_norm = SZ("baz");
- string_t bar = path_norm(foo, kit_alloc_default());
+ string_t bar = path_norm(foo, NULL);
REQUIRE(AR_EQUAL(foo_norm, bar));
@@ -59,7 +57,7 @@ TEST("file path normalize parent") {
str_t foo = SZ("../baz");
str_t foo_norm = SZ(".." PATH_DELIM "baz");
- string_t bar = path_norm(foo, kit_alloc_default());
+ string_t bar = path_norm(foo, NULL);
REQUIRE(AR_EQUAL(foo_norm, bar));
@@ -70,7 +68,7 @@ TEST("file path normalize double parent") {
str_t foo = SZ("foo/../../baz");
str_t foo_norm = SZ(".." PATH_DELIM "baz");
- string_t bar = path_norm(foo, kit_alloc_default());
+ string_t bar = path_norm(foo, NULL);
REQUIRE(AR_EQUAL(foo_norm, bar));
@@ -81,7 +79,7 @@ TEST("file path normalize windows delim") {
str_t foo = SZ("foo\\bar\\..\\baz");
str_t foo_norm = SZ("foo" PATH_DELIM "baz");
- string_t bar = path_norm(foo, kit_alloc_default());
+ string_t bar = path_norm(foo, NULL);
REQUIRE(AR_EQUAL(foo_norm, bar));
@@ -93,7 +91,7 @@ TEST("file path join no delim") {
str_t bar = SZ("bar");
str_t joined = SZ("foo" PATH_DELIM "bar");
- string_t foobar = path_join(foo, bar, kit_alloc_default());
+ string_t foobar = path_join(foo, bar, NULL);
REQUIRE(AR_EQUAL(joined, foobar));
@@ -105,7 +103,7 @@ TEST("file path join delim left") {
str_t bar = SZ("bar");
str_t joined = SZ("foo" PATH_DELIM "bar");
- string_t foobar = path_join(foo, bar, kit_alloc_default());
+ string_t foobar = path_join(foo, bar, NULL);
REQUIRE(AR_EQUAL(joined, foobar));
@@ -117,7 +115,7 @@ TEST("file path join delim right") {
str_t bar = SZ("/bar");
str_t joined = SZ("foo" PATH_DELIM "bar");
- string_t foobar = path_join(foo, bar, kit_alloc_default());
+ string_t foobar = path_join(foo, bar, NULL);
REQUIRE(AR_EQUAL(joined, foobar));
@@ -129,7 +127,7 @@ TEST("file path join delim both") {
str_t bar = SZ("/bar");
str_t joined = SZ("foo" PATH_DELIM "bar");
- string_t foobar = path_join(foo, bar, kit_alloc_default());
+ string_t foobar = path_join(foo, bar, NULL);
REQUIRE(AR_EQUAL(joined, foobar));
@@ -137,7 +135,7 @@ TEST("file path join delim both") {
}
TEST("file path user") {
- string_t user = path_user(kit_alloc_default());
+ string_t user = path_user(NULL);
REQUIRE(user.size > 0);