From b951577f4805de53d233d45df2e343019f24883b Mon Sep 17 00:00:00 2001 From: Max Wash Date: Sat, 2 May 2026 21:01:17 +0100 Subject: [PATCH] fx: update header directories --- fx/CMakeLists.txt | 4 +- fx/bst.c | 15 +- fx/bstr.c | 50 +- fx/class.c | 5 +- fx/class.h | 4 +- fx/encoding.c | 6 +- fx/endian.c | 4 +- fx/error.c | 275 ++++++---- fx/error.h | 4 +- fx/hash/hash.c | 29 +- fx/hash/md4.c | 7 +- fx/hash/md5.c | 23 +- fx/hash/sha1.c | 18 +- fx/hash/sha2_224.c | 11 +- fx/hash/sha2_256.c | 50 +- fx/hash/sha2_384.c | 11 +- fx/hash/sha2_512.c | 154 ++++-- fx/hash/sha3.c | 7 +- fx/include/fx/{core => }/bitop.h | 11 +- fx/include/fx/{core => }/bst.h | 210 ++++---- fx/include/fx/{core => }/bstr.h | 41 +- fx/include/fx/{core => }/class.h | 6 +- fx/include/fx/core.h | 0 fx/include/fx/core/error.h | 418 --------------- fx/include/fx/{core => }/encoding.h | 5 +- fx/include/fx/{core => }/endian.h | 2 +- fx/include/fx/error.h | 643 ++++++++++++++++++++++++ fx/include/fx/{core => }/exception.h | 0 fx/include/fx/{core => }/hash.h | 15 +- fx/include/fx/{core => }/init.h | 0 fx/include/fx/{core => }/iterator.h | 42 +- fx/include/fx/{core => }/macros.h | 164 +++--- fx/include/fx/{core => }/misc.h | 0 fx/include/fx/{core => }/object.h | 13 +- fx/include/fx/{core => }/queue.h | 24 +- fx/include/fx/{core => }/random.h | 6 +- fx/include/fx/{core => }/ringbuffer.h | 35 +- fx/include/fx/{core => }/rope.h | 36 +- fx/include/fx/{core => }/status.h | 2 +- fx/include/fx/{core => }/stream.h | 46 +- fx/include/fx/{core => }/stringstream.h | 16 +- fx/include/fx/{core => }/thread.h | 4 +- fx/include/fx/{core => }/type.h | 10 +- fx/iterator.c | 46 +- fx/misc.c | 2 +- fx/mt19937.c | 28 +- fx/object.c | 32 +- fx/object.h | 4 +- fx/queue.c | 10 +- fx/random.c | 6 +- fx/random.h | 6 +- fx/ringbuffer.c | 104 +++- fx/rope.c | 18 +- fx/status.c | 2 +- fx/stream.c | 4 +- fx/stringstream.c | 49 +- fx/sys/linux/bitop.c | 2 +- fx/type.c | 88 +++- fx/type.h | 6 +- 59 files changed, 1831 insertions(+), 1002 deletions(-) rename fx/include/fx/{core => }/bitop.h (54%) rename fx/include/fx/{core => }/bst.h (68%) rename fx/include/fx/{core => }/bstr.h (69%) rename fx/include/fx/{core => }/class.h (78%) delete mode 100644 fx/include/fx/core.h delete mode 100644 fx/include/fx/core/error.h rename fx/include/fx/{core => }/encoding.h (94%) rename fx/include/fx/{core => }/endian.h (97%) create mode 100644 fx/include/fx/error.h rename fx/include/fx/{core => }/exception.h (100%) rename fx/include/fx/{core => }/hash.h (88%) rename fx/include/fx/{core => }/init.h (100%) rename fx/include/fx/{core => }/iterator.h (90%) rename fx/include/fx/{core => }/macros.h (77%) rename fx/include/fx/{core => }/misc.h (100%) rename fx/include/fx/{core => }/object.h (84%) rename fx/include/fx/{core => }/queue.h (78%) rename fx/include/fx/{core => }/random.h (89%) rename fx/include/fx/{core => }/ringbuffer.h (63%) rename fx/include/fx/{core => }/rope.h (89%) rename fx/include/fx/{core => }/status.h (97%) rename fx/include/fx/{core => }/stream.h (79%) rename fx/include/fx/{core => }/stringstream.h (70%) rename fx/include/fx/{core => }/thread.h (92%) rename fx/include/fx/{core => }/type.h (91%) diff --git a/fx/CMakeLists.txt b/fx/CMakeLists.txt index d74b11b..859cede 100644 --- a/fx/CMakeLists.txt +++ b/fx/CMakeLists.txt @@ -1,5 +1,5 @@ include(../cmake/Templates.cmake) -add_fx_module( - NAME core +add_fx_assembly( + NAME fx SUBDIRS hash) diff --git a/fx/bst.c b/fx/bst.c index d8cfbf1..b6ba867 100644 --- a/fx/bst.c +++ b/fx/bst.c @@ -57,7 +57,7 @@ provide a comparator function. */ -#include +#include #include #define MAX(a, b) ((a) > (b) ? (a) : (b)) @@ -419,7 +419,8 @@ void fx_bst_insert_fixup(struct fx_bst *tree, struct fx_bst_node *node) @param node the node to delete. */ static struct fx_bst_node *remove_node_with_no_children( - struct fx_bst *tree, struct fx_bst_node *node) + struct fx_bst *tree, + struct fx_bst_node *node) { struct fx_bst_node *w = node->n_parent; struct fx_bst_node *p = node->n_parent; @@ -452,7 +453,8 @@ static struct fx_bst_node *remove_node_with_no_children( @param node the node to delete. */ static struct fx_bst_node *replace_node_with_one_subst( - struct fx_bst *tree, struct fx_bst_node *node) + struct fx_bst *tree, + struct fx_bst_node *node) { struct fx_bst_node *p = node->n_parent; struct fx_bst_node *z = NULL; @@ -501,7 +503,8 @@ static struct fx_bst_node *replace_node_with_one_subst( @param z the node to delete. */ static struct fx_bst_node *replace_node_with_two_substs( - struct fx_bst *tree, struct fx_bst_node *z) + struct fx_bst *tree, + struct fx_bst_node *z) { /* x will replace z */ struct fx_bst_node *x = z->n_left; @@ -737,7 +740,9 @@ fx_bst_node *fx_bst_prev(const struct fx_bst_node *node) } void fx_bst_move( - struct fx_bst *tree, struct fx_bst_node *dest, struct fx_bst_node *src) + struct fx_bst *tree, + struct fx_bst_node *dest, + struct fx_bst_node *src) { if (src->n_parent) { if (src->n_parent->n_left == src) { diff --git a/fx/bstr.c b/fx/bstr.c index 84871f1..c0a5043 100644 --- a/fx/bstr.c +++ b/fx/bstr.c @@ -1,7 +1,7 @@ #include "printf.h" -#include -#include +#include +#include #include #include #include @@ -153,7 +153,10 @@ static void bstr_fctprintf(char c, void *arg) } static enum fx_status bstr_putcs( - struct fx_bstr *str, const char *s, size_t len, size_t *nr_written) + struct fx_bstr *str, + const char *s, + size_t len, + size_t *nr_written) { for (size_t i = 0; i < len; i++) { formatter_putchar(str, s[i]); @@ -166,7 +169,10 @@ static enum fx_status bstr_putcs( return FX_SUCCESS; } -static enum fx_status bstr_puts(struct fx_bstr *str, const char *s, size_t *nr_written) +static enum fx_status bstr_puts( + struct fx_bstr *str, + const char *s, + size_t *nr_written) { size_t i; for (i = 0; s[i]; i++) { @@ -222,18 +228,26 @@ enum fx_status fx_bstr_write_char(struct fx_bstr *str, char c) } enum fx_status fx_bstr_write_chars( - struct fx_bstr *str, const char *s, size_t len, size_t *nr_written) + struct fx_bstr *str, + const char *s, + size_t len, + size_t *nr_written) { return bstr_putcs(str, s, len, nr_written); } -enum fx_status fx_bstr_write_cstr(struct fx_bstr *str, const char *s, size_t *nr_written) +enum fx_status fx_bstr_write_cstr( + struct fx_bstr *str, + const char *s, + size_t *nr_written) { return bstr_puts(str, s, nr_written); } enum fx_status fx_bstr_write_cstr_list( - struct fx_bstr *str, const char **strs, size_t *nr_written) + struct fx_bstr *str, + const char **strs, + size_t *nr_written) { size_t w = 0; enum fx_status status = FX_SUCCESS; @@ -256,7 +270,10 @@ enum fx_status fx_bstr_write_cstr_list( } enum fx_status fx_bstr_write_cstr_array( - struct fx_bstr *str, const char **strs, size_t count, size_t *nr_written) + struct fx_bstr *str, + const char **strs, + size_t count, + size_t *nr_written) { enum fx_status status = FX_SUCCESS; size_t w = 0; @@ -308,7 +325,9 @@ enum fx_status fx_bstr_add_many(struct fx_bstr *str, size_t *nr_written, ...) } enum fx_status fx_bstr_write_rope( - fx_bstr *strv, const struct fx_rope *rope, size_t *nr_written) + fx_bstr *strv, + const struct fx_rope *rope, + size_t *nr_written) { size_t start = strv->bstr_len; enum fx_status status = fx_rope_to_bstr(rope, strv); @@ -322,18 +341,25 @@ enum fx_status fx_bstr_write_rope( } enum fx_status fx_bstr_write_fmt( - struct fx_bstr *str, size_t *nr_written, const char *format, ...) + struct fx_bstr *str, + size_t *nr_written, + const char *format, + ...) { va_list arg; va_start(arg, format); - enum fx_status result = fx_bstr_write_vfmt(str, nr_written, format, arg); + enum fx_status result + = fx_bstr_write_vfmt(str, nr_written, format, arg); va_end(arg); return result; } enum fx_status fx_bstr_write_vfmt( - struct fx_bstr *str, size_t *nr_written, const char *format, va_list arg) + struct fx_bstr *str, + size_t *nr_written, + const char *format, + va_list arg) { size_t start = str->bstr_len; z__fx_fctprintf(bstr_fctprintf, str, format, arg); diff --git a/fx/class.c b/fx/class.c index 101452c..7fa8623 100644 --- a/fx/class.c +++ b/fx/class.c @@ -3,7 +3,7 @@ #include "type.h" #include -#include +#include #include #include @@ -48,7 +48,8 @@ void *fx_class_get_interface(const struct _fx_class *c, const union fx_type *id) } fx_result fx_class_instantiate( - struct fx_type_registration *type, struct _fx_class **out_class) + struct fx_type_registration *type, + struct _fx_class **out_class) { struct _fx_class *out = malloc(type->r_class_size); if (!out) { diff --git a/fx/class.h b/fx/class.h index 8230c3e..54da881 100644 --- a/fx/class.h +++ b/fx/class.h @@ -1,8 +1,8 @@ #ifndef _CLASS_H_ #define _CLASS_H_ -#include -#include +#include +#include #include struct fx_type_registration; diff --git a/fx/encoding.c b/fx/encoding.c index 5d055e2..0d3c15e 100644 --- a/fx/encoding.c +++ b/fx/encoding.c @@ -1,4 +1,4 @@ -#include +#include #include bool fx_wchar_is_number(fx_wchar c) @@ -1364,7 +1364,9 @@ size_t fx_wchar_utf8_codepoint_count(const char *s, size_t nr_bytes) return nr_codepoints; } -size_t fx_wchar_utf8_string_encoded_size(const fx_wchar *s, size_t nr_codepoints) +size_t fx_wchar_utf8_string_encoded_size( + const fx_wchar *s, + size_t nr_codepoints) { size_t len = 0; for (size_t i = 0; i < nr_codepoints; i++) { diff --git a/fx/endian.c b/fx/endian.c index e342e61..a6c6fac 100644 --- a/fx/endian.c +++ b/fx/endian.c @@ -1,4 +1,4 @@ -#include +#include fx_i16 fx_i16_htob(uint16_t v) { @@ -210,4 +210,4 @@ uint64_t fx_i64_stoh(fx_i64 v) #endif return x; -} \ No newline at end of file +} diff --git a/fx/error.c b/fx/error.c index 0289dbf..47b83db 100644 --- a/fx/error.c +++ b/fx/error.c @@ -1,12 +1,13 @@ #include "error.h" -#include +#include #include #include #include static void default_error_reporter( - const struct fx_error *error, enum fx_error_report_flags flags); + const struct fx_error *error, + enum fx_error_report_flags flags); static void free_error(struct fx_error *error); static void do_free_error(struct fx_error *error); @@ -16,13 +17,15 @@ static const struct fx_error_vendor builtin_vendor; static struct fx_queue free_errors = FX_QUEUE_INIT; static struct fx_queue allocated_errors = FX_QUEUE_INIT; static fx_error_report_function error_reporter = default_error_reporter; -static enum fx_error_report_flags error_reporter_flags = FX_ERROR_REPORT_DEFAULT; +static enum fx_error_report_flags error_reporter_flags + = FX_ERROR_REPORT_DEFAULT; static void error_cleanup(void) { struct fx_queue_entry *entry = fx_queue_pop_back(&allocated_errors); while (entry) { - struct fx_error *error = fx_unbox(struct fx_error, entry, err_entry); + struct fx_error *error + = fx_unbox(struct fx_error, entry, err_entry); do_free_error(error); free(error); @@ -31,7 +34,8 @@ static void error_cleanup(void) entry = fx_queue_pop_back(&free_errors); while (entry) { - struct fx_error *error = fx_unbox(struct fx_error, entry, err_entry); + struct fx_error *error + = fx_unbox(struct fx_error, entry, err_entry); free(error); entry = fx_queue_pop_back(&free_errors); @@ -39,7 +43,9 @@ static void error_cleanup(void) } bool fx_result_is( - struct fx_error *err, const fx_error_vendor *vendor, fx_error_status_code code) + struct fx_error *err, + const fx_error_vendor *vendor, + fx_error_status_code code) { if (!err) { return false; @@ -58,7 +64,8 @@ const struct fx_error_vendor *fx_error_vendor_get_builtin(void) } const struct fx_error_definition *fx_error_vendor_get_error_definition( - const struct fx_error_vendor *vendor, fx_error_status_code code) + const struct fx_error_vendor *vendor, + fx_error_status_code code) { const struct fx_error_definition *error_def = NULL; @@ -70,7 +77,8 @@ const struct fx_error_definition *fx_error_vendor_get_error_definition( return error_def; } - size_t nr_errors = vendor->v_error_definitions_length / sizeof *error_def; + size_t nr_errors + = vendor->v_error_definitions_length / sizeof *error_def; if (code >= 0 && code < nr_errors) { error_def = &vendor->v_error_definitions[code]; } @@ -83,7 +91,8 @@ const struct fx_error_definition *fx_error_vendor_get_error_definition( } const char *fx_error_vendor_get_status_code_name( - const struct fx_error_vendor *vendor, fx_error_status_code code) + const struct fx_error_vendor *vendor, + fx_error_status_code code) { const struct fx_error_definition *error_def = fx_error_vendor_get_error_definition(vendor, code); @@ -91,7 +100,8 @@ const char *fx_error_vendor_get_status_code_name( } const char *fx_error_vendor_get_status_code_description( - const struct fx_error_vendor *vendor, fx_error_status_code code) + const struct fx_error_vendor *vendor, + fx_error_status_code code) { const struct fx_error_definition *error_def = fx_error_vendor_get_error_definition(vendor, code); @@ -99,7 +109,8 @@ const char *fx_error_vendor_get_status_code_description( } const struct fx_error_msg *fx_error_vendor_get_msg( - const struct fx_error_vendor *vendor, fx_error_msg_id msg_id) + const struct fx_error_vendor *vendor, + fx_error_msg_id msg_id) { const struct fx_error_msg *msg = NULL; @@ -131,7 +142,8 @@ static struct fx_error *allocate_error(void) struct fx_queue_entry *entry = fx_queue_pop_back(&free_errors); if (entry) { - struct fx_error *error = fx_unbox(struct fx_error, entry, err_entry); + struct fx_error *error + = fx_unbox(struct fx_error, entry, err_entry); memset(error, 0x0, sizeof *error); fx_queue_push_back(&allocated_errors, entry); return error; @@ -172,7 +184,8 @@ static void do_free_error(struct fx_error *error) entry = fx_queue_pop_back(&error->err_stack); } - size_t nr_param = sizeof error->err_params / sizeof error->err_params[0]; + size_t nr_param + = sizeof error->err_params / sizeof error->err_params[0]; for (size_t i = 0; i < nr_param; i++) { const struct fx_error_template_parameter_definition *param_def = error->err_params[i].__param_def; @@ -205,9 +218,14 @@ static void free_error(struct fx_error *error) } struct fx_error *z__fx_error_create_string( - const fx_error_vendor *vendor, fx_error_status_code code, - struct fx_error *caused_by, const char *file, unsigned int line, - const char *func, const char *description, va_list arg) + const fx_error_vendor *vendor, + fx_error_status_code code, + struct fx_error *caused_by, + const char *file, + unsigned int line, + const char *func, + const char *description, + va_list arg) { struct fx_error *error = allocate_error(); if (!error) { @@ -229,21 +247,31 @@ struct fx_error *z__fx_error_create_string( } struct fx_error *z__fx_error_create_msg( - const struct fx_error_vendor *vendor, fx_error_status_code code, - struct fx_error *caused_by, const char *file, unsigned int line, - const char *func, fx_error_msg_id msg_id, + const struct fx_error_vendor *vendor, + fx_error_status_code code, + struct fx_error *caused_by, + const char *file, + unsigned int line, + const char *func, + fx_error_msg_id msg_id, const fx_error_template_parameter params[]) { const struct fx_error_definition *error_def = fx_error_vendor_get_error_definition(vendor, code); if (!error_def) { - fprintf(stderr, "Undefined %s error %lu\n", vendor->v_name, code); + fprintf(stderr, + "Undefined %s error %lu\n", + vendor->v_name, + code); abort(); } - const struct fx_error_msg *msg = fx_error_vendor_get_msg(vendor, msg_id); + const struct fx_error_msg *msg + = fx_error_vendor_get_msg(vendor, msg_id); if (!msg) { - fprintf(stderr, "Undefined %s error msg %lu\n", vendor->v_name, + fprintf(stderr, + "Undefined %s error msg %lu\n", + vendor->v_name, msg_id); abort(); } @@ -262,7 +290,9 @@ struct fx_error *z__fx_error_create_msg( const struct fx_error_template_parameter *param = ¶ms[i]; const struct fx_error_template_parameter_definition *param_def - = fx_error_msg_get_template_parameter(msg, param->param_name); + = fx_error_msg_get_template_parameter( + msg, + param->param_name); if (!param_def) { continue; } @@ -270,8 +300,8 @@ struct fx_error *z__fx_error_create_msg( memcpy(&error->err_params[i], param, sizeof *param); error->err_params[i].__param_def = param_def; if (param_def->param_type == FX_ERROR_TEMPLATE_PARAM_STRING) { - const char *s - = (const char *)error->err_params[i].param_value; + const char *s = (const char *)error->err_params[i] + .param_value; size_t len = strlen(s); char *s2 = malloc(len + 1); if (!s2) { @@ -290,14 +320,21 @@ struct fx_error *z__fx_error_create_msg( } struct fx_error *z__fx_error_create_template( - const struct fx_error_vendor *vendor, fx_error_status_code code, - struct fx_error *caused_by, const char *file, unsigned int line, - const char *func, const fx_error_template_parameter params[]) + const struct fx_error_vendor *vendor, + fx_error_status_code code, + struct fx_error *caused_by, + const char *file, + unsigned int line, + const char *func, + const fx_error_template_parameter params[]) { const struct fx_error_definition *error_def = fx_error_vendor_get_error_definition(vendor, code); if (!error_def) { - fprintf(stderr, "Undefined %s error %lu\n", vendor->v_name, code); + fprintf(stderr, + "Undefined %s error %lu\n", + vendor->v_name, + code); return NULL; } @@ -315,7 +352,8 @@ struct fx_error *z__fx_error_create_template( const struct fx_error_template_parameter_definition *param_def = fx_error_definition_get_template_parameter( - error_def, param->param_name); + error_def, + param->param_name); if (!param_def) { continue; } @@ -323,8 +361,8 @@ struct fx_error *z__fx_error_create_template( memcpy(&error->err_params[i], param, sizeof *param); error->err_params[i].__param_def = param_def; if (param_def->param_type == FX_ERROR_TEMPLATE_PARAM_STRING) { - const char *s - = (const char *)error->err_params[i].param_value; + const char *s = (const char *)error->err_params[i] + .param_value; size_t len = strlen(s); char *s2 = malloc(len + 1); if (!s2) { @@ -343,7 +381,9 @@ struct fx_error *z__fx_error_create_template( } struct fx_error *z__fx_error_propagate( - struct fx_error *error, const char *file, unsigned int line, + struct fx_error *error, + const char *file, + unsigned int line, const char *function) { if (!error) { @@ -368,22 +408,34 @@ struct fx_error *z__fx_error_propagate( return error; } -struct fx_error *z__fx_error_caused_by(struct fx_error *error, struct fx_error *caused_by) +struct fx_error *z__fx_error_caused_by( + struct fx_error *error, + struct fx_error *caused_by) { error->err_caused_by = caused_by; return error; } struct fx_error *z__fx_error_caused_by_fx_status( - struct fx_error *error, enum fx_status status) + struct fx_error *error, + enum fx_status status) { error->err_caused_by = z__fx_error_create( - &builtin_vendor, status, NULL, NULL, 0, NULL, NULL); + &builtin_vendor, + status, + NULL, + NULL, + 0, + NULL, + NULL); return error; } enum fx_status fx_error_add_submsg_string( - struct fx_error *error, fx_error_submsg_type type, const char *msg, ...) + struct fx_error *error, + fx_error_submsg_type type, + const char *msg, + ...) { struct fx_error_submsg *submsg = malloc(sizeof *submsg); if (!submsg) { @@ -419,14 +471,18 @@ enum fx_status fx_error_add_submsg_string( } enum fx_status z__fx_error_add_submsg_template( - struct fx_error *error, enum fx_error_submsg_type type, - fx_error_msg_id msg_id, struct fx_error_template_parameter params[]) + struct fx_error *error, + enum fx_error_submsg_type type, + fx_error_msg_id msg_id, + struct fx_error_template_parameter params[]) { const struct fx_error_msg *msg = fx_error_vendor_get_msg(error->err_vendor, msg_id); if (!msg) { - fprintf(stderr, "Undefined %s error message %lu\n", - error->err_vendor->v_name, msg_id); + fprintf(stderr, + "Undefined %s error message %lu\n", + error->err_vendor->v_name, + msg_id); abort(); } @@ -442,7 +498,9 @@ enum fx_status z__fx_error_add_submsg_template( const struct fx_error_template_parameter *param = ¶ms[i]; const struct fx_error_template_parameter_definition *param_def - = fx_error_msg_get_template_parameter(msg, param->param_name); + = fx_error_msg_get_template_parameter( + msg, + param->param_name); if (!param_def) { continue; } @@ -450,8 +508,8 @@ enum fx_status z__fx_error_add_submsg_template( memcpy(&submsg->msg_params[i], param, sizeof *param); submsg->msg_params[i].__param_def = param_def; if (param_def->param_type == FX_ERROR_TEMPLATE_PARAM_STRING) { - const char *s - = (const char *)submsg->msg_params[i].param_value; + const char *s = (const char *)submsg->msg_params[i] + .param_value; size_t len = strlen(s); char *s2 = malloc(len + 1); if (!s2) { @@ -478,7 +536,10 @@ void fx_error_discard(struct fx_error *error) } void z__fx_error_throw( - struct fx_error *error, const char *file, unsigned int line, const char *func) + struct fx_error *error, + const char *file, + unsigned int line, + const char *func) { if (!error) { return; @@ -499,13 +560,15 @@ const struct fx_error_vendor *fx_error_get_vendor(const struct fx_error *error) return error->err_vendor; } -const struct fx_error_definition *fx_error_get_definition(const struct fx_error *error) +const struct fx_error_definition *fx_error_get_definition( + const struct fx_error *error) { return error->err_def; } const struct fx_error_template_parameter *fx_error_get_template_parameter( - const struct fx_error *error, const char *param_name) + const struct fx_error *error, + const char *param_name) { const size_t nr_params = sizeof error->err_params / sizeof error->err_params[0]; @@ -538,7 +601,8 @@ const struct fx_error_msg *fx_error_get_msg(const fx_error *error) return error->err_msg; } -const struct fx_error_submsg *fx_error_get_first_submsg(const struct fx_error *error) +const struct fx_error_submsg *fx_error_get_first_submsg( + const struct fx_error *error) { struct fx_queue_entry *entry = fx_queue_first(&error->err_submsg); if (!entry) { @@ -549,7 +613,8 @@ const struct fx_error_submsg *fx_error_get_first_submsg(const struct fx_error *e } const struct fx_error_submsg *fx_error_get_next_submsg( - const struct fx_error *error, const struct fx_error_submsg *msg) + const struct fx_error *error, + const struct fx_error_submsg *msg) { struct fx_queue_entry *entry = fx_queue_next(&msg->msg_entry); if (!entry) { @@ -571,7 +636,8 @@ const struct fx_error_stack_frame *fx_error_get_first_stack_frame( } const struct fx_error_stack_frame *fx_error_get_next_stack_frame( - const struct fx_error *error, const struct fx_error_stack_frame *frame) + const struct fx_error *error, + const struct fx_error_stack_frame *frame) { struct fx_queue_entry *entry = fx_queue_next(&frame->f_entry); if (!entry) { @@ -586,7 +652,8 @@ const fx_error *fx_error_get_caused_by(const struct fx_error *error) return error->err_caused_by; } -enum fx_error_submsg_type fx_error_submsg_get_type(const struct fx_error_submsg *msg) +enum fx_error_submsg_type fx_error_submsg_get_type( + const struct fx_error_submsg *msg) { return msg->msg_type; } @@ -601,13 +668,14 @@ const struct fx_error_msg *fx_error_submsg_get_msg(const fx_error_submsg *msg) return msg->msg_msg; } -const struct fx_error_template_parameter *fx_error_submsg_get_template_parameters( - const struct fx_error_submsg *msg) +const struct fx_error_template_parameter * +fx_error_submsg_get_template_parameters(const struct fx_error_submsg *msg) { return msg->msg_params; } -const char *fx_error_stack_frame_get_filepath(const struct fx_error_stack_frame *frame) +const char *fx_error_stack_frame_get_filepath( + const struct fx_error_stack_frame *frame) { return frame->f_file; } @@ -624,11 +692,13 @@ const char *fx_error_stack_frame_get_function_name( return frame->f_function; } -const struct fx_error_template_parameter_definition *fx_error_definition_get_template_parameter( - const struct fx_error_definition *error_def, const char *param_name) +const struct fx_error_template_parameter_definition * +fx_error_definition_get_template_parameter( + const struct fx_error_definition *error_def, + const char *param_name) { - size_t nr_param - = sizeof error_def->err_params / sizeof error_def->err_params[0]; + size_t nr_param = sizeof error_def->err_params + / sizeof error_def->err_params[0]; for (size_t i = 0; i < nr_param; i++) { if (!error_def->err_params[i].param_name) { @@ -648,8 +718,10 @@ const char *fx_error_msg_get_content(const struct fx_error_msg *msg) return msg->msg_message; } -const struct fx_error_template_parameter_definition *fx_error_msg_get_template_parameter( - const struct fx_error_msg *msg, const char *param_name) +const struct fx_error_template_parameter_definition * +fx_error_msg_get_template_parameter( + const struct fx_error_msg *msg, + const char *param_name) { size_t nr_param = sizeof msg->msg_params / sizeof msg->msg_params[0]; @@ -667,7 +739,8 @@ const struct fx_error_template_parameter_definition *fx_error_msg_get_template_p } void fx_set_error_report_function( - fx_error_report_function func, enum fx_error_report_flags flags) + fx_error_report_function func, + enum fx_error_report_flags flags) { if (!func) { func = default_error_reporter; @@ -685,51 +758,75 @@ static const struct fx_error_definition builtin_errors[] = { FX_ERROR_DEFINITION(FX_SUCCESS, "SUCCESS", "Success"), FX_ERROR_DEFINITION(FX_ERR_NO_MEMORY, "NO_MEMORY", "Out of memory"), FX_ERROR_DEFINITION( - FX_ERR_OUT_OF_BOUNDS, "OUT_OF_BOUNDS", "Argument out of bounds"), + FX_ERR_OUT_OF_BOUNDS, + "OUT_OF_BOUNDS", + "Argument out of bounds"), FX_ERROR_DEFINITION( - FX_ERR_INVALID_ARGUMENT, "INVALID_ARGUMENT", "Invalid argument"), + FX_ERR_INVALID_ARGUMENT, + "INVALID_ARGUMENT", + "Invalid argument"), FX_ERROR_DEFINITION( - FX_ERR_NAME_EXISTS, "NAME_EXISTS", "Name already exists"), + FX_ERR_NAME_EXISTS, + "NAME_EXISTS", + "Name already exists"), FX_ERROR_DEFINITION( - FX_ERR_NOT_SUPPORTED, "NOT_SUPPORTED", "Operation not supported"), + FX_ERR_NOT_SUPPORTED, + "NOT_SUPPORTED", + "Operation not supported"), FX_ERROR_DEFINITION(FX_ERR_BAD_STATE, "BAD_STATE", "Bad state"), FX_ERROR_DEFINITION(FX_ERR_NO_ENTRY, "NO_ENTRY", "Name does not exist"), FX_ERROR_DEFINITION(FX_ERR_NO_DATA, "NO_DATA", "No data available"), FX_ERROR_DEFINITION(FX_ERR_NO_SPACE, "NO_DATA", "No space available"), FX_ERROR_DEFINITION( - FX_ERR_UNKNOWN_FUNCTION, "UNKNOWN_FUNCTION", "Unknown function"), + FX_ERR_UNKNOWN_FUNCTION, + "UNKNOWN_FUNCTION", + "Unknown function"), FX_ERROR_DEFINITION(FX_ERR_BAD_FORMAT, "BAD_FORMAT", "Bad format"), FX_ERROR_DEFINITION(FX_ERR_IO_FAILURE, "IO_FAILURE", "I/O failure"), FX_ERROR_DEFINITION( - FX_ERR_IS_DIRECTORY, "IS_DIRECTORY", "Item is a directory"), + FX_ERR_IS_DIRECTORY, + "IS_DIRECTORY", + "Item is a directory"), FX_ERROR_DEFINITION( - FX_ERR_NOT_DIRECTORY, "NOT_DIRECTORY", "Item is not a directory"), + FX_ERR_NOT_DIRECTORY, + "NOT_DIRECTORY", + "Item is not a directory"), FX_ERROR_DEFINITION( - FX_ERR_PERMISSION_DENIED, "PERMISSION_DENIED", + FX_ERR_PERMISSION_DENIED, + "PERMISSION_DENIED", "Permission denied"), FX_ERROR_DEFINITION(FX_ERR_BUSY, "BUSY", "Resource busy or locked"), FX_ERROR_DEFINITION( - FX_ERR_COMPRESSION_FAILURE, "COMPRESSION_FAILURE", + FX_ERR_COMPRESSION_FAILURE, + "COMPRESSION_FAILURE", "Compression failure"), FX_ERROR_DEFINITION( - FX_ERR_TYPE_REGISTRATION_FAILURE, "TYPE_REGISTRATION_FAILURE", + FX_ERR_TYPE_REGISTRATION_FAILURE, + "TYPE_REGISTRATION_FAILURE", "Type registration failure"), FX_ERROR_DEFINITION( - FX_ERR_CLASS_INIT_FAILURE, "CLASS_INIT_FAILURE", + FX_ERR_CLASS_INIT_FAILURE, + "CLASS_INIT_FAILURE", "Class initialisation failure"), }; static const struct fx_error_msg builtin_msg[] = { - FX_ERROR_MSG(FX_MSG_TYPE_REGISTRATION_FAILURE, "Type registration failed"), + FX_ERROR_MSG( + FX_MSG_TYPE_REGISTRATION_FAILURE, + "Type registration failed"), FX_ERROR_MSG(FX_MSG_CLASS_INIT_FAILURE, "Class initialisation failed"), FX_ERROR_MSG_TEMPLATE( FX_MSG_CLASS_SPECIFIES_UNKNOWN_INTERFACE, "Class @[class_name] specifies an implementation for interface " "@[interface_name] that it does not extend or inherit", FX_ERROR_TEMPLATE_PARAM( - "class_name", FX_ERROR_TEMPLATE_PARAM_STRING, "%s"), + "class_name", + FX_ERROR_TEMPLATE_PARAM_STRING, + "%s"), FX_ERROR_TEMPLATE_PARAM( - "interface_name", FX_ERROR_TEMPLATE_PARAM_STRING, "%s")), + "interface_name", + FX_ERROR_TEMPLATE_PARAM_STRING, + "%s")), }; static const struct fx_error_vendor builtin_vendor = { @@ -741,8 +838,10 @@ static const struct fx_error_vendor builtin_vendor = { }; static void get_error_id( - const struct fx_error_vendor *vendor, const struct fx_error *error, - char *out, size_t max) + const struct fx_error_vendor *vendor, + const struct fx_error *error, + char *out, + size_t max) { const char *vendor_name = NULL; const char *error_name = NULL; @@ -777,7 +876,8 @@ static void get_error_id( } static void print_template_parameter( - const struct fx_error_template_parameter *params, size_t nr_params, + const struct fx_error_template_parameter *params, + size_t nr_params, const char *param_name) { const struct fx_error_template_parameter *param = NULL; @@ -861,7 +961,8 @@ static void print_template_parameter( } static void print_content( - const struct fx_error_template_parameter *params, size_t nr_params, + const struct fx_error_template_parameter *params, + size_t nr_params, const char *s) { char modifier = 0; @@ -933,7 +1034,8 @@ static void print_content( static void print_submsg(const struct fx_error *error) { - const struct fx_error_definition *error_def = fx_error_get_definition(error); + const struct fx_error_definition *error_def + = fx_error_get_definition(error); const struct fx_error_submsg *msg = fx_error_get_first_submsg(error); while (msg) { @@ -992,20 +1094,24 @@ static void print_stack_trace(const struct fx_error *error) } static void report_error( - const fx_error *error, fx_error_report_flags flags, bool caused_by) + const fx_error *error, + fx_error_report_flags flags, + bool caused_by) { const fx_error_vendor *vendor = fx_error_get_vendor(error); char error_id[128]; get_error_id(vendor, error, error_id, sizeof error_id); - const struct fx_error_definition *error_def = fx_error_get_definition(error); + const struct fx_error_definition *error_def + = fx_error_get_definition(error); fx_error_status_code code = fx_error_get_status_code(error); const char *description = fx_error_get_description(error); if (!description && vendor) { description = fx_error_vendor_get_status_code_description( - vendor, code); + vendor, + code); } if (caused_by) { @@ -1053,7 +1159,8 @@ static void report_error( } static void default_error_reporter( - const struct fx_error *error, enum fx_error_report_flags flags) + const struct fx_error *error, + enum fx_error_report_flags flags) { report_error(error, flags, false); } diff --git a/fx/error.h b/fx/error.h index 22eccd3..5f31419 100644 --- a/fx/error.h +++ b/fx/error.h @@ -1,8 +1,8 @@ #ifndef _FX_ERROR_H_ #define _FX_ERROR_H_ -#include -#include +#include +#include struct fx_error_stack_frame { fx_queue_entry f_entry; diff --git a/fx/hash/hash.c b/fx/hash/hash.c index 8cffcff..f79fe52 100644 --- a/fx/hash/hash.c +++ b/fx/hash/hash.c @@ -1,7 +1,7 @@ #include "hash.h" -#include -#include +#include +#include #include #include #include @@ -66,7 +66,9 @@ uint64_t fx_hash_cstr_ex(const char *s, size_t *len) return hash; } -enum fx_status fx_hash_ctx_init(struct fx_hash_ctx *ctx, enum fx_hash_function func) +enum fx_status fx_hash_ctx_init( + struct fx_hash_ctx *ctx, + enum fx_hash_function func) { if (func < 0 || func >= nr_hash_functions) { return FX_ERR_NOT_SUPPORTED; @@ -104,7 +106,10 @@ enum fx_status fx_hash_ctx_reset(struct fx_hash_ctx *ctx) return FX_SUCCESS; } -enum fx_status fx_hash_ctx_update(struct fx_hash_ctx *ctx, const void *p, size_t len) +enum fx_status fx_hash_ctx_update( + struct fx_hash_ctx *ctx, + const void *p, + size_t len) { if (!ctx->ctx_ops) { return FX_ERR_BAD_STATE; @@ -127,13 +132,18 @@ static void update_rope(const fx_rope *rope, void *arg) switch (type) { case FX_ROPE_F_CHAR: - fx_hash_ctx_update(ctx, &rope->r_v.v_char, sizeof rope->r_v.v_char); + fx_hash_ctx_update( + ctx, + &rope->r_v.v_char, + sizeof rope->r_v.v_char); break; case FX_ROPE_F_CSTR: case FX_ROPE_F_CSTR_BORROWED: case FX_ROPE_F_CSTR_STATIC: fx_hash_ctx_update( - ctx, rope->r_v.v_cstr.s, strlen(rope->r_v.v_cstr.s)); + ctx, + rope->r_v.v_cstr.s, + strlen(rope->r_v.v_cstr.s)); break; case FX_ROPE_F_INT: len = snprintf(tmp, sizeof tmp, "%" PRIdPTR, rope->r_v.v_int); @@ -149,14 +159,17 @@ static void update_rope(const fx_rope *rope, void *arg) } enum fx_status fx_hash_ctx_update_rope( - struct fx_hash_ctx *ctx, const struct fx_rope *rope) + struct fx_hash_ctx *ctx, + const struct fx_rope *rope) { fx_rope_iterate(rope, update_rope, ctx); return FX_SUCCESS; } enum fx_status fx_hash_ctx_finish( - struct fx_hash_ctx *ctx, void *out_digest, size_t out_max) + struct fx_hash_ctx *ctx, + void *out_digest, + size_t out_max) { if (!ctx->ctx_ops) { return FX_ERR_BAD_STATE; diff --git a/fx/hash/md4.c b/fx/hash/md4.c index de1ce32..179af00 100644 --- a/fx/hash/md4.c +++ b/fx/hash/md4.c @@ -36,7 +36,7 @@ */ #include "hash.h" -#include +#include #include #include @@ -88,7 +88,10 @@ * This processes one or more 64-byte data blocks, but does NOT update the bit * counters. There are no alignment requirements. */ -static const void *body(struct fx_hash_ctx *ctx, const void *data, unsigned long size) +static const void *body( + struct fx_hash_ctx *ctx, + const void *data, + unsigned long size) { const unsigned char *ptr; uint32_t a, b, c, d; diff --git a/fx/hash/md5.c b/fx/hash/md5.c index 58d0ae2..8b23607 100644 --- a/fx/hash/md5.c +++ b/fx/hash/md5.c @@ -3,7 +3,7 @@ * and is in the public domain. */ #include "hash.h" -#include +#include #include #include #include @@ -30,7 +30,10 @@ void md5_init(struct fx_hash_ctx *ctx) ctx->ctx_state.md5.count[1] = 0; } -uint8_t *md5_transform(struct fx_hash_ctx *ctx, const void *data, uintmax_t size) +uint8_t *md5_transform( + struct fx_hash_ctx *ctx, + const void *data, + uintmax_t size) { uint8_t *ptr = (uint8_t *)data; uint32_t a, b, c, d, aa, bb, cc, dd; @@ -146,7 +149,8 @@ void md5_update(struct fx_hash_ctx *ctx, const void *buffer, size_t buffer_size) uint32_t used; uint32_t free; - if ((ctx->ctx_state.md5.count[0] = ((saved_low + buffer_size) & 0x1fffffff)) + if ((ctx->ctx_state.md5.count[0] + = ((saved_low + buffer_size) & 0x1fffffff)) < saved_low) { ctx->ctx_state.md5.count[1]++; } @@ -158,7 +162,8 @@ void md5_update(struct fx_hash_ctx *ctx, const void *buffer, size_t buffer_size) free = 64 - used; if (buffer_size < free) { - memcpy(&ctx->ctx_state.md5.input[used], buffer, + memcpy(&ctx->ctx_state.md5.input[used], + buffer, buffer_size); return; } @@ -171,7 +176,9 @@ void md5_update(struct fx_hash_ctx *ctx, const void *buffer, size_t buffer_size) if (buffer_size >= 64) { buffer = md5_transform( - ctx, buffer, buffer_size & ~(unsigned long)0x3f); + ctx, + buffer, + buffer_size & ~(unsigned long)0x3f); buffer_size = buffer_size % 64; } @@ -197,13 +204,15 @@ void md5_finish(struct fx_hash_ctx *ctx, void *out, size_t max) ctx->ctx_state.md5.count[0] <<= 3; ctx->ctx_state.md5.input[56] = (uint8_t)(ctx->ctx_state.md5.count[0]); - ctx->ctx_state.md5.input[57] = (uint8_t)(ctx->ctx_state.md5.count[0] >> 8); + ctx->ctx_state.md5.input[57] + = (uint8_t)(ctx->ctx_state.md5.count[0] >> 8); ctx->ctx_state.md5.input[58] = (uint8_t)(ctx->ctx_state.md5.count[0] >> 16); ctx->ctx_state.md5.input[59] = (uint8_t)(ctx->ctx_state.md5.count[0] >> 24); ctx->ctx_state.md5.input[60] = (uint8_t)(ctx->ctx_state.md5.count[1]); - ctx->ctx_state.md5.input[61] = (uint8_t)(ctx->ctx_state.md5.count[1] >> 8); + ctx->ctx_state.md5.input[61] + = (uint8_t)(ctx->ctx_state.md5.count[1] >> 8); ctx->ctx_state.md5.input[62] = (uint8_t)(ctx->ctx_state.md5.count[1] >> 16); ctx->ctx_state.md5.input[63] diff --git a/fx/hash/sha1.c b/fx/hash/sha1.c index 0dc6094..2d1a0b6 100644 --- a/fx/hash/sha1.c +++ b/fx/hash/sha1.c @@ -19,7 +19,7 @@ A million repetitions of "a" #include "hash.h" -#include +#include #include #include @@ -235,10 +235,11 @@ static void sha_finish(struct fx_hash_ctx *context, void *out, size_t max) unsigned char c; for (i = 0; i < 8; i++) { - finalcount[i] = (unsigned char)((context->ctx_state.sha1 - .count[(i >= 4 ? 0 : 1)] - >> ((3 - (i & 3)) * 8)) - & 255); /* Endian independent */ + finalcount[i] + = (unsigned char)((context->ctx_state.sha1 + .count[(i >= 4 ? 0 : 1)] + >> ((3 - (i & 3)) * 8)) + & 255); /* Endian independent */ } char digest[FX_DIGEST_LENGTH_SHA1]; @@ -250,9 +251,10 @@ static void sha_finish(struct fx_hash_ctx *context, void *out, size_t max) } sha_update(context, finalcount, 8); /* Should cause a SHA1Transform() */ for (i = 0; i < 20; i++) { - digest[i] = (unsigned char)((context->ctx_state.sha1.state[i >> 2] - >> ((3 - (i & 3)) * 8)) - & 255); + digest[i] + = (unsigned char)((context->ctx_state.sha1.state[i >> 2] + >> ((3 - (i & 3)) * 8)) + & 255); } memcpy(out, digest, fx_min(size_t, sizeof digest, max)); diff --git a/fx/hash/sha2_224.c b/fx/hash/sha2_224.c index 4355560..18874e5 100644 --- a/fx/hash/sha2_224.c +++ b/fx/hash/sha2_224.c @@ -1,12 +1,17 @@ // SHA-224. Adapted from LibTomCrypt. This code is Public Domain #include "hash.h" -#include +#include #include extern void z__fx_sha2_256_update( - struct fx_hash_ctx *md, const void *src, size_t inlen); -extern void z__fx_sha2_256_finish(struct fx_hash_ctx *md, void *out, size_t max); + struct fx_hash_ctx *md, + const void *src, + size_t inlen); +extern void z__fx_sha2_256_finish( + struct fx_hash_ctx *md, + void *out, + size_t max); static void sha_init(struct fx_hash_ctx *md) { diff --git a/fx/hash/sha2_256.c b/fx/hash/sha2_256.c index 83af845..fd37700 100644 --- a/fx/hash/sha2_256.c +++ b/fx/hash/sha2_256.c @@ -1,7 +1,7 @@ // SHA-256. Adapted from LibTomCrypt. This code is Public Domain #include "hash.h" -#include +#include #include static const uint32_t K[64] = { @@ -77,9 +77,18 @@ static uint32_t Gamma1(uint32_t x) } static void RND( - uint32_t *t0, uint32_t *t1, uint32_t W[], uint32_t a, uint32_t b, - uint32_t c, uint32_t *d, uint32_t e, uint32_t f, uint32_t g, - uint32_t *h, uint32_t i) + uint32_t *t0, + uint32_t *t1, + uint32_t W[], + uint32_t a, + uint32_t b, + uint32_t c, + uint32_t *d, + uint32_t e, + uint32_t f, + uint32_t g, + uint32_t *h, + uint32_t i) { (*t0) = *h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; (*t1) = Sigma0(a) + Maj(a, b, c); @@ -101,13 +110,24 @@ static void sha_compress(struct fx_hash_ctx *md, const unsigned char *buf) // Fill W[16..63] for (int i = 16; i < 64; i++) - W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16]; + W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + + W[i - 16]; // Compress for (int i = 0; i < 64; ++i) { - RND(&t0, &t1, W, S[0], S[1], S[2], &S[3], S[4], S[5], S[6], - &S[7], i); + RND(&t0, + &t1, + W, + S[0], + S[1], + S[2], + &S[3], + S[4], + S[5], + S[6], + &S[7], + i); t = S[7]; S[7] = S[6]; S[6] = S[5]; @@ -141,7 +161,10 @@ static void sha_init(struct fx_hash_ctx *md) md->ctx_state.sha2_256.state[7] = 0x5BE0CD19UL; } -void z__fx_sha2_256_update(struct fx_hash_ctx *md, const void *src, size_t inlen) +void z__fx_sha2_256_update( + struct fx_hash_ctx *md, + const void *src, + size_t inlen) { const uint32_t block_size = sizeof md->ctx_state.sha2_256.buf; const unsigned char *in = (const unsigned char *)(src); @@ -158,7 +181,8 @@ void z__fx_sha2_256_update(struct fx_hash_ctx *md, const void *src, size_t inlen (block_size - md->ctx_state.sha2_256.curlen)); memcpy(md->ctx_state.sha2_256.buf + md->ctx_state.sha2_256.curlen, - in, n); + in, + n); md->ctx_state.sha2_256.curlen += n; in += n; inlen -= n; @@ -180,11 +204,13 @@ void z__fx_sha2_256_finish(struct fx_hash_ctx *md, void *out, size_t max) md->ctx_state.sha2_256.buf[md->ctx_state.sha2_256.curlen++] = (unsigned char)(0x80); - // If the length is currently above 56 bytes we append zeros then compress. - // Then we can fall back to padding zeros and length encoding like normal. + // If the length is currently above 56 bytes we append zeros then + // compress. Then we can fall back to padding zeros and length encoding + // like normal. if (md->ctx_state.sha2_256.curlen > 56) { while (md->ctx_state.sha2_256.curlen < 64) - md->ctx_state.sha2_256.buf[md->ctx_state.sha2_256.curlen++] + md->ctx_state.sha2_256 + .buf[md->ctx_state.sha2_256.curlen++] = 0; sha_compress(md, md->ctx_state.sha2_256.buf); md->ctx_state.sha2_256.curlen = 0; diff --git a/fx/hash/sha2_384.c b/fx/hash/sha2_384.c index c18c18d..f370ebc 100644 --- a/fx/hash/sha2_384.c +++ b/fx/hash/sha2_384.c @@ -1,12 +1,17 @@ // SHA-384. Adapted from LibTomCrypt. This code is Public Domain #include "hash.h" -#include +#include #include extern void z__fx_sha2_512_update( - struct fx_hash_ctx *md, const void *src, size_t inlen); -extern void z__fx_sha2_512_finish(struct fx_hash_ctx *md, void *out, size_t max); + struct fx_hash_ctx *md, + const void *src, + size_t inlen); +extern void z__fx_sha2_512_finish( + struct fx_hash_ctx *md, + void *out, + size_t max); void sha_init(struct fx_hash_ctx *md) { diff --git a/fx/hash/sha2_512.c b/fx/hash/sha2_512.c index 955e922..2cedd89 100644 --- a/fx/hash/sha2_512.c +++ b/fx/hash/sha2_512.c @@ -1,7 +1,7 @@ // SHA-512. Adapted from LibTomCrypt. This code is Public Domain #include "hash.h" -#include +#include #include #include @@ -86,9 +86,18 @@ static uint64_t Gamma1(uint64_t x) return Rot(x, 19) ^ Rot(x, 61) ^ Sh(x, 6); } static void RND( - uint64_t W[], uint64_t *t0, uint64_t *t1, uint64_t a, uint64_t b, - uint64_t c, uint64_t *d, uint64_t e, uint64_t f, uint64_t g, - uint64_t *h, uint64_t i) + uint64_t W[], + uint64_t *t0, + uint64_t *t1, + uint64_t a, + uint64_t b, + uint64_t c, + uint64_t *d, + uint64_t e, + uint64_t f, + uint64_t g, + uint64_t *h, + uint64_t i) { *t0 = *h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; *t1 = Sigma0(a) + Maj(a, b, c); @@ -112,28 +121,109 @@ static void sha_compress(struct fx_hash_ctx *md, const unsigned char *buf) // Fill W[16..79] for (int i = 16; i < 80; i++) { - W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16]; + W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + + W[i - 16]; } // Compress for (int i = 0; i < 80; i += 8) { - RND(W, &t0, &t1, S[0], S[1], S[2], &S[3], S[4], S[5], S[6], - &S[7], i + 0); - RND(W, &t0, &t1, S[7], S[0], S[1], &S[2], S[3], S[4], S[5], - &S[6], i + 1); - RND(W, &t0, &t1, S[6], S[7], S[0], &S[1], S[2], S[3], S[4], - &S[5], i + 2); - RND(W, &t0, &t1, S[5], S[6], S[7], &S[0], S[1], S[2], S[3], - &S[4], i + 3); - RND(W, &t0, &t1, S[4], S[5], S[6], &S[7], S[0], S[1], S[2], - &S[3], i + 4); - RND(W, &t0, &t1, S[3], S[4], S[5], &S[6], S[7], S[0], S[1], - &S[2], i + 5); - RND(W, &t0, &t1, S[2], S[3], S[4], &S[5], S[6], S[7], S[0], - &S[1], i + 6); - RND(W, &t0, &t1, S[1], S[2], S[3], &S[4], S[5], S[6], S[7], - &S[0], i + 7); + RND(W, + &t0, + &t1, + S[0], + S[1], + S[2], + &S[3], + S[4], + S[5], + S[6], + &S[7], + i + 0); + RND(W, + &t0, + &t1, + S[7], + S[0], + S[1], + &S[2], + S[3], + S[4], + S[5], + &S[6], + i + 1); + RND(W, + &t0, + &t1, + S[6], + S[7], + S[0], + &S[1], + S[2], + S[3], + S[4], + &S[5], + i + 2); + RND(W, + &t0, + &t1, + S[5], + S[6], + S[7], + &S[0], + S[1], + S[2], + S[3], + &S[4], + i + 3); + RND(W, + &t0, + &t1, + S[4], + S[5], + S[6], + &S[7], + S[0], + S[1], + S[2], + &S[3], + i + 4); + RND(W, + &t0, + &t1, + S[3], + S[4], + S[5], + &S[6], + S[7], + S[0], + S[1], + &S[2], + i + 5); + RND(W, + &t0, + &t1, + S[2], + S[3], + S[4], + &S[5], + S[6], + S[7], + S[0], + &S[1], + i + 6); + RND(W, + &t0, + &t1, + S[1], + S[2], + S[3], + &S[4], + S[5], + S[6], + S[7], + &S[0], + i + 7); } // Feedback @@ -158,7 +248,10 @@ static void sha_init(struct fx_hash_ctx *md) md->ctx_state.sha2_512.state[7] = 0x5be0cd19137e2179ULL; } -void z__fx_sha2_512_update(struct fx_hash_ctx *md, const void *src, size_t inlen) +void z__fx_sha2_512_update( + struct fx_hash_ctx *md, + const void *src, + size_t inlen) { const uint32_t block_size = sizeof md->ctx_state.sha2_512.block; const unsigned char *in = (const unsigned char *)src; @@ -175,7 +268,8 @@ void z__fx_sha2_512_update(struct fx_hash_ctx *md, const void *src, size_t inlen (block_size - md->ctx_state.sha2_512.curlen)); memcpy(md->ctx_state.sha2_512.block + md->ctx_state.sha2_512.curlen, - in, n); + in, + n); md->ctx_state.sha2_512.curlen += n; in += n; inlen -= n; @@ -197,11 +291,13 @@ void z__fx_sha2_512_finish(struct fx_hash_ctx *md, void *out, size_t max) // Append the '1' bit md->ctx_state.sha2_512.block[md->ctx_state.sha2_512.curlen++] = 0x80; - // If the length is currently above 112 bytes we append zeros then compress. - // Then we can fall back to padding zeros and length encoding like normal. + // If the length is currently above 112 bytes we append zeros then + // compress. Then we can fall back to padding zeros and length encoding + // like normal. if (md->ctx_state.sha2_512.curlen > 112) { while (md->ctx_state.sha2_512.curlen < 128) - md->ctx_state.sha2_512.block[md->ctx_state.sha2_512.curlen++] + md->ctx_state.sha2_512 + .block[md->ctx_state.sha2_512.curlen++] = 0; sha_compress(md, md->ctx_state.sha2_512.block); md->ctx_state.sha2_512.curlen = 0; @@ -211,11 +307,13 @@ void z__fx_sha2_512_finish(struct fx_hash_ctx *md, void *out, size_t max) // note: that from 112 to 120 is the 64 MSB of the length. We assume // that you won't hash 2^64 bits of data... :-) while (md->ctx_state.sha2_512.curlen < 120) { - md->ctx_state.sha2_512.block[md->ctx_state.sha2_512.curlen++] = 0; + md->ctx_state.sha2_512.block[md->ctx_state.sha2_512.curlen++] + = 0; } // Store length - store64(md->ctx_state.sha2_512.length, md->ctx_state.sha2_512.block + 120); + store64(md->ctx_state.sha2_512.length, + md->ctx_state.sha2_512.block + 120); sha_compress(md, md->ctx_state.sha2_512.block); unsigned char digest[FX_DIGEST_LENGTH_SHA2_512]; diff --git a/fx/hash/sha3.c b/fx/hash/sha3.c index b5382c1..0fbc1af 100644 --- a/fx/hash/sha3.c +++ b/fx/hash/sha3.c @@ -30,8 +30,8 @@ #include "hash.h" -#include -#include +#include +#include #include #ifndef KECCAKF_ROUNDS @@ -108,7 +108,8 @@ void sha3_keccakf(uint64_t st[25]) for (i = 0; i < 5; i++) bc[i] = st[j + i]; for (i = 0; i < 5; i++) - st[j + i] ^= (~bc[(i + 1) % 5]) & bc[(i + 2) % 5]; + st[j + i] + ^= (~bc[(i + 1) % 5]) & bc[(i + 2) % 5]; } // Iota diff --git a/fx/include/fx/core/bitop.h b/fx/include/fx/bitop.h similarity index 54% rename from fx/include/fx/core/bitop.h rename to fx/include/fx/bitop.h index c4b6ad9..8b88286 100644 --- a/fx/include/fx/core/bitop.h +++ b/fx/include/fx/bitop.h @@ -1,16 +1,21 @@ #ifndef FX_CORE_BITOP_H_ #define FX_CORE_BITOP_H_ -#include +#include FX_API int fx_popcountl(long v); FX_API int fx_ctzl(long v); FX_API int fx_clzl(long v); #if defined(__GNUC__) || defined(__clang__) -#define fx_cmpxchg(v, expected_val, new_val) \ +#define fx_cmpxchg(v, expected_val, new_val) \ __atomic_compare_exchange_n( \ - v, expected_val, new_val, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED) + v, \ + expected_val, \ + new_val, \ + 0, \ + __ATOMIC_RELAXED, \ + __ATOMIC_RELAXED) #elif defined(_MSC_VER) /* TODO add MSVC support */ #error MSVC intrinsics not yet supported diff --git a/fx/include/fx/core/bst.h b/fx/include/fx/bst.h similarity index 68% rename from fx/include/fx/core/bst.h rename to fx/include/fx/bst.h index 6453368..4dd99f6 100644 --- a/fx/include/fx/core/bst.h +++ b/fx/include/fx/bst.h @@ -1,9 +1,9 @@ #ifndef FX_CORE_BST_H_ #define FX_CORE_BST_H_ -#include -#include -#include +#include +#include +#include #include #include #include @@ -51,49 +51,56 @@ FX_TYPE_CLASS_DECLARATION_END(fx_bst_iterator) custom type. @param function_name the name of the function to generate. */ -#define FX_BST_DEFINE_SIMPLE_INSERT( \ - node_type, container_node_member, container_key_member, function_name) \ - void function_name(fx_bst *tree, node_type *node) \ - { \ - if (!tree->bst_root) { \ - tree->bst_root = &node->container_node_member; \ - fx_bst_insert_fixup(tree, &node->container_node_member); \ - return; \ - } \ - \ - fx_bst_node *cur = tree->bst_root; \ - while (1) { \ - node_type *cur_node = fx_unbox( \ - node_type, cur, container_node_member); \ - fx_bst_node *next = NULL; \ - \ - if (node->container_key_member \ - >= cur_node->container_key_member) { \ - next = fx_bst_right(cur); \ - \ - if (!next) { \ - fx_bst_put_right( \ - cur, \ - &node->container_node_member); \ - break; \ - } \ - } else if ( \ - node->container_key_member \ - < cur_node->container_key_member) { \ - next = fx_bst_left(cur); \ - \ - if (!next) { \ - fx_bst_put_left( \ - cur, \ - &node->container_node_member); \ - break; \ - } \ - } \ - \ - cur = next; \ - } \ - \ - fx_bst_insert_fixup(tree, &node->container_node_member); \ +#define FX_BST_DEFINE_SIMPLE_INSERT( \ + node_type, \ + container_node_member, \ + container_key_member, \ + function_name) \ + void function_name(fx_bst *tree, node_type *node) \ + { \ + if (!tree->bst_root) { \ + tree->bst_root = &node->container_node_member; \ + fx_bst_insert_fixup( \ + tree, \ + &node->container_node_member); \ + return; \ + } \ + \ + fx_bst_node *cur = tree->bst_root; \ + while (1) { \ + node_type *cur_node = fx_unbox( \ + node_type, \ + cur, \ + container_node_member); \ + fx_bst_node *next = NULL; \ + \ + if (node->container_key_member \ + >= cur_node->container_key_member) { \ + next = fx_bst_right(cur); \ + \ + if (!next) { \ + fx_bst_put_right( \ + cur, \ + &node->container_node_member); \ + break; \ + } \ + } else if ( \ + node->container_key_member \ + < cur_node->container_key_member) { \ + next = fx_bst_left(cur); \ + \ + if (!next) { \ + fx_bst_put_left( \ + cur, \ + &node->container_node_member); \ + break; \ + } \ + } \ + \ + cur = next; \ + } \ + \ + fx_bst_insert_fixup(tree, &node->container_node_member); \ } /* defines a node insertion function. @@ -139,50 +146,57 @@ FX_TYPE_CLASS_DECLARATION_END(fx_bst_iterator) @param comparator the name of a comparator function or functional-macro that conforms to the requirements listed above. */ -#define FX_BST_DEFINE_INSERT( \ - node_type, container_node_member, container_key_member, function_name, \ - comparator) \ - void function_name(fx_bst *tree, node_type *node) \ - { \ - if (!tree->bst_root) { \ - tree->bst_root = &node->container_node_member; \ - fx_bst_insert_fixup(tree, &node->container_node_member); \ - return; \ - } \ - \ - fx_bst_node *cur = tree->bst_root; \ - while (1) { \ - node_type *cur_node = fx_unbox( \ - node_type, cur, container_node_member); \ - fx_bst_node *next = NULL; \ - int cmp = comparator(node, cur_node); \ - \ - if (cmp >= 0) { \ - next = fx_bst_right(cur); \ - \ - if (!next) { \ - fx_bst_put_right( \ - cur, \ - &node->container_node_member); \ - break; \ - } \ - } else if (cmp < 0) { \ - next = fx_bst_left(cur); \ - \ - if (!next) { \ - fx_bst_put_left( \ - cur, \ - &node->container_node_member); \ - break; \ - } \ - } else { \ - return; \ - } \ - \ - cur = next; \ - } \ - \ - fx_bst_insert_fixup(tree, &node->container_node_member); \ +#define FX_BST_DEFINE_INSERT( \ + node_type, \ + container_node_member, \ + container_key_member, \ + function_name, \ + comparator) \ + void function_name(fx_bst *tree, node_type *node) \ + { \ + if (!tree->bst_root) { \ + tree->bst_root = &node->container_node_member; \ + fx_bst_insert_fixup( \ + tree, \ + &node->container_node_member); \ + return; \ + } \ + \ + fx_bst_node *cur = tree->bst_root; \ + while (1) { \ + node_type *cur_node = fx_unbox( \ + node_type, \ + cur, \ + container_node_member); \ + fx_bst_node *next = NULL; \ + int cmp = comparator(node, cur_node); \ + \ + if (cmp >= 0) { \ + next = fx_bst_right(cur); \ + \ + if (!next) { \ + fx_bst_put_right( \ + cur, \ + &node->container_node_member); \ + break; \ + } \ + } else if (cmp < 0) { \ + next = fx_bst_left(cur); \ + \ + if (!next) { \ + fx_bst_put_left( \ + cur, \ + &node->container_node_member); \ + break; \ + } \ + } else { \ + return; \ + } \ + \ + cur = next; \ + } \ + \ + fx_bst_insert_fixup(tree, &node->container_node_member); \ } /* defines a simple tree search function. @@ -217,15 +231,20 @@ FX_TYPE_CLASS_DECLARATION_END(fx_bst_iterator) custom type. @param function_name the name of the function to generate. */ -#define FX_BST_DEFINE_SIMPLE_GET( \ - node_type, key_type, container_node_member, container_key_member, \ +#define FX_BST_DEFINE_SIMPLE_GET( \ + node_type, \ + key_type, \ + container_node_member, \ + container_key_member, \ function_name) \ node_type *function_name(const fx_bst *tree, key_type key) \ { \ - fx_bst_node *cur = tree->bst_root; \ + fx_bst_node *cur = tree->bst_root; \ while (cur) { \ node_type *cur_node = fx_unbox( \ - node_type, cur, container_node_member); \ + node_type, \ + cur, \ + container_node_member); \ if (key > cur_node->container_key_member) { \ cur = fx_bst_right(cur); \ } else if (key < cur_node->container_key_member) { \ @@ -240,7 +259,8 @@ FX_TYPE_CLASS_DECLARATION_END(fx_bst_iterator) #define fx_bst_foreach(it, bst) \ for (int z__fx_unique_name() = fx_bst_iterator_begin(bst, it); \ - (it)->node != NULL; fx_bst_iterator_next(it)) + (it)->node != NULL; \ + fx_bst_iterator_next(it)) /* binary tree nodes. this *cannot* be used directly. you need to define a custom node type that contains a member variable of type fx_bst_node. diff --git a/fx/include/fx/core/bstr.h b/fx/include/fx/bstr.h similarity index 69% rename from fx/include/fx/core/bstr.h rename to fx/include/fx/bstr.h index 30a6faa..05c43bc 100644 --- a/fx/include/fx/core/bstr.h +++ b/fx/include/fx/bstr.h @@ -1,8 +1,8 @@ #ifndef FX_CORE_BSTR_H_ #define FX_CORE_BSTR_H_ -#include -#include +#include +#include #include #include @@ -19,7 +19,8 @@ typedef struct fx_bstr { uint64_t bstr_magic; enum fx_bstr_flags bstr_flags; char *bstr_buf; - /* total number of characters in bstr_buf, not including null terminator */ + /* total number of characters in bstr_buf, not including null terminator + */ size_t bstr_len; /* number of bytes allocated for bstr_buf (includes space for the null * terminator) */ @@ -49,20 +50,32 @@ FX_API fx_status fx_bstr_pop_indent(fx_bstr *strv); FX_API fx_status fx_bstr_write_char(fx_bstr *strv, char c); FX_API fx_status fx_bstr_write_chars( - fx_bstr *strv, const char *cs, size_t len, size_t *nr_written); -FX_API fx_status fx_bstr_write_cstr( - fx_bstr *strv, const char *str, size_t *nr_written); -FX_API fx_status fx_bstr_write_cstr_list( - fx_bstr *strv, const char **strs, size_t *nr_written); + fx_bstr *strv, + const char *cs, + size_t len, + size_t *nr_written); +FX_API fx_status +fx_bstr_write_cstr(fx_bstr *strv, const char *str, size_t *nr_written); +FX_API fx_status +fx_bstr_write_cstr_list(fx_bstr *strv, const char **strs, size_t *nr_written); FX_API fx_status fx_bstr_write_cstr_array( - fx_bstr *strv, const char **strs, size_t count, size_t *nr_written); -FX_API fx_status fx_bstr_write_cstr_varg(fx_bstr *strv, size_t *nr_written, ...); + fx_bstr *strv, + const char **strs, + size_t count, + size_t *nr_written); +FX_API fx_status +fx_bstr_write_cstr_varg(fx_bstr *strv, size_t *nr_written, ...); FX_API fx_status fx_bstr_write_rope( - fx_bstr *strv, const struct fx_rope *rope, size_t *nr_written); -FX_API fx_status fx_bstr_write_fmt( - fx_bstr *strv, size_t *nr_written, const char *format, ...); + fx_bstr *strv, + const struct fx_rope *rope, + size_t *nr_written); +FX_API fx_status +fx_bstr_write_fmt(fx_bstr *strv, size_t *nr_written, const char *format, ...); FX_API fx_status fx_bstr_write_vfmt( - fx_bstr *strv, size_t *nr_written, const char *format, va_list arg); + fx_bstr *strv, + size_t *nr_written, + const char *format, + va_list arg); FX_API char *fx_bstr_rope(const struct fx_rope *rope, size_t *nr_written); FX_API char *fx_bstr_fmt(size_t *nr_written, const char *format, ...); diff --git a/fx/include/fx/core/class.h b/fx/include/fx/class.h similarity index 78% rename from fx/include/fx/core/class.h rename to fx/include/fx/class.h index 32c77df..22593ec 100644 --- a/fx/include/fx/core/class.h +++ b/fx/include/fx/class.h @@ -1,7 +1,7 @@ -#ifndef FX_OBJECT_CLASS_H_ -#define FX_OBJECT_CLASS_H_ +#ifndef FX_CLASS_H_ +#define FX_CLASS_H_ -#include +#include #define FX_CLASS_MAGIC 0xDEADFACEDCAFEBEDULL #define FX_CLASS(p) ((fx_class *)(p)) diff --git a/fx/include/fx/core.h b/fx/include/fx/core.h deleted file mode 100644 index e69de29..0000000 diff --git a/fx/include/fx/core/error.h b/fx/include/fx/core/error.h deleted file mode 100644 index 3c52071..0000000 --- a/fx/include/fx/core/error.h +++ /dev/null @@ -1,418 +0,0 @@ -#ifndef FX_CORE_ERROR_H_ -#define FX_CORE_ERROR_H_ - -#include -#include -#include -#include - -#define FX_ERROR_TEMPLATE_PARAMETER_MAX 4 -#define FX_ERROR_MSG_ID_INVALID ((unsigned long)-1) - -#define FX_CATCH(err, expr) ((err = (expr)) != NULL) - -#define fx_result_is_error(result) ((result) != NULL) -#define fx_result_is_success(result) ((result) == NULL) - -#define FX_RESULT_SUCCESS ((fx_result)NULL) -#define FX_RESULT_ERR(err_name) \ - fx_error_with_code(fx_error_vendor_get_builtin(), FX_ERR_##err_name) -#define FX_RESULT_ERR_WITH_STRING(err_name, ...) \ - fx_error_with_string( \ - fx_error_vendor_get_builtin(), FX_ERR_##err_name, __VA_ARGS__) -#define FX_RESULT_STATUS(code) \ - ((code) == FX_SUCCESS \ - ? FX_RESULT_SUCCESS \ - : (fx_error_with_code(fx_error_vendor_get_builtin(), code))) -#define FX_RESULT_STATUS_WITH_STRING(code, ...) \ - ((code) == FX_SUCCESS \ - ? FX_RESULT_SUCCESS \ - : (fx_error_with_string( \ - fx_error_vendor_get_builtin(), code, __VA_ARGS__))) - -#define FX_ERRORS_BUILTIN (fx_error_vendor_get_builtin()) -#define FX_ERRORS_ERRNO (fx_error_vendor_get_errno()) - -#define FX_ERROR_PARAM(name, value) \ - (fx_error_template_parameter) \ - { \ - .param_name = (name), .param_value = (uintptr_t)(value), \ - } - -#define FX_ERROR_TEMPLATE_PARAM(name, type, format) \ - (fx_error_template_parameter_definition) \ - { \ - .param_name = (name), .param_type = (type), \ - .param_format = (format), \ - } -#define FX_ERROR_DEFINITION(code, name, msg) \ - [code] = (fx_error_definition) \ - { \ - .err_name = (name), .err_message = (msg), \ - } -#define FX_ERROR_DEFINITION_TEMPLATE(code, name, msg, ...) \ - [code] = (fx_error_definition) \ - { \ - .err_name = (name), .err_message = (msg), \ - .err_params = __VA_ARGS__, \ - } - -#define FX_ERROR_MSG(id, content) \ - [id] = (fx_error_msg) \ - { \ - .msg_message = (content), \ - } -#define FX_ERROR_MSG_TEMPLATE(id, content, ...) \ - [id] = (fx_error_msg) \ - { \ - .msg_message = (content), .msg_params = __VA_ARGS__, \ - } - -#define z__fx_error_create_status(status_code) \ - (z__fx_error_create( \ - fx_error_vendor_get_builtin(), status_code, NULL, NULL, 0, \ - NULL, NULL)) - -/* Error creation macros */ -#define fx_error_with_code(vendor, code) \ - (z__fx_error_create( \ - vendor, code, NULL, __FILE__, __LINE__, __FUNCTION__, NULL)) -#define fx_error_caused_by_error(vendor, code, cause_error) \ - (z__fx_error_create( \ - vendor, code, cause_error, __FILE__, __LINE__, __FUNCTION__, NULL)) -#define fx_error_caused_by_status(vendor, code, cause_status) \ - (z__fx_error_create( \ - vendor, code, z__fx_error_create_status(cause_status), \ - __FILE__, __LINE__, __FUNCTION__, NULL)) -#define fx_error_caused_by_code(vendor, code, cause_vendor, cause_code) \ - (z__fx_error_create( \ - vendor, code, fx_error_with_code(cause_vendor, cause_code), \ - __FILE__, __LINE__, __FUNCTION__, NULL)) -#define fx_error_with_string(vendor, code, ...) \ - (z__fx_error_create( \ - vendor, code, NULL, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)) -#define fx_error_with_string_caused_by_error(vendor, code, cause_error, ...) \ - (z__fx_error_create( \ - vendor, code, cause_error, __FILE__, __LINE__, __FUNCTION__, \ - __VA_ARGS__)) -#define fx_error_with_string_caused_by_status(vendor, code, cause_status, ...) \ - (z__fx_error_create( \ - vendor, code, z__fx_error_create_status(cause_status), \ - __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)) -#define fx_error_with_msg(vendor, code, msg_id) \ - (z__fx_error_create_msg( \ - vendor, code, NULL, __FILE__, __LINE__, __FUNCTION__, msg_id, \ - (fx_error_template_parameter[]) {{}})) -#define fx_error_with_msg_caused_by_error(vendor, code, cause_error, msg_id) \ - (z__fx_error_create_msg( \ - vendor, code, cause_error, __FILE__, __LINE__, __FUNCTION__, \ - msg_id, (fx_error_template_parameter[]) {{}})) -#define fx_error_with_msg_caused_by_status(vendor, code, cause_status, msg_id) \ - (z__fx_error_create_msg( \ - vendor, code, z__fx_error_create_status(cause_status), \ - __FILE__, __LINE__, __FUNCTION__, msg_id, \ - (fx_error_template_parameter[]) {{}})) -#define fx_error_with_msg_template(vendor, code, msg_id, ...) \ - (z__fx_error_create_msg( \ - vendor, code, NULL, __FILE__, __LINE__, __FUNCTION__, msg_id, \ - (fx_error_template_parameter[]) {__VA_ARGS__, {}})) -#define fx_error_with_msg_template_caused_by_error( \ - vendor, code, cause_error, msg_id, ...) \ - (z__fx_error_create_msg( \ - vendor, code, cause_error, __FILE__, __LINE__, __FUNCTION__, \ - msg_id, (fx_error_template_parameter[]) {__VA_ARGS__, {}})) -#define fx_error_with_msg_template_caused_by_status( \ - vendor, code, cause_status, msg_id, ...) \ - (z__fx_error_create_msg( \ - vendor, code, z__fx_error_create_status(cause_status), \ - __FILE__, __LINE__, __FUNCTION__, msg_id, \ - (fx_error_template_parameter[]) {__VA_ARGS__, {}})) -#define fx_error_with_template(vendor, code, ...) \ - (z__fx_error_create_template( \ - vendor, code, NULL, __FILE__, __LINE__, __FUNCTION__, \ - (fx_error_template_parameter[]) {__VA_ARGS__, {}})) -#define fx_error_with_template_caused_by_error(vendor, code, cause_error, ...) \ - (z__fx_error_create_template( \ - vendor, code, cause_error, __FILE__, __LINE__, __FUNCTION__, \ - (fx_error_template_parameter[]) {__VA_ARGS__, {}})) -#define fx_error_with_template_caused_by_status(vendor, code, cause_status, ...) \ - (z__fx_error_create_template( \ - vendor, code, z__fx_error_create_status(cause_status), \ - __FILE__, __LINE__, __FUNCTION__, \ - (fx_error_template_parameter[]) {__VA_ARGS__, {}})) - -/* Error propagation macros */ -#define fx_result_propagate(err) \ - (z__fx_error_propagate(err, __FILE__, __LINE__, __FUNCTION__)) -#define fx_error_caused_by(err, caused_by) (z__fx_error_caused_by(err, caused_by)) -#define fx_error_caused_by_fx_status(err, status) \ - (z__fx_error_caused_by_fx_status(err, status)) -#define fx_error_replace(err, caused_by) \ - (z__fx_error_propagate(err, __FILE__, __LINE__, __FUNCTION__)) - -/* Error throw macros */ -#define z__fx_throw(err) (z__fx_error_throw(err, NULL, 0, NULL)) -#define fx_throw(err) (z__fx_error_throw(err, __FILE__, __LINE__, __FUNCTION__)) -#define fx_throw_status(status) \ - (z__fx_error_throw( \ - z__fx_error_create( \ - fx_error_vendor_get_builtin(), status, NULL, NULL, 0, \ - NULL, NULL), \ - __FILE__, __LINE__, __FUNCTION__)) - -#define fx_throw_status_string(status, ...) \ - (z__fx_error_throw( \ - z__fx_error_create( \ - fx_error_vendor_get_builtin(), status, NULL, NULL, 0, \ - NULL, __VA_ARGS__), \ - __FILE__, __LINE__, __FUNCTION__)) -#define fx_throw_error_code(vendor, code) \ - z__fx_throw(fx_error_with_code(vendor, code)) -#define fx_throw_error_caused_by_error(vendor, code, cause) \ - z__fx_throw(fx_error_caused_by_error(vendor, code, cause)) -#define fx_throw_error_caused_by_status(vendor, code, cause) \ - z__fx_throw(fx_error_caused_by_status(vendor, code, cause)) -#define fx_throw_error_with_string(vendor, code, ...) \ - z__fx_throw(fx_error_with_string(vendor, code, __VA_ARGS__)) -#define fx_throw_error_with_string_caused_by_error(vendor, code, cause, ...) \ - z__fx_throw(fx_error_with_string_caused_by_error( \ - vendor, code, cause, __VA_ARGS__)) -#define fx_throw_error_with_string_caused_by_status(vendor, code, cause, ...) \ - z__fx_throw(fx_error_with_string_caused_by_status( \ - vendor, code, cause, __VA_ARGS__)) - -#define fx_throw_error_with_msg(vendor, code, msg_id) \ - z__fx_throw(fx_error_with_msg(vendor, code, msg_id)) -#define fx_throw_error_with_msg_caused_by_error(vendor, code, cause, msg_id) \ - z__fx_throw(fx_error_with_msg_caused_by_error(vendor, code, cause, msg_id)) -#define fx_throw_error_with_msg_caused_by_status(vendor, code, cause, msg_id) \ - z__fx_throw(fx_error_with_msg_caused_by_status(vendor, code, cause, msg_id)) - -#define fx_throw_error_with_msg_template(vendor, code, msg_id, ...) \ - z__fx_throw(fx_error_with_msg_template(vendor, code, msg_id, __VA_ARGS__)) -#define fx_throw_error_with_msg_template_caused_by_error( \ - vendor, code, cause, msg_id, ...) \ - z__fx_throw(fx_error_with_msg_template_caused_by_error( \ - vendor, code, cause, msg_id, __VA_ARGS__)) -#define fx_throw_error_with_msg_template_caused_by_status( \ - vendor, code, cause, msg_id, ...) \ - z__fx_throw(fx_error_with_msg_template_caused_by_status( \ - vendor, code, cause, msg_id, __VA_ARGS__)) - -#define fx_throw_error_with_template(vendor, code, ...) \ - z__fx_throw(fx_error_with_template(vendor, code, __VA_ARGS__)) -#define fx_throw_error_with_template_caused_by_error(vendor, code, cause, ...) \ - z__fx_throw(fx_error_with_template_caused_by_error( \ - vendor, code, cause, __VA_ARGS__)) -#define fx_throw_error_with_template_caused_by_status(vendor, code, cause, ...) \ - z__fx_throw(fx_error_with_template_caused_by_status( \ - vendor, code, cause, __VA_ARGS__)) - -#define FX_ERR_MSG(s) \ - { \ - .msg_type = FX_ERROR_MESSAGE_ERROR, \ - .msg_content = (s), \ - } -#define FX_ERR_MSG_WARN(s) \ - { \ - .msg_type = FX_ERROR_MESSAGE_WARN, \ - .msg_content = (s), \ - } -#define FX_ERR_MSG_INFO(s) \ - { \ - .msg_type = FX_ERROR_MESSAGE_INFO, \ - .msg_content = (s), \ - } -#define FX_ERR_MSG_END(s) \ - { \ - .msg_type = FX_ERROR_MESSAGE_NONE, \ - .msg_content = NULL, \ - } - -typedef enum fx_error_submsg_type { - FX_ERROR_SUBMSG_NONE = 0, - FX_ERROR_SUBMSG_ERROR, - FX_ERROR_SUBMSG_WARNING, - FX_ERROR_SUBMSG_INFO, -} fx_error_submsg_type; - -typedef enum fx_error_report_flags { - FX_ERROR_REPORT_NONE = 0, - FX_ERROR_REPORT_STATUS = 0x01u, - FX_ERROR_REPORT_DESCRIPTION = 0x02u, - FX_ERROR_REPORT_SUBMSG = 0x04u, - FX_ERROR_REPORT_STACK_TRACE = 0x08u, - FX_ERROR_REPORT_CAUSE = 0x10u, - - FX_ERROR_REPORT_MINIMAL = FX_ERROR_REPORT_STATUS | FX_ERROR_REPORT_DESCRIPTION, - FX_ERROR_REPORT_DEFAULT = FX_ERROR_REPORT_MINIMAL | FX_ERROR_REPORT_SUBMSG - | FX_ERROR_REPORT_CAUSE, - FX_ERROR_REPORT_ALL = FX_ERROR_REPORT_DEFAULT | FX_ERROR_REPORT_STACK_TRACE, -} fx_error_report_flags; - -typedef enum fx_error_template_parameter_type { - FX_ERROR_TEMPLATE_PARAM_NONE = 0, - FX_ERROR_TEMPLATE_PARAM_STRING, - FX_ERROR_TEMPLATE_PARAM_CHAR, - FX_ERROR_TEMPLATE_PARAM_INT, - FX_ERROR_TEMPLATE_PARAM_UINT, - FX_ERROR_TEMPLATE_PARAM_LONG, - FX_ERROR_TEMPLATE_PARAM_ULONG, - FX_ERROR_TEMPLATE_PARAM_LONGLONG, - FX_ERROR_TEMPLATE_PARAM_ULONGLONG, - FX_ERROR_TEMPLATE_PARAM_SIZE_T, - FX_ERROR_TEMPLATE_PARAM_INTPTR, - FX_ERROR_TEMPLATE_PARAM_UINTPTR, - FX_ERROR_TEMPLATE_PARAM_PTR, -} fx_error_template_parameter_type; - -typedef struct fx_error_template_parameter_definition { - const char *param_name; - fx_error_template_parameter_type param_type; - const char *param_format; -} fx_error_template_parameter_definition; - -typedef struct fx_error_template_parameter { - const char *param_name; - uintptr_t param_value; - const struct fx_error_template_parameter_definition *__param_def; -} fx_error_template_parameter; - -struct fx_error_vendor; - -typedef struct fx_error fx_error; -typedef struct fx_error *fx_result; -typedef struct fx_error_submsg fx_error_submsg; -typedef struct fx_error_stack_frame fx_error_stack_frame; -typedef long fx_error_status_code; -typedef unsigned long fx_error_msg_id; - -typedef struct fx_error_definition { - const char *err_name; - const char *err_message; - const fx_error_template_parameter_definition err_params[FX_ERROR_TEMPLATE_PARAMETER_MAX]; -} fx_error_definition; - -typedef struct fx_error_msg { - const char *msg_message; - const fx_error_template_parameter_definition msg_params[FX_ERROR_TEMPLATE_PARAMETER_MAX]; -} fx_error_msg; - -typedef const fx_error_definition *(*fx_error_status_code_get_definition)( - const struct fx_error_vendor *, fx_error_status_code); -typedef const fx_error_msg *(*fx_error_msg_get_definition)( - const struct fx_error_vendor *, fx_error_msg_id); -typedef void (*fx_error_report_function)( - const struct fx_error *, fx_error_report_flags); - -typedef struct fx_error_vendor { - const char *v_name; - - fx_error_status_code_get_definition v_status_get_definition; - fx_error_msg_get_definition v_msg_get_definition; - - const fx_error_definition *v_error_definitions; - size_t v_error_definitions_length; - - const fx_error_msg *v_msg; - size_t v_msg_length; -} fx_error_vendor; - -FX_API fx_error *z__fx_error_create_template( - const fx_error_vendor *, fx_error_status_code, fx_error *, const char *, - unsigned int, const char *, const fx_error_template_parameter[]); -FX_API fx_error *z__fx_error_create_string( - const fx_error_vendor *, fx_error_status_code, fx_error *, const char *, - unsigned int, const char *, const char *, va_list); -FX_API fx_error *z__fx_error_create_msg( - const fx_error_vendor *, fx_error_status_code, fx_error *, const char *, - unsigned int, const char *, fx_error_msg_id, - const fx_error_template_parameter[]); -FX_API fx_error *z__fx_error_propagate( - fx_error *, const char *, unsigned int, const char *); -FX_API fx_error *z__fx_error_caused_by(fx_error *, fx_error *); -FX_API fx_error *z__fx_error_caused_by_fx_status(fx_error *, fx_status); -FX_API void z__fx_error_throw(fx_error *, const char *, unsigned int, const char *); - -FX_API bool fx_result_is( - fx_result result, const fx_error_vendor *vendor, fx_error_status_code code); - -FX_API const fx_error_vendor *fx_error_vendor_get_builtin(void); -FX_API const fx_error_vendor *fx_error_vendor_get_errno(void); -FX_API const fx_error_definition *fx_error_vendor_get_error_definition( - const fx_error_vendor *vendor, fx_error_status_code code); -FX_API const char *fx_error_vendor_get_status_code_name( - const fx_error_vendor *vendor, fx_error_status_code code); -FX_API const char *fx_error_vendor_get_status_code_description( - const fx_error_vendor *vendor, fx_error_status_code code); -FX_API const fx_error_msg *fx_error_vendor_get_msg( - const fx_error_vendor *vendor, fx_error_msg_id msg_id); - -static inline fx_error *z__fx_error_create( - const fx_error_vendor *v, fx_error_status_code c, fx_error *c2, - const char *f0, unsigned int l, const char *f1, const char *d, ...) -{ - va_list arg; - va_start(arg, d); - fx_error *err = z__fx_error_create_string(v, c, c2, f0, l, f1, d, arg); - va_end(arg); - return err; -} - -FX_API enum fx_status fx_error_add_submsg_string( - fx_error *error, fx_error_submsg_type type, const char *msg, ...); -FX_API enum fx_status z__fx_error_add_submsg_template( - fx_error *error, fx_error_submsg_type type, fx_error_msg_id msg_id, - fx_error_template_parameter param[]); -#define fx_error_add_submsg(error, type, msg_id) \ - (z__fx_error_add_submsg_template( \ - error, type, msg_id, (fx_error_template_parameter[]) {{}})) -#define fx_error_add_submsg_template(error, type, msg_id, ...) \ - (z__fx_error_add_submsg_template( \ - error, type, msg_id, \ - (fx_error_template_parameter[]) {__VA_ARGS__, {}})) - -FX_API void fx_error_discard(fx_error *error); - -FX_API fx_error_status_code fx_error_get_status_code(const fx_error *error); -FX_API const fx_error_vendor *fx_error_get_vendor(const fx_error *error); -FX_API const fx_error_definition *fx_error_get_definition(const fx_error *error); -FX_API const fx_error_template_parameter *fx_error_get_template_parameter( - const fx_error *error, const char *param_name); -FX_API const fx_error_template_parameter *fx_error_get_template_parameters( - const fx_error *error); -FX_API const char *fx_error_get_description(const fx_error *error); -FX_API const fx_error_msg *fx_error_get_msg(const fx_error *error); -FX_API const fx_error_submsg *fx_error_get_first_submsg(const fx_error *error); -FX_API const fx_error_submsg *fx_error_get_next_submsg( - const fx_error *error, const fx_error_submsg *msg); -FX_API const fx_error_stack_frame *fx_error_get_first_stack_frame( - const fx_error *error); -FX_API const fx_error_stack_frame *fx_error_get_next_stack_frame( - const fx_error *error, const fx_error_stack_frame *frame); -FX_API const fx_error *fx_error_get_caused_by(const fx_error *error); - -FX_API fx_error_submsg_type fx_error_submsg_get_type(const fx_error_submsg *msg); -FX_API const char *fx_error_submsg_get_content(const fx_error_submsg *msg); -FX_API const fx_error_msg *fx_error_submsg_get_msg(const fx_error_submsg *msg); -FX_API const fx_error_template_parameter *fx_error_submsg_get_template_parameters( - const fx_error_submsg *msg); - -FX_API const char *fx_error_stack_frame_get_filepath( - const fx_error_stack_frame *frame); -FX_API unsigned int fx_error_stack_frame_get_line_number( - const fx_error_stack_frame *frame); -FX_API const char *fx_error_stack_frame_get_function_name( - const fx_error_stack_frame *frame); - -FX_API const fx_error_template_parameter_definition *fx_error_definition_get_template_parameter( - const fx_error_definition *error_def, const char *param_name); - -FX_API const char *fx_error_msg_get_content(const fx_error_msg *msg); -FX_API const fx_error_template_parameter_definition *fx_error_msg_get_template_parameter( - const fx_error_msg *msg, const char *param_name); - -FX_API void fx_set_error_report_function( - fx_error_report_function func, fx_error_report_flags flags); - -#endif diff --git a/fx/include/fx/core/encoding.h b/fx/include/fx/encoding.h similarity index 94% rename from fx/include/fx/core/encoding.h rename to fx/include/fx/encoding.h index 1347b27..3b08222 100644 --- a/fx/include/fx/core/encoding.h +++ b/fx/include/fx/encoding.h @@ -1,7 +1,7 @@ #ifndef FX_CORE_ENCODING_H_ #define FX_CORE_ENCODING_H_ -#include +#include #include #include @@ -36,6 +36,7 @@ FX_API unsigned int fx_wchar_utf8_codepoint_encode(fx_wchar c, char s[4]); FX_API unsigned int fx_wchar_utf8_codepoint_stride(const char *s); FX_API size_t fx_wchar_utf8_codepoint_count(const char *s, size_t nr_bytes); FX_API size_t fx_wchar_utf8_string_encoded_size( - const fx_wchar *s, size_t nr_codepoints); + const fx_wchar *s, + size_t nr_codepoints); #endif diff --git a/fx/include/fx/core/endian.h b/fx/include/fx/endian.h similarity index 97% rename from fx/include/fx/core/endian.h rename to fx/include/fx/endian.h index d1a8e6e..efb9bc9 100644 --- a/fx/include/fx/core/endian.h +++ b/fx/include/fx/endian.h @@ -1,7 +1,7 @@ #ifndef FX_CORE_ENDIAN_H_ #define FX_CORE_ENDIAN_H_ -#include +#include #include typedef struct { diff --git a/fx/include/fx/error.h b/fx/include/fx/error.h new file mode 100644 index 0000000..3f44de4 --- /dev/null +++ b/fx/include/fx/error.h @@ -0,0 +1,643 @@ +#ifndef FX_CORE_ERROR_H_ +#define FX_CORE_ERROR_H_ + +#include +#include +#include +#include + +#define FX_ERROR_TEMPLATE_PARAMETER_MAX 4 +#define FX_ERROR_MSG_ID_INVALID ((unsigned long)-1) + +#define FX_CATCH(err, expr) ((err = (expr)) != NULL) + +#define fx_result_is_error(result) ((result) != NULL) +#define fx_result_is_success(result) ((result) == NULL) + +#define FX_RESULT_SUCCESS ((fx_result)NULL) +#define FX_RESULT_ERR(err_name) \ + fx_error_with_code(fx_error_vendor_get_builtin(), FX_ERR_##err_name) +#define FX_RESULT_ERR_WITH_STRING(err_name, ...) \ + fx_error_with_string( \ + fx_error_vendor_get_builtin(), \ + FX_ERR_##err_name, \ + __VA_ARGS__) +#define FX_RESULT_STATUS(code) \ + ((code) == FX_SUCCESS \ + ? FX_RESULT_SUCCESS \ + : (fx_error_with_code(fx_error_vendor_get_builtin(), code))) +#define FX_RESULT_STATUS_WITH_STRING(code, ...) \ + ((code) == FX_SUCCESS ? FX_RESULT_SUCCESS \ + : (fx_error_with_string( \ + fx_error_vendor_get_builtin(), \ + code, \ + __VA_ARGS__))) + +#define FX_ERRORS_BUILTIN (fx_error_vendor_get_builtin()) +#define FX_ERRORS_ERRNO (fx_error_vendor_get_errno()) + +#define FX_ERROR_PARAM(name, value) \ + (fx_error_template_parameter) \ + { \ + .param_name = (name), .param_value = (uintptr_t)(value), \ + } + +#define FX_ERROR_TEMPLATE_PARAM(name, type, format) \ + (fx_error_template_parameter_definition) \ + { \ + .param_name = (name), .param_type = (type), \ + .param_format = (format), \ + } +#define FX_ERROR_DEFINITION(code, name, msg) \ + [code] = (fx_error_definition) \ + { \ + .err_name = (name), .err_message = (msg), \ + } +#define FX_ERROR_DEFINITION_TEMPLATE(code, name, msg, ...) \ + [code] = (fx_error_definition) \ + { \ + .err_name = (name), .err_message = (msg), \ + .err_params = __VA_ARGS__, \ + } + +#define FX_ERROR_MSG(id, content) \ + [id] = (fx_error_msg) \ + { \ + .msg_message = (content), \ + } +#define FX_ERROR_MSG_TEMPLATE(id, content, ...) \ + [id] = (fx_error_msg) \ + { \ + .msg_message = (content), .msg_params = __VA_ARGS__, \ + } + +#define z__fx_error_create_status(status_code) \ + (z__fx_error_create( \ + fx_error_vendor_get_builtin(), \ + status_code, \ + NULL, \ + NULL, \ + 0, \ + NULL, \ + NULL)) + +/* Error creation macros */ +#define fx_error_with_code(vendor, code) \ + (z__fx_error_create( \ + vendor, \ + code, \ + NULL, \ + __FILE__, \ + __LINE__, \ + __FUNCTION__, \ + NULL)) +#define fx_error_caused_by_error(vendor, code, cause_error) \ + (z__fx_error_create( \ + vendor, \ + code, \ + cause_error, \ + __FILE__, \ + __LINE__, \ + __FUNCTION__, \ + NULL)) +#define fx_error_caused_by_status(vendor, code, cause_status) \ + (z__fx_error_create( \ + vendor, \ + code, \ + z__fx_error_create_status(cause_status), \ + __FILE__, \ + __LINE__, \ + __FUNCTION__, \ + NULL)) +#define fx_error_caused_by_code(vendor, code, cause_vendor, cause_code) \ + (z__fx_error_create( \ + vendor, \ + code, \ + fx_error_with_code(cause_vendor, cause_code), \ + __FILE__, \ + __LINE__, \ + __FUNCTION__, \ + NULL)) +#define fx_error_with_string(vendor, code, ...) \ + (z__fx_error_create( \ + vendor, \ + code, \ + NULL, \ + __FILE__, \ + __LINE__, \ + __FUNCTION__, \ + __VA_ARGS__)) +#define fx_error_with_string_caused_by_error(vendor, code, cause_error, ...) \ + (z__fx_error_create( \ + vendor, \ + code, \ + cause_error, \ + __FILE__, \ + __LINE__, \ + __FUNCTION__, \ + __VA_ARGS__)) +#define fx_error_with_string_caused_by_status(vendor, code, cause_status, ...) \ + (z__fx_error_create( \ + vendor, \ + code, \ + z__fx_error_create_status(cause_status), \ + __FILE__, \ + __LINE__, \ + __FUNCTION__, \ + __VA_ARGS__)) +#define fx_error_with_msg(vendor, code, msg_id) \ + (z__fx_error_create_msg( \ + vendor, \ + code, \ + NULL, \ + __FILE__, \ + __LINE__, \ + __FUNCTION__, \ + msg_id, \ + (fx_error_template_parameter[]) {{}})) +#define fx_error_with_msg_caused_by_error(vendor, code, cause_error, msg_id) \ + (z__fx_error_create_msg( \ + vendor, \ + code, \ + cause_error, \ + __FILE__, \ + __LINE__, \ + __FUNCTION__, \ + msg_id, \ + (fx_error_template_parameter[]) {{}})) +#define fx_error_with_msg_caused_by_status(vendor, code, cause_status, msg_id) \ + (z__fx_error_create_msg( \ + vendor, \ + code, \ + z__fx_error_create_status(cause_status), \ + __FILE__, \ + __LINE__, \ + __FUNCTION__, \ + msg_id, \ + (fx_error_template_parameter[]) {{}})) +#define fx_error_with_msg_template(vendor, code, msg_id, ...) \ + (z__fx_error_create_msg( \ + vendor, \ + code, \ + NULL, \ + __FILE__, \ + __LINE__, \ + __FUNCTION__, \ + msg_id, \ + (fx_error_template_parameter[]) {__VA_ARGS__, {}})) +#define fx_error_with_msg_template_caused_by_error( \ + vendor, \ + code, \ + cause_error, \ + msg_id, \ + ...) \ + (z__fx_error_create_msg( \ + vendor, \ + code, \ + cause_error, \ + __FILE__, \ + __LINE__, \ + __FUNCTION__, \ + msg_id, \ + (fx_error_template_parameter[]) {__VA_ARGS__, {}})) +#define fx_error_with_msg_template_caused_by_status( \ + vendor, \ + code, \ + cause_status, \ + msg_id, \ + ...) \ + (z__fx_error_create_msg( \ + vendor, \ + code, \ + z__fx_error_create_status(cause_status), \ + __FILE__, \ + __LINE__, \ + __FUNCTION__, \ + msg_id, \ + (fx_error_template_parameter[]) {__VA_ARGS__, {}})) +#define fx_error_with_template(vendor, code, ...) \ + (z__fx_error_create_template( \ + vendor, \ + code, \ + NULL, \ + __FILE__, \ + __LINE__, \ + __FUNCTION__, \ + (fx_error_template_parameter[]) {__VA_ARGS__, {}})) +#define fx_error_with_template_caused_by_error(vendor, code, cause_error, ...) \ + (z__fx_error_create_template( \ + vendor, \ + code, \ + cause_error, \ + __FILE__, \ + __LINE__, \ + __FUNCTION__, \ + (fx_error_template_parameter[]) {__VA_ARGS__, {}})) +#define fx_error_with_template_caused_by_status( \ + vendor, \ + code, \ + cause_status, \ + ...) \ + (z__fx_error_create_template( \ + vendor, \ + code, \ + z__fx_error_create_status(cause_status), \ + __FILE__, \ + __LINE__, \ + __FUNCTION__, \ + (fx_error_template_parameter[]) {__VA_ARGS__, {}})) + +/* Error propagation macros */ +#define fx_result_propagate(err) \ + (z__fx_error_propagate(err, __FILE__, __LINE__, __FUNCTION__)) +#define fx_error_caused_by(err, caused_by) \ + (z__fx_error_caused_by(err, caused_by)) +#define fx_error_caused_by_fx_status(err, status) \ + (z__fx_error_caused_by_fx_status(err, status)) +#define fx_error_replace(err, caused_by) \ + (z__fx_error_propagate(err, __FILE__, __LINE__, __FUNCTION__)) + +/* Error throw macros */ +#define z__fx_throw(err) (z__fx_error_throw(err, NULL, 0, NULL)) +#define fx_throw(err) (z__fx_error_throw(err, __FILE__, __LINE__, __FUNCTION__)) +#define fx_throw_status(status) \ + (z__fx_error_throw( \ + z__fx_error_create( \ + fx_error_vendor_get_builtin(), \ + status, \ + NULL, \ + NULL, \ + 0, \ + NULL, \ + NULL), \ + __FILE__, \ + __LINE__, \ + __FUNCTION__)) + +#define fx_throw_status_string(status, ...) \ + (z__fx_error_throw( \ + z__fx_error_create( \ + fx_error_vendor_get_builtin(), \ + status, \ + NULL, \ + NULL, \ + 0, \ + NULL, \ + __VA_ARGS__), \ + __FILE__, \ + __LINE__, \ + __FUNCTION__)) +#define fx_throw_error_code(vendor, code) \ + z__fx_throw(fx_error_with_code(vendor, code)) +#define fx_throw_error_caused_by_error(vendor, code, cause) \ + z__fx_throw(fx_error_caused_by_error(vendor, code, cause)) +#define fx_throw_error_caused_by_status(vendor, code, cause) \ + z__fx_throw(fx_error_caused_by_status(vendor, code, cause)) +#define fx_throw_error_with_string(vendor, code, ...) \ + z__fx_throw(fx_error_with_string(vendor, code, __VA_ARGS__)) +#define fx_throw_error_with_string_caused_by_error(vendor, code, cause, ...) \ + z__fx_throw(fx_error_with_string_caused_by_error( \ + vendor, \ + code, \ + cause, \ + __VA_ARGS__)) +#define fx_throw_error_with_string_caused_by_status(vendor, code, cause, ...) \ + z__fx_throw(fx_error_with_string_caused_by_status( \ + vendor, \ + code, \ + cause, \ + __VA_ARGS__)) + +#define fx_throw_error_with_msg(vendor, code, msg_id) \ + z__fx_throw(fx_error_with_msg(vendor, code, msg_id)) +#define fx_throw_error_with_msg_caused_by_error(vendor, code, cause, msg_id) \ + z__fx_throw(fx_error_with_msg_caused_by_error( \ + vendor, \ + code, \ + cause, \ + msg_id)) +#define fx_throw_error_with_msg_caused_by_status(vendor, code, cause, msg_id) \ + z__fx_throw(fx_error_with_msg_caused_by_status( \ + vendor, \ + code, \ + cause, \ + msg_id)) + +#define fx_throw_error_with_msg_template(vendor, code, msg_id, ...) \ + z__fx_throw( \ + fx_error_with_msg_template(vendor, code, msg_id, __VA_ARGS__)) +#define fx_throw_error_with_msg_template_caused_by_error( \ + vendor, \ + code, \ + cause, \ + msg_id, \ + ...) \ + z__fx_throw(fx_error_with_msg_template_caused_by_error( \ + vendor, \ + code, \ + cause, \ + msg_id, \ + __VA_ARGS__)) +#define fx_throw_error_with_msg_template_caused_by_status( \ + vendor, \ + code, \ + cause, \ + msg_id, \ + ...) \ + z__fx_throw(fx_error_with_msg_template_caused_by_status( \ + vendor, \ + code, \ + cause, \ + msg_id, \ + __VA_ARGS__)) + +#define fx_throw_error_with_template(vendor, code, ...) \ + z__fx_throw(fx_error_with_template(vendor, code, __VA_ARGS__)) +#define fx_throw_error_with_template_caused_by_error(vendor, code, cause, ...) \ + z__fx_throw(fx_error_with_template_caused_by_error( \ + vendor, \ + code, \ + cause, \ + __VA_ARGS__)) +#define fx_throw_error_with_template_caused_by_status( \ + vendor, \ + code, \ + cause, \ + ...) \ + z__fx_throw(fx_error_with_template_caused_by_status( \ + vendor, \ + code, \ + cause, \ + __VA_ARGS__)) + +#define FX_ERR_MSG(s) \ + { \ + .msg_type = FX_ERROR_MESSAGE_ERROR, \ + .msg_content = (s), \ + } +#define FX_ERR_MSG_WARN(s) \ + { \ + .msg_type = FX_ERROR_MESSAGE_WARN, \ + .msg_content = (s), \ + } +#define FX_ERR_MSG_INFO(s) \ + { \ + .msg_type = FX_ERROR_MESSAGE_INFO, \ + .msg_content = (s), \ + } +#define FX_ERR_MSG_END(s) \ + { \ + .msg_type = FX_ERROR_MESSAGE_NONE, \ + .msg_content = NULL, \ + } + +typedef enum fx_error_submsg_type { + FX_ERROR_SUBMSG_NONE = 0, + FX_ERROR_SUBMSG_ERROR, + FX_ERROR_SUBMSG_WARNING, + FX_ERROR_SUBMSG_INFO, +} fx_error_submsg_type; + +typedef enum fx_error_report_flags { + FX_ERROR_REPORT_NONE = 0, + FX_ERROR_REPORT_STATUS = 0x01u, + FX_ERROR_REPORT_DESCRIPTION = 0x02u, + FX_ERROR_REPORT_SUBMSG = 0x04u, + FX_ERROR_REPORT_STACK_TRACE = 0x08u, + FX_ERROR_REPORT_CAUSE = 0x10u, + + FX_ERROR_REPORT_MINIMAL + = FX_ERROR_REPORT_STATUS | FX_ERROR_REPORT_DESCRIPTION, + FX_ERROR_REPORT_DEFAULT = FX_ERROR_REPORT_MINIMAL + | FX_ERROR_REPORT_SUBMSG + | FX_ERROR_REPORT_CAUSE, + FX_ERROR_REPORT_ALL + = FX_ERROR_REPORT_DEFAULT | FX_ERROR_REPORT_STACK_TRACE, +} fx_error_report_flags; + +typedef enum fx_error_template_parameter_type { + FX_ERROR_TEMPLATE_PARAM_NONE = 0, + FX_ERROR_TEMPLATE_PARAM_STRING, + FX_ERROR_TEMPLATE_PARAM_CHAR, + FX_ERROR_TEMPLATE_PARAM_INT, + FX_ERROR_TEMPLATE_PARAM_UINT, + FX_ERROR_TEMPLATE_PARAM_LONG, + FX_ERROR_TEMPLATE_PARAM_ULONG, + FX_ERROR_TEMPLATE_PARAM_LONGLONG, + FX_ERROR_TEMPLATE_PARAM_ULONGLONG, + FX_ERROR_TEMPLATE_PARAM_SIZE_T, + FX_ERROR_TEMPLATE_PARAM_INTPTR, + FX_ERROR_TEMPLATE_PARAM_UINTPTR, + FX_ERROR_TEMPLATE_PARAM_PTR, +} fx_error_template_parameter_type; + +typedef struct fx_error_template_parameter_definition { + const char *param_name; + fx_error_template_parameter_type param_type; + const char *param_format; +} fx_error_template_parameter_definition; + +typedef struct fx_error_template_parameter { + const char *param_name; + uintptr_t param_value; + const struct fx_error_template_parameter_definition *__param_def; +} fx_error_template_parameter; + +struct fx_error_vendor; + +typedef struct fx_error fx_error; +typedef struct fx_error *fx_result; +typedef struct fx_error_submsg fx_error_submsg; +typedef struct fx_error_stack_frame fx_error_stack_frame; +typedef long fx_error_status_code; +typedef unsigned long fx_error_msg_id; + +typedef struct fx_error_definition { + const char *err_name; + const char *err_message; + const fx_error_template_parameter_definition + err_params[FX_ERROR_TEMPLATE_PARAMETER_MAX]; +} fx_error_definition; + +typedef struct fx_error_msg { + const char *msg_message; + const fx_error_template_parameter_definition + msg_params[FX_ERROR_TEMPLATE_PARAMETER_MAX]; +} fx_error_msg; + +typedef const fx_error_definition *(*fx_error_status_code_get_definition)( + const struct fx_error_vendor *, + fx_error_status_code); +typedef const fx_error_msg *(*fx_error_msg_get_definition)( + const struct fx_error_vendor *, + fx_error_msg_id); +typedef void (*fx_error_report_function)( + const struct fx_error *, + fx_error_report_flags); + +typedef struct fx_error_vendor { + const char *v_name; + + fx_error_status_code_get_definition v_status_get_definition; + fx_error_msg_get_definition v_msg_get_definition; + + const fx_error_definition *v_error_definitions; + size_t v_error_definitions_length; + + const fx_error_msg *v_msg; + size_t v_msg_length; +} fx_error_vendor; + +FX_API fx_error *z__fx_error_create_template( + const fx_error_vendor *, + fx_error_status_code, + fx_error *, + const char *, + unsigned int, + const char *, + const fx_error_template_parameter[]); +FX_API fx_error *z__fx_error_create_string( + const fx_error_vendor *, + fx_error_status_code, + fx_error *, + const char *, + unsigned int, + const char *, + const char *, + va_list); +FX_API fx_error *z__fx_error_create_msg( + const fx_error_vendor *, + fx_error_status_code, + fx_error *, + const char *, + unsigned int, + const char *, + fx_error_msg_id, + const fx_error_template_parameter[]); +FX_API fx_error *z__fx_error_propagate( + fx_error *, + const char *, + unsigned int, + const char *); +FX_API fx_error *z__fx_error_caused_by(fx_error *, fx_error *); +FX_API fx_error *z__fx_error_caused_by_fx_status(fx_error *, fx_status); +FX_API void z__fx_error_throw( + fx_error *, + const char *, + unsigned int, + const char *); + +FX_API bool fx_result_is( + fx_result result, + const fx_error_vendor *vendor, + fx_error_status_code code); + +FX_API const fx_error_vendor *fx_error_vendor_get_builtin(void); +FX_API const fx_error_vendor *fx_error_vendor_get_errno(void); +FX_API const fx_error_definition *fx_error_vendor_get_error_definition( + const fx_error_vendor *vendor, + fx_error_status_code code); +FX_API const char *fx_error_vendor_get_status_code_name( + const fx_error_vendor *vendor, + fx_error_status_code code); +FX_API const char *fx_error_vendor_get_status_code_description( + const fx_error_vendor *vendor, + fx_error_status_code code); +FX_API const fx_error_msg *fx_error_vendor_get_msg( + const fx_error_vendor *vendor, + fx_error_msg_id msg_id); + +static inline fx_error *z__fx_error_create( + const fx_error_vendor *v, + fx_error_status_code c, + fx_error *c2, + const char *f0, + unsigned int l, + const char *f1, + const char *d, + ...) +{ + va_list arg; + va_start(arg, d); + fx_error *err = z__fx_error_create_string(v, c, c2, f0, l, f1, d, arg); + va_end(arg); + return err; +} + +FX_API enum fx_status fx_error_add_submsg_string( + fx_error *error, + fx_error_submsg_type type, + const char *msg, + ...); +FX_API enum fx_status z__fx_error_add_submsg_template( + fx_error *error, + fx_error_submsg_type type, + fx_error_msg_id msg_id, + fx_error_template_parameter param[]); +#define fx_error_add_submsg(error, type, msg_id) \ + (z__fx_error_add_submsg_template( \ + error, \ + type, \ + msg_id, \ + (fx_error_template_parameter[]) {{}})) +#define fx_error_add_submsg_template(error, type, msg_id, ...) \ + (z__fx_error_add_submsg_template( \ + error, \ + type, \ + msg_id, \ + (fx_error_template_parameter[]) {__VA_ARGS__, {}})) + +FX_API void fx_error_discard(fx_error *error); + +FX_API fx_error_status_code fx_error_get_status_code(const fx_error *error); +FX_API const fx_error_vendor *fx_error_get_vendor(const fx_error *error); +FX_API const fx_error_definition *fx_error_get_definition( + const fx_error *error); +FX_API const fx_error_template_parameter *fx_error_get_template_parameter( + const fx_error *error, + const char *param_name); +FX_API const fx_error_template_parameter *fx_error_get_template_parameters( + const fx_error *error); +FX_API const char *fx_error_get_description(const fx_error *error); +FX_API const fx_error_msg *fx_error_get_msg(const fx_error *error); +FX_API const fx_error_submsg *fx_error_get_first_submsg(const fx_error *error); +FX_API const fx_error_submsg *fx_error_get_next_submsg( + const fx_error *error, + const fx_error_submsg *msg); +FX_API const fx_error_stack_frame *fx_error_get_first_stack_frame( + const fx_error *error); +FX_API const fx_error_stack_frame *fx_error_get_next_stack_frame( + const fx_error *error, + const fx_error_stack_frame *frame); +FX_API const fx_error *fx_error_get_caused_by(const fx_error *error); + +FX_API fx_error_submsg_type fx_error_submsg_get_type( + const fx_error_submsg *msg); +FX_API const char *fx_error_submsg_get_content(const fx_error_submsg *msg); +FX_API const fx_error_msg *fx_error_submsg_get_msg(const fx_error_submsg *msg); +FX_API const fx_error_template_parameter * +fx_error_submsg_get_template_parameters(const fx_error_submsg *msg); + +FX_API const char *fx_error_stack_frame_get_filepath( + const fx_error_stack_frame *frame); +FX_API unsigned int fx_error_stack_frame_get_line_number( + const fx_error_stack_frame *frame); +FX_API const char *fx_error_stack_frame_get_function_name( + const fx_error_stack_frame *frame); + +FX_API const fx_error_template_parameter_definition * +fx_error_definition_get_template_parameter( + const fx_error_definition *error_def, + const char *param_name); + +FX_API const char *fx_error_msg_get_content(const fx_error_msg *msg); +FX_API const fx_error_template_parameter_definition * +fx_error_msg_get_template_parameter( + const fx_error_msg *msg, + const char *param_name); + +FX_API void fx_set_error_report_function( + fx_error_report_function func, + fx_error_report_flags flags); + +#endif diff --git a/fx/include/fx/core/exception.h b/fx/include/fx/exception.h similarity index 100% rename from fx/include/fx/core/exception.h rename to fx/include/fx/exception.h diff --git a/fx/include/fx/core/hash.h b/fx/include/fx/hash.h similarity index 88% rename from fx/include/fx/core/hash.h rename to fx/include/fx/hash.h index d3d44a6..13687a9 100644 --- a/fx/include/fx/core/hash.h +++ b/fx/include/fx/hash.h @@ -1,8 +1,8 @@ #ifndef FX_CORE_HASH_H_ #define FX_CORE_HASH_H_ -#include -#include +#include +#include #include #include @@ -103,9 +103,12 @@ FX_API uint64_t fx_hash_cstr_ex(const char *s, size_t *len); FX_API fx_status fx_hash_ctx_init(fx_hash_ctx *ctx, fx_hash_function func); FX_API fx_status fx_hash_ctx_reset(fx_hash_ctx *ctx); -FX_API fx_status fx_hash_ctx_update(fx_hash_ctx *ctx, const void *p, size_t len); -FX_API fx_status fx_hash_ctx_update_rope(fx_hash_ctx *ctx, const struct fx_rope *rope); -FX_API fx_status fx_hash_ctx_finish( - fx_hash_ctx *ctx, void *out_digest, size_t out_max); +FX_API fx_status +fx_hash_ctx_update(fx_hash_ctx *ctx, const void *p, size_t len); +FX_API fx_status fx_hash_ctx_update_rope( + fx_hash_ctx *ctx, + const struct fx_rope *rope); +FX_API fx_status +fx_hash_ctx_finish(fx_hash_ctx *ctx, void *out_digest, size_t out_max); #endif diff --git a/fx/include/fx/core/init.h b/fx/include/fx/init.h similarity index 100% rename from fx/include/fx/core/init.h rename to fx/include/fx/init.h diff --git a/fx/include/fx/core/iterator.h b/fx/include/fx/iterator.h similarity index 90% rename from fx/include/fx/core/iterator.h rename to fx/include/fx/iterator.h index a91dd3e..03dc5b0 100644 --- a/fx/include/fx/core/iterator.h +++ b/fx/include/fx/iterator.h @@ -1,33 +1,33 @@ #ifndef FX_CORE_ITERATOR_H_ #define FX_CORE_ITERATOR_H_ -#include -#include -#include +#include +#include +#include #include FX_DECLS_BEGIN; -#define fx_foreach(type, var, iterator) \ - for (type var = (type)fx_iterator_get_value(iterator).v_int; \ - FX_OK(fx_iterator_get_status(iterator)); \ - fx_iterator_move_next(iterator), \ +#define fx_foreach(type, var, iterator) \ + for (type var = (type)fx_iterator_get_value(iterator).v_int; \ + FX_OK(fx_iterator_get_status(iterator)); \ + fx_iterator_move_next(iterator), \ var = (type)fx_iterator_get_value(iterator).v_int) -#define fx_foreach_ptr(type, var, iterator) \ - for (type *var = (type *)fx_iterator_get_value(iterator).v_ptr; \ - FX_OK(fx_iterator_get_status(iterator)); \ - fx_iterator_move_next(iterator), \ +#define fx_foreach_ptr(type, var, iterator) \ + for (type *var = (type *)fx_iterator_get_value(iterator).v_ptr; \ + FX_OK(fx_iterator_get_status(iterator)); \ + fx_iterator_move_next(iterator), \ var = (type *)fx_iterator_get_value(iterator).v_ptr) -#define fx_foreach_c(type, var, iterator) \ - for (type var = (type)fx_iterator_get_cvalue(iterator).v_int; \ - FX_OK(fx_iterator_get_status(iterator)); \ - fx_iterator_move_next(iterator), \ +#define fx_foreach_c(type, var, iterator) \ + for (type var = (type)fx_iterator_get_cvalue(iterator).v_int; \ + FX_OK(fx_iterator_get_status(iterator)); \ + fx_iterator_move_next(iterator), \ var = (type)fx_iterator_get_cvalue(iterator).v_int) -#define fx_foreach_cptr(type, var, iterator) \ +#define fx_foreach_cptr(type, var, iterator) \ for (const type *var \ - = (const type *)fx_iterator_get_cvalue(iterator).v_cptr; \ - FX_OK(fx_iterator_get_status(iterator)); \ - fx_iterator_move_next(iterator), \ + = (const type *)fx_iterator_get_cvalue(iterator).v_cptr; \ + FX_OK(fx_iterator_get_status(iterator)); \ + fx_iterator_move_next(iterator), \ var = (const type *)fx_iterator_get_cvalue(iterator).v_cptr) #define FX_ITERATOR_VALUE_INT(v) ((fx_iterator_value) {.v_int = (v)}) @@ -77,7 +77,9 @@ FX_API fx_iterator *fx_iterator_begin(fx_iterable *it); FX_API const fx_iterator *fx_iterator_cbegin(const fx_iterable *it); FX_API fx_status fx_iterator_get_status(const fx_iterator *it); -FX_API fx_status fx_iterator_set_status(const fx_iterator *it, fx_status status); +FX_API fx_status fx_iterator_set_status( + const fx_iterator *it, + fx_status status); FX_API fx_status fx_iterator_move_next(const fx_iterator *it); FX_API fx_iterator_value fx_iterator_get_value(fx_iterator *it); diff --git a/fx/include/fx/core/macros.h b/fx/include/fx/macros.h similarity index 77% rename from fx/include/fx/core/macros.h rename to fx/include/fx/macros.h index c134597..4a6a706 100644 --- a/fx/include/fx/core/macros.h +++ b/fx/include/fx/macros.h @@ -1,10 +1,10 @@ #ifndef FX_CORE_MACROS_H_ #define FX_CORE_MACROS_H_ -#include -#include -#include -#include +#include +#include +#include +#include #include #define __FX_IFACE_I0(p, x) p##x @@ -12,177 +12,191 @@ /* Type definitions macros (for use in .c source file) */ -#define FX_TYPE_CLASS_DEFINITION_BEGIN(type_name) \ - static void type_name##_class_init(fx_class *p, void *d) \ +#define FX_TYPE_CLASS_DEFINITION_BEGIN(type_name) \ + static void type_name##_class_init(fx_class *p, void *d) \ { #define FX_TYPE_CLASS_DEFINITION_END(type_name) } -#define FX_TYPE_CLASS_INTERFACE_BEGIN(interface_name, interface_id) \ - interface_name##_class *__FX_IFACE_I1(iface, __LINE__) \ - = fx_class_get_interface(p, interface_id); \ - if (!__FX_IFACE_I1(iface, __LINE__)) { \ - fx_throw_error_with_msg_template( \ - FX_ERRORS_BUILTIN, FX_ERR_CLASS_INIT_FAILURE, \ - FX_MSG_CLASS_SPECIFIES_UNKNOWN_INTERFACE, \ - FX_ERROR_PARAM("class_name", fx_class_get_name(p)), \ - FX_ERROR_PARAM("interface_name", #interface_name)); \ +#define FX_TYPE_CLASS_INTERFACE_BEGIN(interface_name, interface_id) \ + interface_name##_class *__FX_IFACE_I1(iface, __LINE__) \ + = fx_class_get_interface(p, interface_id); \ + if (!__FX_IFACE_I1(iface, __LINE__)) { \ + fx_throw_error_with_msg_template( \ + FX_ERRORS_BUILTIN, \ + FX_ERR_CLASS_INIT_FAILURE, \ + FX_MSG_CLASS_SPECIFIES_UNKNOWN_INTERFACE, \ + FX_ERROR_PARAM("class_name", fx_class_get_name(p)), \ + FX_ERROR_PARAM("interface_name", #interface_name)); \ exit(-1); \ } else { \ interface_name##_class *iface = __FX_IFACE_I1(iface, __LINE__); #define FX_TYPE_CLASS_INTERFACE_END(interface_name, interface_id) } #define FX_INTERFACE_ENTRY(slot) iface->slot -#define FX_TYPE_DEFINITION_BEGIN(name) \ - static fx_type_info name##_type_info = {0}; \ - static void name##_class_init(fx_class *, void *); \ +#define FX_TYPE_DEFINITION_BEGIN(name) \ + static fx_type_info name##_type_info = {0}; \ + static void name##_class_init(fx_class *, void *); \ static void name##_type_init(void) \ { \ - fx_type_info *type_info = &name##_type_info; \ + fx_type_info *type_info = &name##_type_info; \ unsigned int nr_vtables = 0; \ type_info->t_name = #name; \ type_info->t_class_init = name##_class_init; -#define FX_TYPE_DEFINITION_END(name) \ - fx_result result = fx_type_register(type_info); \ - if (fx_result_is_error(result)) { \ - fx_throw_error_caused_by_error( \ - FX_ERRORS_BUILTIN, FX_ERR_TYPE_REGISTRATION_FAILURE, \ +#define FX_TYPE_DEFINITION_END(name) \ + fx_result result = fx_type_register(type_info); \ + if (fx_result_is_error(result)) { \ + fx_throw_error_caused_by_error( \ + FX_ERRORS_BUILTIN, \ + FX_ERR_TYPE_REGISTRATION_FAILURE, \ result); \ abort(); \ } \ } \ - fx_type name##_get_type(void) \ + fx_type name##_get_type(void) \ { \ - static fx_once static_type_init = FX_ONCE_INIT; \ + static fx_once static_type_init = FX_ONCE_INIT; \ \ - if (fx_init_once(&static_type_init)) { \ + if (fx_init_once(&static_type_init)) { \ name##_type_init(); \ } \ \ return &name##_type_info.t_id; \ } -#define FX_TYPE_ID(a, b, c, d, e) fx_type_id_init(&type_info->t_id, a, b, c, d, e) -#define FX_TYPE_EXTENDS(parent_id) \ +#define FX_TYPE_ID(a, b, c, d, e) \ + fx_type_id_init(&type_info->t_id, a, b, c, d, e) +#define FX_TYPE_EXTENDS(parent_id) \ fx_type_id_copy(parent_id, &type_info->t_parent_id) -#define FX_TYPE_IMPLEMENTS(interface_id) \ - fx_type_id_copy( \ +#define FX_TYPE_IMPLEMENTS(interface_id) \ + fx_type_id_copy( \ interface_id, \ &type_info->t_interfaces[type_info->t_nr_interfaces++]) -#define FX_TYPE_CLASS(class_struct) \ +#define FX_TYPE_CLASS(class_struct) \ type_info->t_class_size = sizeof(class_struct) #define FX_TYPE_FLAGS(flags) type_info->t_flags = (flags) #define FX_TYPE_INSTANCE_INIT(func) type_info->t_instance_init = (func) #define FX_TYPE_INSTANCE_FINI(func) type_info->t_instance_fini = (func) #if 0 -#define FX_TYPE_VTABLE_BEGIN(vtable_struct, interface_id) \ - vtable_struct __FX_IFACE_I1(iface, __LINE__) = {0}; \ - { \ - vtable_struct *iface = &__FX_IFACE_I1(iface, __LINE__); \ - type_info->t_vtables[nr_vtables].v_vtable = iface; \ - type_info->t_vtables[nr_vtables].v_interface_id = interface_id; \ +#define FX_TYPE_VTABLE_BEGIN(vtable_struct, interface_id) \ + vtable_struct __FX_IFACE_I1(iface, __LINE__) = {0}; \ + { \ + vtable_struct *iface = &__FX_IFACE_I1(iface, __LINE__); \ + type_info->t_vtables[nr_vtables].v_vtable = iface; \ + type_info->t_vtables[nr_vtables].v_interface_id \ + = interface_id; \ nr_vtables++; #define FX_TYPE_VTABLE_END(vtable_struct, interface_id) } #endif -#define FX_TYPE_INSTANCE_PRIVATE(instance_struct) \ +#define FX_TYPE_INSTANCE_PRIVATE(instance_struct) \ type_info->t_instance_private_size = sizeof(instance_struct) -#define FX_TYPE_INSTANCE_PROTECTED(instance_struct) \ +#define FX_TYPE_INSTANCE_PROTECTED(instance_struct) \ type_info->t_instance_protected_size = sizeof(instance_struct) /* Type declaration macros (for use in .h header file) */ -#define __FX_DECLARE_TYPE(name) \ - typedef FX_TYPE_FWDREF(name) name; \ +#define __FX_DECLARE_TYPE(name) \ + typedef FX_TYPE_FWDREF(name) name; \ typedef struct _##name##_class name##_class; -#define FX_DECLARE_TYPE(name) \ - __FX_DECLARE_TYPE(name); \ +#define FX_DECLARE_TYPE(name) \ + __FX_DECLARE_TYPE(name); \ static inline name *name##_ref(name *p) \ { \ - return fx_object_ref(p); \ + return fx_object_ref(p); \ } \ static inline void name##_unref(name *p) \ { \ - fx_object_unref(p); \ + fx_object_unref(p); \ } #define FX_TYPE_CLASS_DECLARATION_BEGIN(name) struct _##name##_class { -#define FX_TYPE_CLASS_DECLARATION_END(name) \ +#define FX_TYPE_CLASS_DECLARATION_END(name) \ } \ ; -#define FX_TYPE_VIRTUAL_METHOD(return_type, method_name) \ +#define FX_TYPE_VIRTUAL_METHOD(return_type, method_name) \ return_type(*method_name) -#define FX_TYPE_DEFAULT_CONSTRUCTOR(type_name, type_id) \ +#define FX_TYPE_DEFAULT_CONSTRUCTOR(type_name, type_id) \ static inline type_name *type_name##_create(void) \ { \ - return fx_object_create(type_id); \ + return fx_object_create(type_id); \ } /* Other macros */ -#define FX_CLASS_DISPATCH_VIRTUAL( \ - type_name, type_id, default_value, func, object, ...) \ +#define FX_CLASS_DISPATCH_VIRTUAL( \ + type_name, \ + type_id, \ + default_value, \ + func, \ + object, \ + ...) \ do { \ type_name##_class *iface \ - = fx_object_get_interface(object, type_id); \ + = fx_object_get_interface(object, type_id); \ if (iface && iface->func) { \ return iface->func(object, __VA_ARGS__); \ } else { \ return default_value; \ } \ } while (0) -#define FX_CLASS_DISPATCH_VIRTUAL_0(type_name, type_id, default_value, func, object) \ - do { \ - type_name##_class *iface \ - = fx_object_get_interface(object, type_id); \ - if (iface && iface->func) { \ - return iface->func(object); \ - } else { \ - return default_value; \ - } \ - } while (0) -#define FX_CLASS_DISPATCH_VIRTUAL_V(type_name, type_id, func, object, ...) \ +#define FX_CLASS_DISPATCH_VIRTUAL_0( \ + type_name, \ + type_id, \ + default_value, \ + func, \ + object) \ do { \ type_name##_class *iface \ - = fx_object_get_interface(object, type_id); \ + = fx_object_get_interface(object, type_id); \ + if (iface && iface->func) { \ + return iface->func(object); \ + } else { \ + return default_value; \ + } \ + } while (0) +#define FX_CLASS_DISPATCH_VIRTUAL_V(type_name, type_id, func, object, ...) \ + do { \ + type_name##_class *iface \ + = fx_object_get_interface(object, type_id); \ if (iface && iface->func) { \ iface->func(object, __VA_ARGS__); \ return; \ } \ } while (0) -#define FX_CLASS_DISPATCH_VIRTUAL_V0(type_name, type_id, func, object) \ +#define FX_CLASS_DISPATCH_VIRTUAL_V0(type_name, type_id, func, object) \ do { \ type_name##_class *iface \ - = fx_object_get_interface(object, type_id); \ + = fx_object_get_interface(object, type_id); \ if (iface && iface->func) { \ iface->func(object); \ return; \ } \ } while (0) -#define FX_CLASS_DISPATCH_STATIC(type_id, func_name, obj, ...) \ +#define FX_CLASS_DISPATCH_STATIC(type_id, func_name, obj, ...) \ do { \ - void *priv = fx_object_get_private(obj, type_id); \ + void *priv = fx_object_get_private(obj, type_id); \ return func_name(priv, __VA_ARGS__); \ } while (0) -#define FX_CLASS_DISPATCH_STATIC_V(type_id, func_name, obj, ...) \ +#define FX_CLASS_DISPATCH_STATIC_V(type_id, func_name, obj, ...) \ do { \ - void *priv = fx_object_get_private(obj, type_id); \ + void *priv = fx_object_get_private(obj, type_id); \ func_name(priv, __VA_ARGS__); \ } while (0) -#define FX_CLASS_DISPATCH_STATIC_0(type_id, func_name, obj) \ +#define FX_CLASS_DISPATCH_STATIC_0(type_id, func_name, obj) \ do { \ - void *priv = fx_object_get_private(obj, type_id); \ + void *priv = fx_object_get_private(obj, type_id); \ return func_name(priv); \ } while (0) -#define FX_CLASS_DISPATCH_STATIC_V0(type_id, func_name, obj) \ +#define FX_CLASS_DISPATCH_STATIC_V0(type_id, func_name, obj) \ do { \ - void *priv = fx_object_get_private(obj, type_id); \ + void *priv = fx_object_get_private(obj, type_id); \ func_name(priv); \ } while (0) diff --git a/fx/include/fx/core/misc.h b/fx/include/fx/misc.h similarity index 100% rename from fx/include/fx/core/misc.h rename to fx/include/fx/misc.h diff --git a/fx/include/fx/core/object.h b/fx/include/fx/object.h similarity index 84% rename from fx/include/fx/core/object.h rename to fx/include/fx/object.h index 6705021..ed4c7e7 100644 --- a/fx/include/fx/core/object.h +++ b/fx/include/fx/object.h @@ -1,8 +1,8 @@ #ifndef FX_CORE_OBJECT_H_ #define FX_CORE_OBJECT_H_ -#include -#include +#include +#include #define FX_OBJECT_MAGIC 0xDECAFC0C0ABEEF13ULL @@ -25,7 +25,10 @@ FX_API void *fx_object_get_private(const fx_object *object, fx_type type); FX_API void *fx_object_get_protected(const fx_object *object, fx_type type); FX_API void *fx_object_get_interface(const fx_object *object, fx_type type); FX_API fx_status fx_object_get_data( - const fx_object *object, fx_type type, void **priv, void **prot, + const fx_object *object, + fx_type type, + void **priv, + void **prot, void **iface); FX_API fx_object *fx_object_ref(fx_object *p); @@ -33,7 +36,9 @@ FX_API void fx_object_unref(fx_object *p); FX_API fx_object *fx_object_make_rvalue(fx_object *p); FX_API fx_object *fx_object_create(fx_type type); -FX_API void fx_object_to_string(const fx_object *p, FX_TYPE_FWDREF(fx_stream) * out); +FX_API void fx_object_to_string( + const fx_object *p, + FX_TYPE_FWDREF(fx_stream) * out); FX_API bool fx_object_is_type(const fx_object *p, fx_type type); #endif diff --git a/fx/include/fx/core/queue.h b/fx/include/fx/queue.h similarity index 78% rename from fx/include/fx/core/queue.h rename to fx/include/fx/queue.h index 2ebbed3..9f02292 100644 --- a/fx/include/fx/core/queue.h +++ b/fx/include/fx/queue.h @@ -1,9 +1,9 @@ #ifndef FX_CORE_QUEUE_H_ #define FX_CORE_QUEUE_H_ -#include -#include -#include +#include +#include +#include #include #include @@ -16,8 +16,9 @@ FX_DECLARE_TYPE(fx_queue_iterator); FX_TYPE_CLASS_DECLARATION_BEGIN(fx_queue_iterator) FX_TYPE_CLASS_DECLARATION_END(fx_queue_iterator) -#define FX_QUEUE_INIT ((fx_queue) {.q_first = NULL, .q_last = NULL}) -#define FX_QUEUE_ENTRY_INIT ((fx_queue_entry) {.qe_next = NULL, .qe_prev = NULL}) +#define FX_QUEUE_INIT ((fx_queue) {.q_first = NULL, .q_last = NULL}) +#define FX_QUEUE_ENTRY_INIT \ + ((fx_queue_entry) {.qe_next = NULL, .qe_prev = NULL}) typedef struct fx_queue_entry { struct fx_queue_entry *qe_next; @@ -60,9 +61,13 @@ FX_API fx_type fx_queue_iterator_get_type(void); FX_API size_t fx_queue_length(const fx_queue *q); FX_API void fx_queue_insert_before( - fx_queue *q, fx_queue_entry *entry, fx_queue_entry *before); + fx_queue *q, + fx_queue_entry *entry, + fx_queue_entry *before); FX_API void fx_queue_insert_after( - fx_queue *q, fx_queue_entry *entry, fx_queue_entry *after); + fx_queue *q, + fx_queue_entry *entry, + fx_queue_entry *after); FX_API void fx_queue_push_front(fx_queue *q, fx_queue_entry *entry); FX_API void fx_queue_push_back(fx_queue *q, fx_queue_entry *entry); @@ -70,7 +75,10 @@ FX_API void fx_queue_push_back(fx_queue *q, fx_queue_entry *entry); FX_API fx_queue_entry *fx_queue_pop_front(fx_queue *q); FX_API fx_queue_entry *fx_queue_pop_back(fx_queue *q); -FX_API void fx_queue_move(fx_queue *q, fx_queue_entry *dest, fx_queue_entry *src); +FX_API void fx_queue_move( + fx_queue *q, + fx_queue_entry *dest, + fx_queue_entry *src); FX_API void fx_queue_delete(fx_queue *q, fx_queue_entry *entry); FX_API void fx_queue_delete_all(fx_queue *q); diff --git a/fx/include/fx/core/random.h b/fx/include/fx/random.h similarity index 89% rename from fx/include/fx/core/random.h rename to fx/include/fx/random.h index 583312e..9383ffa 100644 --- a/fx/include/fx/core/random.h +++ b/fx/include/fx/random.h @@ -1,7 +1,7 @@ #ifndef FX_RANDOM_H_ #define FX_RANDOM_H_ -#include +#include #include struct fx_random_algorithm; @@ -32,6 +32,8 @@ FX_API fx_status fx_random_init(fx_random_ctx *ctx, fx_random_flags flags); FX_API unsigned long long fx_random_next_int64(fx_random_ctx *ctx); FX_API double fx_random_next_double(fx_random_ctx *ctx); FX_API void fx_random_next_bytes( - fx_random_ctx *ctx, unsigned char *out, size_t nbytes); + fx_random_ctx *ctx, + unsigned char *out, + size_t nbytes); #endif diff --git a/fx/include/fx/core/ringbuffer.h b/fx/include/fx/ringbuffer.h similarity index 63% rename from fx/include/fx/core/ringbuffer.h rename to fx/include/fx/ringbuffer.h index 1c6fe49..1826949 100644 --- a/fx/include/fx/core/ringbuffer.h +++ b/fx/include/fx/ringbuffer.h @@ -1,9 +1,9 @@ #ifndef FX_CORE_RINGBUFFER_H_ #define FX_CORE_RINGBUFFER_H_ -#include -#include -#include +#include +#include +#include FX_DECLS_BEGIN; @@ -17,14 +17,19 @@ FX_TYPE_CLASS_DECLARATION_END(fx_ringbuffer) FX_API fx_type fx_ringbuffer_get_type(void); FX_API fx_ringbuffer *fx_ringbuffer_create(size_t capacity); -FX_API fx_ringbuffer *fx_ringbuffer_create_with_buffer(void *ptr, size_t capacity); +FX_API fx_ringbuffer *fx_ringbuffer_create_with_buffer( + void *ptr, + size_t capacity); FX_API fx_status fx_ringbuffer_clear(fx_ringbuffer *buf); -FX_API fx_status fx_ringbuffer_read( - fx_ringbuffer *buf, void *p, size_t count, size_t *nr_read); +FX_API fx_status +fx_ringbuffer_read(fx_ringbuffer *buf, void *p, size_t count, size_t *nr_read); FX_API fx_status fx_ringbuffer_write( - fx_ringbuffer *buf, const void *p, size_t count, size_t *nr_written); + fx_ringbuffer *buf, + const void *p, + size_t count, + size_t *nr_written); FX_API int fx_ringbuffer_getc(fx_ringbuffer *buf); FX_API fx_status fx_ringbuffer_putc(fx_ringbuffer *buf, int c); @@ -33,14 +38,22 @@ FX_API size_t fx_ringbuffer_write_capacity_remaining(const fx_ringbuffer *buf); FX_API size_t fx_ringbuffer_available_data_remaining(const fx_ringbuffer *buf); FX_API fx_status fx_ringbuffer_open_read_buffer( - fx_ringbuffer *buf, const void **ptr, size_t *length); + fx_ringbuffer *buf, + const void **ptr, + size_t *length); FX_API fx_status fx_ringbuffer_close_read_buffer( - fx_ringbuffer *buf, const void **ptr, size_t nr_read); + fx_ringbuffer *buf, + const void **ptr, + size_t nr_read); FX_API fx_status fx_ringbuffer_open_write_buffer( - fx_ringbuffer *buf, void **ptr, size_t *capacity); + fx_ringbuffer *buf, + void **ptr, + size_t *capacity); FX_API fx_status fx_ringbuffer_close_write_buffer( - fx_ringbuffer *buf, void **ptr, size_t nr_written); + fx_ringbuffer *buf, + void **ptr, + size_t nr_written); FX_DECLS_END; diff --git a/fx/include/fx/core/rope.h b/fx/include/fx/rope.h similarity index 89% rename from fx/include/fx/core/rope.h rename to fx/include/fx/rope.h index 387fbd5..ecc972a 100644 --- a/fx/include/fx/core/rope.h +++ b/fx/include/fx/rope.h @@ -1,9 +1,9 @@ #ifndef FX_CORE_ROPE_H_ #define FX_CORE_ROPE_H_ -#include -#include -#include +#include +#include +#include #include #include @@ -12,14 +12,14 @@ struct fx_bstr; #define FX_ROPE_TYPE(f) ((f) & 0xFF) -#define FX_ROPE_CHAR(c) \ +#define FX_ROPE_CHAR(c) \ \ { \ - .r_flags = FX_ROPE_F_CHAR, .r_len_total = 1, \ + .r_flags = FX_ROPE_F_CHAR, .r_len_total = 1, \ .r_v = {.v_char = (c) } \ } -#define FX_ROPE_CSTR(str) \ +#define FX_ROPE_CSTR(str) \ { \ .r_flags = FX_ROPE_F_CSTR_BORROWED, \ .r_len_total = strlen(str), \ @@ -31,7 +31,7 @@ struct fx_bstr; }, \ } -#define FX_ROPE_CSTR_STATIC(str) \ +#define FX_ROPE_CSTR_STATIC(str) \ { \ .r_flags = FX_ROPE_F_CSTR_STATIC, \ .r_len_total = strlen(str), \ @@ -42,16 +42,16 @@ struct fx_bstr; }, \ }, \ } -#define FX_ROPE_INT(v) \ +#define FX_ROPE_INT(v) \ \ { \ - .r_flags = FX_ROPE_F_INT, .r_len_total = fx_int_length(v), \ + .r_flags = FX_ROPE_F_INT, .r_len_total = fx_int_length(v), \ .r_v = {.v_int = (v) } \ } -#define FX_ROPE_UINT(v) \ +#define FX_ROPE_UINT(v) \ \ { \ - .r_flags = FX_ROPE_F_UINT, .r_len_total = fx_uint_length(v), \ + .r_flags = FX_ROPE_F_UINT, .r_len_total = fx_uint_length(v), \ .r_v = {.v_uint = (v) } \ } @@ -97,10 +97,18 @@ FX_API void fx_rope_init_uint(fx_rope *rope, uintptr_t v); FX_API void fx_rope_destroy(fx_rope *rope); FX_API void fx_rope_iterate( - const fx_rope *rope, void (*func)(const fx_rope *, void *), void *arg); + const fx_rope *rope, + void (*func)(const fx_rope *, void *), + void *arg); FX_API size_t fx_rope_get_size(const fx_rope *rope); -FX_API void fx_rope_concat(fx_rope *result, const fx_rope *left, const fx_rope *right); -FX_API void fx_rope_join(fx_rope *result, const fx_rope **ropes, size_t nr_ropes); +FX_API void fx_rope_concat( + fx_rope *result, + const fx_rope *left, + const fx_rope *right); +FX_API void fx_rope_join( + fx_rope *result, + const fx_rope **ropes, + size_t nr_ropes); FX_API fx_status fx_rope_to_cstr(const fx_rope *rope, char *out, size_t max); FX_API fx_status fx_rope_to_bstr(const fx_rope *rope, struct fx_bstr *str); diff --git a/fx/include/fx/core/status.h b/fx/include/fx/status.h similarity index 97% rename from fx/include/fx/core/status.h rename to fx/include/fx/status.h index b5368c2..5600bec 100644 --- a/fx/include/fx/core/status.h +++ b/fx/include/fx/status.h @@ -1,7 +1,7 @@ #ifndef FX_CORE_STATUS_H_ #define FX_CORE_STATUS_H_ -#include +#include #define FX_OK(status) ((enum fx_status)((uintptr_t)(status)) == FX_SUCCESS) #define FX_ERR(status) ((status) != FX_SUCCESS) diff --git a/fx/include/fx/core/stream.h b/fx/include/fx/stream.h similarity index 79% rename from fx/include/fx/core/stream.h rename to fx/include/fx/stream.h index afb0fc5..15a00a2 100644 --- a/fx/include/fx/core/stream.h +++ b/fx/include/fx/stream.h @@ -1,10 +1,10 @@ #ifndef FX_CORE_STREAM_H_ #define FX_CORE_STREAM_H_ -#include -#include -#include -#include +#include +#include +#include +#include #include #include @@ -64,7 +64,9 @@ FX_API fx_stream *fx_stream_open_fp(FILE *fp); FX_API fx_status fx_stream_reserve(fx_stream *stream, size_t len); FX_API fx_status fx_stream_seek( - fx_stream *stream, long long offset, fx_stream_seek_origin origin); + fx_stream *stream, + long long offset, + fx_stream_seek_origin origin); FX_API size_t fx_stream_cursor(const fx_stream *stream); FX_API fx_status fx_stream_push_indent(fx_stream *stream, int indent); @@ -73,27 +75,45 @@ FX_API fx_status fx_stream_pop_indent(fx_stream *stream); FX_API fx_status fx_stream_read_char(fx_stream *stream, fx_wchar *c); FX_API fx_status fx_stream_read_bytes( - fx_stream *stream, void *buf, size_t count, size_t *nr_read); + fx_stream *stream, + void *buf, + size_t count, + size_t *nr_read); FX_API fx_status fx_stream_read_line(fx_stream *stream, char *s, size_t max); FX_API fx_status fx_stream_read_line_s(fx_stream *src, fx_stream *dest); FX_API fx_status fx_stream_read_all_bytes( - fx_stream *stream, void *p, size_t max, size_t *nr_read); + fx_stream *stream, + void *p, + size_t max, + size_t *nr_read); FX_API fx_status fx_stream_read_all_bytes_s( - fx_stream *src, fx_stream *dest, fx_stream_buffer *buffer, size_t *nr_read); + fx_stream *src, + fx_stream *dest, + fx_stream_buffer *buffer, + size_t *nr_read); FX_API fx_status fx_stream_write_char(fx_stream *stream, fx_wchar c); -FX_API fx_status fx_stream_write_cstr( - fx_stream *stream, const char *s, size_t *nr_written); +FX_API fx_status +fx_stream_write_cstr(fx_stream *stream, const char *s, size_t *nr_written); FX_API fx_status fx_stream_write_bytes( - fx_stream *stream, const void *buf, size_t count, size_t *nr_written); + fx_stream *stream, + const void *buf, + size_t count, + size_t *nr_written); FX_API fx_status fx_stream_write_fmt( - fx_stream *stream, size_t *nr_written, const char *format, ...); + fx_stream *stream, + size_t *nr_written, + const char *format, + ...); FX_API fx_status fx_stream_write_vfmt( - fx_stream *stream, size_t *nr_written, const char *format, va_list arg); + fx_stream *stream, + size_t *nr_written, + const char *format, + va_list arg); FX_DECLS_END; diff --git a/fx/include/fx/core/stringstream.h b/fx/include/fx/stringstream.h similarity index 70% rename from fx/include/fx/core/stringstream.h rename to fx/include/fx/stringstream.h index bb66843..a7da8de 100644 --- a/fx/include/fx/core/stringstream.h +++ b/fx/include/fx/stringstream.h @@ -1,10 +1,10 @@ #ifndef FX_CORE_STRINGSTREAM_H_ #define FX_CORE_STRINGSTREAM_H_ -#include -#include -#include -#include +#include +#include +#include +#include #include FX_DECLS_BEGIN; @@ -19,11 +19,13 @@ FX_TYPE_CLASS_DECLARATION_END(fx_stringstream) FX_API fx_type fx_stringstream_get_type(void); FX_API fx_stringstream *fx_stringstream_create(void); -FX_API fx_stringstream *fx_stringstream_create_with_buffer(char *buf, size_t max); +FX_API fx_stringstream *fx_stringstream_create_with_buffer( + char *buf, + size_t max); FX_API fx_status fx_stringstream_reset(fx_stringstream *strv); -FX_API fx_status fx_stringstream_reset_with_buffer( - fx_stringstream *strv, char *buf, size_t max); +FX_API fx_status +fx_stringstream_reset_with_buffer(fx_stringstream *strv, char *buf, size_t max); FX_API const char *fx_stringstream_ptr(const fx_stringstream *strv); FX_API char *fx_stringstream_steal(fx_stringstream *strv); diff --git a/fx/include/fx/core/thread.h b/fx/include/fx/thread.h similarity index 92% rename from fx/include/fx/core/thread.h rename to fx/include/fx/thread.h index 54dbee8..1a87164 100644 --- a/fx/include/fx/core/thread.h +++ b/fx/include/fx/thread.h @@ -1,8 +1,8 @@ #ifndef FX_CORE_THREAD_H_ #define FX_CORE_THREAD_H_ -#include -#include +#include +#include #include #if defined(__APPLE__) || defined(__linux__) || defined(__rosetta__) diff --git a/fx/include/fx/core/type.h b/fx/include/fx/type.h similarity index 91% rename from fx/include/fx/core/type.h rename to fx/include/fx/type.h index b33efc2..05b8e67 100644 --- a/fx/include/fx/core/type.h +++ b/fx/include/fx/type.h @@ -1,8 +1,8 @@ #ifndef FX_CORE_TYPE_H_ #define FX_CORE_TYPE_H_ -#include -#include +#include +#include #include #include #include @@ -44,7 +44,11 @@ typedef struct fx_type_info { } fx_type_info; FX_API void fx_type_id_init( - union fx_type *out, uint32_t a, uint16_t b, uint16_t c, uint16_t d, + union fx_type *out, + uint32_t a, + uint16_t b, + uint16_t c, + uint16_t d, uint64_t e); static inline void fx_type_id_copy(fx_type src, union fx_type *dest) { diff --git a/fx/iterator.c b/fx/iterator.c index 2282694..90ab6e8 100644 --- a/fx/iterator.c +++ b/fx/iterator.c @@ -1,4 +1,4 @@ -#include +#include /*** PRIVATE DATA *************************************************************/ @@ -13,7 +13,9 @@ static enum fx_status iterator_get_status(const struct fx_iterator_p *it) return it->it_status; } -static enum fx_status iterator_set_status(struct fx_iterator_p *it, fx_status status) +static enum fx_status iterator_set_status( + struct fx_iterator_p *it, + fx_status status) { it->it_status = status; return FX_SUCCESS; @@ -23,12 +25,22 @@ static enum fx_status iterator_set_status(struct fx_iterator_p *it, fx_status st fx_iterator *fx_iterator_begin(fx_iterable *it) { - FX_CLASS_DISPATCH_VIRTUAL_0(fx_iterable, FX_TYPE_ITERABLE, NULL, it_begin, it); + FX_CLASS_DISPATCH_VIRTUAL_0( + fx_iterable, + FX_TYPE_ITERABLE, + NULL, + it_begin, + it); } const fx_iterator *fx_iterator_cbegin(const fx_iterable *it) { - FX_CLASS_DISPATCH_VIRTUAL_0(fx_iterable, FX_TYPE_ITERABLE, NULL, it_cbegin, it); + FX_CLASS_DISPATCH_VIRTUAL_0( + fx_iterable, + FX_TYPE_ITERABLE, + NULL, + it_cbegin, + it); } enum fx_status fx_iterator_get_status(const fx_iterator *it) @@ -38,14 +50,19 @@ enum fx_status fx_iterator_get_status(const fx_iterator *it) enum fx_status fx_iterator_set_status(const fx_iterator *it, fx_status status) { - FX_CLASS_DISPATCH_STATIC(FX_TYPE_ITERATOR, iterator_set_status, it, status); + FX_CLASS_DISPATCH_STATIC( + FX_TYPE_ITERATOR, + iterator_set_status, + it, + status); } enum fx_status fx_iterator_move_next(const fx_iterator *it) { enum fx_status status = FX_ERR_NOT_SUPPORTED; - fx_iterator_class *iface = fx_object_get_interface(it, FX_TYPE_ITERATOR); + fx_iterator_class *iface + = fx_object_get_interface(it, FX_TYPE_ITERATOR); if (iface && iface->it_move_next) { status = iface->it_move_next(it); } @@ -59,22 +76,29 @@ enum fx_status fx_iterator_move_next(const fx_iterator *it) fx_iterator_value fx_iterator_get_value(fx_iterator *it) { FX_CLASS_DISPATCH_VIRTUAL_0( - fx_iterator, FX_TYPE_ITERATOR, FX_ITERATOR_VALUE_NULL, - it_get_value, it); + fx_iterator, + FX_TYPE_ITERATOR, + FX_ITERATOR_VALUE_NULL, + it_get_value, + it); } const fx_iterator_value fx_iterator_get_cvalue(const fx_iterator *it) { FX_CLASS_DISPATCH_VIRTUAL_0( - fx_iterator, FX_TYPE_ITERATOR, FX_ITERATOR_VALUE_NULL, - it_get_cvalue, it); + fx_iterator, + FX_TYPE_ITERATOR, + FX_ITERATOR_VALUE_NULL, + it_get_cvalue, + it); } fx_status fx_iterator_erase(fx_iterator *it) { enum fx_status status = FX_ERR_NOT_SUPPORTED; - fx_iterator_class *iface = fx_object_get_interface(it, FX_TYPE_ITERATOR); + fx_iterator_class *iface + = fx_object_get_interface(it, FX_TYPE_ITERATOR); if (iface && iface->it_erase) { status = iface->it_erase(it); } diff --git a/fx/misc.c b/fx/misc.c index 9770067..e433f76 100644 --- a/fx/misc.c +++ b/fx/misc.c @@ -1,4 +1,4 @@ -#include +#include #define LENGTH_RET(v, boundary, len) \ if ((v) < (boundary)) \ diff --git a/fx/mt19937.c b/fx/mt19937.c index d7de1a1..8cbaaf6 100644 --- a/fx/mt19937.c +++ b/fx/mt19937.c @@ -29,8 +29,8 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER + OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF @@ -55,7 +55,7 @@ #include "random.h" -#include +#include #define NN 312 #define MM 156 @@ -64,14 +64,17 @@ #define LM 0x7FFFFFFFULL /* Least significant 31 bits */ /* initializes mt[NN] with a seed */ -static void init_genrand64(struct fx_random_ctx *context, unsigned long long seed) +static void init_genrand64( + struct fx_random_ctx *context, + unsigned long long seed) { context->__mt19937.mt[0] = seed; for (context->__mt19937.mti = 1; context->__mt19937.mti < NN; context->__mt19937.mti++) context->__mt19937.mt[context->__mt19937.mti] = (6364136223846793005ULL - * (context->__mt19937.mt[context->__mt19937.mti - 1] + * (context->__mt19937 + .mt[context->__mt19937.mti - 1] ^ (context->__mt19937 .mt[context->__mt19937.mti - 1] >> 62)) @@ -82,7 +85,8 @@ static void init_genrand64(struct fx_random_ctx *context, unsigned long long see /* init_key is the array for initializing keys */ /* key_length is its length */ static void init_by_array64( - struct fx_random_ctx *context, unsigned long long init_key[], + struct fx_random_ctx *context, + unsigned long long init_key[], unsigned long long key_length) { unsigned long long i, j, k; @@ -100,7 +104,8 @@ static void init_by_array64( i++; j++; if (i >= NN) { - context->__mt19937.mt[0] = context->__mt19937.mt[NN - 1]; + context->__mt19937.mt[0] + = context->__mt19937.mt[NN - 1]; i = 1; } if (j >= key_length) @@ -115,7 +120,8 @@ static void init_by_array64( - i; /* non linear */ i++; if (i >= NN) { - context->__mt19937.mt[0] = context->__mt19937.mt[NN - 1]; + context->__mt19937.mt[0] + = context->__mt19937.mt[NN - 1]; i = 1; } } @@ -185,12 +191,14 @@ static uint64_t genrand64_int64(struct fx_random_ctx *context) | (context->__mt19937.mt[i + 1] & LM); context->__mt19937.mt[i] = context->__mt19937.mt[i + mid] ^ (x >> 1) - ^ ((context->__mt19937.mt[i + 1] & 1) * MATRIX_A); + ^ ((context->__mt19937.mt[i + 1] & 1) + * MATRIX_A); y = (context->__mt19937.mt[j] & UM) | (context->__mt19937.mt[j + 1] & LM); context->__mt19937.mt[j] = context->__mt19937.mt[j - mid] ^ (y >> 1) - ^ ((context->__mt19937.mt[j + 1] & 1) * MATRIX_A); + ^ ((context->__mt19937.mt[j + 1] & 1) + * MATRIX_A); } x = (context->__mt19937.mt[mid - 1] & UM) | (stateMid & LM); context->__mt19937.mt[mid - 1] = context->__mt19937.mt[NN - 1] diff --git a/fx/object.c b/fx/object.c index 7516796..11514cc 100644 --- a/fx/object.c +++ b/fx/object.c @@ -3,11 +3,11 @@ #include "type.h" #include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include FX_TYPE_CLASS_DEFINITION_BEGIN(fx_object) FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) @@ -21,7 +21,8 @@ FX_TYPE_DEFINITION_BEGIN(fx_object) FX_TYPE_DEFINITION_END(fx_object) fx_result fx_object_instantiate( - struct fx_type_registration *type, struct _fx_object **out_object) + struct fx_type_registration *type, + struct _fx_object **out_object) { struct _fx_object *out = malloc(type->r_instance_size); if (!out) { @@ -77,8 +78,18 @@ struct _fx_object *fx_object_create(fx_type type) void fx_object_to_string(const struct _fx_object *p, fx_stream *out) { - FX_CLASS_DISPATCH_VIRTUAL_V(fx_object, FX_TYPE_OBJECT, to_string, p, out); - fx_stream_write_fmt(out, NULL, "<%s@%p>", p->obj_type->r_info->t_name, p); + FX_CLASS_DISPATCH_VIRTUAL_V( + fx_object, + FX_TYPE_OBJECT, + to_string, + p, + out); + fx_stream_write_fmt( + out, + NULL, + "<%s@%p>", + p->obj_type->r_info->t_name, + p); } bool fx_object_is_type(const struct _fx_object *p, fx_type type) @@ -143,7 +154,10 @@ void *fx_object_get_interface(const struct _fx_object *object, fx_type type) } enum fx_status fx_object_get_data( - const struct _fx_object *object, fx_type type, void **priv, void **prot, + const struct _fx_object *object, + fx_type type, + void **priv, + void **prot, void **iface) { if (!object) { diff --git a/fx/object.h b/fx/object.h index 622362b..b1285e8 100644 --- a/fx/object.h +++ b/fx/object.h @@ -1,8 +1,8 @@ #ifndef _OBJECT_H_ #define _OBJECT_H_ -#include -#include +#include +#include #include struct fx_type_registration; diff --git a/fx/queue.c b/fx/queue.c index 15b950f..76dbd2e 100644 --- a/fx/queue.c +++ b/fx/queue.c @@ -1,4 +1,4 @@ -#include +#include struct fx_queue_iterator_p { size_t i; @@ -19,7 +19,9 @@ size_t fx_queue_length(const struct fx_queue *q) } void fx_queue_insert_before( - struct fx_queue *q, struct fx_queue_entry *entry, struct fx_queue_entry *before) + struct fx_queue *q, + struct fx_queue_entry *entry, + struct fx_queue_entry *before) { struct fx_queue_entry *x = before->qe_prev; if (x) { @@ -35,7 +37,9 @@ void fx_queue_insert_before( } void fx_queue_insert_after( - struct fx_queue *q, struct fx_queue_entry *entry, struct fx_queue_entry *after) + struct fx_queue *q, + struct fx_queue_entry *entry, + struct fx_queue_entry *after) { struct fx_queue_entry *x = after->qe_next; if (x) { diff --git a/fx/random.c b/fx/random.c index 9490238..56fb2f5 100644 --- a/fx/random.c +++ b/fx/random.c @@ -1,6 +1,6 @@ #include "random.h" -#include +#include #define GET_ALGORITHM_ID(flags) ((flags) & 0xFF) @@ -73,7 +73,9 @@ double fx_random_next_double(struct fx_random_ctx *ctx) #endif void fx_random_next_bytes( - struct fx_random_ctx *ctx, unsigned char *out, size_t nbytes) + struct fx_random_ctx *ctx, + unsigned char *out, + size_t nbytes) { size_t n_qwords = 0; n_qwords = nbytes >> 3; diff --git a/fx/random.h b/fx/random.h index fe6174e..eba9bd6 100644 --- a/fx/random.h +++ b/fx/random.h @@ -1,15 +1,15 @@ #ifndef _FX_RANDOM_H_ #define _FX_RANDOM_H_ +#include #include -#include struct fx_random_ctx; struct fx_random_algorithm { const char *gen_name; - void(*gen_init)(struct fx_random_ctx *, unsigned long long seed); - uint64_t(*gen_getrand)(struct fx_random_ctx *); + void (*gen_init)(struct fx_random_ctx *, unsigned long long seed); + uint64_t (*gen_getrand)(struct fx_random_ctx *); }; FX_API uint64_t z__fx_platform_random_seed(void); diff --git a/fx/ringbuffer.c b/fx/ringbuffer.c index 714c2cf..d060809 100644 --- a/fx/ringbuffer.c +++ b/fx/ringbuffer.c @@ -1,4 +1,4 @@ -#include +#include #include #include @@ -34,7 +34,8 @@ static enum fx_status ringbuffer_clear(struct fx_ringbuffer_p *buf) return FX_SUCCESS; } -static size_t ringbuffer_write_capacity_remaining(const struct fx_ringbuffer_p *buf) +static size_t ringbuffer_write_capacity_remaining( + const struct fx_ringbuffer_p *buf) { if (buf->r_read_ptr > buf->r_write_ptr) { return buf->r_read_ptr - buf->r_write_ptr - 1; @@ -43,7 +44,8 @@ static size_t ringbuffer_write_capacity_remaining(const struct fx_ringbuffer_p * } } -static size_t ringbuffer_available_data_remaining(const struct fx_ringbuffer_p *buf) +static size_t ringbuffer_available_data_remaining( + const struct fx_ringbuffer_p *buf) { if (buf->r_read_ptr < buf->r_write_ptr) { return buf->r_write_ptr - buf->r_read_ptr; @@ -55,7 +57,9 @@ static size_t ringbuffer_available_data_remaining(const struct fx_ringbuffer_p * } static enum fx_status ringbuffer_open_read_buffer( - struct fx_ringbuffer_p *buf, const void **ptr, size_t *length) + struct fx_ringbuffer_p *buf, + const void **ptr, + size_t *length) { if (BUF_LOCKED(buf)) { return FX_ERR_BUSY; @@ -83,7 +87,9 @@ static enum fx_status ringbuffer_open_read_buffer( } static enum fx_status ringbuffer_close_read_buffer( - struct fx_ringbuffer_p *buf, const void **ptr, size_t nr_read) + struct fx_ringbuffer_p *buf, + const void **ptr, + size_t nr_read) { if (!(buf->r_flags & RINGBUFFER_READ_LOCKED)) { return FX_ERR_BAD_STATE; @@ -118,7 +124,9 @@ static enum fx_status ringbuffer_close_read_buffer( } static enum fx_status ringbuffer_open_write_buffer( - struct fx_ringbuffer_p *buf, void **ptr, size_t *capacity) + struct fx_ringbuffer_p *buf, + void **ptr, + size_t *capacity) { if (BUF_LOCKED(buf)) { return FX_ERR_BUSY; @@ -150,7 +158,9 @@ static enum fx_status ringbuffer_open_write_buffer( } static enum fx_status ringbuffer_close_write_buffer( - struct fx_ringbuffer_p *buf, void **ptr, size_t nr_written) + struct fx_ringbuffer_p *buf, + void **ptr, + size_t nr_written) { if (!(buf->r_flags & RINGBUFFER_WRITE_LOCKED)) { return FX_ERR_BAD_STATE; @@ -177,7 +187,10 @@ static enum fx_status ringbuffer_close_write_buffer( } static enum fx_status ringbuffer_read( - struct fx_ringbuffer_p *buf, void *p, size_t count, size_t *nr_read) + struct fx_ringbuffer_p *buf, + void *p, + size_t count, + size_t *nr_read) { if (BUF_LOCKED(buf)) { return FX_ERR_BUSY; @@ -220,7 +233,10 @@ static enum fx_status ringbuffer_read( } static enum fx_status ringbuffer_write( - struct fx_ringbuffer_p *buf, const void *p, size_t count, size_t *nr_written) + struct fx_ringbuffer_p *buf, + const void *p, + size_t count, + size_t *nr_written) { if (BUF_LOCKED(buf)) { return FX_ERR_BUSY; @@ -344,17 +360,33 @@ enum fx_status fx_ringbuffer_clear(fx_ringbuffer *buf) } enum fx_status fx_ringbuffer_read( - fx_ringbuffer *buf, void *p, size_t count, size_t *nr_read) + fx_ringbuffer *buf, + void *p, + size_t count, + size_t *nr_read) { FX_CLASS_DISPATCH_STATIC( - FX_TYPE_RINGBUFFER, ringbuffer_read, buf, p, count, nr_read); + FX_TYPE_RINGBUFFER, + ringbuffer_read, + buf, + p, + count, + nr_read); } enum fx_status fx_ringbuffer_write( - fx_ringbuffer *buf, const void *p, size_t count, size_t *nr_written) + fx_ringbuffer *buf, + const void *p, + size_t count, + size_t *nr_written) { FX_CLASS_DISPATCH_STATIC( - FX_TYPE_RINGBUFFER, ringbuffer_write, buf, p, count, nr_written); + FX_TYPE_RINGBUFFER, + ringbuffer_write, + buf, + p, + count, + nr_written); } int fx_ringbuffer_getc(fx_ringbuffer *buf) @@ -370,42 +402,68 @@ enum fx_status fx_ringbuffer_putc(fx_ringbuffer *buf, int c) size_t fx_ringbuffer_write_capacity_remaining(const fx_ringbuffer *buf) { FX_CLASS_DISPATCH_STATIC_0( - FX_TYPE_RINGBUFFER, ringbuffer_write_capacity_remaining, buf); + FX_TYPE_RINGBUFFER, + ringbuffer_write_capacity_remaining, + buf); } size_t fx_ringbuffer_available_data_remaining(const fx_ringbuffer *buf) { FX_CLASS_DISPATCH_STATIC_0( - FX_TYPE_RINGBUFFER, ringbuffer_available_data_remaining, buf); + FX_TYPE_RINGBUFFER, + ringbuffer_available_data_remaining, + buf); } enum fx_status fx_ringbuffer_open_read_buffer( - fx_ringbuffer *buf, const void **ptr, size_t *length) + fx_ringbuffer *buf, + const void **ptr, + size_t *length) { FX_CLASS_DISPATCH_STATIC( - FX_TYPE_RINGBUFFER, ringbuffer_open_read_buffer, buf, ptr, length); + FX_TYPE_RINGBUFFER, + ringbuffer_open_read_buffer, + buf, + ptr, + length); } enum fx_status fx_ringbuffer_close_read_buffer( - fx_ringbuffer *buf, const void **ptr, size_t nr_read) + fx_ringbuffer *buf, + const void **ptr, + size_t nr_read) { FX_CLASS_DISPATCH_STATIC( - FX_TYPE_RINGBUFFER, ringbuffer_close_read_buffer, buf, ptr, nr_read); + FX_TYPE_RINGBUFFER, + ringbuffer_close_read_buffer, + buf, + ptr, + nr_read); } enum fx_status fx_ringbuffer_open_write_buffer( - fx_ringbuffer *buf, void **ptr, size_t *capacity) + fx_ringbuffer *buf, + void **ptr, + size_t *capacity) { FX_CLASS_DISPATCH_STATIC( - FX_TYPE_RINGBUFFER, ringbuffer_open_write_buffer, buf, ptr, + FX_TYPE_RINGBUFFER, + ringbuffer_open_write_buffer, + buf, + ptr, capacity); } enum fx_status fx_ringbuffer_close_write_buffer( - fx_ringbuffer *buf, void **ptr, size_t nr_written) + fx_ringbuffer *buf, + void **ptr, + size_t nr_written) { FX_CLASS_DISPATCH_STATIC( - FX_TYPE_RINGBUFFER, ringbuffer_close_write_buffer, buf, ptr, + FX_TYPE_RINGBUFFER, + ringbuffer_close_write_buffer, + buf, + ptr, nr_written); } diff --git a/fx/rope.c b/fx/rope.c index 536e55d..8b596cd 100644 --- a/fx/rope.c +++ b/fx/rope.c @@ -1,5 +1,5 @@ -#include -#include +#include +#include #include #include #include @@ -76,7 +76,8 @@ void fx_rope_destroy(struct fx_rope *rope) break; case FX_ROPE_F_COMPOSITE: fx_rope_destroy((struct fx_rope *)rope->r_v.v_composite.r_left); - fx_rope_destroy((struct fx_rope *)rope->r_v.v_composite.r_right); + fx_rope_destroy( + (struct fx_rope *)rope->r_v.v_composite.r_right); break; default: break; @@ -88,7 +89,9 @@ void fx_rope_destroy(struct fx_rope *rope) } void fx_rope_iterate( - const struct fx_rope *rope, void (*func)(const fx_rope *, void *), void *arg) + const struct fx_rope *rope, + void (*func)(const fx_rope *, void *), + void *arg) { if (FX_ROPE_TYPE(rope->r_flags) != FX_ROPE_F_COMPOSITE) { func(rope, arg); @@ -110,7 +113,9 @@ size_t fx_rope_get_size(const struct fx_rope *rope) } void fx_rope_concat( - struct fx_rope *result, const struct fx_rope *left, const struct fx_rope *right) + struct fx_rope *result, + const struct fx_rope *left, + const struct fx_rope *right) { memset(result, 0x0, sizeof *result); @@ -177,7 +182,8 @@ void fx_rope_join(fx_rope *result, const fx_rope **ropes, size_t nr_ropes) static void rope_iterate( const struct fx_rope *rope, - void (*callback)(const struct fx_rope *, void *), void *arg) + void (*callback)(const struct fx_rope *, void *), + void *arg) { unsigned int type = FX_ROPE_TYPE(rope->r_flags); diff --git a/fx/status.c b/fx/status.c index 89e02e6..099fff1 100644 --- a/fx/status.c +++ b/fx/status.c @@ -1,4 +1,4 @@ -#include +#include #include #define ENUM_STR(s) \ diff --git a/fx/stream.c b/fx/stream.c index ef96031..a1644e4 100644 --- a/fx/stream.c +++ b/fx/stream.c @@ -1,7 +1,7 @@ #include "printf.h" -#include -#include +#include +#include #include #include #include diff --git a/fx/stringstream.c b/fx/stringstream.c index efc8d14..961d29f 100644 --- a/fx/stringstream.c +++ b/fx/stringstream.c @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -42,7 +42,10 @@ static enum fx_status __getc(struct fx_stringstream_p *ss, fx_wchar *out) } static enum fx_status __gets( - struct fx_stringstream_p *ss, char *s, size_t len, size_t *nr_read) + struct fx_stringstream_p *ss, + char *s, + size_t len, + size_t *nr_read) { size_t available = ss->ss_len - ss->ss_ptr; size_t to_copy = len; @@ -63,7 +66,10 @@ static enum fx_status __gets( } static enum fx_status __puts( - struct fx_stringstream_p *ss, const char *s, size_t len, size_t *nr_written) + struct fx_stringstream_p *ss, + const char *s, + size_t len, + size_t *nr_written) { size_t available = ss->ss_max - ss->ss_len; size_t to_copy = len; @@ -109,7 +115,9 @@ static enum fx_status stringstream_reset(struct fx_stringstream_p *ss) } static enum fx_status stringstream_reset_with_buffer( - struct fx_stringstream_p *ss, char *buf, size_t max) + struct fx_stringstream_p *ss, + char *buf, + size_t max) { ss->ss_len = 0; @@ -198,19 +206,29 @@ fx_stringstream *fx_stringstream_create(void) size_t fx_stringstream_get_length(const fx_stringstream *strv) { FX_CLASS_DISPATCH_STATIC_0( - FX_TYPE_STRINGSTREAM, stringstream_get_length, strv); + FX_TYPE_STRINGSTREAM, + stringstream_get_length, + strv); } enum fx_status fx_stringstream_reset(fx_stringstream *strv) { - FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_STRINGSTREAM, stringstream_reset, strv); + FX_CLASS_DISPATCH_STATIC_0( + FX_TYPE_STRINGSTREAM, + stringstream_reset, + strv); } enum fx_status fx_stringstream_reset_with_buffer( - fx_stringstream *strv, char *buf, size_t max) + fx_stringstream *strv, + char *buf, + size_t max) { FX_CLASS_DISPATCH_STATIC( - FX_TYPE_STRINGSTREAM, stringstream_reset_with_buffer, strv, buf, + FX_TYPE_STRINGSTREAM, + stringstream_reset_with_buffer, + strv, + buf, max); } @@ -221,7 +239,10 @@ const char *fx_stringstream_ptr(const fx_stringstream *ss) char *fx_stringstream_steal(fx_stringstream *ss) { - FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_STRINGSTREAM, stringstream_steal, ss); + FX_CLASS_DISPATCH_STATIC_0( + FX_TYPE_STRINGSTREAM, + stringstream_steal, + ss); } /*** PUBLIC ALIAS FUNCTIONS ***************************************************/ @@ -252,7 +273,10 @@ enum fx_status stream_getc(fx_stream *stream, fx_wchar *c) } enum fx_status stream_read( - fx_stream *stream, void *buf, size_t count, size_t *nr_read) + fx_stream *stream, + void *buf, + size_t count, + size_t *nr_read) { struct fx_stringstream_p *s = fx_object_get_private(stream, FX_TYPE_STRINGSTREAM); @@ -263,7 +287,10 @@ enum fx_status stream_read( } enum fx_status stream_write( - fx_stream *stream, const void *buf, size_t count, size_t *nr_written) + fx_stream *stream, + const void *buf, + size_t count, + size_t *nr_written) { struct fx_stringstream_p *s = fx_object_get_private(stream, FX_TYPE_STRINGSTREAM); diff --git a/fx/sys/linux/bitop.c b/fx/sys/linux/bitop.c index 3653953..98ba2e7 100644 --- a/fx/sys/linux/bitop.c +++ b/fx/sys/linux/bitop.c @@ -1,4 +1,4 @@ -#include +#include int fx_popcountl(long v) { diff --git a/fx/type.c b/fx/type.c index 09355de..a72d8ee 100644 --- a/fx/type.c +++ b/fx/type.c @@ -3,10 +3,10 @@ #include "class.h" #include "object.h" -#include -#include -#include -#include +#include +#include +#include +#include #include #include #include @@ -20,26 +20,37 @@ struct type_init_ctx { }; static inline int registration_compare( - const struct fx_type_registration *a, const struct fx_type_registration *b) + const struct fx_type_registration *a, + const struct fx_type_registration *b) { return fx_type_id_compare(&a->r_info->t_id, &b->r_info->t_id); } static inline int component_compare( - const struct fx_type_component *a, const struct fx_type_component *b) + const struct fx_type_component *a, + const struct fx_type_component *b) { - return fx_type_id_compare(&a->c_type->r_info->t_id, &b->c_type->r_info->t_id); + return fx_type_id_compare( + &a->c_type->r_info->t_id, + &b->c_type->r_info->t_id); } FX_BST_DEFINE_INSERT( - struct fx_type_registration, r_node, r_info->r_id, put_type, + struct fx_type_registration, + r_node, + r_info->r_id, + put_type, registration_compare) FX_BST_DEFINE_INSERT( - struct fx_type_component, c_node, &c_type->r_info->t_id, - put_type_component, component_compare) + struct fx_type_component, + c_node, + &c_type->r_info->t_id, + put_type_component, + component_compare) static struct fx_type_registration *get_type( - const fx_bst *tree, const union fx_type *key) + const fx_bst *tree, + const union fx_type *key) { fx_bst_node *cur = tree->bst_root; while (cur) { @@ -60,13 +71,16 @@ static struct fx_type_registration *get_type( } struct fx_type_component *fx_type_get_component( - const fx_bst *tree, const union fx_type *key) + const fx_bst *tree, + const union fx_type *key) { fx_bst_node *cur = tree->bst_root; while (cur) { struct fx_type_component *cur_node = fx_unbox(struct fx_type_component, cur, c_node); - int cmp = fx_type_id_compare(key, &cur_node->c_type->r_info->t_id); + int cmp = fx_type_id_compare( + key, + &cur_node->c_type->r_info->t_id); if (cmp > 0) { cur = fx_bst_right(cur); @@ -96,7 +110,11 @@ static struct fx_type_component *create_type_component( } void fx_type_id_init( - union fx_type *out, uint32_t a, uint16_t b, uint16_t c, uint16_t d, + union fx_type *out, + uint32_t a, + uint16_t b, + uint16_t c, + uint16_t d, uint64_t e) { fx_i32 x_a = fx_i32_htob(a); @@ -113,7 +131,8 @@ void fx_type_id_init( } static void initialise_type_component( - struct fx_type_component *comp, const struct fx_type_info *info, + struct fx_type_component *comp, + const struct fx_type_info *info, struct type_init_ctx *init_ctx) { comp->c_class_data_offset = init_ctx->ctx_class_offset; @@ -130,7 +149,8 @@ static void initialise_type_component( } static fx_result locate_interface( - fx_type interface_id, struct fx_type_registration *dest, + fx_type interface_id, + struct fx_type_registration *dest, struct type_init_ctx *init_ctx) { struct fx_type_component *interface_comp @@ -150,15 +170,20 @@ static fx_result locate_interface( return FX_RESULT_ERR(NO_MEMORY); } - initialise_type_component(interface_comp, interface_reg->r_info, init_ctx); + initialise_type_component( + interface_comp, + interface_reg->r_info, + init_ctx); put_type_component(&dest->r_components, interface_comp); return FX_RESULT_SUCCESS; } static fx_result locate_interfaces( - const union fx_type *interfaces, size_t nr_interfaces, - struct fx_type_registration *dest, struct type_init_ctx *init_ctx) + const union fx_type *interfaces, + size_t nr_interfaces, + struct fx_type_registration *dest, + struct type_init_ctx *init_ctx) { fx_result result = FX_RESULT_SUCCESS; for (size_t i = 0; i < nr_interfaces; i++) { @@ -190,7 +215,10 @@ static fx_result find_type_components(struct fx_type_registration *reg) fx_queue_push_front(®->r_class_hierarchy, &comp->c_entry); fx_result result = locate_interfaces( - current->t_interfaces, current->t_nr_interfaces, reg, &init_ctx); + current->t_interfaces, + current->t_nr_interfaces, + reg, + &init_ctx); if (fx_result_is_error(result)) { return result; @@ -221,7 +249,9 @@ static fx_result find_type_components(struct fx_type_registration *reg) result = locate_interfaces( dep_class->r_info->t_interfaces, - dep_class->r_info->t_nr_interfaces, reg, &init_ctx); + dep_class->r_info->t_nr_interfaces, + reg, + &init_ctx); if (fx_result_is_error(result)) { break; } @@ -239,7 +269,10 @@ static fx_result find_type_components(struct fx_type_registration *reg) fx_queue_entry *entry = fx_queue_first(®->r_class_hierarchy); while (entry) { comp = fx_unbox(struct fx_type_component, entry, c_entry); - initialise_type_component(comp, comp->c_type->r_info, &init_ctx); + initialise_type_component( + comp, + comp->c_type->r_info, + &init_ctx); entry = fx_queue_next(entry); } @@ -252,7 +285,10 @@ static fx_result find_type_components(struct fx_type_registration *reg) continue; } - initialise_type_component(comp, comp->c_type->r_info, &init_ctx); + initialise_type_component( + comp, + comp->c_type->r_info, + &init_ctx); node = fx_bst_next(node); } @@ -303,8 +339,10 @@ fx_result fx_type_register(struct fx_type_info *info) if (!r->r_class) { free(r); return fx_error_with_msg_template_caused_by_error( - FX_ERRORS_BUILTIN, FX_ERR_TYPE_REGISTRATION_FAILURE, - result, FX_MSG_TYPE_REGISTRATION_FAILURE, + FX_ERRORS_BUILTIN, + FX_ERR_TYPE_REGISTRATION_FAILURE, + result, + FX_MSG_TYPE_REGISTRATION_FAILURE, FX_ERROR_PARAM("typename", info->t_name)); } diff --git a/fx/type.h b/fx/type.h index 2ad2283..bfa51ea 100644 --- a/fx/type.h +++ b/fx/type.h @@ -1,9 +1,9 @@ #ifndef _TYPE_H_ #define _TYPE_H_ -#include -#include -#include +#include +#include +#include #include enum fx_type_category {