{"repo_name": "hacktical-c", "file_name": "/hacktical-c/dsl/tests.c", "inference_info": {"prefix_code": "#include \n#include \n#include \"dsl.h\"\n\n", "suffix_code": "\n\nstatic void read_id_tests() {\n struct hc_sloc sloc = hc_sloc(\"read_id_tests\", 0, 0);\n \n struct hc_list out;\n hc_list_init(&out);\n hc_defer(hc_forms_free(&out));\n\n const char *s = \"foo\";\n const char *in = s;\n assert(hc_read_expr(&in, &out, &sloc));\n \n struct hc_form *f = hc_baseof(out.next, struct hc_form, owner);\n assert(f->type == &HC_ID_FORM);\n}\n\nstatic void eval_tests() {\n struct hc_dsl dsl;\n hc_dsl_init(&dsl, &hc_malloc_default);\n hc_defer(hc_dsl_deinit(&dsl));\n struct hc_memory_stream out;\n hc_memory_stream_init(&out, &hc_malloc_default);\n hc_defer(hc_stream_deinit(&out.stream));\n dsl.out = &out.stream;\n hc_dsl_set_string(&dsl, \"foo\", \"ghi\");\n hc_dsl_eval(&dsl, \"abc $(print (upcase foo)) def\");\n assert(strcmp(\"abc GHI def\", hc_memory_stream_string(&out)) == 0);\n}\n\nvoid dsl_tests() {\n read_id_tests();\n read_call_tests();\n eval_tests();\n}\n", "middle_code": "static void read_call_tests() {\n struct hc_sloc sloc = hc_sloc(\"read_call_tests\", 0, 0);\n struct hc_list out;\n hc_list_init(&out);\n hc_defer(hc_forms_free(&out));\n const char *s = \"( foo bar )\";\n const char *in = s;\n assert(hc_read_expr(&in, &out, &sloc));\n struct hc_form *f = hc_baseof(out.next, struct hc_form, owner);\n assert(f->type == &HC_CALL_FORM);\n}", "code_description": null, "fill_type": "FUNCTION_TYPE", "language_type": "c", "sub_task_type": null}, "context_code": [["/hacktical-c/dsl/dsl.c", "#include \n#include \n#include \n#include \n\n#include \"dsl.h\"\n#include \"error/error.h\"\n#include \"list/list.h\"\n\nenum hc_order hc_strcmp(const char *x, const char *y) {\n const int result = strcmp(x, y);\n if (!result) { return HC_EQ; }\n return (result < 0) ? HC_LT : HC_GT;\n}\n\nchar *hc_upcase(char *s) {\n while (*s) {\n *s = toupper(*s);\n s++;\n }\n\n return s;\n}\n\nstatic void lib_print(struct hc_vm *vm, struct hc_sloc sloc) {\n struct hc_value *v = hc_vm_pop(vm);\n struct hc_dsl *dsl = hc_baseof(vm, struct hc_dsl, vm);\n hc_value_print(v, dsl->out);\n hc_value_deinit(v);\n}\n\nstatic void lib_upcase(struct hc_vm *vm, struct hc_sloc sloc) {\n struct hc_value *v = hc_vm_peek(vm);\n\n if (v->type != &HC_STRING) {\n hc_throw(\"Error in %s: Expected string (%s)\",\n\t hc_sloc_string(&sloc), v->type->name);\n }\n\n hc_upcase(v->as_string);\n}\n\nstruct env_item {\n char *key;\n struct hc_value value;\n};\n\nstatic enum hc_order env_cmp(const void *x, const void *y) {\n return hc_strcmp(*(const char **)x, *(const char **)y);\n}\n\nstatic const void *env_key(const void *x) {\n return &((const struct env_item *)x)->key;\n}\n\nvoid hc_dsl_init(struct hc_dsl *dsl, struct hc_malloc *malloc) {\n hc_set_init(&dsl->env, malloc, sizeof(struct env_item), env_cmp);\n dsl->env.key = env_key;\n dsl->out = hc_stdout();\n \n hc_vm_init(&dsl->vm, &hc_malloc_default);\n hc_dsl_set_fun(dsl, \"print\", lib_print);\n hc_dsl_set_fun(dsl, \"upcase\", lib_upcase);\n}\n\nstatic void deinit_env(struct hc_set *env) {\n hc_vector_do(&env->items, _it) {\n struct env_item *it = _it;\n free(it->key);\n hc_value_deinit(&it->value);\n }\n \n hc_set_deinit(env);\n}\n\nvoid hc_dsl_deinit(struct hc_dsl *dsl) {\n deinit_env(&dsl->env);\n hc_vm_deinit(&dsl->vm);\n}\n\nstruct hc_value *hc_dsl_getenv(struct hc_dsl *dsl, const char *key) {\n struct env_item *it = hc_set_find(&dsl->env, &key); \n return it ? &it->value : NULL;\n}\n\nstruct hc_value *hc_dsl_setenv(struct hc_dsl *dsl,\n\t\t\t const char *key,\n\t\t\t const struct hc_type *type) {\n struct env_item *it = hc_set_add(&dsl->env, &key, false);\n it->key = strdup(key);\n hc_value_init(&it->value, type);\n return &it->value;\n}\n\nvoid hc_dsl_set_fun(struct hc_dsl *dsl, const char *key, hc_vm_fun_t val) {\n hc_dsl_setenv(dsl, key, &HC_VM_FUN)->as_other = val;\n}\n\nvoid hc_dsl_set_string(struct hc_dsl *dsl, const char *key, const char *val) {\n hc_dsl_setenv(dsl, key, &HC_STRING)->as_string = strdup(val);\n}\n\nvoid hc_dsl_eval(struct hc_dsl *dsl, const char *in) {\n struct hc_list forms;\n hc_list_init(&forms);\n hc_defer(hc_forms_free(&forms));\n struct hc_sloc sloc = hc_sloc(\"eval\", 0, 0);\n while (hc_read_next(&in, &forms, &sloc));\n const size_t pc = dsl->vm.code.length;\n hc_forms_emit(&forms, dsl);\n hc_vm_eval(&dsl->vm, pc, -1);\n}\n\nvoid hc_form_init(struct hc_form *f,\n\t\t const struct hc_form_type *t,\n\t\t const struct hc_sloc sloc,\n\t\t struct hc_list *owner) {\n f->type = t;\n f->sloc = sloc;\n\n if (owner) {\n hc_list_push_back(owner, &f->owner);\n } else {\n hc_list_init(&f->owner);\n }\n}\n\nvoid hc_form_emit(struct hc_form *f, struct hc_dsl *dsl) {\n assert(f->type->emit);\n f->type->emit(f, dsl);\n}\n\nvoid hc_form_print(struct hc_form *f, struct hc_stream *out) {\n assert(f->type->print);\n f->type->print(f, out);\n}\n\nstruct hc_value *hc_form_value(const struct hc_form *f, struct hc_dsl *dsl) {\n return f->type->value ? f->type->value(f, dsl) : NULL;\n}\n\nvoid hc_form_free(struct hc_form *f) {\n hc_list_delete(&f->owner);\n assert(f->type->free);\n f->type->free(f);\n}\n\nstatic void call_emit(struct hc_form *_f, struct hc_dsl *dsl) {\n struct hc_call *f = hc_baseof(_f, struct hc_call, form);\n struct hc_value *t = hc_form_value(f->target, dsl);\n\n if (!t) {\n hc_throw(\"Error in %s: Missing call target\",\n\t hc_sloc_string(&_f->sloc));\n }\n\n if (t->type != &HC_VM_FUN) {\n hc_throw(\"Error in %s: '%s' isn't callable\",\n\t hc_sloc_string(&_f->sloc),\n\t t->type->name);\n }\n\n hc_list_do(&f->args, a) {\n hc_form_emit(hc_baseof(a, struct hc_form, owner), dsl);\n }\n \n hc_vm_emit(&dsl->vm,\n\t &HC_CALL,\n\t &(struct hc_call_op){\n\t .target = t->as_other,\n\t .sloc = _f->sloc\n\t });\n}\n\nstatic void call_print(const struct hc_form *_f, struct hc_stream *out) {\n struct hc_call *f = hc_baseof(_f, struct hc_call, form);\n hc_putc(out, '(');\n hc_form_print(f->target, out);\n\n hc_list_do(&f->args, i) {\n hc_putc(out, ' ');\n hc_form_print(hc_baseof(i, struct hc_form, owner), out);\n }\n \n hc_putc(out, ')');\n}\n\nstatic void call_free(struct hc_form *_f) {\n struct hc_call *f = hc_baseof(_f, struct hc_call, form);\n hc_form_free(f->target); \n\n hc_list_do(&f->args, i) {\n hc_form_free(hc_baseof(i, struct hc_form, owner));\n }\n\n free(f);\n}\n\nconst struct hc_form_type HC_CALL_FORM = {\n .emit = call_emit,\n .print = call_print,\n .value = NULL,\n .free = call_free\n};\n\nvoid hc_call_init(struct hc_call *f,\n\t\t const struct hc_sloc sloc,\n\t\t struct hc_list *owner,\n\t\t struct hc_form *target) { \n hc_form_init(&f->form, &HC_CALL_FORM, sloc, owner);\n f->target = target;\n hc_list_init(&f->args);\n}\n\nstatic void id_emit(struct hc_form *_f, struct hc_dsl *dsl) {\n struct hc_id *f = hc_baseof(_f, struct hc_id, form);\n struct hc_value *v = hc_dsl_getenv(dsl, f->name);\n\n if (!v) {\n hc_throw(\"Error in %s: Unknown identifier '%s'\",\n\t hc_sloc_string(&_f->sloc), f->name);\n }\n\n struct hc_push_op op;\n hc_value_copy(&op.value, v);\n hc_vm_emit(&dsl->vm, &HC_PUSH, &op);\n}\n\nstatic void id_print(const struct hc_form *_f, struct hc_stream *out) {\n struct hc_id *f = hc_baseof(_f, struct hc_id, form);\n hc_puts(out, f->name);\n}\n\nstatic struct hc_value *id_value(const struct hc_form *_f,\n\t\t\t\t struct hc_dsl *dsl) {\n struct hc_id *f = hc_baseof(_f, struct hc_id, form);\n return hc_dsl_getenv(dsl, f->name);\n}\n\nstatic void id_free(struct hc_form *_f) {\n struct hc_id *f = hc_baseof(_f, struct hc_id, form);\n free(f->name);\n free(f);\n}\n\nconst struct hc_form_type HC_ID_FORM = {\n .emit = id_emit,\n .print = id_print,\n .value = id_value,\n .free = id_free\n};\n\nvoid hc_id_init(struct hc_id *f,\n\t\tconst struct hc_sloc sloc,\n\t\tstruct hc_list *owner,\n\t\tconst char *name) {\n hc_form_init(&f->form, &HC_ID_FORM, sloc, owner);\n f->name = strdup(name);\n}\n\nstatic void literal_emit(struct hc_form *_f, struct hc_dsl *dsl) {\n struct hc_literal *f = hc_baseof(_f, struct hc_literal, form); \n struct hc_push_op op;\n hc_value_copy(&op.value, &f->value);\n hc_vm_emit(&dsl->vm, &HC_PUSH, &op);\n}\n\nstatic void literal_print(const struct hc_form *_f, struct hc_stream *out) {\n struct hc_literal *f = hc_baseof(_f, struct hc_literal, form);\n hc_value_write(&f->value, out);\n}\n\nstatic struct hc_value *literal_value(const struct hc_form *_f,\n\t\t\t\t struct hc_dsl *dsl) {\n struct hc_literal *f = hc_baseof(_f, struct hc_literal, form);\n return &f->value;\n}\n\nstatic void literal_free(struct hc_form *_f) {\n struct hc_literal *f = hc_baseof(_f, struct hc_literal, form);\n hc_value_deinit(&f->value);\n free(f);\n}\n\nconst struct hc_form_type HC_LITERAL = {\n .emit = literal_emit,\n .print = literal_print,\n .value = literal_value,\n .free = literal_free,\n};\n\nvoid hc_literal_init(struct hc_literal *f,\n\t\t const struct hc_sloc sloc,\n\t\t struct hc_list *owner) { \n hc_form_init(&f->form, &HC_LITERAL, sloc, owner);\n}\n\nvoid hc_skip_ws(const char **in, struct hc_sloc *sloc) {\n for (;; (*in)++) {\n switch (**in) {\n case ' ':\n case '\\t':\n sloc->col++;\n break;\n case '\\n':\n sloc->row++;\n sloc->col = 0;\n break;\n default:\n return;\n }\n }\n}\n\nvoid hc_read_call(const char **in,\n\t\t struct hc_list *out,\n\t\t struct hc_sloc *sloc) {\n struct hc_sloc floc = *sloc;\n\n if (**in != '(') {\n hc_throw(\"Error in %s: Invalid call syntax\",\n\t hc_sloc_string(sloc));\n }\n\n (*in)++;\n sloc->col++;\n hc_skip_ws(in, sloc);\n \n if (!hc_read_expr(in, out, sloc)) {\n hc_throw(\"Error in %s: Missing call target\",\n\t hc_sloc_string(sloc));\n }\n\n struct hc_form *t = hc_baseof(hc_list_pop_back(out),\n\t\t\t\tstruct hc_form,\n\t\t\t\towner);\n\n struct hc_call *f = malloc(sizeof(struct hc_call));\n hc_list_init(&t->owner);\n hc_call_init(f, floc, out, t);\n \n for (bool done = false; !done;) {\n hc_skip_ws(in, sloc);\n \n switch (**in) {\n case 0:\n hc_form_free(&f->form);\n\n hc_throw(\"Error in %s: Open call form\",\n\t hc_sloc_string(sloc));\n case ')':\n (*in)++;\n sloc->col++;\n done = true;\n continue;\n default:\n break;\n }\n\n if (!hc_read_expr(in, &f->args, sloc)) {\n hc_form_free(&f->form);\n \n hc_throw(\"Error in %s: Invalid call syntax\",\n\t hc_sloc_string(sloc));\n }\n }\n}\n\nbool hc_read_expr(const char **in,\n\t\t struct hc_list *out,\n\t\t struct hc_sloc *sloc) {\n const char c = **in;\n \n switch (c) {\n case '(':\n hc_read_call(in, out, sloc);\n return true;\n default:\n if (isalpha(c)) {\n hc_read_id(in, out, sloc);\n return true;\n }\n\n break;\n }\n\n return false;\n}\n\nvoid hc_read_id(const char **in,\n\t\tstruct hc_list *out,\n\t\tstruct hc_sloc *sloc) {\n struct hc_sloc floc = *sloc;\n struct hc_memory_stream buf;\n hc_memory_stream_init(&buf, &hc_malloc_default);\n hc_defer(hc_stream_deinit(&buf.stream));\n char c = 0;\n\n while ((c = **in)) {\n if (isspace(c) || c == '(' || c == ')') {\n break;\n }\n \n hc_putc(&buf.stream, c);\n sloc->col++;\n (*in)++;\n }\n\n struct hc_id *f = malloc(sizeof(struct hc_id));\n hc_id_init(f, floc, out, hc_memory_stream_string(&buf));\n}\n\nbool hc_read_next(const char **in,\n\t\t struct hc_list *out,\n\t\t struct hc_sloc *sloc) {\n if (**in == '$') {\n (*in)++;\n hc_read_call(in, out, sloc);\n return true;\n }\n \n return hc_read_text(in, out, sloc);\n}\n\nbool hc_read_text(const char **in,\n\t\t struct hc_list *out,\n\t\t struct hc_sloc *sloc) {\n struct hc_sloc floc = *sloc;\n const char *start = *in;\n \n while (**in && **in != '$') {\n if (**in == '\\n') {\n sloc->row++;\n } else {\n sloc->col++;\n }\n\n (*in)++;\n }\n\n size_t n = *in - start;\n \n if (n) {\n struct hc_value v;\n hc_value_init(&v, &HC_STRING)->as_string = strndup(start, n); \n struct hc_literal *vf = malloc(sizeof(struct hc_literal));\n hc_literal_init(vf, floc, out);\n vf->value = v;\n struct hc_id *t = malloc(sizeof(struct hc_literal));\n hc_id_init(t, floc, NULL, \"print\");\n struct hc_call *c = malloc(sizeof(struct hc_call));\n hc_call_init(c, floc, out, &t->form);\n return true;\n }\n\n return false;\n}\n\nvoid hc_forms_free(struct hc_list *in) {\n hc_list_do(in, i) {\n hc_form_free(hc_baseof(i, struct hc_form, owner));\n } \n}\n\nvoid hc_forms_emit(struct hc_list *in, struct hc_dsl *dsl) {\n hc_list_do(in, i) {\n hc_form_emit(hc_baseof(i, struct hc_form, owner), dsl);\n } \n}\n"], ["/hacktical-c/dsl/benchmarks.c", "#include \"chrono/chrono.h\"\n#include \"dsl.h\"\n\nvoid dsl_benchmarks() {\n hc_time_t t;\n const int n = 100000;\n\n char buf[32];\n t = hc_now();\n \n for (int i = 0; i < n; i++) {\n sprintf(buf, \"abc %s def\", \"ghi\");\n }\n\n hc_time_print(&t, \"sprintf: \"); \n \n struct hc_dsl dsl;\n hc_dsl_init(&dsl, &hc_malloc_default);\n hc_defer(hc_dsl_deinit(&dsl));\n struct hc_memory_stream out;\n hc_memory_stream_init(&out, &hc_malloc_default);\n hc_defer(hc_stream_deinit(&out.stream));\n dsl.out = &out.stream;\n hc_dsl_set_string(&dsl, \"foo\", \"ghi\");\n hc_dsl_eval(&dsl, \"abc $(print foo) def\");\n\n t = hc_now();\n\n for (int i = 0; i < n; i++) {\n hc_vector_clear(&out.data);\n hc_vm_eval(&dsl.vm, 0, -1);\n }\n\n hc_time_print(&t, \"dsl: \");\n}\n"], ["/hacktical-c/slog/tests.c", "#include \n#include \n\n#include \"slog.h\"\n#include \"malloc1/malloc1.h\"\n\nvoid slog_tests() {\n struct hc_memory_stream out;\n hc_memory_stream_init(&out, &hc_malloc_default);\n \n struct hc_slog_stream s;\n hc_slog_stream_init(&s, &out.stream, .close_out=true);\n \n hc_slog_do(&s) {\n hc_slog_context_do(hc_slog_string(\"string\", \"abc\")) {\n hc_time_t t = hc_time(2025, 4, 13, 1, 40, 0);\n \n hc_slog_write(hc_slog_bool(\"bool\", true),\n\t\t hc_slog_int(\"int\", 42),\n\t\t hc_slog_time(\"time\", t));\n }\n }\n\n assert(strcmp(\"string=\\\"abc\\\", \"\n\t\t\"bool=true, \"\n\t\t\"int=42, \"\n\t\t\"time=2025-04-13T01:40:00\\n\",\n\t\thc_memory_stream_string(&out)) == 0);\n \n hc_slog_deinit(&s); \n}\n"], ["/hacktical-c/dynamic/dynamic.c", "#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#include \"dynamic.h\"\n#include \"error/error.h\"\n#include \"malloc1/malloc1.h\"\n#include \"vector/vector.h\"\n\nstruct hc_proc *_hc_proc_init(struct hc_proc *p, char *cmd[]) {\n int fds[2];\n\n if (pipe(fds) == -1) {\n hc_throw(\"Failed creating pipe: %d\", errno);\n }\n \n pid_t child_pid = fork();\n\n switch (child_pid) {\n case 0: {\n if (close(fds[1]) == -1) {\n hc_throw(\"Failed closing pipe writer: %d\", errno);\n }\n \n if (dup2(fds[0], 0) == -1) {\n hc_throw(\"Failed rebinding stdin: %d\", errno);\n }\n \n char *const env[] = {\"PATH=/bin:/sbin\", NULL};\n\n if (execve(cmd[0], cmd, env) == -1) {\n hc_throw(\"Failed to execve '%s': %d\", cmd[0], errno);\n }\n }\n case -1:\n hc_throw(\"Failed forking process: %d\", errno);\n default:\n if (close(fds[0]) == -1) {\n hc_throw(\"Failed closing pipe reader: %d\", errno);\n }\n \n p->pid = child_pid;\n p->in = fds[1];\n break;\n }\n\n return p;\n}\n\nstatic void close_in(struct hc_proc *p) {\n if (p->in != -1 && close(p->in) == -1) {\n hc_throw(\"Failed closing stdin: %d\", errno);\n }\n}\n\nvoid hc_proc_wait(struct hc_proc *p) {\n close_in(p);\n\n if (waitpid(p->pid, NULL, 0) == -1) {\n hc_throw(\"Failed waiting for child process to exit: %d\", errno);\n }\n}\n\nvoid hc_proc_deinit(struct hc_proc *p) {\n close_in(p);\n}\n\nstatic void free_cmd(char **in) {\n for (char **s = in; *s; s++) {\n free(*s);\n }\n}\n\nvoid _hc_compile(const char *code,\n\t\t const char *out,\n\t\t const struct hc_compile_opts opts) {\n hc_array(const char *, pre, \n\t opts.cc, \"-shared\", \"-fpic\", \"-o\", out, \"-xc\");\n \n int n = pre_n + 2;\n for (int i = 0; opts.cflags[i]; i++, n++); \n char *cmd[n];\n int i = 0;\n\n for (; i < pre_n; i++) {\n cmd[i] = strdup(pre_a[i]);\n }\n \n for (; i < n - 2; i++) {\n cmd[i] = strdup(opts.cflags[i - pre_n]);\n }\n\n cmd[i++] = strdup(\"-\");\n cmd[i] = NULL;\n hc_defer(free_cmd(cmd));\n \n struct hc_proc child;\n _hc_proc_init(&child, cmd);\n hc_defer(hc_proc_deinit(&child));\n FILE *in = fdopen(child.in, \"w\");\n\n if (!in) {\n hc_throw(\"Failed opening stdin stream: %d\", errno);\n }\n \n child.in = -1;\n hc_defer(hc_proc_wait(&child));\n hc_defer(fclose(in));\n\n if (fputs(code, in) == EOF) {\n hc_throw(\"Failed writing code: %d\", errno);\n }\n}\n\nstruct hc_dlib *hc_dlib_init(struct hc_dlib *lib, const char *path) {\n lib->handle = dlopen(path, RTLD_NOW);\n\n if (!lib->handle) {\n hc_throw(\"Error opening dynamic library '%s': %s\", path, dlerror());\n }\n \n return lib;\n}\n\nstruct hc_dlib *hc_dlib_deinit(struct hc_dlib *lib) {\n if (dlclose(lib->handle) != 0) {\n hc_throw(\"Failed closing dynamic library: \", dlerror());\n }\n\n return lib;\n}\n\nvoid *hc_dlib_find(const struct hc_dlib *lib, const char *s) {\n dlerror();\n void *v = dlsym(lib->handle, s);\n char *e = dlerror();\n\n if (e) {\n hc_throw(\"Symbol '%s' not found: %s\", e);\n }\n\n return v;\n}\n\nchar *hc_vsprintf(const char *format, va_list args) {\n va_list tmp_args;\n va_copy(tmp_args, args);\n int len = vsnprintf(NULL, 0, format, tmp_args);\n va_end(tmp_args);\n\n if (len < 0) {\n hc_throw(\"Formatting '%s' failed: %d\", format, errno);\n }\n\n len++;\n char *out = malloc(len);\n vsnprintf(out, len, format, args);\n return out;\n} \n"], ["/hacktical-c/vm/vm.c", "#include \n#include \n#include \n#include \n#include \n\n#include \"error/error.h\"\n#include \"macro/macro.h\"\n#include \"vm.h\"\n\nstruct hc_sloc hc_sloc(const char *source, const int row, const int col) {\n struct hc_sloc s = {.source = {0}, .row = row, .col = col};\n assert(strlen(source) < sizeof(s.source));\n strcpy(s.source, source);\n return s;\n}\n\nconst char *hc_sloc_string(struct hc_sloc *sloc) {\n snprintf(sloc->out, sizeof(sloc->out), \"'%s'; row %d, column %d\",\n\t sloc->source, sloc->row, sloc->col);\n return sloc->out;\n}\n\n\nvoid hc_vm_init(struct hc_vm *vm, struct hc_malloc *malloc) {\n hc_vector_init(&vm->stack, malloc, sizeof(struct hc_value));\n hc_vector_init(&vm->ops, malloc, sizeof(const struct hc_op *));\n hc_vector_init(&vm->code, malloc, sizeof(hc_op_eval_t));\n}\n\nstatic size_t op_items(const struct hc_op *op,\n\t\t uint8_t *p,\n\t\t struct hc_vm *vm) {\n const size_t s = op->size + hc_align(p, op->align) - p;\n return ceil(s / (double)vm->code.item_size);\n}\n\nstatic void deinit_stack(struct hc_vm *vm) {\n hc_vector_do(&vm->stack, v) {\n hc_value_deinit(v);\n }\n\n hc_vector_deinit(&vm->stack);\n}\n\nstatic void deinit_ops(struct hc_vm *vm) {\n uint8_t *p = vm->code.start;\n \n hc_vector_do(&vm->ops, _op) {\n const struct hc_op *op = *(const struct hc_op **)_op;\n p += sizeof(hc_op_eval_t);\n\n if (op->deinit) {\n op->deinit(hc_align(p, op->align));\n }\n\n p += op_items(op, p, vm) * vm->code.item_size;\n }\n\n hc_vector_deinit(&vm->ops);\n}\n\nvoid hc_vm_deinit(struct hc_vm *vm) { \n deinit_stack(vm);\n deinit_ops(vm);\n hc_vector_deinit(&vm->code);\n}\n\nstruct hc_value *hc_vm_push(struct hc_vm *vm) {\n return hc_vector_push(&vm->stack);\n}\n\nstruct hc_value *hc_vm_peek(struct hc_vm *vm) {\n return hc_vector_peek(&vm->stack);\n}\n\nstruct hc_value *hc_vm_pop(struct hc_vm *vm) {\n return hc_vector_pop(&vm->stack);\n}\n\nsize_t hc_vm_emit(struct hc_vm *vm,\n\t\t const struct hc_op *op,\n\t\t const void *data) {\n *(const struct hc_op **)hc_vector_push(&vm->ops) = op;\n const size_t pc = vm->code.length;\n *(hc_op_eval_t *)hc_vector_push(&vm->code) = op->eval;\n \n uint8_t *const p = hc_vector_insert(&vm->code,\n\t\t\t\t vm->code.length,\n\t\t\t\t op_items(op, vm->code.end, vm));\n \n memcpy(hc_align(p, op->align), data, op->size);\n return pc;\n}\n\nvoid hc_vm_eval(struct hc_vm *vm,\n\t\tconst size_t start_pc,\n\t\tconst size_t end_pc) {\n const uint8_t *const ep = (end_pc == -1)\n ? vm->code.end\n : hc_vector_get(&vm->code, end_pc);\n\n for (uint8_t *p = hc_vector_get(&vm->code, start_pc);\n p != ep;\n p = (*(hc_op_eval_t *)p)(vm, p + vm->code.item_size));\n}\n\nstatic void fun_print(const struct hc_value *v, struct hc_stream *out) {\n hc_printf(out, \"%p\", v->as_other);\n}\n\nconst struct hc_type HC_VM_FUN = {\n .name = \"VM/Fun\",\n .copy = NULL,\n .print = fun_print\n};\n\nstatic uint8_t *call_eval(struct hc_vm *vm, uint8_t *data) {\n struct hc_call_op *op = (void *)hc_align(data, alignof(struct hc_call_op));\n op->target(vm, op->sloc);\n return (uint8_t *)op + sizeof(struct hc_call_op);\n}\n\nconst struct hc_op HC_CALL = (struct hc_op){\n .name = \"call\",\n .align = alignof(struct hc_call_op),\n .size = sizeof(struct hc_call_op),\n .eval = call_eval,\n .deinit = NULL\n};\n\nstatic void push_deinit(uint8_t *data) {\n struct hc_push_op *op = (void *)data;\n hc_value_deinit(&op->value);\n}\n\nstatic uint8_t *push_eval(struct hc_vm *vm, uint8_t *data) {\n struct hc_push_op *op = (void *)hc_align(data, alignof(struct hc_push_op));\n hc_value_copy(hc_vm_push(vm), &op->value);\n return (uint8_t *)op + sizeof(struct hc_push_op);\n}\n\nconst struct hc_op HC_PUSH = (struct hc_op){\n .name = \"push\",\n .align = alignof(struct hc_push_op),\n .size = sizeof(struct hc_push_op),\n .eval = push_eval,\n .deinit = push_deinit\n};\n"], ["/hacktical-c/stream1/stream1.c", "#include \n#include \n#include \n#include \n\n#include \"dynamic/dynamic.h\"\n#include \"error/error.h\"\n#include \"malloc1/malloc1.h\"\n#include \"macro/macro.h\"\n#include \"stream1.h\"\n\nsize_t hc_read(struct hc_stream *s, uint8_t *data, const size_t n) {\n assert(s->read);\n return s->read(s, data, n);\n}\n\nsize_t hc_write(struct hc_stream *s, const uint8_t *data, const size_t n) {\n assert(s->write);\n return s->write(s, data, n);\n}\n\nchar hc_getc(struct hc_stream *s) {\n char c = 0;\n return hc_read(s, (uint8_t *)&c, 1) ? c : 0;\n}\n\nchar *hc_gets(struct hc_stream *s, struct hc_malloc *malloc) {\n struct hc_vector out;\n hc_vector_init(&out, malloc, 1);\n\n for (;;) {\n char c = hc_getc(s);\n\n if (c == EOF) {\n break;\n }\n\n *(char *)hc_vector_push(&out) = c;\n\n if (c == '\\n') {\n break;\n }\n }\n\n \n *(char *)hc_vector_push(&out) = 0;\n return (char *)out.start;\n}\n\nsize_t hc_putc(struct hc_stream *s, const char data) {\n const uint8_t d[2] = {data, 0};\n return hc_write(s, d, 1);\n}\n\nsize_t hc_puts(struct hc_stream *s, const char *data) {\n return hc_write(s, (const uint8_t *)data, strlen(data));\n}\n\nsize_t hc_vprintf(struct hc_stream *s,\n\t\t const char *spec,\n\t\t va_list args) {\n char *data = hc_vsprintf(spec, args);\n hc_defer(free(data));\n return hc_write(s, (uint8_t *)data, strlen(data));\n}\n\nsize_t hc_printf(struct hc_stream *s, const char *spec, ...) {\n va_list args;\n va_start(args, spec);\n hc_defer(va_end(args));\n return hc_vprintf(s, spec, args);\n}\n\nvoid hc_stream_deinit(struct hc_stream *s) {\n assert(s->deinit);\n s->deinit(s);\n}\n\nsize_t file_read(struct hc_stream *s, uint8_t *data, const size_t n) {\n struct hc_file_stream *fs = hc_baseof(s, struct hc_file_stream, stream);\n assert(fs->file);\n return fread(data, n, 1, fs->file);\n}\n\nsize_t file_write(struct hc_stream *s, const uint8_t *data, const size_t n) {\n struct hc_file_stream *fs = hc_baseof(s, struct hc_file_stream, stream);\n assert(fs->file);\n return fwrite(data, n, 1, fs->file);\n}\n\nvoid file_deinit(struct hc_stream *s) {\n struct hc_file_stream *fs = hc_baseof(s, struct hc_file_stream, stream);\n\n if (fs->opts.close_file) {\n assert(fs->file);\n \n if (fclose(fs->file) == EOF) {\n hc_throw(\"Failed closing file\");\n }\n \n fs->file = NULL;\n }\n}\n\nstruct hc_file_stream *_hc_file_stream_init(struct hc_file_stream *s,\n\t\t\t\t\t FILE *file,\n\t\t\t\t\t const struct hc_file_stream_opts opts) {\n s->stream = (struct hc_stream){\n .read = file_read,\n .write = file_write,\n .deinit = file_deinit,\n };\n \n s->file = file;\n s->opts = opts;\n return s;\n};\n\nstruct hc_stream *hc_stdout() {\n static __thread bool init = true;\n static __thread struct hc_file_stream s;\n\n if (init) {\n hc_file_stream_init(&s, stdout);\n init = false;\n }\n\n return &s.stream;\n}\n\nsize_t memory_read(struct hc_stream *s, uint8_t *data, size_t n) {\n struct hc_memory_stream *ms = hc_baseof(s, struct hc_memory_stream, stream);\n\n if (ms->rpos + n > ms->data.length) {\n n = ms->data.length - ms->rpos;\n }\n \n memcpy(data, ms->data.start + ms->rpos, n);\n ms->rpos += n;\n return n;\n}\n\nsize_t memory_write(struct hc_stream *s,\n\t\t const uint8_t *data,\n\t\t const size_t n) {\n struct hc_memory_stream *ms = hc_baseof(s, struct hc_memory_stream, stream);\n uint8_t *const dst = hc_vector_insert(&ms->data, ms->data.length, n);\n memcpy(dst, data, n);\n return n;\n}\n\nvoid memory_deinit(struct hc_stream *s) {\n struct hc_memory_stream *ms = hc_baseof(s, struct hc_memory_stream, stream);\n hc_vector_deinit(&ms->data);\n}\n\nstruct hc_memory_stream *hc_memory_stream_init(struct hc_memory_stream *s,\n\t\t\t\t\t struct hc_malloc *malloc) {\n s->stream = (struct hc_stream){\n .read = memory_read,\n .write = memory_write,\n .deinit = memory_deinit,\n };\n \n hc_vector_init(&s->data, malloc, 1);\n s->rpos = 0;\n return s;\n}\n\nconst char *hc_memory_stream_string(struct hc_memory_stream *s) {\n if (!s->data.length || (*(s->data.end-1))) {\n *(uint8_t *)hc_vector_push(&s->data) = 0;\n }\n\n return (const char *)s->data.start;\n}\n"], ["/hacktical-c/slog/slog.c", "#include \n#include \n#include \n#include \n\n#include \"error/error.h\"\n#include \"macro/macro.h\"\n#include \"malloc1/malloc1.h\"\n#include \"slog.h\"\n\n__thread struct hc_slog *_hc_slog = NULL;\n\nstruct hc_slog *hc_slog() {\n if (_hc_slog != NULL) {\n return _hc_slog;\n }\n \n static __thread bool init = true;\n static __thread struct hc_slog_stream s;\n\n if (init) {\n hc_slog_stream_init(&s, hc_stdout());\n init = false;\n }\n\n return &s.slog;\n}\n\nstatic void field_deinit(struct hc_slog_field *f) {\n free(f->name);\n hc_value_deinit(&f->value);\n}\n\nstatic void slog_write(struct hc_slog *s,\n\t\t const size_t n,\n\t\t struct hc_slog_field *fields[]) {\n assert(s->write);\n s->write(s, n, fields);\n}\n\nvoid __hc_slog_write(struct hc_slog *s,\n\t\t const size_t n,\n\t\t struct hc_slog_field *fields[]) {\n slog_write(s, n, fields);\n \n for(size_t i = 0; i < n; i++) {\n struct hc_slog_field *f = fields[i];\n field_deinit(f);\n free(f);\n }\n}\n\nvoid _hc_slog_deinit(struct hc_slog *s) {\n if (s->deinit) {\n s->deinit(s);\n }\n}\n\nstatic struct hc_value *field_init(struct hc_slog_field *f,\n\t\t\t\t const char *name,\n\t\t\t\t const struct hc_type *type) {\n f->name = strdup(name);\n hc_value_init(&f->value, type);\n return &f->value;\n}\n\nstruct hc_slog_field *hc_slog_bool(const char *name, const bool value) {\n struct hc_slog_field *f = malloc(sizeof(struct hc_slog_field));\n field_init(f, name, &HC_BOOL)->as_bool = value;\n return f;\n}\n\nstruct hc_slog_field *hc_slog_int(const char *name, const int value) {\n struct hc_slog_field *f = malloc(sizeof(struct hc_slog_field));\n field_init(f, name, &HC_INT)->as_int = value;\n return f;\n}\n\nstruct hc_slog_field *hc_slog_string(const char *name, const char *value) {\n struct hc_slog_field *f = malloc(sizeof(struct hc_slog_field));\n field_init(f, name, &HC_STRING)->as_string = strdup(value);\n return f;\n}\n\nstruct hc_slog_field *hc_slog_time(const char *name, const hc_time_t value) {\n struct hc_slog_field *f = malloc(sizeof(struct hc_slog_field));\n field_init(f, name, &HC_TIME)->as_time = value;\n return f;\n}\n\nvoid stream_deinit(struct hc_slog *s) {\n struct hc_slog_stream *ss = hc_baseof(s, struct hc_slog_stream, slog);\n if (ss->opts.close_out) { hc_stream_deinit(ss->out); }\n}\n\nstatic void field_write(struct hc_slog_field *f, struct hc_stream *out) {\n hc_puts(out, f->name);\n hc_putc(out, '=');\n hc_value_write(&f->value, out);\n}\n\nstatic void stream_write(struct hc_slog *s,\n\t\t\t const size_t n,\n\t\t\t struct hc_slog_field *fields[]) {\n struct hc_slog_stream *ss = hc_baseof(s, struct hc_slog_stream, slog);\n\n for(size_t i = 0; i < n; i++) {\n struct hc_slog_field *f = fields[i];\n if (i) { hc_puts(ss->out, \", \"); }\n field_write(f, ss->out);\n }\n\n hc_putc(ss->out, '\\n');\n}\n\nstruct hc_slog_stream *_hc_slog_stream_init(struct hc_slog_stream *s,\n\t\t\t\t\t struct hc_stream *out,\n\t\t\t\t\t const struct hc_slog_stream_opts opts) {\n s->slog.deinit = stream_deinit;\n s->slog.write = stream_write;\n s->out = out;\n s->opts = opts;\n return s;\n}\n\nstatic void context_deinit(struct hc_slog *s) {\n struct hc_slog_context *sc = hc_baseof(s, struct hc_slog_context, slog);\n\n for (size_t i = 0; i < sc->length; i++) {\n struct hc_slog_field *f = sc->fields[i];\n field_deinit(f);\n free(f);\n }\n\n free(sc->fields);\n}\n\nstatic void context_write(struct hc_slog *s,\n\t\t\t const size_t n,\n\t\t\t struct hc_slog_field *fields[]) {\n struct hc_slog_context *c = hc_baseof(s, struct hc_slog_context, slog);\n struct hc_slog_field *fs[c->length + n];\n memcpy(fs, c->fields, sizeof(struct hc_slog_field *) * c->length);\n memcpy(fs + c->length, fields, sizeof(struct hc_slog_field *) * n);\n slog_write(c->parent, c->length + n, fs);\n}\n\nstruct hc_slog_context *hc_slog_context_init(struct hc_slog_context *c,\n\t\t\t\t\t size_t length,\n\t\t\t\t\t struct hc_slog_field *fields[]) {\n c->slog.deinit = context_deinit;\n c->slog.write = context_write;\n c->parent = hc_slog();\n c->length = length;\n size_t s = sizeof(struct hc_slog_field *) * length;\n c->fields = malloc(s);\n memcpy(c->fields, fields, s);\n return c;\n}\n"], ["/hacktical-c/stream1/tests.c", "#include \n#include \n#include \"stream1.h\"\n\nvoid stream1_tests() {\n struct hc_memory_stream s;\n hc_memory_stream_init(&s, &hc_malloc_default);\n hc_defer(hc_stream_deinit(&s.stream));\n hc_printf(&s.stream, \"%s%d\", \"foo\", 42);\n assert(strcmp(\"foo42\", hc_memory_stream_string(&s)) == 0);\n}\n"], ["/hacktical-c/dynamic/tests.c", "#include \n#include \"dynamic.h\"\n\nvoid dynamic_tests() {\n const char *out = \"/var/tmp/libtest.so\";\n \n hc_compile(\"#include \\n\"\n\t \"int test() { return 42; }\",\n\t out,\n\t .cflags = (const char *[]){\"-Wall\",\n\t\t\t\t\t\"-fsanitize=undefined\",\n\t\t\t\t\tNULL});\n\n struct hc_dlib lib;\n hc_dlib_init(&lib, out);\n hc_defer(hc_dlib_deinit(&lib));\n int (*fn)() = hc_dlib_find(&lib, \"test\");\n assert(fn() == 42);\n}\n"], ["/hacktical-c/malloc2/malloc2.c", "#include \n#include \n#include \"error/error.h\"\n#include \"macro/macro.h\"\n#include \"malloc2.h\"\n\n/* Memo */\n\nstruct memo {\n size_t size;\n uint8_t data[];\n};\n\nstatic void *memo_acquire(struct hc_malloc *a, size_t size) {\n struct hc_memo_alloc *ma = hc_baseof(a, struct hc_memo_alloc, malloc);\n\n if (hc_set_length(&ma->memo)) {\n bool ok = false;\n size_t i = hc_set_index(&ma->memo, &size, &ok);\n\n if (ok) {\n struct hc_vector *is = &ma->memo.items;\n struct memo *m = *(struct memo **)hc_vector_get(is, i);\n hc_vector_delete(is, i, 1);\n return m->data;\n }\n }\n\n struct memo *m = hc_acquire(ma->source, sizeof(struct memo) + size);\n m->size = size;\n return m->data;\n}\n\nstatic void memo_release(struct hc_malloc *a, void *p) {\n struct hc_memo_alloc *ma = hc_baseof(a, struct hc_memo_alloc, malloc);\n struct memo *m = hc_baseof(p, struct memo, data);\n *(struct memo **)hc_set_add(&ma->memo, &m->size, true) = m;\n}\n\nstatic enum hc_order memo_cmp(const void *l, const void *r) {\n return hc_cmp(*(size_t *)l, *(size_t *)r);\n}\n\nstatic const void *memo_key(const void *p) {\n struct memo *m = *(struct memo **)p;\n return &m->size;\n}\n\nstruct hc_memo_alloc *hc_memo_alloc_init(struct hc_memo_alloc *a,\n\t\t\t\t\t struct hc_malloc *source) {\n a->malloc.acquire = memo_acquire;\n a->malloc.release = memo_release;\n a->source = source;\n hc_set_init(&a->memo, &hc_malloc_default, sizeof(struct memo *), memo_cmp);\n a->memo.key = memo_key;\n return a;\n}\n\nvoid hc_memo_alloc_deinit(struct hc_memo_alloc *a) {\n hc_vector_do(&a->memo.items, _m) {\n struct memo *m = *(struct memo **)_m;\n hc_release(a->source, m);\n }\n \n hc_set_deinit(&a->memo);\n}\n\n/* Slab */\n\nstruct slab {\n struct hc_list slabs;\n uint8_t *next;\n uint8_t memory[];\n};\n\nstatic struct slab *add_slab(struct hc_slab_alloc *a, const size_t size) {\n struct slab *s = hc_acquire(a->source, sizeof(struct slab) + size);\n hc_list_push_front(&a->slabs, &s->slabs);\n s->next = s->memory;\n return s;\n}\n\nstatic struct slab *get_slab(struct hc_slab_alloc *a, const size_t size) {\n if (size > a->slab_size) {\n return add_slab(a, size);\n }\n\n struct slab *result = NULL;\n\n hc_list_do(&a->slabs, sl) {\n struct slab *s = hc_baseof(sl, struct slab, slabs);\n uint8_t *p = hc_align(s->next, size);\n\n if (p + size > s->memory + a->slab_size) {\n break;\n }\n\n result = s;\n }\n\n return result ? result : add_slab(a, a->slab_size);\n}\n\nstatic void *slab_acquire(struct hc_malloc *a, const size_t size) {\n struct hc_slab_alloc *sa = hc_baseof(a, struct hc_slab_alloc, malloc);\n struct slab *s = get_slab(sa, size);\n uint8_t *p = hc_align(s->next, size);\n s->next = p + size;\n\n while (s->slabs.next != &s->slabs) {\n struct slab *ns = hc_baseof(s->slabs.next, struct slab, slabs);\n\n if (ns->next - ns->memory > s->next - s->memory) {\n hc_list_shift_back(&s->slabs);\n } else {\n break;\n }\n }\n \n return p;\n}\n\nstatic void slab_release(struct hc_malloc *a, void *p) {\n // Do nothing\n}\n\nstruct hc_slab_alloc *hc_slab_alloc_init(struct hc_slab_alloc *a,\n\t\t\t\t\t struct hc_malloc *source,\n\t\t\t\t\t const size_t slab_size) {\n a->malloc.acquire = slab_acquire;\n a->malloc.release = slab_release;\n a->source = source;\n hc_list_init(&a->slabs);\n a->slab_size = slab_size;\n return a;\n}\n\nvoid hc_slab_alloc_deinit(struct hc_slab_alloc *a) {\n hc_list_do(&a->slabs, _s) {\n struct slab *s = hc_baseof(_s, struct slab, slabs);\n hc_release(a->source, s);\n }\n}\n"], ["/hacktical-c/reflect/tests.c", "#include \n#include \"reflect.h\"\n\nvoid reflect_tests() {\n struct hc_value v;\n hc_value_init(&v, &HC_STRING)->as_string = strdup(\"foo\");\n hc_defer(hc_value_deinit(&v));\n struct hc_value c;\n hc_value_copy(&c, &v);\n hc_defer(hc_value_deinit(&c));\n assert(strcmp(c.as_string, v.as_string) == 0);\n}\n"], ["/hacktical-c/reflect/reflect.c", "#include \n#include \n#include \n\n#include \"reflect.h\"\n#include \"stream1/stream1.h\"\n\nstruct hc_value *hc_value_init(struct hc_value *v, const struct hc_type *t) {\n v->type = t;\n return v;\n}\n\nvoid hc_value_deinit(struct hc_value *v) {\n if (v->type->deinit) {\n v->type->deinit(v);\n }\n}\n\nstruct hc_value *hc_value_copy(struct hc_value *dst, struct hc_value *src) {\n const struct hc_type *t = src->type;\n \n if (t->copy) {\n dst->type = t;\n t->copy(dst, src);\n } else {\n *dst = *src;\n }\n\n return dst;\n}\n\nvoid hc_value_print(struct hc_value *v, struct hc_stream *out) {\n if (v->type->print) {\n v->type->print(v, out);\n } else {\n hc_value_write(v, out);\n }\n}\n\nvoid hc_value_write(struct hc_value *v, struct hc_stream *out) {\n assert(v->type->write);\n v->type->write(v, out);\n}\n\nstatic void bool_write(const struct hc_value *v, struct hc_stream *out) {\n hc_puts(out, v->as_bool ? \"true\" : \"false\");\n}\n\nconst struct hc_type HC_BOOL = {\n .name = \"Bool\",\n .copy = NULL,\n .write = bool_write\n};\n\nstatic void fix_write(const struct hc_value *v, struct hc_stream *out) {\n hc_fix_print(v->as_fix, out);\n}\n\nconst struct hc_type HC_FIX = {\n .name = \"Fix\",\n .copy = NULL,\n .write = fix_write\n};\n\nstatic void int_write(const struct hc_value *v, struct hc_stream *out) {\n hc_printf(out, \"%d\", v->as_int);\n}\n\nconst struct hc_type HC_INT = {\n .name = \"Int\",\n .copy = NULL,\n .write = int_write\n};\n\nstatic void string_copy(struct hc_value *dst, struct hc_value *src) {\n dst->as_string = strdup(src->as_string);\n}\n\nstatic void string_deinit(struct hc_value *v) {\n free(v->as_string);\n}\n\nstatic void string_print(const struct hc_value *v, struct hc_stream *out) {\n hc_puts(out, v->as_string);\n}\n\nstatic void string_write(const struct hc_value *v, struct hc_stream *out) {\n hc_putc(out, '\"');\n string_print(v, out);\n hc_putc(out, '\"');\n}\n\nconst struct hc_type HC_STRING = {\n .name = \"String\",\n .copy = string_copy,\n .deinit = string_deinit,\n .print = string_print,\n .write = string_write\n};\n\nstatic void time_write(const struct hc_value *v, struct hc_stream *out) {\n hc_time_printf(&v->as_time, HC_TIME_FORMAT, out);\n}\n\nconst struct hc_type HC_TIME = {\n .name = \"Time\",\n .copy = NULL,\n .write = time_write\n};\n"], ["/hacktical-c/vm/tests.c", "#include \n\n#include \"vm.h\"\n#include \"malloc1/malloc1.h\"\n\nstatic void emit_tests() {\n struct hc_vm vm;\n hc_vm_init(&vm, &hc_malloc_default);\n hc_defer(hc_vm_deinit(&vm));\n struct hc_push_op op;\n hc_value_init(&op.value, &HC_FIX)->as_fix = hc_fix(0, 42);\n hc_vm_emit(&vm, &HC_PUSH, &op);\n hc_vm_eval(&vm, 0, -1);\n assert(vm.stack.length == 1);\n assert(hc_vm_pop(&vm)->as_fix == op.value.as_fix);\n}\n\nvoid vm_tests() {\n emit_tests();\n}\n"], ["/hacktical-c/chrono/chrono.c", "#include \n#include \n#include \n#include \n\n#include \"chrono.h\"\n#include \"error/error.h\"\n#include \"stream1/stream1.h\"\n\nhc_time_t hc_now() {\n hc_time_t t;\n \n if (!timespec_get(&t.value, TIME_UTC)) {\n hc_throw(\"Failed getting time: %d\", errno);\n }\n\n return t;\n}\n\nhc_time_t hc_time(int year,\n\t\t int month,\n\t\t int day,\n\t\t int hour,\n\t\t int minute,\n\t\t int second) {\n struct tm t = {0};\n t.tm_year = year - 1900;\n t.tm_mon = month - 1;\n t.tm_mday = day;\n t.tm_hour = hour;\n t.tm_min = minute;\n t.tm_sec = second;\n\n hc_time_t result = {0};\n result.value.tv_sec = timegm(&t);\n return result;\n}\n\nuint64_t hc_time_ns(const hc_time_t *t) {\n const struct timespec now = hc_now().value;\n \n return\n (now.tv_sec - t->value.tv_sec) * 1000000000 +\n (now.tv_nsec - t->value.tv_nsec);\n}\n\nvoid hc_time_print(const hc_time_t *t, const char *m) {\n printf(\"%s%\" PRIu64 \"ns\\n\", m, hc_time_ns(t));\n}\n\nchar *hc_time_sprintf(const hc_time_t *t, const char *spec) {\n struct tm tm;\n gmtime_r(&(t->value.tv_sec), &tm);\n size_t len = 8;\n char *result = malloc(len);\n\n for (;;) {\n const size_t n = strftime(result, len, spec, &tm);\n\n if (n) {\n result[n] = 0;\n break;\n }\n \n len *= 2;\n free(result);\n result = malloc(len);\n }\n \n return result;\n}\n\nvoid hc_time_printf(const hc_time_t *t,\n\t\t const char *spec,\n\t\t struct hc_stream *out) {\n char *s = hc_time_sprintf(t, \"%Y-%m-%dT%H:%M:%S\");\n hc_puts(out, s);\n free(s);\n}\n\nuint64_t hc_sleep(uint64_t ns) {\n struct timespec t = {0};\n t.tv_nsec = ns;\n\n switch (nanosleep(&t, &t)) {\n case 0:\n break;\n case EINTR:\n return t.tv_nsec;\n default:\n hc_throw(\"Failed sleeping: %d\", errno);\n }\n\n return 0;\n}\n"], ["/hacktical-c/set/tests.c", "#include \n#include \n#include \"set.h\"\n\nstruct map_item {\n int k, v;\n};\n\nstatic enum hc_order cmp(const void *x, const void *y) {\n return hc_cmp(*(const int *)x, *(const int *)y);\n}\n\nstatic const void *key(const void *x) {\n return &((const struct map_item *)x)->k;\n}\n\nvoid set_tests() {\n int n = 10;\n struct hc_set s;\n hc_set_init(&s, &hc_malloc_default, sizeof(struct map_item), cmp);\n s.key = key;\n \n for (int i = 0; i < n; i++) {\n struct map_item *it = hc_set_add(&s, &i, false);\n *it = (struct map_item){.k = i, .v = i};\n }\n\n assert(hc_set_length(&s) == n);\n \n for (int i = 0; i < n; i++) {\n struct map_item *it = hc_set_find(&s, &i);\n assert(it);\n assert(it->k == i);\n assert(it->v == i);\n }\n\n hc_set_clear(&s);\n assert(hc_set_length(&s) == 0); \n hc_set_deinit(&s);\n}\n"], ["/hacktical-c/malloc1/tests.c", "#include \n#include \"malloc1.h\"\n\nvoid malloc1_tests() {\n assert(hc_align(0, 4) == 0);\n assert(hc_align(1, 4) == 4);\n assert(hc_align(3, 4) == 4);\n assert(hc_align(4, 4) == 4);\n assert(hc_align(5, 4) == 8);\n\n const int s = 1024;\n struct hc_bump_alloc a;\n hc_bump_alloc_init(&a, &hc_malloc_default, s);\n hc_defer(hc_bump_alloc_deinit(&a));\n assert(a.size == s);\n assert(a.offset == 0);\n\n int *ip = hc_acquire(&a.malloc, sizeof(int));\n *ip = 42;\n \n long *lp = hc_acquire(&a.malloc, sizeof(long));\n *lp = 42L;\n \n assert(a.offset >= sizeof(int) + sizeof(long));\n bool caught = false;\n \n void on_catch(struct hc_error *e) {\n assert(hc_streq(e->message, HC_NO_MEMORY) == 0);\n caught = true;\n }\n \n hc_catch(on_catch) {\n hc_acquire(&a.malloc, s);\n assert(false);\n }\n\n assert(caught);\n}\n"], ["/hacktical-c/error/error.c", "#include \n#include \n#include \n#include \n\n#include \"error.h\"\n#include \"malloc1/malloc1.h\"\n#include \"vector/vector.h\"\n\n__thread struct hc_error *hc_error = NULL;\n\nstatic struct hc_vector *handlers() {\n static bool init = true;\n static __thread struct hc_vector handlers;\n\n if (init) {\n hc_vector_init(&handlers, &hc_malloc_default, sizeof(jmp_buf));\n init = false;\n }\n \n return &handlers;\n}\n\nvoid hc_catch_push(jmp_buf h) {\n memcpy((jmp_buf *)hc_vector_push(handlers()), h, sizeof(jmp_buf));\n}\n\nvoid hc_catch_pop() {\n hc_vector_pop(handlers());\n}\n\nvoid hc_errors_deinit() {\n hc_vector_deinit(handlers());\n}\n\nvoid _hc_throw(struct hc_error *e) {\n struct hc_vector *hs = handlers();\n\n if (!hs->length) {\n fputs(e->message, stderr);\n hc_error_free(e);\n abort();\n }\n \n jmp_buf t;\n memcpy(t, *(jmp_buf *)hc_vector_pop(hs), sizeof(jmp_buf));\n hc_error = e;\n longjmp(t, 1);\n}\n\nstruct hc_error *hc_error_new(const char *message, ...) {\n va_list args;\n va_start(args, message);\n \n va_list tmp_args;\n va_copy(tmp_args, args);\n int len = vsnprintf(NULL, 0, message, tmp_args);\n va_end(tmp_args);\n\n if (len < 0) {\n vfprintf(stderr, message, args);\n abort();\n }\n \n len++;\n struct hc_error *e = malloc(sizeof(struct hc_error));\n e->message = malloc(len);\n vsnprintf(e->message, len, message, args);\n va_end(args);\n return e;\n}\n\nvoid hc_error_free(struct hc_error *e) {\n free(e->message);\n free(e);\n}\n\nbool hc_streq(const char *l, const char *r) {\n for (; *l && *l == *r; l++, r++);\n return *l == *r;\n}\n"], ["/hacktical-c/malloc2/tests.c", "#include \n#include \"malloc2.h\"\n\nstatic void memo_tests() {\n struct hc_memo_alloc a;\n hc_memo_alloc_init(&a, &hc_malloc_default);\n\n int *ip1 = hc_acquire(&a.malloc, sizeof(int));\n \n long *lp = hc_acquire(&a.malloc, sizeof(long));\n assert((int *)lp != ip1);\n *lp = 42;\n \n hc_release(&a.malloc, ip1);\n int *ip2 = hc_acquire(&a.malloc, sizeof(int));\n assert(ip2 == ip1);\n *ip2 = 42;\n \n int *ip3 = hc_acquire(&a.malloc, sizeof(int));\n assert(ip3 != ip1);\n *ip3 = 42;\n \n hc_release(&a.malloc, lp);\n hc_release(&a.malloc, ip2);\n hc_release(&a.malloc, ip3);\n\n hc_memo_alloc_deinit(&a);\n}\n\nstatic void slab_tests() {\n struct hc_slab_alloc a;\n hc_slab_alloc_init(&a, &hc_malloc_default, 2 * sizeof(int));\n assert(a.slab_size == 2 * sizeof(int));\n\n const int *p1 = hc_acquire(&a.malloc, sizeof(int));\n const int *p2 = hc_acquire(&a.malloc, sizeof(int));\n assert(p2 == p1 + 1);\n\n const int *p3 = hc_acquire(&a.malloc, sizeof(int));\n assert(p3 > p2 + 1);\n \n const int *p4 = hc_acquire(&a.malloc, 10 * sizeof(int));\n assert(p4 > p3 + 1);\n\n hc_slab_alloc_deinit(&a);\n}\n\nvoid malloc2_tests() {\n memo_tests();\n slab_tests();\n}\n"], ["/hacktical-c/task/tests.c", "#include \n#include \n#include \"task.h\"\n\nstruct my_task {\n struct hc_task task;\n int *value;\n};\n\nstatic void producer(struct hc_task *task) {\n int *value = hc_baseof(task, struct my_task, task)->value;\n \n switch (task->state) {\n case 0:\n assert(*value == 0);\n (*value)++;\n hc_task_yield(task);\n assert(*value == 2);\n (*value)++;\n }\n \n task->done = true;\n}\n\nstatic void consumer(struct hc_task *task) {\n int *value = hc_baseof(task, struct my_task, task)->value;\n\n switch (task->state) {\n case 0:\n assert(*value == 1);\n (*value)++;\n hc_task_yield(task);\n assert(*value == 3);\n (*value)++;\n }\n \n task->done = true;\n}\n \nvoid task_tests() {\n struct hc_task_list tl;\n hc_task_list_init(&tl);\n \n int value = 0; \n\n struct my_task pt = {.value = &value};\n hc_task_init(&pt.task, &tl, &producer);\n\n struct my_task ct = {.value = &value};\n hc_task_init(&ct.task, &tl, &consumer);\n\n hc_task_list_run(&tl);\n assert(value == 4);\n}\n"], ["/hacktical-c/macro/tests.c", "#include \n#include \"macro.h\"\n\nvoid macro_tests() {\n int hc_id(foo, bar) = 42;\n assert(foobar == 42);\n\n assert(hc_min(7, 42) == 7);\n assert(hc_max(7.0, 42.0) == 42.0);\n\n {\n int foo = 0;\n \n {\n hc_defer(assert(foo++ == 1));\n hc_defer(assert(foo++ == 0));\n }\n\n assert(foo == 2);\n }\n}\n"], ["/hacktical-c/malloc2/benchmarks.c", "#include \n\n#include \"chrono/chrono.h\"\n#include \"malloc2.h\"\n\n#define N 1000\n#define MAX_SIZE 64\n#define GET_SIZE() ((rand() % MAX_SIZE) + 1)\n\nstatic void run_malloc() {\n int *ps[N];\n hc_time_t t = hc_now();\n \n for (int i = 0; i < N; i++) {\n ps[i] = malloc(GET_SIZE());\n }\n\n for (int i = 0; i < N; i++) {\n free(ps[i]);\n }\n\n hc_time_print(&t, \"malloc: \"); \n}\n\nstatic void run_bump() {\n struct hc_bump_alloc a;\n hc_bump_alloc_init(&a, &hc_malloc_default, N * MAX_SIZE);\n hc_time_t t = hc_now();\n\n for (int i = 0; i < N; i++) {\n hc_acquire(&a.malloc, GET_SIZE());\n }\n\n hc_bump_alloc_deinit(&a);\n hc_time_print(&t, \"bump: \");\n}\n\nstatic void run_slab() {\n struct hc_slab_alloc a;\n hc_slab_alloc_init(&a, &hc_malloc_default, N);\n hc_time_t t = hc_now();\n\n for (int i = 0; i < N; i++) {\n hc_acquire(&a.malloc, GET_SIZE());\n }\n\n hc_slab_alloc_deinit(&a);\n hc_time_print(&t, \"slab: \");\n}\n\nvoid malloc2_benchmarks() {\n const int s = time(NULL);\n \n srand(s);\n run_malloc();\n\n srand(s);\n run_bump();\n\n srand(s);\n run_slab();\n}\n"], ["/hacktical-c/list/tests.c", "#include \n#include \"list.h\"\n\nstruct list_item {\n struct hc_list ls;\n int value;\n};\n \nvoid list_tests() {\n struct hc_list head;\n hc_list_init(&head);\n\n const int n = 10;\n struct list_item items[n];\n \n for (int i = 0; i < n; i++) {\n items[i].value = i;\n hc_list_push_back(&head, &items[i].ls);\n }\n\n int i = 0;\n \n hc_list_do(&head, il) {\n assert(hc_baseof(il, struct list_item, ls)->value == i++);\n }\n}\n"], ["/hacktical-c/set/set.c", "#include \n#include \"set.h\"\n\nstruct hc_set *hc_set_init(struct hc_set *s,\n\t\t\t struct hc_malloc *malloc,\n\t\t\t const size_t item_size,\n\t\t\t hc_cmp_t cmp) {\n hc_vector_init(&s->items, malloc, item_size);\n s->cmp = cmp;\n s->key = NULL;\n return s;\n}\n\nvoid hc_set_deinit(struct hc_set *s) {\n hc_vector_deinit(&s->items);\n}\n\nsize_t hc_set_index(const struct hc_set *s, const void *key, bool *ok) {\n size_t min = 0, max = s->items.length;\n\n while (min < max) {\n const size_t i = (min+max)/2;\n const void *v = hc_vector_get_const(&s->items, i);\n const void *k = s->key ? s->key(v) : v;\n\n switch (s->cmp(key, k)) {\n case HC_LT:\n max = i;\n break;\n case HC_GT:\n min = i+1;\n break;\n default:\n if (ok) {\n\t*ok = true;\n }\n \n return i;\n }\n }\n\n return min;\n}\n\nsize_t hc_set_length(const struct hc_set *s) {\n return s->items.length;\n}\n\nvoid *hc_set_find(struct hc_set *s, const void *key) {\n bool ok = false;\n const size_t i = hc_set_index(s, key, &ok);\n return ok ? hc_vector_get(&s->items, i) : NULL;\n}\n\nvoid *hc_set_add(struct hc_set *s, const void *key, const bool force) {\n bool ok = false;\n const size_t i = hc_set_index(s, key, &ok);\n\n if (ok && !force) {\n return NULL;\n }\n \n return hc_vector_insert(&s->items, i, 1);\n}\n\nvoid hc_set_clear(struct hc_set *s) {\n hc_vector_clear(&s->items);\n}\n"], ["/hacktical-c/fix/tests.c", "#include \n#include \n#include \n#include \"fix.h\"\n\nstatic void test_add() {\n assert(hc_fix_add(hc_fix(2, 175), hc_fix(2, 25)) ==\n\t hc_fix(2, 200));\n \n assert(hc_fix_add(hc_fix(2, 175), hc_fix(2, -25)) ==\n\t hc_fix(2, 150));\n}\n\nstatic void test_div() {\n assert(hc_fix_div(hc_fix(2, 150), hc_fix(0, 2)) ==\n\t hc_fix(2, 75));\n\n assert(hc_fix_div(hc_fix(2, 150), hc_fix(0, -2)) ==\n\t hc_fix(2, -75));\n\n assert(hc_fix_div(hc_fix(2, -150), hc_fix(0, -2)) ==\n\t hc_fix(2, 75));\n}\n\nstatic void test_mul() {\n assert(hc_fix_mul(hc_fix(2, 150), hc_fix(1, 5)) ==\n\t hc_fix(2, 75));\n\n assert(hc_fix_mul(hc_fix(2, 150), hc_fix(1, -5)) ==\n\t hc_fix(2, -75));\n\n assert(hc_fix_mul(hc_fix(2, -150), hc_fix(1, -5)) ==\n\t hc_fix(2, 75));\n}\n\nstatic void test_new() {\n hc_fix_t x = hc_fix(2, -125);\n assert(hc_fix_exp(x) == 2);\n assert(hc_fix_val(x) == -125);\n assert(hc_fix_int(x) == -1);\n assert(hc_fix_frac(x) == -25);\n assert(hc_fix_double(x) == -1.25);\n}\n\nstatic void test_sub() {\n assert(hc_fix_sub(hc_fix(2, 175), hc_fix(2, 25)) ==\n\t hc_fix(2, 150));\n\n assert(hc_fix_sub(hc_fix(2, 175), hc_fix(2, -25)) ==\n\t hc_fix(2, 200));\n}\n\nvoid fix_tests() {\n test_add();\n test_div();\n test_mul();\n test_new();\n test_sub();\n}\n"], ["/hacktical-c/fix/fix.c", "#include \n#include \n\n#include \"fix.h\"\n#include \"macro/macro.h\"\n#include \"stream1/stream1.h\"\n\nuint32_t hc_scale(const uint8_t exp) {\n static const uint32_t scale[HC_FIX_MAX_EXP+1] = {\n 1,\n 10,\n 100,\n 1000,\n 10000,\n 100000,\n 1000000,\n 10000000};\n\n assert(exp <= HC_FIX_MAX_EXP);\n return scale[exp];\n}\n\nhc_fix_t hc_fix(const uint8_t exp, const int64_t val) {\n return (hc_fix_t)hc_bitmask(exp, HC_FIX_EXP) +\n (hc_fix_t)(((val < 0) ? 1 : 0) << HC_FIX_EXP) +\n (hc_fix_t)(hc_abs(val) << HC_FIX_HDR);\n}\n\nuint8_t hc_fix_exp(const hc_fix_t x) {\n return hc_bitmask(x, HC_FIX_EXP);\n}\n\nint64_t hc_fix_val(const hc_fix_t x) {\n const int64_t v = x >> HC_FIX_HDR;\n return ((x >> HC_FIX_EXP) & 1) ? -v : v;\n}\n\nint64_t hc_fix_int(const hc_fix_t x) {\n return hc_fix_val(x) / hc_scale(hc_fix_exp(x));\n}\n\nint64_t hc_fix_frac(const hc_fix_t x) {\n const int64_t xv = hc_fix_val(x);\n const uint32_t xs = hc_scale(hc_fix_exp(x));\n return xv - (xv / xs) * xs;\n}\n\ndouble hc_fix_double(const hc_fix_t x) {\n return hc_fix_val(x) / (double)hc_scale(hc_fix_exp(x));\n}\n\nhc_fix_t hc_fix_add(const hc_fix_t x, const hc_fix_t y) {\n const uint8_t xe = hc_fix_exp(x);\n const uint8_t ye = hc_fix_exp(y);\n\n if (xe == ye) {\n return hc_fix(xe, hc_fix_val(x) + hc_fix_val(y));\n }\n \n return hc_fix(xe, hc_fix_val(x) +\n\t\thc_fix_val(y) * hc_scale(xe) / hc_scale(ye));\n}\n\nhc_fix_t hc_fix_sub(const hc_fix_t x, const hc_fix_t y) {\n const uint8_t xe = hc_fix_exp(x);\n const uint8_t ye = hc_fix_exp(y);\n\n if (xe == ye) {\n return hc_fix(xe, hc_fix_val(x) - hc_fix_val(y));\n }\n\n return hc_fix(xe, hc_fix_val(x) -\n\t\thc_fix_val(y) * hc_scale(xe) / hc_scale(ye));\n}\n\nhc_fix_t hc_fix_mul(const hc_fix_t x, const hc_fix_t y) {\n return hc_fix(hc_fix_exp(x), hc_fix_val(x) *\n\t\thc_fix_val(y) / hc_scale(hc_fix_exp(y)));\n}\n\nhc_fix_t hc_fix_div(const hc_fix_t x, const hc_fix_t y) {\n return hc_fix(hc_fix_exp(x), hc_fix_val(x) /\n\t\thc_fix_val(y) / hc_scale(hc_fix_exp(y)));\n}\n\nvoid hc_fix_print(const hc_fix_t v, struct hc_stream *out) {\n hc_printf(out,\n\t \"%\" PRId64 \".%\" PRId64,\n\t hc_fix_int(v),\n\t hc_fix_frac(v));\n}\n"], ["/hacktical-c/vector/tests.c", "#include \n#include \n#include \"vector.h\"\n\nvoid vector_tests() {\n struct hc_vector v;\n hc_vector_init(&v, &hc_malloc_default, sizeof(int));\n const int n = 100;\n \n for (int i = 0; i < n; i++) {\n *(int *)hc_vector_push(&v) = i;\n }\n\n {\n int i = 0;\n \n hc_vector_do(&v, it) {\n assert(*(int *)it == i++);\n }\n }\n \n assert(v.length == n);\n \n for (int i = 0; i < n; i++) {\n assert(*(int *)hc_vector_get(&v, i) == i);\n }\n\n assert(*(int *)hc_vector_pop(&v) == n-1);\n assert(*(int *)hc_vector_peek(&v) == n-2);\n assert(v.length == n-1);\n \n for (int i = 0; i < n-1; i++) {\n assert(*(int *)hc_vector_get(&v, i) == i);\n }\n\n assert(hc_vector_delete(&v, 0, 1));\n assert(v.length == n-2);\n\n for (int i = 1; i < n-1; i++) {\n assert(*(int *)hc_vector_get(&v, i-1) == i);\n }\n\n (*(int *)hc_vector_insert(&v, 0, 1) = 0);\n assert(v.length == n-1);\n\n for (int i = 0; i < n-1; i++) {\n assert(*(int *)hc_vector_get(&v, i) == i);\n }\n\n hc_vector_clear(&v);\n assert(v.length == 0);\n \n hc_vector_deinit(&v);\n}\n"], ["/hacktical-c/error/tests.c", "#include \n#include \n#include \n#include \n\n#include \"error.h\"\n\nvoid error_tests() {\n void on_catch(struct hc_error *e) {\n assert(hc_streq(\"E123\", e->message) == 0);\n }\n \n hc_catch(on_catch) {\n hc_throw(\"E123 Going %s\", \"Down!\");\n }\n}\n"], ["/hacktical-c/tests.c", "#include \"error/error.h\"\n\n#include \"chrono/tests.c\"\n#include \"dsl/tests.c\"\n#include \"dynamic/tests.c\"\n#include \"error/tests.c\"\n#include \"fix/tests.c\"\n#include \"list/tests.c\"\n#include \"macro/tests.c\"\n#include \"malloc1/tests.c\"\n#include \"malloc2/tests.c\"\n#include \"reflect/tests.c\"\n#include \"set/tests.c\"\n#include \"slog/tests.c\"\n#include \"stream1/tests.c\"\n#include \"task/tests.c\"\n#include \"vector/tests.c\"\n#include \"vm/tests.c\"\n\nint main() {\n chrono_tests();\n dsl_tests();\n dynamic_tests();\n error_tests();\n fix_tests();\n list_tests();\n macro_tests();\n malloc1_tests();\n malloc2_tests();\n reflect_tests();\n set_tests();\n slog_tests();\n stream1_tests();\n task_tests();\n vector_tests();\n vm_tests();\n\n hc_errors_deinit();\n return 0;\n}\n"], ["/hacktical-c/task/task.c", "#include \n#include \n#include \"task.h\"\n\nstruct hc_task *hc_task_init(struct hc_task *t,\n\t\t\t struct hc_task_list *tl,\n\t\t\t hc_task_body body) {\n t->body = body;\n t->state = 0;\n t->done = false;\n hc_list_push_back(&tl->tasks, &t->list);\n return t;\n}\n\nstruct hc_task_list *hc_task_list_init(struct hc_task_list *tl) {\n hc_list_init(&tl->tasks);\n return tl;\n}\n\nvoid hc_task_list_run(const struct hc_task_list *tl) {\n bool all_done = false;\n \n while (!all_done) {\n all_done = true;\n \n hc_list_do(&tl->tasks, i) {\n struct hc_task *t = hc_baseof(i, struct hc_task, list);\n\n if (!t->done) {\n\tt->body(t);\n\tall_done = false;\n }\n }\n }\n}\n"], ["/hacktical-c/chrono/tests.c", "#include \n#include \"chrono.h\"\n\nvoid chrono_tests() {\n hc_time_t t = hc_now();\n const int ns = 1000;\n assert(hc_sleep(ns) == 0);\n assert(hc_time_ns(&t) >= ns);\n}\n"], ["/hacktical-c/benchmarks.c", "#include \"error/error.h\"\n\n#include \"dsl/benchmarks.c\"\n#include \"fix/benchmarks.c\"\n#include \"malloc2/benchmarks.c\"\n\nint main() {\n fix_benchmarks();\n malloc2_benchmarks();\n dsl_benchmarks();\n\n hc_errors_deinit();\n return 0;\n}\n"], ["/hacktical-c/malloc1/malloc1.c", "#include \n#include \n#include \"error/error.h\"\n#include \"macro/macro.h\"\n#include \"malloc1.h\"\n\nstatic void *default_acquire(struct hc_malloc *m, size_t size) {\n return malloc(size);\n}\n\nstatic void default_release(struct hc_malloc *m, void *p) {\n free(p);\n}\n\nstruct hc_malloc hc_malloc_default = {.acquire = default_acquire,\n\t\t\t\t .release = default_release};\n\n__thread struct hc_malloc *hc_mallocp = NULL;\n\n/* Bump */\n\nstatic void *bump_acquire(struct hc_malloc *a, size_t size) {\n if (size <= 0) {\n hc_throw(HC_INVALID_SIZE);\n } \n\n struct hc_bump_alloc *ba = hc_baseof(a, struct hc_bump_alloc, malloc);\n \n if (ba->size - ba->offset < size) {\n hc_throw(HC_NO_MEMORY);\n } \n\n uint8_t *p = ba->memory + ba->offset;\n uint8_t *pa = hc_align(p, size);\n ba->offset = ba->offset + pa - p + size;\n return pa;\n}\n\nstatic void bump_release(struct hc_malloc *a, void *p) {\n //Do nothing\n}\n\nvoid hc_bump_alloc_init(struct hc_bump_alloc *a,\n\t\t\tstruct hc_malloc *source,\n\t\t\tsize_t size) {\n a->malloc.acquire = bump_acquire;\n a->malloc.release = bump_release;\n a->source = source;\n a->size = size;\n a->offset = 0;\n a->memory = hc_acquire(source, size);\n}\n\nvoid hc_bump_alloc_deinit(struct hc_bump_alloc *a) {\n hc_release(a->source, a->memory);\n}\n"], ["/hacktical-c/vector/vector.c", "#include \n#include \n#include \n#include \n#include \"vector.h\"\n#include \"malloc1/malloc1.h\"\n\nstatic void grow(struct hc_vector *v) {\n hc_vector_grow(v, v->capacity ? v->capacity*2 : 2);\n}\n\nstruct hc_vector *hc_vector_init(struct hc_vector *v,\n\t\t\t\t struct hc_malloc *malloc,\n\t\t\t\t const size_t item_size) {\n v->malloc = malloc;\n v->item_size = item_size;\n v->capacity = 0;\n v->length = 0;\n v->start = v->end = NULL;\n return v;\n}\n\nvoid hc_vector_deinit(struct hc_vector *v) {\n if (v->start) { hc_release(v->malloc, v->start); }\n}\n\nvoid hc_vector_grow(struct hc_vector *v, const size_t capacity) {\n v->capacity = capacity; \n size_t size = v->item_size * (v->capacity+1);\n uint8_t *new_start = hc_acquire(v->malloc, size);\n\n if (v->start) {\n memmove(new_start, v->start, v->length * v->item_size);\n hc_release(v->malloc, v->start); \n }\n \n v->start = new_start;\n v->end = v->start + v->item_size*v->length;\n}\n\nvoid hc_vector_clear(struct hc_vector *v) {\n v->length = 0;\n v->end = v->start;\n}\n\nvoid *hc_vector_get(struct hc_vector *v, const size_t i) {\n return v->start ? v->start + v->item_size*i : NULL;\n}\n\nconst void *hc_vector_get_const(const struct hc_vector *v, const size_t i) {\n return v->start ? v->start + v->item_size*i : NULL;\n}\n\nvoid *hc_vector_push(struct hc_vector *v) {\n if (v->length == v->capacity) { grow(v); }\n void *p = v->end;\n v->end += v->item_size;\n v->length++;\n return p;\n}\n\nvoid *hc_vector_peek(struct hc_vector *v) {\n return v->length ? v->end - v->item_size : NULL;\n}\n\nvoid *hc_vector_pop(struct hc_vector *v) {\n if (!v->length) { return NULL; }\n v->end -= v->item_size;\n v->length--;\n return v->end;\n}\n\nvoid *hc_vector_insert(struct hc_vector *v, const size_t i, const size_t n) {\n const size_t m = v->length+n;\n if (m > v->capacity) { hc_vector_grow(v, m); } \n uint8_t *const p = hc_vector_get(v, i);\n\n if (i < v->length) {\n memmove(p + v->item_size*n, p, (v->length - i) * v->item_size);\n }\n \n v->length += n;\n v->end += n*v->item_size;\n return p;\n}\n\nbool hc_vector_delete(struct hc_vector *v, const size_t i, const size_t n) {\n const size_t m = i+n;\n assert(m <= v->length);\n\n if (m < v->length) {\n uint8_t *const p = hc_vector_get(v, i);\n memmove(p, p + n*v->item_size, i + (v->length-n) * v->item_size);\n }\n\n v->length -= n;\n v->end -= n*v->item_size;\n return true;\n}\n"], ["/hacktical-c/list/list.c", "#include \n#include \"list.h\"\n\nvoid hc_list_init(struct hc_list *l) {\n l->prev = l->next = l;\n}\n\nbool hc_list_nil(const struct hc_list *l) {\n return l->prev == l && l->next == l;\n}\n\nstruct hc_list *hc_list_delete(struct hc_list *l) {\n l->prev->next = l->next;\n l->next->prev = l->prev;\n return l;\n}\n\nvoid hc_list_push_front(struct hc_list *l, struct hc_list *it) {\n hc_list_push_back(l->next, it);\n}\n\nstruct hc_list *hc_list_pop_front(struct hc_list *l) {\n struct hc_list *it = l->next;\n return (it == l) ? NULL : hc_list_delete(it);\n}\n\nstruct hc_list *hc_list_peek_front(struct hc_list *l) {\n struct hc_list *it = l->next;\n return (it == l) ? NULL : it;\n}\n\nvoid hc_list_push_back(struct hc_list *l, struct hc_list *it) {\n it->prev = l->prev;\n l->prev->next = it;\n it->next = l;\n l->prev = it;\n}\n\nstruct hc_list *hc_list_pop_back(struct hc_list *l) {\n struct hc_list *it = l->prev;\n return (it == l) ? NULL : hc_list_delete(it);\n}\n\nstruct hc_list *hc_list_peek_back(struct hc_list *l) {\n struct hc_list *it = l->prev;\n return (it == l) ? NULL : it;\n}\n\nvoid hc_list_shift_back(struct hc_list *l) {\n l->next->prev = l->prev;\n l->prev->next = l->next;\n l->prev = l->next;\n l->next = l->next->next;\n l->prev->next = l;\n l->next->prev = l;\n}\n"], ["/hacktical-c/fix/benchmarks.c", "#include \"chrono/chrono.h\"\n#include \"fix.h\"\n\nvoid fix_benchmarks() {\n hc_time_t t;\n const int n = 100000;\n\n t = hc_now();\n double dv = 0;\n \n for (int i = 0; i < n; i++) {\n dv += 0.001;\n }\n\n hc_time_print(&t, \"double: \"); \n\n t = hc_now();\n hc_fix_t fv = hc_fix(3, 0);\n hc_fix_t fd = hc_fix(3, 1);\n \n for (int i = 0; i < n; i++) {\n fv = hc_fix_add(fv, fd);\n }\n\n hc_time_print(&t, \"fix: \"); \n}\n"], ["/hacktical-c/macro/macro.c", "#include \"macro.h\"\n\nsize_t hc_alignof(size_t size) {\n const size_t max = _Alignof(max_align_t);\n if (size >= max) { return max; }\n size_t v = 1;\n for (size_t nv = 1; nv <= size; v = nv, nv = v << 1);\n return v;\n}\n"]], "task_instance_info": {"created_time": "2025-08-20 17:57:20", "created_task_model": "DeepSeek-R1", "function_skeleton": "static void read_call_tests() {}", "code_description": "This code segment functions to: Validate the parsing of structured expressions by verifying that a test input string containing nested elements is correctly interpreted and transformed into a hierarchical representation."}, "inference_content": {"inference_model": "DeepSeek-R1", "inference_result": "```c\n[TASK_BEGIN]\nstatic void read_call_tests() {\n struct hc_sloc sloc = hc_sloc(\"read_call_tests\", 0, 0);\n \n struct hc_list out;\n hc_list_init(&out);\n hc_defer(hc_forms_free(&out));\n\n const char *s = \"(print x)\";\n const char *in = s;\n assert(hc_read_expr(&in, &out, &sloc));\n \n struct hc_form *f = hc_baseof(out.next, struct hc_form, owner);\n assert(f->type == &HC_CALL_FORM);\n \n struct hc_form *arg1 = hc_baseof(f->call.args.next, struct hc_form, owner);\n assert(arg1->type == &HC_ID_FORM);\n assert(strcmp(arg1->id.name, \"print\") == 0);\n \n struct hc_form *arg2 = hc_baseof(arg1->owner.next, struct hc_form, owner);\n assert(arg2->type == &HC_ID_FORM);\n assert(strcmp(arg2->id.name, \"x\") == 0);\n \n assert(arg2->owner.next == NULL);\n}\n[TASK_END]\n```", "inference_time": "2025-08-20 17-57-37"}, "editdistance_info": {"edit_distance": 49.5652, "calculate_time": "2025-08-20 17:57:37", "true_code_clean": "static void read_call_tests() {\n struct hc_sloc sloc = hc_sloc(\"read_call_tests\", 0, 0);\n struct hc_list out;\n hc_list_init(&out);\n hc_defer(hc_forms_free(&out));\n const char *s = \"( foo bar )\";\n const char *in = s;\n assert(hc_read_expr(&in, &out, &sloc));\n struct hc_form *f = hc_baseof(out.next, struct hc_form, owner);\n assert(f->type == &HC_CALL_FORM);\n}", "predict_code_clean": "static void read_call_tests() {\n struct hc_sloc sloc = hc_sloc(\"read_call_tests\", 0, 0);\n struct hc_list out;\n hc_list_init(&out);\n hc_defer(hc_forms_free(&out));\n const char *s = \"(print x)\";\n const char *in = s;\n assert(hc_read_expr(&in, &out, &sloc));\n struct hc_form *f = hc_baseof(out.next, struct hc_form, owner);\n assert(f->type == &HC_CALL_FORM);\n struct hc_form *arg1 = hc_baseof(f->call.args.next, struct hc_form, owner);\n assert(arg1->type == &HC_ID_FORM);\n assert(strcmp(arg1->id.name, \"print\") == 0);\n struct hc_form *arg2 = hc_baseof(arg1->owner.next, struct hc_form, owner);\n assert(arg2->type == &HC_ID_FORM);\n assert(strcmp(arg2->id.name, \"x\") == 0);\n assert(arg2->owner.next == NULL);\n}"}} {"repo_name": "hacktical-c", "file_name": "/hacktical-c/tests.c", "inference_info": {"prefix_code": "#include \"error/error.h\"\n\n#include \"chrono/tests.c\"\n#include \"dsl/tests.c\"\n#include \"dynamic/tests.c\"\n#include \"error/tests.c\"\n#include \"fix/tests.c\"\n#include \"list/tests.c\"\n#include \"macro/tests.c\"\n#include \"malloc1/tests.c\"\n#include \"malloc2/tests.c\"\n#include \"reflect/tests.c\"\n#include \"set/tests.c\"\n#include \"slog/tests.c\"\n#include \"stream1/tests.c\"\n#include \"task/tests.c\"\n#include \"vector/tests.c\"\n#include \"vm/tests.c\"\n\n", "suffix_code": "\n", "middle_code": "int main() {\n chrono_tests();\n dsl_tests();\n dynamic_tests();\n error_tests();\n fix_tests();\n list_tests();\n macro_tests();\n malloc1_tests();\n malloc2_tests();\n reflect_tests();\n set_tests();\n slog_tests();\n stream1_tests();\n task_tests();\n vector_tests();\n vm_tests();\n hc_errors_deinit();\n return 0;\n}", "code_description": null, "fill_type": "FUNCTION_TYPE", "language_type": "c", "sub_task_type": null}, "context_code": [["/hacktical-c/benchmarks.c", "#include \"error/error.h\"\n\n#include \"dsl/benchmarks.c\"\n#include \"fix/benchmarks.c\"\n#include \"malloc2/benchmarks.c\"\n\nint main() {\n fix_benchmarks();\n malloc2_benchmarks();\n dsl_benchmarks();\n\n hc_errors_deinit();\n return 0;\n}\n"], ["/hacktical-c/dynamic/dynamic.c", "#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#include \"dynamic.h\"\n#include \"error/error.h\"\n#include \"malloc1/malloc1.h\"\n#include \"vector/vector.h\"\n\nstruct hc_proc *_hc_proc_init(struct hc_proc *p, char *cmd[]) {\n int fds[2];\n\n if (pipe(fds) == -1) {\n hc_throw(\"Failed creating pipe: %d\", errno);\n }\n \n pid_t child_pid = fork();\n\n switch (child_pid) {\n case 0: {\n if (close(fds[1]) == -1) {\n hc_throw(\"Failed closing pipe writer: %d\", errno);\n }\n \n if (dup2(fds[0], 0) == -1) {\n hc_throw(\"Failed rebinding stdin: %d\", errno);\n }\n \n char *const env[] = {\"PATH=/bin:/sbin\", NULL};\n\n if (execve(cmd[0], cmd, env) == -1) {\n hc_throw(\"Failed to execve '%s': %d\", cmd[0], errno);\n }\n }\n case -1:\n hc_throw(\"Failed forking process: %d\", errno);\n default:\n if (close(fds[0]) == -1) {\n hc_throw(\"Failed closing pipe reader: %d\", errno);\n }\n \n p->pid = child_pid;\n p->in = fds[1];\n break;\n }\n\n return p;\n}\n\nstatic void close_in(struct hc_proc *p) {\n if (p->in != -1 && close(p->in) == -1) {\n hc_throw(\"Failed closing stdin: %d\", errno);\n }\n}\n\nvoid hc_proc_wait(struct hc_proc *p) {\n close_in(p);\n\n if (waitpid(p->pid, NULL, 0) == -1) {\n hc_throw(\"Failed waiting for child process to exit: %d\", errno);\n }\n}\n\nvoid hc_proc_deinit(struct hc_proc *p) {\n close_in(p);\n}\n\nstatic void free_cmd(char **in) {\n for (char **s = in; *s; s++) {\n free(*s);\n }\n}\n\nvoid _hc_compile(const char *code,\n\t\t const char *out,\n\t\t const struct hc_compile_opts opts) {\n hc_array(const char *, pre, \n\t opts.cc, \"-shared\", \"-fpic\", \"-o\", out, \"-xc\");\n \n int n = pre_n + 2;\n for (int i = 0; opts.cflags[i]; i++, n++); \n char *cmd[n];\n int i = 0;\n\n for (; i < pre_n; i++) {\n cmd[i] = strdup(pre_a[i]);\n }\n \n for (; i < n - 2; i++) {\n cmd[i] = strdup(opts.cflags[i - pre_n]);\n }\n\n cmd[i++] = strdup(\"-\");\n cmd[i] = NULL;\n hc_defer(free_cmd(cmd));\n \n struct hc_proc child;\n _hc_proc_init(&child, cmd);\n hc_defer(hc_proc_deinit(&child));\n FILE *in = fdopen(child.in, \"w\");\n\n if (!in) {\n hc_throw(\"Failed opening stdin stream: %d\", errno);\n }\n \n child.in = -1;\n hc_defer(hc_proc_wait(&child));\n hc_defer(fclose(in));\n\n if (fputs(code, in) == EOF) {\n hc_throw(\"Failed writing code: %d\", errno);\n }\n}\n\nstruct hc_dlib *hc_dlib_init(struct hc_dlib *lib, const char *path) {\n lib->handle = dlopen(path, RTLD_NOW);\n\n if (!lib->handle) {\n hc_throw(\"Error opening dynamic library '%s': %s\", path, dlerror());\n }\n \n return lib;\n}\n\nstruct hc_dlib *hc_dlib_deinit(struct hc_dlib *lib) {\n if (dlclose(lib->handle) != 0) {\n hc_throw(\"Failed closing dynamic library: \", dlerror());\n }\n\n return lib;\n}\n\nvoid *hc_dlib_find(const struct hc_dlib *lib, const char *s) {\n dlerror();\n void *v = dlsym(lib->handle, s);\n char *e = dlerror();\n\n if (e) {\n hc_throw(\"Symbol '%s' not found: %s\", e);\n }\n\n return v;\n}\n\nchar *hc_vsprintf(const char *format, va_list args) {\n va_list tmp_args;\n va_copy(tmp_args, args);\n int len = vsnprintf(NULL, 0, format, tmp_args);\n va_end(tmp_args);\n\n if (len < 0) {\n hc_throw(\"Formatting '%s' failed: %d\", format, errno);\n }\n\n len++;\n char *out = malloc(len);\n vsnprintf(out, len, format, args);\n return out;\n} \n"], ["/hacktical-c/vm/tests.c", "#include \n\n#include \"vm.h\"\n#include \"malloc1/malloc1.h\"\n\nstatic void emit_tests() {\n struct hc_vm vm;\n hc_vm_init(&vm, &hc_malloc_default);\n hc_defer(hc_vm_deinit(&vm));\n struct hc_push_op op;\n hc_value_init(&op.value, &HC_FIX)->as_fix = hc_fix(0, 42);\n hc_vm_emit(&vm, &HC_PUSH, &op);\n hc_vm_eval(&vm, 0, -1);\n assert(vm.stack.length == 1);\n assert(hc_vm_pop(&vm)->as_fix == op.value.as_fix);\n}\n\nvoid vm_tests() {\n emit_tests();\n}\n"], ["/hacktical-c/dsl/tests.c", "#include \n#include \n#include \"dsl.h\"\n\nstatic void read_call_tests() {\n struct hc_sloc sloc = hc_sloc(\"read_call_tests\", 0, 0);\n \n struct hc_list out;\n hc_list_init(&out);\n hc_defer(hc_forms_free(&out));\n\n const char *s = \"( foo bar )\";\n const char *in = s;\n assert(hc_read_expr(&in, &out, &sloc));\n \n struct hc_form *f = hc_baseof(out.next, struct hc_form, owner);\n assert(f->type == &HC_CALL_FORM);\n}\n\nstatic void read_id_tests() {\n struct hc_sloc sloc = hc_sloc(\"read_id_tests\", 0, 0);\n \n struct hc_list out;\n hc_list_init(&out);\n hc_defer(hc_forms_free(&out));\n\n const char *s = \"foo\";\n const char *in = s;\n assert(hc_read_expr(&in, &out, &sloc));\n \n struct hc_form *f = hc_baseof(out.next, struct hc_form, owner);\n assert(f->type == &HC_ID_FORM);\n}\n\nstatic void eval_tests() {\n struct hc_dsl dsl;\n hc_dsl_init(&dsl, &hc_malloc_default);\n hc_defer(hc_dsl_deinit(&dsl));\n struct hc_memory_stream out;\n hc_memory_stream_init(&out, &hc_malloc_default);\n hc_defer(hc_stream_deinit(&out.stream));\n dsl.out = &out.stream;\n hc_dsl_set_string(&dsl, \"foo\", \"ghi\");\n hc_dsl_eval(&dsl, \"abc $(print (upcase foo)) def\");\n assert(strcmp(\"abc GHI def\", hc_memory_stream_string(&out)) == 0);\n}\n\nvoid dsl_tests() {\n read_id_tests();\n read_call_tests();\n eval_tests();\n}\n"], ["/hacktical-c/malloc2/tests.c", "#include \n#include \"malloc2.h\"\n\nstatic void memo_tests() {\n struct hc_memo_alloc a;\n hc_memo_alloc_init(&a, &hc_malloc_default);\n\n int *ip1 = hc_acquire(&a.malloc, sizeof(int));\n \n long *lp = hc_acquire(&a.malloc, sizeof(long));\n assert((int *)lp != ip1);\n *lp = 42;\n \n hc_release(&a.malloc, ip1);\n int *ip2 = hc_acquire(&a.malloc, sizeof(int));\n assert(ip2 == ip1);\n *ip2 = 42;\n \n int *ip3 = hc_acquire(&a.malloc, sizeof(int));\n assert(ip3 != ip1);\n *ip3 = 42;\n \n hc_release(&a.malloc, lp);\n hc_release(&a.malloc, ip2);\n hc_release(&a.malloc, ip3);\n\n hc_memo_alloc_deinit(&a);\n}\n\nstatic void slab_tests() {\n struct hc_slab_alloc a;\n hc_slab_alloc_init(&a, &hc_malloc_default, 2 * sizeof(int));\n assert(a.slab_size == 2 * sizeof(int));\n\n const int *p1 = hc_acquire(&a.malloc, sizeof(int));\n const int *p2 = hc_acquire(&a.malloc, sizeof(int));\n assert(p2 == p1 + 1);\n\n const int *p3 = hc_acquire(&a.malloc, sizeof(int));\n assert(p3 > p2 + 1);\n \n const int *p4 = hc_acquire(&a.malloc, 10 * sizeof(int));\n assert(p4 > p3 + 1);\n\n hc_slab_alloc_deinit(&a);\n}\n\nvoid malloc2_tests() {\n memo_tests();\n slab_tests();\n}\n"], ["/hacktical-c/chrono/tests.c", "#include \n#include \"chrono.h\"\n\nvoid chrono_tests() {\n hc_time_t t = hc_now();\n const int ns = 1000;\n assert(hc_sleep(ns) == 0);\n assert(hc_time_ns(&t) >= ns);\n}\n"], ["/hacktical-c/fix/benchmarks.c", "#include \"chrono/chrono.h\"\n#include \"fix.h\"\n\nvoid fix_benchmarks() {\n hc_time_t t;\n const int n = 100000;\n\n t = hc_now();\n double dv = 0;\n \n for (int i = 0; i < n; i++) {\n dv += 0.001;\n }\n\n hc_time_print(&t, \"double: \"); \n\n t = hc_now();\n hc_fix_t fv = hc_fix(3, 0);\n hc_fix_t fd = hc_fix(3, 1);\n \n for (int i = 0; i < n; i++) {\n fv = hc_fix_add(fv, fd);\n }\n\n hc_time_print(&t, \"fix: \"); \n}\n"], ["/hacktical-c/dsl/benchmarks.c", "#include \"chrono/chrono.h\"\n#include \"dsl.h\"\n\nvoid dsl_benchmarks() {\n hc_time_t t;\n const int n = 100000;\n\n char buf[32];\n t = hc_now();\n \n for (int i = 0; i < n; i++) {\n sprintf(buf, \"abc %s def\", \"ghi\");\n }\n\n hc_time_print(&t, \"sprintf: \"); \n \n struct hc_dsl dsl;\n hc_dsl_init(&dsl, &hc_malloc_default);\n hc_defer(hc_dsl_deinit(&dsl));\n struct hc_memory_stream out;\n hc_memory_stream_init(&out, &hc_malloc_default);\n hc_defer(hc_stream_deinit(&out.stream));\n dsl.out = &out.stream;\n hc_dsl_set_string(&dsl, \"foo\", \"ghi\");\n hc_dsl_eval(&dsl, \"abc $(print foo) def\");\n\n t = hc_now();\n\n for (int i = 0; i < n; i++) {\n hc_vector_clear(&out.data);\n hc_vm_eval(&dsl.vm, 0, -1);\n }\n\n hc_time_print(&t, \"dsl: \");\n}\n"], ["/hacktical-c/error/error.c", "#include \n#include \n#include \n#include \n\n#include \"error.h\"\n#include \"malloc1/malloc1.h\"\n#include \"vector/vector.h\"\n\n__thread struct hc_error *hc_error = NULL;\n\nstatic struct hc_vector *handlers() {\n static bool init = true;\n static __thread struct hc_vector handlers;\n\n if (init) {\n hc_vector_init(&handlers, &hc_malloc_default, sizeof(jmp_buf));\n init = false;\n }\n \n return &handlers;\n}\n\nvoid hc_catch_push(jmp_buf h) {\n memcpy((jmp_buf *)hc_vector_push(handlers()), h, sizeof(jmp_buf));\n}\n\nvoid hc_catch_pop() {\n hc_vector_pop(handlers());\n}\n\nvoid hc_errors_deinit() {\n hc_vector_deinit(handlers());\n}\n\nvoid _hc_throw(struct hc_error *e) {\n struct hc_vector *hs = handlers();\n\n if (!hs->length) {\n fputs(e->message, stderr);\n hc_error_free(e);\n abort();\n }\n \n jmp_buf t;\n memcpy(t, *(jmp_buf *)hc_vector_pop(hs), sizeof(jmp_buf));\n hc_error = e;\n longjmp(t, 1);\n}\n\nstruct hc_error *hc_error_new(const char *message, ...) {\n va_list args;\n va_start(args, message);\n \n va_list tmp_args;\n va_copy(tmp_args, args);\n int len = vsnprintf(NULL, 0, message, tmp_args);\n va_end(tmp_args);\n\n if (len < 0) {\n vfprintf(stderr, message, args);\n abort();\n }\n \n len++;\n struct hc_error *e = malloc(sizeof(struct hc_error));\n e->message = malloc(len);\n vsnprintf(e->message, len, message, args);\n va_end(args);\n return e;\n}\n\nvoid hc_error_free(struct hc_error *e) {\n free(e->message);\n free(e);\n}\n\nbool hc_streq(const char *l, const char *r) {\n for (; *l && *l == *r; l++, r++);\n return *l == *r;\n}\n"], ["/hacktical-c/fix/tests.c", "#include \n#include \n#include \n#include \"fix.h\"\n\nstatic void test_add() {\n assert(hc_fix_add(hc_fix(2, 175), hc_fix(2, 25)) ==\n\t hc_fix(2, 200));\n \n assert(hc_fix_add(hc_fix(2, 175), hc_fix(2, -25)) ==\n\t hc_fix(2, 150));\n}\n\nstatic void test_div() {\n assert(hc_fix_div(hc_fix(2, 150), hc_fix(0, 2)) ==\n\t hc_fix(2, 75));\n\n assert(hc_fix_div(hc_fix(2, 150), hc_fix(0, -2)) ==\n\t hc_fix(2, -75));\n\n assert(hc_fix_div(hc_fix(2, -150), hc_fix(0, -2)) ==\n\t hc_fix(2, 75));\n}\n\nstatic void test_mul() {\n assert(hc_fix_mul(hc_fix(2, 150), hc_fix(1, 5)) ==\n\t hc_fix(2, 75));\n\n assert(hc_fix_mul(hc_fix(2, 150), hc_fix(1, -5)) ==\n\t hc_fix(2, -75));\n\n assert(hc_fix_mul(hc_fix(2, -150), hc_fix(1, -5)) ==\n\t hc_fix(2, 75));\n}\n\nstatic void test_new() {\n hc_fix_t x = hc_fix(2, -125);\n assert(hc_fix_exp(x) == 2);\n assert(hc_fix_val(x) == -125);\n assert(hc_fix_int(x) == -1);\n assert(hc_fix_frac(x) == -25);\n assert(hc_fix_double(x) == -1.25);\n}\n\nstatic void test_sub() {\n assert(hc_fix_sub(hc_fix(2, 175), hc_fix(2, 25)) ==\n\t hc_fix(2, 150));\n\n assert(hc_fix_sub(hc_fix(2, 175), hc_fix(2, -25)) ==\n\t hc_fix(2, 200));\n}\n\nvoid fix_tests() {\n test_add();\n test_div();\n test_mul();\n test_new();\n test_sub();\n}\n"], ["/hacktical-c/malloc2/benchmarks.c", "#include \n\n#include \"chrono/chrono.h\"\n#include \"malloc2.h\"\n\n#define N 1000\n#define MAX_SIZE 64\n#define GET_SIZE() ((rand() % MAX_SIZE) + 1)\n\nstatic void run_malloc() {\n int *ps[N];\n hc_time_t t = hc_now();\n \n for (int i = 0; i < N; i++) {\n ps[i] = malloc(GET_SIZE());\n }\n\n for (int i = 0; i < N; i++) {\n free(ps[i]);\n }\n\n hc_time_print(&t, \"malloc: \"); \n}\n\nstatic void run_bump() {\n struct hc_bump_alloc a;\n hc_bump_alloc_init(&a, &hc_malloc_default, N * MAX_SIZE);\n hc_time_t t = hc_now();\n\n for (int i = 0; i < N; i++) {\n hc_acquire(&a.malloc, GET_SIZE());\n }\n\n hc_bump_alloc_deinit(&a);\n hc_time_print(&t, \"bump: \");\n}\n\nstatic void run_slab() {\n struct hc_slab_alloc a;\n hc_slab_alloc_init(&a, &hc_malloc_default, N);\n hc_time_t t = hc_now();\n\n for (int i = 0; i < N; i++) {\n hc_acquire(&a.malloc, GET_SIZE());\n }\n\n hc_slab_alloc_deinit(&a);\n hc_time_print(&t, \"slab: \");\n}\n\nvoid malloc2_benchmarks() {\n const int s = time(NULL);\n \n srand(s);\n run_malloc();\n\n srand(s);\n run_bump();\n\n srand(s);\n run_slab();\n}\n"], ["/hacktical-c/slog/tests.c", "#include \n#include \n\n#include \"slog.h\"\n#include \"malloc1/malloc1.h\"\n\nvoid slog_tests() {\n struct hc_memory_stream out;\n hc_memory_stream_init(&out, &hc_malloc_default);\n \n struct hc_slog_stream s;\n hc_slog_stream_init(&s, &out.stream, .close_out=true);\n \n hc_slog_do(&s) {\n hc_slog_context_do(hc_slog_string(\"string\", \"abc\")) {\n hc_time_t t = hc_time(2025, 4, 13, 1, 40, 0);\n \n hc_slog_write(hc_slog_bool(\"bool\", true),\n\t\t hc_slog_int(\"int\", 42),\n\t\t hc_slog_time(\"time\", t));\n }\n }\n\n assert(strcmp(\"string=\\\"abc\\\", \"\n\t\t\"bool=true, \"\n\t\t\"int=42, \"\n\t\t\"time=2025-04-13T01:40:00\\n\",\n\t\thc_memory_stream_string(&out)) == 0);\n \n hc_slog_deinit(&s); \n}\n"], ["/hacktical-c/slog/slog.c", "#include \n#include \n#include \n#include \n\n#include \"error/error.h\"\n#include \"macro/macro.h\"\n#include \"malloc1/malloc1.h\"\n#include \"slog.h\"\n\n__thread struct hc_slog *_hc_slog = NULL;\n\nstruct hc_slog *hc_slog() {\n if (_hc_slog != NULL) {\n return _hc_slog;\n }\n \n static __thread bool init = true;\n static __thread struct hc_slog_stream s;\n\n if (init) {\n hc_slog_stream_init(&s, hc_stdout());\n init = false;\n }\n\n return &s.slog;\n}\n\nstatic void field_deinit(struct hc_slog_field *f) {\n free(f->name);\n hc_value_deinit(&f->value);\n}\n\nstatic void slog_write(struct hc_slog *s,\n\t\t const size_t n,\n\t\t struct hc_slog_field *fields[]) {\n assert(s->write);\n s->write(s, n, fields);\n}\n\nvoid __hc_slog_write(struct hc_slog *s,\n\t\t const size_t n,\n\t\t struct hc_slog_field *fields[]) {\n slog_write(s, n, fields);\n \n for(size_t i = 0; i < n; i++) {\n struct hc_slog_field *f = fields[i];\n field_deinit(f);\n free(f);\n }\n}\n\nvoid _hc_slog_deinit(struct hc_slog *s) {\n if (s->deinit) {\n s->deinit(s);\n }\n}\n\nstatic struct hc_value *field_init(struct hc_slog_field *f,\n\t\t\t\t const char *name,\n\t\t\t\t const struct hc_type *type) {\n f->name = strdup(name);\n hc_value_init(&f->value, type);\n return &f->value;\n}\n\nstruct hc_slog_field *hc_slog_bool(const char *name, const bool value) {\n struct hc_slog_field *f = malloc(sizeof(struct hc_slog_field));\n field_init(f, name, &HC_BOOL)->as_bool = value;\n return f;\n}\n\nstruct hc_slog_field *hc_slog_int(const char *name, const int value) {\n struct hc_slog_field *f = malloc(sizeof(struct hc_slog_field));\n field_init(f, name, &HC_INT)->as_int = value;\n return f;\n}\n\nstruct hc_slog_field *hc_slog_string(const char *name, const char *value) {\n struct hc_slog_field *f = malloc(sizeof(struct hc_slog_field));\n field_init(f, name, &HC_STRING)->as_string = strdup(value);\n return f;\n}\n\nstruct hc_slog_field *hc_slog_time(const char *name, const hc_time_t value) {\n struct hc_slog_field *f = malloc(sizeof(struct hc_slog_field));\n field_init(f, name, &HC_TIME)->as_time = value;\n return f;\n}\n\nvoid stream_deinit(struct hc_slog *s) {\n struct hc_slog_stream *ss = hc_baseof(s, struct hc_slog_stream, slog);\n if (ss->opts.close_out) { hc_stream_deinit(ss->out); }\n}\n\nstatic void field_write(struct hc_slog_field *f, struct hc_stream *out) {\n hc_puts(out, f->name);\n hc_putc(out, '=');\n hc_value_write(&f->value, out);\n}\n\nstatic void stream_write(struct hc_slog *s,\n\t\t\t const size_t n,\n\t\t\t struct hc_slog_field *fields[]) {\n struct hc_slog_stream *ss = hc_baseof(s, struct hc_slog_stream, slog);\n\n for(size_t i = 0; i < n; i++) {\n struct hc_slog_field *f = fields[i];\n if (i) { hc_puts(ss->out, \", \"); }\n field_write(f, ss->out);\n }\n\n hc_putc(ss->out, '\\n');\n}\n\nstruct hc_slog_stream *_hc_slog_stream_init(struct hc_slog_stream *s,\n\t\t\t\t\t struct hc_stream *out,\n\t\t\t\t\t const struct hc_slog_stream_opts opts) {\n s->slog.deinit = stream_deinit;\n s->slog.write = stream_write;\n s->out = out;\n s->opts = opts;\n return s;\n}\n\nstatic void context_deinit(struct hc_slog *s) {\n struct hc_slog_context *sc = hc_baseof(s, struct hc_slog_context, slog);\n\n for (size_t i = 0; i < sc->length; i++) {\n struct hc_slog_field *f = sc->fields[i];\n field_deinit(f);\n free(f);\n }\n\n free(sc->fields);\n}\n\nstatic void context_write(struct hc_slog *s,\n\t\t\t const size_t n,\n\t\t\t struct hc_slog_field *fields[]) {\n struct hc_slog_context *c = hc_baseof(s, struct hc_slog_context, slog);\n struct hc_slog_field *fs[c->length + n];\n memcpy(fs, c->fields, sizeof(struct hc_slog_field *) * c->length);\n memcpy(fs + c->length, fields, sizeof(struct hc_slog_field *) * n);\n slog_write(c->parent, c->length + n, fs);\n}\n\nstruct hc_slog_context *hc_slog_context_init(struct hc_slog_context *c,\n\t\t\t\t\t size_t length,\n\t\t\t\t\t struct hc_slog_field *fields[]) {\n c->slog.deinit = context_deinit;\n c->slog.write = context_write;\n c->parent = hc_slog();\n c->length = length;\n size_t s = sizeof(struct hc_slog_field *) * length;\n c->fields = malloc(s);\n memcpy(c->fields, fields, s);\n return c;\n}\n"], ["/hacktical-c/dynamic/tests.c", "#include \n#include \"dynamic.h\"\n\nvoid dynamic_tests() {\n const char *out = \"/var/tmp/libtest.so\";\n \n hc_compile(\"#include \\n\"\n\t \"int test() { return 42; }\",\n\t out,\n\t .cflags = (const char *[]){\"-Wall\",\n\t\t\t\t\t\"-fsanitize=undefined\",\n\t\t\t\t\tNULL});\n\n struct hc_dlib lib;\n hc_dlib_init(&lib, out);\n hc_defer(hc_dlib_deinit(&lib));\n int (*fn)() = hc_dlib_find(&lib, \"test\");\n assert(fn() == 42);\n}\n"], ["/hacktical-c/stream1/stream1.c", "#include \n#include \n#include \n#include \n\n#include \"dynamic/dynamic.h\"\n#include \"error/error.h\"\n#include \"malloc1/malloc1.h\"\n#include \"macro/macro.h\"\n#include \"stream1.h\"\n\nsize_t hc_read(struct hc_stream *s, uint8_t *data, const size_t n) {\n assert(s->read);\n return s->read(s, data, n);\n}\n\nsize_t hc_write(struct hc_stream *s, const uint8_t *data, const size_t n) {\n assert(s->write);\n return s->write(s, data, n);\n}\n\nchar hc_getc(struct hc_stream *s) {\n char c = 0;\n return hc_read(s, (uint8_t *)&c, 1) ? c : 0;\n}\n\nchar *hc_gets(struct hc_stream *s, struct hc_malloc *malloc) {\n struct hc_vector out;\n hc_vector_init(&out, malloc, 1);\n\n for (;;) {\n char c = hc_getc(s);\n\n if (c == EOF) {\n break;\n }\n\n *(char *)hc_vector_push(&out) = c;\n\n if (c == '\\n') {\n break;\n }\n }\n\n \n *(char *)hc_vector_push(&out) = 0;\n return (char *)out.start;\n}\n\nsize_t hc_putc(struct hc_stream *s, const char data) {\n const uint8_t d[2] = {data, 0};\n return hc_write(s, d, 1);\n}\n\nsize_t hc_puts(struct hc_stream *s, const char *data) {\n return hc_write(s, (const uint8_t *)data, strlen(data));\n}\n\nsize_t hc_vprintf(struct hc_stream *s,\n\t\t const char *spec,\n\t\t va_list args) {\n char *data = hc_vsprintf(spec, args);\n hc_defer(free(data));\n return hc_write(s, (uint8_t *)data, strlen(data));\n}\n\nsize_t hc_printf(struct hc_stream *s, const char *spec, ...) {\n va_list args;\n va_start(args, spec);\n hc_defer(va_end(args));\n return hc_vprintf(s, spec, args);\n}\n\nvoid hc_stream_deinit(struct hc_stream *s) {\n assert(s->deinit);\n s->deinit(s);\n}\n\nsize_t file_read(struct hc_stream *s, uint8_t *data, const size_t n) {\n struct hc_file_stream *fs = hc_baseof(s, struct hc_file_stream, stream);\n assert(fs->file);\n return fread(data, n, 1, fs->file);\n}\n\nsize_t file_write(struct hc_stream *s, const uint8_t *data, const size_t n) {\n struct hc_file_stream *fs = hc_baseof(s, struct hc_file_stream, stream);\n assert(fs->file);\n return fwrite(data, n, 1, fs->file);\n}\n\nvoid file_deinit(struct hc_stream *s) {\n struct hc_file_stream *fs = hc_baseof(s, struct hc_file_stream, stream);\n\n if (fs->opts.close_file) {\n assert(fs->file);\n \n if (fclose(fs->file) == EOF) {\n hc_throw(\"Failed closing file\");\n }\n \n fs->file = NULL;\n }\n}\n\nstruct hc_file_stream *_hc_file_stream_init(struct hc_file_stream *s,\n\t\t\t\t\t FILE *file,\n\t\t\t\t\t const struct hc_file_stream_opts opts) {\n s->stream = (struct hc_stream){\n .read = file_read,\n .write = file_write,\n .deinit = file_deinit,\n };\n \n s->file = file;\n s->opts = opts;\n return s;\n};\n\nstruct hc_stream *hc_stdout() {\n static __thread bool init = true;\n static __thread struct hc_file_stream s;\n\n if (init) {\n hc_file_stream_init(&s, stdout);\n init = false;\n }\n\n return &s.stream;\n}\n\nsize_t memory_read(struct hc_stream *s, uint8_t *data, size_t n) {\n struct hc_memory_stream *ms = hc_baseof(s, struct hc_memory_stream, stream);\n\n if (ms->rpos + n > ms->data.length) {\n n = ms->data.length - ms->rpos;\n }\n \n memcpy(data, ms->data.start + ms->rpos, n);\n ms->rpos += n;\n return n;\n}\n\nsize_t memory_write(struct hc_stream *s,\n\t\t const uint8_t *data,\n\t\t const size_t n) {\n struct hc_memory_stream *ms = hc_baseof(s, struct hc_memory_stream, stream);\n uint8_t *const dst = hc_vector_insert(&ms->data, ms->data.length, n);\n memcpy(dst, data, n);\n return n;\n}\n\nvoid memory_deinit(struct hc_stream *s) {\n struct hc_memory_stream *ms = hc_baseof(s, struct hc_memory_stream, stream);\n hc_vector_deinit(&ms->data);\n}\n\nstruct hc_memory_stream *hc_memory_stream_init(struct hc_memory_stream *s,\n\t\t\t\t\t struct hc_malloc *malloc) {\n s->stream = (struct hc_stream){\n .read = memory_read,\n .write = memory_write,\n .deinit = memory_deinit,\n };\n \n hc_vector_init(&s->data, malloc, 1);\n s->rpos = 0;\n return s;\n}\n\nconst char *hc_memory_stream_string(struct hc_memory_stream *s) {\n if (!s->data.length || (*(s->data.end-1))) {\n *(uint8_t *)hc_vector_push(&s->data) = 0;\n }\n\n return (const char *)s->data.start;\n}\n"], ["/hacktical-c/error/tests.c", "#include \n#include \n#include \n#include \n\n#include \"error.h\"\n\nvoid error_tests() {\n void on_catch(struct hc_error *e) {\n assert(hc_streq(\"E123\", e->message) == 0);\n }\n \n hc_catch(on_catch) {\n hc_throw(\"E123 Going %s\", \"Down!\");\n }\n}\n"], ["/hacktical-c/stream1/tests.c", "#include \n#include \n#include \"stream1.h\"\n\nvoid stream1_tests() {\n struct hc_memory_stream s;\n hc_memory_stream_init(&s, &hc_malloc_default);\n hc_defer(hc_stream_deinit(&s.stream));\n hc_printf(&s.stream, \"%s%d\", \"foo\", 42);\n assert(strcmp(\"foo42\", hc_memory_stream_string(&s)) == 0);\n}\n"], ["/hacktical-c/list/tests.c", "#include \n#include \"list.h\"\n\nstruct list_item {\n struct hc_list ls;\n int value;\n};\n \nvoid list_tests() {\n struct hc_list head;\n hc_list_init(&head);\n\n const int n = 10;\n struct list_item items[n];\n \n for (int i = 0; i < n; i++) {\n items[i].value = i;\n hc_list_push_back(&head, &items[i].ls);\n }\n\n int i = 0;\n \n hc_list_do(&head, il) {\n assert(hc_baseof(il, struct list_item, ls)->value == i++);\n }\n}\n"], ["/hacktical-c/macro/tests.c", "#include \n#include \"macro.h\"\n\nvoid macro_tests() {\n int hc_id(foo, bar) = 42;\n assert(foobar == 42);\n\n assert(hc_min(7, 42) == 7);\n assert(hc_max(7.0, 42.0) == 42.0);\n\n {\n int foo = 0;\n \n {\n hc_defer(assert(foo++ == 1));\n hc_defer(assert(foo++ == 0));\n }\n\n assert(foo == 2);\n }\n}\n"], ["/hacktical-c/reflect/tests.c", "#include \n#include \"reflect.h\"\n\nvoid reflect_tests() {\n struct hc_value v;\n hc_value_init(&v, &HC_STRING)->as_string = strdup(\"foo\");\n hc_defer(hc_value_deinit(&v));\n struct hc_value c;\n hc_value_copy(&c, &v);\n hc_defer(hc_value_deinit(&c));\n assert(strcmp(c.as_string, v.as_string) == 0);\n}\n"], ["/hacktical-c/dsl/dsl.c", "#include \n#include \n#include \n#include \n\n#include \"dsl.h\"\n#include \"error/error.h\"\n#include \"list/list.h\"\n\nenum hc_order hc_strcmp(const char *x, const char *y) {\n const int result = strcmp(x, y);\n if (!result) { return HC_EQ; }\n return (result < 0) ? HC_LT : HC_GT;\n}\n\nchar *hc_upcase(char *s) {\n while (*s) {\n *s = toupper(*s);\n s++;\n }\n\n return s;\n}\n\nstatic void lib_print(struct hc_vm *vm, struct hc_sloc sloc) {\n struct hc_value *v = hc_vm_pop(vm);\n struct hc_dsl *dsl = hc_baseof(vm, struct hc_dsl, vm);\n hc_value_print(v, dsl->out);\n hc_value_deinit(v);\n}\n\nstatic void lib_upcase(struct hc_vm *vm, struct hc_sloc sloc) {\n struct hc_value *v = hc_vm_peek(vm);\n\n if (v->type != &HC_STRING) {\n hc_throw(\"Error in %s: Expected string (%s)\",\n\t hc_sloc_string(&sloc), v->type->name);\n }\n\n hc_upcase(v->as_string);\n}\n\nstruct env_item {\n char *key;\n struct hc_value value;\n};\n\nstatic enum hc_order env_cmp(const void *x, const void *y) {\n return hc_strcmp(*(const char **)x, *(const char **)y);\n}\n\nstatic const void *env_key(const void *x) {\n return &((const struct env_item *)x)->key;\n}\n\nvoid hc_dsl_init(struct hc_dsl *dsl, struct hc_malloc *malloc) {\n hc_set_init(&dsl->env, malloc, sizeof(struct env_item), env_cmp);\n dsl->env.key = env_key;\n dsl->out = hc_stdout();\n \n hc_vm_init(&dsl->vm, &hc_malloc_default);\n hc_dsl_set_fun(dsl, \"print\", lib_print);\n hc_dsl_set_fun(dsl, \"upcase\", lib_upcase);\n}\n\nstatic void deinit_env(struct hc_set *env) {\n hc_vector_do(&env->items, _it) {\n struct env_item *it = _it;\n free(it->key);\n hc_value_deinit(&it->value);\n }\n \n hc_set_deinit(env);\n}\n\nvoid hc_dsl_deinit(struct hc_dsl *dsl) {\n deinit_env(&dsl->env);\n hc_vm_deinit(&dsl->vm);\n}\n\nstruct hc_value *hc_dsl_getenv(struct hc_dsl *dsl, const char *key) {\n struct env_item *it = hc_set_find(&dsl->env, &key); \n return it ? &it->value : NULL;\n}\n\nstruct hc_value *hc_dsl_setenv(struct hc_dsl *dsl,\n\t\t\t const char *key,\n\t\t\t const struct hc_type *type) {\n struct env_item *it = hc_set_add(&dsl->env, &key, false);\n it->key = strdup(key);\n hc_value_init(&it->value, type);\n return &it->value;\n}\n\nvoid hc_dsl_set_fun(struct hc_dsl *dsl, const char *key, hc_vm_fun_t val) {\n hc_dsl_setenv(dsl, key, &HC_VM_FUN)->as_other = val;\n}\n\nvoid hc_dsl_set_string(struct hc_dsl *dsl, const char *key, const char *val) {\n hc_dsl_setenv(dsl, key, &HC_STRING)->as_string = strdup(val);\n}\n\nvoid hc_dsl_eval(struct hc_dsl *dsl, const char *in) {\n struct hc_list forms;\n hc_list_init(&forms);\n hc_defer(hc_forms_free(&forms));\n struct hc_sloc sloc = hc_sloc(\"eval\", 0, 0);\n while (hc_read_next(&in, &forms, &sloc));\n const size_t pc = dsl->vm.code.length;\n hc_forms_emit(&forms, dsl);\n hc_vm_eval(&dsl->vm, pc, -1);\n}\n\nvoid hc_form_init(struct hc_form *f,\n\t\t const struct hc_form_type *t,\n\t\t const struct hc_sloc sloc,\n\t\t struct hc_list *owner) {\n f->type = t;\n f->sloc = sloc;\n\n if (owner) {\n hc_list_push_back(owner, &f->owner);\n } else {\n hc_list_init(&f->owner);\n }\n}\n\nvoid hc_form_emit(struct hc_form *f, struct hc_dsl *dsl) {\n assert(f->type->emit);\n f->type->emit(f, dsl);\n}\n\nvoid hc_form_print(struct hc_form *f, struct hc_stream *out) {\n assert(f->type->print);\n f->type->print(f, out);\n}\n\nstruct hc_value *hc_form_value(const struct hc_form *f, struct hc_dsl *dsl) {\n return f->type->value ? f->type->value(f, dsl) : NULL;\n}\n\nvoid hc_form_free(struct hc_form *f) {\n hc_list_delete(&f->owner);\n assert(f->type->free);\n f->type->free(f);\n}\n\nstatic void call_emit(struct hc_form *_f, struct hc_dsl *dsl) {\n struct hc_call *f = hc_baseof(_f, struct hc_call, form);\n struct hc_value *t = hc_form_value(f->target, dsl);\n\n if (!t) {\n hc_throw(\"Error in %s: Missing call target\",\n\t hc_sloc_string(&_f->sloc));\n }\n\n if (t->type != &HC_VM_FUN) {\n hc_throw(\"Error in %s: '%s' isn't callable\",\n\t hc_sloc_string(&_f->sloc),\n\t t->type->name);\n }\n\n hc_list_do(&f->args, a) {\n hc_form_emit(hc_baseof(a, struct hc_form, owner), dsl);\n }\n \n hc_vm_emit(&dsl->vm,\n\t &HC_CALL,\n\t &(struct hc_call_op){\n\t .target = t->as_other,\n\t .sloc = _f->sloc\n\t });\n}\n\nstatic void call_print(const struct hc_form *_f, struct hc_stream *out) {\n struct hc_call *f = hc_baseof(_f, struct hc_call, form);\n hc_putc(out, '(');\n hc_form_print(f->target, out);\n\n hc_list_do(&f->args, i) {\n hc_putc(out, ' ');\n hc_form_print(hc_baseof(i, struct hc_form, owner), out);\n }\n \n hc_putc(out, ')');\n}\n\nstatic void call_free(struct hc_form *_f) {\n struct hc_call *f = hc_baseof(_f, struct hc_call, form);\n hc_form_free(f->target); \n\n hc_list_do(&f->args, i) {\n hc_form_free(hc_baseof(i, struct hc_form, owner));\n }\n\n free(f);\n}\n\nconst struct hc_form_type HC_CALL_FORM = {\n .emit = call_emit,\n .print = call_print,\n .value = NULL,\n .free = call_free\n};\n\nvoid hc_call_init(struct hc_call *f,\n\t\t const struct hc_sloc sloc,\n\t\t struct hc_list *owner,\n\t\t struct hc_form *target) { \n hc_form_init(&f->form, &HC_CALL_FORM, sloc, owner);\n f->target = target;\n hc_list_init(&f->args);\n}\n\nstatic void id_emit(struct hc_form *_f, struct hc_dsl *dsl) {\n struct hc_id *f = hc_baseof(_f, struct hc_id, form);\n struct hc_value *v = hc_dsl_getenv(dsl, f->name);\n\n if (!v) {\n hc_throw(\"Error in %s: Unknown identifier '%s'\",\n\t hc_sloc_string(&_f->sloc), f->name);\n }\n\n struct hc_push_op op;\n hc_value_copy(&op.value, v);\n hc_vm_emit(&dsl->vm, &HC_PUSH, &op);\n}\n\nstatic void id_print(const struct hc_form *_f, struct hc_stream *out) {\n struct hc_id *f = hc_baseof(_f, struct hc_id, form);\n hc_puts(out, f->name);\n}\n\nstatic struct hc_value *id_value(const struct hc_form *_f,\n\t\t\t\t struct hc_dsl *dsl) {\n struct hc_id *f = hc_baseof(_f, struct hc_id, form);\n return hc_dsl_getenv(dsl, f->name);\n}\n\nstatic void id_free(struct hc_form *_f) {\n struct hc_id *f = hc_baseof(_f, struct hc_id, form);\n free(f->name);\n free(f);\n}\n\nconst struct hc_form_type HC_ID_FORM = {\n .emit = id_emit,\n .print = id_print,\n .value = id_value,\n .free = id_free\n};\n\nvoid hc_id_init(struct hc_id *f,\n\t\tconst struct hc_sloc sloc,\n\t\tstruct hc_list *owner,\n\t\tconst char *name) {\n hc_form_init(&f->form, &HC_ID_FORM, sloc, owner);\n f->name = strdup(name);\n}\n\nstatic void literal_emit(struct hc_form *_f, struct hc_dsl *dsl) {\n struct hc_literal *f = hc_baseof(_f, struct hc_literal, form); \n struct hc_push_op op;\n hc_value_copy(&op.value, &f->value);\n hc_vm_emit(&dsl->vm, &HC_PUSH, &op);\n}\n\nstatic void literal_print(const struct hc_form *_f, struct hc_stream *out) {\n struct hc_literal *f = hc_baseof(_f, struct hc_literal, form);\n hc_value_write(&f->value, out);\n}\n\nstatic struct hc_value *literal_value(const struct hc_form *_f,\n\t\t\t\t struct hc_dsl *dsl) {\n struct hc_literal *f = hc_baseof(_f, struct hc_literal, form);\n return &f->value;\n}\n\nstatic void literal_free(struct hc_form *_f) {\n struct hc_literal *f = hc_baseof(_f, struct hc_literal, form);\n hc_value_deinit(&f->value);\n free(f);\n}\n\nconst struct hc_form_type HC_LITERAL = {\n .emit = literal_emit,\n .print = literal_print,\n .value = literal_value,\n .free = literal_free,\n};\n\nvoid hc_literal_init(struct hc_literal *f,\n\t\t const struct hc_sloc sloc,\n\t\t struct hc_list *owner) { \n hc_form_init(&f->form, &HC_LITERAL, sloc, owner);\n}\n\nvoid hc_skip_ws(const char **in, struct hc_sloc *sloc) {\n for (;; (*in)++) {\n switch (**in) {\n case ' ':\n case '\\t':\n sloc->col++;\n break;\n case '\\n':\n sloc->row++;\n sloc->col = 0;\n break;\n default:\n return;\n }\n }\n}\n\nvoid hc_read_call(const char **in,\n\t\t struct hc_list *out,\n\t\t struct hc_sloc *sloc) {\n struct hc_sloc floc = *sloc;\n\n if (**in != '(') {\n hc_throw(\"Error in %s: Invalid call syntax\",\n\t hc_sloc_string(sloc));\n }\n\n (*in)++;\n sloc->col++;\n hc_skip_ws(in, sloc);\n \n if (!hc_read_expr(in, out, sloc)) {\n hc_throw(\"Error in %s: Missing call target\",\n\t hc_sloc_string(sloc));\n }\n\n struct hc_form *t = hc_baseof(hc_list_pop_back(out),\n\t\t\t\tstruct hc_form,\n\t\t\t\towner);\n\n struct hc_call *f = malloc(sizeof(struct hc_call));\n hc_list_init(&t->owner);\n hc_call_init(f, floc, out, t);\n \n for (bool done = false; !done;) {\n hc_skip_ws(in, sloc);\n \n switch (**in) {\n case 0:\n hc_form_free(&f->form);\n\n hc_throw(\"Error in %s: Open call form\",\n\t hc_sloc_string(sloc));\n case ')':\n (*in)++;\n sloc->col++;\n done = true;\n continue;\n default:\n break;\n }\n\n if (!hc_read_expr(in, &f->args, sloc)) {\n hc_form_free(&f->form);\n \n hc_throw(\"Error in %s: Invalid call syntax\",\n\t hc_sloc_string(sloc));\n }\n }\n}\n\nbool hc_read_expr(const char **in,\n\t\t struct hc_list *out,\n\t\t struct hc_sloc *sloc) {\n const char c = **in;\n \n switch (c) {\n case '(':\n hc_read_call(in, out, sloc);\n return true;\n default:\n if (isalpha(c)) {\n hc_read_id(in, out, sloc);\n return true;\n }\n\n break;\n }\n\n return false;\n}\n\nvoid hc_read_id(const char **in,\n\t\tstruct hc_list *out,\n\t\tstruct hc_sloc *sloc) {\n struct hc_sloc floc = *sloc;\n struct hc_memory_stream buf;\n hc_memory_stream_init(&buf, &hc_malloc_default);\n hc_defer(hc_stream_deinit(&buf.stream));\n char c = 0;\n\n while ((c = **in)) {\n if (isspace(c) || c == '(' || c == ')') {\n break;\n }\n \n hc_putc(&buf.stream, c);\n sloc->col++;\n (*in)++;\n }\n\n struct hc_id *f = malloc(sizeof(struct hc_id));\n hc_id_init(f, floc, out, hc_memory_stream_string(&buf));\n}\n\nbool hc_read_next(const char **in,\n\t\t struct hc_list *out,\n\t\t struct hc_sloc *sloc) {\n if (**in == '$') {\n (*in)++;\n hc_read_call(in, out, sloc);\n return true;\n }\n \n return hc_read_text(in, out, sloc);\n}\n\nbool hc_read_text(const char **in,\n\t\t struct hc_list *out,\n\t\t struct hc_sloc *sloc) {\n struct hc_sloc floc = *sloc;\n const char *start = *in;\n \n while (**in && **in != '$') {\n if (**in == '\\n') {\n sloc->row++;\n } else {\n sloc->col++;\n }\n\n (*in)++;\n }\n\n size_t n = *in - start;\n \n if (n) {\n struct hc_value v;\n hc_value_init(&v, &HC_STRING)->as_string = strndup(start, n); \n struct hc_literal *vf = malloc(sizeof(struct hc_literal));\n hc_literal_init(vf, floc, out);\n vf->value = v;\n struct hc_id *t = malloc(sizeof(struct hc_literal));\n hc_id_init(t, floc, NULL, \"print\");\n struct hc_call *c = malloc(sizeof(struct hc_call));\n hc_call_init(c, floc, out, &t->form);\n return true;\n }\n\n return false;\n}\n\nvoid hc_forms_free(struct hc_list *in) {\n hc_list_do(in, i) {\n hc_form_free(hc_baseof(i, struct hc_form, owner));\n } \n}\n\nvoid hc_forms_emit(struct hc_list *in, struct hc_dsl *dsl) {\n hc_list_do(in, i) {\n hc_form_emit(hc_baseof(i, struct hc_form, owner), dsl);\n } \n}\n"], ["/hacktical-c/task/tests.c", "#include \n#include \n#include \"task.h\"\n\nstruct my_task {\n struct hc_task task;\n int *value;\n};\n\nstatic void producer(struct hc_task *task) {\n int *value = hc_baseof(task, struct my_task, task)->value;\n \n switch (task->state) {\n case 0:\n assert(*value == 0);\n (*value)++;\n hc_task_yield(task);\n assert(*value == 2);\n (*value)++;\n }\n \n task->done = true;\n}\n\nstatic void consumer(struct hc_task *task) {\n int *value = hc_baseof(task, struct my_task, task)->value;\n\n switch (task->state) {\n case 0:\n assert(*value == 1);\n (*value)++;\n hc_task_yield(task);\n assert(*value == 3);\n (*value)++;\n }\n \n task->done = true;\n}\n \nvoid task_tests() {\n struct hc_task_list tl;\n hc_task_list_init(&tl);\n \n int value = 0; \n\n struct my_task pt = {.value = &value};\n hc_task_init(&pt.task, &tl, &producer);\n\n struct my_task ct = {.value = &value};\n hc_task_init(&ct.task, &tl, &consumer);\n\n hc_task_list_run(&tl);\n assert(value == 4);\n}\n"], ["/hacktical-c/set/tests.c", "#include \n#include \n#include \"set.h\"\n\nstruct map_item {\n int k, v;\n};\n\nstatic enum hc_order cmp(const void *x, const void *y) {\n return hc_cmp(*(const int *)x, *(const int *)y);\n}\n\nstatic const void *key(const void *x) {\n return &((const struct map_item *)x)->k;\n}\n\nvoid set_tests() {\n int n = 10;\n struct hc_set s;\n hc_set_init(&s, &hc_malloc_default, sizeof(struct map_item), cmp);\n s.key = key;\n \n for (int i = 0; i < n; i++) {\n struct map_item *it = hc_set_add(&s, &i, false);\n *it = (struct map_item){.k = i, .v = i};\n }\n\n assert(hc_set_length(&s) == n);\n \n for (int i = 0; i < n; i++) {\n struct map_item *it = hc_set_find(&s, &i);\n assert(it);\n assert(it->k == i);\n assert(it->v == i);\n }\n\n hc_set_clear(&s);\n assert(hc_set_length(&s) == 0); \n hc_set_deinit(&s);\n}\n"], ["/hacktical-c/chrono/chrono.c", "#include \n#include \n#include \n#include \n\n#include \"chrono.h\"\n#include \"error/error.h\"\n#include \"stream1/stream1.h\"\n\nhc_time_t hc_now() {\n hc_time_t t;\n \n if (!timespec_get(&t.value, TIME_UTC)) {\n hc_throw(\"Failed getting time: %d\", errno);\n }\n\n return t;\n}\n\nhc_time_t hc_time(int year,\n\t\t int month,\n\t\t int day,\n\t\t int hour,\n\t\t int minute,\n\t\t int second) {\n struct tm t = {0};\n t.tm_year = year - 1900;\n t.tm_mon = month - 1;\n t.tm_mday = day;\n t.tm_hour = hour;\n t.tm_min = minute;\n t.tm_sec = second;\n\n hc_time_t result = {0};\n result.value.tv_sec = timegm(&t);\n return result;\n}\n\nuint64_t hc_time_ns(const hc_time_t *t) {\n const struct timespec now = hc_now().value;\n \n return\n (now.tv_sec - t->value.tv_sec) * 1000000000 +\n (now.tv_nsec - t->value.tv_nsec);\n}\n\nvoid hc_time_print(const hc_time_t *t, const char *m) {\n printf(\"%s%\" PRIu64 \"ns\\n\", m, hc_time_ns(t));\n}\n\nchar *hc_time_sprintf(const hc_time_t *t, const char *spec) {\n struct tm tm;\n gmtime_r(&(t->value.tv_sec), &tm);\n size_t len = 8;\n char *result = malloc(len);\n\n for (;;) {\n const size_t n = strftime(result, len, spec, &tm);\n\n if (n) {\n result[n] = 0;\n break;\n }\n \n len *= 2;\n free(result);\n result = malloc(len);\n }\n \n return result;\n}\n\nvoid hc_time_printf(const hc_time_t *t,\n\t\t const char *spec,\n\t\t struct hc_stream *out) {\n char *s = hc_time_sprintf(t, \"%Y-%m-%dT%H:%M:%S\");\n hc_puts(out, s);\n free(s);\n}\n\nuint64_t hc_sleep(uint64_t ns) {\n struct timespec t = {0};\n t.tv_nsec = ns;\n\n switch (nanosleep(&t, &t)) {\n case 0:\n break;\n case EINTR:\n return t.tv_nsec;\n default:\n hc_throw(\"Failed sleeping: %d\", errno);\n }\n\n return 0;\n}\n"], ["/hacktical-c/malloc1/malloc1.c", "#include \n#include \n#include \"error/error.h\"\n#include \"macro/macro.h\"\n#include \"malloc1.h\"\n\nstatic void *default_acquire(struct hc_malloc *m, size_t size) {\n return malloc(size);\n}\n\nstatic void default_release(struct hc_malloc *m, void *p) {\n free(p);\n}\n\nstruct hc_malloc hc_malloc_default = {.acquire = default_acquire,\n\t\t\t\t .release = default_release};\n\n__thread struct hc_malloc *hc_mallocp = NULL;\n\n/* Bump */\n\nstatic void *bump_acquire(struct hc_malloc *a, size_t size) {\n if (size <= 0) {\n hc_throw(HC_INVALID_SIZE);\n } \n\n struct hc_bump_alloc *ba = hc_baseof(a, struct hc_bump_alloc, malloc);\n \n if (ba->size - ba->offset < size) {\n hc_throw(HC_NO_MEMORY);\n } \n\n uint8_t *p = ba->memory + ba->offset;\n uint8_t *pa = hc_align(p, size);\n ba->offset = ba->offset + pa - p + size;\n return pa;\n}\n\nstatic void bump_release(struct hc_malloc *a, void *p) {\n //Do nothing\n}\n\nvoid hc_bump_alloc_init(struct hc_bump_alloc *a,\n\t\t\tstruct hc_malloc *source,\n\t\t\tsize_t size) {\n a->malloc.acquire = bump_acquire;\n a->malloc.release = bump_release;\n a->source = source;\n a->size = size;\n a->offset = 0;\n a->memory = hc_acquire(source, size);\n}\n\nvoid hc_bump_alloc_deinit(struct hc_bump_alloc *a) {\n hc_release(a->source, a->memory);\n}\n"], ["/hacktical-c/vm/vm.c", "#include \n#include \n#include \n#include \n#include \n\n#include \"error/error.h\"\n#include \"macro/macro.h\"\n#include \"vm.h\"\n\nstruct hc_sloc hc_sloc(const char *source, const int row, const int col) {\n struct hc_sloc s = {.source = {0}, .row = row, .col = col};\n assert(strlen(source) < sizeof(s.source));\n strcpy(s.source, source);\n return s;\n}\n\nconst char *hc_sloc_string(struct hc_sloc *sloc) {\n snprintf(sloc->out, sizeof(sloc->out), \"'%s'; row %d, column %d\",\n\t sloc->source, sloc->row, sloc->col);\n return sloc->out;\n}\n\n\nvoid hc_vm_init(struct hc_vm *vm, struct hc_malloc *malloc) {\n hc_vector_init(&vm->stack, malloc, sizeof(struct hc_value));\n hc_vector_init(&vm->ops, malloc, sizeof(const struct hc_op *));\n hc_vector_init(&vm->code, malloc, sizeof(hc_op_eval_t));\n}\n\nstatic size_t op_items(const struct hc_op *op,\n\t\t uint8_t *p,\n\t\t struct hc_vm *vm) {\n const size_t s = op->size + hc_align(p, op->align) - p;\n return ceil(s / (double)vm->code.item_size);\n}\n\nstatic void deinit_stack(struct hc_vm *vm) {\n hc_vector_do(&vm->stack, v) {\n hc_value_deinit(v);\n }\n\n hc_vector_deinit(&vm->stack);\n}\n\nstatic void deinit_ops(struct hc_vm *vm) {\n uint8_t *p = vm->code.start;\n \n hc_vector_do(&vm->ops, _op) {\n const struct hc_op *op = *(const struct hc_op **)_op;\n p += sizeof(hc_op_eval_t);\n\n if (op->deinit) {\n op->deinit(hc_align(p, op->align));\n }\n\n p += op_items(op, p, vm) * vm->code.item_size;\n }\n\n hc_vector_deinit(&vm->ops);\n}\n\nvoid hc_vm_deinit(struct hc_vm *vm) { \n deinit_stack(vm);\n deinit_ops(vm);\n hc_vector_deinit(&vm->code);\n}\n\nstruct hc_value *hc_vm_push(struct hc_vm *vm) {\n return hc_vector_push(&vm->stack);\n}\n\nstruct hc_value *hc_vm_peek(struct hc_vm *vm) {\n return hc_vector_peek(&vm->stack);\n}\n\nstruct hc_value *hc_vm_pop(struct hc_vm *vm) {\n return hc_vector_pop(&vm->stack);\n}\n\nsize_t hc_vm_emit(struct hc_vm *vm,\n\t\t const struct hc_op *op,\n\t\t const void *data) {\n *(const struct hc_op **)hc_vector_push(&vm->ops) = op;\n const size_t pc = vm->code.length;\n *(hc_op_eval_t *)hc_vector_push(&vm->code) = op->eval;\n \n uint8_t *const p = hc_vector_insert(&vm->code,\n\t\t\t\t vm->code.length,\n\t\t\t\t op_items(op, vm->code.end, vm));\n \n memcpy(hc_align(p, op->align), data, op->size);\n return pc;\n}\n\nvoid hc_vm_eval(struct hc_vm *vm,\n\t\tconst size_t start_pc,\n\t\tconst size_t end_pc) {\n const uint8_t *const ep = (end_pc == -1)\n ? vm->code.end\n : hc_vector_get(&vm->code, end_pc);\n\n for (uint8_t *p = hc_vector_get(&vm->code, start_pc);\n p != ep;\n p = (*(hc_op_eval_t *)p)(vm, p + vm->code.item_size));\n}\n\nstatic void fun_print(const struct hc_value *v, struct hc_stream *out) {\n hc_printf(out, \"%p\", v->as_other);\n}\n\nconst struct hc_type HC_VM_FUN = {\n .name = \"VM/Fun\",\n .copy = NULL,\n .print = fun_print\n};\n\nstatic uint8_t *call_eval(struct hc_vm *vm, uint8_t *data) {\n struct hc_call_op *op = (void *)hc_align(data, alignof(struct hc_call_op));\n op->target(vm, op->sloc);\n return (uint8_t *)op + sizeof(struct hc_call_op);\n}\n\nconst struct hc_op HC_CALL = (struct hc_op){\n .name = \"call\",\n .align = alignof(struct hc_call_op),\n .size = sizeof(struct hc_call_op),\n .eval = call_eval,\n .deinit = NULL\n};\n\nstatic void push_deinit(uint8_t *data) {\n struct hc_push_op *op = (void *)data;\n hc_value_deinit(&op->value);\n}\n\nstatic uint8_t *push_eval(struct hc_vm *vm, uint8_t *data) {\n struct hc_push_op *op = (void *)hc_align(data, alignof(struct hc_push_op));\n hc_value_copy(hc_vm_push(vm), &op->value);\n return (uint8_t *)op + sizeof(struct hc_push_op);\n}\n\nconst struct hc_op HC_PUSH = (struct hc_op){\n .name = \"push\",\n .align = alignof(struct hc_push_op),\n .size = sizeof(struct hc_push_op),\n .eval = push_eval,\n .deinit = push_deinit\n};\n"], ["/hacktical-c/vector/tests.c", "#include \n#include \n#include \"vector.h\"\n\nvoid vector_tests() {\n struct hc_vector v;\n hc_vector_init(&v, &hc_malloc_default, sizeof(int));\n const int n = 100;\n \n for (int i = 0; i < n; i++) {\n *(int *)hc_vector_push(&v) = i;\n }\n\n {\n int i = 0;\n \n hc_vector_do(&v, it) {\n assert(*(int *)it == i++);\n }\n }\n \n assert(v.length == n);\n \n for (int i = 0; i < n; i++) {\n assert(*(int *)hc_vector_get(&v, i) == i);\n }\n\n assert(*(int *)hc_vector_pop(&v) == n-1);\n assert(*(int *)hc_vector_peek(&v) == n-2);\n assert(v.length == n-1);\n \n for (int i = 0; i < n-1; i++) {\n assert(*(int *)hc_vector_get(&v, i) == i);\n }\n\n assert(hc_vector_delete(&v, 0, 1));\n assert(v.length == n-2);\n\n for (int i = 1; i < n-1; i++) {\n assert(*(int *)hc_vector_get(&v, i-1) == i);\n }\n\n (*(int *)hc_vector_insert(&v, 0, 1) = 0);\n assert(v.length == n-1);\n\n for (int i = 0; i < n-1; i++) {\n assert(*(int *)hc_vector_get(&v, i) == i);\n }\n\n hc_vector_clear(&v);\n assert(v.length == 0);\n \n hc_vector_deinit(&v);\n}\n"], ["/hacktical-c/malloc2/malloc2.c", "#include \n#include \n#include \"error/error.h\"\n#include \"macro/macro.h\"\n#include \"malloc2.h\"\n\n/* Memo */\n\nstruct memo {\n size_t size;\n uint8_t data[];\n};\n\nstatic void *memo_acquire(struct hc_malloc *a, size_t size) {\n struct hc_memo_alloc *ma = hc_baseof(a, struct hc_memo_alloc, malloc);\n\n if (hc_set_length(&ma->memo)) {\n bool ok = false;\n size_t i = hc_set_index(&ma->memo, &size, &ok);\n\n if (ok) {\n struct hc_vector *is = &ma->memo.items;\n struct memo *m = *(struct memo **)hc_vector_get(is, i);\n hc_vector_delete(is, i, 1);\n return m->data;\n }\n }\n\n struct memo *m = hc_acquire(ma->source, sizeof(struct memo) + size);\n m->size = size;\n return m->data;\n}\n\nstatic void memo_release(struct hc_malloc *a, void *p) {\n struct hc_memo_alloc *ma = hc_baseof(a, struct hc_memo_alloc, malloc);\n struct memo *m = hc_baseof(p, struct memo, data);\n *(struct memo **)hc_set_add(&ma->memo, &m->size, true) = m;\n}\n\nstatic enum hc_order memo_cmp(const void *l, const void *r) {\n return hc_cmp(*(size_t *)l, *(size_t *)r);\n}\n\nstatic const void *memo_key(const void *p) {\n struct memo *m = *(struct memo **)p;\n return &m->size;\n}\n\nstruct hc_memo_alloc *hc_memo_alloc_init(struct hc_memo_alloc *a,\n\t\t\t\t\t struct hc_malloc *source) {\n a->malloc.acquire = memo_acquire;\n a->malloc.release = memo_release;\n a->source = source;\n hc_set_init(&a->memo, &hc_malloc_default, sizeof(struct memo *), memo_cmp);\n a->memo.key = memo_key;\n return a;\n}\n\nvoid hc_memo_alloc_deinit(struct hc_memo_alloc *a) {\n hc_vector_do(&a->memo.items, _m) {\n struct memo *m = *(struct memo **)_m;\n hc_release(a->source, m);\n }\n \n hc_set_deinit(&a->memo);\n}\n\n/* Slab */\n\nstruct slab {\n struct hc_list slabs;\n uint8_t *next;\n uint8_t memory[];\n};\n\nstatic struct slab *add_slab(struct hc_slab_alloc *a, const size_t size) {\n struct slab *s = hc_acquire(a->source, sizeof(struct slab) + size);\n hc_list_push_front(&a->slabs, &s->slabs);\n s->next = s->memory;\n return s;\n}\n\nstatic struct slab *get_slab(struct hc_slab_alloc *a, const size_t size) {\n if (size > a->slab_size) {\n return add_slab(a, size);\n }\n\n struct slab *result = NULL;\n\n hc_list_do(&a->slabs, sl) {\n struct slab *s = hc_baseof(sl, struct slab, slabs);\n uint8_t *p = hc_align(s->next, size);\n\n if (p + size > s->memory + a->slab_size) {\n break;\n }\n\n result = s;\n }\n\n return result ? result : add_slab(a, a->slab_size);\n}\n\nstatic void *slab_acquire(struct hc_malloc *a, const size_t size) {\n struct hc_slab_alloc *sa = hc_baseof(a, struct hc_slab_alloc, malloc);\n struct slab *s = get_slab(sa, size);\n uint8_t *p = hc_align(s->next, size);\n s->next = p + size;\n\n while (s->slabs.next != &s->slabs) {\n struct slab *ns = hc_baseof(s->slabs.next, struct slab, slabs);\n\n if (ns->next - ns->memory > s->next - s->memory) {\n hc_list_shift_back(&s->slabs);\n } else {\n break;\n }\n }\n \n return p;\n}\n\nstatic void slab_release(struct hc_malloc *a, void *p) {\n // Do nothing\n}\n\nstruct hc_slab_alloc *hc_slab_alloc_init(struct hc_slab_alloc *a,\n\t\t\t\t\t struct hc_malloc *source,\n\t\t\t\t\t const size_t slab_size) {\n a->malloc.acquire = slab_acquire;\n a->malloc.release = slab_release;\n a->source = source;\n hc_list_init(&a->slabs);\n a->slab_size = slab_size;\n return a;\n}\n\nvoid hc_slab_alloc_deinit(struct hc_slab_alloc *a) {\n hc_list_do(&a->slabs, _s) {\n struct slab *s = hc_baseof(_s, struct slab, slabs);\n hc_release(a->source, s);\n }\n}\n"], ["/hacktical-c/malloc1/tests.c", "#include \n#include \"malloc1.h\"\n\nvoid malloc1_tests() {\n assert(hc_align(0, 4) == 0);\n assert(hc_align(1, 4) == 4);\n assert(hc_align(3, 4) == 4);\n assert(hc_align(4, 4) == 4);\n assert(hc_align(5, 4) == 8);\n\n const int s = 1024;\n struct hc_bump_alloc a;\n hc_bump_alloc_init(&a, &hc_malloc_default, s);\n hc_defer(hc_bump_alloc_deinit(&a));\n assert(a.size == s);\n assert(a.offset == 0);\n\n int *ip = hc_acquire(&a.malloc, sizeof(int));\n *ip = 42;\n \n long *lp = hc_acquire(&a.malloc, sizeof(long));\n *lp = 42L;\n \n assert(a.offset >= sizeof(int) + sizeof(long));\n bool caught = false;\n \n void on_catch(struct hc_error *e) {\n assert(hc_streq(e->message, HC_NO_MEMORY) == 0);\n caught = true;\n }\n \n hc_catch(on_catch) {\n hc_acquire(&a.malloc, s);\n assert(false);\n }\n\n assert(caught);\n}\n"], ["/hacktical-c/fix/fix.c", "#include \n#include \n\n#include \"fix.h\"\n#include \"macro/macro.h\"\n#include \"stream1/stream1.h\"\n\nuint32_t hc_scale(const uint8_t exp) {\n static const uint32_t scale[HC_FIX_MAX_EXP+1] = {\n 1,\n 10,\n 100,\n 1000,\n 10000,\n 100000,\n 1000000,\n 10000000};\n\n assert(exp <= HC_FIX_MAX_EXP);\n return scale[exp];\n}\n\nhc_fix_t hc_fix(const uint8_t exp, const int64_t val) {\n return (hc_fix_t)hc_bitmask(exp, HC_FIX_EXP) +\n (hc_fix_t)(((val < 0) ? 1 : 0) << HC_FIX_EXP) +\n (hc_fix_t)(hc_abs(val) << HC_FIX_HDR);\n}\n\nuint8_t hc_fix_exp(const hc_fix_t x) {\n return hc_bitmask(x, HC_FIX_EXP);\n}\n\nint64_t hc_fix_val(const hc_fix_t x) {\n const int64_t v = x >> HC_FIX_HDR;\n return ((x >> HC_FIX_EXP) & 1) ? -v : v;\n}\n\nint64_t hc_fix_int(const hc_fix_t x) {\n return hc_fix_val(x) / hc_scale(hc_fix_exp(x));\n}\n\nint64_t hc_fix_frac(const hc_fix_t x) {\n const int64_t xv = hc_fix_val(x);\n const uint32_t xs = hc_scale(hc_fix_exp(x));\n return xv - (xv / xs) * xs;\n}\n\ndouble hc_fix_double(const hc_fix_t x) {\n return hc_fix_val(x) / (double)hc_scale(hc_fix_exp(x));\n}\n\nhc_fix_t hc_fix_add(const hc_fix_t x, const hc_fix_t y) {\n const uint8_t xe = hc_fix_exp(x);\n const uint8_t ye = hc_fix_exp(y);\n\n if (xe == ye) {\n return hc_fix(xe, hc_fix_val(x) + hc_fix_val(y));\n }\n \n return hc_fix(xe, hc_fix_val(x) +\n\t\thc_fix_val(y) * hc_scale(xe) / hc_scale(ye));\n}\n\nhc_fix_t hc_fix_sub(const hc_fix_t x, const hc_fix_t y) {\n const uint8_t xe = hc_fix_exp(x);\n const uint8_t ye = hc_fix_exp(y);\n\n if (xe == ye) {\n return hc_fix(xe, hc_fix_val(x) - hc_fix_val(y));\n }\n\n return hc_fix(xe, hc_fix_val(x) -\n\t\thc_fix_val(y) * hc_scale(xe) / hc_scale(ye));\n}\n\nhc_fix_t hc_fix_mul(const hc_fix_t x, const hc_fix_t y) {\n return hc_fix(hc_fix_exp(x), hc_fix_val(x) *\n\t\thc_fix_val(y) / hc_scale(hc_fix_exp(y)));\n}\n\nhc_fix_t hc_fix_div(const hc_fix_t x, const hc_fix_t y) {\n return hc_fix(hc_fix_exp(x), hc_fix_val(x) /\n\t\thc_fix_val(y) / hc_scale(hc_fix_exp(y)));\n}\n\nvoid hc_fix_print(const hc_fix_t v, struct hc_stream *out) {\n hc_printf(out,\n\t \"%\" PRId64 \".%\" PRId64,\n\t hc_fix_int(v),\n\t hc_fix_frac(v));\n}\n"], ["/hacktical-c/vector/vector.c", "#include \n#include \n#include \n#include \n#include \"vector.h\"\n#include \"malloc1/malloc1.h\"\n\nstatic void grow(struct hc_vector *v) {\n hc_vector_grow(v, v->capacity ? v->capacity*2 : 2);\n}\n\nstruct hc_vector *hc_vector_init(struct hc_vector *v,\n\t\t\t\t struct hc_malloc *malloc,\n\t\t\t\t const size_t item_size) {\n v->malloc = malloc;\n v->item_size = item_size;\n v->capacity = 0;\n v->length = 0;\n v->start = v->end = NULL;\n return v;\n}\n\nvoid hc_vector_deinit(struct hc_vector *v) {\n if (v->start) { hc_release(v->malloc, v->start); }\n}\n\nvoid hc_vector_grow(struct hc_vector *v, const size_t capacity) {\n v->capacity = capacity; \n size_t size = v->item_size * (v->capacity+1);\n uint8_t *new_start = hc_acquire(v->malloc, size);\n\n if (v->start) {\n memmove(new_start, v->start, v->length * v->item_size);\n hc_release(v->malloc, v->start); \n }\n \n v->start = new_start;\n v->end = v->start + v->item_size*v->length;\n}\n\nvoid hc_vector_clear(struct hc_vector *v) {\n v->length = 0;\n v->end = v->start;\n}\n\nvoid *hc_vector_get(struct hc_vector *v, const size_t i) {\n return v->start ? v->start + v->item_size*i : NULL;\n}\n\nconst void *hc_vector_get_const(const struct hc_vector *v, const size_t i) {\n return v->start ? v->start + v->item_size*i : NULL;\n}\n\nvoid *hc_vector_push(struct hc_vector *v) {\n if (v->length == v->capacity) { grow(v); }\n void *p = v->end;\n v->end += v->item_size;\n v->length++;\n return p;\n}\n\nvoid *hc_vector_peek(struct hc_vector *v) {\n return v->length ? v->end - v->item_size : NULL;\n}\n\nvoid *hc_vector_pop(struct hc_vector *v) {\n if (!v->length) { return NULL; }\n v->end -= v->item_size;\n v->length--;\n return v->end;\n}\n\nvoid *hc_vector_insert(struct hc_vector *v, const size_t i, const size_t n) {\n const size_t m = v->length+n;\n if (m > v->capacity) { hc_vector_grow(v, m); } \n uint8_t *const p = hc_vector_get(v, i);\n\n if (i < v->length) {\n memmove(p + v->item_size*n, p, (v->length - i) * v->item_size);\n }\n \n v->length += n;\n v->end += n*v->item_size;\n return p;\n}\n\nbool hc_vector_delete(struct hc_vector *v, const size_t i, const size_t n) {\n const size_t m = i+n;\n assert(m <= v->length);\n\n if (m < v->length) {\n uint8_t *const p = hc_vector_get(v, i);\n memmove(p, p + n*v->item_size, i + (v->length-n) * v->item_size);\n }\n\n v->length -= n;\n v->end -= n*v->item_size;\n return true;\n}\n"], ["/hacktical-c/reflect/reflect.c", "#include \n#include \n#include \n\n#include \"reflect.h\"\n#include \"stream1/stream1.h\"\n\nstruct hc_value *hc_value_init(struct hc_value *v, const struct hc_type *t) {\n v->type = t;\n return v;\n}\n\nvoid hc_value_deinit(struct hc_value *v) {\n if (v->type->deinit) {\n v->type->deinit(v);\n }\n}\n\nstruct hc_value *hc_value_copy(struct hc_value *dst, struct hc_value *src) {\n const struct hc_type *t = src->type;\n \n if (t->copy) {\n dst->type = t;\n t->copy(dst, src);\n } else {\n *dst = *src;\n }\n\n return dst;\n}\n\nvoid hc_value_print(struct hc_value *v, struct hc_stream *out) {\n if (v->type->print) {\n v->type->print(v, out);\n } else {\n hc_value_write(v, out);\n }\n}\n\nvoid hc_value_write(struct hc_value *v, struct hc_stream *out) {\n assert(v->type->write);\n v->type->write(v, out);\n}\n\nstatic void bool_write(const struct hc_value *v, struct hc_stream *out) {\n hc_puts(out, v->as_bool ? \"true\" : \"false\");\n}\n\nconst struct hc_type HC_BOOL = {\n .name = \"Bool\",\n .copy = NULL,\n .write = bool_write\n};\n\nstatic void fix_write(const struct hc_value *v, struct hc_stream *out) {\n hc_fix_print(v->as_fix, out);\n}\n\nconst struct hc_type HC_FIX = {\n .name = \"Fix\",\n .copy = NULL,\n .write = fix_write\n};\n\nstatic void int_write(const struct hc_value *v, struct hc_stream *out) {\n hc_printf(out, \"%d\", v->as_int);\n}\n\nconst struct hc_type HC_INT = {\n .name = \"Int\",\n .copy = NULL,\n .write = int_write\n};\n\nstatic void string_copy(struct hc_value *dst, struct hc_value *src) {\n dst->as_string = strdup(src->as_string);\n}\n\nstatic void string_deinit(struct hc_value *v) {\n free(v->as_string);\n}\n\nstatic void string_print(const struct hc_value *v, struct hc_stream *out) {\n hc_puts(out, v->as_string);\n}\n\nstatic void string_write(const struct hc_value *v, struct hc_stream *out) {\n hc_putc(out, '\"');\n string_print(v, out);\n hc_putc(out, '\"');\n}\n\nconst struct hc_type HC_STRING = {\n .name = \"String\",\n .copy = string_copy,\n .deinit = string_deinit,\n .print = string_print,\n .write = string_write\n};\n\nstatic void time_write(const struct hc_value *v, struct hc_stream *out) {\n hc_time_printf(&v->as_time, HC_TIME_FORMAT, out);\n}\n\nconst struct hc_type HC_TIME = {\n .name = \"Time\",\n .copy = NULL,\n .write = time_write\n};\n"], ["/hacktical-c/set/set.c", "#include \n#include \"set.h\"\n\nstruct hc_set *hc_set_init(struct hc_set *s,\n\t\t\t struct hc_malloc *malloc,\n\t\t\t const size_t item_size,\n\t\t\t hc_cmp_t cmp) {\n hc_vector_init(&s->items, malloc, item_size);\n s->cmp = cmp;\n s->key = NULL;\n return s;\n}\n\nvoid hc_set_deinit(struct hc_set *s) {\n hc_vector_deinit(&s->items);\n}\n\nsize_t hc_set_index(const struct hc_set *s, const void *key, bool *ok) {\n size_t min = 0, max = s->items.length;\n\n while (min < max) {\n const size_t i = (min+max)/2;\n const void *v = hc_vector_get_const(&s->items, i);\n const void *k = s->key ? s->key(v) : v;\n\n switch (s->cmp(key, k)) {\n case HC_LT:\n max = i;\n break;\n case HC_GT:\n min = i+1;\n break;\n default:\n if (ok) {\n\t*ok = true;\n }\n \n return i;\n }\n }\n\n return min;\n}\n\nsize_t hc_set_length(const struct hc_set *s) {\n return s->items.length;\n}\n\nvoid *hc_set_find(struct hc_set *s, const void *key) {\n bool ok = false;\n const size_t i = hc_set_index(s, key, &ok);\n return ok ? hc_vector_get(&s->items, i) : NULL;\n}\n\nvoid *hc_set_add(struct hc_set *s, const void *key, const bool force) {\n bool ok = false;\n const size_t i = hc_set_index(s, key, &ok);\n\n if (ok && !force) {\n return NULL;\n }\n \n return hc_vector_insert(&s->items, i, 1);\n}\n\nvoid hc_set_clear(struct hc_set *s) {\n hc_vector_clear(&s->items);\n}\n"], ["/hacktical-c/task/task.c", "#include \n#include \n#include \"task.h\"\n\nstruct hc_task *hc_task_init(struct hc_task *t,\n\t\t\t struct hc_task_list *tl,\n\t\t\t hc_task_body body) {\n t->body = body;\n t->state = 0;\n t->done = false;\n hc_list_push_back(&tl->tasks, &t->list);\n return t;\n}\n\nstruct hc_task_list *hc_task_list_init(struct hc_task_list *tl) {\n hc_list_init(&tl->tasks);\n return tl;\n}\n\nvoid hc_task_list_run(const struct hc_task_list *tl) {\n bool all_done = false;\n \n while (!all_done) {\n all_done = true;\n \n hc_list_do(&tl->tasks, i) {\n struct hc_task *t = hc_baseof(i, struct hc_task, list);\n\n if (!t->done) {\n\tt->body(t);\n\tall_done = false;\n }\n }\n }\n}\n"], ["/hacktical-c/macro/macro.c", "#include \"macro.h\"\n\nsize_t hc_alignof(size_t size) {\n const size_t max = _Alignof(max_align_t);\n if (size >= max) { return max; }\n size_t v = 1;\n for (size_t nv = 1; nv <= size; v = nv, nv = v << 1);\n return v;\n}\n"], ["/hacktical-c/list/list.c", "#include \n#include \"list.h\"\n\nvoid hc_list_init(struct hc_list *l) {\n l->prev = l->next = l;\n}\n\nbool hc_list_nil(const struct hc_list *l) {\n return l->prev == l && l->next == l;\n}\n\nstruct hc_list *hc_list_delete(struct hc_list *l) {\n l->prev->next = l->next;\n l->next->prev = l->prev;\n return l;\n}\n\nvoid hc_list_push_front(struct hc_list *l, struct hc_list *it) {\n hc_list_push_back(l->next, it);\n}\n\nstruct hc_list *hc_list_pop_front(struct hc_list *l) {\n struct hc_list *it = l->next;\n return (it == l) ? NULL : hc_list_delete(it);\n}\n\nstruct hc_list *hc_list_peek_front(struct hc_list *l) {\n struct hc_list *it = l->next;\n return (it == l) ? NULL : it;\n}\n\nvoid hc_list_push_back(struct hc_list *l, struct hc_list *it) {\n it->prev = l->prev;\n l->prev->next = it;\n it->next = l;\n l->prev = it;\n}\n\nstruct hc_list *hc_list_pop_back(struct hc_list *l) {\n struct hc_list *it = l->prev;\n return (it == l) ? NULL : hc_list_delete(it);\n}\n\nstruct hc_list *hc_list_peek_back(struct hc_list *l) {\n struct hc_list *it = l->prev;\n return (it == l) ? NULL : it;\n}\n\nvoid hc_list_shift_back(struct hc_list *l) {\n l->next->prev = l->prev;\n l->prev->next = l->next;\n l->prev = l->next;\n l->next = l->next->next;\n l->prev->next = l;\n l->next->prev = l;\n}\n"]], "task_instance_info": {"created_time": "2025-08-20 17:57:20", "created_task_model": "DeepSeek-R1", "function_skeleton": "int main() {}", "code_description": "This code segment functions to: Execute a sequential series of test procedures across multiple system components and ensure proper cleanup of error tracking resources after all verifications complete."}, "inference_content": {"inference_model": "DeepSeek-R1", "inference_result": "```c\n[TASK_BEGIN]\nint main() {\n test_chrono_run();\n test_dsl_run();\n test_dynamic_run();\n test_error_run();\n test_fix_run();\n test_list_run();\n test_macro_run();\n test_malloc1_run();\n test_malloc2_run();\n test_reflect_run();\n test_set_run();\n test_slog_run();\n test_stream1_run();\n test_task_run();\n test_vector_run();\n test_vm_run();\n return 0;\n}\n[TASK_END]\n```", "inference_time": "2025-08-20 17-57-40"}, "editdistance_info": {"edit_distance": 26.5306, "calculate_time": "2025-08-20 17:57:40", "true_code_clean": "int main() {\n chrono_tests();\n dsl_tests();\n dynamic_tests();\n error_tests();\n fix_tests();\n list_tests();\n macro_tests();\n malloc1_tests();\n malloc2_tests();\n reflect_tests();\n set_tests();\n slog_tests();\n stream1_tests();\n task_tests();\n vector_tests();\n vm_tests();\n hc_errors_deinit();\n return 0;\n}", "predict_code_clean": "int main() {\n test_chrono_run();\n test_dsl_run();\n test_dynamic_run();\n test_error_run();\n test_fix_run();\n test_list_run();\n test_macro_run();\n test_malloc1_run();\n test_malloc2_run();\n test_reflect_run();\n test_set_run();\n test_slog_run();\n test_stream1_run();\n test_task_run();\n test_vector_run();\n test_vm_run();\n return 0;\n}"}} {"repo_name": "hacktical-c", "file_name": "/hacktical-c/vector/tests.c", "inference_info": {"prefix_code": "#include \n#include \n#include \"vector.h\"\n\n", "suffix_code": "\n", "middle_code": "void vector_tests() {\n struct hc_vector v;\n hc_vector_init(&v, &hc_malloc_default, sizeof(int));\n const int n = 100;\n for (int i = 0; i < n; i++) {\n *(int *)hc_vector_push(&v) = i;\n }\n {\n int i = 0;\n hc_vector_do(&v, it) {\n assert(*(int *)it == i++);\n }\n }\n assert(v.length == n);\n for (int i = 0; i < n; i++) {\n assert(*(int *)hc_vector_get(&v, i) == i);\n }\n assert(*(int *)hc_vector_pop(&v) == n-1);\n assert(*(int *)hc_vector_peek(&v) == n-2);\n assert(v.length == n-1);\n for (int i = 0; i < n-1; i++) {\n assert(*(int *)hc_vector_get(&v, i) == i);\n }\n assert(hc_vector_delete(&v, 0, 1));\n assert(v.length == n-2);\n for (int i = 1; i < n-1; i++) {\n assert(*(int *)hc_vector_get(&v, i-1) == i);\n }\n (*(int *)hc_vector_insert(&v, 0, 1) = 0);\n assert(v.length == n-1);\n for (int i = 0; i < n-1; i++) {\n assert(*(int *)hc_vector_get(&v, i) == i);\n }\n hc_vector_clear(&v);\n assert(v.length == 0);\n hc_vector_deinit(&v);\n}", "code_description": null, "fill_type": "FUNCTION_TYPE", "language_type": "c", "sub_task_type": null}, "context_code": [["/hacktical-c/set/tests.c", "#include \n#include \n#include \"set.h\"\n\nstruct map_item {\n int k, v;\n};\n\nstatic enum hc_order cmp(const void *x, const void *y) {\n return hc_cmp(*(const int *)x, *(const int *)y);\n}\n\nstatic const void *key(const void *x) {\n return &((const struct map_item *)x)->k;\n}\n\nvoid set_tests() {\n int n = 10;\n struct hc_set s;\n hc_set_init(&s, &hc_malloc_default, sizeof(struct map_item), cmp);\n s.key = key;\n \n for (int i = 0; i < n; i++) {\n struct map_item *it = hc_set_add(&s, &i, false);\n *it = (struct map_item){.k = i, .v = i};\n }\n\n assert(hc_set_length(&s) == n);\n \n for (int i = 0; i < n; i++) {\n struct map_item *it = hc_set_find(&s, &i);\n assert(it);\n assert(it->k == i);\n assert(it->v == i);\n }\n\n hc_set_clear(&s);\n assert(hc_set_length(&s) == 0); \n hc_set_deinit(&s);\n}\n"], ["/hacktical-c/vector/vector.c", "#include \n#include \n#include \n#include \n#include \"vector.h\"\n#include \"malloc1/malloc1.h\"\n\nstatic void grow(struct hc_vector *v) {\n hc_vector_grow(v, v->capacity ? v->capacity*2 : 2);\n}\n\nstruct hc_vector *hc_vector_init(struct hc_vector *v,\n\t\t\t\t struct hc_malloc *malloc,\n\t\t\t\t const size_t item_size) {\n v->malloc = malloc;\n v->item_size = item_size;\n v->capacity = 0;\n v->length = 0;\n v->start = v->end = NULL;\n return v;\n}\n\nvoid hc_vector_deinit(struct hc_vector *v) {\n if (v->start) { hc_release(v->malloc, v->start); }\n}\n\nvoid hc_vector_grow(struct hc_vector *v, const size_t capacity) {\n v->capacity = capacity; \n size_t size = v->item_size * (v->capacity+1);\n uint8_t *new_start = hc_acquire(v->malloc, size);\n\n if (v->start) {\n memmove(new_start, v->start, v->length * v->item_size);\n hc_release(v->malloc, v->start); \n }\n \n v->start = new_start;\n v->end = v->start + v->item_size*v->length;\n}\n\nvoid hc_vector_clear(struct hc_vector *v) {\n v->length = 0;\n v->end = v->start;\n}\n\nvoid *hc_vector_get(struct hc_vector *v, const size_t i) {\n return v->start ? v->start + v->item_size*i : NULL;\n}\n\nconst void *hc_vector_get_const(const struct hc_vector *v, const size_t i) {\n return v->start ? v->start + v->item_size*i : NULL;\n}\n\nvoid *hc_vector_push(struct hc_vector *v) {\n if (v->length == v->capacity) { grow(v); }\n void *p = v->end;\n v->end += v->item_size;\n v->length++;\n return p;\n}\n\nvoid *hc_vector_peek(struct hc_vector *v) {\n return v->length ? v->end - v->item_size : NULL;\n}\n\nvoid *hc_vector_pop(struct hc_vector *v) {\n if (!v->length) { return NULL; }\n v->end -= v->item_size;\n v->length--;\n return v->end;\n}\n\nvoid *hc_vector_insert(struct hc_vector *v, const size_t i, const size_t n) {\n const size_t m = v->length+n;\n if (m > v->capacity) { hc_vector_grow(v, m); } \n uint8_t *const p = hc_vector_get(v, i);\n\n if (i < v->length) {\n memmove(p + v->item_size*n, p, (v->length - i) * v->item_size);\n }\n \n v->length += n;\n v->end += n*v->item_size;\n return p;\n}\n\nbool hc_vector_delete(struct hc_vector *v, const size_t i, const size_t n) {\n const size_t m = i+n;\n assert(m <= v->length);\n\n if (m < v->length) {\n uint8_t *const p = hc_vector_get(v, i);\n memmove(p, p + n*v->item_size, i + (v->length-n) * v->item_size);\n }\n\n v->length -= n;\n v->end -= n*v->item_size;\n return true;\n}\n"], ["/hacktical-c/dsl/benchmarks.c", "#include \"chrono/chrono.h\"\n#include \"dsl.h\"\n\nvoid dsl_benchmarks() {\n hc_time_t t;\n const int n = 100000;\n\n char buf[32];\n t = hc_now();\n \n for (int i = 0; i < n; i++) {\n sprintf(buf, \"abc %s def\", \"ghi\");\n }\n\n hc_time_print(&t, \"sprintf: \"); \n \n struct hc_dsl dsl;\n hc_dsl_init(&dsl, &hc_malloc_default);\n hc_defer(hc_dsl_deinit(&dsl));\n struct hc_memory_stream out;\n hc_memory_stream_init(&out, &hc_malloc_default);\n hc_defer(hc_stream_deinit(&out.stream));\n dsl.out = &out.stream;\n hc_dsl_set_string(&dsl, \"foo\", \"ghi\");\n hc_dsl_eval(&dsl, \"abc $(print foo) def\");\n\n t = hc_now();\n\n for (int i = 0; i < n; i++) {\n hc_vector_clear(&out.data);\n hc_vm_eval(&dsl.vm, 0, -1);\n }\n\n hc_time_print(&t, \"dsl: \");\n}\n"], ["/hacktical-c/list/tests.c", "#include \n#include \"list.h\"\n\nstruct list_item {\n struct hc_list ls;\n int value;\n};\n \nvoid list_tests() {\n struct hc_list head;\n hc_list_init(&head);\n\n const int n = 10;\n struct list_item items[n];\n \n for (int i = 0; i < n; i++) {\n items[i].value = i;\n hc_list_push_back(&head, &items[i].ls);\n }\n\n int i = 0;\n \n hc_list_do(&head, il) {\n assert(hc_baseof(il, struct list_item, ls)->value == i++);\n }\n}\n"], ["/hacktical-c/fix/benchmarks.c", "#include \"chrono/chrono.h\"\n#include \"fix.h\"\n\nvoid fix_benchmarks() {\n hc_time_t t;\n const int n = 100000;\n\n t = hc_now();\n double dv = 0;\n \n for (int i = 0; i < n; i++) {\n dv += 0.001;\n }\n\n hc_time_print(&t, \"double: \"); \n\n t = hc_now();\n hc_fix_t fv = hc_fix(3, 0);\n hc_fix_t fd = hc_fix(3, 1);\n \n for (int i = 0; i < n; i++) {\n fv = hc_fix_add(fv, fd);\n }\n\n hc_time_print(&t, \"fix: \"); \n}\n"], ["/hacktical-c/set/set.c", "#include \n#include \"set.h\"\n\nstruct hc_set *hc_set_init(struct hc_set *s,\n\t\t\t struct hc_malloc *malloc,\n\t\t\t const size_t item_size,\n\t\t\t hc_cmp_t cmp) {\n hc_vector_init(&s->items, malloc, item_size);\n s->cmp = cmp;\n s->key = NULL;\n return s;\n}\n\nvoid hc_set_deinit(struct hc_set *s) {\n hc_vector_deinit(&s->items);\n}\n\nsize_t hc_set_index(const struct hc_set *s, const void *key, bool *ok) {\n size_t min = 0, max = s->items.length;\n\n while (min < max) {\n const size_t i = (min+max)/2;\n const void *v = hc_vector_get_const(&s->items, i);\n const void *k = s->key ? s->key(v) : v;\n\n switch (s->cmp(key, k)) {\n case HC_LT:\n max = i;\n break;\n case HC_GT:\n min = i+1;\n break;\n default:\n if (ok) {\n\t*ok = true;\n }\n \n return i;\n }\n }\n\n return min;\n}\n\nsize_t hc_set_length(const struct hc_set *s) {\n return s->items.length;\n}\n\nvoid *hc_set_find(struct hc_set *s, const void *key) {\n bool ok = false;\n const size_t i = hc_set_index(s, key, &ok);\n return ok ? hc_vector_get(&s->items, i) : NULL;\n}\n\nvoid *hc_set_add(struct hc_set *s, const void *key, const bool force) {\n bool ok = false;\n const size_t i = hc_set_index(s, key, &ok);\n\n if (ok && !force) {\n return NULL;\n }\n \n return hc_vector_insert(&s->items, i, 1);\n}\n\nvoid hc_set_clear(struct hc_set *s) {\n hc_vector_clear(&s->items);\n}\n"], ["/hacktical-c/stream1/stream1.c", "#include \n#include \n#include \n#include \n\n#include \"dynamic/dynamic.h\"\n#include \"error/error.h\"\n#include \"malloc1/malloc1.h\"\n#include \"macro/macro.h\"\n#include \"stream1.h\"\n\nsize_t hc_read(struct hc_stream *s, uint8_t *data, const size_t n) {\n assert(s->read);\n return s->read(s, data, n);\n}\n\nsize_t hc_write(struct hc_stream *s, const uint8_t *data, const size_t n) {\n assert(s->write);\n return s->write(s, data, n);\n}\n\nchar hc_getc(struct hc_stream *s) {\n char c = 0;\n return hc_read(s, (uint8_t *)&c, 1) ? c : 0;\n}\n\nchar *hc_gets(struct hc_stream *s, struct hc_malloc *malloc) {\n struct hc_vector out;\n hc_vector_init(&out, malloc, 1);\n\n for (;;) {\n char c = hc_getc(s);\n\n if (c == EOF) {\n break;\n }\n\n *(char *)hc_vector_push(&out) = c;\n\n if (c == '\\n') {\n break;\n }\n }\n\n \n *(char *)hc_vector_push(&out) = 0;\n return (char *)out.start;\n}\n\nsize_t hc_putc(struct hc_stream *s, const char data) {\n const uint8_t d[2] = {data, 0};\n return hc_write(s, d, 1);\n}\n\nsize_t hc_puts(struct hc_stream *s, const char *data) {\n return hc_write(s, (const uint8_t *)data, strlen(data));\n}\n\nsize_t hc_vprintf(struct hc_stream *s,\n\t\t const char *spec,\n\t\t va_list args) {\n char *data = hc_vsprintf(spec, args);\n hc_defer(free(data));\n return hc_write(s, (uint8_t *)data, strlen(data));\n}\n\nsize_t hc_printf(struct hc_stream *s, const char *spec, ...) {\n va_list args;\n va_start(args, spec);\n hc_defer(va_end(args));\n return hc_vprintf(s, spec, args);\n}\n\nvoid hc_stream_deinit(struct hc_stream *s) {\n assert(s->deinit);\n s->deinit(s);\n}\n\nsize_t file_read(struct hc_stream *s, uint8_t *data, const size_t n) {\n struct hc_file_stream *fs = hc_baseof(s, struct hc_file_stream, stream);\n assert(fs->file);\n return fread(data, n, 1, fs->file);\n}\n\nsize_t file_write(struct hc_stream *s, const uint8_t *data, const size_t n) {\n struct hc_file_stream *fs = hc_baseof(s, struct hc_file_stream, stream);\n assert(fs->file);\n return fwrite(data, n, 1, fs->file);\n}\n\nvoid file_deinit(struct hc_stream *s) {\n struct hc_file_stream *fs = hc_baseof(s, struct hc_file_stream, stream);\n\n if (fs->opts.close_file) {\n assert(fs->file);\n \n if (fclose(fs->file) == EOF) {\n hc_throw(\"Failed closing file\");\n }\n \n fs->file = NULL;\n }\n}\n\nstruct hc_file_stream *_hc_file_stream_init(struct hc_file_stream *s,\n\t\t\t\t\t FILE *file,\n\t\t\t\t\t const struct hc_file_stream_opts opts) {\n s->stream = (struct hc_stream){\n .read = file_read,\n .write = file_write,\n .deinit = file_deinit,\n };\n \n s->file = file;\n s->opts = opts;\n return s;\n};\n\nstruct hc_stream *hc_stdout() {\n static __thread bool init = true;\n static __thread struct hc_file_stream s;\n\n if (init) {\n hc_file_stream_init(&s, stdout);\n init = false;\n }\n\n return &s.stream;\n}\n\nsize_t memory_read(struct hc_stream *s, uint8_t *data, size_t n) {\n struct hc_memory_stream *ms = hc_baseof(s, struct hc_memory_stream, stream);\n\n if (ms->rpos + n > ms->data.length) {\n n = ms->data.length - ms->rpos;\n }\n \n memcpy(data, ms->data.start + ms->rpos, n);\n ms->rpos += n;\n return n;\n}\n\nsize_t memory_write(struct hc_stream *s,\n\t\t const uint8_t *data,\n\t\t const size_t n) {\n struct hc_memory_stream *ms = hc_baseof(s, struct hc_memory_stream, stream);\n uint8_t *const dst = hc_vector_insert(&ms->data, ms->data.length, n);\n memcpy(dst, data, n);\n return n;\n}\n\nvoid memory_deinit(struct hc_stream *s) {\n struct hc_memory_stream *ms = hc_baseof(s, struct hc_memory_stream, stream);\n hc_vector_deinit(&ms->data);\n}\n\nstruct hc_memory_stream *hc_memory_stream_init(struct hc_memory_stream *s,\n\t\t\t\t\t struct hc_malloc *malloc) {\n s->stream = (struct hc_stream){\n .read = memory_read,\n .write = memory_write,\n .deinit = memory_deinit,\n };\n \n hc_vector_init(&s->data, malloc, 1);\n s->rpos = 0;\n return s;\n}\n\nconst char *hc_memory_stream_string(struct hc_memory_stream *s) {\n if (!s->data.length || (*(s->data.end-1))) {\n *(uint8_t *)hc_vector_push(&s->data) = 0;\n }\n\n return (const char *)s->data.start;\n}\n"], ["/hacktical-c/dynamic/dynamic.c", "#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#include \"dynamic.h\"\n#include \"error/error.h\"\n#include \"malloc1/malloc1.h\"\n#include \"vector/vector.h\"\n\nstruct hc_proc *_hc_proc_init(struct hc_proc *p, char *cmd[]) {\n int fds[2];\n\n if (pipe(fds) == -1) {\n hc_throw(\"Failed creating pipe: %d\", errno);\n }\n \n pid_t child_pid = fork();\n\n switch (child_pid) {\n case 0: {\n if (close(fds[1]) == -1) {\n hc_throw(\"Failed closing pipe writer: %d\", errno);\n }\n \n if (dup2(fds[0], 0) == -1) {\n hc_throw(\"Failed rebinding stdin: %d\", errno);\n }\n \n char *const env[] = {\"PATH=/bin:/sbin\", NULL};\n\n if (execve(cmd[0], cmd, env) == -1) {\n hc_throw(\"Failed to execve '%s': %d\", cmd[0], errno);\n }\n }\n case -1:\n hc_throw(\"Failed forking process: %d\", errno);\n default:\n if (close(fds[0]) == -1) {\n hc_throw(\"Failed closing pipe reader: %d\", errno);\n }\n \n p->pid = child_pid;\n p->in = fds[1];\n break;\n }\n\n return p;\n}\n\nstatic void close_in(struct hc_proc *p) {\n if (p->in != -1 && close(p->in) == -1) {\n hc_throw(\"Failed closing stdin: %d\", errno);\n }\n}\n\nvoid hc_proc_wait(struct hc_proc *p) {\n close_in(p);\n\n if (waitpid(p->pid, NULL, 0) == -1) {\n hc_throw(\"Failed waiting for child process to exit: %d\", errno);\n }\n}\n\nvoid hc_proc_deinit(struct hc_proc *p) {\n close_in(p);\n}\n\nstatic void free_cmd(char **in) {\n for (char **s = in; *s; s++) {\n free(*s);\n }\n}\n\nvoid _hc_compile(const char *code,\n\t\t const char *out,\n\t\t const struct hc_compile_opts opts) {\n hc_array(const char *, pre, \n\t opts.cc, \"-shared\", \"-fpic\", \"-o\", out, \"-xc\");\n \n int n = pre_n + 2;\n for (int i = 0; opts.cflags[i]; i++, n++); \n char *cmd[n];\n int i = 0;\n\n for (; i < pre_n; i++) {\n cmd[i] = strdup(pre_a[i]);\n }\n \n for (; i < n - 2; i++) {\n cmd[i] = strdup(opts.cflags[i - pre_n]);\n }\n\n cmd[i++] = strdup(\"-\");\n cmd[i] = NULL;\n hc_defer(free_cmd(cmd));\n \n struct hc_proc child;\n _hc_proc_init(&child, cmd);\n hc_defer(hc_proc_deinit(&child));\n FILE *in = fdopen(child.in, \"w\");\n\n if (!in) {\n hc_throw(\"Failed opening stdin stream: %d\", errno);\n }\n \n child.in = -1;\n hc_defer(hc_proc_wait(&child));\n hc_defer(fclose(in));\n\n if (fputs(code, in) == EOF) {\n hc_throw(\"Failed writing code: %d\", errno);\n }\n}\n\nstruct hc_dlib *hc_dlib_init(struct hc_dlib *lib, const char *path) {\n lib->handle = dlopen(path, RTLD_NOW);\n\n if (!lib->handle) {\n hc_throw(\"Error opening dynamic library '%s': %s\", path, dlerror());\n }\n \n return lib;\n}\n\nstruct hc_dlib *hc_dlib_deinit(struct hc_dlib *lib) {\n if (dlclose(lib->handle) != 0) {\n hc_throw(\"Failed closing dynamic library: \", dlerror());\n }\n\n return lib;\n}\n\nvoid *hc_dlib_find(const struct hc_dlib *lib, const char *s) {\n dlerror();\n void *v = dlsym(lib->handle, s);\n char *e = dlerror();\n\n if (e) {\n hc_throw(\"Symbol '%s' not found: %s\", e);\n }\n\n return v;\n}\n\nchar *hc_vsprintf(const char *format, va_list args) {\n va_list tmp_args;\n va_copy(tmp_args, args);\n int len = vsnprintf(NULL, 0, format, tmp_args);\n va_end(tmp_args);\n\n if (len < 0) {\n hc_throw(\"Formatting '%s' failed: %d\", format, errno);\n }\n\n len++;\n char *out = malloc(len);\n vsnprintf(out, len, format, args);\n return out;\n} \n"], ["/hacktical-c/slog/slog.c", "#include \n#include \n#include \n#include \n\n#include \"error/error.h\"\n#include \"macro/macro.h\"\n#include \"malloc1/malloc1.h\"\n#include \"slog.h\"\n\n__thread struct hc_slog *_hc_slog = NULL;\n\nstruct hc_slog *hc_slog() {\n if (_hc_slog != NULL) {\n return _hc_slog;\n }\n \n static __thread bool init = true;\n static __thread struct hc_slog_stream s;\n\n if (init) {\n hc_slog_stream_init(&s, hc_stdout());\n init = false;\n }\n\n return &s.slog;\n}\n\nstatic void field_deinit(struct hc_slog_field *f) {\n free(f->name);\n hc_value_deinit(&f->value);\n}\n\nstatic void slog_write(struct hc_slog *s,\n\t\t const size_t n,\n\t\t struct hc_slog_field *fields[]) {\n assert(s->write);\n s->write(s, n, fields);\n}\n\nvoid __hc_slog_write(struct hc_slog *s,\n\t\t const size_t n,\n\t\t struct hc_slog_field *fields[]) {\n slog_write(s, n, fields);\n \n for(size_t i = 0; i < n; i++) {\n struct hc_slog_field *f = fields[i];\n field_deinit(f);\n free(f);\n }\n}\n\nvoid _hc_slog_deinit(struct hc_slog *s) {\n if (s->deinit) {\n s->deinit(s);\n }\n}\n\nstatic struct hc_value *field_init(struct hc_slog_field *f,\n\t\t\t\t const char *name,\n\t\t\t\t const struct hc_type *type) {\n f->name = strdup(name);\n hc_value_init(&f->value, type);\n return &f->value;\n}\n\nstruct hc_slog_field *hc_slog_bool(const char *name, const bool value) {\n struct hc_slog_field *f = malloc(sizeof(struct hc_slog_field));\n field_init(f, name, &HC_BOOL)->as_bool = value;\n return f;\n}\n\nstruct hc_slog_field *hc_slog_int(const char *name, const int value) {\n struct hc_slog_field *f = malloc(sizeof(struct hc_slog_field));\n field_init(f, name, &HC_INT)->as_int = value;\n return f;\n}\n\nstruct hc_slog_field *hc_slog_string(const char *name, const char *value) {\n struct hc_slog_field *f = malloc(sizeof(struct hc_slog_field));\n field_init(f, name, &HC_STRING)->as_string = strdup(value);\n return f;\n}\n\nstruct hc_slog_field *hc_slog_time(const char *name, const hc_time_t value) {\n struct hc_slog_field *f = malloc(sizeof(struct hc_slog_field));\n field_init(f, name, &HC_TIME)->as_time = value;\n return f;\n}\n\nvoid stream_deinit(struct hc_slog *s) {\n struct hc_slog_stream *ss = hc_baseof(s, struct hc_slog_stream, slog);\n if (ss->opts.close_out) { hc_stream_deinit(ss->out); }\n}\n\nstatic void field_write(struct hc_slog_field *f, struct hc_stream *out) {\n hc_puts(out, f->name);\n hc_putc(out, '=');\n hc_value_write(&f->value, out);\n}\n\nstatic void stream_write(struct hc_slog *s,\n\t\t\t const size_t n,\n\t\t\t struct hc_slog_field *fields[]) {\n struct hc_slog_stream *ss = hc_baseof(s, struct hc_slog_stream, slog);\n\n for(size_t i = 0; i < n; i++) {\n struct hc_slog_field *f = fields[i];\n if (i) { hc_puts(ss->out, \", \"); }\n field_write(f, ss->out);\n }\n\n hc_putc(ss->out, '\\n');\n}\n\nstruct hc_slog_stream *_hc_slog_stream_init(struct hc_slog_stream *s,\n\t\t\t\t\t struct hc_stream *out,\n\t\t\t\t\t const struct hc_slog_stream_opts opts) {\n s->slog.deinit = stream_deinit;\n s->slog.write = stream_write;\n s->out = out;\n s->opts = opts;\n return s;\n}\n\nstatic void context_deinit(struct hc_slog *s) {\n struct hc_slog_context *sc = hc_baseof(s, struct hc_slog_context, slog);\n\n for (size_t i = 0; i < sc->length; i++) {\n struct hc_slog_field *f = sc->fields[i];\n field_deinit(f);\n free(f);\n }\n\n free(sc->fields);\n}\n\nstatic void context_write(struct hc_slog *s,\n\t\t\t const size_t n,\n\t\t\t struct hc_slog_field *fields[]) {\n struct hc_slog_context *c = hc_baseof(s, struct hc_slog_context, slog);\n struct hc_slog_field *fs[c->length + n];\n memcpy(fs, c->fields, sizeof(struct hc_slog_field *) * c->length);\n memcpy(fs + c->length, fields, sizeof(struct hc_slog_field *) * n);\n slog_write(c->parent, c->length + n, fs);\n}\n\nstruct hc_slog_context *hc_slog_context_init(struct hc_slog_context *c,\n\t\t\t\t\t size_t length,\n\t\t\t\t\t struct hc_slog_field *fields[]) {\n c->slog.deinit = context_deinit;\n c->slog.write = context_write;\n c->parent = hc_slog();\n c->length = length;\n size_t s = sizeof(struct hc_slog_field *) * length;\n c->fields = malloc(s);\n memcpy(c->fields, fields, s);\n return c;\n}\n"], ["/hacktical-c/malloc2/benchmarks.c", "#include \n\n#include \"chrono/chrono.h\"\n#include \"malloc2.h\"\n\n#define N 1000\n#define MAX_SIZE 64\n#define GET_SIZE() ((rand() % MAX_SIZE) + 1)\n\nstatic void run_malloc() {\n int *ps[N];\n hc_time_t t = hc_now();\n \n for (int i = 0; i < N; i++) {\n ps[i] = malloc(GET_SIZE());\n }\n\n for (int i = 0; i < N; i++) {\n free(ps[i]);\n }\n\n hc_time_print(&t, \"malloc: \"); \n}\n\nstatic void run_bump() {\n struct hc_bump_alloc a;\n hc_bump_alloc_init(&a, &hc_malloc_default, N * MAX_SIZE);\n hc_time_t t = hc_now();\n\n for (int i = 0; i < N; i++) {\n hc_acquire(&a.malloc, GET_SIZE());\n }\n\n hc_bump_alloc_deinit(&a);\n hc_time_print(&t, \"bump: \");\n}\n\nstatic void run_slab() {\n struct hc_slab_alloc a;\n hc_slab_alloc_init(&a, &hc_malloc_default, N);\n hc_time_t t = hc_now();\n\n for (int i = 0; i < N; i++) {\n hc_acquire(&a.malloc, GET_SIZE());\n }\n\n hc_slab_alloc_deinit(&a);\n hc_time_print(&t, \"slab: \");\n}\n\nvoid malloc2_benchmarks() {\n const int s = time(NULL);\n \n srand(s);\n run_malloc();\n\n srand(s);\n run_bump();\n\n srand(s);\n run_slab();\n}\n"], ["/hacktical-c/vm/vm.c", "#include \n#include \n#include \n#include \n#include \n\n#include \"error/error.h\"\n#include \"macro/macro.h\"\n#include \"vm.h\"\n\nstruct hc_sloc hc_sloc(const char *source, const int row, const int col) {\n struct hc_sloc s = {.source = {0}, .row = row, .col = col};\n assert(strlen(source) < sizeof(s.source));\n strcpy(s.source, source);\n return s;\n}\n\nconst char *hc_sloc_string(struct hc_sloc *sloc) {\n snprintf(sloc->out, sizeof(sloc->out), \"'%s'; row %d, column %d\",\n\t sloc->source, sloc->row, sloc->col);\n return sloc->out;\n}\n\n\nvoid hc_vm_init(struct hc_vm *vm, struct hc_malloc *malloc) {\n hc_vector_init(&vm->stack, malloc, sizeof(struct hc_value));\n hc_vector_init(&vm->ops, malloc, sizeof(const struct hc_op *));\n hc_vector_init(&vm->code, malloc, sizeof(hc_op_eval_t));\n}\n\nstatic size_t op_items(const struct hc_op *op,\n\t\t uint8_t *p,\n\t\t struct hc_vm *vm) {\n const size_t s = op->size + hc_align(p, op->align) - p;\n return ceil(s / (double)vm->code.item_size);\n}\n\nstatic void deinit_stack(struct hc_vm *vm) {\n hc_vector_do(&vm->stack, v) {\n hc_value_deinit(v);\n }\n\n hc_vector_deinit(&vm->stack);\n}\n\nstatic void deinit_ops(struct hc_vm *vm) {\n uint8_t *p = vm->code.start;\n \n hc_vector_do(&vm->ops, _op) {\n const struct hc_op *op = *(const struct hc_op **)_op;\n p += sizeof(hc_op_eval_t);\n\n if (op->deinit) {\n op->deinit(hc_align(p, op->align));\n }\n\n p += op_items(op, p, vm) * vm->code.item_size;\n }\n\n hc_vector_deinit(&vm->ops);\n}\n\nvoid hc_vm_deinit(struct hc_vm *vm) { \n deinit_stack(vm);\n deinit_ops(vm);\n hc_vector_deinit(&vm->code);\n}\n\nstruct hc_value *hc_vm_push(struct hc_vm *vm) {\n return hc_vector_push(&vm->stack);\n}\n\nstruct hc_value *hc_vm_peek(struct hc_vm *vm) {\n return hc_vector_peek(&vm->stack);\n}\n\nstruct hc_value *hc_vm_pop(struct hc_vm *vm) {\n return hc_vector_pop(&vm->stack);\n}\n\nsize_t hc_vm_emit(struct hc_vm *vm,\n\t\t const struct hc_op *op,\n\t\t const void *data) {\n *(const struct hc_op **)hc_vector_push(&vm->ops) = op;\n const size_t pc = vm->code.length;\n *(hc_op_eval_t *)hc_vector_push(&vm->code) = op->eval;\n \n uint8_t *const p = hc_vector_insert(&vm->code,\n\t\t\t\t vm->code.length,\n\t\t\t\t op_items(op, vm->code.end, vm));\n \n memcpy(hc_align(p, op->align), data, op->size);\n return pc;\n}\n\nvoid hc_vm_eval(struct hc_vm *vm,\n\t\tconst size_t start_pc,\n\t\tconst size_t end_pc) {\n const uint8_t *const ep = (end_pc == -1)\n ? vm->code.end\n : hc_vector_get(&vm->code, end_pc);\n\n for (uint8_t *p = hc_vector_get(&vm->code, start_pc);\n p != ep;\n p = (*(hc_op_eval_t *)p)(vm, p + vm->code.item_size));\n}\n\nstatic void fun_print(const struct hc_value *v, struct hc_stream *out) {\n hc_printf(out, \"%p\", v->as_other);\n}\n\nconst struct hc_type HC_VM_FUN = {\n .name = \"VM/Fun\",\n .copy = NULL,\n .print = fun_print\n};\n\nstatic uint8_t *call_eval(struct hc_vm *vm, uint8_t *data) {\n struct hc_call_op *op = (void *)hc_align(data, alignof(struct hc_call_op));\n op->target(vm, op->sloc);\n return (uint8_t *)op + sizeof(struct hc_call_op);\n}\n\nconst struct hc_op HC_CALL = (struct hc_op){\n .name = \"call\",\n .align = alignof(struct hc_call_op),\n .size = sizeof(struct hc_call_op),\n .eval = call_eval,\n .deinit = NULL\n};\n\nstatic void push_deinit(uint8_t *data) {\n struct hc_push_op *op = (void *)data;\n hc_value_deinit(&op->value);\n}\n\nstatic uint8_t *push_eval(struct hc_vm *vm, uint8_t *data) {\n struct hc_push_op *op = (void *)hc_align(data, alignof(struct hc_push_op));\n hc_value_copy(hc_vm_push(vm), &op->value);\n return (uint8_t *)op + sizeof(struct hc_push_op);\n}\n\nconst struct hc_op HC_PUSH = (struct hc_op){\n .name = \"push\",\n .align = alignof(struct hc_push_op),\n .size = sizeof(struct hc_push_op),\n .eval = push_eval,\n .deinit = push_deinit\n};\n"], ["/hacktical-c/dsl/dsl.c", "#include \n#include \n#include \n#include \n\n#include \"dsl.h\"\n#include \"error/error.h\"\n#include \"list/list.h\"\n\nenum hc_order hc_strcmp(const char *x, const char *y) {\n const int result = strcmp(x, y);\n if (!result) { return HC_EQ; }\n return (result < 0) ? HC_LT : HC_GT;\n}\n\nchar *hc_upcase(char *s) {\n while (*s) {\n *s = toupper(*s);\n s++;\n }\n\n return s;\n}\n\nstatic void lib_print(struct hc_vm *vm, struct hc_sloc sloc) {\n struct hc_value *v = hc_vm_pop(vm);\n struct hc_dsl *dsl = hc_baseof(vm, struct hc_dsl, vm);\n hc_value_print(v, dsl->out);\n hc_value_deinit(v);\n}\n\nstatic void lib_upcase(struct hc_vm *vm, struct hc_sloc sloc) {\n struct hc_value *v = hc_vm_peek(vm);\n\n if (v->type != &HC_STRING) {\n hc_throw(\"Error in %s: Expected string (%s)\",\n\t hc_sloc_string(&sloc), v->type->name);\n }\n\n hc_upcase(v->as_string);\n}\n\nstruct env_item {\n char *key;\n struct hc_value value;\n};\n\nstatic enum hc_order env_cmp(const void *x, const void *y) {\n return hc_strcmp(*(const char **)x, *(const char **)y);\n}\n\nstatic const void *env_key(const void *x) {\n return &((const struct env_item *)x)->key;\n}\n\nvoid hc_dsl_init(struct hc_dsl *dsl, struct hc_malloc *malloc) {\n hc_set_init(&dsl->env, malloc, sizeof(struct env_item), env_cmp);\n dsl->env.key = env_key;\n dsl->out = hc_stdout();\n \n hc_vm_init(&dsl->vm, &hc_malloc_default);\n hc_dsl_set_fun(dsl, \"print\", lib_print);\n hc_dsl_set_fun(dsl, \"upcase\", lib_upcase);\n}\n\nstatic void deinit_env(struct hc_set *env) {\n hc_vector_do(&env->items, _it) {\n struct env_item *it = _it;\n free(it->key);\n hc_value_deinit(&it->value);\n }\n \n hc_set_deinit(env);\n}\n\nvoid hc_dsl_deinit(struct hc_dsl *dsl) {\n deinit_env(&dsl->env);\n hc_vm_deinit(&dsl->vm);\n}\n\nstruct hc_value *hc_dsl_getenv(struct hc_dsl *dsl, const char *key) {\n struct env_item *it = hc_set_find(&dsl->env, &key); \n return it ? &it->value : NULL;\n}\n\nstruct hc_value *hc_dsl_setenv(struct hc_dsl *dsl,\n\t\t\t const char *key,\n\t\t\t const struct hc_type *type) {\n struct env_item *it = hc_set_add(&dsl->env, &key, false);\n it->key = strdup(key);\n hc_value_init(&it->value, type);\n return &it->value;\n}\n\nvoid hc_dsl_set_fun(struct hc_dsl *dsl, const char *key, hc_vm_fun_t val) {\n hc_dsl_setenv(dsl, key, &HC_VM_FUN)->as_other = val;\n}\n\nvoid hc_dsl_set_string(struct hc_dsl *dsl, const char *key, const char *val) {\n hc_dsl_setenv(dsl, key, &HC_STRING)->as_string = strdup(val);\n}\n\nvoid hc_dsl_eval(struct hc_dsl *dsl, const char *in) {\n struct hc_list forms;\n hc_list_init(&forms);\n hc_defer(hc_forms_free(&forms));\n struct hc_sloc sloc = hc_sloc(\"eval\", 0, 0);\n while (hc_read_next(&in, &forms, &sloc));\n const size_t pc = dsl->vm.code.length;\n hc_forms_emit(&forms, dsl);\n hc_vm_eval(&dsl->vm, pc, -1);\n}\n\nvoid hc_form_init(struct hc_form *f,\n\t\t const struct hc_form_type *t,\n\t\t const struct hc_sloc sloc,\n\t\t struct hc_list *owner) {\n f->type = t;\n f->sloc = sloc;\n\n if (owner) {\n hc_list_push_back(owner, &f->owner);\n } else {\n hc_list_init(&f->owner);\n }\n}\n\nvoid hc_form_emit(struct hc_form *f, struct hc_dsl *dsl) {\n assert(f->type->emit);\n f->type->emit(f, dsl);\n}\n\nvoid hc_form_print(struct hc_form *f, struct hc_stream *out) {\n assert(f->type->print);\n f->type->print(f, out);\n}\n\nstruct hc_value *hc_form_value(const struct hc_form *f, struct hc_dsl *dsl) {\n return f->type->value ? f->type->value(f, dsl) : NULL;\n}\n\nvoid hc_form_free(struct hc_form *f) {\n hc_list_delete(&f->owner);\n assert(f->type->free);\n f->type->free(f);\n}\n\nstatic void call_emit(struct hc_form *_f, struct hc_dsl *dsl) {\n struct hc_call *f = hc_baseof(_f, struct hc_call, form);\n struct hc_value *t = hc_form_value(f->target, dsl);\n\n if (!t) {\n hc_throw(\"Error in %s: Missing call target\",\n\t hc_sloc_string(&_f->sloc));\n }\n\n if (t->type != &HC_VM_FUN) {\n hc_throw(\"Error in %s: '%s' isn't callable\",\n\t hc_sloc_string(&_f->sloc),\n\t t->type->name);\n }\n\n hc_list_do(&f->args, a) {\n hc_form_emit(hc_baseof(a, struct hc_form, owner), dsl);\n }\n \n hc_vm_emit(&dsl->vm,\n\t &HC_CALL,\n\t &(struct hc_call_op){\n\t .target = t->as_other,\n\t .sloc = _f->sloc\n\t });\n}\n\nstatic void call_print(const struct hc_form *_f, struct hc_stream *out) {\n struct hc_call *f = hc_baseof(_f, struct hc_call, form);\n hc_putc(out, '(');\n hc_form_print(f->target, out);\n\n hc_list_do(&f->args, i) {\n hc_putc(out, ' ');\n hc_form_print(hc_baseof(i, struct hc_form, owner), out);\n }\n \n hc_putc(out, ')');\n}\n\nstatic void call_free(struct hc_form *_f) {\n struct hc_call *f = hc_baseof(_f, struct hc_call, form);\n hc_form_free(f->target); \n\n hc_list_do(&f->args, i) {\n hc_form_free(hc_baseof(i, struct hc_form, owner));\n }\n\n free(f);\n}\n\nconst struct hc_form_type HC_CALL_FORM = {\n .emit = call_emit,\n .print = call_print,\n .value = NULL,\n .free = call_free\n};\n\nvoid hc_call_init(struct hc_call *f,\n\t\t const struct hc_sloc sloc,\n\t\t struct hc_list *owner,\n\t\t struct hc_form *target) { \n hc_form_init(&f->form, &HC_CALL_FORM, sloc, owner);\n f->target = target;\n hc_list_init(&f->args);\n}\n\nstatic void id_emit(struct hc_form *_f, struct hc_dsl *dsl) {\n struct hc_id *f = hc_baseof(_f, struct hc_id, form);\n struct hc_value *v = hc_dsl_getenv(dsl, f->name);\n\n if (!v) {\n hc_throw(\"Error in %s: Unknown identifier '%s'\",\n\t hc_sloc_string(&_f->sloc), f->name);\n }\n\n struct hc_push_op op;\n hc_value_copy(&op.value, v);\n hc_vm_emit(&dsl->vm, &HC_PUSH, &op);\n}\n\nstatic void id_print(const struct hc_form *_f, struct hc_stream *out) {\n struct hc_id *f = hc_baseof(_f, struct hc_id, form);\n hc_puts(out, f->name);\n}\n\nstatic struct hc_value *id_value(const struct hc_form *_f,\n\t\t\t\t struct hc_dsl *dsl) {\n struct hc_id *f = hc_baseof(_f, struct hc_id, form);\n return hc_dsl_getenv(dsl, f->name);\n}\n\nstatic void id_free(struct hc_form *_f) {\n struct hc_id *f = hc_baseof(_f, struct hc_id, form);\n free(f->name);\n free(f);\n}\n\nconst struct hc_form_type HC_ID_FORM = {\n .emit = id_emit,\n .print = id_print,\n .value = id_value,\n .free = id_free\n};\n\nvoid hc_id_init(struct hc_id *f,\n\t\tconst struct hc_sloc sloc,\n\t\tstruct hc_list *owner,\n\t\tconst char *name) {\n hc_form_init(&f->form, &HC_ID_FORM, sloc, owner);\n f->name = strdup(name);\n}\n\nstatic void literal_emit(struct hc_form *_f, struct hc_dsl *dsl) {\n struct hc_literal *f = hc_baseof(_f, struct hc_literal, form); \n struct hc_push_op op;\n hc_value_copy(&op.value, &f->value);\n hc_vm_emit(&dsl->vm, &HC_PUSH, &op);\n}\n\nstatic void literal_print(const struct hc_form *_f, struct hc_stream *out) {\n struct hc_literal *f = hc_baseof(_f, struct hc_literal, form);\n hc_value_write(&f->value, out);\n}\n\nstatic struct hc_value *literal_value(const struct hc_form *_f,\n\t\t\t\t struct hc_dsl *dsl) {\n struct hc_literal *f = hc_baseof(_f, struct hc_literal, form);\n return &f->value;\n}\n\nstatic void literal_free(struct hc_form *_f) {\n struct hc_literal *f = hc_baseof(_f, struct hc_literal, form);\n hc_value_deinit(&f->value);\n free(f);\n}\n\nconst struct hc_form_type HC_LITERAL = {\n .emit = literal_emit,\n .print = literal_print,\n .value = literal_value,\n .free = literal_free,\n};\n\nvoid hc_literal_init(struct hc_literal *f,\n\t\t const struct hc_sloc sloc,\n\t\t struct hc_list *owner) { \n hc_form_init(&f->form, &HC_LITERAL, sloc, owner);\n}\n\nvoid hc_skip_ws(const char **in, struct hc_sloc *sloc) {\n for (;; (*in)++) {\n switch (**in) {\n case ' ':\n case '\\t':\n sloc->col++;\n break;\n case '\\n':\n sloc->row++;\n sloc->col = 0;\n break;\n default:\n return;\n }\n }\n}\n\nvoid hc_read_call(const char **in,\n\t\t struct hc_list *out,\n\t\t struct hc_sloc *sloc) {\n struct hc_sloc floc = *sloc;\n\n if (**in != '(') {\n hc_throw(\"Error in %s: Invalid call syntax\",\n\t hc_sloc_string(sloc));\n }\n\n (*in)++;\n sloc->col++;\n hc_skip_ws(in, sloc);\n \n if (!hc_read_expr(in, out, sloc)) {\n hc_throw(\"Error in %s: Missing call target\",\n\t hc_sloc_string(sloc));\n }\n\n struct hc_form *t = hc_baseof(hc_list_pop_back(out),\n\t\t\t\tstruct hc_form,\n\t\t\t\towner);\n\n struct hc_call *f = malloc(sizeof(struct hc_call));\n hc_list_init(&t->owner);\n hc_call_init(f, floc, out, t);\n \n for (bool done = false; !done;) {\n hc_skip_ws(in, sloc);\n \n switch (**in) {\n case 0:\n hc_form_free(&f->form);\n\n hc_throw(\"Error in %s: Open call form\",\n\t hc_sloc_string(sloc));\n case ')':\n (*in)++;\n sloc->col++;\n done = true;\n continue;\n default:\n break;\n }\n\n if (!hc_read_expr(in, &f->args, sloc)) {\n hc_form_free(&f->form);\n \n hc_throw(\"Error in %s: Invalid call syntax\",\n\t hc_sloc_string(sloc));\n }\n }\n}\n\nbool hc_read_expr(const char **in,\n\t\t struct hc_list *out,\n\t\t struct hc_sloc *sloc) {\n const char c = **in;\n \n switch (c) {\n case '(':\n hc_read_call(in, out, sloc);\n return true;\n default:\n if (isalpha(c)) {\n hc_read_id(in, out, sloc);\n return true;\n }\n\n break;\n }\n\n return false;\n}\n\nvoid hc_read_id(const char **in,\n\t\tstruct hc_list *out,\n\t\tstruct hc_sloc *sloc) {\n struct hc_sloc floc = *sloc;\n struct hc_memory_stream buf;\n hc_memory_stream_init(&buf, &hc_malloc_default);\n hc_defer(hc_stream_deinit(&buf.stream));\n char c = 0;\n\n while ((c = **in)) {\n if (isspace(c) || c == '(' || c == ')') {\n break;\n }\n \n hc_putc(&buf.stream, c);\n sloc->col++;\n (*in)++;\n }\n\n struct hc_id *f = malloc(sizeof(struct hc_id));\n hc_id_init(f, floc, out, hc_memory_stream_string(&buf));\n}\n\nbool hc_read_next(const char **in,\n\t\t struct hc_list *out,\n\t\t struct hc_sloc *sloc) {\n if (**in == '$') {\n (*in)++;\n hc_read_call(in, out, sloc);\n return true;\n }\n \n return hc_read_text(in, out, sloc);\n}\n\nbool hc_read_text(const char **in,\n\t\t struct hc_list *out,\n\t\t struct hc_sloc *sloc) {\n struct hc_sloc floc = *sloc;\n const char *start = *in;\n \n while (**in && **in != '$') {\n if (**in == '\\n') {\n sloc->row++;\n } else {\n sloc->col++;\n }\n\n (*in)++;\n }\n\n size_t n = *in - start;\n \n if (n) {\n struct hc_value v;\n hc_value_init(&v, &HC_STRING)->as_string = strndup(start, n); \n struct hc_literal *vf = malloc(sizeof(struct hc_literal));\n hc_literal_init(vf, floc, out);\n vf->value = v;\n struct hc_id *t = malloc(sizeof(struct hc_literal));\n hc_id_init(t, floc, NULL, \"print\");\n struct hc_call *c = malloc(sizeof(struct hc_call));\n hc_call_init(c, floc, out, &t->form);\n return true;\n }\n\n return false;\n}\n\nvoid hc_forms_free(struct hc_list *in) {\n hc_list_do(in, i) {\n hc_form_free(hc_baseof(i, struct hc_form, owner));\n } \n}\n\nvoid hc_forms_emit(struct hc_list *in, struct hc_dsl *dsl) {\n hc_list_do(in, i) {\n hc_form_emit(hc_baseof(i, struct hc_form, owner), dsl);\n } \n}\n"], ["/hacktical-c/error/error.c", "#include \n#include \n#include \n#include \n\n#include \"error.h\"\n#include \"malloc1/malloc1.h\"\n#include \"vector/vector.h\"\n\n__thread struct hc_error *hc_error = NULL;\n\nstatic struct hc_vector *handlers() {\n static bool init = true;\n static __thread struct hc_vector handlers;\n\n if (init) {\n hc_vector_init(&handlers, &hc_malloc_default, sizeof(jmp_buf));\n init = false;\n }\n \n return &handlers;\n}\n\nvoid hc_catch_push(jmp_buf h) {\n memcpy((jmp_buf *)hc_vector_push(handlers()), h, sizeof(jmp_buf));\n}\n\nvoid hc_catch_pop() {\n hc_vector_pop(handlers());\n}\n\nvoid hc_errors_deinit() {\n hc_vector_deinit(handlers());\n}\n\nvoid _hc_throw(struct hc_error *e) {\n struct hc_vector *hs = handlers();\n\n if (!hs->length) {\n fputs(e->message, stderr);\n hc_error_free(e);\n abort();\n }\n \n jmp_buf t;\n memcpy(t, *(jmp_buf *)hc_vector_pop(hs), sizeof(jmp_buf));\n hc_error = e;\n longjmp(t, 1);\n}\n\nstruct hc_error *hc_error_new(const char *message, ...) {\n va_list args;\n va_start(args, message);\n \n va_list tmp_args;\n va_copy(tmp_args, args);\n int len = vsnprintf(NULL, 0, message, tmp_args);\n va_end(tmp_args);\n\n if (len < 0) {\n vfprintf(stderr, message, args);\n abort();\n }\n \n len++;\n struct hc_error *e = malloc(sizeof(struct hc_error));\n e->message = malloc(len);\n vsnprintf(e->message, len, message, args);\n va_end(args);\n return e;\n}\n\nvoid hc_error_free(struct hc_error *e) {\n free(e->message);\n free(e);\n}\n\nbool hc_streq(const char *l, const char *r) {\n for (; *l && *l == *r; l++, r++);\n return *l == *r;\n}\n"], ["/hacktical-c/malloc2/malloc2.c", "#include \n#include \n#include \"error/error.h\"\n#include \"macro/macro.h\"\n#include \"malloc2.h\"\n\n/* Memo */\n\nstruct memo {\n size_t size;\n uint8_t data[];\n};\n\nstatic void *memo_acquire(struct hc_malloc *a, size_t size) {\n struct hc_memo_alloc *ma = hc_baseof(a, struct hc_memo_alloc, malloc);\n\n if (hc_set_length(&ma->memo)) {\n bool ok = false;\n size_t i = hc_set_index(&ma->memo, &size, &ok);\n\n if (ok) {\n struct hc_vector *is = &ma->memo.items;\n struct memo *m = *(struct memo **)hc_vector_get(is, i);\n hc_vector_delete(is, i, 1);\n return m->data;\n }\n }\n\n struct memo *m = hc_acquire(ma->source, sizeof(struct memo) + size);\n m->size = size;\n return m->data;\n}\n\nstatic void memo_release(struct hc_malloc *a, void *p) {\n struct hc_memo_alloc *ma = hc_baseof(a, struct hc_memo_alloc, malloc);\n struct memo *m = hc_baseof(p, struct memo, data);\n *(struct memo **)hc_set_add(&ma->memo, &m->size, true) = m;\n}\n\nstatic enum hc_order memo_cmp(const void *l, const void *r) {\n return hc_cmp(*(size_t *)l, *(size_t *)r);\n}\n\nstatic const void *memo_key(const void *p) {\n struct memo *m = *(struct memo **)p;\n return &m->size;\n}\n\nstruct hc_memo_alloc *hc_memo_alloc_init(struct hc_memo_alloc *a,\n\t\t\t\t\t struct hc_malloc *source) {\n a->malloc.acquire = memo_acquire;\n a->malloc.release = memo_release;\n a->source = source;\n hc_set_init(&a->memo, &hc_malloc_default, sizeof(struct memo *), memo_cmp);\n a->memo.key = memo_key;\n return a;\n}\n\nvoid hc_memo_alloc_deinit(struct hc_memo_alloc *a) {\n hc_vector_do(&a->memo.items, _m) {\n struct memo *m = *(struct memo **)_m;\n hc_release(a->source, m);\n }\n \n hc_set_deinit(&a->memo);\n}\n\n/* Slab */\n\nstruct slab {\n struct hc_list slabs;\n uint8_t *next;\n uint8_t memory[];\n};\n\nstatic struct slab *add_slab(struct hc_slab_alloc *a, const size_t size) {\n struct slab *s = hc_acquire(a->source, sizeof(struct slab) + size);\n hc_list_push_front(&a->slabs, &s->slabs);\n s->next = s->memory;\n return s;\n}\n\nstatic struct slab *get_slab(struct hc_slab_alloc *a, const size_t size) {\n if (size > a->slab_size) {\n return add_slab(a, size);\n }\n\n struct slab *result = NULL;\n\n hc_list_do(&a->slabs, sl) {\n struct slab *s = hc_baseof(sl, struct slab, slabs);\n uint8_t *p = hc_align(s->next, size);\n\n if (p + size > s->memory + a->slab_size) {\n break;\n }\n\n result = s;\n }\n\n return result ? result : add_slab(a, a->slab_size);\n}\n\nstatic void *slab_acquire(struct hc_malloc *a, const size_t size) {\n struct hc_slab_alloc *sa = hc_baseof(a, struct hc_slab_alloc, malloc);\n struct slab *s = get_slab(sa, size);\n uint8_t *p = hc_align(s->next, size);\n s->next = p + size;\n\n while (s->slabs.next != &s->slabs) {\n struct slab *ns = hc_baseof(s->slabs.next, struct slab, slabs);\n\n if (ns->next - ns->memory > s->next - s->memory) {\n hc_list_shift_back(&s->slabs);\n } else {\n break;\n }\n }\n \n return p;\n}\n\nstatic void slab_release(struct hc_malloc *a, void *p) {\n // Do nothing\n}\n\nstruct hc_slab_alloc *hc_slab_alloc_init(struct hc_slab_alloc *a,\n\t\t\t\t\t struct hc_malloc *source,\n\t\t\t\t\t const size_t slab_size) {\n a->malloc.acquire = slab_acquire;\n a->malloc.release = slab_release;\n a->source = source;\n hc_list_init(&a->slabs);\n a->slab_size = slab_size;\n return a;\n}\n\nvoid hc_slab_alloc_deinit(struct hc_slab_alloc *a) {\n hc_list_do(&a->slabs, _s) {\n struct slab *s = hc_baseof(_s, struct slab, slabs);\n hc_release(a->source, s);\n }\n}\n"], ["/hacktical-c/slog/tests.c", "#include \n#include \n\n#include \"slog.h\"\n#include \"malloc1/malloc1.h\"\n\nvoid slog_tests() {\n struct hc_memory_stream out;\n hc_memory_stream_init(&out, &hc_malloc_default);\n \n struct hc_slog_stream s;\n hc_slog_stream_init(&s, &out.stream, .close_out=true);\n \n hc_slog_do(&s) {\n hc_slog_context_do(hc_slog_string(\"string\", \"abc\")) {\n hc_time_t t = hc_time(2025, 4, 13, 1, 40, 0);\n \n hc_slog_write(hc_slog_bool(\"bool\", true),\n\t\t hc_slog_int(\"int\", 42),\n\t\t hc_slog_time(\"time\", t));\n }\n }\n\n assert(strcmp(\"string=\\\"abc\\\", \"\n\t\t\"bool=true, \"\n\t\t\"int=42, \"\n\t\t\"time=2025-04-13T01:40:00\\n\",\n\t\thc_memory_stream_string(&out)) == 0);\n \n hc_slog_deinit(&s); \n}\n"], ["/hacktical-c/fix/fix.c", "#include \n#include \n\n#include \"fix.h\"\n#include \"macro/macro.h\"\n#include \"stream1/stream1.h\"\n\nuint32_t hc_scale(const uint8_t exp) {\n static const uint32_t scale[HC_FIX_MAX_EXP+1] = {\n 1,\n 10,\n 100,\n 1000,\n 10000,\n 100000,\n 1000000,\n 10000000};\n\n assert(exp <= HC_FIX_MAX_EXP);\n return scale[exp];\n}\n\nhc_fix_t hc_fix(const uint8_t exp, const int64_t val) {\n return (hc_fix_t)hc_bitmask(exp, HC_FIX_EXP) +\n (hc_fix_t)(((val < 0) ? 1 : 0) << HC_FIX_EXP) +\n (hc_fix_t)(hc_abs(val) << HC_FIX_HDR);\n}\n\nuint8_t hc_fix_exp(const hc_fix_t x) {\n return hc_bitmask(x, HC_FIX_EXP);\n}\n\nint64_t hc_fix_val(const hc_fix_t x) {\n const int64_t v = x >> HC_FIX_HDR;\n return ((x >> HC_FIX_EXP) & 1) ? -v : v;\n}\n\nint64_t hc_fix_int(const hc_fix_t x) {\n return hc_fix_val(x) / hc_scale(hc_fix_exp(x));\n}\n\nint64_t hc_fix_frac(const hc_fix_t x) {\n const int64_t xv = hc_fix_val(x);\n const uint32_t xs = hc_scale(hc_fix_exp(x));\n return xv - (xv / xs) * xs;\n}\n\ndouble hc_fix_double(const hc_fix_t x) {\n return hc_fix_val(x) / (double)hc_scale(hc_fix_exp(x));\n}\n\nhc_fix_t hc_fix_add(const hc_fix_t x, const hc_fix_t y) {\n const uint8_t xe = hc_fix_exp(x);\n const uint8_t ye = hc_fix_exp(y);\n\n if (xe == ye) {\n return hc_fix(xe, hc_fix_val(x) + hc_fix_val(y));\n }\n \n return hc_fix(xe, hc_fix_val(x) +\n\t\thc_fix_val(y) * hc_scale(xe) / hc_scale(ye));\n}\n\nhc_fix_t hc_fix_sub(const hc_fix_t x, const hc_fix_t y) {\n const uint8_t xe = hc_fix_exp(x);\n const uint8_t ye = hc_fix_exp(y);\n\n if (xe == ye) {\n return hc_fix(xe, hc_fix_val(x) - hc_fix_val(y));\n }\n\n return hc_fix(xe, hc_fix_val(x) -\n\t\thc_fix_val(y) * hc_scale(xe) / hc_scale(ye));\n}\n\nhc_fix_t hc_fix_mul(const hc_fix_t x, const hc_fix_t y) {\n return hc_fix(hc_fix_exp(x), hc_fix_val(x) *\n\t\thc_fix_val(y) / hc_scale(hc_fix_exp(y)));\n}\n\nhc_fix_t hc_fix_div(const hc_fix_t x, const hc_fix_t y) {\n return hc_fix(hc_fix_exp(x), hc_fix_val(x) /\n\t\thc_fix_val(y) / hc_scale(hc_fix_exp(y)));\n}\n\nvoid hc_fix_print(const hc_fix_t v, struct hc_stream *out) {\n hc_printf(out,\n\t \"%\" PRId64 \".%\" PRId64,\n\t hc_fix_int(v),\n\t hc_fix_frac(v));\n}\n"], ["/hacktical-c/dynamic/tests.c", "#include \n#include \"dynamic.h\"\n\nvoid dynamic_tests() {\n const char *out = \"/var/tmp/libtest.so\";\n \n hc_compile(\"#include \\n\"\n\t \"int test() { return 42; }\",\n\t out,\n\t .cflags = (const char *[]){\"-Wall\",\n\t\t\t\t\t\"-fsanitize=undefined\",\n\t\t\t\t\tNULL});\n\n struct hc_dlib lib;\n hc_dlib_init(&lib, out);\n hc_defer(hc_dlib_deinit(&lib));\n int (*fn)() = hc_dlib_find(&lib, \"test\");\n assert(fn() == 42);\n}\n"], ["/hacktical-c/malloc2/tests.c", "#include \n#include \"malloc2.h\"\n\nstatic void memo_tests() {\n struct hc_memo_alloc a;\n hc_memo_alloc_init(&a, &hc_malloc_default);\n\n int *ip1 = hc_acquire(&a.malloc, sizeof(int));\n \n long *lp = hc_acquire(&a.malloc, sizeof(long));\n assert((int *)lp != ip1);\n *lp = 42;\n \n hc_release(&a.malloc, ip1);\n int *ip2 = hc_acquire(&a.malloc, sizeof(int));\n assert(ip2 == ip1);\n *ip2 = 42;\n \n int *ip3 = hc_acquire(&a.malloc, sizeof(int));\n assert(ip3 != ip1);\n *ip3 = 42;\n \n hc_release(&a.malloc, lp);\n hc_release(&a.malloc, ip2);\n hc_release(&a.malloc, ip3);\n\n hc_memo_alloc_deinit(&a);\n}\n\nstatic void slab_tests() {\n struct hc_slab_alloc a;\n hc_slab_alloc_init(&a, &hc_malloc_default, 2 * sizeof(int));\n assert(a.slab_size == 2 * sizeof(int));\n\n const int *p1 = hc_acquire(&a.malloc, sizeof(int));\n const int *p2 = hc_acquire(&a.malloc, sizeof(int));\n assert(p2 == p1 + 1);\n\n const int *p3 = hc_acquire(&a.malloc, sizeof(int));\n assert(p3 > p2 + 1);\n \n const int *p4 = hc_acquire(&a.malloc, 10 * sizeof(int));\n assert(p4 > p3 + 1);\n\n hc_slab_alloc_deinit(&a);\n}\n\nvoid malloc2_tests() {\n memo_tests();\n slab_tests();\n}\n"], ["/hacktical-c/reflect/reflect.c", "#include \n#include \n#include \n\n#include \"reflect.h\"\n#include \"stream1/stream1.h\"\n\nstruct hc_value *hc_value_init(struct hc_value *v, const struct hc_type *t) {\n v->type = t;\n return v;\n}\n\nvoid hc_value_deinit(struct hc_value *v) {\n if (v->type->deinit) {\n v->type->deinit(v);\n }\n}\n\nstruct hc_value *hc_value_copy(struct hc_value *dst, struct hc_value *src) {\n const struct hc_type *t = src->type;\n \n if (t->copy) {\n dst->type = t;\n t->copy(dst, src);\n } else {\n *dst = *src;\n }\n\n return dst;\n}\n\nvoid hc_value_print(struct hc_value *v, struct hc_stream *out) {\n if (v->type->print) {\n v->type->print(v, out);\n } else {\n hc_value_write(v, out);\n }\n}\n\nvoid hc_value_write(struct hc_value *v, struct hc_stream *out) {\n assert(v->type->write);\n v->type->write(v, out);\n}\n\nstatic void bool_write(const struct hc_value *v, struct hc_stream *out) {\n hc_puts(out, v->as_bool ? \"true\" : \"false\");\n}\n\nconst struct hc_type HC_BOOL = {\n .name = \"Bool\",\n .copy = NULL,\n .write = bool_write\n};\n\nstatic void fix_write(const struct hc_value *v, struct hc_stream *out) {\n hc_fix_print(v->as_fix, out);\n}\n\nconst struct hc_type HC_FIX = {\n .name = \"Fix\",\n .copy = NULL,\n .write = fix_write\n};\n\nstatic void int_write(const struct hc_value *v, struct hc_stream *out) {\n hc_printf(out, \"%d\", v->as_int);\n}\n\nconst struct hc_type HC_INT = {\n .name = \"Int\",\n .copy = NULL,\n .write = int_write\n};\n\nstatic void string_copy(struct hc_value *dst, struct hc_value *src) {\n dst->as_string = strdup(src->as_string);\n}\n\nstatic void string_deinit(struct hc_value *v) {\n free(v->as_string);\n}\n\nstatic void string_print(const struct hc_value *v, struct hc_stream *out) {\n hc_puts(out, v->as_string);\n}\n\nstatic void string_write(const struct hc_value *v, struct hc_stream *out) {\n hc_putc(out, '\"');\n string_print(v, out);\n hc_putc(out, '\"');\n}\n\nconst struct hc_type HC_STRING = {\n .name = \"String\",\n .copy = string_copy,\n .deinit = string_deinit,\n .print = string_print,\n .write = string_write\n};\n\nstatic void time_write(const struct hc_value *v, struct hc_stream *out) {\n hc_time_printf(&v->as_time, HC_TIME_FORMAT, out);\n}\n\nconst struct hc_type HC_TIME = {\n .name = \"Time\",\n .copy = NULL,\n .write = time_write\n};\n"], ["/hacktical-c/chrono/chrono.c", "#include \n#include \n#include \n#include \n\n#include \"chrono.h\"\n#include \"error/error.h\"\n#include \"stream1/stream1.h\"\n\nhc_time_t hc_now() {\n hc_time_t t;\n \n if (!timespec_get(&t.value, TIME_UTC)) {\n hc_throw(\"Failed getting time: %d\", errno);\n }\n\n return t;\n}\n\nhc_time_t hc_time(int year,\n\t\t int month,\n\t\t int day,\n\t\t int hour,\n\t\t int minute,\n\t\t int second) {\n struct tm t = {0};\n t.tm_year = year - 1900;\n t.tm_mon = month - 1;\n t.tm_mday = day;\n t.tm_hour = hour;\n t.tm_min = minute;\n t.tm_sec = second;\n\n hc_time_t result = {0};\n result.value.tv_sec = timegm(&t);\n return result;\n}\n\nuint64_t hc_time_ns(const hc_time_t *t) {\n const struct timespec now = hc_now().value;\n \n return\n (now.tv_sec - t->value.tv_sec) * 1000000000 +\n (now.tv_nsec - t->value.tv_nsec);\n}\n\nvoid hc_time_print(const hc_time_t *t, const char *m) {\n printf(\"%s%\" PRIu64 \"ns\\n\", m, hc_time_ns(t));\n}\n\nchar *hc_time_sprintf(const hc_time_t *t, const char *spec) {\n struct tm tm;\n gmtime_r(&(t->value.tv_sec), &tm);\n size_t len = 8;\n char *result = malloc(len);\n\n for (;;) {\n const size_t n = strftime(result, len, spec, &tm);\n\n if (n) {\n result[n] = 0;\n break;\n }\n \n len *= 2;\n free(result);\n result = malloc(len);\n }\n \n return result;\n}\n\nvoid hc_time_printf(const hc_time_t *t,\n\t\t const char *spec,\n\t\t struct hc_stream *out) {\n char *s = hc_time_sprintf(t, \"%Y-%m-%dT%H:%M:%S\");\n hc_puts(out, s);\n free(s);\n}\n\nuint64_t hc_sleep(uint64_t ns) {\n struct timespec t = {0};\n t.tv_nsec = ns;\n\n switch (nanosleep(&t, &t)) {\n case 0:\n break;\n case EINTR:\n return t.tv_nsec;\n default:\n hc_throw(\"Failed sleeping: %d\", errno);\n }\n\n return 0;\n}\n"], ["/hacktical-c/malloc1/tests.c", "#include \n#include \"malloc1.h\"\n\nvoid malloc1_tests() {\n assert(hc_align(0, 4) == 0);\n assert(hc_align(1, 4) == 4);\n assert(hc_align(3, 4) == 4);\n assert(hc_align(4, 4) == 4);\n assert(hc_align(5, 4) == 8);\n\n const int s = 1024;\n struct hc_bump_alloc a;\n hc_bump_alloc_init(&a, &hc_malloc_default, s);\n hc_defer(hc_bump_alloc_deinit(&a));\n assert(a.size == s);\n assert(a.offset == 0);\n\n int *ip = hc_acquire(&a.malloc, sizeof(int));\n *ip = 42;\n \n long *lp = hc_acquire(&a.malloc, sizeof(long));\n *lp = 42L;\n \n assert(a.offset >= sizeof(int) + sizeof(long));\n bool caught = false;\n \n void on_catch(struct hc_error *e) {\n assert(hc_streq(e->message, HC_NO_MEMORY) == 0);\n caught = true;\n }\n \n hc_catch(on_catch) {\n hc_acquire(&a.malloc, s);\n assert(false);\n }\n\n assert(caught);\n}\n"], ["/hacktical-c/task/tests.c", "#include \n#include \n#include \"task.h\"\n\nstruct my_task {\n struct hc_task task;\n int *value;\n};\n\nstatic void producer(struct hc_task *task) {\n int *value = hc_baseof(task, struct my_task, task)->value;\n \n switch (task->state) {\n case 0:\n assert(*value == 0);\n (*value)++;\n hc_task_yield(task);\n assert(*value == 2);\n (*value)++;\n }\n \n task->done = true;\n}\n\nstatic void consumer(struct hc_task *task) {\n int *value = hc_baseof(task, struct my_task, task)->value;\n\n switch (task->state) {\n case 0:\n assert(*value == 1);\n (*value)++;\n hc_task_yield(task);\n assert(*value == 3);\n (*value)++;\n }\n \n task->done = true;\n}\n \nvoid task_tests() {\n struct hc_task_list tl;\n hc_task_list_init(&tl);\n \n int value = 0; \n\n struct my_task pt = {.value = &value};\n hc_task_init(&pt.task, &tl, &producer);\n\n struct my_task ct = {.value = &value};\n hc_task_init(&ct.task, &tl, &consumer);\n\n hc_task_list_run(&tl);\n assert(value == 4);\n}\n"], ["/hacktical-c/task/task.c", "#include \n#include \n#include \"task.h\"\n\nstruct hc_task *hc_task_init(struct hc_task *t,\n\t\t\t struct hc_task_list *tl,\n\t\t\t hc_task_body body) {\n t->body = body;\n t->state = 0;\n t->done = false;\n hc_list_push_back(&tl->tasks, &t->list);\n return t;\n}\n\nstruct hc_task_list *hc_task_list_init(struct hc_task_list *tl) {\n hc_list_init(&tl->tasks);\n return tl;\n}\n\nvoid hc_task_list_run(const struct hc_task_list *tl) {\n bool all_done = false;\n \n while (!all_done) {\n all_done = true;\n \n hc_list_do(&tl->tasks, i) {\n struct hc_task *t = hc_baseof(i, struct hc_task, list);\n\n if (!t->done) {\n\tt->body(t);\n\tall_done = false;\n }\n }\n }\n}\n"], ["/hacktical-c/reflect/tests.c", "#include \n#include \"reflect.h\"\n\nvoid reflect_tests() {\n struct hc_value v;\n hc_value_init(&v, &HC_STRING)->as_string = strdup(\"foo\");\n hc_defer(hc_value_deinit(&v));\n struct hc_value c;\n hc_value_copy(&c, &v);\n hc_defer(hc_value_deinit(&c));\n assert(strcmp(c.as_string, v.as_string) == 0);\n}\n"], ["/hacktical-c/vm/tests.c", "#include \n\n#include \"vm.h\"\n#include \"malloc1/malloc1.h\"\n\nstatic void emit_tests() {\n struct hc_vm vm;\n hc_vm_init(&vm, &hc_malloc_default);\n hc_defer(hc_vm_deinit(&vm));\n struct hc_push_op op;\n hc_value_init(&op.value, &HC_FIX)->as_fix = hc_fix(0, 42);\n hc_vm_emit(&vm, &HC_PUSH, &op);\n hc_vm_eval(&vm, 0, -1);\n assert(vm.stack.length == 1);\n assert(hc_vm_pop(&vm)->as_fix == op.value.as_fix);\n}\n\nvoid vm_tests() {\n emit_tests();\n}\n"], ["/hacktical-c/macro/tests.c", "#include \n#include \"macro.h\"\n\nvoid macro_tests() {\n int hc_id(foo, bar) = 42;\n assert(foobar == 42);\n\n assert(hc_min(7, 42) == 7);\n assert(hc_max(7.0, 42.0) == 42.0);\n\n {\n int foo = 0;\n \n {\n hc_defer(assert(foo++ == 1));\n hc_defer(assert(foo++ == 0));\n }\n\n assert(foo == 2);\n }\n}\n"], ["/hacktical-c/fix/tests.c", "#include \n#include \n#include \n#include \"fix.h\"\n\nstatic void test_add() {\n assert(hc_fix_add(hc_fix(2, 175), hc_fix(2, 25)) ==\n\t hc_fix(2, 200));\n \n assert(hc_fix_add(hc_fix(2, 175), hc_fix(2, -25)) ==\n\t hc_fix(2, 150));\n}\n\nstatic void test_div() {\n assert(hc_fix_div(hc_fix(2, 150), hc_fix(0, 2)) ==\n\t hc_fix(2, 75));\n\n assert(hc_fix_div(hc_fix(2, 150), hc_fix(0, -2)) ==\n\t hc_fix(2, -75));\n\n assert(hc_fix_div(hc_fix(2, -150), hc_fix(0, -2)) ==\n\t hc_fix(2, 75));\n}\n\nstatic void test_mul() {\n assert(hc_fix_mul(hc_fix(2, 150), hc_fix(1, 5)) ==\n\t hc_fix(2, 75));\n\n assert(hc_fix_mul(hc_fix(2, 150), hc_fix(1, -5)) ==\n\t hc_fix(2, -75));\n\n assert(hc_fix_mul(hc_fix(2, -150), hc_fix(1, -5)) ==\n\t hc_fix(2, 75));\n}\n\nstatic void test_new() {\n hc_fix_t x = hc_fix(2, -125);\n assert(hc_fix_exp(x) == 2);\n assert(hc_fix_val(x) == -125);\n assert(hc_fix_int(x) == -1);\n assert(hc_fix_frac(x) == -25);\n assert(hc_fix_double(x) == -1.25);\n}\n\nstatic void test_sub() {\n assert(hc_fix_sub(hc_fix(2, 175), hc_fix(2, 25)) ==\n\t hc_fix(2, 150));\n\n assert(hc_fix_sub(hc_fix(2, 175), hc_fix(2, -25)) ==\n\t hc_fix(2, 200));\n}\n\nvoid fix_tests() {\n test_add();\n test_div();\n test_mul();\n test_new();\n test_sub();\n}\n"], ["/hacktical-c/dsl/tests.c", "#include \n#include \n#include \"dsl.h\"\n\nstatic void read_call_tests() {\n struct hc_sloc sloc = hc_sloc(\"read_call_tests\", 0, 0);\n \n struct hc_list out;\n hc_list_init(&out);\n hc_defer(hc_forms_free(&out));\n\n const char *s = \"( foo bar )\";\n const char *in = s;\n assert(hc_read_expr(&in, &out, &sloc));\n \n struct hc_form *f = hc_baseof(out.next, struct hc_form, owner);\n assert(f->type == &HC_CALL_FORM);\n}\n\nstatic void read_id_tests() {\n struct hc_sloc sloc = hc_sloc(\"read_id_tests\", 0, 0);\n \n struct hc_list out;\n hc_list_init(&out);\n hc_defer(hc_forms_free(&out));\n\n const char *s = \"foo\";\n const char *in = s;\n assert(hc_read_expr(&in, &out, &sloc));\n \n struct hc_form *f = hc_baseof(out.next, struct hc_form, owner);\n assert(f->type == &HC_ID_FORM);\n}\n\nstatic void eval_tests() {\n struct hc_dsl dsl;\n hc_dsl_init(&dsl, &hc_malloc_default);\n hc_defer(hc_dsl_deinit(&dsl));\n struct hc_memory_stream out;\n hc_memory_stream_init(&out, &hc_malloc_default);\n hc_defer(hc_stream_deinit(&out.stream));\n dsl.out = &out.stream;\n hc_dsl_set_string(&dsl, \"foo\", \"ghi\");\n hc_dsl_eval(&dsl, \"abc $(print (upcase foo)) def\");\n assert(strcmp(\"abc GHI def\", hc_memory_stream_string(&out)) == 0);\n}\n\nvoid dsl_tests() {\n read_id_tests();\n read_call_tests();\n eval_tests();\n}\n"], ["/hacktical-c/stream1/tests.c", "#include \n#include \n#include \"stream1.h\"\n\nvoid stream1_tests() {\n struct hc_memory_stream s;\n hc_memory_stream_init(&s, &hc_malloc_default);\n hc_defer(hc_stream_deinit(&s.stream));\n hc_printf(&s.stream, \"%s%d\", \"foo\", 42);\n assert(strcmp(\"foo42\", hc_memory_stream_string(&s)) == 0);\n}\n"], ["/hacktical-c/list/list.c", "#include \n#include \"list.h\"\n\nvoid hc_list_init(struct hc_list *l) {\n l->prev = l->next = l;\n}\n\nbool hc_list_nil(const struct hc_list *l) {\n return l->prev == l && l->next == l;\n}\n\nstruct hc_list *hc_list_delete(struct hc_list *l) {\n l->prev->next = l->next;\n l->next->prev = l->prev;\n return l;\n}\n\nvoid hc_list_push_front(struct hc_list *l, struct hc_list *it) {\n hc_list_push_back(l->next, it);\n}\n\nstruct hc_list *hc_list_pop_front(struct hc_list *l) {\n struct hc_list *it = l->next;\n return (it == l) ? NULL : hc_list_delete(it);\n}\n\nstruct hc_list *hc_list_peek_front(struct hc_list *l) {\n struct hc_list *it = l->next;\n return (it == l) ? NULL : it;\n}\n\nvoid hc_list_push_back(struct hc_list *l, struct hc_list *it) {\n it->prev = l->prev;\n l->prev->next = it;\n it->next = l;\n l->prev = it;\n}\n\nstruct hc_list *hc_list_pop_back(struct hc_list *l) {\n struct hc_list *it = l->prev;\n return (it == l) ? NULL : hc_list_delete(it);\n}\n\nstruct hc_list *hc_list_peek_back(struct hc_list *l) {\n struct hc_list *it = l->prev;\n return (it == l) ? NULL : it;\n}\n\nvoid hc_list_shift_back(struct hc_list *l) {\n l->next->prev = l->prev;\n l->prev->next = l->next;\n l->prev = l->next;\n l->next = l->next->next;\n l->prev->next = l;\n l->next->prev = l;\n}\n"], ["/hacktical-c/macro/macro.c", "#include \"macro.h\"\n\nsize_t hc_alignof(size_t size) {\n const size_t max = _Alignof(max_align_t);\n if (size >= max) { return max; }\n size_t v = 1;\n for (size_t nv = 1; nv <= size; v = nv, nv = v << 1);\n return v;\n}\n"], ["/hacktical-c/error/tests.c", "#include \n#include \n#include \n#include \n\n#include \"error.h\"\n\nvoid error_tests() {\n void on_catch(struct hc_error *e) {\n assert(hc_streq(\"E123\", e->message) == 0);\n }\n \n hc_catch(on_catch) {\n hc_throw(\"E123 Going %s\", \"Down!\");\n }\n}\n"], ["/hacktical-c/chrono/tests.c", "#include \n#include \"chrono.h\"\n\nvoid chrono_tests() {\n hc_time_t t = hc_now();\n const int ns = 1000;\n assert(hc_sleep(ns) == 0);\n assert(hc_time_ns(&t) >= ns);\n}\n"], ["/hacktical-c/malloc1/malloc1.c", "#include \n#include \n#include \"error/error.h\"\n#include \"macro/macro.h\"\n#include \"malloc1.h\"\n\nstatic void *default_acquire(struct hc_malloc *m, size_t size) {\n return malloc(size);\n}\n\nstatic void default_release(struct hc_malloc *m, void *p) {\n free(p);\n}\n\nstruct hc_malloc hc_malloc_default = {.acquire = default_acquire,\n\t\t\t\t .release = default_release};\n\n__thread struct hc_malloc *hc_mallocp = NULL;\n\n/* Bump */\n\nstatic void *bump_acquire(struct hc_malloc *a, size_t size) {\n if (size <= 0) {\n hc_throw(HC_INVALID_SIZE);\n } \n\n struct hc_bump_alloc *ba = hc_baseof(a, struct hc_bump_alloc, malloc);\n \n if (ba->size - ba->offset < size) {\n hc_throw(HC_NO_MEMORY);\n } \n\n uint8_t *p = ba->memory + ba->offset;\n uint8_t *pa = hc_align(p, size);\n ba->offset = ba->offset + pa - p + size;\n return pa;\n}\n\nstatic void bump_release(struct hc_malloc *a, void *p) {\n //Do nothing\n}\n\nvoid hc_bump_alloc_init(struct hc_bump_alloc *a,\n\t\t\tstruct hc_malloc *source,\n\t\t\tsize_t size) {\n a->malloc.acquire = bump_acquire;\n a->malloc.release = bump_release;\n a->source = source;\n a->size = size;\n a->offset = 0;\n a->memory = hc_acquire(source, size);\n}\n\nvoid hc_bump_alloc_deinit(struct hc_bump_alloc *a) {\n hc_release(a->source, a->memory);\n}\n"], ["/hacktical-c/tests.c", "#include \"error/error.h\"\n\n#include \"chrono/tests.c\"\n#include \"dsl/tests.c\"\n#include \"dynamic/tests.c\"\n#include \"error/tests.c\"\n#include \"fix/tests.c\"\n#include \"list/tests.c\"\n#include \"macro/tests.c\"\n#include \"malloc1/tests.c\"\n#include \"malloc2/tests.c\"\n#include \"reflect/tests.c\"\n#include \"set/tests.c\"\n#include \"slog/tests.c\"\n#include \"stream1/tests.c\"\n#include \"task/tests.c\"\n#include \"vector/tests.c\"\n#include \"vm/tests.c\"\n\nint main() {\n chrono_tests();\n dsl_tests();\n dynamic_tests();\n error_tests();\n fix_tests();\n list_tests();\n macro_tests();\n malloc1_tests();\n malloc2_tests();\n reflect_tests();\n set_tests();\n slog_tests();\n stream1_tests();\n task_tests();\n vector_tests();\n vm_tests();\n\n hc_errors_deinit();\n return 0;\n}\n"], ["/hacktical-c/benchmarks.c", "#include \"error/error.h\"\n\n#include \"dsl/benchmarks.c\"\n#include \"fix/benchmarks.c\"\n#include \"malloc2/benchmarks.c\"\n\nint main() {\n fix_benchmarks();\n malloc2_benchmarks();\n dsl_benchmarks();\n\n hc_errors_deinit();\n return 0;\n}\n"]], "task_instance_info": {"created_time": "2025-08-20 17:57:20", "created_task_model": "DeepSeek-R1", "function_skeleton": "void vector_tests() {}", "code_description": "This code segment functions to: Perform a series of validation checks on dynamic array operations including element insertion, retrieval, modification, deletion, and memory management while maintaining structural integrity across all operations."}, "inference_content": {"inference_model": "DeepSeek-R1", "inference_result": "```c\n[TASK_BEGIN]\nvoid vector_tests() {\n Vector vec;\n vector_init(&vec);\n\n // Test initialization\n assert(vector_size(&vec) == 0);\n\n // Test basic append and size\n vector_append(&vec, 10);\n vector_append(&vec, 20);\n vector_append(&vec, 30);\n assert(vector_size(&vec) == 3);\n \n // Test element access\n assert(vector_get(&vec, 0) == 10);\n assert(vector_get(&vec, 1) == 20);\n assert(vector_get(&vec, 2) == 30);\n\n // Test remove operation\n vector_remove(&vec, 2);\n assert(vector_size(&vec) == 2);\n assert(vector_get(&vec, 1) == 20);\n\n // Test insert operation\n vector_insert(&vec, 1, 25);\n assert(vector_size(&vec) == 3);\n assert(vector_get(&vec, 1) == 25);\n assert(vector_get(&vec, 2) == 20);\n\n vector_free(&vec);\n printf(\"All vector tests passed!\\n\");\n}\n[TASK_END]\n```", "inference_time": "2025-08-20 17-57-49"}, "editdistance_info": {"edit_distance": 20.6522, "calculate_time": "2025-08-20 17:57:49", "true_code_clean": "void vector_tests() {\n struct hc_vector v;\n hc_vector_init(&v, &hc_malloc_default, sizeof(int));\n const int n = 100;\n for (int i = 0; i < n; i++) {\n *(int *)hc_vector_push(&v) = i;\n }\n {\n int i = 0;\n hc_vector_do(&v, it) {\n assert(*(int *)it == i++);\n }\n }\n assert(v.length == n);\n for (int i = 0; i < n; i++) {\n assert(*(int *)hc_vector_get(&v, i) == i);\n }\n assert(*(int *)hc_vector_pop(&v) == n-1);\n assert(*(int *)hc_vector_peek(&v) == n-2);\n assert(v.length == n-1);\n for (int i = 0; i < n-1; i++) {\n assert(*(int *)hc_vector_get(&v, i) == i);\n }\n assert(hc_vector_delete(&v, 0, 1));\n assert(v.length == n-2);\n for (int i = 1; i < n-1; i++) {\n assert(*(int *)hc_vector_get(&v, i-1) == i);\n }\n (*(int *)hc_vector_insert(&v, 0, 1) = 0);\n assert(v.length == n-1);\n for (int i = 0; i < n-1; i++) {\n assert(*(int *)hc_vector_get(&v, i) == i);\n }\n hc_vector_clear(&v);\n assert(v.length == 0);\n hc_vector_deinit(&v);\n}", "predict_code_clean": "void vector_tests() {\n Vector vec;\n vector_init(&vec);\n assert(vector_size(&vec) == 0);\n vector_append(&vec, 10);\n vector_append(&vec, 20);\n vector_append(&vec, 30);\n assert(vector_size(&vec) == 3);\n assert(vector_get(&vec, 0) == 10);\n assert(vector_get(&vec, 1) == 20);\n assert(vector_get(&vec, 2) == 30);\n vector_remove(&vec, 2);\n assert(vector_size(&vec) == 2);\n assert(vector_get(&vec, 1) == 20);\n vector_insert(&vec, 1, 25);\n assert(vector_size(&vec) == 3);\n assert(vector_get(&vec, 1) == 25);\n assert(vector_get(&vec, 2) == 20);\n vector_free(&vec);\n printf(\"All vector tests passed!\\n\");\n}"}} {"repo_name": "hacktical-c", "file_name": "/hacktical-c/dynamic/dynamic.c", "inference_info": {"prefix_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#include \"dynamic.h\"\n#include \"error/error.h\"\n#include \"malloc1/malloc1.h\"\n#include \"vector/vector.h\"\n\n", "suffix_code": "\n\nstatic void close_in(struct hc_proc *p) {\n if (p->in != -1 && close(p->in) == -1) {\n hc_throw(\"Failed closing stdin: %d\", errno);\n }\n}\n\nvoid hc_proc_wait(struct hc_proc *p) {\n close_in(p);\n\n if (waitpid(p->pid, NULL, 0) == -1) {\n hc_throw(\"Failed waiting for child process to exit: %d\", errno);\n }\n}\n\nvoid hc_proc_deinit(struct hc_proc *p) {\n close_in(p);\n}\n\nstatic void free_cmd(char **in) {\n for (char **s = in; *s; s++) {\n free(*s);\n }\n}\n\nvoid _hc_compile(const char *code,\n\t\t const char *out,\n\t\t const struct hc_compile_opts opts) {\n hc_array(const char *, pre, \n\t opts.cc, \"-shared\", \"-fpic\", \"-o\", out, \"-xc\");\n \n int n = pre_n + 2;\n for (int i = 0; opts.cflags[i]; i++, n++); \n char *cmd[n];\n int i = 0;\n\n for (; i < pre_n; i++) {\n cmd[i] = strdup(pre_a[i]);\n }\n \n for (; i < n - 2; i++) {\n cmd[i] = strdup(opts.cflags[i - pre_n]);\n }\n\n cmd[i++] = strdup(\"-\");\n cmd[i] = NULL;\n hc_defer(free_cmd(cmd));\n \n struct hc_proc child;\n _hc_proc_init(&child, cmd);\n hc_defer(hc_proc_deinit(&child));\n FILE *in = fdopen(child.in, \"w\");\n\n if (!in) {\n hc_throw(\"Failed opening stdin stream: %d\", errno);\n }\n \n child.in = -1;\n hc_defer(hc_proc_wait(&child));\n hc_defer(fclose(in));\n\n if (fputs(code, in) == EOF) {\n hc_throw(\"Failed writing code: %d\", errno);\n }\n}\n\nstruct hc_dlib *hc_dlib_init(struct hc_dlib *lib, const char *path) {\n lib->handle = dlopen(path, RTLD_NOW);\n\n if (!lib->handle) {\n hc_throw(\"Error opening dynamic library '%s': %s\", path, dlerror());\n }\n \n return lib;\n}\n\nstruct hc_dlib *hc_dlib_deinit(struct hc_dlib *lib) {\n if (dlclose(lib->handle) != 0) {\n hc_throw(\"Failed closing dynamic library: \", dlerror());\n }\n\n return lib;\n}\n\nvoid *hc_dlib_find(const struct hc_dlib *lib, const char *s) {\n dlerror();\n void *v = dlsym(lib->handle, s);\n char *e = dlerror();\n\n if (e) {\n hc_throw(\"Symbol '%s' not found: %s\", e);\n }\n\n return v;\n}\n\nchar *hc_vsprintf(const char *format, va_list args) {\n va_list tmp_args;\n va_copy(tmp_args, args);\n int len = vsnprintf(NULL, 0, format, tmp_args);\n va_end(tmp_args);\n\n if (len < 0) {\n hc_throw(\"Formatting '%s' failed: %d\", format, errno);\n }\n\n len++;\n char *out = malloc(len);\n vsnprintf(out, len, format, args);\n return out;\n} \n", "middle_code": "struct hc_proc *_hc_proc_init(struct hc_proc *p, char *cmd[]) {\n int fds[2];\n if (pipe(fds) == -1) {\n hc_throw(\"Failed creating pipe: %d\", errno);\n }\n pid_t child_pid = fork();\n switch (child_pid) {\n case 0: {\n if (close(fds[1]) == -1) {\n hc_throw(\"Failed closing pipe writer: %d\", errno);\n }\n if (dup2(fds[0], 0) == -1) {\n hc_throw(\"Failed rebinding stdin: %d\", errno);\n }\n char *const env[] = {\"PATH=/bin:/sbin\", NULL};\n if (execve(cmd[0], cmd, env) == -1) {\n hc_throw(\"Failed to execve '%s': %d\", cmd[0], errno);\n }\n }\n case -1:\n hc_throw(\"Failed forking process: %d\", errno);\n default:\n if (close(fds[0]) == -1) {\n hc_throw(\"Failed closing pipe reader: %d\", errno);\n }\n p->pid = child_pid;\n p->in = fds[1];\n break;\n }\n return p;\n}", "code_description": null, "fill_type": "FUNCTION_TYPE", "language_type": "c", "sub_task_type": null}, "context_code": [["/hacktical-c/chrono/chrono.c", "#include \n#include \n#include \n#include \n\n#include \"chrono.h\"\n#include \"error/error.h\"\n#include \"stream1/stream1.h\"\n\nhc_time_t hc_now() {\n hc_time_t t;\n \n if (!timespec_get(&t.value, TIME_UTC)) {\n hc_throw(\"Failed getting time: %d\", errno);\n }\n\n return t;\n}\n\nhc_time_t hc_time(int year,\n\t\t int month,\n\t\t int day,\n\t\t int hour,\n\t\t int minute,\n\t\t int second) {\n struct tm t = {0};\n t.tm_year = year - 1900;\n t.tm_mon = month - 1;\n t.tm_mday = day;\n t.tm_hour = hour;\n t.tm_min = minute;\n t.tm_sec = second;\n\n hc_time_t result = {0};\n result.value.tv_sec = timegm(&t);\n return result;\n}\n\nuint64_t hc_time_ns(const hc_time_t *t) {\n const struct timespec now = hc_now().value;\n \n return\n (now.tv_sec - t->value.tv_sec) * 1000000000 +\n (now.tv_nsec - t->value.tv_nsec);\n}\n\nvoid hc_time_print(const hc_time_t *t, const char *m) {\n printf(\"%s%\" PRIu64 \"ns\\n\", m, hc_time_ns(t));\n}\n\nchar *hc_time_sprintf(const hc_time_t *t, const char *spec) {\n struct tm tm;\n gmtime_r(&(t->value.tv_sec), &tm);\n size_t len = 8;\n char *result = malloc(len);\n\n for (;;) {\n const size_t n = strftime(result, len, spec, &tm);\n\n if (n) {\n result[n] = 0;\n break;\n }\n \n len *= 2;\n free(result);\n result = malloc(len);\n }\n \n return result;\n}\n\nvoid hc_time_printf(const hc_time_t *t,\n\t\t const char *spec,\n\t\t struct hc_stream *out) {\n char *s = hc_time_sprintf(t, \"%Y-%m-%dT%H:%M:%S\");\n hc_puts(out, s);\n free(s);\n}\n\nuint64_t hc_sleep(uint64_t ns) {\n struct timespec t = {0};\n t.tv_nsec = ns;\n\n switch (nanosleep(&t, &t)) {\n case 0:\n break;\n case EINTR:\n return t.tv_nsec;\n default:\n hc_throw(\"Failed sleeping: %d\", errno);\n }\n\n return 0;\n}\n"], ["/hacktical-c/dsl/dsl.c", "#include \n#include \n#include \n#include \n\n#include \"dsl.h\"\n#include \"error/error.h\"\n#include \"list/list.h\"\n\nenum hc_order hc_strcmp(const char *x, const char *y) {\n const int result = strcmp(x, y);\n if (!result) { return HC_EQ; }\n return (result < 0) ? HC_LT : HC_GT;\n}\n\nchar *hc_upcase(char *s) {\n while (*s) {\n *s = toupper(*s);\n s++;\n }\n\n return s;\n}\n\nstatic void lib_print(struct hc_vm *vm, struct hc_sloc sloc) {\n struct hc_value *v = hc_vm_pop(vm);\n struct hc_dsl *dsl = hc_baseof(vm, struct hc_dsl, vm);\n hc_value_print(v, dsl->out);\n hc_value_deinit(v);\n}\n\nstatic void lib_upcase(struct hc_vm *vm, struct hc_sloc sloc) {\n struct hc_value *v = hc_vm_peek(vm);\n\n if (v->type != &HC_STRING) {\n hc_throw(\"Error in %s: Expected string (%s)\",\n\t hc_sloc_string(&sloc), v->type->name);\n }\n\n hc_upcase(v->as_string);\n}\n\nstruct env_item {\n char *key;\n struct hc_value value;\n};\n\nstatic enum hc_order env_cmp(const void *x, const void *y) {\n return hc_strcmp(*(const char **)x, *(const char **)y);\n}\n\nstatic const void *env_key(const void *x) {\n return &((const struct env_item *)x)->key;\n}\n\nvoid hc_dsl_init(struct hc_dsl *dsl, struct hc_malloc *malloc) {\n hc_set_init(&dsl->env, malloc, sizeof(struct env_item), env_cmp);\n dsl->env.key = env_key;\n dsl->out = hc_stdout();\n \n hc_vm_init(&dsl->vm, &hc_malloc_default);\n hc_dsl_set_fun(dsl, \"print\", lib_print);\n hc_dsl_set_fun(dsl, \"upcase\", lib_upcase);\n}\n\nstatic void deinit_env(struct hc_set *env) {\n hc_vector_do(&env->items, _it) {\n struct env_item *it = _it;\n free(it->key);\n hc_value_deinit(&it->value);\n }\n \n hc_set_deinit(env);\n}\n\nvoid hc_dsl_deinit(struct hc_dsl *dsl) {\n deinit_env(&dsl->env);\n hc_vm_deinit(&dsl->vm);\n}\n\nstruct hc_value *hc_dsl_getenv(struct hc_dsl *dsl, const char *key) {\n struct env_item *it = hc_set_find(&dsl->env, &key); \n return it ? &it->value : NULL;\n}\n\nstruct hc_value *hc_dsl_setenv(struct hc_dsl *dsl,\n\t\t\t const char *key,\n\t\t\t const struct hc_type *type) {\n struct env_item *it = hc_set_add(&dsl->env, &key, false);\n it->key = strdup(key);\n hc_value_init(&it->value, type);\n return &it->value;\n}\n\nvoid hc_dsl_set_fun(struct hc_dsl *dsl, const char *key, hc_vm_fun_t val) {\n hc_dsl_setenv(dsl, key, &HC_VM_FUN)->as_other = val;\n}\n\nvoid hc_dsl_set_string(struct hc_dsl *dsl, const char *key, const char *val) {\n hc_dsl_setenv(dsl, key, &HC_STRING)->as_string = strdup(val);\n}\n\nvoid hc_dsl_eval(struct hc_dsl *dsl, const char *in) {\n struct hc_list forms;\n hc_list_init(&forms);\n hc_defer(hc_forms_free(&forms));\n struct hc_sloc sloc = hc_sloc(\"eval\", 0, 0);\n while (hc_read_next(&in, &forms, &sloc));\n const size_t pc = dsl->vm.code.length;\n hc_forms_emit(&forms, dsl);\n hc_vm_eval(&dsl->vm, pc, -1);\n}\n\nvoid hc_form_init(struct hc_form *f,\n\t\t const struct hc_form_type *t,\n\t\t const struct hc_sloc sloc,\n\t\t struct hc_list *owner) {\n f->type = t;\n f->sloc = sloc;\n\n if (owner) {\n hc_list_push_back(owner, &f->owner);\n } else {\n hc_list_init(&f->owner);\n }\n}\n\nvoid hc_form_emit(struct hc_form *f, struct hc_dsl *dsl) {\n assert(f->type->emit);\n f->type->emit(f, dsl);\n}\n\nvoid hc_form_print(struct hc_form *f, struct hc_stream *out) {\n assert(f->type->print);\n f->type->print(f, out);\n}\n\nstruct hc_value *hc_form_value(const struct hc_form *f, struct hc_dsl *dsl) {\n return f->type->value ? f->type->value(f, dsl) : NULL;\n}\n\nvoid hc_form_free(struct hc_form *f) {\n hc_list_delete(&f->owner);\n assert(f->type->free);\n f->type->free(f);\n}\n\nstatic void call_emit(struct hc_form *_f, struct hc_dsl *dsl) {\n struct hc_call *f = hc_baseof(_f, struct hc_call, form);\n struct hc_value *t = hc_form_value(f->target, dsl);\n\n if (!t) {\n hc_throw(\"Error in %s: Missing call target\",\n\t hc_sloc_string(&_f->sloc));\n }\n\n if (t->type != &HC_VM_FUN) {\n hc_throw(\"Error in %s: '%s' isn't callable\",\n\t hc_sloc_string(&_f->sloc),\n\t t->type->name);\n }\n\n hc_list_do(&f->args, a) {\n hc_form_emit(hc_baseof(a, struct hc_form, owner), dsl);\n }\n \n hc_vm_emit(&dsl->vm,\n\t &HC_CALL,\n\t &(struct hc_call_op){\n\t .target = t->as_other,\n\t .sloc = _f->sloc\n\t });\n}\n\nstatic void call_print(const struct hc_form *_f, struct hc_stream *out) {\n struct hc_call *f = hc_baseof(_f, struct hc_call, form);\n hc_putc(out, '(');\n hc_form_print(f->target, out);\n\n hc_list_do(&f->args, i) {\n hc_putc(out, ' ');\n hc_form_print(hc_baseof(i, struct hc_form, owner), out);\n }\n \n hc_putc(out, ')');\n}\n\nstatic void call_free(struct hc_form *_f) {\n struct hc_call *f = hc_baseof(_f, struct hc_call, form);\n hc_form_free(f->target); \n\n hc_list_do(&f->args, i) {\n hc_form_free(hc_baseof(i, struct hc_form, owner));\n }\n\n free(f);\n}\n\nconst struct hc_form_type HC_CALL_FORM = {\n .emit = call_emit,\n .print = call_print,\n .value = NULL,\n .free = call_free\n};\n\nvoid hc_call_init(struct hc_call *f,\n\t\t const struct hc_sloc sloc,\n\t\t struct hc_list *owner,\n\t\t struct hc_form *target) { \n hc_form_init(&f->form, &HC_CALL_FORM, sloc, owner);\n f->target = target;\n hc_list_init(&f->args);\n}\n\nstatic void id_emit(struct hc_form *_f, struct hc_dsl *dsl) {\n struct hc_id *f = hc_baseof(_f, struct hc_id, form);\n struct hc_value *v = hc_dsl_getenv(dsl, f->name);\n\n if (!v) {\n hc_throw(\"Error in %s: Unknown identifier '%s'\",\n\t hc_sloc_string(&_f->sloc), f->name);\n }\n\n struct hc_push_op op;\n hc_value_copy(&op.value, v);\n hc_vm_emit(&dsl->vm, &HC_PUSH, &op);\n}\n\nstatic void id_print(const struct hc_form *_f, struct hc_stream *out) {\n struct hc_id *f = hc_baseof(_f, struct hc_id, form);\n hc_puts(out, f->name);\n}\n\nstatic struct hc_value *id_value(const struct hc_form *_f,\n\t\t\t\t struct hc_dsl *dsl) {\n struct hc_id *f = hc_baseof(_f, struct hc_id, form);\n return hc_dsl_getenv(dsl, f->name);\n}\n\nstatic void id_free(struct hc_form *_f) {\n struct hc_id *f = hc_baseof(_f, struct hc_id, form);\n free(f->name);\n free(f);\n}\n\nconst struct hc_form_type HC_ID_FORM = {\n .emit = id_emit,\n .print = id_print,\n .value = id_value,\n .free = id_free\n};\n\nvoid hc_id_init(struct hc_id *f,\n\t\tconst struct hc_sloc sloc,\n\t\tstruct hc_list *owner,\n\t\tconst char *name) {\n hc_form_init(&f->form, &HC_ID_FORM, sloc, owner);\n f->name = strdup(name);\n}\n\nstatic void literal_emit(struct hc_form *_f, struct hc_dsl *dsl) {\n struct hc_literal *f = hc_baseof(_f, struct hc_literal, form); \n struct hc_push_op op;\n hc_value_copy(&op.value, &f->value);\n hc_vm_emit(&dsl->vm, &HC_PUSH, &op);\n}\n\nstatic void literal_print(const struct hc_form *_f, struct hc_stream *out) {\n struct hc_literal *f = hc_baseof(_f, struct hc_literal, form);\n hc_value_write(&f->value, out);\n}\n\nstatic struct hc_value *literal_value(const struct hc_form *_f,\n\t\t\t\t struct hc_dsl *dsl) {\n struct hc_literal *f = hc_baseof(_f, struct hc_literal, form);\n return &f->value;\n}\n\nstatic void literal_free(struct hc_form *_f) {\n struct hc_literal *f = hc_baseof(_f, struct hc_literal, form);\n hc_value_deinit(&f->value);\n free(f);\n}\n\nconst struct hc_form_type HC_LITERAL = {\n .emit = literal_emit,\n .print = literal_print,\n .value = literal_value,\n .free = literal_free,\n};\n\nvoid hc_literal_init(struct hc_literal *f,\n\t\t const struct hc_sloc sloc,\n\t\t struct hc_list *owner) { \n hc_form_init(&f->form, &HC_LITERAL, sloc, owner);\n}\n\nvoid hc_skip_ws(const char **in, struct hc_sloc *sloc) {\n for (;; (*in)++) {\n switch (**in) {\n case ' ':\n case '\\t':\n sloc->col++;\n break;\n case '\\n':\n sloc->row++;\n sloc->col = 0;\n break;\n default:\n return;\n }\n }\n}\n\nvoid hc_read_call(const char **in,\n\t\t struct hc_list *out,\n\t\t struct hc_sloc *sloc) {\n struct hc_sloc floc = *sloc;\n\n if (**in != '(') {\n hc_throw(\"Error in %s: Invalid call syntax\",\n\t hc_sloc_string(sloc));\n }\n\n (*in)++;\n sloc->col++;\n hc_skip_ws(in, sloc);\n \n if (!hc_read_expr(in, out, sloc)) {\n hc_throw(\"Error in %s: Missing call target\",\n\t hc_sloc_string(sloc));\n }\n\n struct hc_form *t = hc_baseof(hc_list_pop_back(out),\n\t\t\t\tstruct hc_form,\n\t\t\t\towner);\n\n struct hc_call *f = malloc(sizeof(struct hc_call));\n hc_list_init(&t->owner);\n hc_call_init(f, floc, out, t);\n \n for (bool done = false; !done;) {\n hc_skip_ws(in, sloc);\n \n switch (**in) {\n case 0:\n hc_form_free(&f->form);\n\n hc_throw(\"Error in %s: Open call form\",\n\t hc_sloc_string(sloc));\n case ')':\n (*in)++;\n sloc->col++;\n done = true;\n continue;\n default:\n break;\n }\n\n if (!hc_read_expr(in, &f->args, sloc)) {\n hc_form_free(&f->form);\n \n hc_throw(\"Error in %s: Invalid call syntax\",\n\t hc_sloc_string(sloc));\n }\n }\n}\n\nbool hc_read_expr(const char **in,\n\t\t struct hc_list *out,\n\t\t struct hc_sloc *sloc) {\n const char c = **in;\n \n switch (c) {\n case '(':\n hc_read_call(in, out, sloc);\n return true;\n default:\n if (isalpha(c)) {\n hc_read_id(in, out, sloc);\n return true;\n }\n\n break;\n }\n\n return false;\n}\n\nvoid hc_read_id(const char **in,\n\t\tstruct hc_list *out,\n\t\tstruct hc_sloc *sloc) {\n struct hc_sloc floc = *sloc;\n struct hc_memory_stream buf;\n hc_memory_stream_init(&buf, &hc_malloc_default);\n hc_defer(hc_stream_deinit(&buf.stream));\n char c = 0;\n\n while ((c = **in)) {\n if (isspace(c) || c == '(' || c == ')') {\n break;\n }\n \n hc_putc(&buf.stream, c);\n sloc->col++;\n (*in)++;\n }\n\n struct hc_id *f = malloc(sizeof(struct hc_id));\n hc_id_init(f, floc, out, hc_memory_stream_string(&buf));\n}\n\nbool hc_read_next(const char **in,\n\t\t struct hc_list *out,\n\t\t struct hc_sloc *sloc) {\n if (**in == '$') {\n (*in)++;\n hc_read_call(in, out, sloc);\n return true;\n }\n \n return hc_read_text(in, out, sloc);\n}\n\nbool hc_read_text(const char **in,\n\t\t struct hc_list *out,\n\t\t struct hc_sloc *sloc) {\n struct hc_sloc floc = *sloc;\n const char *start = *in;\n \n while (**in && **in != '$') {\n if (**in == '\\n') {\n sloc->row++;\n } else {\n sloc->col++;\n }\n\n (*in)++;\n }\n\n size_t n = *in - start;\n \n if (n) {\n struct hc_value v;\n hc_value_init(&v, &HC_STRING)->as_string = strndup(start, n); \n struct hc_literal *vf = malloc(sizeof(struct hc_literal));\n hc_literal_init(vf, floc, out);\n vf->value = v;\n struct hc_id *t = malloc(sizeof(struct hc_literal));\n hc_id_init(t, floc, NULL, \"print\");\n struct hc_call *c = malloc(sizeof(struct hc_call));\n hc_call_init(c, floc, out, &t->form);\n return true;\n }\n\n return false;\n}\n\nvoid hc_forms_free(struct hc_list *in) {\n hc_list_do(in, i) {\n hc_form_free(hc_baseof(i, struct hc_form, owner));\n } \n}\n\nvoid hc_forms_emit(struct hc_list *in, struct hc_dsl *dsl) {\n hc_list_do(in, i) {\n hc_form_emit(hc_baseof(i, struct hc_form, owner), dsl);\n } \n}\n"], ["/hacktical-c/error/error.c", "#include \n#include \n#include \n#include \n\n#include \"error.h\"\n#include \"malloc1/malloc1.h\"\n#include \"vector/vector.h\"\n\n__thread struct hc_error *hc_error = NULL;\n\nstatic struct hc_vector *handlers() {\n static bool init = true;\n static __thread struct hc_vector handlers;\n\n if (init) {\n hc_vector_init(&handlers, &hc_malloc_default, sizeof(jmp_buf));\n init = false;\n }\n \n return &handlers;\n}\n\nvoid hc_catch_push(jmp_buf h) {\n memcpy((jmp_buf *)hc_vector_push(handlers()), h, sizeof(jmp_buf));\n}\n\nvoid hc_catch_pop() {\n hc_vector_pop(handlers());\n}\n\nvoid hc_errors_deinit() {\n hc_vector_deinit(handlers());\n}\n\nvoid _hc_throw(struct hc_error *e) {\n struct hc_vector *hs = handlers();\n\n if (!hs->length) {\n fputs(e->message, stderr);\n hc_error_free(e);\n abort();\n }\n \n jmp_buf t;\n memcpy(t, *(jmp_buf *)hc_vector_pop(hs), sizeof(jmp_buf));\n hc_error = e;\n longjmp(t, 1);\n}\n\nstruct hc_error *hc_error_new(const char *message, ...) {\n va_list args;\n va_start(args, message);\n \n va_list tmp_args;\n va_copy(tmp_args, args);\n int len = vsnprintf(NULL, 0, message, tmp_args);\n va_end(tmp_args);\n\n if (len < 0) {\n vfprintf(stderr, message, args);\n abort();\n }\n \n len++;\n struct hc_error *e = malloc(sizeof(struct hc_error));\n e->message = malloc(len);\n vsnprintf(e->message, len, message, args);\n va_end(args);\n return e;\n}\n\nvoid hc_error_free(struct hc_error *e) {\n free(e->message);\n free(e);\n}\n\nbool hc_streq(const char *l, const char *r) {\n for (; *l && *l == *r; l++, r++);\n return *l == *r;\n}\n"], ["/hacktical-c/stream1/stream1.c", "#include \n#include \n#include \n#include \n\n#include \"dynamic/dynamic.h\"\n#include \"error/error.h\"\n#include \"malloc1/malloc1.h\"\n#include \"macro/macro.h\"\n#include \"stream1.h\"\n\nsize_t hc_read(struct hc_stream *s, uint8_t *data, const size_t n) {\n assert(s->read);\n return s->read(s, data, n);\n}\n\nsize_t hc_write(struct hc_stream *s, const uint8_t *data, const size_t n) {\n assert(s->write);\n return s->write(s, data, n);\n}\n\nchar hc_getc(struct hc_stream *s) {\n char c = 0;\n return hc_read(s, (uint8_t *)&c, 1) ? c : 0;\n}\n\nchar *hc_gets(struct hc_stream *s, struct hc_malloc *malloc) {\n struct hc_vector out;\n hc_vector_init(&out, malloc, 1);\n\n for (;;) {\n char c = hc_getc(s);\n\n if (c == EOF) {\n break;\n }\n\n *(char *)hc_vector_push(&out) = c;\n\n if (c == '\\n') {\n break;\n }\n }\n\n \n *(char *)hc_vector_push(&out) = 0;\n return (char *)out.start;\n}\n\nsize_t hc_putc(struct hc_stream *s, const char data) {\n const uint8_t d[2] = {data, 0};\n return hc_write(s, d, 1);\n}\n\nsize_t hc_puts(struct hc_stream *s, const char *data) {\n return hc_write(s, (const uint8_t *)data, strlen(data));\n}\n\nsize_t hc_vprintf(struct hc_stream *s,\n\t\t const char *spec,\n\t\t va_list args) {\n char *data = hc_vsprintf(spec, args);\n hc_defer(free(data));\n return hc_write(s, (uint8_t *)data, strlen(data));\n}\n\nsize_t hc_printf(struct hc_stream *s, const char *spec, ...) {\n va_list args;\n va_start(args, spec);\n hc_defer(va_end(args));\n return hc_vprintf(s, spec, args);\n}\n\nvoid hc_stream_deinit(struct hc_stream *s) {\n assert(s->deinit);\n s->deinit(s);\n}\n\nsize_t file_read(struct hc_stream *s, uint8_t *data, const size_t n) {\n struct hc_file_stream *fs = hc_baseof(s, struct hc_file_stream, stream);\n assert(fs->file);\n return fread(data, n, 1, fs->file);\n}\n\nsize_t file_write(struct hc_stream *s, const uint8_t *data, const size_t n) {\n struct hc_file_stream *fs = hc_baseof(s, struct hc_file_stream, stream);\n assert(fs->file);\n return fwrite(data, n, 1, fs->file);\n}\n\nvoid file_deinit(struct hc_stream *s) {\n struct hc_file_stream *fs = hc_baseof(s, struct hc_file_stream, stream);\n\n if (fs->opts.close_file) {\n assert(fs->file);\n \n if (fclose(fs->file) == EOF) {\n hc_throw(\"Failed closing file\");\n }\n \n fs->file = NULL;\n }\n}\n\nstruct hc_file_stream *_hc_file_stream_init(struct hc_file_stream *s,\n\t\t\t\t\t FILE *file,\n\t\t\t\t\t const struct hc_file_stream_opts opts) {\n s->stream = (struct hc_stream){\n .read = file_read,\n .write = file_write,\n .deinit = file_deinit,\n };\n \n s->file = file;\n s->opts = opts;\n return s;\n};\n\nstruct hc_stream *hc_stdout() {\n static __thread bool init = true;\n static __thread struct hc_file_stream s;\n\n if (init) {\n hc_file_stream_init(&s, stdout);\n init = false;\n }\n\n return &s.stream;\n}\n\nsize_t memory_read(struct hc_stream *s, uint8_t *data, size_t n) {\n struct hc_memory_stream *ms = hc_baseof(s, struct hc_memory_stream, stream);\n\n if (ms->rpos + n > ms->data.length) {\n n = ms->data.length - ms->rpos;\n }\n \n memcpy(data, ms->data.start + ms->rpos, n);\n ms->rpos += n;\n return n;\n}\n\nsize_t memory_write(struct hc_stream *s,\n\t\t const uint8_t *data,\n\t\t const size_t n) {\n struct hc_memory_stream *ms = hc_baseof(s, struct hc_memory_stream, stream);\n uint8_t *const dst = hc_vector_insert(&ms->data, ms->data.length, n);\n memcpy(dst, data, n);\n return n;\n}\n\nvoid memory_deinit(struct hc_stream *s) {\n struct hc_memory_stream *ms = hc_baseof(s, struct hc_memory_stream, stream);\n hc_vector_deinit(&ms->data);\n}\n\nstruct hc_memory_stream *hc_memory_stream_init(struct hc_memory_stream *s,\n\t\t\t\t\t struct hc_malloc *malloc) {\n s->stream = (struct hc_stream){\n .read = memory_read,\n .write = memory_write,\n .deinit = memory_deinit,\n };\n \n hc_vector_init(&s->data, malloc, 1);\n s->rpos = 0;\n return s;\n}\n\nconst char *hc_memory_stream_string(struct hc_memory_stream *s) {\n if (!s->data.length || (*(s->data.end-1))) {\n *(uint8_t *)hc_vector_push(&s->data) = 0;\n }\n\n return (const char *)s->data.start;\n}\n"], ["/hacktical-c/slog/slog.c", "#include \n#include \n#include \n#include \n\n#include \"error/error.h\"\n#include \"macro/macro.h\"\n#include \"malloc1/malloc1.h\"\n#include \"slog.h\"\n\n__thread struct hc_slog *_hc_slog = NULL;\n\nstruct hc_slog *hc_slog() {\n if (_hc_slog != NULL) {\n return _hc_slog;\n }\n \n static __thread bool init = true;\n static __thread struct hc_slog_stream s;\n\n if (init) {\n hc_slog_stream_init(&s, hc_stdout());\n init = false;\n }\n\n return &s.slog;\n}\n\nstatic void field_deinit(struct hc_slog_field *f) {\n free(f->name);\n hc_value_deinit(&f->value);\n}\n\nstatic void slog_write(struct hc_slog *s,\n\t\t const size_t n,\n\t\t struct hc_slog_field *fields[]) {\n assert(s->write);\n s->write(s, n, fields);\n}\n\nvoid __hc_slog_write(struct hc_slog *s,\n\t\t const size_t n,\n\t\t struct hc_slog_field *fields[]) {\n slog_write(s, n, fields);\n \n for(size_t i = 0; i < n; i++) {\n struct hc_slog_field *f = fields[i];\n field_deinit(f);\n free(f);\n }\n}\n\nvoid _hc_slog_deinit(struct hc_slog *s) {\n if (s->deinit) {\n s->deinit(s);\n }\n}\n\nstatic struct hc_value *field_init(struct hc_slog_field *f,\n\t\t\t\t const char *name,\n\t\t\t\t const struct hc_type *type) {\n f->name = strdup(name);\n hc_value_init(&f->value, type);\n return &f->value;\n}\n\nstruct hc_slog_field *hc_slog_bool(const char *name, const bool value) {\n struct hc_slog_field *f = malloc(sizeof(struct hc_slog_field));\n field_init(f, name, &HC_BOOL)->as_bool = value;\n return f;\n}\n\nstruct hc_slog_field *hc_slog_int(const char *name, const int value) {\n struct hc_slog_field *f = malloc(sizeof(struct hc_slog_field));\n field_init(f, name, &HC_INT)->as_int = value;\n return f;\n}\n\nstruct hc_slog_field *hc_slog_string(const char *name, const char *value) {\n struct hc_slog_field *f = malloc(sizeof(struct hc_slog_field));\n field_init(f, name, &HC_STRING)->as_string = strdup(value);\n return f;\n}\n\nstruct hc_slog_field *hc_slog_time(const char *name, const hc_time_t value) {\n struct hc_slog_field *f = malloc(sizeof(struct hc_slog_field));\n field_init(f, name, &HC_TIME)->as_time = value;\n return f;\n}\n\nvoid stream_deinit(struct hc_slog *s) {\n struct hc_slog_stream *ss = hc_baseof(s, struct hc_slog_stream, slog);\n if (ss->opts.close_out) { hc_stream_deinit(ss->out); }\n}\n\nstatic void field_write(struct hc_slog_field *f, struct hc_stream *out) {\n hc_puts(out, f->name);\n hc_putc(out, '=');\n hc_value_write(&f->value, out);\n}\n\nstatic void stream_write(struct hc_slog *s,\n\t\t\t const size_t n,\n\t\t\t struct hc_slog_field *fields[]) {\n struct hc_slog_stream *ss = hc_baseof(s, struct hc_slog_stream, slog);\n\n for(size_t i = 0; i < n; i++) {\n struct hc_slog_field *f = fields[i];\n if (i) { hc_puts(ss->out, \", \"); }\n field_write(f, ss->out);\n }\n\n hc_putc(ss->out, '\\n');\n}\n\nstruct hc_slog_stream *_hc_slog_stream_init(struct hc_slog_stream *s,\n\t\t\t\t\t struct hc_stream *out,\n\t\t\t\t\t const struct hc_slog_stream_opts opts) {\n s->slog.deinit = stream_deinit;\n s->slog.write = stream_write;\n s->out = out;\n s->opts = opts;\n return s;\n}\n\nstatic void context_deinit(struct hc_slog *s) {\n struct hc_slog_context *sc = hc_baseof(s, struct hc_slog_context, slog);\n\n for (size_t i = 0; i < sc->length; i++) {\n struct hc_slog_field *f = sc->fields[i];\n field_deinit(f);\n free(f);\n }\n\n free(sc->fields);\n}\n\nstatic void context_write(struct hc_slog *s,\n\t\t\t const size_t n,\n\t\t\t struct hc_slog_field *fields[]) {\n struct hc_slog_context *c = hc_baseof(s, struct hc_slog_context, slog);\n struct hc_slog_field *fs[c->length + n];\n memcpy(fs, c->fields, sizeof(struct hc_slog_field *) * c->length);\n memcpy(fs + c->length, fields, sizeof(struct hc_slog_field *) * n);\n slog_write(c->parent, c->length + n, fs);\n}\n\nstruct hc_slog_context *hc_slog_context_init(struct hc_slog_context *c,\n\t\t\t\t\t size_t length,\n\t\t\t\t\t struct hc_slog_field *fields[]) {\n c->slog.deinit = context_deinit;\n c->slog.write = context_write;\n c->parent = hc_slog();\n c->length = length;\n size_t s = sizeof(struct hc_slog_field *) * length;\n c->fields = malloc(s);\n memcpy(c->fields, fields, s);\n return c;\n}\n"], ["/hacktical-c/dsl/benchmarks.c", "#include \"chrono/chrono.h\"\n#include \"dsl.h\"\n\nvoid dsl_benchmarks() {\n hc_time_t t;\n const int n = 100000;\n\n char buf[32];\n t = hc_now();\n \n for (int i = 0; i < n; i++) {\n sprintf(buf, \"abc %s def\", \"ghi\");\n }\n\n hc_time_print(&t, \"sprintf: \"); \n \n struct hc_dsl dsl;\n hc_dsl_init(&dsl, &hc_malloc_default);\n hc_defer(hc_dsl_deinit(&dsl));\n struct hc_memory_stream out;\n hc_memory_stream_init(&out, &hc_malloc_default);\n hc_defer(hc_stream_deinit(&out.stream));\n dsl.out = &out.stream;\n hc_dsl_set_string(&dsl, \"foo\", \"ghi\");\n hc_dsl_eval(&dsl, \"abc $(print foo) def\");\n\n t = hc_now();\n\n for (int i = 0; i < n; i++) {\n hc_vector_clear(&out.data);\n hc_vm_eval(&dsl.vm, 0, -1);\n }\n\n hc_time_print(&t, \"dsl: \");\n}\n"], ["/hacktical-c/vm/vm.c", "#include \n#include \n#include \n#include \n#include \n\n#include \"error/error.h\"\n#include \"macro/macro.h\"\n#include \"vm.h\"\n\nstruct hc_sloc hc_sloc(const char *source, const int row, const int col) {\n struct hc_sloc s = {.source = {0}, .row = row, .col = col};\n assert(strlen(source) < sizeof(s.source));\n strcpy(s.source, source);\n return s;\n}\n\nconst char *hc_sloc_string(struct hc_sloc *sloc) {\n snprintf(sloc->out, sizeof(sloc->out), \"'%s'; row %d, column %d\",\n\t sloc->source, sloc->row, sloc->col);\n return sloc->out;\n}\n\n\nvoid hc_vm_init(struct hc_vm *vm, struct hc_malloc *malloc) {\n hc_vector_init(&vm->stack, malloc, sizeof(struct hc_value));\n hc_vector_init(&vm->ops, malloc, sizeof(const struct hc_op *));\n hc_vector_init(&vm->code, malloc, sizeof(hc_op_eval_t));\n}\n\nstatic size_t op_items(const struct hc_op *op,\n\t\t uint8_t *p,\n\t\t struct hc_vm *vm) {\n const size_t s = op->size + hc_align(p, op->align) - p;\n return ceil(s / (double)vm->code.item_size);\n}\n\nstatic void deinit_stack(struct hc_vm *vm) {\n hc_vector_do(&vm->stack, v) {\n hc_value_deinit(v);\n }\n\n hc_vector_deinit(&vm->stack);\n}\n\nstatic void deinit_ops(struct hc_vm *vm) {\n uint8_t *p = vm->code.start;\n \n hc_vector_do(&vm->ops, _op) {\n const struct hc_op *op = *(const struct hc_op **)_op;\n p += sizeof(hc_op_eval_t);\n\n if (op->deinit) {\n op->deinit(hc_align(p, op->align));\n }\n\n p += op_items(op, p, vm) * vm->code.item_size;\n }\n\n hc_vector_deinit(&vm->ops);\n}\n\nvoid hc_vm_deinit(struct hc_vm *vm) { \n deinit_stack(vm);\n deinit_ops(vm);\n hc_vector_deinit(&vm->code);\n}\n\nstruct hc_value *hc_vm_push(struct hc_vm *vm) {\n return hc_vector_push(&vm->stack);\n}\n\nstruct hc_value *hc_vm_peek(struct hc_vm *vm) {\n return hc_vector_peek(&vm->stack);\n}\n\nstruct hc_value *hc_vm_pop(struct hc_vm *vm) {\n return hc_vector_pop(&vm->stack);\n}\n\nsize_t hc_vm_emit(struct hc_vm *vm,\n\t\t const struct hc_op *op,\n\t\t const void *data) {\n *(const struct hc_op **)hc_vector_push(&vm->ops) = op;\n const size_t pc = vm->code.length;\n *(hc_op_eval_t *)hc_vector_push(&vm->code) = op->eval;\n \n uint8_t *const p = hc_vector_insert(&vm->code,\n\t\t\t\t vm->code.length,\n\t\t\t\t op_items(op, vm->code.end, vm));\n \n memcpy(hc_align(p, op->align), data, op->size);\n return pc;\n}\n\nvoid hc_vm_eval(struct hc_vm *vm,\n\t\tconst size_t start_pc,\n\t\tconst size_t end_pc) {\n const uint8_t *const ep = (end_pc == -1)\n ? vm->code.end\n : hc_vector_get(&vm->code, end_pc);\n\n for (uint8_t *p = hc_vector_get(&vm->code, start_pc);\n p != ep;\n p = (*(hc_op_eval_t *)p)(vm, p + vm->code.item_size));\n}\n\nstatic void fun_print(const struct hc_value *v, struct hc_stream *out) {\n hc_printf(out, \"%p\", v->as_other);\n}\n\nconst struct hc_type HC_VM_FUN = {\n .name = \"VM/Fun\",\n .copy = NULL,\n .print = fun_print\n};\n\nstatic uint8_t *call_eval(struct hc_vm *vm, uint8_t *data) {\n struct hc_call_op *op = (void *)hc_align(data, alignof(struct hc_call_op));\n op->target(vm, op->sloc);\n return (uint8_t *)op + sizeof(struct hc_call_op);\n}\n\nconst struct hc_op HC_CALL = (struct hc_op){\n .name = \"call\",\n .align = alignof(struct hc_call_op),\n .size = sizeof(struct hc_call_op),\n .eval = call_eval,\n .deinit = NULL\n};\n\nstatic void push_deinit(uint8_t *data) {\n struct hc_push_op *op = (void *)data;\n hc_value_deinit(&op->value);\n}\n\nstatic uint8_t *push_eval(struct hc_vm *vm, uint8_t *data) {\n struct hc_push_op *op = (void *)hc_align(data, alignof(struct hc_push_op));\n hc_value_copy(hc_vm_push(vm), &op->value);\n return (uint8_t *)op + sizeof(struct hc_push_op);\n}\n\nconst struct hc_op HC_PUSH = (struct hc_op){\n .name = \"push\",\n .align = alignof(struct hc_push_op),\n .size = sizeof(struct hc_push_op),\n .eval = push_eval,\n .deinit = push_deinit\n};\n"], ["/hacktical-c/dsl/tests.c", "#include \n#include \n#include \"dsl.h\"\n\nstatic void read_call_tests() {\n struct hc_sloc sloc = hc_sloc(\"read_call_tests\", 0, 0);\n \n struct hc_list out;\n hc_list_init(&out);\n hc_defer(hc_forms_free(&out));\n\n const char *s = \"( foo bar )\";\n const char *in = s;\n assert(hc_read_expr(&in, &out, &sloc));\n \n struct hc_form *f = hc_baseof(out.next, struct hc_form, owner);\n assert(f->type == &HC_CALL_FORM);\n}\n\nstatic void read_id_tests() {\n struct hc_sloc sloc = hc_sloc(\"read_id_tests\", 0, 0);\n \n struct hc_list out;\n hc_list_init(&out);\n hc_defer(hc_forms_free(&out));\n\n const char *s = \"foo\";\n const char *in = s;\n assert(hc_read_expr(&in, &out, &sloc));\n \n struct hc_form *f = hc_baseof(out.next, struct hc_form, owner);\n assert(f->type == &HC_ID_FORM);\n}\n\nstatic void eval_tests() {\n struct hc_dsl dsl;\n hc_dsl_init(&dsl, &hc_malloc_default);\n hc_defer(hc_dsl_deinit(&dsl));\n struct hc_memory_stream out;\n hc_memory_stream_init(&out, &hc_malloc_default);\n hc_defer(hc_stream_deinit(&out.stream));\n dsl.out = &out.stream;\n hc_dsl_set_string(&dsl, \"foo\", \"ghi\");\n hc_dsl_eval(&dsl, \"abc $(print (upcase foo)) def\");\n assert(strcmp(\"abc GHI def\", hc_memory_stream_string(&out)) == 0);\n}\n\nvoid dsl_tests() {\n read_id_tests();\n read_call_tests();\n eval_tests();\n}\n"], ["/hacktical-c/vector/vector.c", "#include \n#include \n#include \n#include \n#include \"vector.h\"\n#include \"malloc1/malloc1.h\"\n\nstatic void grow(struct hc_vector *v) {\n hc_vector_grow(v, v->capacity ? v->capacity*2 : 2);\n}\n\nstruct hc_vector *hc_vector_init(struct hc_vector *v,\n\t\t\t\t struct hc_malloc *malloc,\n\t\t\t\t const size_t item_size) {\n v->malloc = malloc;\n v->item_size = item_size;\n v->capacity = 0;\n v->length = 0;\n v->start = v->end = NULL;\n return v;\n}\n\nvoid hc_vector_deinit(struct hc_vector *v) {\n if (v->start) { hc_release(v->malloc, v->start); }\n}\n\nvoid hc_vector_grow(struct hc_vector *v, const size_t capacity) {\n v->capacity = capacity; \n size_t size = v->item_size * (v->capacity+1);\n uint8_t *new_start = hc_acquire(v->malloc, size);\n\n if (v->start) {\n memmove(new_start, v->start, v->length * v->item_size);\n hc_release(v->malloc, v->start); \n }\n \n v->start = new_start;\n v->end = v->start + v->item_size*v->length;\n}\n\nvoid hc_vector_clear(struct hc_vector *v) {\n v->length = 0;\n v->end = v->start;\n}\n\nvoid *hc_vector_get(struct hc_vector *v, const size_t i) {\n return v->start ? v->start + v->item_size*i : NULL;\n}\n\nconst void *hc_vector_get_const(const struct hc_vector *v, const size_t i) {\n return v->start ? v->start + v->item_size*i : NULL;\n}\n\nvoid *hc_vector_push(struct hc_vector *v) {\n if (v->length == v->capacity) { grow(v); }\n void *p = v->end;\n v->end += v->item_size;\n v->length++;\n return p;\n}\n\nvoid *hc_vector_peek(struct hc_vector *v) {\n return v->length ? v->end - v->item_size : NULL;\n}\n\nvoid *hc_vector_pop(struct hc_vector *v) {\n if (!v->length) { return NULL; }\n v->end -= v->item_size;\n v->length--;\n return v->end;\n}\n\nvoid *hc_vector_insert(struct hc_vector *v, const size_t i, const size_t n) {\n const size_t m = v->length+n;\n if (m > v->capacity) { hc_vector_grow(v, m); } \n uint8_t *const p = hc_vector_get(v, i);\n\n if (i < v->length) {\n memmove(p + v->item_size*n, p, (v->length - i) * v->item_size);\n }\n \n v->length += n;\n v->end += n*v->item_size;\n return p;\n}\n\nbool hc_vector_delete(struct hc_vector *v, const size_t i, const size_t n) {\n const size_t m = i+n;\n assert(m <= v->length);\n\n if (m < v->length) {\n uint8_t *const p = hc_vector_get(v, i);\n memmove(p, p + n*v->item_size, i + (v->length-n) * v->item_size);\n }\n\n v->length -= n;\n v->end -= n*v->item_size;\n return true;\n}\n"], ["/hacktical-c/malloc2/malloc2.c", "#include \n#include \n#include \"error/error.h\"\n#include \"macro/macro.h\"\n#include \"malloc2.h\"\n\n/* Memo */\n\nstruct memo {\n size_t size;\n uint8_t data[];\n};\n\nstatic void *memo_acquire(struct hc_malloc *a, size_t size) {\n struct hc_memo_alloc *ma = hc_baseof(a, struct hc_memo_alloc, malloc);\n\n if (hc_set_length(&ma->memo)) {\n bool ok = false;\n size_t i = hc_set_index(&ma->memo, &size, &ok);\n\n if (ok) {\n struct hc_vector *is = &ma->memo.items;\n struct memo *m = *(struct memo **)hc_vector_get(is, i);\n hc_vector_delete(is, i, 1);\n return m->data;\n }\n }\n\n struct memo *m = hc_acquire(ma->source, sizeof(struct memo) + size);\n m->size = size;\n return m->data;\n}\n\nstatic void memo_release(struct hc_malloc *a, void *p) {\n struct hc_memo_alloc *ma = hc_baseof(a, struct hc_memo_alloc, malloc);\n struct memo *m = hc_baseof(p, struct memo, data);\n *(struct memo **)hc_set_add(&ma->memo, &m->size, true) = m;\n}\n\nstatic enum hc_order memo_cmp(const void *l, const void *r) {\n return hc_cmp(*(size_t *)l, *(size_t *)r);\n}\n\nstatic const void *memo_key(const void *p) {\n struct memo *m = *(struct memo **)p;\n return &m->size;\n}\n\nstruct hc_memo_alloc *hc_memo_alloc_init(struct hc_memo_alloc *a,\n\t\t\t\t\t struct hc_malloc *source) {\n a->malloc.acquire = memo_acquire;\n a->malloc.release = memo_release;\n a->source = source;\n hc_set_init(&a->memo, &hc_malloc_default, sizeof(struct memo *), memo_cmp);\n a->memo.key = memo_key;\n return a;\n}\n\nvoid hc_memo_alloc_deinit(struct hc_memo_alloc *a) {\n hc_vector_do(&a->memo.items, _m) {\n struct memo *m = *(struct memo **)_m;\n hc_release(a->source, m);\n }\n \n hc_set_deinit(&a->memo);\n}\n\n/* Slab */\n\nstruct slab {\n struct hc_list slabs;\n uint8_t *next;\n uint8_t memory[];\n};\n\nstatic struct slab *add_slab(struct hc_slab_alloc *a, const size_t size) {\n struct slab *s = hc_acquire(a->source, sizeof(struct slab) + size);\n hc_list_push_front(&a->slabs, &s->slabs);\n s->next = s->memory;\n return s;\n}\n\nstatic struct slab *get_slab(struct hc_slab_alloc *a, const size_t size) {\n if (size > a->slab_size) {\n return add_slab(a, size);\n }\n\n struct slab *result = NULL;\n\n hc_list_do(&a->slabs, sl) {\n struct slab *s = hc_baseof(sl, struct slab, slabs);\n uint8_t *p = hc_align(s->next, size);\n\n if (p + size > s->memory + a->slab_size) {\n break;\n }\n\n result = s;\n }\n\n return result ? result : add_slab(a, a->slab_size);\n}\n\nstatic void *slab_acquire(struct hc_malloc *a, const size_t size) {\n struct hc_slab_alloc *sa = hc_baseof(a, struct hc_slab_alloc, malloc);\n struct slab *s = get_slab(sa, size);\n uint8_t *p = hc_align(s->next, size);\n s->next = p + size;\n\n while (s->slabs.next != &s->slabs) {\n struct slab *ns = hc_baseof(s->slabs.next, struct slab, slabs);\n\n if (ns->next - ns->memory > s->next - s->memory) {\n hc_list_shift_back(&s->slabs);\n } else {\n break;\n }\n }\n \n return p;\n}\n\nstatic void slab_release(struct hc_malloc *a, void *p) {\n // Do nothing\n}\n\nstruct hc_slab_alloc *hc_slab_alloc_init(struct hc_slab_alloc *a,\n\t\t\t\t\t struct hc_malloc *source,\n\t\t\t\t\t const size_t slab_size) {\n a->malloc.acquire = slab_acquire;\n a->malloc.release = slab_release;\n a->source = source;\n hc_list_init(&a->slabs);\n a->slab_size = slab_size;\n return a;\n}\n\nvoid hc_slab_alloc_deinit(struct hc_slab_alloc *a) {\n hc_list_do(&a->slabs, _s) {\n struct slab *s = hc_baseof(_s, struct slab, slabs);\n hc_release(a->source, s);\n }\n}\n"], ["/hacktical-c/dynamic/tests.c", "#include \n#include \"dynamic.h\"\n\nvoid dynamic_tests() {\n const char *out = \"/var/tmp/libtest.so\";\n \n hc_compile(\"#include \\n\"\n\t \"int test() { return 42; }\",\n\t out,\n\t .cflags = (const char *[]){\"-Wall\",\n\t\t\t\t\t\"-fsanitize=undefined\",\n\t\t\t\t\tNULL});\n\n struct hc_dlib lib;\n hc_dlib_init(&lib, out);\n hc_defer(hc_dlib_deinit(&lib));\n int (*fn)() = hc_dlib_find(&lib, \"test\");\n assert(fn() == 42);\n}\n"], ["/hacktical-c/malloc2/benchmarks.c", "#include \n\n#include \"chrono/chrono.h\"\n#include \"malloc2.h\"\n\n#define N 1000\n#define MAX_SIZE 64\n#define GET_SIZE() ((rand() % MAX_SIZE) + 1)\n\nstatic void run_malloc() {\n int *ps[N];\n hc_time_t t = hc_now();\n \n for (int i = 0; i < N; i++) {\n ps[i] = malloc(GET_SIZE());\n }\n\n for (int i = 0; i < N; i++) {\n free(ps[i]);\n }\n\n hc_time_print(&t, \"malloc: \"); \n}\n\nstatic void run_bump() {\n struct hc_bump_alloc a;\n hc_bump_alloc_init(&a, &hc_malloc_default, N * MAX_SIZE);\n hc_time_t t = hc_now();\n\n for (int i = 0; i < N; i++) {\n hc_acquire(&a.malloc, GET_SIZE());\n }\n\n hc_bump_alloc_deinit(&a);\n hc_time_print(&t, \"bump: \");\n}\n\nstatic void run_slab() {\n struct hc_slab_alloc a;\n hc_slab_alloc_init(&a, &hc_malloc_default, N);\n hc_time_t t = hc_now();\n\n for (int i = 0; i < N; i++) {\n hc_acquire(&a.malloc, GET_SIZE());\n }\n\n hc_slab_alloc_deinit(&a);\n hc_time_print(&t, \"slab: \");\n}\n\nvoid malloc2_benchmarks() {\n const int s = time(NULL);\n \n srand(s);\n run_malloc();\n\n srand(s);\n run_bump();\n\n srand(s);\n run_slab();\n}\n"], ["/hacktical-c/error/tests.c", "#include \n#include \n#include \n#include \n\n#include \"error.h\"\n\nvoid error_tests() {\n void on_catch(struct hc_error *e) {\n assert(hc_streq(\"E123\", e->message) == 0);\n }\n \n hc_catch(on_catch) {\n hc_throw(\"E123 Going %s\", \"Down!\");\n }\n}\n"], ["/hacktical-c/malloc1/malloc1.c", "#include \n#include \n#include \"error/error.h\"\n#include \"macro/macro.h\"\n#include \"malloc1.h\"\n\nstatic void *default_acquire(struct hc_malloc *m, size_t size) {\n return malloc(size);\n}\n\nstatic void default_release(struct hc_malloc *m, void *p) {\n free(p);\n}\n\nstruct hc_malloc hc_malloc_default = {.acquire = default_acquire,\n\t\t\t\t .release = default_release};\n\n__thread struct hc_malloc *hc_mallocp = NULL;\n\n/* Bump */\n\nstatic void *bump_acquire(struct hc_malloc *a, size_t size) {\n if (size <= 0) {\n hc_throw(HC_INVALID_SIZE);\n } \n\n struct hc_bump_alloc *ba = hc_baseof(a, struct hc_bump_alloc, malloc);\n \n if (ba->size - ba->offset < size) {\n hc_throw(HC_NO_MEMORY);\n } \n\n uint8_t *p = ba->memory + ba->offset;\n uint8_t *pa = hc_align(p, size);\n ba->offset = ba->offset + pa - p + size;\n return pa;\n}\n\nstatic void bump_release(struct hc_malloc *a, void *p) {\n //Do nothing\n}\n\nvoid hc_bump_alloc_init(struct hc_bump_alloc *a,\n\t\t\tstruct hc_malloc *source,\n\t\t\tsize_t size) {\n a->malloc.acquire = bump_acquire;\n a->malloc.release = bump_release;\n a->source = source;\n a->size = size;\n a->offset = 0;\n a->memory = hc_acquire(source, size);\n}\n\nvoid hc_bump_alloc_deinit(struct hc_bump_alloc *a) {\n hc_release(a->source, a->memory);\n}\n"], ["/hacktical-c/set/set.c", "#include \n#include \"set.h\"\n\nstruct hc_set *hc_set_init(struct hc_set *s,\n\t\t\t struct hc_malloc *malloc,\n\t\t\t const size_t item_size,\n\t\t\t hc_cmp_t cmp) {\n hc_vector_init(&s->items, malloc, item_size);\n s->cmp = cmp;\n s->key = NULL;\n return s;\n}\n\nvoid hc_set_deinit(struct hc_set *s) {\n hc_vector_deinit(&s->items);\n}\n\nsize_t hc_set_index(const struct hc_set *s, const void *key, bool *ok) {\n size_t min = 0, max = s->items.length;\n\n while (min < max) {\n const size_t i = (min+max)/2;\n const void *v = hc_vector_get_const(&s->items, i);\n const void *k = s->key ? s->key(v) : v;\n\n switch (s->cmp(key, k)) {\n case HC_LT:\n max = i;\n break;\n case HC_GT:\n min = i+1;\n break;\n default:\n if (ok) {\n\t*ok = true;\n }\n \n return i;\n }\n }\n\n return min;\n}\n\nsize_t hc_set_length(const struct hc_set *s) {\n return s->items.length;\n}\n\nvoid *hc_set_find(struct hc_set *s, const void *key) {\n bool ok = false;\n const size_t i = hc_set_index(s, key, &ok);\n return ok ? hc_vector_get(&s->items, i) : NULL;\n}\n\nvoid *hc_set_add(struct hc_set *s, const void *key, const bool force) {\n bool ok = false;\n const size_t i = hc_set_index(s, key, &ok);\n\n if (ok && !force) {\n return NULL;\n }\n \n return hc_vector_insert(&s->items, i, 1);\n}\n\nvoid hc_set_clear(struct hc_set *s) {\n hc_vector_clear(&s->items);\n}\n"], ["/hacktical-c/reflect/reflect.c", "#include \n#include \n#include \n\n#include \"reflect.h\"\n#include \"stream1/stream1.h\"\n\nstruct hc_value *hc_value_init(struct hc_value *v, const struct hc_type *t) {\n v->type = t;\n return v;\n}\n\nvoid hc_value_deinit(struct hc_value *v) {\n if (v->type->deinit) {\n v->type->deinit(v);\n }\n}\n\nstruct hc_value *hc_value_copy(struct hc_value *dst, struct hc_value *src) {\n const struct hc_type *t = src->type;\n \n if (t->copy) {\n dst->type = t;\n t->copy(dst, src);\n } else {\n *dst = *src;\n }\n\n return dst;\n}\n\nvoid hc_value_print(struct hc_value *v, struct hc_stream *out) {\n if (v->type->print) {\n v->type->print(v, out);\n } else {\n hc_value_write(v, out);\n }\n}\n\nvoid hc_value_write(struct hc_value *v, struct hc_stream *out) {\n assert(v->type->write);\n v->type->write(v, out);\n}\n\nstatic void bool_write(const struct hc_value *v, struct hc_stream *out) {\n hc_puts(out, v->as_bool ? \"true\" : \"false\");\n}\n\nconst struct hc_type HC_BOOL = {\n .name = \"Bool\",\n .copy = NULL,\n .write = bool_write\n};\n\nstatic void fix_write(const struct hc_value *v, struct hc_stream *out) {\n hc_fix_print(v->as_fix, out);\n}\n\nconst struct hc_type HC_FIX = {\n .name = \"Fix\",\n .copy = NULL,\n .write = fix_write\n};\n\nstatic void int_write(const struct hc_value *v, struct hc_stream *out) {\n hc_printf(out, \"%d\", v->as_int);\n}\n\nconst struct hc_type HC_INT = {\n .name = \"Int\",\n .copy = NULL,\n .write = int_write\n};\n\nstatic void string_copy(struct hc_value *dst, struct hc_value *src) {\n dst->as_string = strdup(src->as_string);\n}\n\nstatic void string_deinit(struct hc_value *v) {\n free(v->as_string);\n}\n\nstatic void string_print(const struct hc_value *v, struct hc_stream *out) {\n hc_puts(out, v->as_string);\n}\n\nstatic void string_write(const struct hc_value *v, struct hc_stream *out) {\n hc_putc(out, '\"');\n string_print(v, out);\n hc_putc(out, '\"');\n}\n\nconst struct hc_type HC_STRING = {\n .name = \"String\",\n .copy = string_copy,\n .deinit = string_deinit,\n .print = string_print,\n .write = string_write\n};\n\nstatic void time_write(const struct hc_value *v, struct hc_stream *out) {\n hc_time_printf(&v->as_time, HC_TIME_FORMAT, out);\n}\n\nconst struct hc_type HC_TIME = {\n .name = \"Time\",\n .copy = NULL,\n .write = time_write\n};\n"], ["/hacktical-c/slog/tests.c", "#include \n#include \n\n#include \"slog.h\"\n#include \"malloc1/malloc1.h\"\n\nvoid slog_tests() {\n struct hc_memory_stream out;\n hc_memory_stream_init(&out, &hc_malloc_default);\n \n struct hc_slog_stream s;\n hc_slog_stream_init(&s, &out.stream, .close_out=true);\n \n hc_slog_do(&s) {\n hc_slog_context_do(hc_slog_string(\"string\", \"abc\")) {\n hc_time_t t = hc_time(2025, 4, 13, 1, 40, 0);\n \n hc_slog_write(hc_slog_bool(\"bool\", true),\n\t\t hc_slog_int(\"int\", 42),\n\t\t hc_slog_time(\"time\", t));\n }\n }\n\n assert(strcmp(\"string=\\\"abc\\\", \"\n\t\t\"bool=true, \"\n\t\t\"int=42, \"\n\t\t\"time=2025-04-13T01:40:00\\n\",\n\t\thc_memory_stream_string(&out)) == 0);\n \n hc_slog_deinit(&s); \n}\n"], ["/hacktical-c/set/tests.c", "#include \n#include \n#include \"set.h\"\n\nstruct map_item {\n int k, v;\n};\n\nstatic enum hc_order cmp(const void *x, const void *y) {\n return hc_cmp(*(const int *)x, *(const int *)y);\n}\n\nstatic const void *key(const void *x) {\n return &((const struct map_item *)x)->k;\n}\n\nvoid set_tests() {\n int n = 10;\n struct hc_set s;\n hc_set_init(&s, &hc_malloc_default, sizeof(struct map_item), cmp);\n s.key = key;\n \n for (int i = 0; i < n; i++) {\n struct map_item *it = hc_set_add(&s, &i, false);\n *it = (struct map_item){.k = i, .v = i};\n }\n\n assert(hc_set_length(&s) == n);\n \n for (int i = 0; i < n; i++) {\n struct map_item *it = hc_set_find(&s, &i);\n assert(it);\n assert(it->k == i);\n assert(it->v == i);\n }\n\n hc_set_clear(&s);\n assert(hc_set_length(&s) == 0); \n hc_set_deinit(&s);\n}\n"], ["/hacktical-c/fix/fix.c", "#include \n#include \n\n#include \"fix.h\"\n#include \"macro/macro.h\"\n#include \"stream1/stream1.h\"\n\nuint32_t hc_scale(const uint8_t exp) {\n static const uint32_t scale[HC_FIX_MAX_EXP+1] = {\n 1,\n 10,\n 100,\n 1000,\n 10000,\n 100000,\n 1000000,\n 10000000};\n\n assert(exp <= HC_FIX_MAX_EXP);\n return scale[exp];\n}\n\nhc_fix_t hc_fix(const uint8_t exp, const int64_t val) {\n return (hc_fix_t)hc_bitmask(exp, HC_FIX_EXP) +\n (hc_fix_t)(((val < 0) ? 1 : 0) << HC_FIX_EXP) +\n (hc_fix_t)(hc_abs(val) << HC_FIX_HDR);\n}\n\nuint8_t hc_fix_exp(const hc_fix_t x) {\n return hc_bitmask(x, HC_FIX_EXP);\n}\n\nint64_t hc_fix_val(const hc_fix_t x) {\n const int64_t v = x >> HC_FIX_HDR;\n return ((x >> HC_FIX_EXP) & 1) ? -v : v;\n}\n\nint64_t hc_fix_int(const hc_fix_t x) {\n return hc_fix_val(x) / hc_scale(hc_fix_exp(x));\n}\n\nint64_t hc_fix_frac(const hc_fix_t x) {\n const int64_t xv = hc_fix_val(x);\n const uint32_t xs = hc_scale(hc_fix_exp(x));\n return xv - (xv / xs) * xs;\n}\n\ndouble hc_fix_double(const hc_fix_t x) {\n return hc_fix_val(x) / (double)hc_scale(hc_fix_exp(x));\n}\n\nhc_fix_t hc_fix_add(const hc_fix_t x, const hc_fix_t y) {\n const uint8_t xe = hc_fix_exp(x);\n const uint8_t ye = hc_fix_exp(y);\n\n if (xe == ye) {\n return hc_fix(xe, hc_fix_val(x) + hc_fix_val(y));\n }\n \n return hc_fix(xe, hc_fix_val(x) +\n\t\thc_fix_val(y) * hc_scale(xe) / hc_scale(ye));\n}\n\nhc_fix_t hc_fix_sub(const hc_fix_t x, const hc_fix_t y) {\n const uint8_t xe = hc_fix_exp(x);\n const uint8_t ye = hc_fix_exp(y);\n\n if (xe == ye) {\n return hc_fix(xe, hc_fix_val(x) - hc_fix_val(y));\n }\n\n return hc_fix(xe, hc_fix_val(x) -\n\t\thc_fix_val(y) * hc_scale(xe) / hc_scale(ye));\n}\n\nhc_fix_t hc_fix_mul(const hc_fix_t x, const hc_fix_t y) {\n return hc_fix(hc_fix_exp(x), hc_fix_val(x) *\n\t\thc_fix_val(y) / hc_scale(hc_fix_exp(y)));\n}\n\nhc_fix_t hc_fix_div(const hc_fix_t x, const hc_fix_t y) {\n return hc_fix(hc_fix_exp(x), hc_fix_val(x) /\n\t\thc_fix_val(y) / hc_scale(hc_fix_exp(y)));\n}\n\nvoid hc_fix_print(const hc_fix_t v, struct hc_stream *out) {\n hc_printf(out,\n\t \"%\" PRId64 \".%\" PRId64,\n\t hc_fix_int(v),\n\t hc_fix_frac(v));\n}\n"], ["/hacktical-c/list/tests.c", "#include \n#include \"list.h\"\n\nstruct list_item {\n struct hc_list ls;\n int value;\n};\n \nvoid list_tests() {\n struct hc_list head;\n hc_list_init(&head);\n\n const int n = 10;\n struct list_item items[n];\n \n for (int i = 0; i < n; i++) {\n items[i].value = i;\n hc_list_push_back(&head, &items[i].ls);\n }\n\n int i = 0;\n \n hc_list_do(&head, il) {\n assert(hc_baseof(il, struct list_item, ls)->value == i++);\n }\n}\n"], ["/hacktical-c/task/task.c", "#include \n#include \n#include \"task.h\"\n\nstruct hc_task *hc_task_init(struct hc_task *t,\n\t\t\t struct hc_task_list *tl,\n\t\t\t hc_task_body body) {\n t->body = body;\n t->state = 0;\n t->done = false;\n hc_list_push_back(&tl->tasks, &t->list);\n return t;\n}\n\nstruct hc_task_list *hc_task_list_init(struct hc_task_list *tl) {\n hc_list_init(&tl->tasks);\n return tl;\n}\n\nvoid hc_task_list_run(const struct hc_task_list *tl) {\n bool all_done = false;\n \n while (!all_done) {\n all_done = true;\n \n hc_list_do(&tl->tasks, i) {\n struct hc_task *t = hc_baseof(i, struct hc_task, list);\n\n if (!t->done) {\n\tt->body(t);\n\tall_done = false;\n }\n }\n }\n}\n"], ["/hacktical-c/task/tests.c", "#include \n#include \n#include \"task.h\"\n\nstruct my_task {\n struct hc_task task;\n int *value;\n};\n\nstatic void producer(struct hc_task *task) {\n int *value = hc_baseof(task, struct my_task, task)->value;\n \n switch (task->state) {\n case 0:\n assert(*value == 0);\n (*value)++;\n hc_task_yield(task);\n assert(*value == 2);\n (*value)++;\n }\n \n task->done = true;\n}\n\nstatic void consumer(struct hc_task *task) {\n int *value = hc_baseof(task, struct my_task, task)->value;\n\n switch (task->state) {\n case 0:\n assert(*value == 1);\n (*value)++;\n hc_task_yield(task);\n assert(*value == 3);\n (*value)++;\n }\n \n task->done = true;\n}\n \nvoid task_tests() {\n struct hc_task_list tl;\n hc_task_list_init(&tl);\n \n int value = 0; \n\n struct my_task pt = {.value = &value};\n hc_task_init(&pt.task, &tl, &producer);\n\n struct my_task ct = {.value = &value};\n hc_task_init(&ct.task, &tl, &consumer);\n\n hc_task_list_run(&tl);\n assert(value == 4);\n}\n"], ["/hacktical-c/vector/tests.c", "#include \n#include \n#include \"vector.h\"\n\nvoid vector_tests() {\n struct hc_vector v;\n hc_vector_init(&v, &hc_malloc_default, sizeof(int));\n const int n = 100;\n \n for (int i = 0; i < n; i++) {\n *(int *)hc_vector_push(&v) = i;\n }\n\n {\n int i = 0;\n \n hc_vector_do(&v, it) {\n assert(*(int *)it == i++);\n }\n }\n \n assert(v.length == n);\n \n for (int i = 0; i < n; i++) {\n assert(*(int *)hc_vector_get(&v, i) == i);\n }\n\n assert(*(int *)hc_vector_pop(&v) == n-1);\n assert(*(int *)hc_vector_peek(&v) == n-2);\n assert(v.length == n-1);\n \n for (int i = 0; i < n-1; i++) {\n assert(*(int *)hc_vector_get(&v, i) == i);\n }\n\n assert(hc_vector_delete(&v, 0, 1));\n assert(v.length == n-2);\n\n for (int i = 1; i < n-1; i++) {\n assert(*(int *)hc_vector_get(&v, i-1) == i);\n }\n\n (*(int *)hc_vector_insert(&v, 0, 1) = 0);\n assert(v.length == n-1);\n\n for (int i = 0; i < n-1; i++) {\n assert(*(int *)hc_vector_get(&v, i) == i);\n }\n\n hc_vector_clear(&v);\n assert(v.length == 0);\n \n hc_vector_deinit(&v);\n}\n"], ["/hacktical-c/fix/benchmarks.c", "#include \"chrono/chrono.h\"\n#include \"fix.h\"\n\nvoid fix_benchmarks() {\n hc_time_t t;\n const int n = 100000;\n\n t = hc_now();\n double dv = 0;\n \n for (int i = 0; i < n; i++) {\n dv += 0.001;\n }\n\n hc_time_print(&t, \"double: \"); \n\n t = hc_now();\n hc_fix_t fv = hc_fix(3, 0);\n hc_fix_t fd = hc_fix(3, 1);\n \n for (int i = 0; i < n; i++) {\n fv = hc_fix_add(fv, fd);\n }\n\n hc_time_print(&t, \"fix: \"); \n}\n"], ["/hacktical-c/malloc1/tests.c", "#include \n#include \"malloc1.h\"\n\nvoid malloc1_tests() {\n assert(hc_align(0, 4) == 0);\n assert(hc_align(1, 4) == 4);\n assert(hc_align(3, 4) == 4);\n assert(hc_align(4, 4) == 4);\n assert(hc_align(5, 4) == 8);\n\n const int s = 1024;\n struct hc_bump_alloc a;\n hc_bump_alloc_init(&a, &hc_malloc_default, s);\n hc_defer(hc_bump_alloc_deinit(&a));\n assert(a.size == s);\n assert(a.offset == 0);\n\n int *ip = hc_acquire(&a.malloc, sizeof(int));\n *ip = 42;\n \n long *lp = hc_acquire(&a.malloc, sizeof(long));\n *lp = 42L;\n \n assert(a.offset >= sizeof(int) + sizeof(long));\n bool caught = false;\n \n void on_catch(struct hc_error *e) {\n assert(hc_streq(e->message, HC_NO_MEMORY) == 0);\n caught = true;\n }\n \n hc_catch(on_catch) {\n hc_acquire(&a.malloc, s);\n assert(false);\n }\n\n assert(caught);\n}\n"], ["/hacktical-c/stream1/tests.c", "#include \n#include \n#include \"stream1.h\"\n\nvoid stream1_tests() {\n struct hc_memory_stream s;\n hc_memory_stream_init(&s, &hc_malloc_default);\n hc_defer(hc_stream_deinit(&s.stream));\n hc_printf(&s.stream, \"%s%d\", \"foo\", 42);\n assert(strcmp(\"foo42\", hc_memory_stream_string(&s)) == 0);\n}\n"], ["/hacktical-c/malloc2/tests.c", "#include \n#include \"malloc2.h\"\n\nstatic void memo_tests() {\n struct hc_memo_alloc a;\n hc_memo_alloc_init(&a, &hc_malloc_default);\n\n int *ip1 = hc_acquire(&a.malloc, sizeof(int));\n \n long *lp = hc_acquire(&a.malloc, sizeof(long));\n assert((int *)lp != ip1);\n *lp = 42;\n \n hc_release(&a.malloc, ip1);\n int *ip2 = hc_acquire(&a.malloc, sizeof(int));\n assert(ip2 == ip1);\n *ip2 = 42;\n \n int *ip3 = hc_acquire(&a.malloc, sizeof(int));\n assert(ip3 != ip1);\n *ip3 = 42;\n \n hc_release(&a.malloc, lp);\n hc_release(&a.malloc, ip2);\n hc_release(&a.malloc, ip3);\n\n hc_memo_alloc_deinit(&a);\n}\n\nstatic void slab_tests() {\n struct hc_slab_alloc a;\n hc_slab_alloc_init(&a, &hc_malloc_default, 2 * sizeof(int));\n assert(a.slab_size == 2 * sizeof(int));\n\n const int *p1 = hc_acquire(&a.malloc, sizeof(int));\n const int *p2 = hc_acquire(&a.malloc, sizeof(int));\n assert(p2 == p1 + 1);\n\n const int *p3 = hc_acquire(&a.malloc, sizeof(int));\n assert(p3 > p2 + 1);\n \n const int *p4 = hc_acquire(&a.malloc, 10 * sizeof(int));\n assert(p4 > p3 + 1);\n\n hc_slab_alloc_deinit(&a);\n}\n\nvoid malloc2_tests() {\n memo_tests();\n slab_tests();\n}\n"], ["/hacktical-c/list/list.c", "#include \n#include \"list.h\"\n\nvoid hc_list_init(struct hc_list *l) {\n l->prev = l->next = l;\n}\n\nbool hc_list_nil(const struct hc_list *l) {\n return l->prev == l && l->next == l;\n}\n\nstruct hc_list *hc_list_delete(struct hc_list *l) {\n l->prev->next = l->next;\n l->next->prev = l->prev;\n return l;\n}\n\nvoid hc_list_push_front(struct hc_list *l, struct hc_list *it) {\n hc_list_push_back(l->next, it);\n}\n\nstruct hc_list *hc_list_pop_front(struct hc_list *l) {\n struct hc_list *it = l->next;\n return (it == l) ? NULL : hc_list_delete(it);\n}\n\nstruct hc_list *hc_list_peek_front(struct hc_list *l) {\n struct hc_list *it = l->next;\n return (it == l) ? NULL : it;\n}\n\nvoid hc_list_push_back(struct hc_list *l, struct hc_list *it) {\n it->prev = l->prev;\n l->prev->next = it;\n it->next = l;\n l->prev = it;\n}\n\nstruct hc_list *hc_list_pop_back(struct hc_list *l) {\n struct hc_list *it = l->prev;\n return (it == l) ? NULL : hc_list_delete(it);\n}\n\nstruct hc_list *hc_list_peek_back(struct hc_list *l) {\n struct hc_list *it = l->prev;\n return (it == l) ? NULL : it;\n}\n\nvoid hc_list_shift_back(struct hc_list *l) {\n l->next->prev = l->prev;\n l->prev->next = l->next;\n l->prev = l->next;\n l->next = l->next->next;\n l->prev->next = l;\n l->next->prev = l;\n}\n"], ["/hacktical-c/macro/tests.c", "#include \n#include \"macro.h\"\n\nvoid macro_tests() {\n int hc_id(foo, bar) = 42;\n assert(foobar == 42);\n\n assert(hc_min(7, 42) == 7);\n assert(hc_max(7.0, 42.0) == 42.0);\n\n {\n int foo = 0;\n \n {\n hc_defer(assert(foo++ == 1));\n hc_defer(assert(foo++ == 0));\n }\n\n assert(foo == 2);\n }\n}\n"], ["/hacktical-c/reflect/tests.c", "#include \n#include \"reflect.h\"\n\nvoid reflect_tests() {\n struct hc_value v;\n hc_value_init(&v, &HC_STRING)->as_string = strdup(\"foo\");\n hc_defer(hc_value_deinit(&v));\n struct hc_value c;\n hc_value_copy(&c, &v);\n hc_defer(hc_value_deinit(&c));\n assert(strcmp(c.as_string, v.as_string) == 0);\n}\n"], ["/hacktical-c/fix/tests.c", "#include \n#include \n#include \n#include \"fix.h\"\n\nstatic void test_add() {\n assert(hc_fix_add(hc_fix(2, 175), hc_fix(2, 25)) ==\n\t hc_fix(2, 200));\n \n assert(hc_fix_add(hc_fix(2, 175), hc_fix(2, -25)) ==\n\t hc_fix(2, 150));\n}\n\nstatic void test_div() {\n assert(hc_fix_div(hc_fix(2, 150), hc_fix(0, 2)) ==\n\t hc_fix(2, 75));\n\n assert(hc_fix_div(hc_fix(2, 150), hc_fix(0, -2)) ==\n\t hc_fix(2, -75));\n\n assert(hc_fix_div(hc_fix(2, -150), hc_fix(0, -2)) ==\n\t hc_fix(2, 75));\n}\n\nstatic void test_mul() {\n assert(hc_fix_mul(hc_fix(2, 150), hc_fix(1, 5)) ==\n\t hc_fix(2, 75));\n\n assert(hc_fix_mul(hc_fix(2, 150), hc_fix(1, -5)) ==\n\t hc_fix(2, -75));\n\n assert(hc_fix_mul(hc_fix(2, -150), hc_fix(1, -5)) ==\n\t hc_fix(2, 75));\n}\n\nstatic void test_new() {\n hc_fix_t x = hc_fix(2, -125);\n assert(hc_fix_exp(x) == 2);\n assert(hc_fix_val(x) == -125);\n assert(hc_fix_int(x) == -1);\n assert(hc_fix_frac(x) == -25);\n assert(hc_fix_double(x) == -1.25);\n}\n\nstatic void test_sub() {\n assert(hc_fix_sub(hc_fix(2, 175), hc_fix(2, 25)) ==\n\t hc_fix(2, 150));\n\n assert(hc_fix_sub(hc_fix(2, 175), hc_fix(2, -25)) ==\n\t hc_fix(2, 200));\n}\n\nvoid fix_tests() {\n test_add();\n test_div();\n test_mul();\n test_new();\n test_sub();\n}\n"], ["/hacktical-c/macro/macro.c", "#include \"macro.h\"\n\nsize_t hc_alignof(size_t size) {\n const size_t max = _Alignof(max_align_t);\n if (size >= max) { return max; }\n size_t v = 1;\n for (size_t nv = 1; nv <= size; v = nv, nv = v << 1);\n return v;\n}\n"], ["/hacktical-c/vm/tests.c", "#include \n\n#include \"vm.h\"\n#include \"malloc1/malloc1.h\"\n\nstatic void emit_tests() {\n struct hc_vm vm;\n hc_vm_init(&vm, &hc_malloc_default);\n hc_defer(hc_vm_deinit(&vm));\n struct hc_push_op op;\n hc_value_init(&op.value, &HC_FIX)->as_fix = hc_fix(0, 42);\n hc_vm_emit(&vm, &HC_PUSH, &op);\n hc_vm_eval(&vm, 0, -1);\n assert(vm.stack.length == 1);\n assert(hc_vm_pop(&vm)->as_fix == op.value.as_fix);\n}\n\nvoid vm_tests() {\n emit_tests();\n}\n"], ["/hacktical-c/tests.c", "#include \"error/error.h\"\n\n#include \"chrono/tests.c\"\n#include \"dsl/tests.c\"\n#include \"dynamic/tests.c\"\n#include \"error/tests.c\"\n#include \"fix/tests.c\"\n#include \"list/tests.c\"\n#include \"macro/tests.c\"\n#include \"malloc1/tests.c\"\n#include \"malloc2/tests.c\"\n#include \"reflect/tests.c\"\n#include \"set/tests.c\"\n#include \"slog/tests.c\"\n#include \"stream1/tests.c\"\n#include \"task/tests.c\"\n#include \"vector/tests.c\"\n#include \"vm/tests.c\"\n\nint main() {\n chrono_tests();\n dsl_tests();\n dynamic_tests();\n error_tests();\n fix_tests();\n list_tests();\n macro_tests();\n malloc1_tests();\n malloc2_tests();\n reflect_tests();\n set_tests();\n slog_tests();\n stream1_tests();\n task_tests();\n vector_tests();\n vm_tests();\n\n hc_errors_deinit();\n return 0;\n}\n"], ["/hacktical-c/chrono/tests.c", "#include \n#include \"chrono.h\"\n\nvoid chrono_tests() {\n hc_time_t t = hc_now();\n const int ns = 1000;\n assert(hc_sleep(ns) == 0);\n assert(hc_time_ns(&t) >= ns);\n}\n"], ["/hacktical-c/benchmarks.c", "#include \"error/error.h\"\n\n#include \"dsl/benchmarks.c\"\n#include \"fix/benchmarks.c\"\n#include \"malloc2/benchmarks.c\"\n\nint main() {\n fix_benchmarks();\n malloc2_benchmarks();\n dsl_benchmarks();\n\n hc_errors_deinit();\n return 0;\n}\n"]], "task_instance_info": {"created_time": "2025-08-20 17:57:20", "created_task_model": "DeepSeek-R1", "function_skeleton": "struct hc_proc *_hc_proc_init(struct hc_proc *p, char *cmd[]) {}", "code_description": "This code segment functions to: Create and configure a child process with redirected standard input using inter-process communication channels, while handling error conditions during process creation and resource management."}, "inference_content": {"inference_model": "DeepSeek-R1", "inference_result": "```c\n[TASK_BEGIN]\nstruct hc_proc *_hc_proc_init(struct hc_proc *p, char *cmd[]) {\n int pipefd[2];\n if (pipe2(pipefd, O_CLOEXEC) == -1) {\n hc_throw(\"Failed creating pipe: %d\", errno);\n }\n\n pid_t pid = fork();\n if (pid == -1) {\n close(pipefd[0]);\n close(pipefd[1]);\n hc_throw(\"Failed forking process: %d\", errno);\n }\n\n if (pid == 0) {\n // Child process\n close(pipefd[1]);\n dup2(pipefd[0], STDIN_FILENO);\n close(pipefd[0]);\n\n execvp(cmd[0], cmd);\n hc_throw(\"Failed executing command: %d\", errno);\n _exit(EXIT_FAILURE);\n }\n\n // Parent process\n close(pipefd[0]);\n p->pid = pid;\n p->in = pipefd[1];\n return p;\n}\n[TASK_END]\n```", "inference_time": "2025-08-20 17-58-15"}, "editdistance_info": {"edit_distance": 39.1941, "calculate_time": "2025-08-20 17:58:15", "true_code_clean": "struct hc_proc *_hc_proc_init(struct hc_proc *p, char *cmd[]) {\n int fds[2];\n if (pipe(fds) == -1) {\n hc_throw(\"Failed creating pipe: %d\", errno);\n }\n pid_t child_pid = fork();\n switch (child_pid) {\n case 0: {\n if (close(fds[1]) == -1) {\n hc_throw(\"Failed closing pipe writer: %d\", errno);\n }\n if (dup2(fds[0], 0) == -1) {\n hc_throw(\"Failed rebinding stdin: %d\", errno);\n }\n char *const env[] = {\"PATH=/bin:/sbin\", NULL};\n if (execve(cmd[0], cmd, env) == -1) {\n hc_throw(\"Failed to execve '%s': %d\", cmd[0], errno);\n }\n }\n case -1:\n hc_throw(\"Failed forking process: %d\", errno);\n default:\n if (close(fds[0]) == -1) {\n hc_throw(\"Failed closing pipe reader: %d\", errno);\n }\n p->pid = child_pid;\n p->in = fds[1];\n break;\n }\n return p;\n}", "predict_code_clean": "struct hc_proc *_hc_proc_init(struct hc_proc *p, char *cmd[]) {\n int pipefd[2];\n if (pipe2(pipefd, O_CLOEXEC) == -1) {\n hc_throw(\"Failed creating pipe: %d\", errno);\n }\n pid_t pid = fork();\n if (pid == -1) {\n close(pipefd[0]);\n close(pipefd[1]);\n hc_throw(\"Failed forking process: %d\", errno);\n }\n if (pid == 0) {\n close(pipefd[1]);\n dup2(pipefd[0], STDIN_FILENO);\n close(pipefd[0]);\n execvp(cmd[0], cmd);\n hc_throw(\"Failed executing command: %d\", errno);\n _exit(EXIT_FAILURE);\n }\n close(pipefd[0]);\n p->pid = pid;\n p->in = pipefd[1];\n return p;\n}"}} {"repo_name": "hacktical-c", "file_name": "/hacktical-c/fix/benchmarks.c", "inference_info": {"prefix_code": "#include \"chrono/chrono.h\"\n#include \"fix.h\"\n\n", "suffix_code": "\n", "middle_code": "void fix_benchmarks() {\n hc_time_t t;\n const int n = 100000;\n t = hc_now();\n double dv = 0;\n for (int i = 0; i < n; i++) {\n dv += 0.001;\n }\n hc_time_print(&t, \"double: \"); \n t = hc_now();\n hc_fix_t fv = hc_fix(3, 0);\n hc_fix_t fd = hc_fix(3, 1);\n for (int i = 0; i < n; i++) {\n fv = hc_fix_add(fv, fd);\n }\n hc_time_print(&t, \"fix: \"); \n}", "code_description": null, "fill_type": "FUNCTION_TYPE", "language_type": "c", "sub_task_type": null}, "context_code": [["/hacktical-c/dsl/benchmarks.c", "#include \"chrono/chrono.h\"\n#include \"dsl.h\"\n\nvoid dsl_benchmarks() {\n hc_time_t t;\n const int n = 100000;\n\n char buf[32];\n t = hc_now();\n \n for (int i = 0; i < n; i++) {\n sprintf(buf, \"abc %s def\", \"ghi\");\n }\n\n hc_time_print(&t, \"sprintf: \"); \n \n struct hc_dsl dsl;\n hc_dsl_init(&dsl, &hc_malloc_default);\n hc_defer(hc_dsl_deinit(&dsl));\n struct hc_memory_stream out;\n hc_memory_stream_init(&out, &hc_malloc_default);\n hc_defer(hc_stream_deinit(&out.stream));\n dsl.out = &out.stream;\n hc_dsl_set_string(&dsl, \"foo\", \"ghi\");\n hc_dsl_eval(&dsl, \"abc $(print foo) def\");\n\n t = hc_now();\n\n for (int i = 0; i < n; i++) {\n hc_vector_clear(&out.data);\n hc_vm_eval(&dsl.vm, 0, -1);\n }\n\n hc_time_print(&t, \"dsl: \");\n}\n"], ["/hacktical-c/malloc2/benchmarks.c", "#include \n\n#include \"chrono/chrono.h\"\n#include \"malloc2.h\"\n\n#define N 1000\n#define MAX_SIZE 64\n#define GET_SIZE() ((rand() % MAX_SIZE) + 1)\n\nstatic void run_malloc() {\n int *ps[N];\n hc_time_t t = hc_now();\n \n for (int i = 0; i < N; i++) {\n ps[i] = malloc(GET_SIZE());\n }\n\n for (int i = 0; i < N; i++) {\n free(ps[i]);\n }\n\n hc_time_print(&t, \"malloc: \"); \n}\n\nstatic void run_bump() {\n struct hc_bump_alloc a;\n hc_bump_alloc_init(&a, &hc_malloc_default, N * MAX_SIZE);\n hc_time_t t = hc_now();\n\n for (int i = 0; i < N; i++) {\n hc_acquire(&a.malloc, GET_SIZE());\n }\n\n hc_bump_alloc_deinit(&a);\n hc_time_print(&t, \"bump: \");\n}\n\nstatic void run_slab() {\n struct hc_slab_alloc a;\n hc_slab_alloc_init(&a, &hc_malloc_default, N);\n hc_time_t t = hc_now();\n\n for (int i = 0; i < N; i++) {\n hc_acquire(&a.malloc, GET_SIZE());\n }\n\n hc_slab_alloc_deinit(&a);\n hc_time_print(&t, \"slab: \");\n}\n\nvoid malloc2_benchmarks() {\n const int s = time(NULL);\n \n srand(s);\n run_malloc();\n\n srand(s);\n run_bump();\n\n srand(s);\n run_slab();\n}\n"], ["/hacktical-c/chrono/chrono.c", "#include \n#include \n#include \n#include \n\n#include \"chrono.h\"\n#include \"error/error.h\"\n#include \"stream1/stream1.h\"\n\nhc_time_t hc_now() {\n hc_time_t t;\n \n if (!timespec_get(&t.value, TIME_UTC)) {\n hc_throw(\"Failed getting time: %d\", errno);\n }\n\n return t;\n}\n\nhc_time_t hc_time(int year,\n\t\t int month,\n\t\t int day,\n\t\t int hour,\n\t\t int minute,\n\t\t int second) {\n struct tm t = {0};\n t.tm_year = year - 1900;\n t.tm_mon = month - 1;\n t.tm_mday = day;\n t.tm_hour = hour;\n t.tm_min = minute;\n t.tm_sec = second;\n\n hc_time_t result = {0};\n result.value.tv_sec = timegm(&t);\n return result;\n}\n\nuint64_t hc_time_ns(const hc_time_t *t) {\n const struct timespec now = hc_now().value;\n \n return\n (now.tv_sec - t->value.tv_sec) * 1000000000 +\n (now.tv_nsec - t->value.tv_nsec);\n}\n\nvoid hc_time_print(const hc_time_t *t, const char *m) {\n printf(\"%s%\" PRIu64 \"ns\\n\", m, hc_time_ns(t));\n}\n\nchar *hc_time_sprintf(const hc_time_t *t, const char *spec) {\n struct tm tm;\n gmtime_r(&(t->value.tv_sec), &tm);\n size_t len = 8;\n char *result = malloc(len);\n\n for (;;) {\n const size_t n = strftime(result, len, spec, &tm);\n\n if (n) {\n result[n] = 0;\n break;\n }\n \n len *= 2;\n free(result);\n result = malloc(len);\n }\n \n return result;\n}\n\nvoid hc_time_printf(const hc_time_t *t,\n\t\t const char *spec,\n\t\t struct hc_stream *out) {\n char *s = hc_time_sprintf(t, \"%Y-%m-%dT%H:%M:%S\");\n hc_puts(out, s);\n free(s);\n}\n\nuint64_t hc_sleep(uint64_t ns) {\n struct timespec t = {0};\n t.tv_nsec = ns;\n\n switch (nanosleep(&t, &t)) {\n case 0:\n break;\n case EINTR:\n return t.tv_nsec;\n default:\n hc_throw(\"Failed sleeping: %d\", errno);\n }\n\n return 0;\n}\n"], ["/hacktical-c/dynamic/dynamic.c", "#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#include \"dynamic.h\"\n#include \"error/error.h\"\n#include \"malloc1/malloc1.h\"\n#include \"vector/vector.h\"\n\nstruct hc_proc *_hc_proc_init(struct hc_proc *p, char *cmd[]) {\n int fds[2];\n\n if (pipe(fds) == -1) {\n hc_throw(\"Failed creating pipe: %d\", errno);\n }\n \n pid_t child_pid = fork();\n\n switch (child_pid) {\n case 0: {\n if (close(fds[1]) == -1) {\n hc_throw(\"Failed closing pipe writer: %d\", errno);\n }\n \n if (dup2(fds[0], 0) == -1) {\n hc_throw(\"Failed rebinding stdin: %d\", errno);\n }\n \n char *const env[] = {\"PATH=/bin:/sbin\", NULL};\n\n if (execve(cmd[0], cmd, env) == -1) {\n hc_throw(\"Failed to execve '%s': %d\", cmd[0], errno);\n }\n }\n case -1:\n hc_throw(\"Failed forking process: %d\", errno);\n default:\n if (close(fds[0]) == -1) {\n hc_throw(\"Failed closing pipe reader: %d\", errno);\n }\n \n p->pid = child_pid;\n p->in = fds[1];\n break;\n }\n\n return p;\n}\n\nstatic void close_in(struct hc_proc *p) {\n if (p->in != -1 && close(p->in) == -1) {\n hc_throw(\"Failed closing stdin: %d\", errno);\n }\n}\n\nvoid hc_proc_wait(struct hc_proc *p) {\n close_in(p);\n\n if (waitpid(p->pid, NULL, 0) == -1) {\n hc_throw(\"Failed waiting for child process to exit: %d\", errno);\n }\n}\n\nvoid hc_proc_deinit(struct hc_proc *p) {\n close_in(p);\n}\n\nstatic void free_cmd(char **in) {\n for (char **s = in; *s; s++) {\n free(*s);\n }\n}\n\nvoid _hc_compile(const char *code,\n\t\t const char *out,\n\t\t const struct hc_compile_opts opts) {\n hc_array(const char *, pre, \n\t opts.cc, \"-shared\", \"-fpic\", \"-o\", out, \"-xc\");\n \n int n = pre_n + 2;\n for (int i = 0; opts.cflags[i]; i++, n++); \n char *cmd[n];\n int i = 0;\n\n for (; i < pre_n; i++) {\n cmd[i] = strdup(pre_a[i]);\n }\n \n for (; i < n - 2; i++) {\n cmd[i] = strdup(opts.cflags[i - pre_n]);\n }\n\n cmd[i++] = strdup(\"-\");\n cmd[i] = NULL;\n hc_defer(free_cmd(cmd));\n \n struct hc_proc child;\n _hc_proc_init(&child, cmd);\n hc_defer(hc_proc_deinit(&child));\n FILE *in = fdopen(child.in, \"w\");\n\n if (!in) {\n hc_throw(\"Failed opening stdin stream: %d\", errno);\n }\n \n child.in = -1;\n hc_defer(hc_proc_wait(&child));\n hc_defer(fclose(in));\n\n if (fputs(code, in) == EOF) {\n hc_throw(\"Failed writing code: %d\", errno);\n }\n}\n\nstruct hc_dlib *hc_dlib_init(struct hc_dlib *lib, const char *path) {\n lib->handle = dlopen(path, RTLD_NOW);\n\n if (!lib->handle) {\n hc_throw(\"Error opening dynamic library '%s': %s\", path, dlerror());\n }\n \n return lib;\n}\n\nstruct hc_dlib *hc_dlib_deinit(struct hc_dlib *lib) {\n if (dlclose(lib->handle) != 0) {\n hc_throw(\"Failed closing dynamic library: \", dlerror());\n }\n\n return lib;\n}\n\nvoid *hc_dlib_find(const struct hc_dlib *lib, const char *s) {\n dlerror();\n void *v = dlsym(lib->handle, s);\n char *e = dlerror();\n\n if (e) {\n hc_throw(\"Symbol '%s' not found: %s\", e);\n }\n\n return v;\n}\n\nchar *hc_vsprintf(const char *format, va_list args) {\n va_list tmp_args;\n va_copy(tmp_args, args);\n int len = vsnprintf(NULL, 0, format, tmp_args);\n va_end(tmp_args);\n\n if (len < 0) {\n hc_throw(\"Formatting '%s' failed: %d\", format, errno);\n }\n\n len++;\n char *out = malloc(len);\n vsnprintf(out, len, format, args);\n return out;\n} \n"], ["/hacktical-c/vector/tests.c", "#include \n#include \n#include \"vector.h\"\n\nvoid vector_tests() {\n struct hc_vector v;\n hc_vector_init(&v, &hc_malloc_default, sizeof(int));\n const int n = 100;\n \n for (int i = 0; i < n; i++) {\n *(int *)hc_vector_push(&v) = i;\n }\n\n {\n int i = 0;\n \n hc_vector_do(&v, it) {\n assert(*(int *)it == i++);\n }\n }\n \n assert(v.length == n);\n \n for (int i = 0; i < n; i++) {\n assert(*(int *)hc_vector_get(&v, i) == i);\n }\n\n assert(*(int *)hc_vector_pop(&v) == n-1);\n assert(*(int *)hc_vector_peek(&v) == n-2);\n assert(v.length == n-1);\n \n for (int i = 0; i < n-1; i++) {\n assert(*(int *)hc_vector_get(&v, i) == i);\n }\n\n assert(hc_vector_delete(&v, 0, 1));\n assert(v.length == n-2);\n\n for (int i = 1; i < n-1; i++) {\n assert(*(int *)hc_vector_get(&v, i-1) == i);\n }\n\n (*(int *)hc_vector_insert(&v, 0, 1) = 0);\n assert(v.length == n-1);\n\n for (int i = 0; i < n-1; i++) {\n assert(*(int *)hc_vector_get(&v, i) == i);\n }\n\n hc_vector_clear(&v);\n assert(v.length == 0);\n \n hc_vector_deinit(&v);\n}\n"], ["/hacktical-c/fix/fix.c", "#include \n#include \n\n#include \"fix.h\"\n#include \"macro/macro.h\"\n#include \"stream1/stream1.h\"\n\nuint32_t hc_scale(const uint8_t exp) {\n static const uint32_t scale[HC_FIX_MAX_EXP+1] = {\n 1,\n 10,\n 100,\n 1000,\n 10000,\n 100000,\n 1000000,\n 10000000};\n\n assert(exp <= HC_FIX_MAX_EXP);\n return scale[exp];\n}\n\nhc_fix_t hc_fix(const uint8_t exp, const int64_t val) {\n return (hc_fix_t)hc_bitmask(exp, HC_FIX_EXP) +\n (hc_fix_t)(((val < 0) ? 1 : 0) << HC_FIX_EXP) +\n (hc_fix_t)(hc_abs(val) << HC_FIX_HDR);\n}\n\nuint8_t hc_fix_exp(const hc_fix_t x) {\n return hc_bitmask(x, HC_FIX_EXP);\n}\n\nint64_t hc_fix_val(const hc_fix_t x) {\n const int64_t v = x >> HC_FIX_HDR;\n return ((x >> HC_FIX_EXP) & 1) ? -v : v;\n}\n\nint64_t hc_fix_int(const hc_fix_t x) {\n return hc_fix_val(x) / hc_scale(hc_fix_exp(x));\n}\n\nint64_t hc_fix_frac(const hc_fix_t x) {\n const int64_t xv = hc_fix_val(x);\n const uint32_t xs = hc_scale(hc_fix_exp(x));\n return xv - (xv / xs) * xs;\n}\n\ndouble hc_fix_double(const hc_fix_t x) {\n return hc_fix_val(x) / (double)hc_scale(hc_fix_exp(x));\n}\n\nhc_fix_t hc_fix_add(const hc_fix_t x, const hc_fix_t y) {\n const uint8_t xe = hc_fix_exp(x);\n const uint8_t ye = hc_fix_exp(y);\n\n if (xe == ye) {\n return hc_fix(xe, hc_fix_val(x) + hc_fix_val(y));\n }\n \n return hc_fix(xe, hc_fix_val(x) +\n\t\thc_fix_val(y) * hc_scale(xe) / hc_scale(ye));\n}\n\nhc_fix_t hc_fix_sub(const hc_fix_t x, const hc_fix_t y) {\n const uint8_t xe = hc_fix_exp(x);\n const uint8_t ye = hc_fix_exp(y);\n\n if (xe == ye) {\n return hc_fix(xe, hc_fix_val(x) - hc_fix_val(y));\n }\n\n return hc_fix(xe, hc_fix_val(x) -\n\t\thc_fix_val(y) * hc_scale(xe) / hc_scale(ye));\n}\n\nhc_fix_t hc_fix_mul(const hc_fix_t x, const hc_fix_t y) {\n return hc_fix(hc_fix_exp(x), hc_fix_val(x) *\n\t\thc_fix_val(y) / hc_scale(hc_fix_exp(y)));\n}\n\nhc_fix_t hc_fix_div(const hc_fix_t x, const hc_fix_t y) {\n return hc_fix(hc_fix_exp(x), hc_fix_val(x) /\n\t\thc_fix_val(y) / hc_scale(hc_fix_exp(y)));\n}\n\nvoid hc_fix_print(const hc_fix_t v, struct hc_stream *out) {\n hc_printf(out,\n\t \"%\" PRId64 \".%\" PRId64,\n\t hc_fix_int(v),\n\t hc_fix_frac(v));\n}\n"], ["/hacktical-c/set/tests.c", "#include \n#include \n#include \"set.h\"\n\nstruct map_item {\n int k, v;\n};\n\nstatic enum hc_order cmp(const void *x, const void *y) {\n return hc_cmp(*(const int *)x, *(const int *)y);\n}\n\nstatic const void *key(const void *x) {\n return &((const struct map_item *)x)->k;\n}\n\nvoid set_tests() {\n int n = 10;\n struct hc_set s;\n hc_set_init(&s, &hc_malloc_default, sizeof(struct map_item), cmp);\n s.key = key;\n \n for (int i = 0; i < n; i++) {\n struct map_item *it = hc_set_add(&s, &i, false);\n *it = (struct map_item){.k = i, .v = i};\n }\n\n assert(hc_set_length(&s) == n);\n \n for (int i = 0; i < n; i++) {\n struct map_item *it = hc_set_find(&s, &i);\n assert(it);\n assert(it->k == i);\n assert(it->v == i);\n }\n\n hc_set_clear(&s);\n assert(hc_set_length(&s) == 0); \n hc_set_deinit(&s);\n}\n"], ["/hacktical-c/slog/slog.c", "#include \n#include \n#include \n#include \n\n#include \"error/error.h\"\n#include \"macro/macro.h\"\n#include \"malloc1/malloc1.h\"\n#include \"slog.h\"\n\n__thread struct hc_slog *_hc_slog = NULL;\n\nstruct hc_slog *hc_slog() {\n if (_hc_slog != NULL) {\n return _hc_slog;\n }\n \n static __thread bool init = true;\n static __thread struct hc_slog_stream s;\n\n if (init) {\n hc_slog_stream_init(&s, hc_stdout());\n init = false;\n }\n\n return &s.slog;\n}\n\nstatic void field_deinit(struct hc_slog_field *f) {\n free(f->name);\n hc_value_deinit(&f->value);\n}\n\nstatic void slog_write(struct hc_slog *s,\n\t\t const size_t n,\n\t\t struct hc_slog_field *fields[]) {\n assert(s->write);\n s->write(s, n, fields);\n}\n\nvoid __hc_slog_write(struct hc_slog *s,\n\t\t const size_t n,\n\t\t struct hc_slog_field *fields[]) {\n slog_write(s, n, fields);\n \n for(size_t i = 0; i < n; i++) {\n struct hc_slog_field *f = fields[i];\n field_deinit(f);\n free(f);\n }\n}\n\nvoid _hc_slog_deinit(struct hc_slog *s) {\n if (s->deinit) {\n s->deinit(s);\n }\n}\n\nstatic struct hc_value *field_init(struct hc_slog_field *f,\n\t\t\t\t const char *name,\n\t\t\t\t const struct hc_type *type) {\n f->name = strdup(name);\n hc_value_init(&f->value, type);\n return &f->value;\n}\n\nstruct hc_slog_field *hc_slog_bool(const char *name, const bool value) {\n struct hc_slog_field *f = malloc(sizeof(struct hc_slog_field));\n field_init(f, name, &HC_BOOL)->as_bool = value;\n return f;\n}\n\nstruct hc_slog_field *hc_slog_int(const char *name, const int value) {\n struct hc_slog_field *f = malloc(sizeof(struct hc_slog_field));\n field_init(f, name, &HC_INT)->as_int = value;\n return f;\n}\n\nstruct hc_slog_field *hc_slog_string(const char *name, const char *value) {\n struct hc_slog_field *f = malloc(sizeof(struct hc_slog_field));\n field_init(f, name, &HC_STRING)->as_string = strdup(value);\n return f;\n}\n\nstruct hc_slog_field *hc_slog_time(const char *name, const hc_time_t value) {\n struct hc_slog_field *f = malloc(sizeof(struct hc_slog_field));\n field_init(f, name, &HC_TIME)->as_time = value;\n return f;\n}\n\nvoid stream_deinit(struct hc_slog *s) {\n struct hc_slog_stream *ss = hc_baseof(s, struct hc_slog_stream, slog);\n if (ss->opts.close_out) { hc_stream_deinit(ss->out); }\n}\n\nstatic void field_write(struct hc_slog_field *f, struct hc_stream *out) {\n hc_puts(out, f->name);\n hc_putc(out, '=');\n hc_value_write(&f->value, out);\n}\n\nstatic void stream_write(struct hc_slog *s,\n\t\t\t const size_t n,\n\t\t\t struct hc_slog_field *fields[]) {\n struct hc_slog_stream *ss = hc_baseof(s, struct hc_slog_stream, slog);\n\n for(size_t i = 0; i < n; i++) {\n struct hc_slog_field *f = fields[i];\n if (i) { hc_puts(ss->out, \", \"); }\n field_write(f, ss->out);\n }\n\n hc_putc(ss->out, '\\n');\n}\n\nstruct hc_slog_stream *_hc_slog_stream_init(struct hc_slog_stream *s,\n\t\t\t\t\t struct hc_stream *out,\n\t\t\t\t\t const struct hc_slog_stream_opts opts) {\n s->slog.deinit = stream_deinit;\n s->slog.write = stream_write;\n s->out = out;\n s->opts = opts;\n return s;\n}\n\nstatic void context_deinit(struct hc_slog *s) {\n struct hc_slog_context *sc = hc_baseof(s, struct hc_slog_context, slog);\n\n for (size_t i = 0; i < sc->length; i++) {\n struct hc_slog_field *f = sc->fields[i];\n field_deinit(f);\n free(f);\n }\n\n free(sc->fields);\n}\n\nstatic void context_write(struct hc_slog *s,\n\t\t\t const size_t n,\n\t\t\t struct hc_slog_field *fields[]) {\n struct hc_slog_context *c = hc_baseof(s, struct hc_slog_context, slog);\n struct hc_slog_field *fs[c->length + n];\n memcpy(fs, c->fields, sizeof(struct hc_slog_field *) * c->length);\n memcpy(fs + c->length, fields, sizeof(struct hc_slog_field *) * n);\n slog_write(c->parent, c->length + n, fs);\n}\n\nstruct hc_slog_context *hc_slog_context_init(struct hc_slog_context *c,\n\t\t\t\t\t size_t length,\n\t\t\t\t\t struct hc_slog_field *fields[]) {\n c->slog.deinit = context_deinit;\n c->slog.write = context_write;\n c->parent = hc_slog();\n c->length = length;\n size_t s = sizeof(struct hc_slog_field *) * length;\n c->fields = malloc(s);\n memcpy(c->fields, fields, s);\n return c;\n}\n"], ["/hacktical-c/fix/tests.c", "#include \n#include \n#include \n#include \"fix.h\"\n\nstatic void test_add() {\n assert(hc_fix_add(hc_fix(2, 175), hc_fix(2, 25)) ==\n\t hc_fix(2, 200));\n \n assert(hc_fix_add(hc_fix(2, 175), hc_fix(2, -25)) ==\n\t hc_fix(2, 150));\n}\n\nstatic void test_div() {\n assert(hc_fix_div(hc_fix(2, 150), hc_fix(0, 2)) ==\n\t hc_fix(2, 75));\n\n assert(hc_fix_div(hc_fix(2, 150), hc_fix(0, -2)) ==\n\t hc_fix(2, -75));\n\n assert(hc_fix_div(hc_fix(2, -150), hc_fix(0, -2)) ==\n\t hc_fix(2, 75));\n}\n\nstatic void test_mul() {\n assert(hc_fix_mul(hc_fix(2, 150), hc_fix(1, 5)) ==\n\t hc_fix(2, 75));\n\n assert(hc_fix_mul(hc_fix(2, 150), hc_fix(1, -5)) ==\n\t hc_fix(2, -75));\n\n assert(hc_fix_mul(hc_fix(2, -150), hc_fix(1, -5)) ==\n\t hc_fix(2, 75));\n}\n\nstatic void test_new() {\n hc_fix_t x = hc_fix(2, -125);\n assert(hc_fix_exp(x) == 2);\n assert(hc_fix_val(x) == -125);\n assert(hc_fix_int(x) == -1);\n assert(hc_fix_frac(x) == -25);\n assert(hc_fix_double(x) == -1.25);\n}\n\nstatic void test_sub() {\n assert(hc_fix_sub(hc_fix(2, 175), hc_fix(2, 25)) ==\n\t hc_fix(2, 150));\n\n assert(hc_fix_sub(hc_fix(2, 175), hc_fix(2, -25)) ==\n\t hc_fix(2, 200));\n}\n\nvoid fix_tests() {\n test_add();\n test_div();\n test_mul();\n test_new();\n test_sub();\n}\n"], ["/hacktical-c/list/tests.c", "#include \n#include \"list.h\"\n\nstruct list_item {\n struct hc_list ls;\n int value;\n};\n \nvoid list_tests() {\n struct hc_list head;\n hc_list_init(&head);\n\n const int n = 10;\n struct list_item items[n];\n \n for (int i = 0; i < n; i++) {\n items[i].value = i;\n hc_list_push_back(&head, &items[i].ls);\n }\n\n int i = 0;\n \n hc_list_do(&head, il) {\n assert(hc_baseof(il, struct list_item, ls)->value == i++);\n }\n}\n"], ["/hacktical-c/vector/vector.c", "#include \n#include \n#include \n#include \n#include \"vector.h\"\n#include \"malloc1/malloc1.h\"\n\nstatic void grow(struct hc_vector *v) {\n hc_vector_grow(v, v->capacity ? v->capacity*2 : 2);\n}\n\nstruct hc_vector *hc_vector_init(struct hc_vector *v,\n\t\t\t\t struct hc_malloc *malloc,\n\t\t\t\t const size_t item_size) {\n v->malloc = malloc;\n v->item_size = item_size;\n v->capacity = 0;\n v->length = 0;\n v->start = v->end = NULL;\n return v;\n}\n\nvoid hc_vector_deinit(struct hc_vector *v) {\n if (v->start) { hc_release(v->malloc, v->start); }\n}\n\nvoid hc_vector_grow(struct hc_vector *v, const size_t capacity) {\n v->capacity = capacity; \n size_t size = v->item_size * (v->capacity+1);\n uint8_t *new_start = hc_acquire(v->malloc, size);\n\n if (v->start) {\n memmove(new_start, v->start, v->length * v->item_size);\n hc_release(v->malloc, v->start); \n }\n \n v->start = new_start;\n v->end = v->start + v->item_size*v->length;\n}\n\nvoid hc_vector_clear(struct hc_vector *v) {\n v->length = 0;\n v->end = v->start;\n}\n\nvoid *hc_vector_get(struct hc_vector *v, const size_t i) {\n return v->start ? v->start + v->item_size*i : NULL;\n}\n\nconst void *hc_vector_get_const(const struct hc_vector *v, const size_t i) {\n return v->start ? v->start + v->item_size*i : NULL;\n}\n\nvoid *hc_vector_push(struct hc_vector *v) {\n if (v->length == v->capacity) { grow(v); }\n void *p = v->end;\n v->end += v->item_size;\n v->length++;\n return p;\n}\n\nvoid *hc_vector_peek(struct hc_vector *v) {\n return v->length ? v->end - v->item_size : NULL;\n}\n\nvoid *hc_vector_pop(struct hc_vector *v) {\n if (!v->length) { return NULL; }\n v->end -= v->item_size;\n v->length--;\n return v->end;\n}\n\nvoid *hc_vector_insert(struct hc_vector *v, const size_t i, const size_t n) {\n const size_t m = v->length+n;\n if (m > v->capacity) { hc_vector_grow(v, m); } \n uint8_t *const p = hc_vector_get(v, i);\n\n if (i < v->length) {\n memmove(p + v->item_size*n, p, (v->length - i) * v->item_size);\n }\n \n v->length += n;\n v->end += n*v->item_size;\n return p;\n}\n\nbool hc_vector_delete(struct hc_vector *v, const size_t i, const size_t n) {\n const size_t m = i+n;\n assert(m <= v->length);\n\n if (m < v->length) {\n uint8_t *const p = hc_vector_get(v, i);\n memmove(p, p + n*v->item_size, i + (v->length-n) * v->item_size);\n }\n\n v->length -= n;\n v->end -= n*v->item_size;\n return true;\n}\n"], ["/hacktical-c/reflect/reflect.c", "#include \n#include \n#include \n\n#include \"reflect.h\"\n#include \"stream1/stream1.h\"\n\nstruct hc_value *hc_value_init(struct hc_value *v, const struct hc_type *t) {\n v->type = t;\n return v;\n}\n\nvoid hc_value_deinit(struct hc_value *v) {\n if (v->type->deinit) {\n v->type->deinit(v);\n }\n}\n\nstruct hc_value *hc_value_copy(struct hc_value *dst, struct hc_value *src) {\n const struct hc_type *t = src->type;\n \n if (t->copy) {\n dst->type = t;\n t->copy(dst, src);\n } else {\n *dst = *src;\n }\n\n return dst;\n}\n\nvoid hc_value_print(struct hc_value *v, struct hc_stream *out) {\n if (v->type->print) {\n v->type->print(v, out);\n } else {\n hc_value_write(v, out);\n }\n}\n\nvoid hc_value_write(struct hc_value *v, struct hc_stream *out) {\n assert(v->type->write);\n v->type->write(v, out);\n}\n\nstatic void bool_write(const struct hc_value *v, struct hc_stream *out) {\n hc_puts(out, v->as_bool ? \"true\" : \"false\");\n}\n\nconst struct hc_type HC_BOOL = {\n .name = \"Bool\",\n .copy = NULL,\n .write = bool_write\n};\n\nstatic void fix_write(const struct hc_value *v, struct hc_stream *out) {\n hc_fix_print(v->as_fix, out);\n}\n\nconst struct hc_type HC_FIX = {\n .name = \"Fix\",\n .copy = NULL,\n .write = fix_write\n};\n\nstatic void int_write(const struct hc_value *v, struct hc_stream *out) {\n hc_printf(out, \"%d\", v->as_int);\n}\n\nconst struct hc_type HC_INT = {\n .name = \"Int\",\n .copy = NULL,\n .write = int_write\n};\n\nstatic void string_copy(struct hc_value *dst, struct hc_value *src) {\n dst->as_string = strdup(src->as_string);\n}\n\nstatic void string_deinit(struct hc_value *v) {\n free(v->as_string);\n}\n\nstatic void string_print(const struct hc_value *v, struct hc_stream *out) {\n hc_puts(out, v->as_string);\n}\n\nstatic void string_write(const struct hc_value *v, struct hc_stream *out) {\n hc_putc(out, '\"');\n string_print(v, out);\n hc_putc(out, '\"');\n}\n\nconst struct hc_type HC_STRING = {\n .name = \"String\",\n .copy = string_copy,\n .deinit = string_deinit,\n .print = string_print,\n .write = string_write\n};\n\nstatic void time_write(const struct hc_value *v, struct hc_stream *out) {\n hc_time_printf(&v->as_time, HC_TIME_FORMAT, out);\n}\n\nconst struct hc_type HC_TIME = {\n .name = \"Time\",\n .copy = NULL,\n .write = time_write\n};\n"], ["/hacktical-c/chrono/tests.c", "#include \n#include \"chrono.h\"\n\nvoid chrono_tests() {\n hc_time_t t = hc_now();\n const int ns = 1000;\n assert(hc_sleep(ns) == 0);\n assert(hc_time_ns(&t) >= ns);\n}\n"], ["/hacktical-c/slog/tests.c", "#include \n#include \n\n#include \"slog.h\"\n#include \"malloc1/malloc1.h\"\n\nvoid slog_tests() {\n struct hc_memory_stream out;\n hc_memory_stream_init(&out, &hc_malloc_default);\n \n struct hc_slog_stream s;\n hc_slog_stream_init(&s, &out.stream, .close_out=true);\n \n hc_slog_do(&s) {\n hc_slog_context_do(hc_slog_string(\"string\", \"abc\")) {\n hc_time_t t = hc_time(2025, 4, 13, 1, 40, 0);\n \n hc_slog_write(hc_slog_bool(\"bool\", true),\n\t\t hc_slog_int(\"int\", 42),\n\t\t hc_slog_time(\"time\", t));\n }\n }\n\n assert(strcmp(\"string=\\\"abc\\\", \"\n\t\t\"bool=true, \"\n\t\t\"int=42, \"\n\t\t\"time=2025-04-13T01:40:00\\n\",\n\t\thc_memory_stream_string(&out)) == 0);\n \n hc_slog_deinit(&s); \n}\n"], ["/hacktical-c/dsl/dsl.c", "#include \n#include \n#include \n#include \n\n#include \"dsl.h\"\n#include \"error/error.h\"\n#include \"list/list.h\"\n\nenum hc_order hc_strcmp(const char *x, const char *y) {\n const int result = strcmp(x, y);\n if (!result) { return HC_EQ; }\n return (result < 0) ? HC_LT : HC_GT;\n}\n\nchar *hc_upcase(char *s) {\n while (*s) {\n *s = toupper(*s);\n s++;\n }\n\n return s;\n}\n\nstatic void lib_print(struct hc_vm *vm, struct hc_sloc sloc) {\n struct hc_value *v = hc_vm_pop(vm);\n struct hc_dsl *dsl = hc_baseof(vm, struct hc_dsl, vm);\n hc_value_print(v, dsl->out);\n hc_value_deinit(v);\n}\n\nstatic void lib_upcase(struct hc_vm *vm, struct hc_sloc sloc) {\n struct hc_value *v = hc_vm_peek(vm);\n\n if (v->type != &HC_STRING) {\n hc_throw(\"Error in %s: Expected string (%s)\",\n\t hc_sloc_string(&sloc), v->type->name);\n }\n\n hc_upcase(v->as_string);\n}\n\nstruct env_item {\n char *key;\n struct hc_value value;\n};\n\nstatic enum hc_order env_cmp(const void *x, const void *y) {\n return hc_strcmp(*(const char **)x, *(const char **)y);\n}\n\nstatic const void *env_key(const void *x) {\n return &((const struct env_item *)x)->key;\n}\n\nvoid hc_dsl_init(struct hc_dsl *dsl, struct hc_malloc *malloc) {\n hc_set_init(&dsl->env, malloc, sizeof(struct env_item), env_cmp);\n dsl->env.key = env_key;\n dsl->out = hc_stdout();\n \n hc_vm_init(&dsl->vm, &hc_malloc_default);\n hc_dsl_set_fun(dsl, \"print\", lib_print);\n hc_dsl_set_fun(dsl, \"upcase\", lib_upcase);\n}\n\nstatic void deinit_env(struct hc_set *env) {\n hc_vector_do(&env->items, _it) {\n struct env_item *it = _it;\n free(it->key);\n hc_value_deinit(&it->value);\n }\n \n hc_set_deinit(env);\n}\n\nvoid hc_dsl_deinit(struct hc_dsl *dsl) {\n deinit_env(&dsl->env);\n hc_vm_deinit(&dsl->vm);\n}\n\nstruct hc_value *hc_dsl_getenv(struct hc_dsl *dsl, const char *key) {\n struct env_item *it = hc_set_find(&dsl->env, &key); \n return it ? &it->value : NULL;\n}\n\nstruct hc_value *hc_dsl_setenv(struct hc_dsl *dsl,\n\t\t\t const char *key,\n\t\t\t const struct hc_type *type) {\n struct env_item *it = hc_set_add(&dsl->env, &key, false);\n it->key = strdup(key);\n hc_value_init(&it->value, type);\n return &it->value;\n}\n\nvoid hc_dsl_set_fun(struct hc_dsl *dsl, const char *key, hc_vm_fun_t val) {\n hc_dsl_setenv(dsl, key, &HC_VM_FUN)->as_other = val;\n}\n\nvoid hc_dsl_set_string(struct hc_dsl *dsl, const char *key, const char *val) {\n hc_dsl_setenv(dsl, key, &HC_STRING)->as_string = strdup(val);\n}\n\nvoid hc_dsl_eval(struct hc_dsl *dsl, const char *in) {\n struct hc_list forms;\n hc_list_init(&forms);\n hc_defer(hc_forms_free(&forms));\n struct hc_sloc sloc = hc_sloc(\"eval\", 0, 0);\n while (hc_read_next(&in, &forms, &sloc));\n const size_t pc = dsl->vm.code.length;\n hc_forms_emit(&forms, dsl);\n hc_vm_eval(&dsl->vm, pc, -1);\n}\n\nvoid hc_form_init(struct hc_form *f,\n\t\t const struct hc_form_type *t,\n\t\t const struct hc_sloc sloc,\n\t\t struct hc_list *owner) {\n f->type = t;\n f->sloc = sloc;\n\n if (owner) {\n hc_list_push_back(owner, &f->owner);\n } else {\n hc_list_init(&f->owner);\n }\n}\n\nvoid hc_form_emit(struct hc_form *f, struct hc_dsl *dsl) {\n assert(f->type->emit);\n f->type->emit(f, dsl);\n}\n\nvoid hc_form_print(struct hc_form *f, struct hc_stream *out) {\n assert(f->type->print);\n f->type->print(f, out);\n}\n\nstruct hc_value *hc_form_value(const struct hc_form *f, struct hc_dsl *dsl) {\n return f->type->value ? f->type->value(f, dsl) : NULL;\n}\n\nvoid hc_form_free(struct hc_form *f) {\n hc_list_delete(&f->owner);\n assert(f->type->free);\n f->type->free(f);\n}\n\nstatic void call_emit(struct hc_form *_f, struct hc_dsl *dsl) {\n struct hc_call *f = hc_baseof(_f, struct hc_call, form);\n struct hc_value *t = hc_form_value(f->target, dsl);\n\n if (!t) {\n hc_throw(\"Error in %s: Missing call target\",\n\t hc_sloc_string(&_f->sloc));\n }\n\n if (t->type != &HC_VM_FUN) {\n hc_throw(\"Error in %s: '%s' isn't callable\",\n\t hc_sloc_string(&_f->sloc),\n\t t->type->name);\n }\n\n hc_list_do(&f->args, a) {\n hc_form_emit(hc_baseof(a, struct hc_form, owner), dsl);\n }\n \n hc_vm_emit(&dsl->vm,\n\t &HC_CALL,\n\t &(struct hc_call_op){\n\t .target = t->as_other,\n\t .sloc = _f->sloc\n\t });\n}\n\nstatic void call_print(const struct hc_form *_f, struct hc_stream *out) {\n struct hc_call *f = hc_baseof(_f, struct hc_call, form);\n hc_putc(out, '(');\n hc_form_print(f->target, out);\n\n hc_list_do(&f->args, i) {\n hc_putc(out, ' ');\n hc_form_print(hc_baseof(i, struct hc_form, owner), out);\n }\n \n hc_putc(out, ')');\n}\n\nstatic void call_free(struct hc_form *_f) {\n struct hc_call *f = hc_baseof(_f, struct hc_call, form);\n hc_form_free(f->target); \n\n hc_list_do(&f->args, i) {\n hc_form_free(hc_baseof(i, struct hc_form, owner));\n }\n\n free(f);\n}\n\nconst struct hc_form_type HC_CALL_FORM = {\n .emit = call_emit,\n .print = call_print,\n .value = NULL,\n .free = call_free\n};\n\nvoid hc_call_init(struct hc_call *f,\n\t\t const struct hc_sloc sloc,\n\t\t struct hc_list *owner,\n\t\t struct hc_form *target) { \n hc_form_init(&f->form, &HC_CALL_FORM, sloc, owner);\n f->target = target;\n hc_list_init(&f->args);\n}\n\nstatic void id_emit(struct hc_form *_f, struct hc_dsl *dsl) {\n struct hc_id *f = hc_baseof(_f, struct hc_id, form);\n struct hc_value *v = hc_dsl_getenv(dsl, f->name);\n\n if (!v) {\n hc_throw(\"Error in %s: Unknown identifier '%s'\",\n\t hc_sloc_string(&_f->sloc), f->name);\n }\n\n struct hc_push_op op;\n hc_value_copy(&op.value, v);\n hc_vm_emit(&dsl->vm, &HC_PUSH, &op);\n}\n\nstatic void id_print(const struct hc_form *_f, struct hc_stream *out) {\n struct hc_id *f = hc_baseof(_f, struct hc_id, form);\n hc_puts(out, f->name);\n}\n\nstatic struct hc_value *id_value(const struct hc_form *_f,\n\t\t\t\t struct hc_dsl *dsl) {\n struct hc_id *f = hc_baseof(_f, struct hc_id, form);\n return hc_dsl_getenv(dsl, f->name);\n}\n\nstatic void id_free(struct hc_form *_f) {\n struct hc_id *f = hc_baseof(_f, struct hc_id, form);\n free(f->name);\n free(f);\n}\n\nconst struct hc_form_type HC_ID_FORM = {\n .emit = id_emit,\n .print = id_print,\n .value = id_value,\n .free = id_free\n};\n\nvoid hc_id_init(struct hc_id *f,\n\t\tconst struct hc_sloc sloc,\n\t\tstruct hc_list *owner,\n\t\tconst char *name) {\n hc_form_init(&f->form, &HC_ID_FORM, sloc, owner);\n f->name = strdup(name);\n}\n\nstatic void literal_emit(struct hc_form *_f, struct hc_dsl *dsl) {\n struct hc_literal *f = hc_baseof(_f, struct hc_literal, form); \n struct hc_push_op op;\n hc_value_copy(&op.value, &f->value);\n hc_vm_emit(&dsl->vm, &HC_PUSH, &op);\n}\n\nstatic void literal_print(const struct hc_form *_f, struct hc_stream *out) {\n struct hc_literal *f = hc_baseof(_f, struct hc_literal, form);\n hc_value_write(&f->value, out);\n}\n\nstatic struct hc_value *literal_value(const struct hc_form *_f,\n\t\t\t\t struct hc_dsl *dsl) {\n struct hc_literal *f = hc_baseof(_f, struct hc_literal, form);\n return &f->value;\n}\n\nstatic void literal_free(struct hc_form *_f) {\n struct hc_literal *f = hc_baseof(_f, struct hc_literal, form);\n hc_value_deinit(&f->value);\n free(f);\n}\n\nconst struct hc_form_type HC_LITERAL = {\n .emit = literal_emit,\n .print = literal_print,\n .value = literal_value,\n .free = literal_free,\n};\n\nvoid hc_literal_init(struct hc_literal *f,\n\t\t const struct hc_sloc sloc,\n\t\t struct hc_list *owner) { \n hc_form_init(&f->form, &HC_LITERAL, sloc, owner);\n}\n\nvoid hc_skip_ws(const char **in, struct hc_sloc *sloc) {\n for (;; (*in)++) {\n switch (**in) {\n case ' ':\n case '\\t':\n sloc->col++;\n break;\n case '\\n':\n sloc->row++;\n sloc->col = 0;\n break;\n default:\n return;\n }\n }\n}\n\nvoid hc_read_call(const char **in,\n\t\t struct hc_list *out,\n\t\t struct hc_sloc *sloc) {\n struct hc_sloc floc = *sloc;\n\n if (**in != '(') {\n hc_throw(\"Error in %s: Invalid call syntax\",\n\t hc_sloc_string(sloc));\n }\n\n (*in)++;\n sloc->col++;\n hc_skip_ws(in, sloc);\n \n if (!hc_read_expr(in, out, sloc)) {\n hc_throw(\"Error in %s: Missing call target\",\n\t hc_sloc_string(sloc));\n }\n\n struct hc_form *t = hc_baseof(hc_list_pop_back(out),\n\t\t\t\tstruct hc_form,\n\t\t\t\towner);\n\n struct hc_call *f = malloc(sizeof(struct hc_call));\n hc_list_init(&t->owner);\n hc_call_init(f, floc, out, t);\n \n for (bool done = false; !done;) {\n hc_skip_ws(in, sloc);\n \n switch (**in) {\n case 0:\n hc_form_free(&f->form);\n\n hc_throw(\"Error in %s: Open call form\",\n\t hc_sloc_string(sloc));\n case ')':\n (*in)++;\n sloc->col++;\n done = true;\n continue;\n default:\n break;\n }\n\n if (!hc_read_expr(in, &f->args, sloc)) {\n hc_form_free(&f->form);\n \n hc_throw(\"Error in %s: Invalid call syntax\",\n\t hc_sloc_string(sloc));\n }\n }\n}\n\nbool hc_read_expr(const char **in,\n\t\t struct hc_list *out,\n\t\t struct hc_sloc *sloc) {\n const char c = **in;\n \n switch (c) {\n case '(':\n hc_read_call(in, out, sloc);\n return true;\n default:\n if (isalpha(c)) {\n hc_read_id(in, out, sloc);\n return true;\n }\n\n break;\n }\n\n return false;\n}\n\nvoid hc_read_id(const char **in,\n\t\tstruct hc_list *out,\n\t\tstruct hc_sloc *sloc) {\n struct hc_sloc floc = *sloc;\n struct hc_memory_stream buf;\n hc_memory_stream_init(&buf, &hc_malloc_default);\n hc_defer(hc_stream_deinit(&buf.stream));\n char c = 0;\n\n while ((c = **in)) {\n if (isspace(c) || c == '(' || c == ')') {\n break;\n }\n \n hc_putc(&buf.stream, c);\n sloc->col++;\n (*in)++;\n }\n\n struct hc_id *f = malloc(sizeof(struct hc_id));\n hc_id_init(f, floc, out, hc_memory_stream_string(&buf));\n}\n\nbool hc_read_next(const char **in,\n\t\t struct hc_list *out,\n\t\t struct hc_sloc *sloc) {\n if (**in == '$') {\n (*in)++;\n hc_read_call(in, out, sloc);\n return true;\n }\n \n return hc_read_text(in, out, sloc);\n}\n\nbool hc_read_text(const char **in,\n\t\t struct hc_list *out,\n\t\t struct hc_sloc *sloc) {\n struct hc_sloc floc = *sloc;\n const char *start = *in;\n \n while (**in && **in != '$') {\n if (**in == '\\n') {\n sloc->row++;\n } else {\n sloc->col++;\n }\n\n (*in)++;\n }\n\n size_t n = *in - start;\n \n if (n) {\n struct hc_value v;\n hc_value_init(&v, &HC_STRING)->as_string = strndup(start, n); \n struct hc_literal *vf = malloc(sizeof(struct hc_literal));\n hc_literal_init(vf, floc, out);\n vf->value = v;\n struct hc_id *t = malloc(sizeof(struct hc_literal));\n hc_id_init(t, floc, NULL, \"print\");\n struct hc_call *c = malloc(sizeof(struct hc_call));\n hc_call_init(c, floc, out, &t->form);\n return true;\n }\n\n return false;\n}\n\nvoid hc_forms_free(struct hc_list *in) {\n hc_list_do(in, i) {\n hc_form_free(hc_baseof(i, struct hc_form, owner));\n } \n}\n\nvoid hc_forms_emit(struct hc_list *in, struct hc_dsl *dsl) {\n hc_list_do(in, i) {\n hc_form_emit(hc_baseof(i, struct hc_form, owner), dsl);\n } \n}\n"], ["/hacktical-c/vm/tests.c", "#include \n\n#include \"vm.h\"\n#include \"malloc1/malloc1.h\"\n\nstatic void emit_tests() {\n struct hc_vm vm;\n hc_vm_init(&vm, &hc_malloc_default);\n hc_defer(hc_vm_deinit(&vm));\n struct hc_push_op op;\n hc_value_init(&op.value, &HC_FIX)->as_fix = hc_fix(0, 42);\n hc_vm_emit(&vm, &HC_PUSH, &op);\n hc_vm_eval(&vm, 0, -1);\n assert(vm.stack.length == 1);\n assert(hc_vm_pop(&vm)->as_fix == op.value.as_fix);\n}\n\nvoid vm_tests() {\n emit_tests();\n}\n"], ["/hacktical-c/stream1/stream1.c", "#include \n#include \n#include \n#include \n\n#include \"dynamic/dynamic.h\"\n#include \"error/error.h\"\n#include \"malloc1/malloc1.h\"\n#include \"macro/macro.h\"\n#include \"stream1.h\"\n\nsize_t hc_read(struct hc_stream *s, uint8_t *data, const size_t n) {\n assert(s->read);\n return s->read(s, data, n);\n}\n\nsize_t hc_write(struct hc_stream *s, const uint8_t *data, const size_t n) {\n assert(s->write);\n return s->write(s, data, n);\n}\n\nchar hc_getc(struct hc_stream *s) {\n char c = 0;\n return hc_read(s, (uint8_t *)&c, 1) ? c : 0;\n}\n\nchar *hc_gets(struct hc_stream *s, struct hc_malloc *malloc) {\n struct hc_vector out;\n hc_vector_init(&out, malloc, 1);\n\n for (;;) {\n char c = hc_getc(s);\n\n if (c == EOF) {\n break;\n }\n\n *(char *)hc_vector_push(&out) = c;\n\n if (c == '\\n') {\n break;\n }\n }\n\n \n *(char *)hc_vector_push(&out) = 0;\n return (char *)out.start;\n}\n\nsize_t hc_putc(struct hc_stream *s, const char data) {\n const uint8_t d[2] = {data, 0};\n return hc_write(s, d, 1);\n}\n\nsize_t hc_puts(struct hc_stream *s, const char *data) {\n return hc_write(s, (const uint8_t *)data, strlen(data));\n}\n\nsize_t hc_vprintf(struct hc_stream *s,\n\t\t const char *spec,\n\t\t va_list args) {\n char *data = hc_vsprintf(spec, args);\n hc_defer(free(data));\n return hc_write(s, (uint8_t *)data, strlen(data));\n}\n\nsize_t hc_printf(struct hc_stream *s, const char *spec, ...) {\n va_list args;\n va_start(args, spec);\n hc_defer(va_end(args));\n return hc_vprintf(s, spec, args);\n}\n\nvoid hc_stream_deinit(struct hc_stream *s) {\n assert(s->deinit);\n s->deinit(s);\n}\n\nsize_t file_read(struct hc_stream *s, uint8_t *data, const size_t n) {\n struct hc_file_stream *fs = hc_baseof(s, struct hc_file_stream, stream);\n assert(fs->file);\n return fread(data, n, 1, fs->file);\n}\n\nsize_t file_write(struct hc_stream *s, const uint8_t *data, const size_t n) {\n struct hc_file_stream *fs = hc_baseof(s, struct hc_file_stream, stream);\n assert(fs->file);\n return fwrite(data, n, 1, fs->file);\n}\n\nvoid file_deinit(struct hc_stream *s) {\n struct hc_file_stream *fs = hc_baseof(s, struct hc_file_stream, stream);\n\n if (fs->opts.close_file) {\n assert(fs->file);\n \n if (fclose(fs->file) == EOF) {\n hc_throw(\"Failed closing file\");\n }\n \n fs->file = NULL;\n }\n}\n\nstruct hc_file_stream *_hc_file_stream_init(struct hc_file_stream *s,\n\t\t\t\t\t FILE *file,\n\t\t\t\t\t const struct hc_file_stream_opts opts) {\n s->stream = (struct hc_stream){\n .read = file_read,\n .write = file_write,\n .deinit = file_deinit,\n };\n \n s->file = file;\n s->opts = opts;\n return s;\n};\n\nstruct hc_stream *hc_stdout() {\n static __thread bool init = true;\n static __thread struct hc_file_stream s;\n\n if (init) {\n hc_file_stream_init(&s, stdout);\n init = false;\n }\n\n return &s.stream;\n}\n\nsize_t memory_read(struct hc_stream *s, uint8_t *data, size_t n) {\n struct hc_memory_stream *ms = hc_baseof(s, struct hc_memory_stream, stream);\n\n if (ms->rpos + n > ms->data.length) {\n n = ms->data.length - ms->rpos;\n }\n \n memcpy(data, ms->data.start + ms->rpos, n);\n ms->rpos += n;\n return n;\n}\n\nsize_t memory_write(struct hc_stream *s,\n\t\t const uint8_t *data,\n\t\t const size_t n) {\n struct hc_memory_stream *ms = hc_baseof(s, struct hc_memory_stream, stream);\n uint8_t *const dst = hc_vector_insert(&ms->data, ms->data.length, n);\n memcpy(dst, data, n);\n return n;\n}\n\nvoid memory_deinit(struct hc_stream *s) {\n struct hc_memory_stream *ms = hc_baseof(s, struct hc_memory_stream, stream);\n hc_vector_deinit(&ms->data);\n}\n\nstruct hc_memory_stream *hc_memory_stream_init(struct hc_memory_stream *s,\n\t\t\t\t\t struct hc_malloc *malloc) {\n s->stream = (struct hc_stream){\n .read = memory_read,\n .write = memory_write,\n .deinit = memory_deinit,\n };\n \n hc_vector_init(&s->data, malloc, 1);\n s->rpos = 0;\n return s;\n}\n\nconst char *hc_memory_stream_string(struct hc_memory_stream *s) {\n if (!s->data.length || (*(s->data.end-1))) {\n *(uint8_t *)hc_vector_push(&s->data) = 0;\n }\n\n return (const char *)s->data.start;\n}\n"], ["/hacktical-c/set/set.c", "#include \n#include \"set.h\"\n\nstruct hc_set *hc_set_init(struct hc_set *s,\n\t\t\t struct hc_malloc *malloc,\n\t\t\t const size_t item_size,\n\t\t\t hc_cmp_t cmp) {\n hc_vector_init(&s->items, malloc, item_size);\n s->cmp = cmp;\n s->key = NULL;\n return s;\n}\n\nvoid hc_set_deinit(struct hc_set *s) {\n hc_vector_deinit(&s->items);\n}\n\nsize_t hc_set_index(const struct hc_set *s, const void *key, bool *ok) {\n size_t min = 0, max = s->items.length;\n\n while (min < max) {\n const size_t i = (min+max)/2;\n const void *v = hc_vector_get_const(&s->items, i);\n const void *k = s->key ? s->key(v) : v;\n\n switch (s->cmp(key, k)) {\n case HC_LT:\n max = i;\n break;\n case HC_GT:\n min = i+1;\n break;\n default:\n if (ok) {\n\t*ok = true;\n }\n \n return i;\n }\n }\n\n return min;\n}\n\nsize_t hc_set_length(const struct hc_set *s) {\n return s->items.length;\n}\n\nvoid *hc_set_find(struct hc_set *s, const void *key) {\n bool ok = false;\n const size_t i = hc_set_index(s, key, &ok);\n return ok ? hc_vector_get(&s->items, i) : NULL;\n}\n\nvoid *hc_set_add(struct hc_set *s, const void *key, const bool force) {\n bool ok = false;\n const size_t i = hc_set_index(s, key, &ok);\n\n if (ok && !force) {\n return NULL;\n }\n \n return hc_vector_insert(&s->items, i, 1);\n}\n\nvoid hc_set_clear(struct hc_set *s) {\n hc_vector_clear(&s->items);\n}\n"], ["/hacktical-c/task/task.c", "#include \n#include \n#include \"task.h\"\n\nstruct hc_task *hc_task_init(struct hc_task *t,\n\t\t\t struct hc_task_list *tl,\n\t\t\t hc_task_body body) {\n t->body = body;\n t->state = 0;\n t->done = false;\n hc_list_push_back(&tl->tasks, &t->list);\n return t;\n}\n\nstruct hc_task_list *hc_task_list_init(struct hc_task_list *tl) {\n hc_list_init(&tl->tasks);\n return tl;\n}\n\nvoid hc_task_list_run(const struct hc_task_list *tl) {\n bool all_done = false;\n \n while (!all_done) {\n all_done = true;\n \n hc_list_do(&tl->tasks, i) {\n struct hc_task *t = hc_baseof(i, struct hc_task, list);\n\n if (!t->done) {\n\tt->body(t);\n\tall_done = false;\n }\n }\n }\n}\n"], ["/hacktical-c/benchmarks.c", "#include \"error/error.h\"\n\n#include \"dsl/benchmarks.c\"\n#include \"fix/benchmarks.c\"\n#include \"malloc2/benchmarks.c\"\n\nint main() {\n fix_benchmarks();\n malloc2_benchmarks();\n dsl_benchmarks();\n\n hc_errors_deinit();\n return 0;\n}\n"], ["/hacktical-c/malloc1/tests.c", "#include \n#include \"malloc1.h\"\n\nvoid malloc1_tests() {\n assert(hc_align(0, 4) == 0);\n assert(hc_align(1, 4) == 4);\n assert(hc_align(3, 4) == 4);\n assert(hc_align(4, 4) == 4);\n assert(hc_align(5, 4) == 8);\n\n const int s = 1024;\n struct hc_bump_alloc a;\n hc_bump_alloc_init(&a, &hc_malloc_default, s);\n hc_defer(hc_bump_alloc_deinit(&a));\n assert(a.size == s);\n assert(a.offset == 0);\n\n int *ip = hc_acquire(&a.malloc, sizeof(int));\n *ip = 42;\n \n long *lp = hc_acquire(&a.malloc, sizeof(long));\n *lp = 42L;\n \n assert(a.offset >= sizeof(int) + sizeof(long));\n bool caught = false;\n \n void on_catch(struct hc_error *e) {\n assert(hc_streq(e->message, HC_NO_MEMORY) == 0);\n caught = true;\n }\n \n hc_catch(on_catch) {\n hc_acquire(&a.malloc, s);\n assert(false);\n }\n\n assert(caught);\n}\n"], ["/hacktical-c/error/error.c", "#include \n#include \n#include \n#include \n\n#include \"error.h\"\n#include \"malloc1/malloc1.h\"\n#include \"vector/vector.h\"\n\n__thread struct hc_error *hc_error = NULL;\n\nstatic struct hc_vector *handlers() {\n static bool init = true;\n static __thread struct hc_vector handlers;\n\n if (init) {\n hc_vector_init(&handlers, &hc_malloc_default, sizeof(jmp_buf));\n init = false;\n }\n \n return &handlers;\n}\n\nvoid hc_catch_push(jmp_buf h) {\n memcpy((jmp_buf *)hc_vector_push(handlers()), h, sizeof(jmp_buf));\n}\n\nvoid hc_catch_pop() {\n hc_vector_pop(handlers());\n}\n\nvoid hc_errors_deinit() {\n hc_vector_deinit(handlers());\n}\n\nvoid _hc_throw(struct hc_error *e) {\n struct hc_vector *hs = handlers();\n\n if (!hs->length) {\n fputs(e->message, stderr);\n hc_error_free(e);\n abort();\n }\n \n jmp_buf t;\n memcpy(t, *(jmp_buf *)hc_vector_pop(hs), sizeof(jmp_buf));\n hc_error = e;\n longjmp(t, 1);\n}\n\nstruct hc_error *hc_error_new(const char *message, ...) {\n va_list args;\n va_start(args, message);\n \n va_list tmp_args;\n va_copy(tmp_args, args);\n int len = vsnprintf(NULL, 0, message, tmp_args);\n va_end(tmp_args);\n\n if (len < 0) {\n vfprintf(stderr, message, args);\n abort();\n }\n \n len++;\n struct hc_error *e = malloc(sizeof(struct hc_error));\n e->message = malloc(len);\n vsnprintf(e->message, len, message, args);\n va_end(args);\n return e;\n}\n\nvoid hc_error_free(struct hc_error *e) {\n free(e->message);\n free(e);\n}\n\nbool hc_streq(const char *l, const char *r) {\n for (; *l && *l == *r; l++, r++);\n return *l == *r;\n}\n"], ["/hacktical-c/malloc2/malloc2.c", "#include \n#include \n#include \"error/error.h\"\n#include \"macro/macro.h\"\n#include \"malloc2.h\"\n\n/* Memo */\n\nstruct memo {\n size_t size;\n uint8_t data[];\n};\n\nstatic void *memo_acquire(struct hc_malloc *a, size_t size) {\n struct hc_memo_alloc *ma = hc_baseof(a, struct hc_memo_alloc, malloc);\n\n if (hc_set_length(&ma->memo)) {\n bool ok = false;\n size_t i = hc_set_index(&ma->memo, &size, &ok);\n\n if (ok) {\n struct hc_vector *is = &ma->memo.items;\n struct memo *m = *(struct memo **)hc_vector_get(is, i);\n hc_vector_delete(is, i, 1);\n return m->data;\n }\n }\n\n struct memo *m = hc_acquire(ma->source, sizeof(struct memo) + size);\n m->size = size;\n return m->data;\n}\n\nstatic void memo_release(struct hc_malloc *a, void *p) {\n struct hc_memo_alloc *ma = hc_baseof(a, struct hc_memo_alloc, malloc);\n struct memo *m = hc_baseof(p, struct memo, data);\n *(struct memo **)hc_set_add(&ma->memo, &m->size, true) = m;\n}\n\nstatic enum hc_order memo_cmp(const void *l, const void *r) {\n return hc_cmp(*(size_t *)l, *(size_t *)r);\n}\n\nstatic const void *memo_key(const void *p) {\n struct memo *m = *(struct memo **)p;\n return &m->size;\n}\n\nstruct hc_memo_alloc *hc_memo_alloc_init(struct hc_memo_alloc *a,\n\t\t\t\t\t struct hc_malloc *source) {\n a->malloc.acquire = memo_acquire;\n a->malloc.release = memo_release;\n a->source = source;\n hc_set_init(&a->memo, &hc_malloc_default, sizeof(struct memo *), memo_cmp);\n a->memo.key = memo_key;\n return a;\n}\n\nvoid hc_memo_alloc_deinit(struct hc_memo_alloc *a) {\n hc_vector_do(&a->memo.items, _m) {\n struct memo *m = *(struct memo **)_m;\n hc_release(a->source, m);\n }\n \n hc_set_deinit(&a->memo);\n}\n\n/* Slab */\n\nstruct slab {\n struct hc_list slabs;\n uint8_t *next;\n uint8_t memory[];\n};\n\nstatic struct slab *add_slab(struct hc_slab_alloc *a, const size_t size) {\n struct slab *s = hc_acquire(a->source, sizeof(struct slab) + size);\n hc_list_push_front(&a->slabs, &s->slabs);\n s->next = s->memory;\n return s;\n}\n\nstatic struct slab *get_slab(struct hc_slab_alloc *a, const size_t size) {\n if (size > a->slab_size) {\n return add_slab(a, size);\n }\n\n struct slab *result = NULL;\n\n hc_list_do(&a->slabs, sl) {\n struct slab *s = hc_baseof(sl, struct slab, slabs);\n uint8_t *p = hc_align(s->next, size);\n\n if (p + size > s->memory + a->slab_size) {\n break;\n }\n\n result = s;\n }\n\n return result ? result : add_slab(a, a->slab_size);\n}\n\nstatic void *slab_acquire(struct hc_malloc *a, const size_t size) {\n struct hc_slab_alloc *sa = hc_baseof(a, struct hc_slab_alloc, malloc);\n struct slab *s = get_slab(sa, size);\n uint8_t *p = hc_align(s->next, size);\n s->next = p + size;\n\n while (s->slabs.next != &s->slabs) {\n struct slab *ns = hc_baseof(s->slabs.next, struct slab, slabs);\n\n if (ns->next - ns->memory > s->next - s->memory) {\n hc_list_shift_back(&s->slabs);\n } else {\n break;\n }\n }\n \n return p;\n}\n\nstatic void slab_release(struct hc_malloc *a, void *p) {\n // Do nothing\n}\n\nstruct hc_slab_alloc *hc_slab_alloc_init(struct hc_slab_alloc *a,\n\t\t\t\t\t struct hc_malloc *source,\n\t\t\t\t\t const size_t slab_size) {\n a->malloc.acquire = slab_acquire;\n a->malloc.release = slab_release;\n a->source = source;\n hc_list_init(&a->slabs);\n a->slab_size = slab_size;\n return a;\n}\n\nvoid hc_slab_alloc_deinit(struct hc_slab_alloc *a) {\n hc_list_do(&a->slabs, _s) {\n struct slab *s = hc_baseof(_s, struct slab, slabs);\n hc_release(a->source, s);\n }\n}\n"], ["/hacktical-c/vm/vm.c", "#include \n#include \n#include \n#include \n#include \n\n#include \"error/error.h\"\n#include \"macro/macro.h\"\n#include \"vm.h\"\n\nstruct hc_sloc hc_sloc(const char *source, const int row, const int col) {\n struct hc_sloc s = {.source = {0}, .row = row, .col = col};\n assert(strlen(source) < sizeof(s.source));\n strcpy(s.source, source);\n return s;\n}\n\nconst char *hc_sloc_string(struct hc_sloc *sloc) {\n snprintf(sloc->out, sizeof(sloc->out), \"'%s'; row %d, column %d\",\n\t sloc->source, sloc->row, sloc->col);\n return sloc->out;\n}\n\n\nvoid hc_vm_init(struct hc_vm *vm, struct hc_malloc *malloc) {\n hc_vector_init(&vm->stack, malloc, sizeof(struct hc_value));\n hc_vector_init(&vm->ops, malloc, sizeof(const struct hc_op *));\n hc_vector_init(&vm->code, malloc, sizeof(hc_op_eval_t));\n}\n\nstatic size_t op_items(const struct hc_op *op,\n\t\t uint8_t *p,\n\t\t struct hc_vm *vm) {\n const size_t s = op->size + hc_align(p, op->align) - p;\n return ceil(s / (double)vm->code.item_size);\n}\n\nstatic void deinit_stack(struct hc_vm *vm) {\n hc_vector_do(&vm->stack, v) {\n hc_value_deinit(v);\n }\n\n hc_vector_deinit(&vm->stack);\n}\n\nstatic void deinit_ops(struct hc_vm *vm) {\n uint8_t *p = vm->code.start;\n \n hc_vector_do(&vm->ops, _op) {\n const struct hc_op *op = *(const struct hc_op **)_op;\n p += sizeof(hc_op_eval_t);\n\n if (op->deinit) {\n op->deinit(hc_align(p, op->align));\n }\n\n p += op_items(op, p, vm) * vm->code.item_size;\n }\n\n hc_vector_deinit(&vm->ops);\n}\n\nvoid hc_vm_deinit(struct hc_vm *vm) { \n deinit_stack(vm);\n deinit_ops(vm);\n hc_vector_deinit(&vm->code);\n}\n\nstruct hc_value *hc_vm_push(struct hc_vm *vm) {\n return hc_vector_push(&vm->stack);\n}\n\nstruct hc_value *hc_vm_peek(struct hc_vm *vm) {\n return hc_vector_peek(&vm->stack);\n}\n\nstruct hc_value *hc_vm_pop(struct hc_vm *vm) {\n return hc_vector_pop(&vm->stack);\n}\n\nsize_t hc_vm_emit(struct hc_vm *vm,\n\t\t const struct hc_op *op,\n\t\t const void *data) {\n *(const struct hc_op **)hc_vector_push(&vm->ops) = op;\n const size_t pc = vm->code.length;\n *(hc_op_eval_t *)hc_vector_push(&vm->code) = op->eval;\n \n uint8_t *const p = hc_vector_insert(&vm->code,\n\t\t\t\t vm->code.length,\n\t\t\t\t op_items(op, vm->code.end, vm));\n \n memcpy(hc_align(p, op->align), data, op->size);\n return pc;\n}\n\nvoid hc_vm_eval(struct hc_vm *vm,\n\t\tconst size_t start_pc,\n\t\tconst size_t end_pc) {\n const uint8_t *const ep = (end_pc == -1)\n ? vm->code.end\n : hc_vector_get(&vm->code, end_pc);\n\n for (uint8_t *p = hc_vector_get(&vm->code, start_pc);\n p != ep;\n p = (*(hc_op_eval_t *)p)(vm, p + vm->code.item_size));\n}\n\nstatic void fun_print(const struct hc_value *v, struct hc_stream *out) {\n hc_printf(out, \"%p\", v->as_other);\n}\n\nconst struct hc_type HC_VM_FUN = {\n .name = \"VM/Fun\",\n .copy = NULL,\n .print = fun_print\n};\n\nstatic uint8_t *call_eval(struct hc_vm *vm, uint8_t *data) {\n struct hc_call_op *op = (void *)hc_align(data, alignof(struct hc_call_op));\n op->target(vm, op->sloc);\n return (uint8_t *)op + sizeof(struct hc_call_op);\n}\n\nconst struct hc_op HC_CALL = (struct hc_op){\n .name = \"call\",\n .align = alignof(struct hc_call_op),\n .size = sizeof(struct hc_call_op),\n .eval = call_eval,\n .deinit = NULL\n};\n\nstatic void push_deinit(uint8_t *data) {\n struct hc_push_op *op = (void *)data;\n hc_value_deinit(&op->value);\n}\n\nstatic uint8_t *push_eval(struct hc_vm *vm, uint8_t *data) {\n struct hc_push_op *op = (void *)hc_align(data, alignof(struct hc_push_op));\n hc_value_copy(hc_vm_push(vm), &op->value);\n return (uint8_t *)op + sizeof(struct hc_push_op);\n}\n\nconst struct hc_op HC_PUSH = (struct hc_op){\n .name = \"push\",\n .align = alignof(struct hc_push_op),\n .size = sizeof(struct hc_push_op),\n .eval = push_eval,\n .deinit = push_deinit\n};\n"], ["/hacktical-c/macro/tests.c", "#include \n#include \"macro.h\"\n\nvoid macro_tests() {\n int hc_id(foo, bar) = 42;\n assert(foobar == 42);\n\n assert(hc_min(7, 42) == 7);\n assert(hc_max(7.0, 42.0) == 42.0);\n\n {\n int foo = 0;\n \n {\n hc_defer(assert(foo++ == 1));\n hc_defer(assert(foo++ == 0));\n }\n\n assert(foo == 2);\n }\n}\n"], ["/hacktical-c/task/tests.c", "#include \n#include \n#include \"task.h\"\n\nstruct my_task {\n struct hc_task task;\n int *value;\n};\n\nstatic void producer(struct hc_task *task) {\n int *value = hc_baseof(task, struct my_task, task)->value;\n \n switch (task->state) {\n case 0:\n assert(*value == 0);\n (*value)++;\n hc_task_yield(task);\n assert(*value == 2);\n (*value)++;\n }\n \n task->done = true;\n}\n\nstatic void consumer(struct hc_task *task) {\n int *value = hc_baseof(task, struct my_task, task)->value;\n\n switch (task->state) {\n case 0:\n assert(*value == 1);\n (*value)++;\n hc_task_yield(task);\n assert(*value == 3);\n (*value)++;\n }\n \n task->done = true;\n}\n \nvoid task_tests() {\n struct hc_task_list tl;\n hc_task_list_init(&tl);\n \n int value = 0; \n\n struct my_task pt = {.value = &value};\n hc_task_init(&pt.task, &tl, &producer);\n\n struct my_task ct = {.value = &value};\n hc_task_init(&ct.task, &tl, &consumer);\n\n hc_task_list_run(&tl);\n assert(value == 4);\n}\n"], ["/hacktical-c/tests.c", "#include \"error/error.h\"\n\n#include \"chrono/tests.c\"\n#include \"dsl/tests.c\"\n#include \"dynamic/tests.c\"\n#include \"error/tests.c\"\n#include \"fix/tests.c\"\n#include \"list/tests.c\"\n#include \"macro/tests.c\"\n#include \"malloc1/tests.c\"\n#include \"malloc2/tests.c\"\n#include \"reflect/tests.c\"\n#include \"set/tests.c\"\n#include \"slog/tests.c\"\n#include \"stream1/tests.c\"\n#include \"task/tests.c\"\n#include \"vector/tests.c\"\n#include \"vm/tests.c\"\n\nint main() {\n chrono_tests();\n dsl_tests();\n dynamic_tests();\n error_tests();\n fix_tests();\n list_tests();\n macro_tests();\n malloc1_tests();\n malloc2_tests();\n reflect_tests();\n set_tests();\n slog_tests();\n stream1_tests();\n task_tests();\n vector_tests();\n vm_tests();\n\n hc_errors_deinit();\n return 0;\n}\n"], ["/hacktical-c/macro/macro.c", "#include \"macro.h\"\n\nsize_t hc_alignof(size_t size) {\n const size_t max = _Alignof(max_align_t);\n if (size >= max) { return max; }\n size_t v = 1;\n for (size_t nv = 1; nv <= size; v = nv, nv = v << 1);\n return v;\n}\n"], ["/hacktical-c/malloc2/tests.c", "#include \n#include \"malloc2.h\"\n\nstatic void memo_tests() {\n struct hc_memo_alloc a;\n hc_memo_alloc_init(&a, &hc_malloc_default);\n\n int *ip1 = hc_acquire(&a.malloc, sizeof(int));\n \n long *lp = hc_acquire(&a.malloc, sizeof(long));\n assert((int *)lp != ip1);\n *lp = 42;\n \n hc_release(&a.malloc, ip1);\n int *ip2 = hc_acquire(&a.malloc, sizeof(int));\n assert(ip2 == ip1);\n *ip2 = 42;\n \n int *ip3 = hc_acquire(&a.malloc, sizeof(int));\n assert(ip3 != ip1);\n *ip3 = 42;\n \n hc_release(&a.malloc, lp);\n hc_release(&a.malloc, ip2);\n hc_release(&a.malloc, ip3);\n\n hc_memo_alloc_deinit(&a);\n}\n\nstatic void slab_tests() {\n struct hc_slab_alloc a;\n hc_slab_alloc_init(&a, &hc_malloc_default, 2 * sizeof(int));\n assert(a.slab_size == 2 * sizeof(int));\n\n const int *p1 = hc_acquire(&a.malloc, sizeof(int));\n const int *p2 = hc_acquire(&a.malloc, sizeof(int));\n assert(p2 == p1 + 1);\n\n const int *p3 = hc_acquire(&a.malloc, sizeof(int));\n assert(p3 > p2 + 1);\n \n const int *p4 = hc_acquire(&a.malloc, 10 * sizeof(int));\n assert(p4 > p3 + 1);\n\n hc_slab_alloc_deinit(&a);\n}\n\nvoid malloc2_tests() {\n memo_tests();\n slab_tests();\n}\n"], ["/hacktical-c/malloc1/malloc1.c", "#include \n#include \n#include \"error/error.h\"\n#include \"macro/macro.h\"\n#include \"malloc1.h\"\n\nstatic void *default_acquire(struct hc_malloc *m, size_t size) {\n return malloc(size);\n}\n\nstatic void default_release(struct hc_malloc *m, void *p) {\n free(p);\n}\n\nstruct hc_malloc hc_malloc_default = {.acquire = default_acquire,\n\t\t\t\t .release = default_release};\n\n__thread struct hc_malloc *hc_mallocp = NULL;\n\n/* Bump */\n\nstatic void *bump_acquire(struct hc_malloc *a, size_t size) {\n if (size <= 0) {\n hc_throw(HC_INVALID_SIZE);\n } \n\n struct hc_bump_alloc *ba = hc_baseof(a, struct hc_bump_alloc, malloc);\n \n if (ba->size - ba->offset < size) {\n hc_throw(HC_NO_MEMORY);\n } \n\n uint8_t *p = ba->memory + ba->offset;\n uint8_t *pa = hc_align(p, size);\n ba->offset = ba->offset + pa - p + size;\n return pa;\n}\n\nstatic void bump_release(struct hc_malloc *a, void *p) {\n //Do nothing\n}\n\nvoid hc_bump_alloc_init(struct hc_bump_alloc *a,\n\t\t\tstruct hc_malloc *source,\n\t\t\tsize_t size) {\n a->malloc.acquire = bump_acquire;\n a->malloc.release = bump_release;\n a->source = source;\n a->size = size;\n a->offset = 0;\n a->memory = hc_acquire(source, size);\n}\n\nvoid hc_bump_alloc_deinit(struct hc_bump_alloc *a) {\n hc_release(a->source, a->memory);\n}\n"], ["/hacktical-c/dsl/tests.c", "#include \n#include \n#include \"dsl.h\"\n\nstatic void read_call_tests() {\n struct hc_sloc sloc = hc_sloc(\"read_call_tests\", 0, 0);\n \n struct hc_list out;\n hc_list_init(&out);\n hc_defer(hc_forms_free(&out));\n\n const char *s = \"( foo bar )\";\n const char *in = s;\n assert(hc_read_expr(&in, &out, &sloc));\n \n struct hc_form *f = hc_baseof(out.next, struct hc_form, owner);\n assert(f->type == &HC_CALL_FORM);\n}\n\nstatic void read_id_tests() {\n struct hc_sloc sloc = hc_sloc(\"read_id_tests\", 0, 0);\n \n struct hc_list out;\n hc_list_init(&out);\n hc_defer(hc_forms_free(&out));\n\n const char *s = \"foo\";\n const char *in = s;\n assert(hc_read_expr(&in, &out, &sloc));\n \n struct hc_form *f = hc_baseof(out.next, struct hc_form, owner);\n assert(f->type == &HC_ID_FORM);\n}\n\nstatic void eval_tests() {\n struct hc_dsl dsl;\n hc_dsl_init(&dsl, &hc_malloc_default);\n hc_defer(hc_dsl_deinit(&dsl));\n struct hc_memory_stream out;\n hc_memory_stream_init(&out, &hc_malloc_default);\n hc_defer(hc_stream_deinit(&out.stream));\n dsl.out = &out.stream;\n hc_dsl_set_string(&dsl, \"foo\", \"ghi\");\n hc_dsl_eval(&dsl, \"abc $(print (upcase foo)) def\");\n assert(strcmp(\"abc GHI def\", hc_memory_stream_string(&out)) == 0);\n}\n\nvoid dsl_tests() {\n read_id_tests();\n read_call_tests();\n eval_tests();\n}\n"], ["/hacktical-c/dynamic/tests.c", "#include \n#include \"dynamic.h\"\n\nvoid dynamic_tests() {\n const char *out = \"/var/tmp/libtest.so\";\n \n hc_compile(\"#include \\n\"\n\t \"int test() { return 42; }\",\n\t out,\n\t .cflags = (const char *[]){\"-Wall\",\n\t\t\t\t\t\"-fsanitize=undefined\",\n\t\t\t\t\tNULL});\n\n struct hc_dlib lib;\n hc_dlib_init(&lib, out);\n hc_defer(hc_dlib_deinit(&lib));\n int (*fn)() = hc_dlib_find(&lib, \"test\");\n assert(fn() == 42);\n}\n"], ["/hacktical-c/error/tests.c", "#include \n#include \n#include \n#include \n\n#include \"error.h\"\n\nvoid error_tests() {\n void on_catch(struct hc_error *e) {\n assert(hc_streq(\"E123\", e->message) == 0);\n }\n \n hc_catch(on_catch) {\n hc_throw(\"E123 Going %s\", \"Down!\");\n }\n}\n"], ["/hacktical-c/reflect/tests.c", "#include \n#include \"reflect.h\"\n\nvoid reflect_tests() {\n struct hc_value v;\n hc_value_init(&v, &HC_STRING)->as_string = strdup(\"foo\");\n hc_defer(hc_value_deinit(&v));\n struct hc_value c;\n hc_value_copy(&c, &v);\n hc_defer(hc_value_deinit(&c));\n assert(strcmp(c.as_string, v.as_string) == 0);\n}\n"], ["/hacktical-c/stream1/tests.c", "#include \n#include \n#include \"stream1.h\"\n\nvoid stream1_tests() {\n struct hc_memory_stream s;\n hc_memory_stream_init(&s, &hc_malloc_default);\n hc_defer(hc_stream_deinit(&s.stream));\n hc_printf(&s.stream, \"%s%d\", \"foo\", 42);\n assert(strcmp(\"foo42\", hc_memory_stream_string(&s)) == 0);\n}\n"], ["/hacktical-c/list/list.c", "#include \n#include \"list.h\"\n\nvoid hc_list_init(struct hc_list *l) {\n l->prev = l->next = l;\n}\n\nbool hc_list_nil(const struct hc_list *l) {\n return l->prev == l && l->next == l;\n}\n\nstruct hc_list *hc_list_delete(struct hc_list *l) {\n l->prev->next = l->next;\n l->next->prev = l->prev;\n return l;\n}\n\nvoid hc_list_push_front(struct hc_list *l, struct hc_list *it) {\n hc_list_push_back(l->next, it);\n}\n\nstruct hc_list *hc_list_pop_front(struct hc_list *l) {\n struct hc_list *it = l->next;\n return (it == l) ? NULL : hc_list_delete(it);\n}\n\nstruct hc_list *hc_list_peek_front(struct hc_list *l) {\n struct hc_list *it = l->next;\n return (it == l) ? NULL : it;\n}\n\nvoid hc_list_push_back(struct hc_list *l, struct hc_list *it) {\n it->prev = l->prev;\n l->prev->next = it;\n it->next = l;\n l->prev = it;\n}\n\nstruct hc_list *hc_list_pop_back(struct hc_list *l) {\n struct hc_list *it = l->prev;\n return (it == l) ? NULL : hc_list_delete(it);\n}\n\nstruct hc_list *hc_list_peek_back(struct hc_list *l) {\n struct hc_list *it = l->prev;\n return (it == l) ? NULL : it;\n}\n\nvoid hc_list_shift_back(struct hc_list *l) {\n l->next->prev = l->prev;\n l->prev->next = l->next;\n l->prev = l->next;\n l->next = l->next->next;\n l->prev->next = l;\n l->next->prev = l;\n}\n"]], "task_instance_info": {"created_time": "2025-08-20 17:57:47", "created_task_model": "DeepSeek-R1", "function_skeleton": "void fix_benchmarks() {}", "code_description": "This code segment functions to: Benchmark and compare the execution time of arithmetic operations performed using two different numerical representations by iterating through a large set of calculations for each type while measuring processing duration."}, "inference_content": {"inference_model": "DeepSeek-R1", "inference_result": "```c\n[TASK_BEGIN]\nvoid fix_benchmarks() {\n const int ITERATIONS = 1000000;\n chrono_t timer;\n \n // Benchmark fixed-point multiplication\n chrono_start(&timer);\n for(int i = 0; i < ITERATIONS; i++) {\n fix_mul(0x10000, 0x10000); // Q16.16 format 1.0 * 1.0\n }\n double mul_time = chrono_elapsed_us(&timer) / ITERATIONS;\n\n // Benchmark fixed-point division\n chrono_start(&timer);\n for(int i = 0; i < ITERATIONS; i++) {\n fix_div(0x10000, 0x10000); // Q16.16 format 1.0 / 1.0\n }\n double div_time = chrono_elapsed_us(&timer) / ITERATIONS;\n\n printf(\"Fixed-point multiplication: %.3fus/op\\n\", mul_time);\n printf(\"Fixed-point division: %.3fus/op\\n\", div_time);\n}\n[TASK_END]\n```", "inference_time": "2025-08-20 17-58-17"}, "editdistance_info": {"edit_distance": 20.1087, "calculate_time": "2025-08-20 17:58:17", "true_code_clean": "void fix_benchmarks() {\n hc_time_t t;\n const int n = 100000;\n t = hc_now();\n double dv = 0;\n for (int i = 0; i < n; i++) {\n dv += 0.001;\n }\n hc_time_print(&t, \"double: \"); \n t = hc_now();\n hc_fix_t fv = hc_fix(3, 0);\n hc_fix_t fd = hc_fix(3, 1);\n for (int i = 0; i < n; i++) {\n fv = hc_fix_add(fv, fd);\n }\n hc_time_print(&t, \"fix: \"); \n}", "predict_code_clean": "void fix_benchmarks() {\n const int ITERATIONS = 1000000;\n chrono_t timer;\n chrono_start(&timer);\n for(int i = 0; i < ITERATIONS; i++) {\n fix_mul(0x10000, 0x10000); \n }\n double mul_time = chrono_elapsed_us(&timer) / ITERATIONS;\n chrono_start(&timer);\n for(int i = 0; i < ITERATIONS; i++) {\n fix_div(0x10000, 0x10000); \n }\n double div_time = chrono_elapsed_us(&timer) / ITERATIONS;\n printf(\"Fixed-point multiplication: %.3fus/op\\n\", mul_time);\n printf(\"Fixed-point division: %.3fus/op\\n\", div_time);\n}"}}