repo_name
string
dataset
string
owner
string
lang
string
func_name
string
code
string
docstring
string
url
string
sha
string
zap
github_2023
zigzap
c
fio_flush_all
size_t fio_flush_all(void) { if (!fio_data) return 0; size_t count = 0; for (uintptr_t i = 0; i <= fio_data->max_protocol_fd; ++i) { if ((fd_data(i).open || fd_data(i).packet) && fio_flush(fd2uuid(i)) > 0) ++count; } return count; }
/** `fio_flush_all` attempts flush all the open connections. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L3054-L3063
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_hooks_default_read
static ssize_t fio_hooks_default_read(intptr_t uuid, void *udata, void *buf, size_t count) { return read(fio_uuid2fd(uuid), buf, count); (void)(udata); }
/* ***************************************************************************** Connection Read / Write Hooks, for overriding the system calls ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L3069-L3073
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_rw_hook_replace_unsafe
int fio_rw_hook_replace_unsafe(intptr_t uuid, fio_rw_hook_s *rw_hooks, void *udata) { int replaced = -1; uint8_t was_locked; intptr_t fd = fio_uuid2fd(uuid); if (!rw_hooks->read) rw_hooks->read = fio_hooks_default_read; if (!rw_hooks->write) rw_hooks->write = fio_hooks_default_write; if (!rw_hooks->flush) rw_hooks->flush = fio_hooks_default_flush; if (!rw_hooks->before_close) rw_hooks->before_close = fio_hooks_default_before_close; if (!rw_hooks->cleanup) rw_hooks->cleanup = fio_hooks_default_cleanup; /* protect against some fulishness... but not all of it. */ was_locked = fio_trylock(&fd_data(fd).sock_lock); if (fd2uuid(fd) == uuid) { fd_data(fd).rw_hooks = rw_hooks; fd_data(fd).rw_udata = udata; replaced = 0; } if (!was_locked) fio_unlock(&fd_data(fd).sock_lock); return replaced; }
/** * Replaces an existing read/write hook with another from within a read/write * hook callback. * * Does NOT call any cleanup callbacks. * * Returns -1 on error, 0 on success. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L3110-L3135
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_rw_hook_set
int fio_rw_hook_set(intptr_t uuid, fio_rw_hook_s *rw_hooks, void *udata) { if (fio_is_closed(uuid)) goto invalid_uuid; if (!rw_hooks->read) rw_hooks->read = fio_hooks_default_read; if (!rw_hooks->write) rw_hooks->write = fio_hooks_default_write; if (!rw_hooks->flush) rw_hooks->flush = fio_hooks_default_flush; if (!rw_hooks->before_close) rw_hooks->before_close = fio_hooks_default_before_close; if (!rw_hooks->cleanup) rw_hooks->cleanup = fio_hooks_default_cleanup; intptr_t fd = fio_uuid2fd(uuid); fio_rw_hook_s *old_rw_hooks; void *old_udata; fio_lock(&fd_data(fd).sock_lock); if (fd2uuid(fd) != uuid) { fio_unlock(&fd_data(fd).sock_lock); goto invalid_uuid; } old_rw_hooks = fd_data(fd).rw_hooks; old_udata = fd_data(fd).rw_udata; fd_data(fd).rw_hooks = rw_hooks; fd_data(fd).rw_udata = udata; fio_unlock(&fd_data(fd).sock_lock); if (old_rw_hooks && old_rw_hooks->cleanup) old_rw_hooks->cleanup(old_udata); return 0; invalid_uuid: if (!rw_hooks->cleanup) rw_hooks->cleanup(udata); return -1; }
/** Sets a socket hook state (a pointer to the struct). */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L3138-L3171
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_attach__internal
static int fio_attach__internal(void *uuid_, void *protocol_) { intptr_t uuid = (intptr_t)uuid_; fio_protocol_s *protocol = (fio_protocol_s *)protocol_; if (protocol) { if (!protocol->on_close) { protocol->on_close = mock_on_ev; } if (!protocol->on_data) { protocol->on_data = mock_on_data; } if (!protocol->on_ready) { protocol->on_ready = mock_on_ev; } if (!protocol->ping) { protocol->ping = mock_ping; } if (!protocol->on_shutdown) { protocol->on_shutdown = mock_on_shutdown; } prt_meta(protocol) = (protocol_metadata_s){.rsv = 0}; } if (!uuid_is_valid(uuid)) goto invalid_uuid_unlocked; fio_lock(&uuid_data(uuid).protocol_lock); if (!uuid_is_valid(uuid)) { goto invalid_uuid; } fio_protocol_s *old_pr = uuid_data(uuid).protocol; uuid_data(uuid).open = 1; uuid_data(uuid).protocol = protocol; touchfd(fio_uuid2fd(uuid)); fio_unlock(&uuid_data(uuid).protocol_lock); if (old_pr) { /* protocol replacement */ fio_defer_push_task(deferred_on_close, (void *)uuid, old_pr); if (!protocol) { /* hijacking */ fio_poll_remove_fd(fio_uuid2fd(uuid)); fio_poll_add_write(fio_uuid2fd(uuid)); } } else if (protocol) { /* adding a new uuid to the reactor */ fio_poll_add(fio_uuid2fd(uuid)); } fio_max_fd_min(fio_uuid2fd(uuid)); return 0; invalid_uuid: fio_unlock(&uuid_data(uuid).protocol_lock); invalid_uuid_unlocked: // FIO_LOG_DEBUG("fio_attach failed for invalid uuid %p", (void *)uuid); if (protocol) fio_defer_push_task(deferred_on_close, (void *)uuid, protocol); if (uuid == -1) errno = EBADF; else errno = ENOTCONN; return -1; }
/* ***************************************************************************** Section Start Marker IO Protocols and Attachment ***************************************************************************** */ /* ***************************************************************************** Setting the protocol ***************************************************************************** */ /* managing the protocol pointer array and the `on_close` callback */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L3208-L3266
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_attach
void fio_attach(intptr_t uuid, fio_protocol_s *protocol) { fio_attach__internal((void *)uuid, protocol); }
/** * Attaches (or updates) a protocol object to a socket UUID. * Returns -1 on error and 0 on success. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L3272-L3274
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_attach_fd
void fio_attach_fd(int fd, fio_protocol_s *protocol) { fio_attach__internal((void *)fio_fd2uuid(fd), protocol); }
/** Attaches (or updates) a protocol object to a socket UUID. * Returns -1 on error and 0 on success. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L3278-L3280
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_timeout_set
void fio_timeout_set(intptr_t uuid, uint8_t timeout) { if (uuid_is_valid(uuid)) { touchfd(fio_uuid2fd(uuid)); uuid_data(uuid).timeout = timeout; } else { FIO_LOG_DEBUG("Called fio_timeout_set for invalid uuid %p", (void *)uuid); } }
/** Sets a timeout for a specific connection (only when running and valid). */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L3283-L3290
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_timeout_get
uint8_t fio_timeout_get(intptr_t uuid) { return uuid_data(uuid).timeout; }
/** Gets a timeout for a specific connection. Returns 0 if there's no set * timeout or the connection is inactive. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L3293-L3293
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_state_callback_add
void fio_state_callback_add(callback_type_e c_type, void (*func)(void *), void *arg) { if (c_type == FIO_CALL_ON_INITIALIZE && fio_data) { func(arg); return; } if (!func || (int)c_type < 0 || c_type > FIO_CALL_NEVER) return; fio_lock(&callback_collection[c_type].lock); fio_state_callback_ensure(&callback_collection[c_type]); callback_data_s *tmp = malloc(sizeof(*tmp)); FIO_ASSERT_ALLOC(tmp); *tmp = (callback_data_s){.func = func, .arg = arg}; fio_ls_embd_push(&callback_collection[c_type].callbacks, &tmp->node); fio_unlock(&callback_collection[c_type].lock); }
/** Adds a callback to the list of callbacks to be called for the event. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L3323-L3338
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_state_callback_remove
int fio_state_callback_remove(callback_type_e c_type, void (*func)(void *), void *arg) { if ((int)c_type < 0 || c_type > FIO_CALL_NEVER) return -1; fio_lock(&callback_collection[c_type].lock); FIO_LS_EMBD_FOR(&callback_collection[c_type].callbacks, pos) { callback_data_s *tmp = (FIO_LS_EMBD_OBJ(callback_data_s, node, pos)); if (tmp->func == func && tmp->arg == arg) { fio_ls_embd_remove(&tmp->node); free(tmp); goto success; } } fio_unlock(&callback_collection[c_type].lock); return -1; success: fio_unlock(&callback_collection[c_type].lock); return -0; }
/** Removes a callback from the list of callbacks to be called for the event. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L3341-L3359
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_state_callback_force
void fio_state_callback_force(callback_type_e c_type) { if ((int)c_type < 0 || c_type > FIO_CALL_NEVER) return; /* copy collection */ fio_ls_embd_s copy = FIO_LS_INIT(copy); fio_lock(&callback_collection[c_type].lock); fio_state_callback_ensure(&callback_collection[c_type]); switch (c_type) { /* the difference between `unshift` and `push` */ case FIO_CALL_ON_INITIALIZE: /* fallthrough */ case FIO_CALL_PRE_START: /* fallthrough */ case FIO_CALL_BEFORE_FORK: /* fallthrough */ case FIO_CALL_AFTER_FORK: /* fallthrough */ case FIO_CALL_IN_CHILD: /* fallthrough */ case FIO_CALL_IN_MASTER: /* fallthrough */ case FIO_CALL_ON_START: /* fallthrough */ FIO_LS_EMBD_FOR(&callback_collection[c_type].callbacks, pos) { callback_data_s *tmp = fio_malloc(sizeof(*tmp)); FIO_ASSERT_ALLOC(tmp); *tmp = *(FIO_LS_EMBD_OBJ(callback_data_s, node, pos)); fio_ls_embd_unshift(&copy, &tmp->node); } break; case FIO_CALL_ON_IDLE: /* idle callbacks are orderless and evented */ FIO_LS_EMBD_FOR(&callback_collection[c_type].callbacks, pos) { callback_data_s *tmp = FIO_LS_EMBD_OBJ(callback_data_s, node, pos); fio_defer_push_task(fio_state_on_idle_perform, (void *)(uintptr_t)tmp->func, tmp->arg); } break; case FIO_CALL_ON_SHUTDOWN: /* fallthrough */ case FIO_CALL_ON_FINISH: /* fallthrough */ case FIO_CALL_ON_PARENT_CRUSH: /* fallthrough */ case FIO_CALL_ON_CHILD_CRUSH: /* fallthrough */ case FIO_CALL_AT_EXIT: /* fallthrough */ case FIO_CALL_NEVER: /* fallthrough */ default: FIO_LS_EMBD_FOR(&callback_collection[c_type].callbacks, pos) { callback_data_s *tmp = fio_malloc(sizeof(*tmp)); FIO_ASSERT_ALLOC(tmp); *tmp = *(FIO_LS_EMBD_OBJ(callback_data_s, node, pos)); fio_ls_embd_push(&copy, &tmp->node); } break; } fio_unlock(&callback_collection[c_type].lock); /* run callbacks + free data */ while (fio_ls_embd_any(&copy)) { callback_data_s *tmp = FIO_LS_EMBD_OBJ(callback_data_s, node, fio_ls_embd_pop(&copy)); if (tmp->func) { tmp->func(tmp->arg); } fio_free(tmp); } }
/** Forces all the existing callbacks to run, as if the event occurred. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L3362-L3419
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_state_callback_clear
void fio_state_callback_clear(callback_type_e c_type) { if ((int)c_type < 0 || c_type > FIO_CALL_NEVER) return; fio_lock(&callback_collection[c_type].lock); fio_state_callback_ensure(&callback_collection[c_type]); while (fio_ls_embd_any(&callback_collection[c_type].callbacks)) { callback_data_s *tmp = FIO_LS_EMBD_OBJ( callback_data_s, node, fio_ls_embd_shift(&callback_collection[c_type].callbacks)); free(tmp); } fio_unlock(&callback_collection[c_type].lock); }
/** Clears all the existing callbacks for the event. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L3422-L3434
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_io_task_perform
static void fio_io_task_perform(void *uuid_, void *args_) { fio_defer_iotask_args_s *args = args_; intptr_t uuid = (intptr_t)uuid_; fio_protocol_s *pr = fio_protocol_try_lock(uuid, args->type); if (!pr) goto postpone; args->task(uuid, pr, args->udata); fio_protocol_unlock(pr, args->type); fio_free(args); return; postpone: if (errno == EBADF) { if (args->fallback) args->fallback(uuid, args->udata); fio_free(args); return; } fio_defer_push_task(fio_io_task_perform, uuid_, args_); }
/* ***************************************************************************** IO bound tasks ***************************************************************************** */ // typedef struct { // enum fio_protocol_lock_e type; // void (*task)(intptr_t uuid, fio_protocol_s *, void *udata); // void *udata; // void (*fallback)(intptr_t uuid, void *udata); // } fio_defer_iotask_args_s;
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L3458-L3476
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
FIO_IGNORE_MACRO
void fio_defer_io_task FIO_IGNORE_MACRO(intptr_t uuid, fio_defer_iotask_args_s args) { if (!args.task) { if (args.fallback) fio_defer_push_task((void (*)(void *, void *))args.fallback, (void *)uuid, args.udata); return; } fio_defer_iotask_args_s *cpy = fio_malloc(sizeof(*cpy)); FIO_ASSERT_ALLOC(cpy); *cpy = args; fio_defer_push_task(fio_io_task_perform, (void *)uuid, cpy); }
/** * Schedules a protected connection task. The task will run within the * connection's lock. * * If an error ocuurs or the connection is closed before the task can run, the * `fallback` task wil be called instead, allowing for resource cleanup. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L3484-L3496
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_on_fork
static void fio_on_fork(void) { fio_timer_lock = FIO_LOCK_INIT; fio_data->lock = FIO_LOCK_INIT; fio_defer_on_fork(); fio_malloc_after_fork(); fio_poll_init(); fio_state_callback_on_fork(); const size_t limit = fio_data->capa; for (size_t i = 0; i < limit; ++i) { fd_data(i).sock_lock = FIO_LOCK_INIT; fd_data(i).protocol_lock = FIO_LOCK_INIT; if (fd_data(i).protocol) { fd_data(i).protocol->rsv = 0; fio_force_close(fd2uuid(i)); } } fio_pubsub_on_fork(); fio_max_fd_shrink(); uint16_t old_active = fio_data->active; fio_data->active = 0; fio_defer_perform(); fio_data->active = old_active; fio_data->is_worker = 1; }
/* Called within a child process after it starts. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L3505-L3530
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_cycle_schedule_events
static void fio_cycle_schedule_events(void) { static int idle = 0; static time_t last_to_review = 0; fio_mark_time(); fio_timer_schedule(); fio_max_fd_shrink(); if (fio_signal_children_flag) { /* hot restart support */ fio_signal_children_flag = 0; fio_cluster_signal_children(); } int events = fio_poll(); if (events < 0) { return; } if (events > 0) { idle = 1; } else { /* events == 0 */ if (idle) { fio_state_callback_force(FIO_CALL_ON_IDLE); idle = 0; } } if (fio_data->need_review && fio_data->last_cycle.tv_sec != last_to_review) { last_to_review = fio_data->last_cycle.tv_sec; fio_data->need_review = 0; fio_defer_push_task(fio_review_timeout, (void *)0, NULL); } }
/* reactor pattern cycling - common actions */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L3712-L3741
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_cycle_unwind
static void fio_cycle_unwind(void *ignr, void *ignr2) { if (fio_data->connection_count) { fio_cycle_schedule_events(); fio_defer_push_task(fio_cycle_unwind, ignr, ignr2); return; } fio_stop(); return; }
/* reactor pattern cycling during cleanup */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L3744-L3752
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_cycle
static void fio_cycle(void *ignr, void *ignr2) { fio_cycle_schedule_events(); if (fio_data->active) { fio_defer_push_task(fio_cycle, ignr, ignr2); return; } return; }
/* reactor pattern cycling */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L3755-L3762
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_worker_startup
static void fio_worker_startup(void) { /* Call the on_start callbacks for worker processes. */ if (fio_data->workers == 1 || fio_data->is_worker) { fio_state_callback_force(FIO_CALL_ON_START); fio_state_callback_clear(FIO_CALL_ON_START); } if (fio_data->workers == 1) { /* Single Process - the root is also a worker */ fio_data->is_worker = 1; } else if (fio_data->is_worker) { /* Worker Process */ FIO_LOG_INFO("%d is running.", (int)getpid()); } else { /* Root Process should run in single thread mode */ fio_data->threads = 1; } /* require timeout review */ fio_data->need_review = 1; /* the cycle task will loop by re-scheduling until it's time to finish */ fio_defer_push_task(fio_cycle, NULL, NULL); /* A single thread doesn't need a pool. */ if (fio_data->threads > 1) { fio_defer_thread_pool_join(fio_defer_thread_pool_new(fio_data->threads)); } else { fio_defer_perform(); } }
/* TODO: fixme */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L3765-L3795
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_worker_cleanup
static void fio_worker_cleanup(void) { /* switch to winding down */ if (fio_data->is_worker) FIO_LOG_INFO("(%d) detected exit signal.", (int)getpid()); else FIO_LOG_INFO("Server Detected exit signal."); fio_state_callback_force(FIO_CALL_ON_SHUTDOWN); for (size_t i = 0; i <= fio_data->max_protocol_fd; ++i) { if (fd_data(i).protocol) { fio_defer_push_task(deferred_on_shutdown, (void *)fd2uuid(i), NULL); } } fio_defer_push_task(fio_cycle_unwind, NULL, NULL); fio_defer_perform(); for (size_t i = 0; i <= fio_data->max_protocol_fd; ++i) { if (fd_data(i).protocol || fd_data(i).open) { fio_force_close(fd2uuid(i)); } } fio_timer_clear_all(); fio_defer_perform(); if (!fio_data->is_worker) { fio_cluster_signal_children(); fio_defer_perform(); while (wait(NULL) != -1) ; } fio_defer_perform(); fio_state_callback_force(FIO_CALL_ON_FINISH); fio_defer_perform(); fio_signal_handler_reset(); if (fio_data->parent == getpid()) { FIO_LOG_INFO(" --- Shutdown Complete ---\n"); } else { FIO_LOG_INFO("(%d) cleanup complete.", (int)getpid()); } }
/* performs all clean-up / shutdown requirements except for the exit sequence */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L3798-L3834
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
FIO_IGNORE_MACRO
void fio_start FIO_IGNORE_MACRO(struct fio_start_args args) { fio_expected_concurrency(&args.threads, &args.workers); fio_signal_handler_setup(); fio_data->workers = (uint16_t)args.workers; fio_data->threads = (uint16_t)args.threads; fio_data->active = 1; fio_data->is_worker = 0; fio_state_callback_force(FIO_CALL_PRE_START); FIO_LOG_INFO( "Server is running %u %s X %u %s with facil.io " FIO_VERSION_STRING " (%s)\n" "* Detected capacity: %d open file limit\n" "* Root pid: %d\n" "* Press ^C to stop\n", fio_data->workers, fio_data->workers > 1 ? "workers" : "worker", fio_data->threads, fio_data->threads > 1 ? "threads" : "thread", fio_engine(), fio_data->capa, (int)fio_data->parent); if (args.workers > 1) { for (int i = 0; i < args.workers && fio_data->active; ++i) { fio_sentinel_task(NULL, NULL); } } fio_worker_startup(); fio_worker_cleanup(); }
/* marker for SublimeText3 jump feature */ /** * Starts the facil.io event loop. This function will return after facil.io is * done (after shutdown). * * See the `struct fio_start_args` details for any possible named arguments. * * This method blocks the current thread until the server is stopped (when a * SIGINT/SIGTERM is received). */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L3916-L3944
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_atol_skip_zero
FIO_FUNC inline size_t fio_atol_skip_zero(char **pstr) { char *const start = *pstr; while (**pstr == '0') { ++(*pstr); } return (size_t)(*pstr - *start); }
/* ***************************************************************************** Section Start Marker Converting Numbers to Strings (and back) ***************************************************************************** */ /* ***************************************************************************** Strings to Numbers ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L3986-L3992
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_atol_consume
FIO_FUNC inline uint64_t fio_atol_consume(char **pstr, uint8_t base) { uint64_t result = 0; const uint64_t limit = UINT64_MAX - (base * base); while (**pstr >= '0' && **pstr < ('0' + base) && result <= (limit)) { result = (result * base) + (**pstr - '0'); ++(*pstr); } return result; }
/* consumes any digits in the string (base 2-10), returning their value */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L3995-L4003
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_atol_skip_test
FIO_FUNC inline uint8_t fio_atol_skip_test(char **pstr, uint8_t base) { return (**pstr >= '0' && **pstr < ('0' + base)); }
/* returns true if there's data to be skipped */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L4006-L4008
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_atol_skip
FIO_FUNC inline uint64_t fio_atol_skip(char **pstr, uint8_t base) { uint64_t result = 0; while (fio_atol_skip_test(pstr, base)) { ++result; ++(*pstr); } return result; }
/* consumes any digits in the string (base 2-10), returning the count skipped */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L4011-L4018
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_atol_consume_hex
FIO_FUNC inline uint64_t fio_atol_consume_hex(char **pstr) { uint64_t result = 0; const uint64_t limit = UINT64_MAX - (16 * 16); for (; result <= limit;) { uint8_t tmp; if (**pstr >= '0' && **pstr <= '9') tmp = **pstr - '0'; else if (**pstr >= 'A' && **pstr <= 'F') tmp = **pstr - ('A' - 10); else if (**pstr >= 'a' && **pstr <= 'f') tmp = **pstr - ('a' - 10); else return result; result = (result << 4) | tmp; ++(*pstr); } return result; }
/* consumes any hex data in the string, returning their value */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L4021-L4038
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_atol_skip_hex_test
FIO_FUNC inline uint8_t fio_atol_skip_hex_test(char **pstr) { return (**pstr >= '0' && **pstr <= '9') || (**pstr >= 'A' && **pstr <= 'F') || (**pstr >= 'a' && **pstr <= 'f'); }
/* returns true if there's data to be skipped */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L4041-L4044
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_atol_skip_hex
FIO_FUNC inline uint64_t fio_atol_skip_hex(char **pstr) { uint64_t result = 0; while (fio_atol_skip_hex_test(pstr)) { ++result; ++(*pstr); } return result; }
/* consumes any digits in the string (base 2-10), returning the count skipped */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L4047-L4054
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_atol
int64_t fio_atol(char **pstr) { /* No binary representation in strtol */ char *str = *pstr; uint64_t result = 0; uint8_t invert = 0; while (isspace(*str)) ++(str); if (str[0] == '-') { invert ^= 1; ++str; } else if (*str == '+') { ++(str); } if (str[0] == 'B' || str[0] == 'b' || (str[0] == '0' && (str[1] == 'b' || str[1] == 'B'))) { /* base 2 */ if (str[0] == '0') str++; str++; fio_atol_skip_zero(&str); while (str[0] == '0' || str[0] == '1') { result = (result << 1) | (str[0] - '0'); str++; } goto sign; /* no overlow protection, since sign might be embedded */ } else if (str[0] == 'x' || str[0] == 'X' || (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))) { /* base 16 */ if (str[0] == '0') str++; str++; fio_atol_skip_zero(&str); result = fio_atol_consume_hex(&str); if (fio_atol_skip_hex_test(&str)) /* too large for a number */ return 0; goto sign; /* no overlow protection, since sign might be embedded */ } else if (str[0] == '0') { fio_atol_skip_zero(&str); /* base 8 */ result = fio_atol_consume(&str, 8); if (fio_atol_skip_test(&str, 8)) /* too large for a number */ return 0; } else { /* base 10 */ result = fio_atol_consume(&str, 10); if (fio_atol_skip_test(&str, 10)) /* too large for a number */ return 0; } if (result & ((uint64_t)1 << 63)) result = INT64_MAX; /* signed overflow protection */ sign: if (invert) result = 0 - result; *pstr = str; return (int64_t)result; }
/* caches a up to 8*8 */ // static inline fio_atol_pow_10_cache(size_t ex) {} /** * A helper function that converts between String data to a signed int64_t. * * Numbers are assumed to be in base 10. Octal (`0###`), Hex (`0x##`/`x##`) and * binary (`0b##`/ `b##`) are recognized as well. For binary Most Significant * Bit must come first. * * The most significant difference between this function and `strtol` (aside of * API design), is the added support for binary representations. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L4069-L4126
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_atof
double fio_atof(char **pstr) { return strtold(*pstr, pstr); }
/** A helper function that converts between String data to a signed double. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L4129-L4129
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_ltoa
size_t fio_ltoa(char *dest, int64_t num, uint8_t base) { const char notation[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; size_t len = 0; char buf[48]; /* we only need up to 20 for base 10, but base 3 needs 41... */ if (!num) goto zero; switch (base) { case 1: /* fallthrough */ case 2: /* Base 2 */ { uint64_t n = num; /* avoid bit shifting inconsistencies with signed bit */ uint8_t i = 0; /* counting bits */ dest[len++] = '0'; dest[len++] = 'b'; while ((i < 64) && (n & 0x8000000000000000) == 0) { n = n << 1; i++; } /* make sure the Binary representation doesn't appear signed. */ if (i) { dest[len++] = '0'; } /* write to dest. */ while (i < 64) { dest[len++] = ((n & 0x8000000000000000) ? '1' : '0'); n = n << 1; i++; } dest[len] = 0; return len; } case 8: /* Base 8 */ { uint64_t l = 0; if (num < 0) { dest[len++] = '-'; num = 0 - num; } dest[len++] = '0'; while (num) { buf[l++] = '0' + (num & 7); num = num >> 3; } while (l) { --l; dest[len++] = buf[l]; } dest[len] = 0; return len; } case 16: /* Base 16 */ { uint64_t n = num; /* avoid bit shifting inconsistencies with signed bit */ uint8_t i = 0; /* counting bits */ dest[len++] = '0'; dest[len++] = 'x'; while (i < 8 && (n & 0xFF00000000000000) == 0) { n = n << 8; i++; } /* make sure the Hex representation doesn't appear misleadingly signed. */ if (i && (n & 0x8000000000000000)) { dest[len++] = '0'; dest[len++] = '0'; } /* write the damn thing, high to low */ while (i < 8) { uint8_t tmp = (n & 0xF000000000000000) >> 60; dest[len++] = notation[tmp]; tmp = (n & 0x0F00000000000000) >> 56; dest[len++] = notation[tmp]; i++; n = n << 8; } dest[len] = 0; return len; } case 3: /* fallthrough */ case 4: /* fallthrough */ case 5: /* fallthrough */ case 6: /* fallthrough */ case 7: /* fallthrough */ case 9: /* fallthrough */ /* rare bases */ if (num < 0) { dest[len++] = '-'; num = 0 - num; } uint64_t l = 0; while (num) { uint64_t t = num / base; buf[l++] = '0' + (num - (t * base)); num = t; } while (l) { --l; dest[len++] = buf[l]; } dest[len] = 0; return len; default: break; } /* Base 10, the default base */ if (num < 0) { dest[len++] = '-'; num = 0 - num; } uint64_t l = 0; while (num) { uint64_t t = num / 10; buf[l++] = '0' + (num - (t * 10)); num = t; } while (l) { --l; dest[len++] = buf[l]; } dest[len] = 0; return len; zero: switch (base) { case 1: case 2: dest[len++] = '0'; dest[len++] = 'b'; break; case 8: dest[len++] = '0'; break; case 16: dest[len++] = '0'; dest[len++] = 'x'; dest[len++] = '0'; break; } dest[len++] = '0'; dest[len] = 0; return len; }
/* ***************************************************************************** Numbers to Strings ***************************************************************************** */ /** * A helper function that writes a signed int64_t to a string. * * No overflow guard is provided, make sure there's at least 68 bytes * available (for base 2). * * Offers special support for base 2 (binary), base 8 (octal), base 10 and base * 16 (hex). An unsupported base will silently default to base 10. Prefixes * are automatically added (i.e., "0x" for hex and "0b" for base 2). * * Returns the number of bytes actually written (excluding the NUL * terminator). */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L4148-L4300
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_ftoa
size_t fio_ftoa(char *dest, double num, uint8_t base) { if (base == 2 || base == 16) { /* handle the binary / Hex representation the same as if it were an * int64_t */ int64_t *i = (void *)&num; return fio_ltoa(dest, *i, base); } size_t written = sprintf(dest, "%g", num); uint8_t need_zero = 1; char *start = dest; while (*start) { if (*start == ',') // locale issues? *start = '.'; if (*start == '.' || *start == 'e') { need_zero = 0; break; } start++; } if (need_zero) { dest[written++] = '.'; dest[written++] = '0'; } return written; }
/** * A helper function that converts between a double to a string. * * No overflow guard is provided, make sure there's at least 130 bytes * available (for base 2). * * Supports base 2, base 10 and base 16. An unsupported base will silently * default to base 10. Prefixes aren't added (i.e., no "0x" or "0b" at the * beginning of the string). * * Returns the number of bytes actually written (excluding the NUL * terminator). */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L4315-L4341
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_tls_alpn_count
uintptr_t FIO_TLS_WEAK fio_tls_alpn_count(void *tls) { return 0; (void)tls; }
/* ***************************************************************************** Section Start Marker SSL/TLS Weak Symbols for TLS Support ***************************************************************************** */ /** * Returns the number of registered ALPN protocol names. * * This could be used when deciding if protocol selection should be delegated to * the ALPN mechanism, or whether a protocol should be immediately assigned. * * If no ALPN protocols are registered, zero (0) is returned. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L4371-L4374
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_tls_accept
void FIO_TLS_WEAK fio_tls_accept(intptr_t uuid, void *tls, void *udata) { FIO_LOG_FATAL("No supported SSL/TLS library available."); exit(-1); return; (void)uuid; (void)tls; (void)udata; }
/** * Establishes an SSL/TLS connection as an SSL/TLS Server, using the specified * context / settings object. * * The `uuid` should be a socket UUID that is already connected to a peer (i.e., * the result of `fio_accept`). * * The `udata` is an opaque user data pointer that is passed along to the * protocol selected (if any protocols were added using `fio_tls_alpn_add`). */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L4386-L4393
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_tls_connect
void FIO_TLS_WEAK fio_tls_connect(intptr_t uuid, void *tls, void *udata) { FIO_LOG_FATAL("No supported SSL/TLS library available."); exit(-1); return; (void)uuid; (void)tls; (void)udata; }
/** * Establishes an SSL/TLS connection as an SSL/TLS Client, using the specified * context / settings object. * * The `uuid` should be a socket UUID that is already connected to a peer (i.e., * one received by a `fio_connect` specified callback `on_connect`). * * The `udata` is an opaque user data pointer that is passed along to the * protocol selected (if any protocols were added using `fio_tls_alpn_add`). */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L4405-L4412
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_tls_dup
void FIO_TLS_WEAK fio_tls_dup(void *tls) { FIO_LOG_FATAL("No supported SSL/TLS library available."); exit(-1); return; (void)tls; }
/** * Increase the reference count for the TLS object. * * Decrease with `fio_tls_destroy`. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L4419-L4424
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_tls_destroy
void FIO_TLS_WEAK fio_tls_destroy(void *tls) { FIO_LOG_FATAL("No supported SSL/TLS library available."); exit(-1); return; (void)tls; }
/** * Destroys the SSL/TLS context / settings object and frees any related * resources / memory. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L4430-L4435
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
FIO_IGNORE_MACRO
intptr_t fio_listen FIO_IGNORE_MACRO(struct fio_listen_args args) { // ... if ((!args.on_open && (!args.tls || !fio_tls_alpn_count(args.tls))) || (!args.address && !args.port)) { errno = EINVAL; goto error; } size_t addr_len = 0; size_t port_len = 0; if (args.address) addr_len = strlen(args.address); if (args.port) { port_len = strlen(args.port); char *tmp = (char *)args.port; if (!fio_atol(&tmp)) { port_len = 0; args.port = NULL; } if (*tmp) { /* port format was invalid, should be only numerals */ errno = EINVAL; goto error; } } const intptr_t uuid = fio_socket(args.address, args.port, 1); if (uuid == -1) goto error; fio_listen_protocol_s *pr = malloc(sizeof(*pr) + addr_len + port_len + ((addr_len + port_len) ? 2 : 0)); FIO_ASSERT_ALLOC(pr); if (args.tls) fio_tls_dup(args.tls); *pr = (fio_listen_protocol_s){ .pr = { .on_close = fio_listen_on_close, .ping = mock_ping_eternal, .on_data = (args.tls ? (fio_tls_alpn_count(args.tls) ? fio_listen_on_data_tls_alpn : fio_listen_on_data_tls) : fio_listen_on_data), }, .uuid = uuid, .udata = args.udata, .on_open = args.on_open, .on_start = args.on_start, .on_finish = args.on_finish, .tls = args.tls, .addr_len = addr_len, .port_len = port_len, .addr = (char *)(pr + 1), .port = ((char *)(pr + 1) + addr_len + 1), }; if (addr_len) memcpy(pr->addr, args.address, addr_len + 1); if (port_len) memcpy(pr->port, args.port, port_len + 1); if (fio_is_running()) { fio_attach(pr->uuid, &pr->pr); } else { fio_state_callback_add(FIO_CALL_ON_START, fio_listen_on_startup, pr); fio_state_callback_add(FIO_CALL_ON_SHUTDOWN, fio_listen_cleanup_task, pr); } if (args.port) FIO_LOG_INFO("Listening on port %s", args.port); else FIO_LOG_INFO("Listening on Unix Socket at %s", args.address); return uuid; error: if (args.on_finish) { args.on_finish(-1, args.udata); } return -1; }
/** * Schedule a network service on a listening socket. * * Returns the listening socket or -1 (on error). */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L4563-L4644
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_url_parse
fio_url_s fio_url_parse(const char *url, size_t length) { /* Intention: [schema://][user[:]][password[@]][host.com[:/]][:port/][/path][?quary][#target] */ const char *end = url + length; const char *pos = url; fio_url_s r = {.scheme = {.data = (char *)url}}; if (length == 0) { goto finish; } if (pos[0] == '/') { /* start at path */ goto start_path; } while (pos < end && pos[0] != ':' && pos[0] != '/' && pos[0] != '@' && pos[0] != '#' && pos[0] != '?') ++pos; if (pos == end) { /* was only host (path starts with '/') */ r.host = (fio_str_info_s){.data = (char *)url, .len = pos - url}; goto finish; } switch (pos[0]) { case '@': /* username@[host] */ r.user = (fio_str_info_s){.data = (char *)url, .len = pos - url}; ++pos; goto start_host; case '/': /* host[/path] */ r.host = (fio_str_info_s){.data = (char *)url, .len = pos - url}; goto start_path; case '?': /* host?[query] */ r.host = (fio_str_info_s){.data = (char *)url, .len = pos - url}; ++pos; goto start_query; case '#': /* host#[target] */ r.host = (fio_str_info_s){.data = (char *)url, .len = pos - url}; ++pos; goto start_target; case ':': if (pos + 2 <= end && pos[1] == '/' && pos[2] == '/') { /* scheme:// */ r.scheme.len = pos - url; pos += 3; } else { /* username:[password] OR */ /* host:[port] */ r.user = (fio_str_info_s){.data = (char *)url, .len = pos - url}; ++pos; goto start_password; } break; } // start_username: url = pos; while (pos < end && pos[0] != ':' && pos[0] != '/' && pos[0] != '@' /* && pos[0] != '#' && pos[0] != '?' */) ++pos; if (pos >= end) { /* scheme://host */ r.host = (fio_str_info_s){.data = (char *)url, .len = pos - url}; goto finish; } switch (pos[0]) { case '/': /* scheme://host[/path] */ r.host = (fio_str_info_s){.data = (char *)url, .len = pos - url}; goto start_path; case '@': /* scheme://username@[host]... */ r.user = (fio_str_info_s){.data = (char *)url, .len = pos - url}; ++pos; goto start_host; case ':': /* scheme://username:[password]@[host]... OR */ /* scheme://host:[port][/...] */ r.user = (fio_str_info_s){.data = (char *)url, .len = pos - url}; ++pos; break; } start_password: url = pos; while (pos < end && pos[0] != '/' && pos[0] != '@') ++pos; if (pos >= end) { /* was host:port */ r.port = (fio_str_info_s){.data = (char *)url, .len = pos - url}; r.host = r.user; r.user.len = 0; goto finish; ; } switch (pos[0]) { case '/': r.port = (fio_str_info_s){.data = (char *)url, .len = pos - url}; r.host = r.user; r.user.len = 0; goto start_path; case '@': r.password = (fio_str_info_s){.data = (char *)url, .len = pos - url}; ++pos; break; } start_host: url = pos; while (pos < end && pos[0] != '/' && pos[0] != ':' && pos[0] != '#' && pos[0] != '?') ++pos; r.host = (fio_str_info_s){.data = (char *)url, .len = pos - url}; if (pos >= end) { goto finish; } switch (pos[0]) { case '/': /* scheme://[...@]host[/path] */ goto start_path; case '?': /* scheme://[...@]host?[query] (bad)*/ ++pos; goto start_query; case '#': /* scheme://[...@]host#[target] (bad)*/ ++pos; goto start_target; // case ':': /* scheme://[...@]host:[port] */ } ++pos; // start_port: url = pos; while (pos < end && pos[0] != '/' && pos[0] != '#' && pos[0] != '?') ++pos; r.port = (fio_str_info_s){.data = (char *)url, .len = pos - url}; if (pos >= end) { /* scheme://[...@]host:port */ goto finish; } switch (pos[0]) { case '?': /* scheme://[...@]host:port?[query] (bad)*/ ++pos; goto start_query; case '#': /* scheme://[...@]host:port#[target] (bad)*/ ++pos; goto start_target; // case '/': /* scheme://[...@]host:port[/path] */ } start_path: url = pos; while (pos < end && pos[0] != '#' && pos[0] != '?') ++pos; r.path = (fio_str_info_s){.data = (char *)url, .len = pos - url}; if (pos >= end) { goto finish; } ++pos; if (pos[-1] == '#') goto start_target; start_query: url = pos; while (pos < end && pos[0] != '#') ++pos; r.query = (fio_str_info_s){.data = (char *)url, .len = pos - url}; ++pos; if (pos >= end) goto finish; start_target: r.target = (fio_str_info_s){.data = (char *)pos, .len = end - pos}; finish: /* set any empty values to NULL */ if (!r.scheme.len) r.scheme.data = NULL; if (!r.user.len) r.user.data = NULL; if (!r.password.len) r.password.data = NULL; if (!r.host.len) r.host.data = NULL; if (!r.port.len) r.port.data = NULL; if (!r.path.len) r.path.data = NULL; if (!r.query.len) r.query.data = NULL; if (!r.target.len) r.target.data = NULL; return r; }
/* ***************************************************************************** URL address parsing ***************************************************************************** */ /** * Parses the URI returning it's components and their lengths (no decoding * performed, doesn't accept decoded URIs). * * The returned string are NOT NUL terminated, they are merely locations within * the original string. * * This function expects any of the following formats: * * * `/complete_path?query#target` * * i.e.: /index.html?page=1#list * * * `host:port/complete_path?query#target` * * i.e.: * example.com/index.html * example.com:8080/index.html * * * `schema://user:password@host:port/path?query#target` * * i.e.: http://example.com/index.html?page=1#list * * Invalid formats might produce unexpected results. No error testing performed. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L4811-L5027
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_channel_free
static void fio_channel_free(channel_s *ch) { if (!ch) return; if (fio_atomic_sub(&ch->ref, 1)) return; free(ch); }
/** Frees a channel (reference counting). */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L5158-L5164
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_channel_dup
static void fio_channel_dup(channel_s *ch) { if (!ch) return; fio_atomic_add(&ch->ref, 1); }
/** Increases a channel's reference count. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L5166-L5170
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_channel_cmp
static int fio_channel_cmp(channel_s *ch1, channel_s *ch2) { return ch1->name_len == ch2->name_len && ch1->match == ch2->match && !memcmp(ch1->name, ch2->name, ch1->name_len); }
/** Tests if two channels are equal. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L5172-L5175
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_postoffice_meta_copy_new
static fio_meta_ary_s fio_postoffice_meta_copy_new(void) { fio_meta_ary_s t = FIO_ARY_INIT; if (!fio_meta_ary_count(&fio_postoffice.meta.ary)) { return t; } fio_lock(&fio_postoffice.meta.lock); fio_meta_ary_concat(&t, &fio_postoffice.meta.ary); fio_unlock(&fio_postoffice.meta.lock); return t; }
/* ***************************************************************************** Internal message object creation ***************************************************************************** */ /** returns a temporary fio_meta_ary_s with a copy of the metadata array */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L5251-L5260
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_postoffice_meta_copy_free
static inline void fio_postoffice_meta_copy_free(fio_meta_ary_s *cpy) { fio_meta_ary_free(cpy); }
/** frees a temporary copy created by postoffice_meta_copy_new */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L5263-L5265
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_msg_internal_finalize
static inline void fio_msg_internal_finalize(fio_msg_internal_s *m) { if (!m->channel.len) m->channel.data = NULL; if (!m->data.len) m->data.data = NULL; }
/** frees the internal message data */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L5321-L5326
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_msg_internal_free
static inline void fio_msg_internal_free(fio_msg_internal_s *m) { if (fio_atomic_sub(&m->ref, 1)) return; while (m->meta_len) { --m->meta_len; if (m->meta[m->meta_len].on_finish) { fio_msg_s tmp_msg = { .channel = m->channel, .msg = m->data, }; m->meta[m->meta_len].on_finish(&tmp_msg, m->meta[m->meta_len].metadata); } } fio_free(m); }
/** frees the internal message data */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L5329-L5343
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_msg_internal_send_dup
static inline ssize_t fio_msg_internal_send_dup(intptr_t uuid, fio_msg_internal_s *m) { return fio_write2(uuid, .data.buffer = fio_msg_internal_dup(m), .offset = (sizeof(*m) + (m->meta_len * sizeof(*m->meta))), .length = 16 + m->data.len + m->channel.len + 2, .after.dealloc = fio_msg_internal_free2); }
/** internal helper */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L5355-L5361
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_mock_on_message
static void fio_mock_on_message(fio_msg_s *msg) { (void)msg; }
/** * A mock pub/sub callback for external subscriptions. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L5366-L5366
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_subscription_free
static inline void fio_subscription_free(subscription_s *s) { if (fio_atomic_sub(&s->ref, 1)) { return; } if (s->on_unsubscribe) { s->on_unsubscribe(s->udata1, s->udata2); } fio_channel_free(s->parent); fio_free(s); }
/* to be used for reference counting (subtructing) */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L5437-L5446
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_unsubscribe
void fio_unsubscribe(subscription_s *s) { if (!s) return; if (fio_trylock(&s->unsubscribed)) goto finish; fio_lock(&s->lock); channel_s *ch = s->parent; uint8_t removed = 0; fio_lock(&ch->lock); fio_ls_embd_remove(&s->node); /* check if channel is done for */ if (fio_ls_embd_is_empty(&ch->subscriptions)) { fio_collection_s *c = ch->parent; uint64_t hashed = FIO_HASH_FN( ch->name, ch->name_len, &fio_postoffice.pubsub, &fio_postoffice.pubsub); /* lock collection */ fio_lock(&c->lock); /* test again within lock */ if (fio_ls_embd_is_empty(&ch->subscriptions)) { fio_ch_set_remove(&c->channels, hashed, ch, NULL); removed = (c != &fio_postoffice.filters); } fio_unlock(&c->lock); } fio_unlock(&ch->lock); if (removed) { fio_pubsub_on_channel_destroy(ch); } /* promise the subscription will be inactive */ s->on_message = NULL; fio_unlock(&s->lock); finish: fio_subscription_free(s); }
/** Unsubscribes from a filter, pub/sub channle or patten */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L5484-L5518
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_subscription_channel
fio_str_info_s fio_subscription_channel(subscription_s *subscription) { return (fio_str_info_s){.data = subscription->parent->name, .len = subscription->parent->name_len}; }
/** * This helper returns a temporary String with the subscription's channel (or a * string representing the filter). * * To keep the string beyond the lifetime of the subscription, copy the string. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L5526-L5529
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_pubsub_on_channel_create
static void fio_pubsub_on_channel_create(channel_s *ch) { fio_lock(&fio_postoffice.engines.lock); FIO_SET_FOR_LOOP(&fio_postoffice.engines.set, pos) { if (!pos->hash) continue; pos->obj->subscribe(pos->obj, (fio_str_info_s){.data = ch->name, .len = ch->name_len}, ch->match); } fio_unlock(&fio_postoffice.engines.lock); fio_cluster_inform_root_about_channel(ch, 1); }
/* runs in lock(!) let'm all know */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L5540-L5551
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_pubsub_on_channel_destroy
static void fio_pubsub_on_channel_destroy(channel_s *ch) { fio_lock(&fio_postoffice.engines.lock); FIO_SET_FOR_LOOP(&fio_postoffice.engines.set, pos) { if (!pos->hash) continue; pos->obj->unsubscribe( pos->obj, (fio_str_info_s){.data = ch->name, .len = ch->name_len}, ch->match); } fio_unlock(&fio_postoffice.engines.lock); fio_cluster_inform_root_about_channel(ch, 0); }
/* runs in lock(!) let'm all know */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L5554-L5565
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_pubsub_attach
void fio_pubsub_attach(fio_pubsub_engine_s *engine) { fio_lock(&fio_postoffice.engines.lock); fio_engine_set_insert(&fio_postoffice.engines.set, (uintptr_t)engine, engine); fio_unlock(&fio_postoffice.engines.lock); fio_pubsub_reattach(engine); }
/** * Attaches an engine, so it's callback can be called by facil.io. * * The `subscribe` callback will be called for every existing channel. * * NOTE: the root (master) process will call `subscribe` for any channel in any * process, while all the other processes will call `subscribe` only for their * own channels. This allows engines to use the root (master) process as an * exclusive subscription process. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L5577-L5582
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_pubsub_detach
void fio_pubsub_detach(fio_pubsub_engine_s *engine) { fio_lock(&fio_postoffice.engines.lock); fio_engine_set_remove(&fio_postoffice.engines.set, (uintptr_t)engine, engine, NULL); fio_unlock(&fio_postoffice.engines.lock); }
/** Detaches an engine, so it could be safely destroyed. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L5585-L5590
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_pubsub_is_attached
int fio_pubsub_is_attached(fio_pubsub_engine_s *engine) { fio_pubsub_engine_s *addr; fio_lock(&fio_postoffice.engines.lock); addr = fio_engine_set_find(&fio_postoffice.engines.set, (uintptr_t)engine, engine); fio_unlock(&fio_postoffice.engines.lock); return addr != NULL; }
/** Returns true (1) if the engine is attached to the system. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L5593-L5600
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_pubsub_reattach
void fio_pubsub_reattach(fio_pubsub_engine_s *eng) { fio_lock(&fio_postoffice.pubsub.lock); FIO_SET_FOR_LOOP(&fio_postoffice.pubsub.channels, pos) { if (!pos->hash) continue; eng->subscribe( eng, (fio_str_info_s){.data = pos->obj->name, .len = pos->obj->name_len}, NULL); } fio_unlock(&fio_postoffice.pubsub.lock); fio_lock(&fio_postoffice.patterns.lock); FIO_SET_FOR_LOOP(&fio_postoffice.patterns.channels, pos) { if (!pos->hash) continue; eng->subscribe( eng, (fio_str_info_s){.data = pos->obj->name, .len = pos->obj->name_len}, pos->obj->match); } fio_unlock(&fio_postoffice.patterns.lock); }
/** * Engines can ask facil.io to call the `subscribe` callback for all active * channels. * * This allows engines that lost their connection to their Pub/Sub service to * resubscribe all the currently active channels with the new connection. * * CAUTION: This is an evented task... try not to free the engine's memory while * resubscriptions are under way... * * NOTE: the root (master) process will call `subscribe` for any channel in any * process, while all the other processes will call `subscribe` only for their * own channels. This allows engines to use the root (master) process as an * exclusive subscription process. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L5617-L5638
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_message_metadata_callback_set
void fio_message_metadata_callback_set(fio_msg_metadata_fn callback, int enable) { if (!callback) return; fio_lock(&fio_postoffice.meta.lock); fio_meta_ary_remove2(&fio_postoffice.meta.ary, callback, NULL); if (enable) fio_meta_ary_push(&fio_postoffice.meta.ary, callback); fio_unlock(&fio_postoffice.meta.lock); }
/* ***************************************************************************** * Message Metadata handling **************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L5644-L5653
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_message_defer
void fio_message_defer(fio_msg_s *msg_) { fio_msg_client_s *cl = (fio_msg_client_s *)msg_; cl->marker = 1; }
/* defers the callback (mark only) */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L5705-L5708
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_perform_subscription_callback
static void fio_perform_subscription_callback(void *s_, void *msg_) { subscription_s *s = s_; if (fio_trylock(&s->lock)) { fio_defer_push_task(fio_perform_subscription_callback, s_, msg_); return; } fio_msg_internal_s *msg = (fio_msg_internal_s *)msg_; fio_msg_client_s m = { .msg = { .channel = msg->channel, .msg = msg->data, .filter = msg->filter, .udata1 = s->udata1, .udata2 = s->udata2, }, .meta_len = msg->meta_len, .meta = msg->meta, .marker = 0, }; if (s->on_message) { /* the on_message callback is removed when a subscription is canceled. */ s->on_message(&m.msg); } fio_unlock(&s->lock); if (m.marker) { fio_defer_push_task(fio_perform_subscription_callback, s_, msg_); return; } fio_msg_internal_free(msg); fio_subscription_free(s); }
/* performs the actual callback */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L5711-L5742
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_publish2channel
static void fio_publish2channel(channel_s *ch, fio_msg_internal_s *msg) { FIO_LS_EMBD_FOR(&ch->subscriptions, pos) { subscription_s *s = FIO_LS_EMBD_OBJ(subscription_s, node, pos); if (!s || s->on_message == fio_mock_on_message) { continue; } fio_atomic_add(&s->ref, 1); fio_atomic_add(&msg->ref, 1); fio_defer_push_task(fio_perform_subscription_callback, s, msg); } fio_msg_internal_free(msg); }
/** UNSAFE! publishes a message to a channel, managing the reference counts */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L5745-L5756
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_publish2process
static void fio_publish2process(fio_msg_internal_s *m) { fio_msg_internal_finalize(m); channel_s *ch; if (m->filter) { ch = fio_filter_find_dup(m->filter); if (!ch) { goto finish; } } else { ch = fio_channel_find_dup(m->channel); } /* exact match */ if (ch) { fio_defer_push_urgent(fio_publish2channel_task, ch, fio_msg_internal_dup(m)); } if (m->filter == 0) { /* pattern matching match */ fio_lock(&fio_postoffice.patterns.lock); FIO_SET_FOR_LOOP(&fio_postoffice.patterns.channels, p) { if (!p->hash) { continue; } if (p->obj->match( (fio_str_info_s){.data = p->obj->name, .len = p->obj->name_len}, m->channel)) { fio_channel_dup(p->obj); fio_defer_push_urgent(fio_publish2channel_task, p->obj, fio_msg_internal_dup(m)); } } fio_unlock(&fio_postoffice.patterns.lock); } finish: fio_msg_internal_free(m); }
/** Publishes the message to the current process and frees the strings. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L5774-L5810
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_cluster_protocol_free
static inline void fio_cluster_protocol_free(void *pr) { fio_free(pr); }
/* ***************************************************************************** * Cluster Protocol callbacks **************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L5921-L5921
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_cluster_server_sender
static void fio_cluster_server_sender(void *m_, intptr_t avoid_uuid) { fio_msg_internal_s *m = m_; fio_lock(&cluster_data.lock); FIO_LS_FOR(&cluster_data.clients, pos) { if ((intptr_t)pos->obj != -1) { if ((intptr_t)pos->obj != avoid_uuid) { fio_msg_internal_send_dup((intptr_t)pos->obj, m); } } } fio_unlock(&cluster_data.lock); fio_msg_internal_free(m); }
/* ***************************************************************************** * Master (server) IPC Connections **************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L6086-L6098
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_cluster_listen_accept
static void fio_cluster_listen_accept(intptr_t uuid, fio_protocol_s *protocol) { (void)protocol; /* prevent `accept` backlog in parent */ intptr_t client; while ((client = fio_accept(uuid)) != -1) { fio_attach(client, fio_cluster_protocol_alloc(client, fio_cluster_server_handler, fio_cluster_server_sender)); fio_lock(&cluster_data.lock); fio_ls_push(&cluster_data.clients, (void *)client); fio_unlock(&cluster_data.lock); } }
/** Called when a ne client is available */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L6185-L6197
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_cluster_listen_on_close
static void fio_cluster_listen_on_close(intptr_t uuid, fio_protocol_s *protocol) { free(protocol); cluster_data.uuid = -1; if (fio_parent_pid() == getpid()) { #if DEBUG FIO_LOG_DEBUG("(%d) stopped listening for cluster connections", (int)getpid()); #endif if (fio_data->active) fio_stop(); } (void)uuid; }
/** Called when the connection was closed, but will not run concurrently */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L6200-L6213
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_cluster_client_handler
static void fio_cluster_client_handler(struct cluster_pr_s *pr) { /* what to do? */ switch ((fio_cluster_message_type_e)pr->type) { case FIO_CLUSTER_MSG_FORWARD: /* fallthrough */ case FIO_CLUSTER_MSG_JSON: fio_publish2process(fio_msg_internal_dup(pr->msg)); break; case FIO_CLUSTER_MSG_SHUTDOWN: fio_stop(); case FIO_CLUSTER_MSG_ERROR: /* fallthrough */ case FIO_CLUSTER_MSG_PING: /* fallthrough */ case FIO_CLUSTER_MSG_ROOT: /* fallthrough */ case FIO_CLUSTER_MSG_ROOT_JSON: /* fallthrough */ case FIO_CLUSTER_MSG_PUBSUB_SUB: /* fallthrough */ case FIO_CLUSTER_MSG_PUBSUB_UNSUB: /* fallthrough */ case FIO_CLUSTER_MSG_PATTERN_SUB: /* fallthrough */ case FIO_CLUSTER_MSG_PATTERN_UNSUB: /* fallthrough */ default: break; } }
/* ***************************************************************************** * Worker (client) IPC connections **************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L6243-L6264
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_cluster_on_connect
static void fio_cluster_on_connect(intptr_t uuid, void *udata) { cluster_data.uuid = uuid; /* inform root about all existing channels */ fio_lock(&fio_postoffice.pubsub.lock); FIO_SET_FOR_LOOP(&fio_postoffice.pubsub.channels, pos) { if (!pos->hash) { continue; } fio_cluster_inform_root_about_channel(pos->obj, 1); } fio_unlock(&fio_postoffice.pubsub.lock); fio_lock(&fio_postoffice.patterns.lock); FIO_SET_FOR_LOOP(&fio_postoffice.patterns.channels, pos) { if (!pos->hash) { continue; } fio_cluster_inform_root_about_channel(pos->obj, 1); } fio_unlock(&fio_postoffice.patterns.lock); fio_attach(uuid, fio_cluster_protocol_alloc(uuid, fio_cluster_client_handler, fio_cluster_client_sender)); (void)udata; }
/** The address of the server we are connecting to. */ // char *address; /** The port on the server we are connecting to. */ // char *port; /** * The `on_connect` callback should return a pointer to a protocol object * that will handle any connection related events. * * Should either call `facil_attach` or close the connection. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L6287-L6311
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_cluster_on_fail
static void fio_cluster_on_fail(intptr_t uuid, void *udata) { FIO_LOG_FATAL("(facil.io) unknown cluster connection error"); perror(" errno"); kill(fio_parent_pid(), SIGINT); fio_stop(); // exit(errno ? errno : 1); (void)udata; (void)uuid; }
/** * The `on_fail` is called when a socket fails to connect. The old sock UUID * is passed along. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L6316-L6324
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_cluster_inform_root_about_channel
static inline void fio_cluster_inform_root_about_channel(channel_s *ch, int add) { if (!fio_data->is_worker || fio_data->workers == 1 || !cluster_data.uuid || !ch) return; fio_str_info_s ch_name = {.data = ch->name, .len = ch->name_len}; fio_str_info_s msg = {.data = NULL, .len = 0}; #if DEBUG FIO_LOG_DEBUG("(%d) informing root about: %s (%zu) msg type %d", (int)getpid(), ch_name.data, ch_name.len, (ch->match ? (add ? FIO_CLUSTER_MSG_PATTERN_SUB : FIO_CLUSTER_MSG_PATTERN_UNSUB) : (add ? FIO_CLUSTER_MSG_PUBSUB_SUB : FIO_CLUSTER_MSG_PUBSUB_UNSUB))); #endif char buf[8] = {0}; if (ch->match) { fio_u2str64(buf, (uint64_t)ch->match); msg.data = buf; msg.len = sizeof(ch->match); } fio_cluster_client_sender( fio_msg_internal_create(0, (ch->match ? (add ? FIO_CLUSTER_MSG_PATTERN_SUB : FIO_CLUSTER_MSG_PATTERN_UNSUB) : (add ? FIO_CLUSTER_MSG_PUBSUB_SUB : FIO_CLUSTER_MSG_PUBSUB_UNSUB)), ch_name, msg, 0, 1), -1); }
/* ***************************************************************************** * Propegation **************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L6357-L6388
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_accept_after_fork
static void fio_accept_after_fork(void *ignore) { /* prevent `accept` backlog in parent */ fio_cluster_listen_accept(cluster_data.uuid, NULL); (void)ignore; }
/* ***************************************************************************** * Initialization **************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L6394-L6398
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_pubsub_on_fork
static void fio_pubsub_on_fork(void) { fio_postoffice.filters.lock = FIO_LOCK_INIT; fio_postoffice.pubsub.lock = FIO_LOCK_INIT; fio_postoffice.patterns.lock = FIO_LOCK_INIT; fio_postoffice.engines.lock = FIO_LOCK_INIT; fio_postoffice.meta.lock = FIO_LOCK_INIT; cluster_data.lock = FIO_LOCK_INIT; cluster_data.uuid = 0; FIO_SET_FOR_LOOP(&fio_postoffice.filters.channels, pos) { if (!pos->hash) continue; pos->obj->lock = FIO_LOCK_INIT; FIO_LS_EMBD_FOR(&pos->obj->subscriptions, n) { FIO_LS_EMBD_OBJ(subscription_s, node, n)->lock = FIO_LOCK_INIT; } } FIO_SET_FOR_LOOP(&fio_postoffice.pubsub.channels, pos) { if (!pos->hash) continue; pos->obj->lock = FIO_LOCK_INIT; FIO_LS_EMBD_FOR(&pos->obj->subscriptions, n) { FIO_LS_EMBD_OBJ(subscription_s, node, n)->lock = FIO_LOCK_INIT; } } FIO_SET_FOR_LOOP(&fio_postoffice.patterns.channels, pos) { if (!pos->hash) continue; pos->obj->lock = FIO_LOCK_INIT; FIO_LS_EMBD_FOR(&pos->obj->subscriptions, n) { FIO_LS_EMBD_OBJ(subscription_s, node, n)->lock = FIO_LOCK_INIT; } } }
/* ***************************************************************************** Cluster forking handler ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L6465-L6497
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_cluster_signal_children
static void fio_cluster_signal_children(void) { if (fio_parent_pid() != getpid()) { fio_stop(); return; } fio_cluster_server_sender(fio_msg_internal_create(0, FIO_CLUSTER_MSG_SHUTDOWN, (fio_str_info_s){.len = 0}, (fio_str_info_s){.len = 0}, 0, 1), -1); }
/* ***************************************************************************** * External API **************************************************************************** */ /** Signals children (or self) to shutdown) - NOT signal safe. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L6504-L6514
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
FIO_IGNORE_MACRO
void fio_publish FIO_IGNORE_MACRO(fio_publish_args_s args) { if (args.filter && !args.engine) { args.engine = FIO_PUBSUB_CLUSTER; } else if (!args.engine) { args.engine = FIO_PUBSUB_DEFAULT; } fio_msg_internal_s *m = NULL; switch ((uintptr_t)args.engine) { case 0UL: /* fallthrough (missing default) */ case 1UL: // ((uintptr_t)FIO_PUBSUB_CLUSTER): m = fio_msg_internal_create( args.filter, (args.is_json ? FIO_CLUSTER_MSG_JSON : FIO_CLUSTER_MSG_FORWARD), args.channel, args.message, args.is_json, 1); fio_send2cluster(m); fio_publish2process(m); break; case 2UL: // ((uintptr_t)FIO_PUBSUB_PROCESS): m = fio_msg_internal_create(args.filter, 0, args.channel, args.message, args.is_json, 1); fio_publish2process(m); break; case 3UL: // ((uintptr_t)FIO_PUBSUB_SIBLINGS): m = fio_msg_internal_create( args.filter, (args.is_json ? FIO_CLUSTER_MSG_JSON : FIO_CLUSTER_MSG_FORWARD), args.channel, args.message, args.is_json, 1); fio_send2cluster(m); fio_msg_internal_free(m); m = NULL; break; case 4UL: // ((uintptr_t)FIO_PUBSUB_ROOT): m = fio_msg_internal_create( args.filter, (args.is_json ? FIO_CLUSTER_MSG_ROOT_JSON : FIO_CLUSTER_MSG_ROOT), args.channel, args.message, args.is_json, 1); if (fio_data->is_worker == 0 || fio_data->workers == 1) { fio_publish2process(m); } else { fio_cluster_client_sender(m, -1); } break; default: if (args.filter != 0) { FIO_LOG_ERROR("(pub/sub) pub/sub engines can only be used for " "pub/sub messages (no filter)."); return; } args.engine->publish(args.engine, args.channel, args.message, args.is_json); } return; }
/** * Publishes a message to the relevant subscribers (if any). * * See `facil_publish_args_s` for details. * * By default the message is sent using the FIO_PUBSUB_CLUSTER engine (all * processes, including the calling process). * * To limit the message only to other processes (exclude the calling process), * use the FIO_PUBSUB_SIBLINGS engine. * * To limit the message only to the calling process, use the * FIO_PUBSUB_PROCESS engine. * * To publish messages to the pub/sub layer, the `.filter` argument MUST be * equal to 0 or missing. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L6535-L6586
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_glob_match
static int fio_glob_match(fio_str_info_s pat, fio_str_info_s ch) { /* adapted and rewritten, with thankfulness, from the code at: * https://github.com/opnfv/kvmfornfv/blob/master/kernel/lib/glob.c * * Original version's copyright: * Copyright 2015 Open Platform for NFV Project, Inc. and its contributors * Under the MIT license. */ /* * Backtrack to previous * on mismatch and retry starting one * character later in the string. Because * matches all characters, * there's never a need to backtrack multiple levels. */ uint8_t *back_pat = NULL, *back_str = (uint8_t *)ch.data; size_t back_pat_len = 0, back_str_len = ch.len; /* * Loop over each token (character or class) in pat, matching * it against the remaining unmatched tail of str. Return false * on mismatch, or true after matching the trailing nul bytes. */ while (ch.len) { uint8_t c = *(uint8_t *)ch.data++; uint8_t d = *(uint8_t *)pat.data++; ch.len--; pat.len--; switch (d) { case '?': /* Wildcard: anything goes */ break; case '*': /* Any-length wildcard */ if (!pat.len) /* Optimize trailing * case */ return 1; back_pat = (uint8_t *)pat.data; back_pat_len = pat.len; back_str = (uint8_t *)--ch.data; /* Allow zero-length match */ back_str_len = ++ch.len; break; case '[': { /* Character class */ uint8_t match = 0, inverted = (*(uint8_t *)pat.data == '^'); uint8_t *cls = (uint8_t *)pat.data + inverted; uint8_t a = *cls++; /* * Iterate over each span in the character class. * A span is either a single character a, or a * range a-b. The first span may begin with ']'. */ do { uint8_t b = a; if (cls[0] == '-' && cls[1] != ']') { b = cls[1]; cls += 2; if (a > b) { uint8_t tmp = a; a = b; b = tmp; } } match |= (a <= c && c <= b); } while ((a = *cls++) != ']'); if (match == inverted) goto backtrack; pat.len -= cls - (uint8_t *)pat.data; pat.data = (char *)cls; } break; case '\\': d = *(uint8_t *)pat.data++; pat.len--; /* fallthrough */ default: /* Literal character */ if (c == d) break; backtrack: if (!back_pat) return 0; /* No point continuing */ /* Try again from last *, one character later in str. */ pat.data = (char *)back_pat; ch.data = (char *)++back_str; ch.len = --back_str_len; pat.len = back_pat_len; } } return !ch.len && !pat.len; }
/* ***************************************************************************** * Glob Matching **************************************************************************** */ /** A binary glob matching helper. Returns 1 on match, otherwise returns 0. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L6593-L6684
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_pubsub_on_fork
static void fio_pubsub_on_fork(void) {}
/* FIO_PUBSUB_SUPPORT */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L6690-L6690
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_memcpy
static inline void fio_memcpy(void *__restrict dest_, void *__restrict src_, size_t units) { #if __SIZEOF_INT128__ == 9 /* a 128bit type exists... but tests favor 64bit */ register __uint128_t *dest = dest_; register __uint128_t *src = src_; #elif SIZE_MAX == 0xFFFFFFFFFFFFFFFF /* 64 bit size_t */ register size_t *dest = dest_; register size_t *src = src_; units = units << 1; #elif SIZE_MAX == 0xFFFFFFFF /* 32 bit size_t */ register size_t *dest = dest_; register size_t *src = src_; units = units << 2; #else /* unknow... assume 16 bit? */ register size_t *dest = dest_; register size_t *src = src_; units = units << 3; #endif while (units >= 16) { /* unroll loop */ dest[0] = src[0]; dest[1] = src[1]; dest[2] = src[2]; dest[3] = src[3]; dest[4] = src[4]; dest[5] = src[5]; dest[6] = src[6]; dest[7] = src[7]; dest[8] = src[8]; dest[9] = src[9]; dest[10] = src[10]; dest[11] = src[11]; dest[12] = src[12]; dest[13] = src[13]; dest[14] = src[14]; dest[15] = src[15]; dest += 16; src += 16; units -= 16; } switch (units) { case 15: *(dest++) = *(src++); /* fallthrough */ case 14: *(dest++) = *(src++); /* fallthrough */ case 13: *(dest++) = *(src++); /* fallthrough */ case 12: *(dest++) = *(src++); /* fallthrough */ case 11: *(dest++) = *(src++); /* fallthrough */ case 10: *(dest++) = *(src++); /* fallthrough */ case 9: *(dest++) = *(src++); /* fallthrough */ case 8: *(dest++) = *(src++); /* fallthrough */ case 7: *(dest++) = *(src++); /* fallthrough */ case 6: *(dest++) = *(src++); /* fallthrough */ case 5: *(dest++) = *(src++); /* fallthrough */ case 4: *(dest++) = *(src++); /* fallthrough */ case 3: *(dest++) = *(src++); /* fallthrough */ case 2: *(dest++) = *(src++); /* fallthrough */ case 1: *(dest++) = *(src++); } }
/* ***************************************************************************** Memory Copying by 16 byte units ***************************************************************************** */ /** used internally, only when memory addresses are known to be aligned */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L6811-L6882
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
sys_free
static inline void sys_free(void *mem, size_t len) { munmap(mem, len); }
/* frees memory using `munmap`. requires exact, page aligned, `len` */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L6937-L6937
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
sys_round_size
static inline size_t sys_round_size(size_t size) { return (size & (~4095)) + (4096 * (!!(size & 4095))); }
/** Rounds up any size to the nearest page alignment (assumes 4096 bytes per * page) */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L6971-L6973
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_malloc_after_fork
void fio_malloc_after_fork(void) { arena_last_used = NULL; if (!arenas) { return; } memory.lock = FIO_LOCK_INIT; memory.forked = 1; for (size_t i = 0; i < memory.cores; ++i) { arenas[i].lock = FIO_LOCK_INIT; } }
/** Clears any memory locks, in case of a system call to `fork`. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L7082-L7092
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
block_init_root
static inline void block_init_root(block_s *blk, block_s *parent) { *blk = (block_s){ .parent = parent, .ref = 1, .pos = FIO_MEMORY_BLOCK_START_POS, .root_ref = 1, }; }
/* ***************************************************************************** Block management / allocation ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L7098-L7105
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
block_init
static inline void block_init(block_s *blk) { /* initialization shouldn't effect `parent` or `root_ref`*/ blk->ref = 1; blk->pos = FIO_MEMORY_BLOCK_START_POS; /* zero out linked list memory (everything else is already zero) */ ((block_node_s *)blk)->node.next = NULL; ((block_node_s *)blk)->node.prev = NULL; /* bump parent reference count */ fio_atomic_add(&blk->parent->root_ref, 1); }
/* intializes the block header for an available block of memory. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L7108-L7117
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
block_free
static inline void block_free(block_s *blk) { if (fio_atomic_sub(&blk->ref, 1)) return; memset(blk + 1, 0, (FIO_MEMORY_BLOCK_SIZE - sizeof(*blk))); fio_lock(&memory.lock); fio_ls_embd_push(&memory.available, &((block_node_s *)blk)->node); blk = blk->parent; if (fio_atomic_sub(&blk->root_ref, 1)) { fio_unlock(&memory.lock); return; } // fio_unlock(&memory.lock); // return; /* remove all of the root block's children (slices) from the memory pool */ for (size_t i = 0; i < FIO_MEMORY_BLOCKS_PER_ALLOCATION; ++i) { block_node_s *pos = (block_node_s *)((uintptr_t)blk + (i * FIO_MEMORY_BLOCK_SIZE)); fio_ls_embd_remove(&pos->node); } fio_unlock(&memory.lock); sys_free(blk, FIO_MEMORY_BLOCK_SIZE * FIO_MEMORY_BLOCKS_PER_ALLOCATION); FIO_LOG_DEBUG("memory allocator returned %p to the system", (void *)blk); FIO_MEMORY_ON_BLOCK_FREE(); }
/* intializes the block header for an available block of memory. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L7120-L7148
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
block_slice_free
static inline void block_slice_free(void *mem) { /* locate block boundary */ block_s *blk = (block_s *)((uintptr_t)mem & (~FIO_MEMORY_BLOCK_MASK)); block_free(blk); }
/* handle's a bock's reference count - called without a lock */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L7216-L7220
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
big_free
static inline void big_free(void *ptr) { size_t *mem = (void *)(((uintptr_t)ptr) - 16); sys_free(mem, *mem); }
/* reads size header and frees memory back to the system */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L7239-L7242
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_rand64
uint64_t fio_rand64(void) { /* modeled after xoroshiro128+, by David Blackman and Sebastiano Vigna */ static __thread uint64_t s[2]; /* random state */ static __thread uint16_t c; /* seed counter */ const uint64_t P[] = {0x37701261ED6C16C7ULL, 0x764DBBB75F3B3E0DULL}; if (c++ == 0) { /* re-seed state every 65,536 requests */ #ifdef RUSAGE_SELF struct rusage rusage; getrusage(RUSAGE_SELF, &rusage); s[0] = fio_risky_hash(&rusage, sizeof(rusage), s[0]); s[1] = fio_risky_hash(&rusage, sizeof(rusage), s[0]); #else struct timespec clk; clock_gettime(CLOCK_REALTIME, &clk); s[0] = fio_risky_hash(&clk, sizeof(clk), s[0]); s[1] = fio_risky_hash(&clk, sizeof(clk), s[0]); #endif } s[0] += fio_lrot64(s[0], 33) * P[0]; s[1] += fio_lrot64(s[1], 33) * P[1]; return fio_lrot64(s[0], 31) + fio_lrot64(s[1], 29); }
/* ***************************************************************************** Random Generator Functions ***************************************************************************** */ /* tested for randomness using code from: http://xoshiro.di.unimi.it/hwd.php */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L7461-L7483
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_rand_bytes
void fio_rand_bytes(void *data_, size_t len) { if (!data_ || !len) return; uint8_t *data = data_; /* unroll 32 bytes / 256 bit writes */ for (size_t i = (len >> 5); i; --i) { const uint64_t t0 = fio_rand64(); const uint64_t t1 = fio_rand64(); const uint64_t t2 = fio_rand64(); const uint64_t t3 = fio_rand64(); fio_u2str64(data, t0); fio_u2str64(data + 8, t1); fio_u2str64(data + 16, t2); fio_u2str64(data + 24, t3); data += 32; } uint64_t tmp; /* 64 bit steps */ switch (len & 24) { case 24: tmp = fio_rand64(); fio_u2str64(data + 16, tmp); /* fallthrough */ case 16: tmp = fio_rand64(); fio_u2str64(data + 8, tmp); /* fallthrough */ case 8: tmp = fio_rand64(); fio_u2str64(data, tmp); data += len & 24; } if ((len & 7)) { tmp = fio_rand64(); /* leftover bytes */ switch ((len & 7)) { case 7: data[6] = (tmp >> 8) & 0xFF; /* fallthrough */ case 6: data[5] = (tmp >> 16) & 0xFF; /* fallthrough */ case 5: data[4] = (tmp >> 24) & 0xFF; /* fallthrough */ case 4: data[3] = (tmp >> 32) & 0xFF; /* fallthrough */ case 3: data[2] = (tmp >> 40) & 0xFF; /* fallthrough */ case 2: data[1] = (tmp >> 48) & 0xFF; /* fallthrough */ case 1: data[0] = (tmp >> 56) & 0xFF; } } }
/* copies 64 bits of randomness (8 bytes) repeatedly... */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L7486-L7544
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_sha1_perform_all_rounds
static inline void fio_sha1_perform_all_rounds(fio_sha1_s *s, const uint8_t *buffer) { /* collect data */ uint32_t a = s->digest.i[0]; uint32_t b = s->digest.i[1]; uint32_t c = s->digest.i[2]; uint32_t d = s->digest.i[3]; uint32_t e = s->digest.i[4]; uint32_t t, w[16]; /* copy data to words, performing byte swapping as needed */ w[0] = fio_str2u32(buffer); w[1] = fio_str2u32(buffer + 4); w[2] = fio_str2u32(buffer + 8); w[3] = fio_str2u32(buffer + 12); w[4] = fio_str2u32(buffer + 16); w[5] = fio_str2u32(buffer + 20); w[6] = fio_str2u32(buffer + 24); w[7] = fio_str2u32(buffer + 28); w[8] = fio_str2u32(buffer + 32); w[9] = fio_str2u32(buffer + 36); w[10] = fio_str2u32(buffer + 40); w[11] = fio_str2u32(buffer + 44); w[12] = fio_str2u32(buffer + 48); w[13] = fio_str2u32(buffer + 52); w[14] = fio_str2u32(buffer + 56); w[15] = fio_str2u32(buffer + 60); /* perform rounds */ #undef perform_single_round #define perform_single_round(num) \ t = fio_lrot32(a, 5) + e + w[num] + ((b & c) | ((~b) & d)) + 0x5A827999; \ e = d; \ d = c; \ c = fio_lrot32(b, 30); \ b = a; \ a = t; #define perform_four_rounds(i) \ perform_single_round(i); \ perform_single_round(i + 1); \ perform_single_round(i + 2); \ perform_single_round(i + 3); perform_four_rounds(0); perform_four_rounds(4); perform_four_rounds(8); perform_four_rounds(12); #undef perform_single_round #define perform_single_round(i) \ w[(i)&15] = fio_lrot32((w[(i - 3) & 15] ^ w[(i - 8) & 15] ^ \ w[(i - 14) & 15] ^ w[(i - 16) & 15]), \ 1); \ t = fio_lrot32(a, 5) + e + w[(i)&15] + ((b & c) | ((~b) & d)) + 0x5A827999; \ e = d; \ d = c; \ c = fio_lrot32(b, 30); \ b = a; \ a = t; perform_four_rounds(16); #undef perform_single_round #define perform_single_round(i) \ w[(i)&15] = fio_lrot32((w[(i - 3) & 15] ^ w[(i - 8) & 15] ^ \ w[(i - 14) & 15] ^ w[(i - 16) & 15]), \ 1); \ t = fio_lrot32(a, 5) + e + w[(i)&15] + (b ^ c ^ d) + 0x6ED9EBA1; \ e = d; \ d = c; \ c = fio_lrot32(b, 30); \ b = a; \ a = t; perform_four_rounds(20); perform_four_rounds(24); perform_four_rounds(28); perform_four_rounds(32); perform_four_rounds(36); #undef perform_single_round #define perform_single_round(i) \ w[(i)&15] = fio_lrot32((w[(i - 3) & 15] ^ w[(i - 8) & 15] ^ \ w[(i - 14) & 15] ^ w[(i - 16) & 15]), \ 1); \ t = fio_lrot32(a, 5) + e + w[(i)&15] + ((b & (c | d)) | (c & d)) + \ 0x8F1BBCDC; \ e = d; \ d = c; \ c = fio_lrot32(b, 30); \ b = a; \ a = t; perform_four_rounds(40); perform_four_rounds(44); perform_four_rounds(48); perform_four_rounds(52); perform_four_rounds(56); #undef perform_single_round #define perform_single_round(i) \ w[(i)&15] = fio_lrot32((w[(i - 3) & 15] ^ w[(i - 8) & 15] ^ \ w[(i - 14) & 15] ^ w[(i - 16) & 15]), \ 1); \ t = fio_lrot32(a, 5) + e + w[(i)&15] + (b ^ c ^ d) + 0xCA62C1D6; \ e = d; \ d = c; \ c = fio_lrot32(b, 30); \ b = a; \ a = t; perform_four_rounds(60); perform_four_rounds(64); perform_four_rounds(68); perform_four_rounds(72); perform_four_rounds(76); /* store data */ s->digest.i[4] += e; s->digest.i[3] += d; s->digest.i[2] += c; s->digest.i[1] += b; s->digest.i[0] += a; }
/** Process the buffer once full. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L7693-L7813
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_sha1_init
fio_sha1_s fio_sha1_init(void) { return (fio_sha1_s){.digest.i[0] = 0x67452301, .digest.i[1] = 0xefcdab89, .digest.i[2] = 0x98badcfe, .digest.i[3] = 0x10325476, .digest.i[4] = 0xc3d2e1f0}; }
/** Initialize or reset the `sha1` object. This must be performed before hashing data using sha1. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L7819-L7825
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_sha1_write
void fio_sha1_write(fio_sha1_s *s, const void *data, size_t len) { size_t in_buffer = s->length & 63; size_t partial = 64 - in_buffer; s->length += len; if (partial > len) { memcpy(s->buffer + in_buffer, data, len); return; } if (in_buffer) { memcpy(s->buffer + in_buffer, data, partial); len -= partial; data = (void *)((uintptr_t)data + partial); fio_sha1_perform_all_rounds(s, s->buffer); } while (len >= 64) { fio_sha1_perform_all_rounds(s, data); data = (void *)((uintptr_t)data + 64); len -= 64; } if (len) { memcpy(s->buffer + in_buffer, data, len); } return; }
/** Writes data to the sha1 buffer. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L7830-L7853
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_sha2_perform_all_rounds
static inline void fio_sha2_perform_all_rounds(fio_sha2_s *s, const uint8_t *data) { if (s->type & 1) { /* 512 derived type */ // process values for the 64bit words uint64_t a = s->digest.i64[0]; uint64_t b = s->digest.i64[1]; uint64_t c = s->digest.i64[2]; uint64_t d = s->digest.i64[3]; uint64_t e = s->digest.i64[4]; uint64_t f = s->digest.i64[5]; uint64_t g = s->digest.i64[6]; uint64_t h = s->digest.i64[7]; uint64_t t1, t2, w[80]; w[0] = fio_str2u64(data); w[1] = fio_str2u64(data + 8); w[2] = fio_str2u64(data + 16); w[3] = fio_str2u64(data + 24); w[4] = fio_str2u64(data + 32); w[5] = fio_str2u64(data + 40); w[6] = fio_str2u64(data + 48); w[7] = fio_str2u64(data + 56); w[8] = fio_str2u64(data + 64); w[9] = fio_str2u64(data + 72); w[10] = fio_str2u64(data + 80); w[11] = fio_str2u64(data + 88); w[12] = fio_str2u64(data + 96); w[13] = fio_str2u64(data + 104); w[14] = fio_str2u64(data + 112); w[15] = fio_str2u64(data + 120); #undef perform_single_round #define perform_single_round(i) \ t1 = h + Eps1_64(e) + Ch(e, f, g) + sha2_512_words[i] + w[i]; \ t2 = Eps0_64(a) + Maj(a, b, c); \ h = g; \ g = f; \ f = e; \ e = d + t1; \ d = c; \ c = b; \ b = a; \ a = t1 + t2; #define perform_4rounds(i) \ perform_single_round(i); \ perform_single_round(i + 1); \ perform_single_round(i + 2); \ perform_single_round(i + 3); perform_4rounds(0); perform_4rounds(4); perform_4rounds(8); perform_4rounds(12); #undef perform_single_round #define perform_single_round(i) \ w[i] = Omg1_64(w[i - 2]) + w[i - 7] + Omg0_64(w[i - 15]) + w[i - 16]; \ t1 = h + Eps1_64(e) + Ch(e, f, g) + sha2_512_words[i] + w[i]; \ t2 = Eps0_64(a) + Maj(a, b, c); \ h = g; \ g = f; \ f = e; \ e = d + t1; \ d = c; \ c = b; \ b = a; \ a = t1 + t2; perform_4rounds(16); perform_4rounds(20); perform_4rounds(24); perform_4rounds(28); perform_4rounds(32); perform_4rounds(36); perform_4rounds(40); perform_4rounds(44); perform_4rounds(48); perform_4rounds(52); perform_4rounds(56); perform_4rounds(60); perform_4rounds(64); perform_4rounds(68); perform_4rounds(72); perform_4rounds(76); s->digest.i64[0] += a; s->digest.i64[1] += b; s->digest.i64[2] += c; s->digest.i64[3] += d; s->digest.i64[4] += e; s->digest.i64[5] += f; s->digest.i64[6] += g; s->digest.i64[7] += h; return; } else { // process values for the 32bit words uint32_t a = s->digest.i32[0]; uint32_t b = s->digest.i32[1]; uint32_t c = s->digest.i32[2]; uint32_t d = s->digest.i32[3]; uint32_t e = s->digest.i32[4]; uint32_t f = s->digest.i32[5]; uint32_t g = s->digest.i32[6]; uint32_t h = s->digest.i32[7]; uint32_t t1, t2, w[64]; w[0] = fio_str2u32(data); w[1] = fio_str2u32(data + 4); w[2] = fio_str2u32(data + 8); w[3] = fio_str2u32(data + 12); w[4] = fio_str2u32(data + 16); w[5] = fio_str2u32(data + 20); w[6] = fio_str2u32(data + 24); w[7] = fio_str2u32(data + 28); w[8] = fio_str2u32(data + 32); w[9] = fio_str2u32(data + 36); w[10] = fio_str2u32(data + 40); w[11] = fio_str2u32(data + 44); w[12] = fio_str2u32(data + 48); w[13] = fio_str2u32(data + 52); w[14] = fio_str2u32(data + 56); w[15] = fio_str2u32(data + 60); #undef perform_single_round #define perform_single_round(i) \ t1 = h + Eps1_32(e) + Ch(e, f, g) + sha2_256_words[i] + w[i]; \ t2 = Eps0_32(a) + Maj(a, b, c); \ h = g; \ g = f; \ f = e; \ e = d + t1; \ d = c; \ c = b; \ b = a; \ a = t1 + t2; perform_4rounds(0); perform_4rounds(4); perform_4rounds(8); perform_4rounds(12); #undef perform_single_round #define perform_single_round(i) \ w[i] = Omg1_32(w[i - 2]) + w[i - 7] + Omg0_32(w[i - 15]) + w[i - 16]; \ t1 = h + Eps1_32(e) + Ch(e, f, g) + sha2_256_words[i] + w[i]; \ t2 = Eps0_32(a) + Maj(a, b, c); \ h = g; \ g = f; \ f = e; \ e = d + t1; \ d = c; \ c = b; \ b = a; \ a = t1 + t2; perform_4rounds(16); perform_4rounds(20); perform_4rounds(24); perform_4rounds(28); perform_4rounds(32); perform_4rounds(36); perform_4rounds(40); perform_4rounds(44); perform_4rounds(48); perform_4rounds(52); perform_4rounds(56); perform_4rounds(60); s->digest.i32[0] += a; s->digest.i32[1] += b; s->digest.i32[2] += c; s->digest.i32[3] += d; s->digest.i32[4] += e; s->digest.i32[5] += f; s->digest.i32[6] += g; s->digest.i32[7] += h; } }
/** Process the buffer once full. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L7957-L8134
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_sha2_init
fio_sha2_s fio_sha2_init(fio_sha2_variant_e variant) { if (variant == SHA_256) { return (fio_sha2_s){ .type = SHA_256, .digest.i32[0] = 0x6a09e667, .digest.i32[1] = 0xbb67ae85, .digest.i32[2] = 0x3c6ef372, .digest.i32[3] = 0xa54ff53a, .digest.i32[4] = 0x510e527f, .digest.i32[5] = 0x9b05688c, .digest.i32[6] = 0x1f83d9ab, .digest.i32[7] = 0x5be0cd19, }; } else if (variant == SHA_384) { return (fio_sha2_s){ .type = SHA_384, .digest.i64[0] = 0xcbbb9d5dc1059ed8, .digest.i64[1] = 0x629a292a367cd507, .digest.i64[2] = 0x9159015a3070dd17, .digest.i64[3] = 0x152fecd8f70e5939, .digest.i64[4] = 0x67332667ffc00b31, .digest.i64[5] = 0x8eb44a8768581511, .digest.i64[6] = 0xdb0c2e0d64f98fa7, .digest.i64[7] = 0x47b5481dbefa4fa4, }; } else if (variant == SHA_512) { return (fio_sha2_s){ .type = SHA_512, .digest.i64[0] = 0x6a09e667f3bcc908, .digest.i64[1] = 0xbb67ae8584caa73b, .digest.i64[2] = 0x3c6ef372fe94f82b, .digest.i64[3] = 0xa54ff53a5f1d36f1, .digest.i64[4] = 0x510e527fade682d1, .digest.i64[5] = 0x9b05688c2b3e6c1f, .digest.i64[6] = 0x1f83d9abfb41bd6b, .digest.i64[7] = 0x5be0cd19137e2179, }; } else if (variant == SHA_224) { return (fio_sha2_s){ .type = SHA_224, .digest.i32[0] = 0xc1059ed8, .digest.i32[1] = 0x367cd507, .digest.i32[2] = 0x3070dd17, .digest.i32[3] = 0xf70e5939, .digest.i32[4] = 0xffc00b31, .digest.i32[5] = 0x68581511, .digest.i32[6] = 0x64f98fa7, .digest.i32[7] = 0xbefa4fa4, }; } else if (variant == SHA_512_224) { return (fio_sha2_s){ .type = SHA_512_224, .digest.i64[0] = 0x8c3d37c819544da2, .digest.i64[1] = 0x73e1996689dcd4d6, .digest.i64[2] = 0x1dfab7ae32ff9c82, .digest.i64[3] = 0x679dd514582f9fcf, .digest.i64[4] = 0x0f6d2b697bd44da8, .digest.i64[5] = 0x77e36f7304c48942, .digest.i64[6] = 0x3f9d85a86a1d36c8, .digest.i64[7] = 0x1112e6ad91d692a1, }; } else if (variant == SHA_512_256) { return (fio_sha2_s){ .type = SHA_512_256, .digest.i64[0] = 0x22312194fc2bf72c, .digest.i64[1] = 0x9f555fa3c84c64c2, .digest.i64[2] = 0x2393b86b6f53b151, .digest.i64[3] = 0x963877195940eabd, .digest.i64[4] = 0x96283ee2a88effe3, .digest.i64[5] = 0xbe5e1e2553863992, .digest.i64[6] = 0x2b0199fc2c85b8aa, .digest.i64[7] = 0x0eb72ddc81c52ca2, }; } FIO_LOG_FATAL("SHA-2 ERROR - variant unknown"); exit(2); }
/** Initialize/reset the SHA-2 object. SHA-2 is actually a family of functions with different variants. When initializing the SHA-2 container, you must select the variant you intend to apply. The following are valid options (see the fio_sha2_variant_e enum): - SHA_512 (== 0) - SHA_384 - SHA_512_224 - SHA_512_256 - SHA_256 - SHA_224 */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L8151-L8227
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_sha2_write
void fio_sha2_write(fio_sha2_s *s, const void *data, size_t len) { size_t in_buffer; size_t partial; if (s->type & 1) { /* 512 type derived */ in_buffer = s->length.words[0] & 127; if (s->length.words[0] + len < s->length.words[0]) { /* we are at wraping around the 64bit limit */ s->length.words[1] = (s->length.words[1] << 1) | 1; } s->length.words[0] += len; partial = 128 - in_buffer; if (partial > len) { memcpy(s->buffer + in_buffer, data, len); return; } if (in_buffer) { memcpy(s->buffer + in_buffer, data, partial); len -= partial; data = (void *)((uintptr_t)data + partial); fio_sha2_perform_all_rounds(s, s->buffer); } while (len >= 128) { fio_sha2_perform_all_rounds(s, data); data = (void *)((uintptr_t)data + 128); len -= 128; } if (len) { memcpy(s->buffer + in_buffer, data, len); } return; } /* else... NOT 512 bits derived (64bit base) */ in_buffer = s->length.words[0] & 63; partial = 64 - in_buffer; s->length.words[0] += len; if (partial > len) { memcpy(s->buffer + in_buffer, data, len); return; } if (in_buffer) { memcpy(s->buffer + in_buffer, data, partial); len -= partial; data = (void *)((uintptr_t)data + partial); fio_sha2_perform_all_rounds(s, s->buffer); } while (len >= 64) { fio_sha2_perform_all_rounds(s, data); data = (void *)((uintptr_t)data + 64); len -= 64; } if (len) { memcpy(s->buffer + in_buffer, data, len); } return; }
/** Writes data to the SHA-2 buffer. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L8232-L8290
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_base64_encode_internal
static inline int fio_base64_encode_internal(char *target, const char *data, int len, const char *base64_encodes) { /* walk backwards, allowing fo inplace decoding (target == data) */ int groups = len / 3; const int mod = len - (groups * 3); const int target_size = (groups + (mod != 0)) * 4; char *writer = target + target_size - 1; const char *reader = data + len - 1; writer[1] = 0; switch (mod) { case 2: { char tmp2 = *(reader--); char tmp1 = *(reader--); *(writer--) = '='; *(writer--) = base64_encodes[((tmp2 & 15) << 2)]; *(writer--) = base64_encodes[((tmp1 & 3) << 4) | ((tmp2 >> 4) & 15)]; *(writer--) = base64_encodes[(tmp1 >> 2) & 63]; } break; case 1: { char tmp1 = *(reader--); *(writer--) = '='; *(writer--) = '='; *(writer--) = base64_encodes[(tmp1 & 3) << 4]; *(writer--) = base64_encodes[(tmp1 >> 2) & 63]; } break; } while (groups) { groups--; const char tmp3 = *(reader--); const char tmp2 = *(reader--); const char tmp1 = *(reader--); *(writer--) = base64_encodes[tmp3 & 63]; *(writer--) = base64_encodes[((tmp2 & 15) << 2) | ((tmp3 >> 6) & 3)]; *(writer--) = base64_encodes[(((tmp1 & 3) << 4) | ((tmp2 >> 4) & 15))]; *(writer--) = base64_encodes[(tmp1 >> 2) & 63]; } return target_size; }
/* * The actual encoding logic. The map can be switched for encoding variations. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L8453-L8491
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_base64_encode
int fio_base64_encode(char *target, const char *data, int len) { return fio_base64_encode_internal(target, data, len, base64_encodes_original); }
/** This will encode a byte array (data) of a specified length (len) and place the encoded data into the target byte buffer (target). The target buffer MUST have enough room for the expected data. Base64 encoding always requires 4 bytes for each 3 bytes. Padding is added if the raw data's length isn't devisable by 3. Always assume the target buffer should have room enough for (len*4/3 + 4) bytes. Returns the number of bytes actually written to the target buffer (including the Base64 required padding and excluding a NULL terminator). A NULL terminator char is NOT written to the target buffer. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L8509-L8511
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_base64url_encode
int fio_base64url_encode(char *target, const char *data, int len) { return fio_base64_encode_internal(target, data, len, base64_encodes_url); }
/** Same as fio_base64_encode, but using Base64URL encoding. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L8516-L8518
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_base64_decode
int fio_base64_decode(char *target, char *encoded, int base64_len) { if (!target) target = encoded; if (base64_len <= 0) { target[0] = 0; return 0; } int written = 0; uint8_t tmp1, tmp2, tmp3, tmp4; // skip unknown data at end while (base64_len && !base64_decodes[*(uint8_t *)(encoded + (base64_len - 1))]) { base64_len--; } // skip white space while (base64_len && isspace((*(uint8_t *)encoded))) { base64_len--; encoded++; } while (base64_len >= 4) { if (!base64_len) { return written; } tmp1 = *(uint8_t *)(encoded++); tmp2 = *(uint8_t *)(encoded++); tmp3 = *(uint8_t *)(encoded++); tmp4 = *(uint8_t *)(encoded++); if (!base64_decodes[tmp1] || !base64_decodes[tmp2] || !base64_decodes[tmp3] || !base64_decodes[tmp4]) { errno = ERANGE; goto finish; } *(target++) = (BITVAL(tmp1) << 2) | (BITVAL(tmp2) >> 4); *(target++) = (BITVAL(tmp2) << 4) | (BITVAL(tmp3) >> 2); *(target++) = (BITVAL(tmp3) << 6) | (BITVAL(tmp4)); // make sure we don't loop forever. base64_len -= 4; // count written bytes written += 3; // skip white space while (base64_len && isspace((*encoded))) { base64_len--; encoded++; } } // deal with the "tail" of the mis-encoded stream - this shouldn't happen tmp1 = 0; tmp2 = 0; tmp3 = 0; tmp4 = 0; switch (base64_len) { case 1: tmp1 = *(uint8_t *)(encoded++); if (!base64_decodes[tmp1]) { errno = ERANGE; goto finish; } *(target++) = BITVAL(tmp1); written += 1; break; case 2: tmp1 = *(uint8_t *)(encoded++); tmp2 = *(uint8_t *)(encoded++); if (!base64_decodes[tmp1] || !base64_decodes[tmp2]) { errno = ERANGE; goto finish; } *(target++) = (BITVAL(tmp1) << 2) | (BITVAL(tmp2) >> 6); *(target++) = (BITVAL(tmp2) << 4); written += 2; break; case 3: tmp1 = *(uint8_t *)(encoded++); tmp2 = *(uint8_t *)(encoded++); tmp3 = *(uint8_t *)(encoded++); if (!base64_decodes[tmp1] || !base64_decodes[tmp2] || !base64_decodes[tmp3]) { errno = ERANGE; goto finish; } *(target++) = (BITVAL(tmp1) << 2) | (BITVAL(tmp2) >> 6); *(target++) = (BITVAL(tmp2) << 4) | (BITVAL(tmp3) >> 2); *(target++) = BITVAL(tmp3) << 6; written += 3; break; } finish: if (encoded[-1] == '=') { target--; written--; if (encoded[-2] == '=') { target--; written--; } if (written < 0) written = 0; } *target = 0; return written; }
/** This will decode a Base64 encoded string of a specified length (len) and place the decoded data into the target byte buffer (target). The target buffer MUST have enough room for the expected data. A NULL byte will be appended to the target buffer. The function will return the number of bytes written to the target buffer. If the target buffer is NULL, the encoded string will be destructively edited and the decoded data will be placed in the original string's buffer. Base64 encoding always requires 4 bytes for each 3 bytes. Padding is added if the raw data's length isn't devisable by 3. Hence, the target buffer should be, at least, `base64_len/4*3 + 3` long. Returns the number of bytes actually written to the target buffer (excluding the NULL terminator byte). If an error occurred, returns the number of bytes written up to the error. Test `errno` for error (will be set to ERANGE). */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L8542-L8641
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_str_test
FIO_FUNC inline void fio_str_test(void) { #define ROUND_UP_CAPA_2WORDS(num) \ (((num + 1) & (sizeof(long double) - 1)) \ ? ((num + 1) | (sizeof(long double) - 1)) \ : (num)) fprintf(stderr, "=== Testing Core String features (fio_str_s functions)\n"); fprintf(stderr, "* String container size: %zu\n", sizeof(fio_str_s)); fprintf(stderr, "* Self-Contained String Capacity (FIO_STR_SMALL_CAPA): %zu\n", FIO_STR_SMALL_CAPA); fio_str_s str = {.small = 0}; /* test zeroed out memory */ FIO_ASSERT(fio_str_capa(&str) == FIO_STR_SMALL_CAPA - 1, "Small String capacity reporting error!"); FIO_ASSERT(fio_str_len(&str) == 0, "Small String length reporting error!"); FIO_ASSERT(fio_str_data(&str) == (char *)((uintptr_t)(&str + 1) - FIO_STR_SMALL_CAPA), "Small String pointer reporting error (%zd offset)!", (ssize_t)(((char *)((uintptr_t)(&str + 1) - FIO_STR_SMALL_CAPA)) - fio_str_data(&str))); fio_str_write(&str, "World", 4); FIO_ASSERT(str.small, "Small String writing error - not small on small write!"); FIO_ASSERT(fio_str_capa(&str) == FIO_STR_SMALL_CAPA - 1, "Small String capacity reporting error after write!"); FIO_ASSERT(fio_str_len(&str) == 4, "Small String length reporting error after write!"); FIO_ASSERT(fio_str_data(&str) == (char *)((uintptr_t)(&str + 1) - FIO_STR_SMALL_CAPA), "Small String pointer reporting error after write!"); FIO_ASSERT(strlen(fio_str_data(&str)) == 4, "Small String NUL missing after write (%zu)!", strlen(fio_str_data(&str))); FIO_ASSERT(!strcmp(fio_str_data(&str), "Worl"), "Small String write error (%s)!", fio_str_data(&str)); FIO_ASSERT(fio_str_data(&str) == fio_str_info(&str).data, "Small String `fio_str_data` != `fio_str_info(s).data` (%p != %p)", (void *)fio_str_data(&str), (void *)fio_str_info(&str).data); fio_str_capa_assert(&str, sizeof(fio_str_s) - 1); FIO_ASSERT(!str.small, "Long String reporting as small after capacity update!"); FIO_ASSERT(fio_str_capa(&str) >= sizeof(fio_str_s) - 1, "Long String capacity update error (%zu != %zu)!", fio_str_capa(&str), sizeof(fio_str_s)); FIO_ASSERT(fio_str_data(&str) == fio_str_info(&str).data, "Long String `fio_str_data` !>= `fio_str_info(s).data` (%p != %p)", (void *)fio_str_data(&str), (void *)fio_str_info(&str).data); FIO_ASSERT( fio_str_len(&str) == 4, "Long String length changed during conversion from small string (%zu)!", fio_str_len(&str)); FIO_ASSERT(fio_str_data(&str) == str.data, "Long String pointer reporting error after capacity update!"); FIO_ASSERT(strlen(fio_str_data(&str)) == 4, "Long String NUL missing after capacity update (%zu)!", strlen(fio_str_data(&str))); FIO_ASSERT(!strcmp(fio_str_data(&str), "Worl"), "Long String value changed after capacity update (%s)!", fio_str_data(&str)); fio_str_write(&str, "d!", 2); FIO_ASSERT(!strcmp(fio_str_data(&str), "World!"), "Long String `write` error (%s)!", fio_str_data(&str)); fio_str_replace(&str, 0, 0, "Hello ", 6); FIO_ASSERT(!strcmp(fio_str_data(&str), "Hello World!"), "Long String `insert` error (%s)!", fio_str_data(&str)); fio_str_resize(&str, 6); FIO_ASSERT(!strcmp(fio_str_data(&str), "Hello "), "Long String `resize` clipping error (%s)!", fio_str_data(&str)); fio_str_replace(&str, 6, 0, "My World!", 9); FIO_ASSERT(!strcmp(fio_str_data(&str), "Hello My World!"), "Long String `replace` error when testing overflow (%s)!", fio_str_data(&str)); str.capa = str.len; fio_str_replace(&str, -10, 2, "Big", 3); FIO_ASSERT(!strcmp(fio_str_data(&str), "Hello Big World!"), "Long String `replace` error when testing splicing (%s)!", fio_str_data(&str)); FIO_ASSERT( fio_str_capa(&str) == ROUND_UP_CAPA_2WORDS(strlen("Hello Big World!")), "Long String `fio_str_replace` capacity update error (%zu != %zu)!", fio_str_capa(&str), ROUND_UP_CAPA_2WORDS(strlen("Hello Big World!"))); if (str.len < FIO_STR_SMALL_CAPA) { fio_str_compact(&str); FIO_ASSERT(str.small, "Compacting didn't change String to small!"); FIO_ASSERT(fio_str_len(&str) == strlen("Hello Big World!"), "Compacting altered String length! (%zu != %zu)!", fio_str_len(&str), strlen("Hello Big World!")); FIO_ASSERT(!strcmp(fio_str_data(&str), "Hello Big World!"), "Compact data error (%s)!", fio_str_data(&str)); FIO_ASSERT(fio_str_capa(&str) == FIO_STR_SMALL_CAPA - 1, "Compacted String capacity reporting error!"); } else { fprintf(stderr, "* skipped `compact` test!\n"); } { fio_str_freeze(&str); fio_str_info_s old_state = fio_str_info(&str); fio_str_write(&str, "more data to be written here", 28); fio_str_replace(&str, 2, 1, "more data to be written here", 28); fio_str_info_s new_state = fio_str_info(&str); FIO_ASSERT(old_state.len == new_state.len, "Frozen String length changed!"); FIO_ASSERT(old_state.data == new_state.data, "Frozen String pointer changed!"); FIO_ASSERT( old_state.capa == new_state.capa, "Frozen String capacity changed (allowed, but shouldn't happen)!"); str.frozen = 0; } fio_str_printf(&str, " %u", 42); FIO_ASSERT(!strcmp(fio_str_data(&str), "Hello Big World! 42"), "`fio_str_printf` data error (%s)!", fio_str_data(&str)); { fio_str_s str2 = FIO_STR_INIT; fio_str_concat(&str2, &str); FIO_ASSERT(fio_str_iseq(&str, &str2), "`fio_str_concat` error, strings not equal (%s != %s)!", fio_str_data(&str), fio_str_data(&str2)); fio_str_write(&str2, ":extra data", 11); FIO_ASSERT( !fio_str_iseq(&str, &str2), "`fio_str_write` error after copy, strings equal ((%zu)%s == (%zu)%s)!", fio_str_len(&str), fio_str_data(&str), fio_str_len(&str2), fio_str_data(&str2)); fio_str_free(&str2); } fio_str_free(&str); fio_str_write_i(&str, -42); FIO_ASSERT(fio_str_len(&str) == 3 && !memcmp("-42", fio_str_data(&str), 3), "fio_str_write_i output error ((%zu) %s != -42)", fio_str_len(&str), fio_str_data(&str)); fio_str_free(&str); { fprintf(stderr, "* testing `fio_str_readfile`, and reference counting.\n"); fio_str_s *s = fio_str_new2(); FIO_ASSERT(s && s->small, "`fio_str_new2` error, string not initialized (%p)!", (void *)s); fio_str_s *s2 = fio_str_dup(s); ++fio_str_test_dealloc_counter; FIO_ASSERT(s2 == s, "`fio_str_dup` error, should return self!"); FIO_ASSERT(s->ref == 1, "`fio_str_dup` error, reference counter not incremented!"); fprintf(stderr, "* reading a file.\n"); fio_str_info_s state = fio_str_readfile(s, __FILE__, 0, 0); if (!s->small) /* attach deallocation test */ s->dealloc = fio_str_test_dealloc; FIO_ASSERT(state.data, "`fio_str_readfile` error, no data was read for file %s!", __FILE__); FIO_ASSERT(!memcmp(state.data, "/* " "******************************************************" "***********************", 80), "`fio_str_readfile` content error, header mismatch!\n %s", state.data); fprintf(stderr, "* testing UTF-8 validation and length.\n"); FIO_ASSERT( fio_str_utf8_valid(s), "`fio_str_utf8_valid` error, code in this file should be valid!"); FIO_ASSERT(fio_str_utf8_len(s) && (fio_str_utf8_len(s) <= fio_str_len(s)) && (fio_str_utf8_len(s) >= (fio_str_len(s)) >> 1), "`fio_str_utf8_len` error, invalid value (%zu / %zu!", fio_str_utf8_len(s), fio_str_len(s)); fprintf(stderr, "* reviewing reference counting `fio_str_free2` (1/2).\n"); fio_str_free2(s2); --fio_str_test_dealloc_counter; FIO_ASSERT(s->ref == 0, "`fio_str_free2` error, reference counter not subtracted!"); FIO_ASSERT(s->small == 0, "`fio_str_free2` error, strring reinitialized!"); FIO_ASSERT( fio_str_data(s) == state.data, "`fio_str_free2` error, data freed while references exist! (%p != %p)", (void *)fio_str_data(s), (void *)state.data); if (1) { /* String content == whole file (this file) */ intptr_t pos = -11; size_t len = 20; fprintf(stderr, "* testing UTF-8 positioning.\n"); FIO_ASSERT( fio_str_utf8_select(s, &pos, &len) == 0, "`fio_str_utf8_select` returned error for negative pos! (%zd, %zu)", (ssize_t)pos, len); FIO_ASSERT( pos == (intptr_t)state.len - 10, /* no UTF-8 bytes in this file */ "`fio_str_utf8_select` error, negative position invalid! (%zd)", (ssize_t)pos); FIO_ASSERT(len == 10, "`fio_str_utf8_select` error, trancated length invalid! (%zd)", (ssize_t)len); pos = 10; len = 20; FIO_ASSERT(fio_str_utf8_select(s, &pos, &len) == 0, "`fio_str_utf8_select` returned error! (%zd, %zu)", (ssize_t)pos, len); FIO_ASSERT(pos == 10, "`fio_str_utf8_select` error, position invalid! (%zd)", (ssize_t)pos); FIO_ASSERT(len == 20, "`fio_str_utf8_select` error, length invalid! (%zd)", (ssize_t)len); } fprintf(stderr, "* reviewing reference counting `fio_str_free2` (2/2).\n"); fio_str_free2(s); fprintf(stderr, "* finished reference counting test.\n"); } fio_str_free(&str); if (1) { const char *utf8_sample = /* three hearts, small-big-small*/ "\xf0\x9f\x92\x95\xe2\x9d\xa4\xef\xb8\x8f\xf0\x9f\x92\x95"; fio_str_write(&str, utf8_sample, strlen(utf8_sample)); intptr_t pos = -2; size_t len = 2; FIO_ASSERT(fio_str_utf8_select(&str, &pos, &len) == 0, "`fio_str_utf8_select` returned error for negative pos on " "UTF-8 data! (%zd, %zu)", (ssize_t)pos, len); FIO_ASSERT(pos == (intptr_t)fio_str_len(&str) - 4, /* 4 byte emoji */ "`fio_str_utf8_select` error, negative position invalid on " "UTF-8 data! (%zd)", (ssize_t)pos); FIO_ASSERT(len == 4, /* last utf-8 char is 4 byte long */ "`fio_str_utf8_select` error, trancated length invalid on " "UTF-8 data! (%zd)", (ssize_t)len); pos = 1; len = 20; FIO_ASSERT(fio_str_utf8_select(&str, &pos, &len) == 0, "`fio_str_utf8_select` returned error on UTF-8 data! (%zd, %zu)", (ssize_t)pos, len); FIO_ASSERT( pos == 4, "`fio_str_utf8_select` error, position invalid on UTF-8 data! (%zd)", (ssize_t)pos); FIO_ASSERT( len == 10, "`fio_str_utf8_select` error, length invalid on UTF-8 data! (%zd)", (ssize_t)len); pos = 1; len = 3; FIO_ASSERT( fio_str_utf8_select(&str, &pos, &len) == 0, "`fio_str_utf8_select` returned error on UTF-8 data (2)! (%zd, %zu)", (ssize_t)pos, len); FIO_ASSERT( len == 10, /* 3 UTF-8 chars: 4 byte + 4 byte + 2 byte codes == 10 */ "`fio_str_utf8_select` error, length invalid on UTF-8 data! (%zd)", (ssize_t)len); } fio_str_free(&str); if (1) { str = FIO_STR_INIT_STATIC("Welcome"); FIO_ASSERT(fio_str_capa(&str) == 0, "Static string capacity non-zero."); FIO_ASSERT(fio_str_len(&str) > 0, "Static string length should be automatically calculated."); FIO_ASSERT(str.dealloc == NULL, "Static string deallocation function should be NULL."); fio_str_free(&str); str = FIO_STR_INIT_STATIC("Welcome"); fio_str_info_s state = fio_str_write(&str, " Home", 5); FIO_ASSERT(state.capa > 0, "Static string not converted to non-static."); FIO_ASSERT(str.dealloc, "Missing static string deallocation function" " after `fio_str_write`."); fprintf(stderr, "* reviewing `fio_str_detach`.\n (%zu): %s\n", fio_str_info(&str).len, fio_str_info(&str).data); char *cstr = fio_str_detach(&str); FIO_ASSERT(cstr, "`fio_str_detach` returned NULL"); FIO_ASSERT(!memcmp(cstr, "Welcome Home\0", 13), "`fio_str_detach` string error: %s", cstr); fio_free(cstr); FIO_ASSERT(fio_str_len(&str) == 0, "`fio_str_detach` data wasn't cleared."); // fio_str_free(&str); } fprintf(stderr, "* passed.\n"); }
/** * Tests the fio_str functionality. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L8870-L9167
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_state_callback_test_task
FIO_FUNC void fio_state_callback_test_task(void *pi) { ((uintptr_t *)pi)[0] += 1; }
/* ***************************************************************************** Testing Core Callback add / remove / ensure ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L9328-L9330
675c65b509d48c21a8d1fa4c5ec53fc407643a3b