koliseo 0.5.4
Loading...
Searching...
No Matches
koliseo.c
Go to the documentation of this file.
1// jgabaut @ github.com/jgabaut
2// SPDX-License-Identifier: GPL-3.0-only
3/*
4 Copyright (C) 2023-2025 jgabaut
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, version 3 of the License.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
17*/
18#include "koliseo.h"
19
21 .kls_collect_stats = 0,
22 .kls_verbose_lvl = 0,
23 .kls_block_while_has_temp = 1,
24 .kls_allow_zerocount_push = 0,
25 .kls_log_fp = NULL,
26 .kls_growable = 0,
27 .kls_log_filepath = "",
28 .err_handlers = {
29#ifndef KOLISEO_HAS_LOCATE
30 .OOM_handler = &KLS_OOM_default_handler__,
31 .PTRDIFF_MAX_handler = &KLS_PTRDIFF_MAX_default_handler__,
32 .ZEROCOUNT_handler = &KLS_ZEROCOUNT_default_handler__,
33#else
34 .OOM_handler = &KLS_OOM_default_handler_dbg__,
35 .PTRDIFF_MAX_handler = &KLS_PTRDIFF_MAX_default_handler_dbg__,
36 .ZEROCOUNT_handler = &KLS_ZEROCOUNT_default_handler_dbg__,
37#endif // KOLISEO_HAS_LOCATE
38 },
39};
40
42 .tot_pushes = 0,
43 .tot_temp_pushes = 0,
44 .tot_pops = 0,
45 .tot_temp_pops = 0,
46 .tot_logcalls = 0,
47 .tot_hiccups = 0,
48#ifdef KLS_DEBUG_CORE
49 .worst_pushcall_time = -1,
50#endif
51};
52
53static bool kls_set_conf(Koliseo * kls, KLS_Conf conf); //Declare function used internally by kls_new() and kls_new_conf()
54
59const char *string_koliseo_version(void)
60{
61 return KOLISEO_API_VERSION_STRING;
62}
63
69{
70 return KOLISEO_API_VERSION_INT;
71}
72
82#ifndef KOLISEO_HAS_LOCATE
83void KLS_OOM_default_handler__(Koliseo* kls, ptrdiff_t available, ptrdiff_t padding, ptrdiff_t size, ptrdiff_t count)
84#else
85void KLS_OOM_default_handler_dbg__(Koliseo* kls, ptrdiff_t available, ptrdiff_t padding, ptrdiff_t size, ptrdiff_t count, Koliseo_Loc loc)
86#endif // KOLISEO_HAS_LOCATE
87{
88#ifndef KOLISEO_HAS_LOCATE
89 fprintf(stderr,
90 "[KLS] Out of memory. size*count [%td] was bigger than available-padding [%td].\n",
91 size * count, available - padding);
92#else
93 fprintf(stderr,
94 "[KLS] " KLS_Loc_Fmt "Out of memory. size*count [%td] was bigger than available-padding [%td].\n",
95 KLS_Loc_Arg(loc),
96 size * count, available - padding);
97#endif // KOLISEO_HAS_LOCATE
98 kls_free(kls); // Is it even worth it to try?
99 exit(EXIT_FAILURE); // Better than nothing. May change to return NULL instead? Requiring refactor of handler signature
100}
101
102#ifndef KOLISEO_HAS_LOCATE
103void KLS_PTRDIFF_MAX_default_handler__(struct Koliseo* kls, ptrdiff_t size, ptrdiff_t count)
104#else
105void KLS_PTRDIFF_MAX_default_handler_dbg__(struct Koliseo* kls, ptrdiff_t size, ptrdiff_t count, Koliseo_Loc loc)
106#endif
107{
108#ifndef _WIN32
109#ifndef KOLISEO_HAS_LOCATE
110 fprintf(stderr,
111 "[KLS] count [%td] was bigger than PTRDIFF_MAX/size [%li].\n",
112 count, PTRDIFF_MAX / size);
113#else
114 fprintf(stderr,
115 "[KLS] " KLS_Loc_Fmt "count [%td] was bigger than PTRDIFF_MAX/size [%li].\n",
116 KLS_Loc_Arg(loc),
117 count, PTRDIFF_MAX / size);
118#endif // KOLISEO_HAS_LOCATE
119#else
120#ifndef KOLISEO_HAS_LOCATE
121 fprintf(stderr,
122 "[KLS] count [%td] was bigger than PTRDIFF_MAX/size [%lli].\n",
123 count, PTRDIFF_MAX / size);
124#else
125 fprintf(stderr,
126 "[KLS] " KLS_Loc_Fmt "count [%td] was bigger than PTRDIFF_MAX/size [%lli].\n",
127 KLS_Loc_Arg(loc),
128 count, PTRDIFF_MAX / size);
129#endif // KOLISEO_HAS_LOCATE
130#endif // _WIN32
131 kls_free(kls);
132 exit(EXIT_FAILURE);
133}
134
144#ifndef KOLISEO_HAS_LOCATE
145void KLS_ZEROCOUNT_default_handler__(Koliseo* kls, ptrdiff_t available, ptrdiff_t padding, ptrdiff_t size)
146#else
147void KLS_ZEROCOUNT_default_handler_dbg__(Koliseo* kls, ptrdiff_t available, ptrdiff_t padding, ptrdiff_t size, Koliseo_Loc loc)
148#endif // KOLISEO_HAS_LOCATE
149{
150#ifndef KOLISEO_HAS_LOCATE
151 fprintf(stderr,
152 "[KLS] Doing a zero-count push. size [%td] padding [%td] available [%td].\n",
153 size, padding, available);
154#else
155 fprintf(stderr,
156 "[KLS] " KLS_Loc_Fmt "Doing a zero-count push. size [%td] padding [%td] available [%td].\n",
157 KLS_Loc_Arg(loc),
158 size, padding, available);
159#endif // KOLISEO_HAS_LOCATE
160 kls_free(kls);
161 exit(EXIT_FAILURE);
162}
163
169KLS_Conf kls_conf_init_handled(int collect_stats, int verbose_lvl, int block_while_has_temp, int allow_zerocount_push, int growable, FILE* log_fp, const char* log_filepath, KLS_Err_Handlers err_handlers)
170{
171 KLS_Conf res = {0};
172 res.kls_collect_stats = collect_stats;
173 res.kls_verbose_lvl = verbose_lvl;
174 res.kls_block_while_has_temp = block_while_has_temp;
175 res.kls_allow_zerocount_push = allow_zerocount_push;
176 res.kls_growable = growable;
177 res.kls_log_fp = log_fp;
178 res.kls_log_filepath = log_filepath;
179
180 if (err_handlers.OOM_handler != NULL) {
181 res.err_handlers.OOM_handler = err_handlers.OOM_handler;
182 } else {
183#ifndef KOLISEO_HAS_LOCATE
185#else
186 res.err_handlers.OOM_handler = &KLS_OOM_default_handler_dbg__;
187#endif // KOLISEO_HAS_LOCATE
188 }
189
190 if (err_handlers.PTRDIFF_MAX_handler != NULL) {
192 } else {
193#ifndef KOLISEO_HAS_LOCATE
195#else
196 res.err_handlers.PTRDIFF_MAX_handler = &KLS_PTRDIFF_MAX_default_handler_dbg__;
197#endif // KOLISEO_HAS_LOCATE
198 }
199
200 if (err_handlers.ZEROCOUNT_handler != NULL) {
202 } else {
203#ifndef KOLISEO_HAS_LOCATE
205#else
206 res.err_handlers.ZEROCOUNT_handler = &KLS_ZEROCOUNT_default_handler_dbg__;
207#endif // KOLISEO_HAS_LOCATE
208 }
209
210 return res;
211}
212
217KLS_Conf kls_conf_init(int collect_stats, int verbose_lvl, int block_while_has_temp, int allow_zerocount_push, int growable, FILE* log_fp, const char* log_filepath)
218{
220 return kls_conf_init_handled(collect_stats, verbose_lvl, block_while_has_temp, allow_zerocount_push, growable, log_fp, log_filepath, err_handlers);
221}
222
227{
228#ifdef KOLISEO_HAS_LOCATE
229 bool kls_locate = true;
230#else
231 bool kls_locate = false;
232#endif
233#ifdef KLS_DEBUG_CORE
234 bool kls_debug = true;
235#else
236 bool kls_debug = false;
237#endif
238#ifdef KOLISEO_HAS_EXPER
239 bool kls_exper = true;
240#else
241 bool kls_exper = false;
242#endif
243 bool features[3] = {
244 [0] = kls_debug,
245 [1] = kls_locate,
246 [2] = kls_exper,
247 };
248 int total_enabled = 0;
249 for (int i=0; i<3; i++) {
250 if (features[i]) {
251 total_enabled += 1;
252 }
253 }
254 fprintf(stderr, "[KLS] Enabled features: {");
255 if (total_enabled == 0) {
256 fprintf(stderr, "none}\n");
257 return;
258 } else {
259 if (kls_debug) {
260 fprintf(stderr, "debug%s", (total_enabled > 1 ? ", " : ""));
261 total_enabled -= 1;
262 }
263 if (kls_locate) {
264 fprintf(stderr, "locate%s", (total_enabled > 1 ? ", " : ""));
265 total_enabled -= 1;
266 }
267 if (kls_exper) {
268 fprintf(stderr, "exper");
269 }
270 fprintf(stderr, "}\n");
271 }
272}
273
279ptrdiff_t kls_get_pos(const Koliseo *kls)
280{
281 return kls->offset;
282}
283
290void kls_log(Koliseo *kls, const char *tag, const char *format, ...)
291{
292 if (kls == NULL) {
293 fprintf(stderr, "[KLS] %s(): Passed kls was NULL.\n", __func__);
294 return;
295 }
296 if (kls->conf.kls_verbose_lvl > 0) {
297 va_list args;
298 FILE *fp = kls->conf.kls_log_fp;
299 va_start(args, format);
300 if (fp == NULL) {
301 fprintf(stderr,
302 "[KLS] %s(): Failed opening file to print logs.\n",
303 __func__);
304 } else {
305 time_t now = time(0);
306 const struct tm *mytime = localtime(&now);
307 char timeheader[500];
308 if (strftime(timeheader, sizeof timeheader, "%X", mytime)) {
309 fprintf(fp, "[%-10.10s] [%s] [", tag, timeheader);
310 vfprintf(fp, format, args);
311 fprintf(fp, "]\n");
312 }
313 }
314 va_end(args);
315 }
316}
317
335#ifndef KOLISEO_HAS_LOCATE
336Koliseo *kls_new_alloc_ext(ptrdiff_t size, kls_alloc_func alloc_func, kls_free_func free_func, KLS_Hooks* ext_handlers, void** user, size_t ext_len)
337#else
338Koliseo *kls_new_alloc_ext_dbg(ptrdiff_t size, kls_alloc_func alloc_func, kls_free_func free_func, KLS_Hooks* ext_handlers, void** user, size_t ext_len, Koliseo_Loc loc)
339#endif // KOLISEO_HAS_LOCATE
340{
341 if (size < (ptrdiff_t)sizeof(Koliseo)) {
342#ifndef KOLISEO_HAS_LOCATE
343 fprintf(stderr,
344 "[ERROR] at %s(): invalid requested kls size (%td). Min accepted is: (%td).\n",
345 __func__, size, (ptrdiff_t)sizeof(Koliseo));
346#else
347 fprintf(stderr,
348 "[ERROR] " KLS_Loc_Fmt "%s(): invalid requested kls size (%td). Min accepted is: (%td).\n",
349 KLS_Loc_Arg(loc), __func__, size, (ptrdiff_t)sizeof(Koliseo));
350#endif // KOLISEO_HAS_LOCATE
351 //TODO Is it better to abort the program?
352 return NULL;
353 }
354 void *p = alloc_func(size);
355 if (p) {
356 //sprintf(msg,"Allocated (%li) for new KLS.",size);
357 //kls_log("KLS",msg);
358 char h_size[200];
359 kls_formatSize(size, h_size, sizeof(h_size));
360 Koliseo *kls = p;
361 kls->data = p;
362 kls->size = size;
363 kls->offset = sizeof(*kls);
364 kls->prev_offset = kls->offset;
365 kls->has_temp = 0;
366 kls->t_kls = NULL;
367 kls_set_conf(kls, KLS_DEFAULT_CONF);
369 kls->conf.kls_log_fp = stderr;
370 kls->hooks_len = ext_len;
371
372 for (size_t i=0; i < kls->hooks_len; i++) {
373 kls->hooks[i] = ext_handlers[i];
374 }
375 for (size_t i=0; i < kls->hooks_len; i++) {
376 if (user) {
377 kls->extension_data[i] = user[i];
378 } else {
379 kls->extension_data[i] = NULL;
380 }
381 }
382 kls->free_func = free_func;
383 kls->next = NULL;
384#ifdef KLS_DEBUG_CORE
385 kls_log(kls, "KLS", "API Level { %i } -> Allocated (%s) for new KLS.",
386 int_koliseo_version(), h_size);
387 kls_log(kls, "KLS", "KLS offset: { %p }.", kls);
388 kls_log(kls, "KLS", "Allocation begin offset: { %p }.",
389 kls + kls->offset);
390#endif
391
392 for (size_t i=0; i < kls->hooks_len; i++) {
393 if (kls->hooks[i].on_new_handler != NULL) {
394 // Call on_new extension
395 kls->hooks[i].on_new_handler(kls);
396 }
397 }
398 } else {
399#ifndef KOLISEO_HAS_LOCATE
400 fprintf(stderr, "[KLS] Failed %s() call.\n", __func__);
401#else
402 fprintf(stderr, "[KLS] " KLS_Loc_Fmt "Failed %s() call.\n", KLS_Loc_Arg(loc), __func__);
403#endif // KOLISEO_HAS_LOCATE
404 exit(EXIT_FAILURE);
405 }
406#ifdef KLS_DEBUG_CORE
407 Koliseo *kls_ref = p;
408 if (kls_ref->conf.kls_verbose_lvl > 0) {
409 print_kls_2file(kls_ref->conf.kls_log_fp, p);
410 }
411#endif
412 return p;
413}
414
430#ifndef KOLISEO_HAS_LOCATE
431Koliseo *kls_new_alloc(ptrdiff_t size, kls_alloc_func alloc_func, kls_free_func free_func)
432#else
433Koliseo *kls_new_alloc_dbg(ptrdiff_t size, kls_alloc_func alloc_func, kls_free_func free_func, Koliseo_Loc loc)
434#endif // KOLISEO_HAS_LOCATE
435{
436#ifndef KOLISEO_HAS_LOCATE
438#else
439 return kls_new_alloc_ext_dbg(size, alloc_func, free_func, KLS_DEFAULT_HOOKS, KLS_DEFAULT_EXTENSION_DATA, KLS_DEFAULT_EXTENSIONS_LEN, loc);
440#endif // KOLISEO_HAS_LOCATE
441}
442
455Koliseo *kls_new(ptrdiff_t size)
456{
458}
459
476Koliseo *kls_new_conf_alloc_ext(ptrdiff_t size, KLS_Conf conf, kls_alloc_func alloc_func, kls_free_func free_func, KLS_Hooks* ext_handlers, void** user, size_t ext_len)
477{
478 Koliseo *k = kls_new_alloc_ext(size, alloc_func, free_func, ext_handlers, user, ext_len);
479 bool conf_res = kls_set_conf(k, conf);
480 if (!conf_res) {
481 fprintf(stderr,
482 "[ERROR] [%s()]: Failed to set config for new Koliseo.\n",
483 __func__);
484 exit(EXIT_FAILURE);
485 }
486 return k;
487}
488
505Koliseo *kls_new_conf_alloc(ptrdiff_t size, KLS_Conf conf, kls_alloc_func alloc_func, kls_free_func free_func)
506{
508}
509
522Koliseo *kls_new_conf_ext(ptrdiff_t size, KLS_Conf conf, KLS_Hooks* ext_handlers, void** user, size_t ext_len)
523{
524 return kls_new_conf_alloc_ext(size, conf, KLS_DEFAULT_ALLOCF, KLS_DEFAULT_FREEF, ext_handlers, user, ext_len);
525}
526
538Koliseo *kls_new_conf(ptrdiff_t size, KLS_Conf conf)
539{
541}
542
558Koliseo *kls_new_traced_alloc_handled_ext(ptrdiff_t size, const char *output_path, kls_alloc_func alloc_func, kls_free_func free_func, KLS_Err_Handlers err_handlers, KLS_Hooks* ext_handlers, void** user, size_t ext_len)
559{
560
561#ifndef KLS_DEBUG_CORE
562 fprintf(stderr,
563 "[WARN] %s(): KLS_DEBUG_CORE is not defined. No tracing allowed.\n",
564 __func__);
565#endif
566 KLS_Conf k = (KLS_Conf) {
567 .kls_collect_stats = 1,.kls_verbose_lvl =
568 1,.kls_log_filepath = output_path,
569#ifndef KOLISEO_HAS_LOCATE
570 .err_handlers.OOM_handler = (err_handlers.OOM_handler != NULL ? err_handlers.OOM_handler : &KLS_OOM_default_handler__),
571 .err_handlers.PTRDIFF_MAX_handler = ( err_handlers.PTRDIFF_MAX_handler != NULL ? err_handlers.PTRDIFF_MAX_handler : &KLS_PTRDIFF_MAX_default_handler__),
572#else
573 .err_handlers.OOM_handler = (err_handlers.OOM_handler != NULL ? err_handlers.OOM_handler : &KLS_OOM_default_handler_dbg__),
574 .err_handlers.PTRDIFF_MAX_handler = ( err_handlers.PTRDIFF_MAX_handler != NULL ? err_handlers.PTRDIFF_MAX_handler : &KLS_PTRDIFF_MAX_default_handler_dbg__),
575#endif // KOLISEO_HAS_LOCATE
576 };
577 return kls_new_conf_alloc_ext(size, k, alloc_func, free_func, ext_handlers, user, ext_len);
578}
579
594Koliseo *kls_new_traced_alloc_handled(ptrdiff_t size, const char *output_path, kls_alloc_func alloc_func, kls_free_func free_func, KLS_Err_Handlers err_handlers)
595{
596 return kls_new_traced_alloc_handled_ext(size, output_path, alloc_func, free_func, err_handlers, KLS_DEFAULT_HOOKS, KLS_DEFAULT_EXTENSION_DATA, KLS_DEFAULT_EXTENSIONS_LEN);
597}
598
611Koliseo *kls_new_traced_ext(ptrdiff_t size, const char *output_path, KLS_Hooks* ext_handlers, void** user, size_t ext_len)
612{
614 return kls_new_traced_alloc_handled_ext(size, output_path, KLS_DEFAULT_ALLOCF, KLS_DEFAULT_FREEF, err_handlers, ext_handlers, user, ext_len);
615}
616
630Koliseo *kls_new_traced_alloc(ptrdiff_t size, const char *output_path, kls_alloc_func alloc_func, kls_free_func free_func)
631{
633 return kls_new_traced_alloc_handled(size, output_path, alloc_func, free_func, err_handlers);
634}
635
647Koliseo *kls_new_traced(ptrdiff_t size, const char *output_path)
648{
650}
651
664Koliseo *kls_new_traced_handled(ptrdiff_t size, const char *output_path, KLS_Err_Handlers err_handlers)
665{
666 return kls_new_traced_alloc_handled(size, output_path, KLS_DEFAULT_ALLOCF, KLS_DEFAULT_FREEF, err_handlers);
667}
668
682Koliseo *kls_new_dbg_alloc_handled_ext(ptrdiff_t size, kls_alloc_func alloc_func, kls_free_func free_func, KLS_Err_Handlers err_handlers, KLS_Hooks* ext_handlers, void** user, size_t ext_len)
683{
684#ifndef KLS_DEBUG_CORE
685 fprintf(stderr,
686 "[WARN] %s(): KLS_DEBUG_CORE is not defined. No debugging support.\n",
687 __func__);
688#endif
689 KLS_Conf k = (KLS_Conf) {
690 .kls_collect_stats = 1,.kls_verbose_lvl = 0,
691#ifndef KOLISEO_HAS_LOCATE
692 .err_handlers.OOM_handler = ( err_handlers.OOM_handler != NULL ? err_handlers.OOM_handler : &KLS_OOM_default_handler__),
693 .err_handlers.PTRDIFF_MAX_handler = ( err_handlers.PTRDIFF_MAX_handler != NULL ? err_handlers.PTRDIFF_MAX_handler : &KLS_PTRDIFF_MAX_default_handler__),
694#else
695 .err_handlers.OOM_handler = ( err_handlers.OOM_handler != NULL ? err_handlers.OOM_handler : &KLS_OOM_default_handler_dbg__),
696 .err_handlers.PTRDIFF_MAX_handler = ( err_handlers.PTRDIFF_MAX_handler != NULL ? err_handlers.PTRDIFF_MAX_handler : &KLS_PTRDIFF_MAX_default_handler_dbg__),
697#endif // KOLIEO_HAS_LOCATE
698 };
699 Koliseo * kls = kls_new_conf_alloc_ext(size, k, alloc_func, free_func, ext_handlers, user, ext_len);
700 kls->conf.kls_verbose_lvl = 1;
701 return kls;
702}
703
716Koliseo *kls_new_dbg_alloc_handled(ptrdiff_t size, kls_alloc_func alloc_func, kls_free_func free_func, KLS_Err_Handlers err_handlers)
717{
719}
720
731Koliseo *kls_new_dbg_ext(ptrdiff_t size, KLS_Hooks* ext_handlers, void** user, size_t ext_len)
732{
734 return kls_new_dbg_alloc_handled_ext(size, KLS_DEFAULT_ALLOCF, KLS_DEFAULT_FREEF, err_handlers, ext_handlers, user, ext_len);
735}
736
748Koliseo *kls_new_dbg_alloc(ptrdiff_t size, kls_alloc_func alloc_func, kls_free_func free_func)
749{
751 return kls_new_dbg_alloc_handled(size, alloc_func, free_func, err_handlers);
752}
753
763Koliseo *kls_new_dbg(ptrdiff_t size)
764{
766}
767
778Koliseo *kls_new_dbg_handled(ptrdiff_t size, KLS_Err_Handlers err_handlers)
779{
781}
782
789bool kls_set_conf(Koliseo *kls, KLS_Conf conf)
790{
791 if (kls == NULL) {
792 fprintf(stderr, "[ERROR] [%s()]: Passed Koliseo was NULL.\n", __func__);
793 //TODO: is it better to exit() here?
794 return false;
795 }
796
797 kls->conf = conf;
798 if (kls->conf.kls_log_fp == NULL) {
799 kls->conf.kls_log_fp = stderr;
800#ifdef KLS_DEBUG_CORE
801#ifdef KLS_SETCONF_DEBUG
802 kls_log(kls, "KLS",
803 "[%s()]: Preliminary set of conf.kls_log_fp to stderr.",
804 __func__);
805#endif
806#endif // KLS_DEBUG_CORE
807 }
808
809 if (conf.err_handlers.OOM_handler == NULL) {
810 fprintf(stderr,
811 "[ERROR] at %s(): passed OOM_handler is NULL. Using default.\n",
812 __func__);
813#ifdef KLS_DEBUG_CORE
814#ifdef KLS_SETCONF_DEBUG
815 kls_log(kls, "KLS",
816 "[%s()]: Passed OOM_handler was NULL, using default.",
817 __func__);
818#endif
819#endif // KLS_DEBUG_CORE
820#ifndef KOLISEO_HAS_LOCATE
822#else
823 kls->conf.err_handlers.OOM_handler = &KLS_OOM_default_handler_dbg__;
824#endif
825 }
826
827 if (conf.err_handlers.PTRDIFF_MAX_handler == NULL) {
828 fprintf(stderr,
829 "[ERROR] at %s(): passed PTRDIFF_MAX_handler is NULL. Using default.\n",
830 __func__);
831#ifdef KLS_DEBUG_CORE
832#ifdef KLS_SETCONF_DEBUG
833 kls_log(kls, "KLS",
834 "[%s()]: Passed PTRDIFF_MAX_handler was NULL, using default.",
835 __func__);
836#endif
837#endif // KLS_DEBUG_CORE
838#ifndef KOLISEO_HAS_LOCATE
840#else
841 kls->conf.err_handlers.PTRDIFF_MAX_handler = &KLS_PTRDIFF_MAX_default_handler_dbg__;
842#endif
843 }
844
845#ifndef KLS_DEBUG_CORE
846 if (kls->conf.kls_collect_stats == 1) {
847 fprintf(stderr,
848 "[WARN] [%s()]: KLS_DEBUG_CORE is not defined. Stats may not be collected in full.\n",
849 __func__);
850 }
851#endif
852
853 if (kls->conf.kls_verbose_lvl > 0) {
854 if (kls->conf.kls_log_fp != NULL) {
855#ifdef KLS_DEBUG_CORE
856#ifdef KLS_SETCONF_DEBUG
857 kls_log(kls, "WARN",
858 "[%s()]: kls->conf.kls_log_fp was not NULL. Overriding it.",
859 __func__);
860#endif
861#endif
862 if (kls->conf.kls_collect_stats == 1) {
863 kls->stats.tot_hiccups += 1;
864 }
865 }
866
867 FILE *log_fp = NULL;
868 log_fp = fopen(kls->conf.kls_log_filepath, "w");
869 if (!log_fp) {
870 fprintf(stderr,
871 "[ERROR] [%s()]: Failed opening logfile at {\"%s\"} [write].\n",
872 __func__, kls->conf.kls_log_filepath);
873 return false;
874 } else {
875 fprintf(log_fp, "%s", ""); //Reset log_fp
876 fclose(log_fp);
877 }
878 log_fp = fopen(kls->conf.kls_log_filepath, "a");
879 if (!log_fp) {
880 fprintf(stderr,
881 "[ERROR] [%s()]: Failed opening logfile at {\"%s\"} [append].\n",
882 __func__, kls->conf.kls_log_filepath);
883 return false;
884 } else {
885 kls->conf.kls_log_fp = log_fp;
886 }
887 }
888 return true;
889}
890
891static bool kls__try_grow(Koliseo* kls, ptrdiff_t needed);
892
905#ifndef KOLISEO_HAS_LOCATE
906int kls__check_available_failable(Koliseo* kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count, const char* caller_name)
907#else
908int kls__check_available_failable_dbg(Koliseo* kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count, const char* caller_name, Koliseo_Loc loc)
909#endif // KOLISEO_HAS_LOCATE
910{
911 assert(kls != NULL);
912 assert(caller_name != NULL);
913 if (count < 0) {
914#ifndef KOLISEO_HAS_LOCATE
915 fprintf(stderr,
916 "[KLS] %s(): count [%td] was < 0.\n",
917 caller_name,
918 count);
919#else
920 fprintf(stderr,
921 "[KLS] " KLS_Loc_Fmt "%s(): count [%td] was < 0.\n",
922 KLS_Loc_Arg(loc),
923 caller_name,
924 count);
925#endif // KOLISEO_HAS_LOCATE
926 return -1;
927 }
928 if (size < 1) {
929#ifndef KOLISEO_HAS_LOCATE
930 fprintf(stderr,
931 "[KLS] %s(): size [%td] was < 1.\n",
932 caller_name,
933 size);
934#else
935 fprintf(stderr,
936 "[KLS] " KLS_Loc_Fmt "%s(): size [%td] was < 1.\n",
937 KLS_Loc_Arg(loc),
938 caller_name,
939 size);
940#endif // KOLISEO_HAS_LOCATE
941 return -1;
942 }
943 if (align < 1) {
944#ifndef KOLISEO_HAS_LOCATE
945 fprintf(stderr,
946 "[KLS] %s(): align [%td] was < 1.\n",
947 caller_name,
948 align);
949#else
950 fprintf(stderr,
951 "[KLS] " KLS_Loc_Fmt "%s(): align [%td] was < 1.\n",
952 KLS_Loc_Arg(loc),
953 caller_name,
954 align);
955#endif // KOLISEO_HAS_LOCATE
956 return -1;
957 }
958 if (! ((align & (align - 1)) == 0)) {
959#ifndef KOLISEO_HAS_LOCATE
960 fprintf(stderr,
961 "[KLS] %s(): align [%td] was not a power of 2.\n",
962 caller_name,
963 align);
964#else
965 fprintf(stderr,
966 "[KLS] " KLS_Loc_Fmt "%s(): align [%td] was not a power of 2.\n",
967 KLS_Loc_Arg(loc),
968 caller_name,
969 align);
970#endif // KOLISEO_HAS_LOCATE
971 return -1;
972 }
973 Koliseo* current = kls;
974 while (current->next != NULL) {
975 current = current->next;
976 }
977 const ptrdiff_t available = current->size - current->offset;
978 const ptrdiff_t padding = -current->offset & (align - 1);
979 bool ZEROCOUNT_happened = false;
980 bool ZEROCOUNT_handled = false;
981 if (count == 0) {
982 if (current->conf.kls_allow_zerocount_push != 1) {
983 ZEROCOUNT_happened = true;
984 if (current->conf.err_handlers.ZEROCOUNT_handler != NULL) {
985#ifndef KOLISEO_HAS_LOCATE
986 current->conf.err_handlers.ZEROCOUNT_handler(current, available, padding, size);
987#else
988 current->conf.err_handlers.ZEROCOUNT_handler(current, available, padding, size, loc);
989#endif // KOLISEO_HAS_LOCATE
990 ZEROCOUNT_handled = true;
991 } else {
992#ifndef KOLISEO_HAS_LOCATE
993 fprintf(stderr,
994 "[KLS] %s(): Doing a zero-count push. size [%td] padding [%td] available [%td].\n",
995 caller_name,
996 size, padding, available);
997#else
998 fprintf(stderr,
999 "[KLS] " KLS_Loc_Fmt "%s(): Doing a zero-count push. size [%td] padding [%td] available [%td].\n",
1000 KLS_Loc_Arg(loc),
1001 caller_name,
1002 size, padding, available);
1003#endif // KOLISEO_HAS_LOCATE
1004 kls_free(kls);
1005 exit(EXIT_FAILURE);
1006 }
1007 } else {
1008#ifdef KLS_DEBUG_CORE
1009 kls_log(current, "DEBUG", "Accepting zero-count push: conf.kls_allow_zerocount_push was 1");
1010#endif // KLS_DEBUG_CORE
1011 }
1012 }
1013
1014 if (ZEROCOUNT_happened && ZEROCOUNT_handled) {
1015#ifdef KLS_DEBUG_CORE
1016 kls_log(current, "DEBUG", "Requested a zero-count push while kls_allow_zerocount_push is not 1, but the error handler returned instead of exiting.");
1017#endif // KLS_DEBUG_CORE
1018#ifndef KOLISEO_HAS_LOCATE
1019 fprintf(stderr,
1020 "[KLS] %s(): Doing a zero-count push. size [%td] padding [%td] available [%td].\n",
1021 caller_name,
1022 size, padding, available);
1023#else
1024 fprintf(stderr,
1025 "[KLS] " KLS_Loc_Fmt "%s(): Doing a zero-count push. size [%td] padding [%td] available [%td].\n",
1026 KLS_Loc_Arg(loc),
1027 caller_name,
1028 size, padding, available);
1029#endif // KOLISEO_HAS_LOCATE
1030 kls_free(kls);
1031 exit(EXIT_FAILURE);
1032 }
1033
1034 bool OOM_happened = false;
1035 bool OOM_handled = false;
1036 bool PTRDIFF_MAX_happened = false;
1037 bool PTRDIFF_MAX_handled = false;
1038 if (count > PTRDIFF_MAX / size || available - padding < size * count) {
1039 if (count > PTRDIFF_MAX / size) {
1040 PTRDIFF_MAX_happened = true;
1041 if (current->conf.err_handlers.PTRDIFF_MAX_handler != NULL) {
1042#ifndef KOLISEO_HAS_LOCATE
1043 current->conf.err_handlers.PTRDIFF_MAX_handler(current, size, count);
1044#else
1045 current->conf.err_handlers.PTRDIFF_MAX_handler(current, size, count, loc);
1046#endif // KOLISEO_HAS_LOCATE
1047 PTRDIFF_MAX_handled = true;
1048 } else { // Let's keep this here for now? It's the original part before adding KLS_PTRDIFF_MAX_default_handler__()
1049#ifndef _WIN32
1050#ifndef KOLISEO_HAS_LOCATE
1051 fprintf(stderr,
1052 "[KLS] %s(): count [%td] was bigger than PTRDIFF_MAX/size [%li].\n",
1053 caller_name,
1054 count, PTRDIFF_MAX / size);
1055#else
1056 fprintf(stderr,
1057 "[KLS] " KLS_Loc_Fmt "%s(): count [%td] was bigger than PTRDIFF_MAX/size [%li].\n",
1058 KLS_Loc_Arg(loc),
1059 caller_name,
1060 count, PTRDIFF_MAX / size);
1061#endif // KOLISEO_HAS_LOCATE
1062#else
1063#ifndef KOLISEO_HAS_LOCATE
1064 fprintf(stderr,
1065 "[KLS] %s(): count [%td] was bigger than PTRDIFF_MAX/size [%lli].\n",
1066 caller_name,
1067 count, PTRDIFF_MAX / size);
1068#else
1069 fprintf(stderr,
1070 "[KLS] " KLS_Loc_Fmt "%s(): count [%td] was bigger than PTRDIFF_MAX/size [%lli].\n",
1071 KLS_Loc_Arg(loc),
1072 caller_name,
1073 count, PTRDIFF_MAX / size);
1074#endif // KOLISEO_HAS_LOCATE
1075#endif // _WIN32
1076 }
1077 } else {
1078 if (current->conf.kls_growable == 1 && kls__try_grow(current, size + count + padding)) {
1079 return 0;
1080 }
1081 OOM_happened = true;
1082 if (current->conf.err_handlers.OOM_handler != NULL) {
1083#ifndef KOLISEO_HAS_LOCATE
1084 current->conf.err_handlers.OOM_handler(current, available, padding, size, count);
1085#else
1086 current->conf.err_handlers.OOM_handler(current, available, padding, size, count, loc);
1087#endif // KOLISEO_HAS_LOCATE
1088 OOM_handled = true;
1089 } else { // Let's keep this here for now? It's the original part before adding KLS_OOM_default_handler__()
1090#ifndef KOLISEO_HAS_LOCATE
1091 fprintf(stderr,
1092 "[KLS] %s(): Out of memory. size*count [%td] was bigger than available-padding [%td].\n",
1093 caller_name,
1094 size * count, available - padding);
1095#else
1096 fprintf(stderr,
1097 "[KLS] " KLS_Loc_Fmt "%s(): Out of memory. size*count [%td] was bigger than available-padding [%td].\n",
1098 KLS_Loc_Arg(loc),
1099 caller_name,
1100 size * count, available - padding);
1101#endif // KOLISEO_HAS_LOCATE
1102 }
1103 }
1104 if (PTRDIFF_MAX_happened) {
1105 if (current->conf.err_handlers.PTRDIFF_MAX_handler && PTRDIFF_MAX_handled) {
1106#ifndef KOLISEO_HAS_LOCATE
1107 fprintf(stderr, "[KLS] %s(): PTRDIFF_MAX fault happened and was handled.\n", caller_name);
1108#ifdef KLS_DEBUG_CORE
1109 kls_log(current, "DEBUG", "%s(): PTRDIFF_MAX fault happened and was handled.", caller_name);
1110#endif // KLS_DEBUG_CORE
1111#else
1112 fprintf(stderr, "[KLS] " KLS_Loc_Fmt "%s(): PTRDIFF_MAX fault happened and was handled.\n", KLS_Loc_Arg(loc), caller_name);
1113#ifdef KLS_DEBUG_CORE
1114 kls_log(current, "DEBUG", KLS_Loc_Fmt "%s(): PTRDIFF_MAX fault happened and was handled.", KLS_Loc_Arg(loc), caller_name);
1115#endif // KLS_DEBUG_CORE
1116#endif // KOLISEO_HAS_LOCATE
1117 return -1;
1118 }
1119 } else if (OOM_happened) {
1120 if (current->conf.err_handlers.OOM_handler && OOM_handled) {
1121#ifndef KOLISEO_HAS_LOCATE
1122 fprintf(stderr, "[KLS] %s(): OOM fault happened and was handled.\n", caller_name);
1123#ifdef KLS_DEBUG_CORE
1124 kls_log(current, "DEBUG", "%s(): OOM fault happened and was handled.", caller_name);
1125#endif // KLS_DEBUG_CORE
1126#else
1127 fprintf(stderr, "[KLS] " KLS_Loc_Fmt "%s(): OOM fault happened and was handled.\n", KLS_Loc_Arg(loc), caller_name);
1128#ifdef KLS_DEBUG_CORE
1129 kls_log(current, "DEBUG", KLS_Loc_Fmt "%s(): OOM fault happened and was handled.", KLS_Loc_Arg(loc), caller_name);
1130#endif // KLS_DEBUG_CORE
1131#endif // KOLISEO_HAS_LOCATE
1132 return -1;
1133 }
1134 }
1135#ifndef KOLISEO_HAS_LOCATE
1136 fprintf(stderr, "[KLS] Failed %s() call.\n", caller_name);
1137#else
1138 fprintf(stderr, "[KLS] " KLS_Loc_Fmt "Failed %s() call.\n", KLS_Loc_Arg(loc), caller_name);
1139#endif // KOLISEO_HAS_LOCATE
1140 kls_free(kls);
1141 exit(EXIT_FAILURE);
1142 }
1143 return 0;
1144}
1145
1146bool kls__try_grow(Koliseo* kls, ptrdiff_t needed)
1147{
1148 ptrdiff_t new_size = KLS_MAX(kls->size * 2, needed);
1150 kls_log(kls, "DEBUG", "%s(): growing Koliseo, new size: {%td}", __func__, new_size);
1151 if (!new_kls) return false;
1152 kls->next = new_kls;
1153 return true;
1154}
1155
1165void *kls_push(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count)
1166{
1167
1168#ifdef KLS_DEBUG_CORE
1169#ifndef _WIN32
1170 struct timespec start_time, end_time;
1171 clock_gettime(CLOCK_MONOTONIC, &start_time);
1172#else
1173 LARGE_INTEGER start_time, end_time, frequency;
1174 QueryPerformanceFrequency(&frequency);
1175 QueryPerformanceCounter(&start_time);
1176#endif
1177#endif
1178
1179 if (kls == NULL) {
1180 fprintf(stderr, "[ERROR] [%s()]: Passed Koliseo was NULL.\n", __func__);
1181 exit(EXIT_FAILURE);
1182 }
1183 if ((kls->has_temp == 1) && (kls->conf.kls_block_while_has_temp == 1)) {
1184 fprintf(stderr, "[ERROR] [%s()]: Passed Koliseo has an open Koliseo_Temp session.\n", __func__);
1185#ifdef KLS_DEBUG_CORE
1186 kls_log(kls, "ERROR", "[%s()]: Passed Koliseo has an open Koliseo_Temp session.", __func__);
1187 exit(EXIT_FAILURE);
1188#endif // KLS_DEBUG_CORE
1189 return NULL;
1190 }
1191#ifndef KOLISEO_HAS_LOCATE
1192 kls__check_available(kls, size, align, count);
1193#else
1194 kls__check_available_dbg(kls, size, align, count, KLS_HERE);
1195#endif // KOLISEO_HAS_LOCATE
1196 Koliseo* current = kls;
1197 while (current->next != NULL) {
1198 current = current->next;
1199 }
1200 ptrdiff_t padding = -current->offset & (align - 1);
1201 char *p = current->data + current->offset + padding;
1202 current->prev_offset = current->offset;
1203 current->offset += padding + size * count;
1204 char h_size[200];
1205 kls_formatSize(size * count, h_size, sizeof(h_size));
1206 //sprintf(msg,"Pushed size (%li) for KLS.",size);
1207 //kls_log("KLS",msg);
1208#ifdef KLS_DEBUG_CORE
1209 kls_log(current, "KLS", "Curr offset: { %p }.", current + current->offset);
1210 kls_log(current, "KLS", "API Level { %i } -> Pushed size (%s) for KLS.",
1211 int_koliseo_version(), h_size);
1212 if (current->conf.kls_verbose_lvl > 0) {
1213 print_kls_2file(current->conf.kls_log_fp, current);
1214 }
1215 if (current->conf.kls_collect_stats == 1) {
1216#ifndef _WIN32
1217 clock_gettime(CLOCK_MONOTONIC, &end_time); // %.9f
1218 double elapsed_time =
1219 (end_time.tv_sec - start_time.tv_sec) + (end_time.tv_nsec -
1220 start_time.tv_nsec) / 1e9;
1221#else
1222 QueryPerformanceCounter(&end_time); // %.7f
1223 double elapsed_time =
1224 (double)(end_time.QuadPart -
1225 start_time.QuadPart) / frequency.QuadPart;
1226#endif
1227 if (elapsed_time > current->stats.worst_pushcall_time) {
1228 current->stats.worst_pushcall_time = elapsed_time;
1229 }
1230 }
1231#endif
1232 if (current->conf.kls_collect_stats == 1) {
1233 current->stats.tot_pushes += 1;
1234 }
1235 return p;
1236}
1237
1247#ifndef KOLISEO_HAS_LOCATE
1248void *kls_push_zero(Koliseo *kls, ptrdiff_t size, ptrdiff_t align,
1249 ptrdiff_t count)
1250#else
1251void *kls_push_zero_dbg(Koliseo *kls, ptrdiff_t size, ptrdiff_t align,
1252 ptrdiff_t count, Koliseo_Loc loc)
1253#endif // KOLISEO_HAS_LOCATE
1254{
1255
1256#ifdef KLS_DEBUG_CORE
1257#ifndef _WIN32
1258 struct timespec start_time, end_time;
1259 clock_gettime(CLOCK_MONOTONIC, &start_time);
1260#else
1261 LARGE_INTEGER start_time, end_time, frequency;
1262 QueryPerformanceFrequency(&frequency);
1263 QueryPerformanceCounter(&start_time);
1264#endif
1265#endif
1266
1267 if (kls == NULL) {
1268 fprintf(stderr, "[ERROR] [%s()]: Passed Koliseo was NULL.\n", __func__);
1269 exit(EXIT_FAILURE);
1270 }
1271 if ((kls->has_temp == 1) && (kls->conf.kls_block_while_has_temp == 1)) {
1272#ifndef KOLISEO_HAS_LOCATE
1273 fprintf(stderr, "[ERROR] [%s()]: Passed Koliseo has an open Koliseo_Temp session.\n", __func__);
1274#else
1275 fprintf(stderr, "[ERROR] " KLS_Loc_Fmt "[%s()]: Passed Koliseo has an open Koliseo_Temp session.\n", KLS_Loc_Arg(loc), __func__);
1276 kls_log(kls, "ERROR", KLS_Loc_Fmt "[%s()]: Passed Koliseo has an open Koliseo_Temp session.", KLS_Loc_Arg(loc), __func__);
1277 exit(EXIT_FAILURE);
1278#endif // KOLISEO_HAS_LOCATE
1279 return NULL;
1280 }
1281#ifndef KOLISEO_HAS_LOCATE
1282 kls__check_available(kls, size, align, count);
1283#else
1284 kls__check_available_dbg(kls, size, align, count, loc);
1285#endif
1286 Koliseo* current = kls;
1287 while (current->next != NULL) {
1288 current = current->next;
1289 }
1290 ptrdiff_t padding = -current->offset & (align - 1);
1291 char *p = current->data + current->offset + padding;
1292 //Zero new area
1293 memset(p, 0, size * count);
1294 current->prev_offset = current->offset;
1295 current->offset += padding + size * count;
1296 char h_size[200];
1297 kls_formatSize(size * count, h_size, sizeof(h_size));
1298 //sprintf(msg,"Pushed zeroes, size (%li) for KLS.",size);
1299 //kls_log("KLS",msg);
1300#ifdef KLS_DEBUG_CORE
1301 kls_log(current, "KLS", "Curr offset: { %p }.", current + current->offset);
1302 kls_log(current, "KLS", "API Level { %i } -> Pushed zeroes, size (%s) for KLS.",
1303 int_koliseo_version(), h_size);
1304 if (current->conf.kls_verbose_lvl > 0) {
1305 print_kls_2file(current->conf.kls_log_fp, current);
1306 }
1307 if (current->conf.kls_collect_stats == 1) {
1308#ifndef _WIN32
1309 clock_gettime(CLOCK_MONOTONIC, &end_time); // %.9f
1310 double elapsed_time =
1311 (end_time.tv_sec - start_time.tv_sec) + (end_time.tv_nsec -
1312 start_time.tv_nsec) / 1e9;
1313#else
1314 QueryPerformanceCounter(&end_time); // %.7f
1315 double elapsed_time =
1316 (double)(end_time.QuadPart -
1317 start_time.QuadPart) / frequency.QuadPart;
1318#endif
1319 if (elapsed_time > current->stats.worst_pushcall_time) {
1320 current->stats.worst_pushcall_time = elapsed_time;
1321 }
1322 }
1323#endif
1324 if (current->conf.kls_collect_stats == 1) {
1325 current->stats.tot_pushes += 1;
1326 }
1327 return p;
1328}
1329
1339#ifndef KOLISEO_HAS_LOCATE
1340void *kls_push_zero_ext(Koliseo *kls, ptrdiff_t size, ptrdiff_t align,
1341 ptrdiff_t count)
1342#else
1343void *kls_push_zero_ext_dbg(Koliseo *kls, ptrdiff_t size, ptrdiff_t align,
1344 ptrdiff_t count, Koliseo_Loc loc)
1345#endif // KOLISEO_HAS_LOCATE
1346{
1347
1348#ifdef KLS_DEBUG_CORE
1349#ifndef _WIN32
1350 struct timespec start_time, end_time;
1351 clock_gettime(CLOCK_MONOTONIC, &start_time);
1352#else
1353 LARGE_INTEGER start_time, end_time, frequency;
1354 QueryPerformanceFrequency(&frequency);
1355 QueryPerformanceCounter(&start_time);
1356#endif
1357#endif
1358
1359 if (kls == NULL) {
1360 fprintf(stderr, "[ERROR] [%s()]: Passed Koliseo was NULL.\n", __func__);
1361 exit(EXIT_FAILURE);
1362 }
1363 if ((kls->has_temp == 1) && (kls->conf.kls_block_while_has_temp == 1)) {
1364#ifndef KOLISEO_HAS_LOCATE
1365 fprintf(stderr, "[ERROR] [%s()]: Passed Koliseo has an open Koliseo_Temp session.\n", __func__);
1366#else
1367 fprintf(stderr, "[ERROR] " KLS_Loc_Fmt "[%s()]: Passed Koliseo has an open Koliseo_Temp session.\n", KLS_Loc_Arg(loc), __func__);
1368 kls_log(kls, "ERROR", KLS_Loc_Fmt "[%s()]: Passed Koliseo has an open Koliseo_Temp session.", KLS_Loc_Arg(loc), __func__);
1369 exit(EXIT_FAILURE);
1370#endif // KOLISEO_HAS_LOCATE
1371 return NULL;
1372 }
1373
1374#ifndef KOLISEO_HAS_LOCATE
1375 kls__check_available(kls, size, align, count);
1376#else
1377 kls__check_available_dbg(kls, size, align, count, loc);
1378#endif
1379 Koliseo* current = kls;
1380 while (current->next != NULL) {
1381 current = current->next;
1382 }
1383 ptrdiff_t padding = -current->offset & (align - 1);
1384 char *p = current->data + current->offset + padding;
1385 //Zero new area
1386 memset(p, 0, size * count);
1387 current->prev_offset = current->offset;
1388 current->offset += padding + size * count;
1389
1390 for (size_t i=0; i < kls->hooks_len; i++) {
1391 if (current->hooks[i].on_push_handler != NULL) {
1392 /*
1393 struct KLS_EXTENSION_AR_DEFAULT_ARGS {
1394 const char* region_name;
1395 size_t region_name_len;
1396 const char* region_desc;
1397 size_t region_desc_len;
1398 int region_type;
1399 };
1400 struct KLS_EXTENSION_AR_DEFAULT_ARGS ar_args = {
1401 .region_name = KOLISEO_DEFAULT_REGION_NAME,
1402 .region_name_len = strlen(KOLISEO_DEFAULT_REGION_NAME),
1403 .region_desc = KOLISEO_DEFAULT_REGION_DESC,
1404 .region_desc_len = strlen(KOLISEO_DEFAULT_REGION_DESC),
1405 .region_type = KLS_None
1406 };
1407 kls->hooks.on_push_handler(kls, padding, (void*)&ar_args);
1408 */
1409 current->hooks[i].on_push_handler(current, padding, __func__, NULL);
1410 }
1411 }
1412
1413 char h_size[200];
1414 kls_formatSize(size * count, h_size, sizeof(h_size));
1415 //sprintf(msg,"Pushed zeroes, size (%li) for KLS.",size);
1416 //kls_log("KLS",msg);
1417#ifdef KLS_DEBUG_CORE
1418 kls_log(current, "KLS", "Curr offset: { %p }.", current + current->offset);
1419 kls_log(current, "KLS", "API Level { %i } -> Pushed zeroes, size (%s) for KLS.",
1420 int_koliseo_version(), h_size);
1421 if (current->conf.kls_verbose_lvl > 0) {
1422 print_kls_2file(current->conf.kls_log_fp, current);
1423 }
1424 if (current->conf.kls_collect_stats == 1) {
1425#ifndef _WIN32
1426 clock_gettime(CLOCK_MONOTONIC, &end_time); // %.9f
1427 double elapsed_time =
1428 (end_time.tv_sec - start_time.tv_sec) + (end_time.tv_nsec -
1429 start_time.tv_nsec) / 1e9;
1430#else
1431 QueryPerformanceCounter(&end_time); // %.7f
1432 double elapsed_time =
1433 (double)(end_time.QuadPart -
1434 start_time.QuadPart) / frequency.QuadPart;
1435#endif
1436 if (elapsed_time > current->stats.worst_pushcall_time) {
1437 current->stats.worst_pushcall_time = elapsed_time;
1438 }
1439 }
1440#endif
1441 if (current->conf.kls_collect_stats == 1) {
1442 current->stats.tot_pushes += 1;
1443 }
1444 return p;
1445}
1446
1457#ifndef KOLISEO_HAS_LOCATE
1458void *kls_temp_push_zero_ext(Koliseo_Temp *t_kls, ptrdiff_t size,
1459 ptrdiff_t align, ptrdiff_t count)
1460#else
1461void *kls_temp_push_zero_ext_dbg(Koliseo_Temp *t_kls, ptrdiff_t size,
1462 ptrdiff_t align, ptrdiff_t count, Koliseo_Loc loc)
1463#endif // KOLISEO_HAS_LOCATE
1464{
1465
1466#ifdef KLS_DEBUG_CORE
1467#ifndef _WIN32
1468 struct timespec start_time, end_time;
1469 clock_gettime(CLOCK_MONOTONIC, &start_time);
1470#else
1471 LARGE_INTEGER start_time, end_time, frequency;
1472 QueryPerformanceFrequency(&frequency);
1473 QueryPerformanceCounter(&start_time);
1474#endif
1475#endif
1476
1477 if (t_kls == NULL) {
1478 fprintf(stderr, "[ERROR] [%s()]: Passed Koliseo_Temp was NULL.\n",
1479 __func__);
1480 exit(EXIT_FAILURE);
1481 }
1482 Koliseo *kls = t_kls->kls;
1483 if (kls == NULL) {
1484 fprintf(stderr, "[ERROR] [%s()]: Referred Koliseo was NULL.\n",
1485 __func__);
1486 exit(EXIT_FAILURE);
1487 }
1488#ifndef KOLISEO_HAS_LOCATE
1489 kls__check_available(kls, size, align, count);
1490#else
1491 kls__check_available_dbg(kls, size, align, count, loc);
1492#endif
1493 Koliseo* current = kls;
1494 while (current->next != NULL) {
1495 current = current->next;
1496 }
1497 ptrdiff_t padding = -current->offset & (align - 1);
1498 char *p = current->data + current->offset + padding;
1499 //Zero new area
1500 memset(p, 0, size * count);
1501 current->prev_offset = current->offset;
1502 current->offset += padding + size * count;
1503
1504 for (size_t i=0; i < kls->hooks_len; i++) {
1505 if (current->hooks[i].on_temp_push_handler != NULL) {
1506 // Call on_temp_push extension with empty user arg
1507 current->hooks[i].on_temp_push_handler(t_kls, padding, __func__, NULL);
1508 }
1509 }
1510
1511 char h_size[200];
1512 kls_formatSize(size * count, h_size, sizeof(h_size));
1513 //sprintf(msg,"Pushed zeroes, size (%li) for KLS.",size);
1514 //kls_log("KLS",msg);
1515#ifdef KLS_DEBUG_CORE
1516 if (current->conf.kls_collect_stats == 1) {
1517#ifndef _WIN32
1518 clock_gettime(CLOCK_MONOTONIC, &end_time); // %.9f
1519 double elapsed_time =
1520 (end_time.tv_sec - start_time.tv_sec) + (end_time.tv_nsec -
1521 start_time.tv_nsec) / 1e9;
1522#else
1523 QueryPerformanceCounter(&end_time); // %.7f
1524 double elapsed_time =
1525 (double)(end_time.QuadPart -
1526 start_time.QuadPart) / frequency.QuadPart;
1527#endif
1528 if (elapsed_time > current->stats.worst_pushcall_time) {
1529 current->stats.worst_pushcall_time = elapsed_time;
1530 }
1531 }
1532 kls_log(current, "KLS", "Curr offset: { %p }.", current + current->offset);
1533 kls_log(current, "KLS",
1534 "API Level { %i } -> Pushed zeroes, size (%s) for Temp_KLS.",
1535 int_koliseo_version(), h_size);
1536 if (current->conf.kls_verbose_lvl > 0) {
1537 print_kls_2file(current->conf.kls_log_fp, current);
1538 }
1539#endif
1540 if (current->conf.kls_collect_stats == 1) {
1541 current->stats.tot_temp_pushes += 1;
1542 }
1543 return p;
1544}
1545
1551void print_kls_2file(FILE *fp, const Koliseo *kls)
1552{
1553 if (fp == NULL) {
1554 fprintf(stderr, "print_kls_2file(): fp was NULL.\n");
1555 return;
1556 }
1557 if (kls == NULL) {
1558 fprintf(fp, "[KLS] kls was NULL.\n");
1559 } else {
1560 fprintf(fp, "\n[KLS] API Level: { %i }\n", int_koliseo_version());
1561 fprintf(fp, "\n[INFO] Conf: { " KLS_Conf_Fmt " }\n",
1562 KLS_Conf_Arg(kls->conf));
1563 fprintf(fp, "\n[INFO] Stats: { " KLS_Stats_Fmt " }\n",
1564 KLS_Stats_Arg(kls->stats));
1565 fprintf(fp, "\n[KLS] Size: { %td }\n", kls->size);
1566 char human_size[200];
1567 char curr_size[200];
1568 kls_formatSize(kls->size, human_size, sizeof(human_size));
1569 fprintf(fp, "[KLS] Size (Human): { %s }\n", human_size);
1570 kls_formatSize(kls->offset, curr_size, sizeof(curr_size));
1571 fprintf(fp, "[KLS] Used (Human): { %s }\n", curr_size);
1572 fprintf(fp, "[KLS] Offset: { %td }\n", kls->offset);
1573 fprintf(fp, "[KLS] Prev_Offset: { %td }\n", kls->prev_offset);
1574 fprintf(fp, "\n");
1575 }
1576}
1577
1582void print_dbg_kls(const Koliseo *kls)
1583{
1584 if (kls == NULL) {
1585 fprintf(stderr, "[ERROR] [%s()]: Passed Koliseo was NULL.\n", __func__);
1586 exit(EXIT_FAILURE);
1587 }
1588 print_kls_2file(stderr, kls);
1589}
1590
1596void print_temp_kls_2file(FILE *fp, const Koliseo_Temp *t_kls)
1597{
1598 if (fp == NULL) {
1599 fprintf(stderr, "print_temp_kls_2file(): fp was NULL.\n");
1600 exit(EXIT_FAILURE);
1601 }
1602 if (t_kls == NULL) {
1603 fprintf(fp, "[KLS_T] t_kls was NULL.");
1604 } else if (t_kls->kls == NULL) {
1605 fprintf(fp, "[KLS_T] [%s()]: Referred Koliseo was NULL.\n", __func__);
1606 } else {
1607 const Koliseo *kls = t_kls->kls;
1608 fprintf(fp, "\n[KLS_T] API Level: { %i }\n", int_koliseo_version());
1609 fprintf(fp, "\n[KLS_T] Temp Size: { %td }\n",
1610 kls->size - t_kls->offset);
1611 fprintf(fp, "\n[KLS_T] Refer Size: { %td }\n", kls->size);
1612 char human_size[200];
1613 char curr_size[200];
1614 kls_formatSize(kls->size - t_kls->offset, human_size,
1615 sizeof(human_size));
1616 fprintf(fp, "[KLS_T] Temp Size Human: { %s }\n", human_size);
1617 kls_formatSize(kls->size, human_size, sizeof(human_size));
1618 fprintf(fp, "[KLS_T] Refer Size Human: { %s }\n", human_size);
1619 kls_formatSize(kls->offset, curr_size, sizeof(curr_size));
1620 fprintf(fp, "[KLS_T] Inner Used (Human): { %s }\n", curr_size);
1621 kls_formatSize(t_kls->offset, curr_size, sizeof(curr_size));
1622 fprintf(fp, "[KLS_T] Temp Used (Human): { %s }\n", curr_size);
1623 fprintf(fp, "[KLS_T] Inner Offset: { %td }\n", kls->offset);
1624 fprintf(fp, "[KLS_T] Temp Offset: { %td }\n", t_kls->offset);
1625 fprintf(fp, "[KLS_T] Inner Prev_Offset: { %td }\n", kls->prev_offset);
1626 fprintf(fp, "[KLS_T] Temp Prev_Offset: { %td }\n\n",
1627 t_kls->prev_offset);
1628 }
1629}
1630
1636{
1637 print_temp_kls_2file(stderr, t_kls);
1638}
1639
1647void kls_formatSize(ptrdiff_t size, char *outputBuffer, size_t bufferSize)
1648{
1649 const char *units[] =
1650 { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
1651 const int numUnits = sizeof(units) / sizeof(units[0]);
1652
1653 int unitIndex = 0;
1654 double sizeValue = (double)size;
1655
1656 while (sizeValue >= 1000 && unitIndex < numUnits - 1) {
1657 sizeValue /= 1000;
1658 unitIndex++;
1659 }
1660
1661 snprintf(outputBuffer, bufferSize, "%.2f %s", sizeValue, units[unitIndex]);
1662}
1663
1670{
1671 if (kls == NULL) {
1672 fprintf(stderr, "[ERROR] [%s()]: Passed Koliseo was NULL.\n", __func__);
1673 exit(EXIT_FAILURE);
1674 }
1675 //Reset pointer
1676 kls->prev_offset = kls->offset;
1677 kls->offset = sizeof(*kls);
1678#ifdef KLS_DEBUG_CORE
1679 kls_log(kls, "KLS", "API Level { %i } -> Cleared offsets for KLS.",
1681#endif
1682}
1683
1690{
1691 if (kls == NULL) {
1692 fprintf(stderr, "[ERROR] [%s()]: Passed Koliseo was NULL.\n", __func__);
1693 exit(EXIT_FAILURE);
1694 }
1695 Koliseo* current = kls;
1696 while (current) {
1697 Koliseo* next = current->next;
1698 current->next = NULL;
1699 for (size_t i=0; i < current->hooks_len; i++) {
1700 if (current->hooks[i].on_free_handler != NULL) {
1701 // Call on_free() extension
1702 current->hooks[i].on_free_handler(current);
1703 }
1704 }
1705 if (current->has_temp == 1) {
1706#ifdef KLS_DEBUG_CORE
1707 kls_log(current, "KLS",
1708 "API Level { %i } -> KLS had an active Koliseo_Temp.",
1710#endif
1711 kls_temp_end(current->t_kls);
1712 }
1713 kls_clear(current);
1714#ifdef KLS_DEBUG_CORE
1715 kls_log(current, "KLS", "API Level { %i } -> Freeing KLS.",
1717#endif
1718 if (current->conf.kls_log_fp != NULL && current->conf.kls_log_fp != stdout
1719 && current->conf.kls_log_fp != stderr) {
1720#ifdef KLS_DEBUG_CORE
1721 kls_log(current, "KLS", "Closing kls log file. Path: {\"%s\"}.",
1722 kls->conf.kls_log_filepath);
1723#endif
1724 int close_res = fclose(current->conf.kls_log_fp);
1725 if (close_res != 0) {
1726 fprintf(stderr,
1727 "[ERROR] %s(): Failed fclose() on log_fp. Path: {\"%s\"}.",
1728 __func__, current->conf.kls_log_filepath);
1729 }
1730 } else if (current->conf.kls_log_fp == stdout || current->conf.kls_log_fp == stderr) {
1731 if (current->conf.kls_verbose_lvl > 1) {
1732 fprintf(stderr,
1733 "[INFO] %s(): kls->conf.kls_log_fp is %s. Not closing it.\n",
1734 __func__,
1735 (current->conf.kls_log_fp == stdout ? "stdout" : "stderr"));
1736 }
1737 }
1738 if (current->free_func == NULL) {
1739 fprintf(stderr,
1740 "[ERROR] %s(): free function was NULL.\n", __func__);
1741 return;
1742 }
1743 current->free_func(current);
1744 current = next;
1745 }
1746}
1747
1755#ifndef KOLISEO_HAS_LOCATE
1757#else
1758Koliseo_Temp *kls_temp_start_dbg(Koliseo *kls, Koliseo_Loc loc)
1759#endif // KOLISEO_HAS_LOCATE
1760{
1761 if (kls == NULL) {
1762 fprintf(stderr, "[ERROR] [%s()]: Passed Koliseo was NULL.\n", __func__);
1763 exit(EXIT_FAILURE);
1764 }
1765 Koliseo* current = kls;
1766 while (current->next != NULL) {
1767 current = current->next;
1768 }
1769 if (current->has_temp != 0) {
1770 fprintf(stderr,
1771 "[ERROR] [%s()]: Passed Koliseo->has_temp is not 0. {%i}\n",
1772 __func__, current->has_temp);
1773#ifdef KLS_DEBUG_CORE
1774 kls_log(current, "ERROR", "[%s()]: Passed Koliseo->has_temp != 0 . {%i}",
1775 __func__, current->has_temp);
1776#endif
1777 if (current->conf.kls_collect_stats == 1) {
1778 current->stats.tot_hiccups += 1;
1779 }
1780 return NULL;
1781 }
1782 ptrdiff_t prev = current->prev_offset;
1783 ptrdiff_t off = current->offset;
1784
1785 Koliseo_Temp *tmp = KLS_PUSH(current, Koliseo_Temp);
1786 tmp->kls = current;
1787 tmp->prev_offset = prev;
1788 tmp->offset = off;
1789#ifdef KLS_DEBUG_CORE
1790 kls_log(current, "INFO", "Passed kls conf: " KLS_Conf_Fmt "\n",
1791 KLS_Conf_Arg(current->conf));
1792#endif
1793
1794 current->has_temp = 1;
1795 current->t_kls = tmp;
1796 for (size_t i=0; i < kls->hooks_len; i++) {
1797 if (current->hooks[i].on_temp_start_handler != NULL) {
1798 // Call on_temp_start extension
1799 current->hooks[i].on_temp_start_handler(tmp);
1800 }
1801 }
1802#ifdef KLS_DEBUG_CORE
1803 kls_log(current, "KLS", "Prepared new Temp KLS.");
1804#endif
1805 return tmp;
1806}
1807
1813{
1814 if (tmp_kls == NULL) {
1815 fprintf(stderr, "[ERROR] [%s()]: Passed Koliseo_Temp was NULL.\n",
1816 __func__);
1817 exit(EXIT_FAILURE);
1818 }
1819
1820 Koliseo *kls_ref = tmp_kls->kls;
1821 if (kls_ref == NULL) {
1822 fprintf(stderr, "[ERROR] [%s()]: Referred Koliseo was NULL.\n",
1823 __func__);
1824 exit(EXIT_FAILURE);
1825 }
1826
1827 for (size_t i=0; i < kls_ref->hooks_len; i++) {
1828 if (kls_ref->hooks[i].on_temp_free_handler != NULL) {
1829 // Call on_temp_free() extension
1830 kls_ref->hooks[i].on_temp_free_handler(tmp_kls);
1831 }
1832 }
1833
1834#ifdef KLS_DEBUG_CORE
1835 kls_log(kls_ref, "KLS", "Ended Temp KLS.");
1836#endif
1837 tmp_kls->kls->has_temp = 0;
1838 tmp_kls->kls->t_kls = NULL;
1839 tmp_kls->kls->prev_offset = tmp_kls->prev_offset;
1840 tmp_kls->kls->offset = tmp_kls->offset;
1841
1842 // Free any Koliseo chained after the current one
1843 Koliseo* to_free = tmp_kls->kls->next;
1844 if (to_free != NULL) {
1845 kls_free(to_free);
1846 tmp_kls->kls->next = NULL;
1847 }
1848
1849 tmp_kls = NULL; // statement with no effect TODO: Clear tmp_kls from caller
1850 if (kls_ref->conf.kls_collect_stats == 1) {
1851 kls_ref->stats.tot_temp_pushes = 0;
1852 kls_ref->stats.tot_temp_pops = 0;
1853 }
1854}
1855
1856#ifdef KOLISEO_HAS_EXPER
1865void *kls_pop(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count)
1866{
1867 if (kls == NULL) {
1868 fprintf(stderr, "[ERROR] [%s()]: Passed Koliseo was NULL.\n", __func__);
1869 exit(EXIT_FAILURE);
1870 }
1871 ptrdiff_t padding = -kls->offset & (align - 1);
1872 if (count > PTRDIFF_MAX / size
1873 || (kls->size + kls->offset) < (size * count)) {
1874 fprintf(stderr, "[KLS] Failed %s() call.\n", __func__);
1875 kls_free(kls);
1876 exit(EXIT_FAILURE);
1877 }
1878 char *p = kls->data + kls->offset - padding - size * count;
1879 kls->prev_offset = kls->offset;
1880 kls->offset -= padding + size * count;
1881#ifdef KLS_DEBUG_CORE
1882 kls_log(kls, "KLS", "API Level { %i } -> Popped (%li) for KLS.",
1883 int_koliseo_version(), size);
1884 if (kls->conf.kls_verbose_lvl > 0) {
1885 print_kls_2file(kls->conf.kls_log_fp, kls);
1886 }
1887#endif
1888 if (kls->conf.kls_collect_stats == 1) {
1889 kls->stats.tot_pops += 1;
1890 }
1891 return p;
1892}
1893
1902void *kls_pop_AR(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count)
1903{
1904 if (kls == NULL) {
1905 fprintf(stderr, "[ERROR] [%s()]: Passed Koliseo was NULL.\n", __func__);
1906 exit(EXIT_FAILURE);
1907 }
1908 ptrdiff_t padding = -kls->offset & (align - 1);
1909 if (count > PTRDIFF_MAX / size
1910 || (kls->size + kls->offset) < (size * count)) {
1911 fprintf(stderr, "[KLS] Failed %s() call.\n", __func__);
1912 kls_free(kls);
1913 exit(EXIT_FAILURE);
1914 }
1915 char *p = kls->data + kls->offset - padding - size * count;
1916 kls->prev_offset = kls->offset;
1917 kls->offset -= padding + size * count;
1918#ifdef KLS_DEBUG_CORE
1919 kls_log(kls, "KLS", "API Level { %i } -> Popped (%li) for KLS.",
1920 int_koliseo_version(), size);
1921 if (kls->conf.kls_verbose_lvl > 0) {
1922 print_kls_2file(kls->conf.kls_log_fp, kls);
1923 }
1924#endif
1925 if (kls->conf.kls_collect_stats == 1) {
1926 kls->stats.tot_pops += 1;
1927 }
1928 return p;
1929}
1930
1939void *kls_temp_pop(Koliseo_Temp *t_kls, ptrdiff_t size, ptrdiff_t align,
1940 ptrdiff_t count)
1941{
1942 if (t_kls == NULL) {
1943 fprintf(stderr, "[ERROR] [%s()]: Passed Koliseo_Temp was NULL.\n",
1944 __func__);
1945 exit(EXIT_FAILURE);
1946 }
1947 Koliseo *kls = t_kls->kls;
1948 if (kls == NULL) {
1949 fprintf(stderr, "[ERROR] [%s()]: Referred Koliseo was NULL.\n",
1950 __func__);
1951 exit(EXIT_FAILURE);
1952 }
1953 ptrdiff_t padding = -kls->offset & (align - 1);
1954 if (count > PTRDIFF_MAX / size
1955 || (kls->size + kls->offset) < (size * count)) {
1956 fprintf(stderr, "[KLS] Failed %s() call.\n", __func__);
1957 kls_free(kls);
1958 exit(EXIT_FAILURE);
1959 }
1960 char *p = kls->data + kls->offset - padding - size * count;
1961 kls->prev_offset = kls->offset;
1962 kls->offset -= padding + size * count;
1963#ifdef KLS_DEBUG_CORE
1964 kls_log(kls, "KLS", "Curr offset: { %p }.", kls + kls->offset);
1965 kls_log(kls, "KLS", "API Level { %i } -> Popped (%li) for Temp_KLS.",
1966 int_koliseo_version(), size);
1967 if (kls->conf.kls_verbose_lvl > 0) {
1968 print_kls_2file(kls->conf.kls_log_fp, kls);
1969 }
1970#endif
1971 if (kls->conf.kls_collect_stats == 1) {
1972 kls->stats.tot_temp_pops += 1;
1973 }
1974 return p;
1975}
1976
1985void *kls_temp_pop_AR(Koliseo_Temp *t_kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count)
1986{
1987 if (t_kls == NULL) {
1988 fprintf(stderr, "[ERROR] [%s()]: Passed Koliseo_Temp was NULL.\n",
1989 __func__);
1990 exit(EXIT_FAILURE);
1991 }
1992 Koliseo *kls = t_kls->kls;
1993 if (kls == NULL) {
1994 fprintf(stderr, "[ERROR] [%s()]: Referred Koliseo was NULL.\n",
1995 __func__);
1996 exit(EXIT_FAILURE);
1997 }
1998 ptrdiff_t padding = -kls->offset & (align - 1);
1999 if (count > PTRDIFF_MAX / size
2000 || (kls->size + kls->offset) < (size * count)) {
2001 fprintf(stderr, "[KLS] Failed %s() call.\n", __func__);
2002 kls_free(kls);
2003 exit(EXIT_FAILURE);
2004 }
2005 char *p = kls->data + kls->offset - padding - size * count;
2006 kls->prev_offset = kls->offset;
2007 kls->offset -= padding + size * count;
2008#ifdef KLS_DEBUG_CORE
2009 kls_log(kls, "KLS", "Curr offset: { %p }.", kls + kls->offset);
2010 kls_log(kls, "KLS", "API Level { %i } -> Popped (%li) for Temp_KLS.",
2011 int_koliseo_version(), size);
2012 if (kls->conf.kls_verbose_lvl > 0) {
2013 print_kls_2file(kls->conf.kls_log_fp, kls);
2014 }
2015#endif
2016 if (kls->conf.kls_collect_stats == 1) {
2017 kls->stats.tot_temp_pops += 1;
2018 }
2019 return p;
2020}
2021
2028char* kls_strdup(Koliseo* kls, char* source)
2029{
2030 char* dest = KLS_PUSH_STR(kls, source);
2031 KLS__STRCPY(dest, source);
2032 return dest;
2033}
2034
2041char** kls_strdup_arr(Koliseo* kls, size_t count, char** source)
2042{
2043 char** strings = NULL;
2044 strings = KLS_PUSH_ARR(kls, char*, count);
2045 for (int i=0; i < count; i++) {
2046 strings[i] = KLS_STRDUP(kls, source[i]);
2047 }
2048 return strings;
2049}
2050
2057char* kls_t_strdup(Koliseo_Temp* t_kls, char* source)
2058{
2059 char* dest = KLS_PUSH_STR_T(t_kls, source);
2060 KLS__STRCPY(dest, source);
2061 return dest;
2062}
2063
2070char** kls_t_strdup_arr(Koliseo_Temp* t_kls, size_t count, char** source)
2071{
2072 char** strings = NULL;
2073 strings = KLS_PUSH_ARR_T(t_kls, char*, count);
2074 for (int i=0; i < count; i++) {
2075 strings[i] = KLS_STRDUP_T(t_kls, source[i]);
2076 }
2077 return strings;
2078}
2079#endif // KOLISEO_HAS_EXPER
#define KLS_DEFAULT_HOOKS
Definition kls_region.h:162
#define KLS_DEFAULT_EXTENSIONS_LEN
Definition kls_region.h:173
Koliseo * kls_new_traced(ptrdiff_t size, const char *output_path)
Takes a ptrdiff_t size and a filepath for the trace output file.
Definition koliseo.c:647
Koliseo * kls_new(ptrdiff_t size)
Takes a ptrdiff_t size.
Definition koliseo.c:455
char * kls_strdup(Koliseo *kls, char *source)
Function to dupe a C string to a Koliseo, and return a pointer to the allocated string.
Definition koliseo.c:2028
void * kls_temp_pop(Koliseo_Temp *t_kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count)
Takes a Koliseo_Temp, and ptrdiff_t values for size, align and count.
Definition koliseo.c:1939
Koliseo_Temp * kls_temp_start(Koliseo *kls)
Starts a new savestate for the passed Koliseo pointer, by initialising its Koliseo_Temp pointer and r...
Definition koliseo.c:1756
void print_dbg_temp_kls(const Koliseo_Temp *t_kls)
Prints header fields from the passed Koliseo_Temp pointer, to stderr.
Definition koliseo.c:1635
Koliseo * kls_new_dbg_alloc(ptrdiff_t size, kls_alloc_func alloc_func, kls_free_func free_func)
Takes a ptrdiff_t size, an allocation function pointer and a free function pointer,...
Definition koliseo.c:748
void print_temp_kls_2file(FILE *fp, const Koliseo_Temp *t_kls)
Prints header fields from the passed Koliseo_Temp pointer, to the passed FILE pointer.
Definition koliseo.c:1596
char * kls_t_strdup(Koliseo_Temp *t_kls, char *source)
Function to dupe a C string to a Koliseo_Temp, and return a pointer to the allocated string.
Definition koliseo.c:2057
Koliseo * kls_new_traced_alloc_handled(ptrdiff_t size, const char *output_path, kls_alloc_func alloc_func, kls_free_func free_func, KLS_Err_Handlers err_handlers)
Takes a ptrdiff_t size, a filepath for the trace output file, an allocation function pointer and a fr...
Definition koliseo.c:594
KLS_Stats KLS_STATS_DEFAULT
Default KLS_Stats values, used by kls_new().
Definition koliseo.c:41
void kls_log(Koliseo *kls, const char *tag, const char *format,...)
Logs a message to the log_fp FILE field of the passed Koliseo pointer, if its conf....
Definition koliseo.c:290
Koliseo * kls_new_traced_alloc_handled_ext(ptrdiff_t size, const char *output_path, kls_alloc_func alloc_func, kls_free_func free_func, KLS_Err_Handlers err_handlers, KLS_Hooks *ext_handlers, void **user, size_t ext_len)
Takes a ptrdiff_t size, a filepath for the trace output file, an allocation function pointer and a fr...
Definition koliseo.c:558
void kls_temp_end(Koliseo_Temp *tmp_kls)
Ends passed Koliseo_Temp pointer.
Definition koliseo.c:1812
void kls_dbg_features(void)
Prints enabled Koliseo features to stderr.
Definition koliseo.c:226
char ** kls_strdup_arr(Koliseo *kls, size_t count, char **source)
Function to dupe a C string array to a Koliseo, and return a pointer to the allocated array.
Definition koliseo.c:2041
void * kls_temp_push_zero_ext(Koliseo_Temp *t_kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count)
Takes a Koliseo_Temp, and ptrdiff_t values for size, align and count.
Definition koliseo.c:1458
void print_dbg_kls(const Koliseo *kls)
Prints header fields from the passed Koliseo pointer, to stderr.
Definition koliseo.c:1582
const char * string_koliseo_version(void)
Returns the constant string representing current version for Koliseo.
Definition koliseo.c:59
ptrdiff_t kls_get_pos(const Koliseo *kls)
Returns the current offset (position of pointer bumper) for the passed Koliseo.
Definition koliseo.c:279
void * kls_temp_pop_AR(Koliseo_Temp *t_kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count)
Takes a Koliseo_Temp, and ptrdiff_t values for size, align and count.
Definition koliseo.c:1985
void * kls_pop_AR(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count)
Takes a Koliseo pointer, and ptrdiff_t values for size, align and count.
Definition koliseo.c:1902
void kls_free(Koliseo *kls)
Calls kls_clear() on the passed Koliseo pointer and the frees the actual Koliseo.
Definition koliseo.c:1689
KLS_Conf kls_conf_init_handled(int collect_stats, int verbose_lvl, int block_while_has_temp, int allow_zerocount_push, int growable, FILE *log_fp, const char *log_filepath, KLS_Err_Handlers err_handlers)
Used to prepare a KLS_Conf without caring about KOLISEO_HAS_REGIONS.
Definition koliseo.c:169
Koliseo * kls_new_traced_ext(ptrdiff_t size, const char *output_path, KLS_Hooks *ext_handlers, void **user, size_t ext_len)
Takes a ptrdiff_t size and a filepath for the trace output file.
Definition koliseo.c:611
char ** kls_t_strdup_arr(Koliseo_Temp *t_kls, size_t count, char **source)
Function to dupe a C string array to a Koliseo_Temp, and return a pointer to the allocated array.
Definition koliseo.c:2070
void * kls_push(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count)
Takes a Koliseo pointer, and ptrdiff_t values for size, align and count.
Definition koliseo.c:1165
Koliseo * kls_new_dbg_alloc_handled(ptrdiff_t size, kls_alloc_func alloc_func, kls_free_func free_func, KLS_Err_Handlers err_handlers)
Takes a ptrdiff_t size, an allocation function pointer and a free function pointer,...
Definition koliseo.c:716
void * kls_push_zero(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count)
Takes a Koliseo pointer, and ptrdiff_t values for size, align and count.
Definition koliseo.c:1248
Koliseo * kls_new_alloc(ptrdiff_t size, kls_alloc_func alloc_func, kls_free_func free_func)
Takes a ptrdiff_t size and a function pointer to the allocation function.
Definition koliseo.c:431
Koliseo * kls_new_dbg(ptrdiff_t size)
Takes a ptrdiff_t size and returns a pointer to the prepared Koliseo.
Definition koliseo.c:763
int kls__check_available_failable(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count, const char *caller_name)
Takes a Koliseo, a ptrdiff_t size, align and count, and a caller name.
Definition koliseo.c:906
Koliseo * kls_new_traced_alloc(ptrdiff_t size, const char *output_path, kls_alloc_func alloc_func, kls_free_func free_func)
Takes a ptrdiff_t size, a filepath for the trace output file, an allocation function pointer and a fr...
Definition koliseo.c:630
Koliseo * kls_new_conf_alloc_ext(ptrdiff_t size, KLS_Conf conf, kls_alloc_func alloc_func, kls_free_func free_func, KLS_Hooks *ext_handlers, void **user, size_t ext_len)
Takes a ptrdiff_t size, a KLS_Conf to configure the new Koliseo, an allocation function pointer and a...
Definition koliseo.c:476
Koliseo * kls_new_traced_handled(ptrdiff_t size, const char *output_path, KLS_Err_Handlers err_handlers)
Takes a ptrdiff_t size and a filepath for the trace output file.
Definition koliseo.c:664
void * kls_pop(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count)
Takes a Koliseo pointer, and ptrdiff_t values for size, align and count.
Definition koliseo.c:1865
void KLS_PTRDIFF_MAX_default_handler__(struct Koliseo *kls, ptrdiff_t size, ptrdiff_t count)
Used by default when no handler is passed.
Definition koliseo.c:103
KLS_Conf KLS_DEFAULT_CONF
Config used by any new Koliseo by default.
Definition koliseo.c:20
KLS_Conf kls_conf_init(int collect_stats, int verbose_lvl, int block_while_has_temp, int allow_zerocount_push, int growable, FILE *log_fp, const char *log_filepath)
Used to prepare a KLS_Conf without caring about KOLISEO_HAS_REGIONS.
Definition koliseo.c:217
void KLS_ZEROCOUNT_default_handler__(Koliseo *kls, ptrdiff_t available, ptrdiff_t padding, ptrdiff_t size)
Used internally for handling zero-count in push calls when no user handler is provided.
Definition koliseo.c:145
Koliseo * kls_new_dbg_ext(ptrdiff_t size, KLS_Hooks *ext_handlers, void **user, size_t ext_len)
Takes a ptrdiff_t size.
Definition koliseo.c:731
Koliseo * kls_new_alloc_ext(ptrdiff_t size, kls_alloc_func alloc_func, kls_free_func free_func, KLS_Hooks *ext_handlers, void **user, size_t ext_len)
Takes a ptrdiff_t size and a function pointer to the allocation function.
Definition koliseo.c:336
void print_kls_2file(FILE *fp, const Koliseo *kls)
Prints header fields from the passed Koliseo pointer, to the passed FILE pointer.
Definition koliseo.c:1551
Koliseo * kls_new_dbg_alloc_handled_ext(ptrdiff_t size, kls_alloc_func alloc_func, kls_free_func free_func, KLS_Err_Handlers err_handlers, KLS_Hooks *ext_handlers, void **user, size_t ext_len)
Takes a ptrdiff_t size, an allocation function pointer and a free function pointer,...
Definition koliseo.c:682
Koliseo * kls_new_dbg_handled(ptrdiff_t size, KLS_Err_Handlers err_handlers)
Takes a ptrdiff_t size and returns a pointer to the prepared Koliseo.
Definition koliseo.c:778
void kls_clear(Koliseo *kls)
Resets the offset field for the passed Koliseo pointer.
Definition koliseo.c:1669
void * kls_push_zero_ext(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count)
Takes a Koliseo pointer, and ptrdiff_t values for size, align and count.
Definition koliseo.c:1340
void kls_formatSize(ptrdiff_t size, char *outputBuffer, size_t bufferSize)
Converts a ptrdiff_t size to human-readable SI units (modulo 1000).
Definition koliseo.c:1647
void KLS_OOM_default_handler__(Koliseo *kls, ptrdiff_t available, ptrdiff_t padding, ptrdiff_t size, ptrdiff_t count)
Used internally for handling Out-Of-Memory in push calls when no user handler is provided.
Definition koliseo.c:83
Koliseo * kls_new_conf_alloc(ptrdiff_t size, KLS_Conf conf, kls_alloc_func alloc_func, kls_free_func free_func)
Takes a ptrdiff_t size, a KLS_Conf to configure the new Koliseo, an allocation function pointer and a...
Definition koliseo.c:505
int int_koliseo_version(void)
Returns the constant int representing current version for Koliseo.
Definition koliseo.c:68
Koliseo * kls_new_conf(ptrdiff_t size, KLS_Conf conf)
Takes a ptrdiff_t size and a KLS_Conf to configure the new Koliseo.
Definition koliseo.c:538
Koliseo * kls_new_conf_ext(ptrdiff_t size, KLS_Conf conf, KLS_Hooks *ext_handlers, void **user, size_t ext_len)
Takes a ptrdiff_t size and a KLS_Conf to configure the new Koliseo.
Definition koliseo.c:522
void kls_free_func(void *)
Used to select a free function for the arena's backing memory.
Definition koliseo.h:83
#define KLS_PUSH_STR_T(kls_temp, cstr)
Macro to request memory for a C string from a Koliseo_Temp.
Definition koliseo.h:555
#define KLS_Stats_Fmt
Defines a format string for KLS_Stats.
Definition koliseo.h:284
#define KLS_PUSH_STR(kls, cstr)
Macro to request memory for a C string from a Koliseo.
Definition koliseo.h:470
#define KLS_Conf_Arg(conf)
Defines a format macro for KLS_Conf args.
Definition koliseo.h:277
#define KLS_DEFAULT_FREEF
Defines the default free function.
Definition koliseo.h:394
#define KLS_DEFAULT_ALLOCF
Defines the default allocation function.
Definition koliseo.h:390
#define KLS_PUSH(kls, type)
Macro used to request memory from a Koliseo.
Definition koliseo.h:495
#define KLS__STRCPY(dest, source)
Macro to copy a C string from a source buffer to a destination buffer.
Definition koliseo.h:659
#define KLS_DEFAULT_EXTENSION_DATA
Defines default hooks that are loaded on kls_new() variants lacking explicit set of KLS_Hooks.
Definition koliseo.h:213
void * kls_alloc_func(size_t)
Used to select an allocation function for the arena's backing memory.
Definition koliseo.h:82
#define KLS_MAX(a, b)
Definition koliseo.h:89
#define KLS_STRDUP(kls, source)
Definition koliseo.h:668
#define KLS_Stats_Arg(stats)
Defines a format macro for KLS_Stats args.
Definition koliseo.h:294
#define KLS_Conf_Fmt
Defines a format string for KLS_Conf.
Definition koliseo.h:271
#define kls__check_available(kls, size, align, count)
Macro to return NULL on errors from kls__check_available_failable.
Definition koliseo.h:429
#define KLS_PUSH_ARR_T(kls_temp, type, count)
Macro used to request memory for an array of type values from a Koliseo_Temp.
Definition koliseo.h:549
#define KLS_STRDUP_T(t_kls, source)
Definition koliseo.h:678
#define KLS_DEFAULT_ERR_HANDLERS
Definition koliseo.h:169
#define KLS_PUSH_ARR(kls, type, count)
Macro used to request memory for an array of type values from a Koliseo.
Definition koliseo.h:464
Defines flags for Koliseo.
Definition koliseo.h:220
int kls_growable
If set to 1, make the Koliseo grow when a out of memory for a push call.
Definition koliseo.h:227
int kls_collect_stats
If set to 1, make the Koliseo collect performance stats.
Definition koliseo.h:221
int kls_allow_zerocount_push
If set to 1, make the Koliseo accept push calls with a count of 0.
Definition koliseo.h:226
int kls_block_while_has_temp
If set to 1, make the Koliseo reject push calls while it has an open Koliseo_Temp.
Definition koliseo.h:225
int kls_verbose_lvl
If > 0, makes the Koliseo try to acquire kls_log_fp from kls_log_filepath.
Definition koliseo.h:222
KLS_Err_Handlers err_handlers
Used to pass custom error handlers for push calls.
Definition koliseo.h:228
const char * kls_log_filepath
String representing the path to the Koliseo logfile.
Definition koliseo.h:224
FILE * kls_log_fp
FILE pointer used by the Koliseo to print its kls_log() output.
Definition koliseo.h:223
Defines the handlers used for errors in push calls.
Definition koliseo.h:162
KLS_OOM_Handler * OOM_handler
Pointer to handler for Out-Of-Memory errors in push calls.
Definition koliseo.h:163
KLS_ZEROCOUNT_Handler * ZEROCOUNT_handler
Pointer to handler for zero-count errors in push calls.
Definition koliseo.h:165
KLS_PTRDIFF_MAX_Handler * PTRDIFF_MAX_handler
Pointer to handler for count > (PTRDIFF_MAX / size) errors in push calls.
Definition koliseo.h:164
Definition koliseo.h:186
KLS_hook_on_temp_start * on_temp_start_handler
Used to pass custom start handler for kls_temp_start calls.
Definition koliseo.h:190
KLS_hook_on_free * on_free_handler
Used to pass custom free handler for kls_free calls.
Definition koliseo.h:188
KLS_hook_on_temp_free * on_temp_free_handler
Used to pass custom free handler for kls_temp_end calls.
Definition koliseo.h:191
KLS_hook_on_push * on_push_handler
Used to pass custom push handler for kls_push calls.
Definition koliseo.h:189
KLS_hook_on_new * on_new_handler
Used to pass custom new handler for kls_new_alloc calls.
Definition koliseo.h:187
KLS_hook_on_temp_push * on_temp_push_handler
Used to pass custom push handler for kls_temp_push calls.
Definition koliseo.h:192
Defines a stat struct for Koliseo.
Definition koliseo.h:241
int tot_pops
Total POP calls done.
Definition koliseo.h:244
double worst_pushcall_time
Longest time taken by a PUSH call.
Definition koliseo.h:249
int tot_temp_pushes
Total PUSH_T calls done.
Definition koliseo.h:243
int tot_pushes
Total PUSH calls done.
Definition koliseo.h:242
int tot_hiccups
Total hiccups encountered.
Definition koliseo.h:247
int tot_temp_pops
Total POP_T calls done.
Definition koliseo.h:245
Represents a savestate for a Koliseo.
Definition koliseo.h:366
ptrdiff_t offset
Current position of memory pointer.
Definition koliseo.h:368
ptrdiff_t prev_offset
Previous position of memory pointer.
Definition koliseo.h:369
Koliseo * kls
Reference to the actual Koliseo we're saving.
Definition koliseo.h:367
Represents the initialised arena allocator struct.
Definition koliseo.h:327
ptrdiff_t size
Size of data field.
Definition koliseo.h:329
ptrdiff_t offset
Current position of memory pointer.
Definition koliseo.h:330
ptrdiff_t prev_offset
Previous position of memory pointer.
Definition koliseo.h:331
KLS_Conf conf
Contains flags to change the Koliseo behaviour.
Definition koliseo.h:333
void * extension_data[KLS_MAX_EXTENSIONS]
Points to data for extensions.
Definition koliseo.h:337
struct Koliseo_Temp * t_kls
Points to related active Kolieo_Temp, when has_temp == 1.
Definition koliseo.h:335
char * data
Points to data field.
Definition koliseo.h:328
size_t hooks_len
Length for hooks and extension_data.
Definition koliseo.h:338
struct Koliseo * next
Points to the next Koliseo when conf.kls_growable == 1.
Definition koliseo.h:340
int has_temp
When == 1, a Koliseo_Temp is currently active on this Koliseo.
Definition koliseo.h:332
KLS_Stats stats
Contains stats for Koliseo performance analysis.
Definition koliseo.h:334
KLS_Hooks hooks[KLS_MAX_EXTENSIONS]
Contains handlers for extensions.
Definition koliseo.h:336
kls_free_func * free_func
Points to the free function for the arena's backing memory.
Definition koliseo.h:339