From 15367a8479e4cff33737d8a8ab84637866336114 Mon Sep 17 00:00:00 2001 From: jgabaut <109908086+jgabaut@users.noreply.github.com> Date: Tue, 9 Apr 2024 05:06:37 +0200 Subject: [PATCH] feat: 0.4.2 Add list template (#67) * feat: Add linked list template to generate code, including Koliseo-based allocation * feat: rename internal KLS_Region_List funct * chore: add back showList signatures as macros * chore: bump amboso, invil * fix: basic_example.c using < 0.4.x macro signature * fix: drop () for type arg in KLS_PUSH_T_ARR_NAMED() * feat: bump bootstrap_anvil.sh to v0.2 * chore: use actual current function name for failed calls --- README.md | 22 ++ amboso | 2 +- bin/v0.4.1/.gitignore | 4 + bin/v0.4.1/static | 1 + bin/v0.4.2/.gitignore | 4 + bin/v0.4.2/static | 1 + configure.ac | 8 +- docs/koliseo.doxyfile | 2 +- invil | 2 +- scripts/bootstrap_anvil.sh | 20 +- src/koliseo.c | 345 ++++++++++---------- src/koliseo.h | 599 +++++++++++++++++++++++++++++++--- static/basic_example.c | 2 +- static/demo.c | 55 ++-- stego.lock | 3 +- tests/error/bad_size.k.stderr | 2 +- 16 files changed, 830 insertions(+), 242 deletions(-) create mode 100644 bin/v0.4.1/.gitignore create mode 120000 bin/v0.4.1/static create mode 100644 bin/v0.4.2/.gitignore create mode 120000 bin/v0.4.2/static diff --git a/README.md b/README.md index 8d7e8af..33e6527 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ + [Curses](#extra_curses) + [Debug](#extra_debug) + [Gulp](#extra_gulp) + + [List template](#list_template) + [Title banner](#extra_title) + [Experimental](#extra_exper) + [How to use extras](#extra_howto) @@ -118,6 +119,25 @@ int main(void) Utility to memory-map a file (always the best idea, right?) to a C string, by providing the filepath. - Also includes a minimal string-view API, in case you want to work on the file contents differently. +### List template + + Any time `LIST_T` is defined before including `koliseo.h`, a basic linked-list implementation supporting `Koliseo` allocation will be declared for the passed type. + - It can be done also after building a static object for the library. + + The `LIST_T` macro and the `koliseo.h` should be repeatable without issues, allowing definition of more than one list interface. + + This is implemented using some code-generating macros, which could rended build time slower if overused. + + Defining the `LIST_NAME`, `LIST_PREFIX` and `LIST_LINKAGE` can allow customisation for each list implementation: + - `LIST_NAME`: The name of the data type to be generated. + - If not given, will expand to something like `list_int` for an `int`. + - `LIST_PREFIX`: Prefix for generated functions. + - If not given, will expand to something `LIST_NAME` + `_`. (eg. `list_int_`) + - `LIST_LINKAGE`: Customize the linkage of the function. + - If not given, will expand to `static inline`. + + This is inspired by the dynamic array example by [David Priver](#credits). + ### Title banner Include an ASCII art string to be printed as a title banner. @@ -209,6 +229,8 @@ int main(void) Thanks to [Tsoding](https://github.com/tsoding) for its creative string view library ([repo](https://github.com/tsoding/sv)), which indeed does things so simply you mostly can't do anything different. + Thanks to [David Priver](https://www.davidpriver.com/ctemplates.html#template-headers.) for its dynamic array template example. + ## Todo - Break up internal extensions to the core functionality diff --git a/amboso b/amboso index a229a00..5d9de3b 160000 --- a/amboso +++ b/amboso @@ -1 +1 @@ -Subproject commit a229a00c8690c4527ee4eb8cc59a400f69d44cc9 +Subproject commit 5d9de3b9a920ec5f26d61a73661040e48830dd08 diff --git a/bin/v0.4.1/.gitignore b/bin/v0.4.1/.gitignore new file mode 100644 index 0000000..f915d8e --- /dev/null +++ b/bin/v0.4.1/.gitignore @@ -0,0 +1,4 @@ +#amboso compliant version folder, will ignore everything inside BUT the gitignore, to keep the clean dir +* +!.gitignore +!static diff --git a/bin/v0.4.1/static b/bin/v0.4.1/static new file mode 120000 index 0000000..382349a --- /dev/null +++ b/bin/v0.4.1/static @@ -0,0 +1 @@ +../../static/ \ No newline at end of file diff --git a/bin/v0.4.2/.gitignore b/bin/v0.4.2/.gitignore new file mode 100644 index 0000000..f915d8e --- /dev/null +++ b/bin/v0.4.2/.gitignore @@ -0,0 +1,4 @@ +#amboso compliant version folder, will ignore everything inside BUT the gitignore, to keep the clean dir +* +!.gitignore +!static diff --git a/bin/v0.4.2/static b/bin/v0.4.2/static new file mode 120000 index 0000000..382349a --- /dev/null +++ b/bin/v0.4.2/static @@ -0,0 +1 @@ +../../static/ \ No newline at end of file diff --git a/configure.ac b/configure.ac index 2bdd401..7203e12 100644 --- a/configure.ac +++ b/configure.ac @@ -1,5 +1,5 @@ # Define the package name and version -AC_INIT([koliseo], [0.4.1], [jgabaut@github.com]) +AC_INIT([koliseo], [0.4.2], [jgabaut@github.com]) # Verify automake version and enable foreign option AM_INIT_AUTOMAKE([foreign -Wall]) @@ -50,10 +50,10 @@ case "${host_os}" in if test "$enable_curses" = "yes"; then echo "Building with curses header" build_windows=yes - AC_SUBST([KOLISEO_CFLAGS], ["-I/usr/x86_64-w64-mingw32/include -static -fstack-protector -DWINDOWS_BUILD -DNCURSES_STATIC"]) + AC_SUBST([KOLISEO_CFLAGS], ["-I/usr/x86_64-w64-mingw32/include -static -fstack-protector -DNCURSES_STATIC"]) AC_SUBST([KOLISEO_LDFLAGS], ["-L/usr/x86_64-w64-mingw32/lib -lncursesw"]) else - AC_SUBST([KOLISEO_CFLAGS], ["-I/usr/x86_64-w64-mingw32/include -static -fstack-protector -DWINDOWS_BUILD"]) + AC_SUBST([KOLISEO_CFLAGS], ["-I/usr/x86_64-w64-mingw32/include -static -fstack-protector"]) AC_SUBST([KOLISEO_LDFLAGS], ["-L/usr/x86_64-w64-mingw32/lib"]) fi AC_SUBST([CCOMP], ["/usr/bin/x86_64-w64-mingw32-gcc"]) @@ -104,7 +104,7 @@ AM_CONDITIONAL([LINUX_BUILD], [test "$build_linux" = "yes"]) # Set a default version number if not specified externally AC_ARG_VAR([VERSION], [Version number]) if test -z "$VERSION"; then - VERSION="0.4.1" + VERSION="0.4.2" fi # Output variables to the config.h header diff --git a/docs/koliseo.doxyfile b/docs/koliseo.doxyfile index 0d055c5..7bf4b62 100644 --- a/docs/koliseo.doxyfile +++ b/docs/koliseo.doxyfile @@ -48,7 +48,7 @@ PROJECT_NAME = koliseo # could be handy for archiving the generated documentation or if some version # control system is used. -PROJECT_NUMBER = 0.4.1 +PROJECT_NUMBER = 0.4.2 # Using the PROJECT_BRIEF tag one can provide an optional one line description # for a project that appears at the top of each page and should give viewer a diff --git a/invil b/invil index a2058f1..e220628 160000 --- a/invil +++ b/invil @@ -1 +1 @@ -Subproject commit a2058f105bd82622d85b28b7e9fd3dc1a5c87934 +Subproject commit e2206284808bccbe78cf34af8f63c551c7d39903 diff --git a/scripts/bootstrap_anvil.sh b/scripts/bootstrap_anvil.sh index 0c80371..14c9529 100755 --- a/scripts/bootstrap_anvil.sh +++ b/scripts/bootstrap_anvil.sh @@ -15,15 +15,21 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -script_version="0.1" +script_version="0.2" print_impls() { printf "Impl list:\n" - printf " repo_amboso\n repo_invil\n path_anvil\n\n" + printf " repo_amboso\n repo_invil\n path_anvil\n cargo_invil\n\n" } prompt_impls() { - printf "\nSelect anvil implementation:\n" + hint="$1" + if [ ! -z "$hint" ] ; then { + printf "\nPress Enter to use {%s} as requested anvil impl or select anvil implementation:\n" "$hint" + } else { + printf "\nSelect anvil implementation:\n" + } + fi print_impls printf "Choice: \n" } @@ -73,13 +79,16 @@ if [ "$is_interactive" -eq 0 ] ; then { } elif [ "$impl_q" = "path_anvil" ] ; then { ln -s /usr/local/bin/anvil ./anvil exit "$?" + } elif [ "$impl_q" = "cargo_invil" ] ; then { + ln -s "$HOME"/.cargo/bin/invil ./anvil + exit "$?" } else { printf "[ERROR] Invalid impl query: {%s}.\n" "$impl_q" exit 1 } fi } else { -while read -p "$(prompt_impls)" line; do { + while read -p "$(prompt_impls "$2")" line; do { if [ -z "$line" ] ; then { line="$2" } @@ -103,6 +112,9 @@ while read -p "$(prompt_impls)" line; do { } elif [ "$impl_q" = "path_anvil" ] ; then { ln -s /usr/local/bin/anvil ./anvil exit "$?" + } elif [ "$impl_q" = "cargo_invil" ] ; then { + ln -s "$HOME"/.cargo/bin/invil ./anvil + exit "$?" } else { printf "[ERROR] Invalid impl query: {%s}.\n" "$impl_q" } diff --git a/src/koliseo.c b/src/koliseo.c index 1a250c7..c151b88 100644 --- a/src/koliseo.c +++ b/src/koliseo.c @@ -429,7 +429,7 @@ Koliseo *kls_new_alloc(ptrdiff_t size, kls_alloc_func alloc_func) //KLS_Region_List reglist = kls_emptyList(); //reglist = kls_cons(kls,kls_header,reglist); //kls->regs = reglist; - kls->regs = kls_cons(kls, kls_header, kls->regs); + kls->regs = kls_rl_cons(kls, kls_header, kls->regs); if (kls->regs == NULL) { fprintf(stderr, "[KLS] [%s()]: failed to get a KLS_Region_List.\n", @@ -607,7 +607,7 @@ bool kls_set_conf(Koliseo *kls, KLS_Conf conf) #endif #endif - kls_freeList(kls->regs); + kls_rl_freeList(kls->regs); Koliseo *reglist_kls = NULL; reglist_kls = kls_new_conf(kls->conf.kls_reglist_kls_size, KLS_DEFAULT_CONF__); @@ -648,7 +648,7 @@ bool kls_set_conf(Koliseo *kls, KLS_Conf conf) //KLS_Region_List reglist = kls_emptyList(); //reglist = kls_cons(kls,kls_header,reglist); //kls->regs = reglist; - kls->regs = kls_cons(kls, kls_header, kls->regs); + kls->regs = kls_rl_cons(kls, kls_header, kls->regs); if (kls->regs == NULL) { fprintf(stderr, "[KLS] %s() failed to get a KLS_Region_List.\n.", @@ -754,7 +754,7 @@ void *kls_push(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count) ptrdiff_t padding = -kls->offset & (align - 1); if (count > PTRDIFF_MAX / size || available - padding < size * count) { if (count > PTRDIFF_MAX / size) { -#ifndef WINDOWS_BUILD +#ifndef _WIN32 fprintf(stderr, "[KLS] count [%li] was bigger than PTRDIFF_MAX/size [%li].\n", count, PTRDIFF_MAX / size); @@ -764,7 +764,7 @@ void *kls_push(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count) count, PTRDIFF_MAX / size); #endif } else { -#ifndef WINDOWS_BUILD +#ifndef _WIN32 fprintf(stderr, "[KLS] Out of memory. size*count [%li] was bigger than available-padding [%li].\n", size * count, available - padding); @@ -774,7 +774,7 @@ void *kls_push(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count) size * count, available - padding); #endif } - fprintf(stderr, "[KLS] Failed kls_push() call.\n"); + fprintf(stderr, "[KLS] Failed %s() call.\n", __func__); kls_free(kls); exit(EXIT_FAILURE); } @@ -850,7 +850,7 @@ void *kls_push_zero(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t padding = -kls->offset & (align - 1); if (count > PTRDIFF_MAX / size || (available - padding) < (size * count)) { if (count > PTRDIFF_MAX / size) { -#ifndef WINDOWS_BUILD +#ifndef _WIN32 fprintf(stderr, "[KLS] count [%li] was bigger than PTRDIFF_MAX/size [%li].\n", count, PTRDIFF_MAX / size); @@ -860,7 +860,7 @@ void *kls_push_zero(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, count, PTRDIFF_MAX / size); #endif } else { -#ifndef WINDOWS_BUILD +#ifndef _WIN32 fprintf(stderr, "[KLS] Out of memory. size*count [%li] was bigger than available-padding [%li].\n", size * count, available - padding); @@ -870,7 +870,7 @@ void *kls_push_zero(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, size * count, available - padding); #endif } - fprintf(stderr, "[KLS] Failed kls_push_zero() call.\n"); + fprintf(stderr, "[KLS] Failed %s() call.\n", __func__); kls_free(kls); exit(EXIT_FAILURE); //return 0; @@ -950,7 +950,7 @@ void *kls_push_zero_AR(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t padding = -kls->offset & (align - 1); if (count > PTRDIFF_MAX / size || (available - padding) < (size * count)) { if (count > PTRDIFF_MAX / size) { -#ifndef WINDOWS_BUILD +#ifndef _WIN32 fprintf(stderr, "[KLS] count [%li] was bigger than PTRDIFF_MAX/size [%li].\n", count, PTRDIFF_MAX / size); @@ -960,7 +960,7 @@ void *kls_push_zero_AR(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, count, PTRDIFF_MAX / size); #endif } else { -#ifndef WINDOWS_BUILD +#ifndef _WIN32 fprintf(stderr, "[KLS] Out of memory. size*count [%li] was bigger than available-padding [%li].\n", size * count, available - padding); @@ -989,7 +989,7 @@ void *kls_push_zero_AR(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, } break; case KLS_REGLIST_ALLOC_KLS_BASIC: { - if (kls_length(kls->regs) < kls->max_regions_kls_alloc_basic) { + if (kls_rl_length(kls->regs) < kls->max_regions_kls_alloc_basic) { reg = KLS_PUSH(kls->reglist_kls, KLS_Region); } else { fprintf(stderr, @@ -999,7 +999,7 @@ void *kls_push_zero_AR(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, kls_log(kls, "ERROR", "[%s()]: Exceeding kls->max_regions_kls_alloc_basic: {%i}.", __func__, kls->max_regions_kls_alloc_basic); - kls_showList_toFile(kls->regs, kls->conf.kls_log_fp); + kls_rl_showList_toFile(kls->regs, kls->conf.kls_log_fp); print_kls_2file(kls->conf.kls_log_fp, kls->reglist_kls); print_kls_2file(kls->conf.kls_log_fp, kls); } @@ -1036,7 +1036,7 @@ void *kls_push_zero_AR(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, //KLS_Region_List reglist = kls_emptyList(); //reglist = kls_cons(kls,reg,reglist); //kls->regs = kls_append(kls,reglist, kls->regs); - kls->regs = kls_cons(kls, reg, kls->regs); + kls->regs = kls_rl_cons(kls, reg, kls->regs); } #endif // KOLISEO_HAS_REGION @@ -1116,7 +1116,7 @@ void *kls_temp_push_zero_AR(Koliseo_Temp *t_kls, ptrdiff_t size, ptrdiff_t padding = -kls->offset & (align - 1); if (count > PTRDIFF_MAX / size || (available - padding) < (size * count)) { if (count > PTRDIFF_MAX / size) { -#ifndef WINDOWS_BUILD +#ifndef _WIN32 fprintf(stderr, "[KLS] count [%li] was bigger than PTRDIFF_MAX/size [%li].\n", count, PTRDIFF_MAX / size); @@ -1126,7 +1126,7 @@ void *kls_temp_push_zero_AR(Koliseo_Temp *t_kls, ptrdiff_t size, count, PTRDIFF_MAX / size); #endif } else { -#ifndef WINDOWS_BUILD +#ifndef _WIN32 fprintf(stderr, "[KLS] Out of memory. size*count [%li] was bigger than available-padding [%li].\n", size * count, available - padding); @@ -1136,7 +1136,7 @@ void *kls_temp_push_zero_AR(Koliseo_Temp *t_kls, ptrdiff_t size, size * count, available - padding); #endif } - fprintf(stderr, "[KLS] Failed kls_push_zero() call.\n"); + fprintf(stderr, "[KLS] Failed %s() call.\n", __func__); kls_free(kls); exit(EXIT_FAILURE); //return 0; @@ -1155,7 +1155,7 @@ void *kls_temp_push_zero_AR(Koliseo_Temp *t_kls, ptrdiff_t size, } break; case KLS_REGLIST_ALLOC_KLS_BASIC: { - if (kls_length(t_kls->t_regs) < + if (kls_rl_length(t_kls->t_regs) < t_kls->max_regions_kls_alloc_basic) { reg = KLS_PUSH(t_kls->reglist_kls, KLS_Region); } else { @@ -1166,8 +1166,8 @@ void *kls_temp_push_zero_AR(Koliseo_Temp *t_kls, ptrdiff_t size, kls_log(kls, "ERROR", "[%s()]: Exceeding t_kls->max_regions_kls_alloc_basic: {%i}.", __func__, t_kls->max_regions_kls_alloc_basic); - kls_showList_toFile(t_kls->t_regs, - kls->conf.kls_log_fp); + kls_rl_showList_toFile(t_kls->t_regs, + kls->conf.kls_log_fp); print_kls_2file(kls->conf.kls_log_fp, t_kls->reglist_kls); print_kls_2file(kls->conf.kls_log_fp, kls); @@ -1205,7 +1205,7 @@ void *kls_temp_push_zero_AR(Koliseo_Temp *t_kls, ptrdiff_t size, //KLS_Region_List reglist = kls_emptyList(); //reglist = kls_cons(kls,reg,reglist); //t_kls->t_regs = kls_append(kls,reglist, t_kls->t_regs); - t_kls->t_regs = kls_t_cons(t_kls, reg, t_kls->t_regs); + t_kls->t_regs = kls_rl_t_cons(t_kls, reg, t_kls->t_regs); } #endif // KOLISEO_HAS_REGION @@ -1283,7 +1283,7 @@ void *kls_push_zero_named(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t padding = -kls->offset & (align - 1); if (count > PTRDIFF_MAX / size || (available - padding) < (size * count)) { if (count > PTRDIFF_MAX / size) { -#ifndef WINDOWS_BUILD +#ifndef _WIN32 fprintf(stderr, "[KLS] count [%li] was bigger than PTRDIFF_MAX/size [%li].\n", count, PTRDIFF_MAX / size); @@ -1293,7 +1293,7 @@ void *kls_push_zero_named(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, count, PTRDIFF_MAX / size); #endif } else { -#ifndef WINDOWS_BUILD +#ifndef _WIN32 fprintf(stderr, "[KLS] Out of memory. size*count [%li] was bigger than available-padding [%li].\n", size * count, available - padding); @@ -1303,7 +1303,7 @@ void *kls_push_zero_named(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, size * count, available - padding); #endif } - fprintf(stderr, "[KLS] Failed kls_push_zero() call.\n"); + fprintf(stderr, "[KLS] Failed %s() call.\n", __func__); kls_free(kls); exit(EXIT_FAILURE); //return 0; @@ -1321,7 +1321,7 @@ void *kls_push_zero_named(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, } break; case KLS_REGLIST_ALLOC_KLS_BASIC: { - if (kls_length(kls->regs) < kls->max_regions_kls_alloc_basic) { + if (kls_rl_length(kls->regs) < kls->max_regions_kls_alloc_basic) { reg = KLS_PUSH(kls->reglist_kls, KLS_Region); } else { fprintf(stderr, @@ -1331,7 +1331,7 @@ void *kls_push_zero_named(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, kls_log(kls, "ERROR", "[%s()]: Exceeding kls->max_regions_kls_alloc_basic: {%i}.", __func__, kls->max_regions_kls_alloc_basic); - kls_showList_toFile(kls->regs, kls->conf.kls_log_fp); + kls_rl_showList_toFile(kls->regs, kls->conf.kls_log_fp); print_kls_2file(kls->conf.kls_log_fp, kls->reglist_kls); print_kls_2file(kls->conf.kls_log_fp, kls); } @@ -1367,7 +1367,7 @@ void *kls_push_zero_named(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, //KLS_Region_List reglist = kls_emptyList(); //reglist = kls_cons(kls,reg,reglist); //kls->regs = kls_append(kls,reglist, kls->regs); - kls->regs = kls_cons(kls, reg, kls->regs); + kls->regs = kls_rl_cons(kls, reg, kls->regs); char h_size[200]; kls_formatSize(size * count, h_size, sizeof(h_size)); @@ -1453,7 +1453,7 @@ void *kls_temp_push_zero_named(Koliseo_Temp *t_kls, ptrdiff_t size, ptrdiff_t padding = -kls->offset & (align - 1); if (count > PTRDIFF_MAX / size || (available - padding) < (size * count)) { if (count > PTRDIFF_MAX / size) { -#ifndef WINDOWS_BUILD +#ifndef _WIN32 fprintf(stderr, "[KLS] count [%li] was bigger than PTRDIFF_MAX/size [%li].\n", count, PTRDIFF_MAX / size); @@ -1463,7 +1463,7 @@ void *kls_temp_push_zero_named(Koliseo_Temp *t_kls, ptrdiff_t size, count, PTRDIFF_MAX / size); #endif } else { -#ifndef WINDOWS_BUILD +#ifndef _WIN32 fprintf(stderr, "[KLS] Out of memory. size*count [%li] was bigger than available-padding [%li].\n", size * count, available - padding); @@ -1491,7 +1491,7 @@ void *kls_temp_push_zero_named(Koliseo_Temp *t_kls, ptrdiff_t size, } break; case KLS_REGLIST_ALLOC_KLS_BASIC: { - if (kls_length(t_kls->t_regs) < + if (kls_rl_length(t_kls->t_regs) < t_kls->max_regions_kls_alloc_basic) { reg = KLS_PUSH(t_kls->reglist_kls, KLS_Region); } else { @@ -1502,8 +1502,8 @@ void *kls_temp_push_zero_named(Koliseo_Temp *t_kls, ptrdiff_t size, kls_log(kls, "ERROR", "[%s()]: Exceeding t_kls->max_regions_kls_alloc_basic: {%i}.", __func__, t_kls->max_regions_kls_alloc_basic); - kls_showList_toFile(t_kls->t_regs, - kls->conf.kls_log_fp); + kls_rl_showList_toFile(t_kls->t_regs, + kls->conf.kls_log_fp); print_kls_2file(kls->conf.kls_log_fp, t_kls->reglist_kls); print_kls_2file(kls->conf.kls_log_fp, kls); @@ -1540,7 +1540,7 @@ void *kls_temp_push_zero_named(Koliseo_Temp *t_kls, ptrdiff_t size, //KLS_Region_List reglist = kls_emptyList(); //reglist = kls_cons(kls,reg,reglist); //t_kls->t_regs = kls_append(kls,reglist, t_kls->t_regs); - t_kls->t_regs = kls_t_cons(t_kls, reg, t_kls->t_regs); + t_kls->t_regs = kls_rl_t_cons(t_kls, reg, t_kls->t_regs); char h_size[200]; kls_formatSize(size, h_size, sizeof(h_size)); @@ -1614,7 +1614,7 @@ void *kls_push_zero_typed(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t padding = -kls->offset & (align - 1); if (count > PTRDIFF_MAX / size || (available - padding) < (size * count)) { if (count > PTRDIFF_MAX / size) { -#ifndef WINDOWS_BUILD +#ifndef _WIN32 fprintf(stderr, "[KLS] count [%li] was bigger than PTRDIFF_MAX/size [%li].\n", count, PTRDIFF_MAX / size); @@ -1624,7 +1624,7 @@ void *kls_push_zero_typed(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, count, PTRDIFF_MAX / size); #endif } else { -#ifndef WINDOWS_BUILD +#ifndef _WIN32 fprintf(stderr, "[KLS] Out of memory. size*count [%li] was bigger than available-padding [%li].\n", size * count, available - padding); @@ -1634,7 +1634,7 @@ void *kls_push_zero_typed(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, size * count, available - padding); #endif } - fprintf(stderr, "[KLS] Failed kls_push_zero() call.\n"); + fprintf(stderr, "[KLS] Failed %s() call.\n", __func__); kls_free(kls); exit(EXIT_FAILURE); //return 0; @@ -1652,7 +1652,7 @@ void *kls_push_zero_typed(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, } break; case KLS_REGLIST_ALLOC_KLS_BASIC: { - if (kls_length(kls->regs) < kls->max_regions_kls_alloc_basic) { + if (kls_rl_length(kls->regs) < kls->max_regions_kls_alloc_basic) { reg = KLS_PUSH(kls->reglist_kls, KLS_Region); } else { fprintf(stderr, @@ -1662,7 +1662,7 @@ void *kls_push_zero_typed(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, kls_log(kls, "ERROR", "[%s()]: Exceeding kls->max_regions_kls_alloc_basic: {%i}.", __func__, kls->max_regions_kls_alloc_basic); - kls_showList_toFile(kls->regs, kls->conf.kls_log_fp); + kls_rl_showList_toFile(kls->regs, kls->conf.kls_log_fp); print_kls_2file(kls->conf.kls_log_fp, kls->reglist_kls); print_kls_2file(kls->conf.kls_log_fp, kls); } @@ -1698,7 +1698,7 @@ void *kls_push_zero_typed(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, //KLS_Region_List reglist = kls_emptyList(); //reglist = kls_cons(kls,reg,reglist); //kls->regs = kls_append(kls,reglist, kls->regs); - kls->regs = kls_cons(kls, reg, kls->regs); + kls->regs = kls_rl_cons(kls, reg, kls->regs); char h_size[200]; kls_formatSize(size * count, h_size, sizeof(h_size)); @@ -1783,7 +1783,7 @@ void *kls_temp_push_zero_typed(Koliseo_Temp *t_kls, ptrdiff_t size, ptrdiff_t padding = -kls->offset & (align - 1); if (count > PTRDIFF_MAX / size || (available - padding) < (size * count)) { if (count > PTRDIFF_MAX / size) { -#ifndef WINDOWS_BUILD +#ifndef _WIN32 fprintf(stderr, "[KLS] count [%li] was bigger than PTRDIFF_MAX/size [%li].\n", count, PTRDIFF_MAX / size); @@ -1793,7 +1793,7 @@ void *kls_temp_push_zero_typed(Koliseo_Temp *t_kls, ptrdiff_t size, count, PTRDIFF_MAX / size); #endif } else { -#ifndef WINDOWS_BUILD +#ifndef _WIN32 fprintf(stderr, "[KLS] Out of memory. size*count [%li] was bigger than available-padding [%li].\n", size * count, available - padding); @@ -1803,7 +1803,7 @@ void *kls_temp_push_zero_typed(Koliseo_Temp *t_kls, ptrdiff_t size, size * count, available - padding); #endif } - fprintf(stderr, "[KLS] Failed kls_push_zero() call.\n"); + fprintf(stderr, "[KLS] Failed %s() call.\n", __func__); kls_free(kls); exit(EXIT_FAILURE); //return 0; @@ -1821,7 +1821,7 @@ void *kls_temp_push_zero_typed(Koliseo_Temp *t_kls, ptrdiff_t size, } break; case KLS_REGLIST_ALLOC_KLS_BASIC: { - if (kls_length(t_kls->t_regs) < + if (kls_rl_length(t_kls->t_regs) < t_kls->max_regions_kls_alloc_basic) { reg = KLS_PUSH(t_kls->reglist_kls, KLS_Region); } else { @@ -1832,8 +1832,8 @@ void *kls_temp_push_zero_typed(Koliseo_Temp *t_kls, ptrdiff_t size, kls_log(kls, "ERROR", "[%s()]: Exceeding t_kls->max_regions_kls_alloc_basic: {%i}.", __func__, t_kls->max_regions_kls_alloc_basic); - kls_showList_toFile(t_kls->t_regs, - kls->conf.kls_log_fp); + kls_rl_showList_toFile(t_kls->t_regs, + kls->conf.kls_log_fp); print_kls_2file(kls->conf.kls_log_fp, t_kls->reglist_kls); print_kls_2file(kls->conf.kls_log_fp, kls); @@ -1869,7 +1869,7 @@ void *kls_temp_push_zero_typed(Koliseo_Temp *t_kls, ptrdiff_t size, //KLS_Region_List reglist = kls_emptyList(); //reglist = kls_cons(kls,reg,reglist); //t_kls->t_regs = kls_append(kls,reglist, t_kls->t_regs); - t_kls->t_regs = kls_t_cons(t_kls, reg, t_kls->t_regs); + t_kls->t_regs = kls_rl_t_cons(t_kls, reg, t_kls->t_regs); char h_size[200]; kls_formatSize(size * count, h_size, sizeof(h_size)); @@ -1931,7 +1931,7 @@ void print_kls_2file(FILE *fp, Koliseo *kls) KLS_Conf_Arg(kls->conf)); fprintf(fp, "\n[INFO] Stats: { " KLS_Stats_Fmt " }\n", KLS_Stats_Arg(kls->stats)); -#ifndef WINDOWS_BUILD +#ifndef _WIN32 fprintf(fp, "\n[KLS] Size: { %li }\n", kls->size); #else fprintf(fp, "\n[KLS] Size: { %lli }\n", kls->size); @@ -1942,7 +1942,7 @@ void print_kls_2file(FILE *fp, Koliseo *kls) fprintf(fp, "[KLS] Size (Human): { %s }\n", human_size); kls_formatSize(kls->offset, curr_size, sizeof(curr_size)); fprintf(fp, "[KLS] Used (Human): { %s }\n", curr_size); -#ifndef WINDOWS_BUILD +#ifndef _WIN32 fprintf(fp, "[KLS] Offset: { %li }\n", kls->offset); fprintf(fp, "[KLS] Prev_Offset: { %li }\n", kls->prev_offset); #else @@ -1993,7 +1993,7 @@ void print_temp_kls_2file(FILE *fp, Koliseo_Temp *t_kls) } else { Koliseo *kls = t_kls->kls; fprintf(fp, "\n[KLS_T] API Level: { %i }\n", int_koliseo_version()); -#ifndef WINDOWS_BUILD +#ifndef _WIN32 fprintf(fp, "\n[KLS_T] Temp Size: { %li }\n", kls->size - t_kls->offset); fprintf(fp, "\n[KLS_T] Refer Size: { %li }\n", kls->size); @@ -2013,14 +2013,14 @@ void print_temp_kls_2file(FILE *fp, Koliseo_Temp *t_kls) fprintf(fp, "[KLS_T] Inner Used (Human): { %s }\n", curr_size); kls_formatSize(t_kls->offset, curr_size, sizeof(curr_size)); fprintf(fp, "[KLS_T] Temp Used (Human): { %s }\n", curr_size); -#ifndef WINDOWS_BUILD +#ifndef _WIN32 fprintf(fp, "[KLS_T] Inner Offset: { %li }\n", kls->offset); fprintf(fp, "[KLS_T] Temp Offset: { %li }\n", t_kls->offset); #else fprintf(fp, "[KLS_T] Inner Offset: { %lli }\n", kls->offset); fprintf(fp, "[KLS_T] Temp Offset: { %lli }\n", t_kls->offset); #endif -#ifndef WINDOWS_BUILD +#ifndef _WIN32 fprintf(fp, "[KLS_T] Inner Prev_Offset: { %li }\n", kls->prev_offset); fprintf(fp, "[KLS_T] Temp Prev_Offset: { %li }\n\n", t_kls->prev_offset); @@ -2092,7 +2092,7 @@ void kls_show_toWin(Koliseo *kls, WINDOW *win) int x = 2; mvwprintw(win, y++, x, "Koliseo data:"); mvwprintw(win, y++, x, "API Level: { %i }", int_koliseo_version()); -#ifndef WINDOWS_BUILD +#ifndef _WIN32 mvwprintw(win, y++, x, "Size: { %li }", kls->size); #else mvwprintw(win, y++, x, "Size: { %lli }", kls->size); @@ -2103,19 +2103,19 @@ void kls_show_toWin(Koliseo *kls, WINDOW *win) char curr_size[200]; kls_formatSize(kls->offset, curr_size, sizeof(curr_size)); mvwprintw(win, y++, x, "Used (Human): { %s }", curr_size); -#ifndef WINDOWS_BUILD +#ifndef _WIN32 mvwprintw(win, y++, x, "Offset: { %li }", kls->offset); #else mvwprintw(win, y++, x, "Offset: { %lli }", kls->offset); #endif -#ifndef WINDOWS_BUILD +#ifndef _WIN32 mvwprintw(win, y++, x, "Prev_Offset: { %li }", kls->prev_offset); #else mvwprintw(win, y++, x, "Prev_Offset: { %lli }", kls->prev_offset); #endif #ifdef KOLISEO_HAS_REGION mvwprintw(win, y++, x, "KLS_Region_List len: { %i }", - kls_length(kls->regs)); + kls_rl_length(kls->regs)); #endif mvwprintw(win, y++, x, "Current usage: { %.3f%% }", (kls->offset * 100.0) / kls->size); @@ -2179,7 +2179,7 @@ void kls_temp_show_toWin(Koliseo_Temp *t_kls, WINDOW *win) int x = 2; mvwprintw(win, y++, x, "Koliseo_Temp data:"); mvwprintw(win, y++, x, "API Level: { %i }", int_koliseo_version()); -#ifndef WINDOWS_BUILD +#ifndef _WIN32 mvwprintw(win, y++, x, "Temp Size: { %li }", kls->size - t_kls->offset); mvwprintw(win, y++, x, "Refer Size: { %li }", kls->size); #else @@ -2196,14 +2196,14 @@ void kls_temp_show_toWin(Koliseo_Temp *t_kls, WINDOW *win) mvwprintw(win, y++, x, "Inner Used (Human): { %s }", curr_size); kls_formatSize(t_kls->offset, curr_size, sizeof(curr_size)); mvwprintw(win, y++, x, "Temp Used (Human): { %s }", curr_size); -#ifndef WINDOWS_BUILD +#ifndef _WIN32 mvwprintw(win, y++, x, "Inner Offset: { %li }", kls->offset); mvwprintw(win, y++, x, "Temp Offset: { %li }", t_kls->offset); #else mvwprintw(win, y++, x, "Inner Offset: { %lli }", kls->offset); mvwprintw(win, y++, x, "Temp Offset: { %lli }", t_kls->offset); #endif -#ifndef WINDOWS_BUILD +#ifndef _WIN32 mvwprintw(win, y++, x, "Inner Prev_Offset: { %li }", kls->prev_offset); mvwprintw(win, y++, x, "Temp Prev_Offset: { %li }", t_kls->prev_offset); #else @@ -2212,9 +2212,9 @@ void kls_temp_show_toWin(Koliseo_Temp *t_kls, WINDOW *win) #endif #ifdef KOLISEO_HAS_REGION mvwprintw(win, y++, x, "Refer KLS_Region_List len: { %i }", - kls_length(kls->regs)); + kls_rl_length(kls->regs)); mvwprintw(win, y++, x, "Temp KLS_Region_List len: { %i }", - kls_length(t_kls->t_regs)); + kls_rl_length(t_kls->t_regs)); #endif mvwprintw(win, y++, x, "Current inner usage: { %.3f%% }", (kls->offset * 100.0) / kls->size); @@ -2276,13 +2276,13 @@ void kls_showList_toWin(Koliseo *kls, WINDOW *win) int quit = 0; mvwprintw(win, y++, x, "KLS_Region_List data:"); KLS_Region_List rl = kls->regs; - while (!quit && !kls_empty(rl)) { + while (!quit && !kls_rl_empty(rl)) { wclear(win); y = 3; - KLS_list_element e = kls_head(rl); + KLS_list_element e = kls_rl_head(rl); mvwprintw(win, y++, x, "Name: { %s }", e->name); mvwprintw(win, y++, x, "Desc: { %s }", e->desc); -#ifndef WINDOWS_BUILD +#ifndef _WIN32 mvwprintw(win, y++, x, "Offsets: { %li } -> { %li }", e->begin_offset, e->end_offset); mvwprintw(win, y++, x, "Size: { %li }", e->size); @@ -2294,7 +2294,7 @@ void kls_showList_toWin(Koliseo *kls, WINDOW *win) mvwprintw(win, y++, x, "Padding: { %lli }", e->padding); #endif mvwprintw(win, y++, x, "KLS_Region_List len: { %i }", - kls_length(kls->regs)); + kls_rl_length(kls->regs)); mvwprintw(win, y++, x, "Current usage: { %.3f%% }", kls_usageShare(e, kls)); char h_size[200]; @@ -2318,7 +2318,7 @@ void kls_showList_toWin(Koliseo *kls, WINDOW *win) ch = wgetch(win); switch (ch) { case KEY_RIGHT: { - rl = kls_tail(rl); + rl = kls_rl_tail(rl); picked = 1; } break; @@ -2371,13 +2371,13 @@ void kls_temp_showList_toWin(Koliseo_Temp *t_kls, WINDOW *win) int quit = 0; mvwprintw(win, y++, x, "KLS_Region_List data:"); KLS_Region_List rl = t_kls->t_regs; - while (!quit && !kls_empty(rl)) { + while (!quit && !kls_rl_empty(rl)) { wclear(win); y = 3; - KLS_list_element e = kls_head(rl); + KLS_list_element e = kls_rl_head(rl); mvwprintw(win, y++, x, "Name: { %s }", e->name); mvwprintw(win, y++, x, "Desc: { %s }", e->desc); -#ifndef WINDOWS_BUILD +#ifndef _WIN32 mvwprintw(win, y++, x, "Offsets: { %li } -> { %li }", e->begin_offset, e->end_offset); mvwprintw(win, y++, x, "Size: { %li }", e->size); @@ -2389,7 +2389,7 @@ void kls_temp_showList_toWin(Koliseo_Temp *t_kls, WINDOW *win) mvwprintw(win, y++, x, "Padding: { %lli }", e->padding); #endif mvwprintw(win, y++, x, "KLS_Region_List len: { %i }", - kls_length(t_kls->t_regs)); + kls_rl_length(t_kls->t_regs)); //mvwprintw(win, y++, x, "Current usage: { %.3f%% }", kls_usageShare(e,kls)); char h_size[200]; ptrdiff_t reg_size = e->end_offset - e->begin_offset; @@ -2412,7 +2412,7 @@ void kls_temp_showList_toWin(Koliseo_Temp *t_kls, WINDOW *win) ch = wgetch(win); switch (ch) { case KEY_RIGHT: { - rl = kls_tail(rl); + rl = kls_rl_tail(rl); picked = 1; } break; @@ -2501,7 +2501,7 @@ void kls_free(Koliseo *kls) kls_free(kls->reglist_kls); //free(kls->reglist_kls); } else { - kls_freeList(kls->regs); + kls_rl_freeList(kls->regs); } #endif free(kls); @@ -2620,8 +2620,8 @@ Koliseo_Temp *kls_temp_start(Koliseo *kls) strncpy(temp_kls_header->desc, "Last Reg b4 KLS_T", KLS_REGION_MAX_DESC_SIZE); temp_kls_header->desc[KLS_REGION_MAX_DESC_SIZE] = '\0'; - KLS_Region_List reglist = kls_emptyList(); - reglist = kls_t_cons(tmp, temp_kls_header, reglist); + KLS_Region_List reglist = kls_rl_emptyList(); + reglist = kls_rl_t_cons(tmp, temp_kls_header, reglist); tmp->t_regs = reglist; if (tmp->t_regs == NULL) { fprintf(stderr, "[KLS] [%s()]: failed to get a KLS_Region_List.\n", @@ -2675,7 +2675,7 @@ void kls_temp_end(Koliseo_Temp *tmp_kls) if (tmp_kls->conf.kls_autoset_regions == 1) { switch (tmp_kls->conf.tkls_reglist_alloc_backend) { case KLS_REGLIST_ALLOC_LIBC: { - kls_freeList(tmp_kls->t_regs); + kls_rl_freeList(tmp_kls->t_regs); } break; case KLS_REGLIST_ALLOC_KLS_BASIC: { @@ -2720,12 +2720,12 @@ void kls_temp_end(Koliseo_Temp *tmp_kls) #ifdef KOLISEO_HAS_REGION -KLS_Region_List kls_emptyList(void) +KLS_Region_List kls_rl_emptyList(void) { return NULL; } -bool kls_empty(KLS_Region_List l) +bool kls_rl_empty(KLS_Region_List l) { if (l == NULL) { return true; @@ -2734,25 +2734,25 @@ bool kls_empty(KLS_Region_List l) } } -KLS_list_element kls_head(KLS_Region_List l) +KLS_list_element kls_rl_head(KLS_Region_List l) { - if (kls_empty(l)) { + if (kls_rl_empty(l)) { exit(EXIT_FAILURE); } else { return l->value; } } -KLS_Region_List kls_tail(KLS_Region_List l) +KLS_Region_List kls_rl_tail(KLS_Region_List l) { - if (kls_empty(l)) { + if (kls_rl_empty(l)) { exit(EXIT_FAILURE); } else { return l->next; } } -KLS_Region_List kls_cons(Koliseo *kls, KLS_list_element e, KLS_Region_List l) +KLS_Region_List kls_rl_cons(Koliseo *kls, KLS_list_element e, KLS_Region_List l) { if (e == NULL) { #ifdef KLS_DEBUG_CORE @@ -2853,8 +2853,8 @@ KLS_region_list_item* kls_list_pop(Koliseo *kls) } #endif // KOLISEO_HAS_EXPER -KLS_Region_List kls_t_cons(Koliseo_Temp *t_kls, KLS_list_element e, - KLS_Region_List l) +KLS_Region_List kls_rl_t_cons(Koliseo_Temp *t_kls, KLS_list_element e, + KLS_Region_List l) { if (e == NULL) { #ifdef KLS_DEBUG_CORE @@ -2944,12 +2944,17 @@ KLS_region_list_item* kls_t_list_pop(Koliseo_Temp *t_kls) } #endif // KOLISEO_HAS_EXPER -void kls_freeList(KLS_Region_List l) +/** + * Frees all values and nodes for passed Region list. + * Should only be used internally for operations with ALLOC_LIBC for allocation backend. + * @param l The list to free (allocated by using malloc). + */ +void kls_rl_freeList(KLS_Region_List l) { - if (kls_empty(l)) { + if (kls_rl_empty(l)) { return; } else { - kls_freeList(kls_tail(l)); + kls_rl_freeList(kls_rl_tail(l)); #ifdef KLS_DEBUG_CORE fprintf(stderr, "[KLS] %s(): Freeing KLS_Region_List->value.\n", __func__); @@ -2963,7 +2968,7 @@ void kls_freeList(KLS_Region_List l) return; } -void kls_showList_toFile(KLS_Region_List l, FILE *fp) +void kls_rl_showList_toFile(KLS_Region_List l, FILE *fp) { if (fp == NULL) { fprintf(stderr, @@ -2971,15 +2976,15 @@ void kls_showList_toFile(KLS_Region_List l, FILE *fp) exit(EXIT_FAILURE); } fprintf(fp, "{"); - while (!kls_empty(l)) { - fprintf(fp, "\n{ %s }, { %s } ", kls_head(l)->name, - kls_head(l)->desc); -#ifndef WINDOWS_BUILD - fprintf(fp, "{ %li } -> { %li }", kls_head(l)->begin_offset, - kls_head(l)->end_offset); + while (!kls_rl_empty(l)) { + fprintf(fp, "\n{ %s }, { %s } ", kls_rl_head(l)->name, + kls_rl_head(l)->desc); +#ifndef _WIN32 + fprintf(fp, "{ %li } -> { %li }", kls_rl_head(l)->begin_offset, + kls_rl_head(l)->end_offset); #else - fprintf(fp, "{ %lli } -> { %lli }", kls_head(l)->begin_offset, - kls_head(l)->end_offset); + fprintf(fp, "{ %lli } -> { %lli }", kls_rl_head(l)->begin_offset, + kls_rl_head(l)->end_offset); #endif /* #ifdef KLS_DEBUG_CORE @@ -2994,118 +2999,118 @@ void kls_showList_toFile(KLS_Region_List l, FILE *fp) #endif */ - l = kls_tail(l); - if (!kls_empty(l)) { + l = kls_rl_tail(l); + if (!kls_rl_empty(l)) { fprintf(fp, ",\n"); } } fprintf(fp, "\n}\n"); } -void kls_showList(KLS_Region_List l) +void kls_rl_showList(KLS_Region_List l) { - kls_showList_toFile(l, stdout); + kls_rl_showList_toFile(l, stdout); } -bool kls_member(KLS_list_element el, KLS_Region_List l) +bool kls_rl_member(KLS_list_element el, KLS_Region_List l) { - if (kls_empty(l)) { + if (kls_rl_empty(l)) { return false; } else { - if (el == kls_head(l)) { + if (el == kls_rl_head(l)) { return true; } else { - return kls_member(el, kls_tail(l)); + return kls_rl_member(el, kls_rl_tail(l)); } } } -int kls_length(KLS_Region_List l) +int kls_rl_length(KLS_Region_List l) { - if (kls_empty(l)) { + if (kls_rl_empty(l)) { return 0; } else { - return 1 + kls_length(kls_tail(l)); + return 1 + kls_rl_length(kls_rl_tail(l)); } } -KLS_Region_List kls_append(Koliseo *kls, KLS_Region_List l1, KLS_Region_List l2) +KLS_Region_List kls_rl_append(Koliseo *kls, KLS_Region_List l1, KLS_Region_List l2) { if (kls == NULL) { fprintf(stderr, "[ERROR] [%s()]: Koliseo was NULL.\n", __func__); exit(EXIT_FAILURE); } - if (kls_empty(l1)) { + if (kls_rl_empty(l1)) { return l2; } else { - return kls_cons(kls, kls_head(l1), kls_append(kls, kls_tail(l1), l2)); + return kls_rl_cons(kls, kls_rl_head(l1), kls_rl_append(kls, kls_rl_tail(l1), l2)); } } -KLS_Region_List kls_reverse(Koliseo *kls, KLS_Region_List l) +KLS_Region_List kls_rl_reverse(Koliseo *kls, KLS_Region_List l) { if (kls == NULL) { fprintf(stderr, "[ERROR] [%s()]: Koliseo was NULL.\n", __func__); exit(EXIT_FAILURE); } - if (kls_empty(l)) { - return kls_emptyList(); + if (kls_rl_empty(l)) { + return kls_rl_emptyList(); } else { - return kls_append(kls, kls_reverse(kls, kls_tail(l)), - kls_cons(kls, kls_head(l), kls_emptyList())); + return kls_rl_append(kls, kls_rl_reverse(kls, kls_rl_tail(l)), + kls_rl_cons(kls, kls_rl_head(l), kls_rl_emptyList())); } } -KLS_Region_List kls_copy(Koliseo *kls, KLS_Region_List l) +KLS_Region_List kls_rl_copy(Koliseo *kls, KLS_Region_List l) { if (kls == NULL) { fprintf(stderr, "[ERROR] [%s()]: Koliseo was NULL.\n", __func__); exit(EXIT_FAILURE); } - if (kls_empty(l)) { + if (kls_rl_empty(l)) { return l; } else { - return kls_cons(kls, kls_head(l), kls_copy(kls, kls_tail(l))); + return kls_rl_cons(kls, kls_rl_head(l), kls_rl_copy(kls, kls_rl_tail(l))); } } -KLS_Region_List kls_delete(Koliseo *kls, KLS_list_element el, KLS_Region_List l) +KLS_Region_List kls_rl_delete(Koliseo *kls, KLS_list_element el, KLS_Region_List l) { if (kls == NULL) { fprintf(stderr, "[ERROR] [%s()]: Koliseo was NULL.\n", __func__); exit(EXIT_FAILURE); } - if (kls_empty(l)) { - return kls_emptyList(); + if (kls_rl_empty(l)) { + return kls_rl_emptyList(); } else { - if (el == kls_head(l)) { - return kls_tail(l); + if (el == kls_rl_head(l)) { + return kls_rl_tail(l); } else { - return kls_cons(kls, kls_head(l), kls_delete(kls, el, kls_tail(l))); + return kls_rl_cons(kls, kls_rl_head(l), kls_rl_delete(kls, el, kls_rl_tail(l))); } } } -KLS_Region_List kls_insord(Koliseo *kls, KLS_list_element el, KLS_Region_List l) +KLS_Region_List kls_rl_insord(Koliseo *kls, KLS_list_element el, KLS_Region_List l) { if (kls == NULL) { fprintf(stderr, "[ERROR] [%s()]: Koliseo was NULL.\n", __func__); exit(EXIT_FAILURE); } - if (kls_empty(l)) { - return kls_cons(kls, el, l); + if (kls_rl_empty(l)) { + return kls_rl_cons(kls, el, l); } else { //Insert KLS_list_element according to its begin_offset - if (el->begin_offset <= kls_head(l)->begin_offset) { - return kls_cons(kls, el, l); + if (el->begin_offset <= kls_rl_head(l)->begin_offset) { + return kls_rl_cons(kls, el, l); } else { - return kls_cons(kls, kls_head(l), kls_insord(kls, el, kls_tail(l))); + return kls_rl_cons(kls, kls_rl_head(l), kls_rl_insord(kls, el, kls_rl_tail(l))); } } } -KLS_Region_List kls_insord_p(Koliseo *kls, KLS_list_element el, - KLS_Region_List l) +KLS_Region_List kls_rl_insord_p(Koliseo *kls, KLS_list_element el, + KLS_Region_List l) { if (kls == NULL) { fprintf(stderr, "[ERROR] [%s()]: Koliseo was NULL.\n", __func__); @@ -3167,73 +3172,73 @@ KLS_Region_List kls_insord_p(Koliseo *kls, KLS_list_element el, } } -KLS_Region_List kls_mergeList(Koliseo *kls, KLS_Region_List l1, - KLS_Region_List l2) +KLS_Region_List kls_rl_mergeList(Koliseo *kls, KLS_Region_List l1, + KLS_Region_List l2) { if (kls == NULL) { fprintf(stderr, "[ERROR] [%s()]: Koliseo was NULL.\n", __func__); exit(EXIT_FAILURE); } - if (kls_empty(l1)) { + if (kls_rl_empty(l1)) { return l2; } else { - if (kls_empty(l2)) { + if (kls_rl_empty(l2)) { return l1; } else { - if (kls_isLess(kls_head(l1), kls_head(l2))) { - return kls_cons(kls, kls_head(l1), - kls_mergeList(kls, kls_tail(l1), l2)); + if (kls_rl_isLess(kls_rl_head(l1), kls_rl_head(l2))) { + return kls_rl_cons(kls, kls_rl_head(l1), + kls_rl_mergeList(kls, kls_rl_tail(l1), l2)); } else { - if (kls_isEqual(kls_head(l1), kls_head(l2))) { - return kls_cons(kls, kls_head(l1), - kls_mergeList(kls, kls_tail(l1), - kls_tail(l2))); + if (kls_rl_isEqual(kls_rl_head(l1), kls_rl_head(l2))) { + return kls_rl_cons(kls, kls_rl_head(l1), + kls_rl_mergeList(kls, kls_rl_tail(l1), + kls_rl_tail(l2))); } else { - return kls_cons(kls, kls_head(l2), - kls_mergeList(kls, l1, kls_tail(l2))); + return kls_rl_cons(kls, kls_rl_head(l2), + kls_rl_mergeList(kls, l1, kls_rl_tail(l2))); } } } } } -KLS_Region_List kls_intersect(Koliseo *kls, KLS_Region_List l1, - KLS_Region_List l2) +KLS_Region_List kls_rl_intersect(Koliseo *kls, KLS_Region_List l1, + KLS_Region_List l2) { if (kls == NULL) { fprintf(stderr, "[ERROR] [%s()]: Koliseo was NULL.\n", __func__); exit(EXIT_FAILURE); } - if (kls_empty(l1) || kls_empty(l2)) { - return kls_emptyList(); + if (kls_rl_empty(l1) || kls_rl_empty(l2)) { + return kls_rl_emptyList(); } - if (kls_member(kls_head(l1), l2) && !kls_member(kls_head(l1), kls_tail(l1))) { - return kls_cons(kls, kls_head(l1), - kls_intersect(kls, kls_tail(l1), l2)); + if (kls_rl_member(kls_rl_head(l1), l2) && !kls_rl_member(kls_rl_head(l1), kls_rl_tail(l1))) { + return kls_rl_cons(kls, kls_rl_head(l1), + kls_rl_intersect(kls, kls_rl_tail(l1), l2)); } else { - return kls_intersect(kls, kls_tail(l1), l2); + return kls_rl_intersect(kls, kls_rl_tail(l1), l2); } } -KLS_Region_List kls_diff(Koliseo *kls, KLS_Region_List l1, KLS_Region_List l2) +KLS_Region_List kls_rl_diff(Koliseo *kls, KLS_Region_List l1, KLS_Region_List l2) { if (kls == NULL) { fprintf(stderr, "[ERROR] [%s()]: Koliseo was NULL.\n", __func__); exit(EXIT_FAILURE); } - if (kls_empty(l1) || kls_empty(l2)) { + if (kls_rl_empty(l1) || kls_rl_empty(l2)) { return l1; } else { - if (!kls_member(kls_head(l1), l2) - && !kls_member(kls_head(l1), kls_tail(l1))) { - return kls_cons(kls, kls_head(l1), kls_diff(kls, kls_tail(l1), l2)); + if (!kls_rl_member(kls_rl_head(l1), l2) + && !kls_rl_member(kls_rl_head(l1), kls_rl_tail(l1))) { + return kls_rl_cons(kls, kls_rl_head(l1), kls_rl_diff(kls, kls_rl_tail(l1), l2)); } else { - return kls_diff(kls, kls_tail(l1), l2); + return kls_rl_diff(kls, kls_rl_tail(l1), l2); } } } @@ -3244,7 +3249,7 @@ KLS_Region_List kls_diff(Koliseo *kls, KLS_Region_List l1, KLS_Region_List l2) * @param r2 The KLS_Region expected to be bigger * @return True if first region size is less than second region size. */ -bool kls_isLess(KLS_Region *r1, KLS_Region *r2) +bool kls_rl_isLess(KLS_Region *r1, KLS_Region *r2) { //Compare regions by their effective size ptrdiff_t s1 = r1->end_offset - r1->begin_offset; @@ -3258,7 +3263,7 @@ bool kls_isLess(KLS_Region *r1, KLS_Region *r2) * @param r2 The second KLS_Region * @return True if first region size is equal than second region size. */ -bool kls_isEqual(KLS_Region *r1, KLS_Region *r2) +bool kls_rl_isEqual(KLS_Region *r1, KLS_Region *r2) { //Compare regions by their effective size ptrdiff_t s1 = r1->end_offset - r1->begin_offset; @@ -3310,10 +3315,10 @@ ptrdiff_t kls_avg_regionSize(Koliseo *kls) } KLS_Region_List rl = kls->regs; ptrdiff_t res = 0; - int tot_regs = kls_length(rl); + int tot_regs = kls_rl_length(rl); if (tot_regs > 0) { int tot_size = 0; - while (!kls_empty(rl)) { + while (!kls_rl_empty(rl)) { ptrdiff_t curr_size = 0; if (rl->value->size > 0) { curr_size = rl->value->size; @@ -3322,7 +3327,7 @@ ptrdiff_t kls_avg_regionSize(Koliseo *kls) rl->value->size = curr_size; } tot_size += curr_size; - rl = kls_tail(rl); + rl = kls_rl_tail(rl); } res = (ptrdiff_t)((double)tot_size / tot_regs); } @@ -3352,10 +3357,10 @@ void kls_usageReport_toFile(Koliseo *kls, FILE *fp) } KLS_Region_List rl = kls->regs; int i = 0; - while (!kls_empty(rl)) { + while (!kls_rl_empty(rl)) { fprintf(fp, "Usage for region (%i) [%s]: [%.3f%%]\n", i, rl->value->name, kls_usageShare(rl->value, kls)); - rl = kls_tail(rl); + rl = kls_rl_tail(rl); i++; } } @@ -3387,12 +3392,12 @@ ptrdiff_t kls_type_usage(int type, Koliseo *kls) ptrdiff_t res = 0; - while (!kls_empty(rl)) { - KLS_list_element h = kls_head(rl); + while (!kls_rl_empty(rl)) { + KLS_list_element h = kls_rl_head(rl); if (h->type == type) { res += (h->end_offset - h->begin_offset); } - rl = kls_tail(rl); + rl = kls_rl_tail(rl); } return res; @@ -4089,7 +4094,7 @@ void *kls_temp_pop(Koliseo_Temp *t_kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t padding = -kls->offset & (align - 1); if (count > PTRDIFF_MAX / size || (kls->size + kls->offset) < (size * count)) { - fprintf(stderr, "[KLS] Failed kls_temp_pop() call.\n"); + fprintf(stderr, "[KLS] Failed %s() call.\n", __func__); kls_free(kls); exit(EXIT_FAILURE); } @@ -4134,7 +4139,7 @@ void *kls_temp_pop_AR(Koliseo_Temp *t_kls, ptrdiff_t size, ptrdiff_t align, ptrd ptrdiff_t padding = -kls->offset & (align - 1); if (count > PTRDIFF_MAX / size || (kls->size + kls->offset) < (size * count)) { - fprintf(stderr, "[KLS] Failed kls_temp_pop() call.\n"); + fprintf(stderr, "[KLS] Failed %s() call.\n", __func__); kls_free(kls); exit(EXIT_FAILURE); } diff --git a/src/koliseo.h b/src/koliseo.h index 6e5ff69..344fa5f 100644 --- a/src/koliseo.h +++ b/src/koliseo.h @@ -45,7 +45,7 @@ #define KLS_MAJOR 0 /**< Represents current major release.*/ #define KLS_MINOR 4 /**< Represents current minor release.*/ -#define KLS_PATCH 1 /**< Represents current patch release.*/ +#define KLS_PATCH 2 /**< Represents current patch release.*/ typedef void*(kls_alloc_func)(size_t); /**< Used to select an allocation function for the arena's backing memory.*/ @@ -250,7 +250,7 @@ static const int KOLISEO_API_VERSION_INT = /** * Defines current API version string. */ -static const char KOLISEO_API_VERSION_STRING[] = "0.4.1"; /**< Represents current version with MAJOR.MINOR.PATCH format.*/ +static const char KOLISEO_API_VERSION_STRING[] = "0.4.2"; /**< Represents current version with MAJOR.MINOR.PATCH format.*/ /** * Returns current koliseo version as a string. @@ -514,11 +514,11 @@ void kls_formatSize(ptrdiff_t size, char *outputBuffer, size_t bufferSize); #ifndef KOLISEO_CURSES_H_ #define KOLISEO_CURSES_H_ -#ifndef WINDOWS_BUILD +#ifndef _WIN32 #include "ncurses.h" #else #include -#endif //WINDOWS_BUILD +#endif // _WIN32 void kls_show_toWin(Koliseo * kls, WINDOW * win); void kls_temp_show_toWin(Koliseo_Temp * t_kls, WINDOW * win); @@ -563,7 +563,7 @@ void print_dbg_temp_kls(Koliseo_Temp * t_kls); #ifdef KOLISEO_HAS_REGION #define KLS_PUSH_ARR_T_NAMED(kls_temp, type, count, name, desc) (type*)kls_temp_push_zero_named((kls_temp), sizeof(type), _Alignof(type), (count), (name), (desc)) #else -#define KLS_PUSH_ARR_T_NAMED(kls_temp, type, count, name, desc) KLS_PUSH_ARR_T((kls_temp),(type),(count)) +#define KLS_PUSH_ARR_T_NAMED(kls_temp, type, count, name, desc) KLS_PUSH_ARR_T((kls_temp),type,(count)) #endif // KOLISEO_HAS_REGION /** @@ -614,44 +614,46 @@ void print_dbg_temp_kls(Koliseo_Temp * t_kls); #ifdef KOLISEO_HAS_REGION -KLS_Region_List kls_emptyList(void); -#define KLS_GETLIST() kls_emptyList() -bool kls_empty(KLS_Region_List); -KLS_list_element kls_head(KLS_Region_List); -KLS_Region_List kls_tail(KLS_Region_List); -KLS_Region_List kls_cons(Koliseo *, KLS_list_element, KLS_Region_List); +KLS_Region_List kls_rl_emptyList(void); +#define KLS_RL_GETLIST() kls_rl_emptyList() +bool kls_rl_empty(KLS_Region_List); +KLS_list_element kls_rl_head(KLS_Region_List); +KLS_Region_List kls_rl_tail(KLS_Region_List); +KLS_Region_List kls_rl_cons(Koliseo *, KLS_list_element, KLS_Region_List); #ifdef KOLISEO_HAS_EXPER -KLS_region_list_item* kls_list_pop(Koliseo *kls); +KLS_region_list_item* kls_rl_list_pop(Koliseo *kls); #endif // KOLISEO_HAS_EXPER -KLS_Region_List kls_t_cons(Koliseo_Temp *, KLS_list_element, KLS_Region_List); +KLS_Region_List kls_rl_t_cons(Koliseo_Temp *, KLS_list_element, KLS_Region_List); #ifdef KOLISEO_HAS_EXPER -KLS_region_list_item* kls_t_list_pop(Koliseo_Temp *t_kls); +KLS_region_list_item* kls_rl_t_list_pop(Koliseo_Temp *t_kls); #endif // KOLISEO_HAS_EXPER -void kls_freeList(KLS_Region_List); -#define KLS_FREELIST(kls_list) kls_freeList(kls_list) -void kls_showList(KLS_Region_List); -void kls_showList_toFile(KLS_Region_List, FILE * fp); -#define KLS_ECHOLIST(kls_list) kls_showList(kls_list) -#define KLS_PRINTLIST(kls_list,file) kls_showList_toFile(kls_list,file) -bool kls_member(KLS_list_element, KLS_Region_List); -int kls_length(KLS_Region_List); -KLS_Region_List kls_append(Koliseo *, KLS_Region_List, KLS_Region_List); -KLS_Region_List kls_reverse(Koliseo *, KLS_Region_List); -KLS_Region_List kls_copy(Koliseo *, KLS_Region_List); -KLS_Region_List kls_delete(Koliseo *, KLS_list_element, KLS_Region_List); - -KLS_Region_List kls_insord(Koliseo *, KLS_list_element, KLS_Region_List); -#define KLS_PUSHLIST(kls,reg,kls_list) kls_insord(kls,reg,kls_list) -KLS_Region_List kls_insord_p(Koliseo *, KLS_list_element, KLS_Region_List); -#define KLS_PUSHLIST_P(kls,reg,kls_list) kls_insord_p(kls,reg,kls_list) -KLS_Region_List kls_mergeList(Koliseo *, KLS_Region_List, KLS_Region_List); -KLS_Region_List kls_intersect(Koliseo *, KLS_Region_List, KLS_Region_List); -KLS_Region_List kls_diff(Koliseo *, KLS_Region_List, KLS_Region_List); - -#define KLS_DIFF(kls,kls_list1,kls_list2) kls_diff(kls,kls_list1,kls_list2) -bool kls_isLess(KLS_list_element, KLS_list_element); -bool kls_isEqual(KLS_list_element, KLS_list_element); +void kls_rl_freeList(KLS_Region_List); +#define KLS_RL_FREELIST(kls_list) kls_rl_freeList(kls_list) +void kls_rl_showList(KLS_Region_List); +#define kls_showList(list) kls_rl_showList((list)) +void kls_rl_showList_toFile(KLS_Region_List, FILE * fp); +#define kls_showList_toFile(list, fp) kls_rl_showList_toFile((list), (fp)) +#define KLS_RL_ECHOLIST(kls_list) kls_rl_showList(kls_list) +#define KLS_RL_PRINTLIST(kls_list,file) kls_rl_showList_toFile(kls_list,file) +bool kls_rl_member(KLS_list_element, KLS_Region_List); +int kls_rl_length(KLS_Region_List); +KLS_Region_List kls_rl_append(Koliseo *, KLS_Region_List, KLS_Region_List); +KLS_Region_List kls_rl_reverse(Koliseo *, KLS_Region_List); +KLS_Region_List kls_rl_copy(Koliseo *, KLS_Region_List); +KLS_Region_List kls_rl_delete(Koliseo *, KLS_list_element, KLS_Region_List); + +KLS_Region_List kls_rl_insord(Koliseo *, KLS_list_element, KLS_Region_List); +#define KLS_RL_PUSHLIST(kls,reg,kls_list) kls_rl_insord(kls,reg,kls_list) +KLS_Region_List kls_rl_insord_p(Koliseo *, KLS_list_element, KLS_Region_List); +#define KLS_RL_PUSHLIST_P(kls,reg,kls_list) kls_rl_insord_p(kls,reg,kls_list) +KLS_Region_List kls_rl_mergeList(Koliseo *, KLS_Region_List, KLS_Region_List); +KLS_Region_List kls_rl_intersect(Koliseo *, KLS_Region_List, KLS_Region_List); +KLS_Region_List kls_rl_diff(Koliseo *, KLS_Region_List, KLS_Region_List); + +#define KLS_RL_DIFF(kls,kls_list1,kls_list2) kls_rl_diff(kls,kls_list1,kls_list2) +bool kls_rl_isLess(KLS_list_element, KLS_list_element); +bool kls_rl_isEqual(KLS_list_element, KLS_list_element); double kls_usageShare(KLS_list_element, Koliseo *); ptrdiff_t kls_regionSize(KLS_list_element); ptrdiff_t kls_avg_regionSize(Koliseo *); @@ -840,3 +842,524 @@ char** kls_t_strdup_arr(Koliseo_Temp* t_kls, size_t count, char** source); #endif // __STDC_VERSION__ && __STDC_VERSION__ >= 201112L //We need C11 #endif //KOLISEO_H_ + +#ifdef LIST_T //This ensures the library never causes any trouble if this macro was not defined. +// jgabaut @ github.com/jgabaut +// SPDX-License-Identifier: GPL-3.0-only +/* + Copyright (C) 2023-2024 jgabaut + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, version 3 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +// list.h +// This is a template for a linked list, inspired by the dynamic array example in https://www.davidpriver.com/ctemplates.html#template-headers. +// Include this header multiple times to implement a +// simplistic linked list. Before inclusion define at +// least DLIST_T to the type the linked list can hold. +// See DLIST_NAME, DLIST_PREFIX and DLIST_LINKAGE for +// other customization points. +// +// If you define DLIST_DECLS_ONLY, only the declarations +// of the type and its function will be declared. +// +// Functions ending with _gl use malloc() for the nodes. +// Functions ending with _kls expect a Koliseo arg to use for allocating nodes. +// + +#ifndef LIST_HEADER_H +#define LIST_HEADER_H +// Inline functions, #defines and includes that will be +// needed for all instantiations can go up here. +#include // bool +#include // malloc, size_t + +#define LIST_IMPL(word) LIST_COMB1(LIST_PREFIX,word) +#define LIST_COMB1(pre, word) LIST_COMB2(pre, word) +#define LIST_COMB2(pre, word) pre##word + +#define LIST_HEADER_VERSION "0.1.0" + +#endif // LIST_HEADER_H + +// NOTE: this section is *not* guarded as it is intended +// to be included multiple times. + +#ifndef LIST_T +#error "LIST_T must be defined" +#endif + +// The name of the data type to be generated. +// If not given, will expand to something like +// `list_int` for an `int`. +#ifndef LIST_NAME +#define LIST_NAME LIST_COMB1(LIST_COMB1(list,_), LIST_T) +#endif + +// Prefix for generated functions. +#ifndef LIST_PREFIX +#define LIST_PREFIX LIST_COMB1(LIST_NAME, _) +#endif + +// Customize the linkage of the function. +#ifndef LIST_LINKAGE +#define LIST_LINKAGE static inline +#endif + +// Suffix for generated list item struct. +#ifndef LIST_I_SUFFIX +#define LIST_I_SUFFIX item +#endif + +// The name of the item data type to be generated. +#ifndef LIST_ITEM_NAME +#define LIST_ITEM_NAME LIST_COMB1(LIST_COMB1(LIST_T,_), LIST_I_SUFFIX) +#endif + +typedef struct LIST_ITEM_NAME LIST_ITEM_NAME; +struct LIST_ITEM_NAME { + LIST_T* value; + struct LIST_ITEM_NAME* next; +}; +typedef LIST_ITEM_NAME* LIST_NAME; + +#define LIST_nullList LIST_IMPL(nullList) +#define LIST_isEmpty LIST_IMPL(isEmpty) +#define LIST_head LIST_IMPL(head) +#define LIST_tail LIST_IMPL(tail) +#define LIST_cons_gl LIST_IMPL(cons_gl) +#define LIST_cons_kls LIST_IMPL(cons_kls) +#define LIST_free_gl LIST_IMPL(free_gl) +#define LIST_member LIST_IMPL(member) +#define LIST_length LIST_IMPL(length) +#define LIST_append_gl LIST_IMPL(append_gl) +#define LIST_append_kls LIST_IMPL(append_kls) +#define LIST_reverse_gl LIST_IMPL(reverse_gl) +#define LIST_reverse_kls LIST_IMPL(reverse_kls) +#define LIST_copy_gl LIST_IMPL(copy_gl) +#define LIST_copy_kls LIST_IMPL(copy_kls) +#define LIST_remove_gl LIST_IMPL(remove_gl) +#define LIST_remove_kls LIST_IMPL(remove_kls) +#define LIST_intersect_gl LIST_IMPL(intersect_gl) +#define LIST_intersect_kls LIST_IMPL(intersect_kls) +#define LIST_diff_gl LIST_IMPL(diff_gl) +#define LIST_diff_kls LIST_IMPL(diff_kls) + +#ifdef LIST_DECLS_ONLY + +LIST_LINKAGE +LIST_NAME +LIST_nullList(void); + +LIST_LINKAGE +bool +LIST_isEmpty(LIST_NAME list); + +LIST_LINKAGE +LIST_T* +LIST_head(LIST_NAME list); + +LIST_LINKAGE +LIST_NAME +LIST_tail(LIST_NAME list); + +LIST_LINKAGE +LIST_NAME +LIST_cons_gl(LIST_T* element, LIST_NAME list); + +LIST_LINKAGE +LIST_NAME +LIST_cons_kls(Koliseo* kls, LIST_T* element, LIST_NAME list); + +LIST_LINKAGE +void +LIST_free_gl(LIST_NAME list); + +LIST_LINKAGE +bool +LIST_member(LIST_T* element, LIST_NAME list); + +LIST_LINKAGE +int +LIST_length(LIST_NAME list); + +LIST_LINKAGE +LIST_NAME +LIST_append_gl(LIST_NAME l1, LIST_NAME l2); + +LIST_LINKAGE +LIST_NAME +LIST_append_kls(Koliseo* kls, LIST_NAME l1, LIST_NAME l2); + +LIST_LINKAGE +LIST_NAME +LIST_reverse_gl(LIST_NAME list); + +LIST_LINKAGE +LIST_NAME +LIST_reverse_kls(Koliseo* kls, LIST_NAME list); + +LIST_LINKAGE +LIST_NAME +LIST_copy_gl(LIST_NAME list); + +LIST_LINKAGE +LIST_NAME +LIST_copy_kls(Koliseo* kls, LIST_NAME list); + +LIST_LINKAGE +LIST_NAME +LIST_remove_gl(LIST_T* element, LIST_NAME list); + +LIST_LINKAGE +LIST_NAME +LIST_remove_kls(Koliseo* kls, LIST_T* element, LIST_NAME list); + +LIST_LINKAGE +LIST_NAME +LIST_intersect_gl(LIST_NAME l1, LIST_NAME l2); + +LIST_LINKAGE +LIST_NAME +LIST_intersect_kls(Koliseo* kls, LIST_NAME l1, LIST_NAME l2); + +LIST_LINKAGE +LIST_NAME +LIST_diff_gl(LIST_NAME l1, LIST_NAME l2); + +LIST_LINKAGE +LIST_NAME +LIST_diff_kls(Koliseo* kls, LIST_NAME l1, LIST_NAME l2); +#else + +LIST_LINKAGE +LIST_NAME +LIST_nullList(void) +{ + return NULL; +} + +LIST_LINKAGE +bool +LIST_isEmpty(LIST_NAME list) +{ + if (list == NULL) { + return true; + }; + return false; +} + +LIST_LINKAGE +LIST_T* +LIST_head(LIST_NAME list) +{ + if (LIST_isEmpty(list)) { + fprintf(stderr, "%s at %i: %s(): List is empty.\n", __FILE__, __LINE__, __func__); + return NULL; + } + return list->value; +} + +LIST_LINKAGE +LIST_NAME +LIST_tail(LIST_NAME list) +{ + if (LIST_isEmpty(list)) { + fprintf(stderr, "%s at %i: %s(): List is empty.\n", __FILE__, __LINE__, __func__); + return NULL; + } + return list->next; +} + +LIST_LINKAGE +LIST_NAME +LIST_cons_gl(LIST_T* element, LIST_NAME list) +{ + LIST_NAME t; + t = (LIST_NAME) malloc(sizeof(LIST_ITEM_NAME)); + t->value = element; + t->next = list; + return t; +} + +LIST_LINKAGE +LIST_NAME +LIST_cons_kls(Koliseo* kls, LIST_T* element, LIST_NAME list) +{ + if (kls == NULL) { + fprintf(stderr, "%s at %i: %s(): Koliseo is NULL.\n", __FILE__, __LINE__, __func__); + return NULL; + } + LIST_NAME t; + t = (LIST_NAME) KLS_PUSH_EX(kls, LIST_ITEM_NAME, "List node"); + if (t == NULL ) { + fprintf(stderr, "%s at %i: %s(): Failed KLS_PUSH_EX() call.\n", __FILE__, __LINE__, __func__); + return NULL; + } + t->value = element; + t->next = list; + return t; +} + +LIST_LINKAGE +void +LIST_free_gl(LIST_NAME list) +{ + if (LIST_isEmpty(list)) { + return; + } else { + LIST_free_gl(LIST_tail(list)); + free(list); + } + return; +} + +LIST_LINKAGE +bool +LIST_member(LIST_T* element, LIST_NAME list) +{ + if (LIST_isEmpty(list)) { + return false; + } else { + if (element == LIST_head(list)) { + return true; + } else { + return LIST_member(element, LIST_tail(list)); + } + } +} + +LIST_LINKAGE +int +LIST_length(LIST_NAME list) +{ + if (LIST_isEmpty(list)) { + return 0; + } else { + return 1 + LIST_length(LIST_tail(list)); + } +} + +LIST_LINKAGE +LIST_NAME +LIST_append_gl(LIST_NAME l1, LIST_NAME l2) +{ + if (LIST_isEmpty(l1)) { + return l2; + } else { + return LIST_cons_gl(LIST_head(l1), LIST_append_gl(LIST_tail(l1), l2)); + } +} + +LIST_LINKAGE +LIST_NAME +LIST_append_kls(Koliseo* kls, LIST_NAME l1, LIST_NAME l2) +{ + if (kls == NULL) { + fprintf(stderr, "%s at %i: %s(): Koliseo is NULL.\n", __FILE__, __LINE__, __func__); + return NULL; + } + if (LIST_isEmpty(l1)) { + return l2; + } else { + return LIST_cons_kls(kls, LIST_head(l1), LIST_append_kls(kls, LIST_tail(l1), l2)); + } +} + +LIST_LINKAGE +LIST_NAME +LIST_reverse_gl(LIST_NAME list) +{ + if (LIST_isEmpty(list)) { + return LIST_nullList(); + } else { + return LIST_append_gl(LIST_reverse_gl(LIST_tail(list)), LIST_cons_gl(LIST_head(list), LIST_nullList())); + } +} + +LIST_LINKAGE +LIST_NAME +LIST_reverse_kls(Koliseo* kls, LIST_NAME list) +{ + if (kls == NULL) { + fprintf(stderr, "%s at %i: %s(): Koliseo is NULL.\n", __FILE__, __LINE__, __func__); + return NULL; + } + if (LIST_isEmpty(list)) { + return LIST_nullList(); + } else { + return LIST_append_kls(kls, LIST_reverse_kls(kls, LIST_tail(list)), LIST_cons_kls(kls, LIST_head(list), LIST_nullList())); + } +} + +LIST_LINKAGE +LIST_NAME +LIST_copy_gl(LIST_NAME list) +{ + if (LIST_isEmpty(list)) { + return list; + } else { + return LIST_cons_gl(LIST_head(list), LIST_copy_gl(LIST_tail(list))); + } +} + +LIST_LINKAGE +LIST_NAME +LIST_copy_kls(Koliseo* kls, LIST_NAME list) +{ + if (kls == NULL) { + fprintf(stderr, "%s at %i: %s(): Koliseo is NULL.\n", __FILE__, __LINE__, __func__); + return NULL; + } + if (LIST_isEmpty(list)) { + return list; + } else { + return LIST_cons_kls(kls, LIST_head(list), LIST_copy_kls(kls, LIST_tail(list))); + } + +} + +LIST_LINKAGE +LIST_NAME +LIST_remove_gl(LIST_T* element, LIST_NAME list) +{ + if (LIST_isEmpty(list)) { + return LIST_nullList(); + } else { + if (element == LIST_head(list)) { + return LIST_tail(list); + } else { + return LIST_cons_gl(LIST_head(list), LIST_remove_gl(element, LIST_tail(list))); + } + } +} + +LIST_LINKAGE +LIST_NAME +LIST_remove_kls(Koliseo* kls, LIST_T* element, LIST_NAME list) +{ + if (kls == NULL) { + fprintf(stderr, "%s at %i: %s(): Koliseo is NULL.\n", __FILE__, __LINE__, __func__); + return NULL; + } + if (LIST_isEmpty(list)) { + return LIST_nullList(); + } else { + if (element == LIST_head(list)) { + return LIST_tail(list); + } else { + return LIST_cons_kls(kls, LIST_head(list), LIST_remove_kls(kls, element, LIST_tail(list))); + } + } +} + +LIST_LINKAGE +LIST_NAME +LIST_intersect_gl(LIST_NAME l1, LIST_NAME l2) +{ + if (LIST_isEmpty(l1) || LIST_isEmpty(l2)) { + return LIST_nullList(); + } + if (LIST_member(LIST_head(l1), l2) && !(LIST_member(LIST_head(l1), LIST_tail(l2)))) { + return LIST_cons_gl(LIST_head(l1), LIST_intersect_gl(LIST_tail(l1), l2)); + } else { + return LIST_intersect_gl(LIST_tail(l1), l2); + } +} + +LIST_LINKAGE +LIST_NAME +LIST_intersect_kls(Koliseo* kls, LIST_NAME l1, LIST_NAME l2) +{ + if (kls == NULL) { + fprintf(stderr, "%s at %i: %s(): Koliseo is NULL.\n", __FILE__, __LINE__, __func__); + return NULL; + } + if (LIST_isEmpty(l1) || LIST_isEmpty(l2)) { + return LIST_nullList(); + } + if (LIST_member(LIST_head(l1), l2) && !(LIST_member(LIST_head(l1), LIST_tail(l2)))) { + return LIST_cons_kls(kls, LIST_head(l1), LIST_intersect_kls(kls, LIST_tail(l1), l2)); + } else { + return LIST_intersect_kls(kls, LIST_tail(l1), l2); + } +} + +LIST_LINKAGE +LIST_NAME +LIST_diff_gl(LIST_NAME l1, LIST_NAME l2) +{ + if (LIST_isEmpty(l1) || LIST_isEmpty(l2)) { + return l1; + } else { + if (!LIST_member(LIST_head(l1), l2) && !LIST_member(LIST_head(l1), LIST_tail(l1))) { + return LIST_cons_gl(LIST_head(l1), LIST_diff_gl(LIST_tail(l1), l2)); + } else { + return LIST_diff_gl(LIST_tail(l1), l2); + } + } +} + +LIST_LINKAGE +LIST_NAME +LIST_diff_kls(Koliseo* kls, LIST_NAME l1, LIST_NAME l2) +{ + if (kls == NULL) { + fprintf(stderr, "%s at %i: %s(): Koliseo is NULL.\n", __FILE__, __LINE__, __func__); + return NULL; + } + if (LIST_isEmpty(l1) || LIST_isEmpty(l2)) { + return l1; + } else { + if (!LIST_member(LIST_head(l1), l2) && !LIST_member(LIST_head(l1), LIST_tail(l1))) { + return LIST_cons_kls(kls, LIST_head(l1), LIST_diff_kls(kls, LIST_tail(l1), l2)); + } else { + return LIST_diff_kls(kls, LIST_tail(l1), l2); + } + } +} +#endif + +// Cleanup +// These need to be undef'ed so they can be redefined the +// next time you need to instantiate this template. +#undef LIST_T +#undef LIST_PREFIX +#undef LIST_NAME +#undef LIST_LINKAGE +#undef LIST_I_SUFFIX +#undef LIST_ITEM_NAME +#undef LIST_nullList +#undef LIST_isEmpty +#undef LIST_head +#undef LIST_tail +#undef LIST_cons_gl +#undef LIST_cons_kls +#undef LIST_free_gl +#undef LIST_member +#undef LIST_length +#undef LIST_append_gl +#undef LIST_append_kls +#undef LIST_reverse_gl +#undef LIST_reverse_kls +#undef LIST_copy_gl +#undef LIST_copy_kls +#undef LIST_remove_gl +#undef LIST_remove_kls +#undef LIST_intersect_gl +#undef LIST_intersect_kls +#undef LIST_diff_gl +#undef LIST_diff_kls +#ifdef LIST_DECLS_ONLY +#undef LIST_DECLS_ONLY +#endif // LIST_DECLS_ONLY +#endif // LIST_T diff --git a/static/basic_example.c b/static/basic_example.c index 55ca1c0..be8ad73 100644 --- a/static/basic_example.c +++ b/static/basic_example.c @@ -14,7 +14,7 @@ int main(void) Koliseo *kls = kls_new_traced(KLS_DEFAULT_SIZE, "./static/debug_log.txt"); //Use the arena (see demo for Koliseo_Temp usage) - Example *e = KLS_PUSH(kls, Example, 1); + Example *e = KLS_PUSH(kls, Example); e->val = 42; //Show contents to stdout diff --git a/static/demo.c b/static/demo.c index fbe755f..5b32097 100644 --- a/static/demo.c +++ b/static/demo.c @@ -82,9 +82,24 @@ int main(int argc, char **argv) printf("*pt_ex is {%c}\n", *pt_ex); + char* pt_nm = KLS_PUSH_ARR_NAMED(kls, char, 10, "char array", "Named array"); + + pt_nm[0] = 'W'; + + printf("pt_nm[0] is {%c}\n", pt_nm[0]); + + char* pt_tp = KLS_PUSH_ARR_TYPED(kls, char, 10, 42, "char array, typed \"42\"", "Typed chararray"); + + pt_tp[0] = 'W'; + pt_tp[1] = 'W'; + pt_tp[2] = '\0'; + + printf("pt_tp is {%s}\n", pt_tp); + + printf(KLS_Conf_Fmt "\n", KLS_Conf_Arg(kls->conf)); -#ifndef WINDOWS_BUILD +#ifndef _WIN32 printf("[Current position in Koliseo] [pos: %li]\n", kls_get_pos(kls)); #else printf("[Current position in Koliseo] [pos: %lli]\n", kls_get_pos(kls)); @@ -94,20 +109,20 @@ int main(int argc, char **argv) #ifdef KOLISEO_HAS_REGION -#ifndef WINDOWS_BUILD +#ifndef _WIN32 printf("[Show Region list for Koliseo] [pos: %li]\n", kls_get_pos(kls)); #else printf("[Show Region list for Koliseo] [pos: %lli]\n", kls_get_pos(kls)); #endif - KLS_ECHOLIST(kls->regs); + KLS_RL_ECHOLIST(kls->regs); #endif // KOLISEO_HAS_REGION Koliseo_Temp *temp_kls = kls_temp_start(kls); //temp_kls->conf.kls_autoset_regions = 1; TODO why does this crash? -#ifndef WINDOWS_BUILD +#ifndef _WIN32 printf("[Started Koliseo_Temp] [pos: %li]\n", kls_get_pos(temp_kls->kls)); #else printf("[Started Koliseo_Temp] [pos: %lli]\n", kls_get_pos(temp_kls->kls)); @@ -126,7 +141,7 @@ int main(int argc, char **argv) printf("\n*p2 is [%i] before KLS_PUSH_T\n", *p2); printf("\n*p3 is [%i] before KLS_PUSH_T\n", *p3); -#ifndef WINDOWS_BUILD +#ifndef _WIN32 printf("[KLS_PUSH for a int to Koliseo] [size: %li]\n", sizeof(int)); #else printf("[KLS_PUSH for a int to Koliseo] [size: %lli]\n", sizeof(int)); @@ -137,7 +152,7 @@ int main(int argc, char **argv) p = (int *)KLS_PUSH(kls, int); #ifdef KOLISEO_HAS_REGION -#ifndef WINDOWS_BUILD +#ifndef _WIN32 printf("[KLS_PUSH_T_NAMED for a int to Koliseo_Temp] [size: %li]\n", sizeof(int)); #else @@ -147,7 +162,7 @@ int main(int argc, char **argv) p2 = (int *)KLS_PUSH_T_NAMED(temp_kls, int, "int", "Another int"); #endif // KOLISEO_HAS_REGION -#ifndef WINDOWS_BUILD +#ifndef _WIN32 printf("[KLS_PUSH_T for a int to Koliseo_Temp] [size: %li]\n", sizeof(int)); #else printf("[KLS_PUSH_T for a int to Koliseo_Temp] [size: %lli]\n", @@ -155,13 +170,13 @@ int main(int argc, char **argv) #endif p3 = (int *)KLS_PUSH_T(temp_kls, int); -#ifndef WINDOWS_BUILD +#ifndef _WIN32 printf("[Current position in Koliseo] [pos: %li]\n", kls_get_pos(kls)); #else printf("[Current position in Koliseo] [pos: %lli]\n", kls_get_pos(kls)); #endif -#ifndef WINDOWS_BUILD +#ifndef _WIN32 printf("[Current position in Koliseo_Temp] [pos: %li]\n", temp_kls->offset); #else printf("[Current position in Koliseo_Temp] [pos: %lli]\n", @@ -178,17 +193,17 @@ int main(int argc, char **argv) printf("\n*p3 is [%i] after KLS_PUSH\n", *p3); #ifdef KOLISEO_HAS_REGION -#ifndef WINDOWS_BUILD +#ifndef _WIN32 printf("[Show Region list for Koliseo] [pos: %li]\n", kls_get_pos(kls)); #else printf("[Show Region list for Koliseo] [pos: %lli]\n", kls_get_pos(kls)); #endif - KLS_ECHOLIST(kls->regs); - printf("[%i] List size\n", kls_length(kls->regs)); + KLS_RL_ECHOLIST(kls->regs); + printf("[%i] List size\n", kls_rl_length(kls->regs)); - KLS_ECHOLIST(temp_kls->t_regs); - printf("[%i] Temp List size\n", kls_length(temp_kls->t_regs)); + KLS_RL_ECHOLIST(temp_kls->t_regs); + printf("[%i] Temp List size\n", kls_rl_length(temp_kls->t_regs)); printf("[Usage report for Koliseo]\n"); kls_usageReport(kls); @@ -226,7 +241,7 @@ int main(int argc, char **argv) int *z = &minusone; printf("\n*z is [%i] before KLS_POP\n", *z); -#ifndef WINDOWS_BUILD +#ifndef _WIN32 printf("[KLS_POP a int from Koliseo] [size: %li]\n", sizeof(int)); #else printf("[KLS_POP a int from Koliseo] [size: %lli]\n", sizeof(int)); @@ -237,7 +252,7 @@ int main(int argc, char **argv) printf("\n*z is [%i] after KLS_POP\n", *z); #endif // KOLISEO_HAS_EXPER -#ifndef WINDOWS_BUILD +#ifndef _WIN32 printf("[Current position in Koliseo] [pos: %li]\n", kls_get_pos(kls)); #else printf("[Current position in Koliseo] [pos: %lli]\n", kls_get_pos(kls)); @@ -259,12 +274,12 @@ int main(int argc, char **argv) print_dbg_kls(kls); #ifdef KOLISEO_HAS_REGION print_dbg_kls(kls->reglist_kls); - KLS_ECHOLIST(kls->regs); + KLS_RL_ECHOLIST(kls->regs); printf("[Koliseo_Temp Info]\n"); - KLS_ECHOLIST(temp_kls->t_regs); + KLS_RL_ECHOLIST(temp_kls->t_regs); print_dbg_kls(temp_kls->reglist_kls); - printf("[%i] List size for Koliseo\n", kls_length(kls->regs)); - printf("[%i] List size for Koliseo_Temp\n", kls_length(temp_kls->t_regs)); + printf("[%i] List size for Koliseo\n", kls_rl_length(kls->regs)); + printf("[%i] List size for Koliseo_Temp\n", kls_rl_length(temp_kls->t_regs)); #endif // KOLISEO_HAS_REGION printf("[Clear Koliseo]\n"); kls_clear(kls); diff --git a/stego.lock b/stego.lock index 0dcf52b..2380c6f 100644 --- a/stego.lock +++ b/stego.lock @@ -1,6 +1,6 @@ [ anvil ] -version = "2.0.4" +version = "2.0.5" kern = "amboso-C" [ build ] @@ -62,3 +62,4 @@ testsdir = "ok" "0.3.20" = "Improve configure.ac, refactor MINGW32_BUILD, add SHARED_LIB define" "0.4.0" = "Refactor usage macros, add KLS_DEFAULT_CONF__" "0.4.1" = "Region is an optional feature, move pop ops under experimental features" +"0.4.2" = "Add list template, fix KLS_PUSH_ARR_T_NAMED(), bump amboso to 2.0.5, invil to 0.2.9" diff --git a/tests/error/bad_size.k.stderr b/tests/error/bad_size.k.stderr index 7911a46..e23c7ef 100644 --- a/tests/error/bad_size.k.stderr +++ b/tests/error/bad_size.k.stderr @@ -1,2 +1,2 @@ -[ERROR] at kls_new(): invalid requested kls size (-1). Min accepted is: (160). +[ERROR] at kls_new_alloc(): invalid requested kls size (-1). Min accepted is: (104). [ERROR] [kls_push_zero_AR()]: Passed Koliseo was NULL.