diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/kit.inl.h | 415 |
1 files changed, 281 insertions, 134 deletions
diff --git a/include/kit.inl.h b/include/kit.inl.h index 968d973..c28bd27 100644 --- a/include/kit.inl.h +++ b/include/kit.inl.h @@ -56,27 +56,41 @@ typedef signed int kit_status_t; # define _GNU_SOURCE #endif #include <stddef.h> +#include <stdint.h> #ifdef __cplusplus extern "C" { #endif -enum { KIT_ALLOCATE, KIT_DEALLOCATE, KIT_REALLOCATE }; -typedef void *(*kit_allocate_fn)(int request, void *state, - ptrdiff_t size, - ptrdiff_t previous_size, - void *pointer); +enum { + KIT_ALLOC_TYPE_NONE, + KIT_ALLOC_TYPE_DEFAULT, + KIT_ALLOC_TYPE_BUFFER +}; +enum { + KIT_ALLOCATE, + KIT_ALLOCATE_ZERO, + KIT_DEALLOCATE, + KIT_REALLOCATE, + KIT_REALLOCATE_ZERO, + KIT_DEALLOCATE_ALL +}; typedef struct { - void *state; - kit_allocate_fn allocate; + int type; + ptrdiff_t size; + union { + uint8_t *bytes; + 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_buffer(ptrdiff_t size, void *buffer); #ifdef __cplusplus } #endif @@ -575,23 +589,25 @@ static char *kit_make_bs(kit_str_t s) { 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. */ @@ -602,7 +618,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. */ #define KIT_DA_DESTROY(array_) \ @@ -616,6 +632,11 @@ void kit_da_resize(kit_da_void_t *array, ptrdiff_t element_size, #define KIT_DA_RESIZE(array_, 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_) \ @@ -660,6 +681,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 @@ -1269,13 +1291,12 @@ typedef struct { void *state; 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_file(FILE *f, 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); 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_FILE(f) kit_is_wrap_file((f), 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), NULL) #define KIT_IS_READ(in, destination) \ (in).read((in).state, (destination)) #ifndef KIT_DISABLE_SHORT_NAMES @@ -1308,15 +1329,14 @@ typedef struct { void *internal; 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); typedef int (*kit_ib_read_condition_fn)(kit_str_t data); kit_ib_handle_t kit_ib_read_while(kit_ib_handle_t buf, kit_ib_read_condition_fn condition); 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 # define ib_read_condition_fn kit_ib_read_condition_fn @@ -1550,19 +1570,19 @@ extern "C" { # define KIT_PATH_DELIM "/" # 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_string_t kit_path_user(kit_allocator_t alloc); -kit_string_t kit_path_cache(kit_allocator_t alloc); + 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_str_t kit_path_index(kit_str_t path, ptrdiff_t index); kit_str_t kit_path_take(kit_str_t path, ptrdiff_t count); kit_status_t kit_file_create_folder(kit_str_t path); kit_status_t kit_file_create_folder_recursive(kit_str_t path); 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, KIT_PATH_FILE, @@ -1580,8 +1600,8 @@ typedef struct { kit_status_t status; 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); #ifndef KIT_DISABLE_SHORT_NAMES # define path_norm kit_path_norm @@ -1808,48 +1828,161 @@ static int kit_socket_set_nonblocking(socket_t s) { * * *********************************************************************/ #include <assert.h> +#include <stdint.h> #ifndef KIT_DISABLE_SYSTEM_MALLOC # include <stdlib.h> +# include <string.h> #endif -static void *allocate(int request, void *state, ptrdiff_t size, - ptrdiff_t previous_size, void *pointer) { +static void *kit_allocate_default_(int request, ptrdiff_t size, + ptrdiff_t previous_size, + void *pointer) { #ifndef KIT_DISABLE_SYSTEM_MALLOC switch (request) { case KIT_ALLOCATE: + case KIT_ALLOCATE_ZERO: { + assert(size >= 0); assert(previous_size == 0); assert(pointer == NULL); - return malloc(size); + if (size <= 0) + return NULL; + void *p = malloc(size); + 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; + uint8_t *p = NULL; + if (size > 0) { + p = (uint8_t *) 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); + if (pointer != NULL) + free(pointer); return NULL; - case KIT_REALLOCATE: - /* FIXME - * Not implemented. - */ - assert(0); + case KIT_DEALLOCATE_ALL: + // Do nothing. + // 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->bytes += 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 ((uint8_t *) pointer + previous_size == alloc->data) { + if (alloc->size < size - previous_size) + return NULL; + alloc->bytes += size - previous_size; + alloc->size -= size - previous_size; + return pointer; + } + if (alloc->size < size) + return NULL; + uint8_t *p = alloc->bytes; + alloc->bytes += 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) { - assert(alloc.allocate != NULL); - if (alloc.allocate == NULL) - return NULL; - return alloc.allocate(request, alloc.state, size, previous_size, - pointer); + 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_BUFFER: + return kit_allocate_from_buffer_(alloc, request, size, + previous_size, pointer); + default:; + } + return NULL; } #endif kit_allocator_t kit_alloc_default(void) { - kit_allocator_t alloc = { .state = NULL, .allocate = allocate }; + kit_allocator_t alloc = { .type = KIT_ALLOC_TYPE_DEFAULT, + .size = 0, + .data = NULL }; + return alloc; +} +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 alloc; } /********************************************************************* @@ -1885,13 +2018,12 @@ 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); } void call_once(once_flag *flag, void (*func)(void)) { @@ -2038,19 +2170,17 @@ 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; @@ -2148,10 +2278,9 @@ Implementation limits: - Emulated `mtx_timelock()' with mtx_trylock() + *busy loop* */ typedef struct { - thrd_start_t func; - void *arg; - thrd_t thrd; - kit_allocator_t alloc; + thrd_start_t func; + void *arg; + thrd_t thrd; } impl_thrd_param_t; struct thrd_state { thrd_t thrd; @@ -2165,7 +2294,7 @@ static unsigned __stdcall impl_thrd_routine(void *p) { impl_current_thread.thrd = pack_p->thrd; impl_current_thread.handle_need_close = false; memcpy(&pack, pack_p, sizeof(impl_thrd_param_t)); - kit_alloc_dispatch(pack.alloc, KIT_DEALLOCATE, 0, 0, p); + kit_alloc_dispatch(NULL, KIT_DEALLOCATE, 0, 0, p); code = pack.func(pack.arg); return (unsigned) code; } @@ -2332,19 +2461,17 @@ int thrd_create_with_stack(thrd_t *thr, thrd_start_t func, void *arg, uintptr_t handle; assert(thr != NULL); assert(stack_size >= 0 && stack_size < 0x100000000); - 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) return thrd_nomem; - pack->func = func; - pack->arg = arg; - pack->alloc = alloc; - handle = _beginthreadex(NULL, (unsigned) stack_size, - impl_thrd_routine, pack, CREATE_SUSPENDED, - NULL); + pack->func = func; + pack->arg = arg; + handle = _beginthreadex(NULL, (unsigned) stack_size, + impl_thrd_routine, pack, CREATE_SUSPENDED, + NULL); if (handle == 0) { - kit_alloc_dispatch(alloc, KIT_DEALLOCATE, 0, 0, pack); + kit_alloc_dispatch(NULL, KIT_DEALLOCATE, 0, 0, pack); if (errno == EAGAIN || errno == EACCES) return thrd_nomem; return thrd_error; @@ -2598,7 +2725,7 @@ uint64_t kit_atomic_fetch_add_explicit_64(uint64_t volatile *var, #include <assert.h> #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); @@ -2643,6 +2770,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; + } +} /********************************************************************* * * * File: source/kit/input_stream.c * @@ -2651,18 +2799,18 @@ void kit_da_resize(kit_da_void_t *array, ptrdiff_t element_size, #include <string.h> 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) { kit_is_state_basic_t *basic = (kit_is_state_basic_t *) state; @@ -2692,8 +2840,8 @@ static ptrdiff_t kit_read_file_(void *state, kit_str_t destination) { return 0; 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); kit_is_state_str_t *state = (kit_is_state_str_t *) @@ -2709,7 +2857,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); kit_is_state_file_t *state = (kit_is_state_file_t *) @@ -2738,14 +2886,13 @@ void kit_is_destroy(kit_is_handle_t in) { #include <assert.h> #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) { - assert(alloc.allocate != NULL); +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); if (buf != NULL) { @@ -2757,15 +2904,15 @@ 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; if (--buf->ref_count == 0) { @@ -2773,7 +2920,7 @@ static void buf_release(void *p) { kit_alloc_dispatch(buf->alloc, KIT_DEALLOCATE, 0, 0, buf); } } -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); internal_buffer_t *buf = (internal_buffer_t *) p; @@ -2786,8 +2933,8 @@ static void buf_adjust(void *p, ptrdiff_t size) { DA_RESIZE(buf->data, offset + n); } } -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 @@ -2795,13 +2942,13 @@ static ptrdiff_t buf_read(void *p, ptrdiff_t offset, memcpy(destination.values, buf->data.values + offset, n); 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; @@ -2813,15 +2960,15 @@ kit_ib_handle_t kit_ib_read(kit_ib_handle_t buf, ptrdiff_t size) { next.status = buf.status; return next; } - buf_acquire(buf.internal); - buf_adjust(buf.internal, buf.offset + size); - DA_INIT(next.data, size, buf_alloc(buf.internal)); + kit_buf_acquire_(buf.internal); + kit_buf_adjust_(buf.internal, buf.offset + size); + 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); if (next.data.size != n) @@ -2836,18 +2983,18 @@ kit_ib_handle_t kit_ib_read_while( next.status = buf.status; return next; } - buf_acquire(buf.internal); - DA_INIT(next.data, 0, buf_alloc(buf.internal)); + kit_buf_acquire_(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) next.status = KIT_ERROR_BAD_ALLOC; 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) break; @@ -2860,7 +3007,7 @@ kit_ib_handle_t kit_ib_read_while( return next; } void kit_ib_destroy(kit_ib_handle_t buf) { - buf_release(buf.internal); + kit_buf_release_(buf.internal); DA_DESTROY(buf.data); } /********************************************************************* @@ -2890,7 +3037,7 @@ enum { PATH_BUF_SIZE = 4096 }; 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; string_t result; @@ -2902,7 +3049,7 @@ static kit_string_t kit_get_env_(char *name, kit_allocator_t alloc) { DA_RESIZE(result, 0); 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; string_t norm; @@ -2946,7 +3093,7 @@ kit_string_t kit_path_norm(kit_str_t path, kit_allocator_t alloc) { return norm; } 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; @@ -2966,7 +3113,7 @@ kit_string_t kit_path_join(kit_str_t left, kit_str_t right, memcpy(joined.values + left_size + 1, right_values, right_size); 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); @@ -2975,7 +3122,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__) cache = kit_get_env_("LOCALAPPDATA", alloc); @@ -3115,8 +3262,8 @@ kit_status_t kit_file_remove_folder(kit_str_t path) { return rmdir(buf) == 0 ? KIT_OK : KIT_ERROR_RMDIR_FAILED; #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; switch (type) { @@ -3204,8 +3351,8 @@ kit_file_info_t kit_file_info(kit_str_t path) { result.status = KIT_ERROR_FILE_DO_NOT_EXIST; 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 }; DA_INIT(result.files, 0, alloc); |