koliseo 0.5.4
Loading...
Searching...
No Matches
koliseo.h
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
19#ifndef KOLISEO_H_
20
21#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) //We need C11
22#define KOLISEO_H_
23
24#ifndef _WIN32
25#define _POSIX_C_SOURCE 200809L
26#endif
27
28#include <stdio.h>
29#include <stdint.h>
30#include <stddef.h>
31#include <stdlib.h>
32#include <stdarg.h>
33#include <assert.h>
34#include <string.h>
35#include <time.h>
36#include <stdbool.h>
37
38#ifdef KLS_DEBUG_CORE
39#include <time.h>
40
41/*
42#ifndef KOLISEO_HAS_LOCATE
43#define KOLISEO_HAS_LOCATE // Used for enabling caller location arguments for some APIs.
44#endif // KOLISEO_HAS_LOCATE
45*/
46
47#ifdef _WIN32
48#include <profileapi.h> //Used for QueryPerformanceFrequency(), QueryPerformanceCounter()
49#endif
50#endif //KLS_DEBUG_CORE
51
52#ifdef KOLISEO_HAS_LOCATE
53typedef struct Koliseo_Loc {
54 const char* file;
55 const int line;
56 const char* func;
57} Koliseo_Loc;
58
59#define KLS_HERE (Koliseo_Loc){ \
60 .file = __FILE__, \
61 .line = __LINE__, \
62 .func = __func__, \
63}
64
69#define KLS_Loc_Fmt "[%s:%i at %s():] "
70
75#define KLS_Loc_Arg(loc) (loc.file), (loc.line), (loc.func)
76#endif // KOLISEO_HAS_LOCATE
77
78#define KLS_MAJOR 0
79#define KLS_MINOR 5
80#define KLS_PATCH 4
81
82typedef void*(kls_alloc_func)(size_t);
83typedef void(kls_free_func)(void*);
84
85#define STRINGIFY_2(x) #x
86
87#define STRINGIFY(x) STRINGIFY_2(x)
88
89#define KLS_MAX(a, b) ((a) > (b) ? (a) : (b))
90
94static const int KOLISEO_API_VERSION_INT =
95 (KLS_MAJOR * 1000000 + KLS_MINOR * 10000 + KLS_PATCH * 100);
97
101static const char KOLISEO_API_VERSION_STRING[] = "0.5.4";
102
106const char *string_koliseo_version(void);
107
111int int_koliseo_version(void);
112
113#define KLS_DEFAULT_SIZE (16*1024)
114
115#ifndef KLS_DEFAULT_ALIGNMENT
116#define KLS_DEFAULT_ALIGNMENT (2*sizeof(void *))
117#endif
118
119struct Koliseo_Temp; //Forward declaration for Koliseo itself
120struct Koliseo; //Forward declaration for KLS_OOM_Handler
121
122#ifndef KOLISEO_HAS_LOCATE
123typedef void(KLS_OOM_Handler)(struct Koliseo* kls, ptrdiff_t available, ptrdiff_t padding, ptrdiff_t size, ptrdiff_t count);
124#else
125typedef void(KLS_OOM_Handler)(struct Koliseo* kls, ptrdiff_t available, ptrdiff_t padding, ptrdiff_t size, ptrdiff_t count, Koliseo_Loc loc);
126#endif
127
128#ifndef KOLISEO_HAS_LOCATE
129typedef void(KLS_PTRDIFF_MAX_Handler)(struct Koliseo* kls, ptrdiff_t size, ptrdiff_t count);
130#else
131typedef void(KLS_PTRDIFF_MAX_Handler)(struct Koliseo* kls, ptrdiff_t size, ptrdiff_t count, Koliseo_Loc loc);
132#endif
133
134#ifndef KOLISEO_HAS_LOCATE
135typedef void(KLS_ZEROCOUNT_Handler)(struct Koliseo* kls, ptrdiff_t available, ptrdiff_t padding, ptrdiff_t size);
136#else
137typedef void(KLS_ZEROCOUNT_Handler)(struct Koliseo* kls, ptrdiff_t available, ptrdiff_t padding, ptrdiff_t size, Koliseo_Loc loc);
138#endif
139
140#ifndef KOLISEO_HAS_LOCATE
141void KLS_OOM_default_handler__(struct Koliseo* kls, ptrdiff_t available, ptrdiff_t padding, ptrdiff_t size, ptrdiff_t count);
142#else
143void KLS_OOM_default_handler_dbg__(struct Koliseo* kls, ptrdiff_t available, ptrdiff_t padding, ptrdiff_t size, ptrdiff_t count, Koliseo_Loc loc);
144#endif
145
146#ifndef KOLISEO_HAS_LOCATE
147void KLS_PTRDIFF_MAX_default_handler__(struct Koliseo* kls, ptrdiff_t size, ptrdiff_t count);
148#else
149void KLS_PTRDIFF_MAX_default_handler_dbg__(struct Koliseo* kls, ptrdiff_t size, ptrdiff_t count, Koliseo_Loc loc);
150#endif
151
152#ifndef KOLISEO_HAS_LOCATE
153void KLS_ZEROCOUNT_default_handler__(struct Koliseo* kls, ptrdiff_t available, ptrdiff_t padding, ptrdiff_t size);
154#else
155void KLS_ZEROCOUNT_default_handler_dbg__(struct Koliseo* kls, ptrdiff_t available, ptrdiff_t padding, ptrdiff_t size, Koliseo_Loc loc);
156#endif
157
167
168#ifndef KOLISEO_HAS_LOCATE
169#define KLS_DEFAULT_ERR_HANDLERS (KLS_Err_Handlers) { .OOM_handler = &KLS_OOM_default_handler__, .PTRDIFF_MAX_handler = &KLS_PTRDIFF_MAX_default_handler__, .ZEROCOUNT_handler = &KLS_ZEROCOUNT_default_handler__, }
170#else
171#define KLS_DEFAULT_ERR_HANDLERS (KLS_Err_Handlers) { .OOM_handler = &KLS_OOM_default_handler_dbg__, .PTRDIFF_MAX_handler = &KLS_PTRDIFF_MAX_default_handler_dbg__, .ZEROCOUNT_handler = &KLS_ZEROCOUNT_default_handler_dbg__, }
172#endif // KOLISEO_HAS_LOCATE
173
174typedef void(KLS_hook_on_new)(struct Koliseo* kls);
175
176typedef void(KLS_hook_on_free)(struct Koliseo* kls);
177
178typedef void(KLS_hook_on_push)(struct Koliseo* kls, ptrdiff_t padding, const char* caller, void* user);
179
180typedef void(KLS_hook_on_temp_start)(struct Koliseo_Temp* t_kls);
181
182typedef void(KLS_hook_on_temp_free)(struct Koliseo_Temp* t_kls);
183
184typedef void(KLS_hook_on_temp_push)(struct Koliseo_Temp* t_kls, ptrdiff_t padding, const char* caller, void* user);
185
194
202#ifndef KLS_DEFAULT_HOOKS
203#define KLS_DEFAULT_HOOKS &(KLS_Hooks){0}
204#endif // KLS_DEFAULT_HOOKS
205
212#ifndef KLS_DEFAULT_EXTENSION_DATA
213#define KLS_DEFAULT_EXTENSION_DATA NULL
214#endif // KLS_DEFAULT_EXTENSION_DATA
215
230
231KLS_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);
232
233KLS_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);
234
235void kls_dbg_features(void);
236
241typedef struct KLS_Stats {
248#ifdef KLS_DEBUG_CORE
250#endif
252
259
266
271#define KLS_Conf_Fmt "KLS_Conf { collect_stats: %i, verbose_lvl: %i, log_filepath: \"%s\", log_fp: %p, block_while_has_temp: %i, allow_zerocount_push: %i }"
272
277#define KLS_Conf_Arg(conf) (conf.kls_collect_stats),(conf.kls_verbose_lvl),(conf.kls_log_filepath),(void*)(conf.kls_log_fp),(conf.kls_block_while_has_temp),(conf.kls_allow_zerocount_push)
278
283#ifdef KLS_DEBUG_CORE
284#define KLS_Stats_Fmt "KLS_Stats { tot_pushes: %i, tot_pops: %i, tot_temp_pushes: %i, tot_temp_pops: %i, tot_hiccups: %i, worst_push_time: %.7f }"
285#else
286#define KLS_Stats_Fmt "KLS_Stats { tot_pushes: %i, tot_pops: %i, tot_temp_pushes: %i, tot_temp_pops: %i, tot_hiccups: %i }"
287#endif // KLS_DEBUG_CORE
288
293#ifdef KLS_DEBUG_CORE
294#define KLS_Stats_Arg(stats) (stats.tot_pushes),(stats.tot_pops),(stats.tot_temp_pushes),(stats.tot_temp_pops),(stats.tot_hiccups),(stats.worst_pushcall_time)
295#else
296#define KLS_Stats_Arg(stats) (stats.tot_pushes),(stats.tot_pops),(stats.tot_temp_pushes),(stats.tot_temp_pops),(stats.tot_hiccups)
297#endif // KLS_DEBUG_CORE
298
304#ifndef KLS_MAX_EXTENSIONS
305#define KLS_MAX_EXTENSIONS 1
306#endif // KLS_MAX_EXTENSIONS
307
315#ifndef KLS_DEFAULT_EXTENSIONS_LEN
316#define KLS_DEFAULT_EXTENSIONS_LEN 0
317#endif // KLS_MAX_EXTENSIONS
318
342
347#ifndef _WIN32
348#define KLSFmt "KLS {begin: %p, curr: %p, size: %li, offset: %li, has_temp: %i}"
349#else
350#define KLSFmt "KLS {begin: %p, curr: %p, size: %lli, offset: %lli, has_temp: %i}"
351#endif
352
357#define KLS_Arg(kls) (void*)(kls),(void*)((kls)+(kls->offset)),(kls->size),(kls->offset),(kls->has_temp)
358
366typedef struct Koliseo_Temp {
368 ptrdiff_t offset;
369 ptrdiff_t prev_offset;
371
372void kls_log(Koliseo * kls, const char *tag, const char *format, ...);
373ptrdiff_t kls_get_pos(const Koliseo * kls);
374
375#ifndef KOLISEO_HAS_LOCATE
376Koliseo *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);
377#else
378Koliseo *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);
379#define kls_new_alloc_ext(size, alloc_func, free_func, ext_handlers, user, ext_len) kls_new_alloc_ext_dbg((size), (alloc_func), (free_func), (ext_handlers), (user), (ext_len), KLS_HERE)
380#endif // KOLISEO_HAS_LOCATE
381
382#ifndef KOLISEO_HAS_LOCATE
383Koliseo *kls_new_alloc(ptrdiff_t size, kls_alloc_func alloc_func, kls_free_func free_func);
384#else
385Koliseo *kls_new_alloc_dbg(ptrdiff_t size, kls_alloc_func alloc_func, kls_free_func free_func, Koliseo_Loc loc);
386#define kls_new_alloc(size, alloc_func, free_func) kls_new_alloc_dbg((size), (alloc_func), (free_func), KLS_HERE)
387#endif // KOLISEO_HAS_LOCATE
388
389#ifndef KLS_DEFAULT_ALLOCF
390#define KLS_DEFAULT_ALLOCF malloc
391#endif
392
393#ifndef KLS_DEFAULT_FREEF
394#define KLS_DEFAULT_FREEF free
395#endif
396
397Koliseo* kls_new(ptrdiff_t size);
398//bool kls_set_conf(Koliseo* kls, KLS_Conf conf);
399Koliseo *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);
401Koliseo *kls_new_conf_ext(ptrdiff_t size, KLS_Conf conf, KLS_Hooks* ext_handlers, void** user, size_t ext_len);
402Koliseo *kls_new_conf(ptrdiff_t size, KLS_Conf conf);
403
404Koliseo *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);
405Koliseo *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);
406Koliseo *kls_new_traced_ext(ptrdiff_t size, const char *output_path, KLS_Hooks* ext_handlers, void** user, size_t ext_len);
407Koliseo *kls_new_traced_alloc(ptrdiff_t size, const char *output_path, kls_alloc_func alloc_func, kls_free_func free_func);
408Koliseo *kls_new_traced(ptrdiff_t size, const char* output_path);
409Koliseo *kls_new_traced_handled(ptrdiff_t size, const char* output_path, KLS_Err_Handlers err_handlers);
410
411Koliseo *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);
412Koliseo *kls_new_dbg_alloc_handled(ptrdiff_t size, kls_alloc_func alloc_func, kls_free_func free_func, KLS_Err_Handlers err_handlers);
413Koliseo *kls_new_dbg_ext(ptrdiff_t size, KLS_Hooks* ext_handlers, void** user, size_t ext_len);
414Koliseo *kls_new_dbg_alloc(ptrdiff_t size, kls_alloc_func alloc_func, kls_free_func free_func);
415Koliseo *kls_new_dbg(ptrdiff_t size);
416Koliseo *kls_new_dbg_handled(ptrdiff_t size, KLS_Err_Handlers err_handlers);
417
418#ifndef KOLISEO_HAS_LOCATE
419int kls__check_available_failable(Koliseo* kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count, const char* caller_name);
420#else
421int kls__check_available_failable_dbg(Koliseo* kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count, const char* caller_name, Koliseo_Loc loc);
422#endif // KOLISEO_HAS_LOCATE
423
428#ifndef KOLISEO_HAS_LOCATE
429#define kls__check_available(kls, size, align, count) do { \
430 int res = kls__check_available_failable((kls), (size), (align), (count), __func__); \
431 if (res != 0) return NULL; \
432} while(0)
433#else
434#define kls__check_available_dbg(kls, size, align, count, loc) do { \
435 int res = kls__check_available_failable_dbg((kls), (size), (align), (count), __func__, (loc)); \
436 if (res != 0) return NULL; \
437} while(0)
438#endif // KOLISEO_HAS_LOCATE
439
440//void* kls_push(Koliseo* kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count);
441
442#ifndef KOLISEO_HAS_LOCATE
443void *kls_push_zero(Koliseo * kls, ptrdiff_t size, ptrdiff_t align,
444 ptrdiff_t count);
445#else
446void *kls_push_zero_dbg(Koliseo * kls, ptrdiff_t size, ptrdiff_t align,
447 ptrdiff_t count, Koliseo_Loc loc);
448
449#define kls_push_zero(kls, size, align, count) kls_push_zero_dbg((kls), (size), (align), (count), KLS_HERE)
450#endif // KOLISEO_HAS_LOCATE
451
452#ifndef KOLISEO_HAS_LOCATE
453void *kls_push_zero_ext(Koliseo * kls, ptrdiff_t size, ptrdiff_t align,
454 ptrdiff_t count);
455#else
456void *kls_push_zero_ext_dbg(Koliseo * kls, ptrdiff_t size, ptrdiff_t align,
457 ptrdiff_t count, Koliseo_Loc loc);
458#define kls_push_zero_ext(kls, size, align, count) kls_push_zero_ext_dbg((kls), (size), (align), (count), KLS_HERE)
459#endif // KOLISEO_HAS_LOCATE
460
464#define KLS_PUSH_ARR(kls, type, count) (type*)kls_push_zero_ext((kls), sizeof(type), _Alignof(type), (count))
465
470#define KLS_PUSH_STR(kls, cstr) KLS_PUSH_ARR((kls), char, strlen((cstr))+1)
471
475#define KLS_PUSH_ARR_NAMED(kls, type, count, name, desc) KLS_PUSH_ARR((kls),type,(count))
476
480#define KLS_PUSH_STR_NAMED(kls, cstr, name, desc) KLS_PUSH_ARR_NAMED((kls), char, strlen((cstr))+1, (name), (desc))
481
485#define KLS_PUSH_ARR_TYPED(kls, type, count, region_type, name, desc) KLS_PUSH_ARR((kls),type,(count))
486
490#define KLS_PUSH_STR_TYPED(kls, cstr, region_type, name, desc) KLS_PUSH_ARR_TYPED((kls), char, strlen((cstr))+1, (region_type), (name), (desc))
491
495#define KLS_PUSH(kls, type) KLS_PUSH_ARR((kls), type, 1)
496
500#define KLS_PUSH_NAMED(kls, type, name, desc) KLS_PUSH_ARR_NAMED((kls), type, 1, (name), (desc))
501
506#define KLS_PUSH_EX(kls, type, name) KLS_PUSH_NAMED((kls), type, (name), STRINGIFY(type))
507
511#define KLS_PUSH_TYPED(kls, type, region_type, name, desc) KLS_PUSH_ARR_TYPED((kls), type, 1, (region_type), (name), (desc))
512
517#define KLS_PUSH_TYPED_EX(kls, type, region_type, name) KLS_PUSH_TYPED((kls), type, (region_type), (name), STRINGIFY(type))
518
519void kls_clear(Koliseo * kls);
520void kls_free(Koliseo * kls);
521void print_kls_2file(FILE * fp, const Koliseo * kls);
522void print_dbg_kls(const Koliseo * kls);
523void kls_formatSize(ptrdiff_t size, char *outputBuffer, size_t bufferSize);
524
525#ifndef KOLISEO_HAS_LOCATE
527#else
528Koliseo_Temp *kls_temp_start_dbg(Koliseo * kls, Koliseo_Loc loc);
529#define kls_temp_start(kls) kls_temp_start_dbg((kls), KLS_HERE)
530#endif // KOLISEO_HAS_LOCATE
531//bool kls_temp_set_conf(Koliseo_Temp* t_kls, KLS_Temp_Conf conf);
532void kls_temp_end(Koliseo_Temp * tmp_kls);
533
534#ifndef KOLISEO_HAS_LOCATE
535void *kls_temp_push_zero_ext(Koliseo_Temp * t_kls, ptrdiff_t size,
536 ptrdiff_t align, ptrdiff_t count);
537#else
538void *kls_temp_push_zero_ext_dbg(Koliseo_Temp * t_kls, ptrdiff_t size,
539 ptrdiff_t align, ptrdiff_t count, Koliseo_Loc loc);
540#define kls_temp_push_zero_ext(t_kls, size, align, count) kls_temp_push_zero_ext_dbg((t_kls), (size), (align), (count), KLS_HERE)
541#endif // KOLISEO_HAS_LOCATE
542
543void print_temp_kls_2file(FILE * fp, const Koliseo_Temp * t_kls);
544void print_dbg_temp_kls(const Koliseo_Temp * t_kls);
545
549#define KLS_PUSH_ARR_T(kls_temp, type, count) (type*)kls_temp_push_zero_ext((kls_temp), sizeof(type), _Alignof(type), (count))
550
555#define KLS_PUSH_STR_T(kls_temp, cstr) KLS_PUSH_ARR_T((kls_temp), char, strlen((cstr))+1)
556
560#define KLS_PUSH_ARR_T_NAMED(kls_temp, type, count, name, desc) KLS_PUSH_ARR_T((kls_temp),type,(count))
561
565#define KLS_PUSH_STR_T_NAMED(kls_temp, cstr, name, desc) KLS_PUSH_ARR_T_NAMED((kls_temp), char, strlen((cstr))+1, (name), (desc))
566
570#define KLS_PUSH_ARR_T_TYPED(kls_temp, type, count, region_type, name, desc) KLS_PUSH_ARR_T((kls_temp),type,(count))
571
575#define KLS_PUSH_STR_T_TYPED(kls_temp, cstr, region_type, name, desc) KLS_PUSH_ARR_T_TYPED((kls_temp), char, strlen((cstr))+1, (region_type), (name), (desc))
576
580#define KLS_PUSH_T(kls_temp, type) KLS_PUSH_ARR_T((kls_temp), type, 1)
581
585#define KLS_PUSH_T_NAMED(kls_temp, type, name, desc) KLS_PUSH_ARR_T_NAMED((kls_temp), type, 1, (name), (desc))
586
591#define KLS_PUSH_T_EX(kls_temp, type, name) KLS_PUSH_T_NAMED((kls_temp), type, (name), STRINGIFY(type))
592
596#define KLS_PUSH_T_TYPED(kls_temp, type, region_type, name, desc) KLS_PUSH_ARR_T_TYPED((kls_temp), type, 1, (region_type), (name), (desc))
597
602#define KLS_PUSH_T_TYPED_EX(kls_temp, type, region_type, name) KLS_PUSH_T_TYPED((kls_temp), type, (region_type), (name), STRINFIGY(type))
603
604#ifdef KOLISEO_HAS_EXPER
605
606void *kls_pop(Koliseo * kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count);
607void *kls_pop_AR(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count);
608
613#define KLS_POP_ARR(kls, type, count) (type*)kls_pop_AR((kls), sizeof(type), _Alignof(type), (count))
614
619#define KLS_POP_STR(kls, cstr) KLS_POP_ARR((kls), char, strlen((cstr)))
620
625#define KLS_POP(kls, type) KLS_POP_ARR((kls), type, 1)
626
627void *kls_temp_pop(Koliseo_Temp * t_kls, ptrdiff_t size, ptrdiff_t align,
628 ptrdiff_t count);
629void *kls_temp_pop_AR(Koliseo_Temp *t_kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count);
630
635#define KLS_POP_ARR_T(kls_temp, type, count) (type*)kls_temp_pop_AR((kls_temp), sizeof(type), _Alignof(type), (count))
636
641#define KLS_POP_STR_T(kls_temp, cstr) KLS_POP_ARR_T((kls_temp), char, strlen((cstr)))
642
647#define KLS_POP_T(kls_temp, type) KLS_POP_ARR_T((kls_temp), type, 1)
648
649char* kls_strdup(Koliseo* kls, char* source);
650char** kls_strdup_arr(Koliseo* kls, size_t count, char** source);
651
659#define KLS__STRCPY(dest, source) do {\
660 strcpy((dest), (source));\
661} while (0)
662
663/*
664 * Macro to dupe a C string to a passed Koliseo, returns a pointer to the allocated string.
665 * Unsafe, do not use.
666 * @see kls_strdup()
667 */
668#define KLS_STRDUP(kls, source) kls_strdup((kls), (source))
669
670char* kls_t_strdup(Koliseo_Temp* t_kls, char* source);
671char** kls_t_strdup_arr(Koliseo_Temp* t_kls, size_t count, char** source);
672
673/*
674 * Macro to dupe a C string to a passed Koliseo_Temp, returns a pointer to the allocated string.
675 * Unsafe, do not use.
676 * @see kls_t_strdup()
677 */
678#define KLS_STRDUP_T(t_kls, source) kls_t_strdup((t_kls), (source))
679
680#endif // KOLISEO_HAS_EXPER
681
682#else
683#error "This code requires C11 or later.\n _Alignof() is not available"
684#endif // __STDC_VERSION__ && __STDC_VERSION__ >= 201112L //We need C11
685
686#endif //KOLISEO_H_
KLS_Stats KLS_STATS_DEFAULT
Default KLS_Stats values, used by kls_new().
Definition koliseo.c:41
KLS_Conf KLS_DEFAULT_CONF
Config used by any new Koliseo by default.
Definition koliseo.c:20
void kls_free_func(void *)
Used to select a free function for the arena's backing memory.
Definition koliseo.h:83
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 KLS_hook_on_new(struct Koliseo *kls)
Used to pass an extension handler for kls_new_alloc().
Definition koliseo.h:174
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
void KLS_OOM_default_handler__(struct Koliseo *kls, ptrdiff_t available, ptrdiff_t padding, ptrdiff_t size, ptrdiff_t count)
Used by default when no handler is passed.
Definition koliseo.c:83
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
void KLS_OOM_Handler(struct Koliseo *kls, ptrdiff_t available, ptrdiff_t padding, ptrdiff_t size, ptrdiff_t count)
Used to pass an error handler for Out-Of-Memory error.
Definition koliseo.h:123
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_hook_on_temp_start(struct Koliseo_Temp *t_kls)
Used to pass an extension handler for kls_temp_start().
Definition koliseo.h:180
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
#define KLS_MAX_EXTENSIONS
Defines how many extensions can be handled at once.
Definition koliseo.h:305
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 current koliseo version as a string.
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_ZEROCOUNT_Handler(struct Koliseo *kls, ptrdiff_t available, ptrdiff_t padding, ptrdiff_t size)
Used to pass an error handler for zero-count push call error.
Definition koliseo.h:135
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
#define KLS_MINOR
Represents current minor release.
Definition koliseo.h:79
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_ZEROCOUNT_default_handler__(struct Koliseo *kls, ptrdiff_t available, ptrdiff_t padding, ptrdiff_t size)
Used by default when no handler is passed.
Definition koliseo.c:145
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
#define KLS_PATCH
Represents current patch release.
Definition koliseo.h:80
void * kls_alloc_func(size_t)
Used to select an allocation function for the arena's backing memory.
Definition koliseo.h:82
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
void KLS_hook_on_free(struct Koliseo *kls)
Used to pass an extension handler for kls_free().
Definition koliseo.h:176
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
void KLS_hook_on_temp_push(struct Koliseo_Temp *t_kls, ptrdiff_t padding, const char *caller, void *user)
Used to pass an extension handler for kls_temp_push().
Definition koliseo.h:184
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
Koliseo * kls_new_conf_alloc(ptrdiff_t size, KLS_Conf conf, kls_alloc_func alloc_func, kls_free_func kls_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
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_hook_on_temp_free(struct Koliseo_Temp *t_kls)
Used to pass an extension handler for kls_temp_end().
Definition koliseo.h:182
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
void KLS_hook_on_push(struct Koliseo *kls, ptrdiff_t padding, const char *caller, void *user)
Used to pass an extension handler for kls_push().
Definition koliseo.h:178
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
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_PTRDIFF_MAX_Handler(struct Koliseo *kls, ptrdiff_t size, ptrdiff_t count)
Used to pass an error handler for count > (PTRDIFF_MAX / size) error.
Definition koliseo.h:129
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
#define KLS_MAJOR
Represents current major release.
Definition koliseo.h:78
int int_koliseo_version(void)
Returns current koliseo version as an integer.
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
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_logcalls
Total kls_log() calls done.
Definition koliseo.h:246
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