koliseo 0.5.2
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 2
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.2";
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
162typedef struct KLS_Err_Handlers {
163 KLS_OOM_Handler* OOM_handler;
164 KLS_PTRDIFF_MAX_Handler* PTRDIFF_MAX_handler;
165 KLS_ZEROCOUNT_Handler* ZEROCOUNT_handler;
166} KLS_Err_Handlers;
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
186typedef struct KLS_Hooks {
187 KLS_hook_on_new* on_new_handler;
188 KLS_hook_on_free* on_free_handler;
189 KLS_hook_on_push* on_push_handler;
190 KLS_hook_on_temp_start* on_temp_start_handler;
191 KLS_hook_on_temp_free* on_temp_free_handler;
192 KLS_hook_on_temp_push* on_temp_push_handler;
193} KLS_Hooks;
194
195#ifndef KLS_DEFAULT_HOOKS
196#define KLS_DEFAULT_HOOKS (KLS_Hooks){0}
197#endif // KLS_DEFAULT_HOOKS
198
199#ifndef KLS_DEFAULT_EXTENSION_DATA
200#define KLS_DEFAULT_EXTENSION_DATA NULL
201#endif // KLS_DEFAULT_EXTENSION_DATA
202
207typedef struct KLS_Conf {
208 int kls_collect_stats;
209 int kls_verbose_lvl;
210 FILE *kls_log_fp;
211 const char *kls_log_filepath;
212 int kls_block_while_has_temp;
213 int kls_allow_zerocount_push;
214 int kls_growable;
215 KLS_Err_Handlers err_handlers;
216} KLS_Conf;
217
218KLS_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);
219
220KLS_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);
221
222void kls_dbg_features(void);
223
228typedef struct KLS_Stats {
229 int tot_pushes;
230 int tot_temp_pushes;
231 int tot_pops;
232 int tot_temp_pops;
233 int tot_logcalls;
234 int tot_hiccups;
235#ifdef KLS_DEBUG_CORE
236 double worst_pushcall_time;
237#endif
238} KLS_Stats;
239
245extern KLS_Conf KLS_DEFAULT_CONF;
246
252extern KLS_Stats KLS_STATS_DEFAULT;
253
258#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 }"
259
264#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)
265
270#ifdef KLS_DEBUG_CORE
271#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 }"
272#else
273#define KLS_Stats_Fmt "KLS_Stats { tot_pushes: %i, tot_pops: %i, tot_temp_pushes: %i, tot_temp_pops: %i, tot_hiccups: %i }"
274#endif // KLS_DEBUG_CORE
275
280#ifdef KLS_DEBUG_CORE
281#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)
282#else
283#define KLS_Stats_Arg(stats) (stats.tot_pushes),(stats.tot_pops),(stats.tot_temp_pushes),(stats.tot_temp_pops),(stats.tot_hiccups)
284#endif // KLS_DEBUG_CORE
285
290#ifndef _WIN32
291#define KLS_Temp_Conf_Fmt "KLS_Temp_Conf {autoset_regions: %i, tkls_reglist_alloc_backend: %i, kls_reglist_kls_size: %li}"
292#else
293#define KLS_Temp_Conf_Fmt "KLS_Temp_Conf {autoset_regions: %i, tkls_reglist_alloc_backend: %i, kls_reglist_kls_size: %lli}"
294#endif
295
300#define KLS_Temp_Conf_Arg(conf) (conf.kls_autoset_regions),(conf.tkls_reglist_alloc_backend),(conf.kls_reglist_kls_size)
301
310typedef struct Koliseo {
311 char *data;
312 ptrdiff_t size;
313 ptrdiff_t offset;
314 ptrdiff_t prev_offset;
315 int has_temp;
316 KLS_Conf conf;
317 KLS_Stats stats;
318 struct Koliseo_Temp *t_kls;
319 KLS_Hooks hooks;
320 void* extension_data;
321 kls_free_func* free_func;
322 struct Koliseo* next;
323} Koliseo;
324
329#ifndef _WIN32
330#define KLSFmt "KLS {begin: %p, curr: %p, size: %li, offset: %li, has_temp: %i}"
331#else
332#define KLSFmt "KLS {begin: %p, curr: %p, size: %lli, offset: %lli, has_temp: %i}"
333#endif
334
339#define KLS_Arg(kls) (void*)(kls),(void*)((kls)+(kls->offset)),(kls->size),(kls->offset),(kls->has_temp)
340
348typedef struct Koliseo_Temp {
349 Koliseo *kls;
350 ptrdiff_t offset;
351 ptrdiff_t prev_offset;
352} Koliseo_Temp;
353
354void kls_log(Koliseo * kls, const char *tag, const char *format, ...);
355ptrdiff_t kls_get_pos(const Koliseo * kls);
356
357#ifndef KOLISEO_HAS_LOCATE
358Koliseo *kls_new_alloc_ext(ptrdiff_t size, kls_alloc_func alloc_func, kls_free_func free_func, KLS_Hooks ext_handlers, void* user);
359#else
360Koliseo *kls_new_alloc_ext_dbg(ptrdiff_t size, kls_alloc_func alloc_func, kls_free_func free_func, KLS_Hooks ext_handlers, void* user, Koliseo_Loc loc);
361#define kls_new_alloc_ext(size, alloc_func, free_func, ext_handlers, user) kls_new_alloc_ext_dbg((size), (alloc_func), (free_func), (ext_handlers), (user), KLS_HERE)
362#endif // KOLISEO_HAS_LOCATE
363
364#ifndef KOLISEO_HAS_LOCATE
365Koliseo *kls_new_alloc(ptrdiff_t size, kls_alloc_func alloc_func, kls_free_func free_func);
366#else
367Koliseo *kls_new_alloc_dbg(ptrdiff_t size, kls_alloc_func alloc_func, kls_free_func free_func, Koliseo_Loc loc);
368#define kls_new_alloc(size, alloc_func, free_func) kls_new_alloc_dbg((size), (alloc_func), (free_func), KLS_HERE)
369#endif // KOLISEO_HAS_LOCATE
370
371#ifndef KLS_DEFAULT_ALLOCF
372#define KLS_DEFAULT_ALLOCF malloc
373#endif
374
375#ifndef KLS_DEFAULT_FREEF
376#define KLS_DEFAULT_FREEF free
377#endif
378
379#define kls_new(size) kls_new_alloc((size), KLS_DEFAULT_ALLOCF, KLS_DEFAULT_FREEF)
380//bool kls_set_conf(Koliseo* kls, KLS_Conf conf);
381Koliseo *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);
382Koliseo *kls_new_conf_alloc(ptrdiff_t size, KLS_Conf conf, kls_alloc_func alloc_func, kls_free_func kls_free_func);
383#define kls_new_conf_ext(size, conf, ext_handlers, user) kls_new_conf_alloc_ext((size), (conf), KLS_DEFAULT_ALLOCF, KLS_DEFAULT_FREEF, (ext_handlers), (user))
384#define kls_new_conf(size, conf) kls_new_conf_alloc((size), (conf), KLS_DEFAULT_ALLOCF, KLS_DEFAULT_FREEF)
385
386Koliseo *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);
387Koliseo *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);
388Koliseo *kls_new_traced_ext(ptrdiff_t size, const char *output_path, KLS_Hooks ext_handlers, void* user);
389Koliseo *kls_new_traced_alloc(ptrdiff_t size, const char *output_path, kls_alloc_func alloc_func, kls_free_func free_func);
390#define kls_new_traced(size, output_path) kls_new_traced_alloc((size), (output_path), KLS_DEFAULT_ALLOCF, KLS_DEFAULT_FREEF)
391#define kls_new_traced_handled(size, output_path, err_handlers) kls_new_traced_alloc_handled((size), (output_path), KLS_DEFAULT_ALLOCF, KLS_DEFAULT_FREEF, (err_handlers))
392Koliseo *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);
393Koliseo *kls_new_dbg_alloc_handled(ptrdiff_t size, kls_alloc_func alloc_func, kls_free_func free_func, KLS_Err_Handlers err_handlers);
394Koliseo *kls_new_dbg_ext(ptrdiff_t size, KLS_Hooks ext_handlers, void* user);
395Koliseo *kls_new_dbg_alloc(ptrdiff_t size, kls_alloc_func alloc_func, kls_free_func free_func);
396#define kls_new_dbg(size) kls_new_dbg_alloc((size), KLS_DEFAULT_ALLOCF, KLS_DEFAULT_FREEF)
397#define kls_new_dbg_handled(size, err_handlers) kls_new_dbg_alloc_handled((size), KLS_DEFAULT_ALLOCF, KLS_DEFAULT_FREEF, (err_handlers))
398
399#ifndef KOLISEO_HAS_LOCATE
400int kls__check_available_failable(Koliseo* kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count, const char* caller_name);
401#else
402int kls__check_available_failable_dbg(Koliseo* kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count, const char* caller_name, Koliseo_Loc loc);
403#endif // KOLISEO_HAS_LOCATE
404
409#ifndef KOLISEO_HAS_LOCATE
410#define kls__check_available(kls, size, align, count) do { \
411 int res = kls__check_available_failable((kls), (size), (align), (count), __func__); \
412 if (res != 0) return NULL; \
413} while(0)
414#else
415#define kls__check_available_dbg(kls, size, align, count, loc) do { \
416 int res = kls__check_available_failable_dbg((kls), (size), (align), (count), __func__, (loc)); \
417 if (res != 0) return NULL; \
418} while(0)
419#endif // KOLISEO_HAS_LOCATE
420
421//void* kls_push(Koliseo* kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count);
422
423#ifndef KOLISEO_HAS_LOCATE
424void *kls_push_zero(Koliseo * kls, ptrdiff_t size, ptrdiff_t align,
425 ptrdiff_t count);
426#else
427void *kls_push_zero_dbg(Koliseo * kls, ptrdiff_t size, ptrdiff_t align,
428 ptrdiff_t count, Koliseo_Loc loc);
429
430#define kls_push_zero(kls, size, align, count) kls_push_zero_dbg((kls), (size), (align), (count), KLS_HERE)
431#endif // KOLISEO_HAS_LOCATE
432
433#ifndef KOLISEO_HAS_LOCATE
434void *kls_push_zero_ext(Koliseo * kls, ptrdiff_t size, ptrdiff_t align,
435 ptrdiff_t count);
436#else
437void *kls_push_zero_ext_dbg(Koliseo * kls, ptrdiff_t size, ptrdiff_t align,
438 ptrdiff_t count, Koliseo_Loc loc);
439#define kls_push_zero_ext(kls, size, align, count) kls_push_zero_ext_dbg((kls), (size), (align), (count), KLS_HERE)
440#endif // KOLISEO_HAS_LOCATE
441
445#define KLS_PUSH_ARR(kls, type, count) (type*)kls_push_zero_ext((kls), sizeof(type), _Alignof(type), (count))
446
451#define KLS_PUSH_STR(kls, cstr) KLS_PUSH_ARR((kls), char, strlen((cstr))+1)
452
456#define KLS_PUSH_ARR_NAMED(kls, type, count, name, desc) KLS_PUSH_ARR((kls),type,(count))
457
461#define KLS_PUSH_STR_NAMED(kls, cstr, name, desc) KLS_PUSH_ARR_NAMED((kls), char, strlen((cstr))+1, (name), (desc))
462
466#define KLS_PUSH_ARR_TYPED(kls, type, count, region_type, name, desc) KLS_PUSH_ARR((kls),type,(count))
467
471#define KLS_PUSH_STR_TYPED(kls, cstr, region_type, name, desc) KLS_PUSH_ARR_TYPED((kls), char, strlen((cstr))+1, (region_type), (name), (desc))
472
476#define KLS_PUSH(kls, type) KLS_PUSH_ARR((kls), type, 1)
477
481#define KLS_PUSH_NAMED(kls, type, name, desc) KLS_PUSH_ARR_NAMED((kls), type, 1, (name), (desc))
482
487#define KLS_PUSH_EX(kls, type, name) KLS_PUSH_NAMED((kls), type, (name), STRINGIFY(type))
488
492#define KLS_PUSH_TYPED(kls, type, region_type, name, desc) KLS_PUSH_ARR_TYPED((kls), type, 1, (region_type), (name), (desc))
493
498#define KLS_PUSH_TYPED_EX(kls, type, region_type, name) KLS_PUSH_TYPED((kls), type, (region_type), (name), STRINGIFY(type))
499
500void kls_clear(Koliseo * kls);
501void kls_free(Koliseo * kls);
502void print_kls_2file(FILE * fp, const Koliseo * kls);
503void print_dbg_kls(const Koliseo * kls);
504void kls_formatSize(ptrdiff_t size, char *outputBuffer, size_t bufferSize);
505
506#ifndef KOLISEO_HAS_LOCATE
507Koliseo_Temp *kls_temp_start(Koliseo * kls);
508#else
509Koliseo_Temp *kls_temp_start_dbg(Koliseo * kls, Koliseo_Loc loc);
510#define kls_temp_start(kls) kls_temp_start_dbg((kls), KLS_HERE)
511#endif // KOLISEO_HAS_LOCATE
512//bool kls_temp_set_conf(Koliseo_Temp* t_kls, KLS_Temp_Conf conf);
513void kls_temp_end(Koliseo_Temp * tmp_kls);
514
515#ifndef KOLISEO_HAS_LOCATE
516void *kls_temp_push_zero_ext(Koliseo_Temp * t_kls, ptrdiff_t size,
517 ptrdiff_t align, ptrdiff_t count);
518#else
519void *kls_temp_push_zero_ext_dbg(Koliseo_Temp * t_kls, ptrdiff_t size,
520 ptrdiff_t align, ptrdiff_t count, Koliseo_Loc loc);
521#define kls_temp_push_zero_ext(t_kls, size, align, count) kls_temp_push_zero_ext_dbg((t_kls), (size), (align), (count), KLS_HERE)
522#endif // KOLISEO_HAS_LOCATE
523
524void print_temp_kls_2file(FILE * fp, const Koliseo_Temp * t_kls);
525void print_dbg_temp_kls(const Koliseo_Temp * t_kls);
526
530#define KLS_PUSH_ARR_T(kls_temp, type, count) (type*)kls_temp_push_zero_ext((kls_temp), sizeof(type), _Alignof(type), (count))
531
536#define KLS_PUSH_STR_T(kls_temp, cstr) KLS_PUSH_ARR_T((kls_temp), char, strlen((cstr))+1)
537
541#define KLS_PUSH_ARR_T_NAMED(kls_temp, type, count, name, desc) KLS_PUSH_ARR_T((kls_temp),type,(count))
542
546#define KLS_PUSH_STR_T_NAMED(kls_temp, cstr, name, desc) KLS_PUSH_ARR_T_NAMED((kls_temp), char, strlen((cstr))+1, (name), (desc))
547
551#define KLS_PUSH_ARR_T_TYPED(kls_temp, type, count, region_type, name, desc) KLS_PUSH_ARR_T((kls_temp),type,(count))
552
556#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))
557
561#define KLS_PUSH_T(kls_temp, type) KLS_PUSH_ARR_T((kls_temp), type, 1)
562
566#define KLS_PUSH_T_NAMED(kls_temp, type, name, desc) KLS_PUSH_ARR_T_NAMED((kls_temp), type, 1, (name), (desc))
567
572#define KLS_PUSH_T_EX(kls_temp, type, name) KLS_PUSH_T_NAMED((kls_temp), type, (name), STRINGIFY(type))
573
577#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))
578
583#define KLS_PUSH_T_TYPED_EX(kls_temp, type, region_type, name) KLS_PUSH_T_TYPED((kls_temp), type, (region_type), (name), STRINFIGY(type))
584
585#ifdef KOLISEO_HAS_EXPER
586
587void *kls_pop(Koliseo * kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count);
588void *kls_pop_AR(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count);
589
594#define KLS_POP_ARR(kls, type, count) (type*)kls_pop_AR((kls), sizeof(type), _Alignof(type), (count))
595
600#define KLS_POP_STR(kls, cstr) KLS_POP_ARR((kls), char, strlen((cstr)))
601
606#define KLS_POP(kls, type) KLS_POP_ARR((kls), type, 1)
607
608void *kls_temp_pop(Koliseo_Temp * t_kls, ptrdiff_t size, ptrdiff_t align,
609 ptrdiff_t count);
610void *kls_temp_pop_AR(Koliseo_Temp *t_kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count);
611
616#define KLS_POP_ARR_T(kls_temp, type, count) (type*)kls_temp_pop_AR((kls_temp), sizeof(type), _Alignof(type), (count))
617
622#define KLS_POP_STR_T(kls_temp, cstr) KLS_POP_ARR_T((kls_temp), char, strlen((cstr)))
623
628#define KLS_POP_T(kls_temp, type) KLS_POP_ARR_T((kls_temp), type, 1)
629
630char* kls_strdup(Koliseo* kls, char* source);
631char** kls_strdup_arr(Koliseo* kls, size_t count, char** source);
632
640#define KLS__STRCPY(dest, source) do {\
641 strcpy((dest), (source));\
642} while (0)
643
644/*
645 * Macro to dupe a C string to a passed Koliseo, returns a pointer to the allocated string.
646 * Unsafe, do not use.
647 * @see kls_strdup()
648 */
649#define KLS_STRDUP(kls, source) kls_strdup((kls), (source))
650
651char* kls_t_strdup(Koliseo_Temp* t_kls, char* source);
652char** kls_t_strdup_arr(Koliseo_Temp* t_kls, size_t count, char** source);
653
654/*
655 * Macro to dupe a C string to a passed Koliseo_Temp, returns a pointer to the allocated string.
656 * Unsafe, do not use.
657 * @see kls_t_strdup()
658 */
659#define KLS_STRDUP_T(t_kls, source) kls_t_strdup((t_kls), (source))
660
661#endif // KOLISEO_HAS_EXPER
662
663#else
664#error "This code requires C11 or later.\n _Alignof() is not available"
665#endif // __STDC_VERSION__ && __STDC_VERSION__ >= 201112L //We need C11
666
667#endif //KOLISEO_H_
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:1894
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:1805
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:1626
void print_dbg_temp_kls(const Koliseo_Temp *t_kls)
Prints header fields from the passed Koliseo_Temp pointer, to stderr.
Definition koliseo.c:1507
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:653
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:1468
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:1923
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:532
KLS_Stats KLS_STATS_DEFAULT
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
void kls_temp_end(Koliseo_Temp *tmp_kls)
Ends passed Koliseo_Temp pointer.
Definition koliseo.c:1680
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:1907
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:1332
void print_dbg_kls(const Koliseo *kls)
Prints header fields from the passed Koliseo pointer, to stderr.
Definition koliseo.c:1454
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:1851
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)
Takes a ptrdiff_t size, an allocation function pointer and a free function pointer,...
Definition koliseo.c:587
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:1768
void kls_free(Koliseo *kls)
Calls kls_clear() on the passed Koliseo pointer and the frees the actual Koliseo.
Definition koliseo.c:1561
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_dbg_ext(ptrdiff_t size, KLS_Hooks ext_handlers, void *user)
Takes a ptrdiff_t size.
Definition koliseo.c:636
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:1936
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:621
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:1124
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:419
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:782
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:568
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:1731
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)
Takes a ptrdiff_t size, a KLS_Conf to configure the new Koliseo, an allocation function pointer and a...
Definition koliseo.c:447
void KLS_PTRDIFF_MAX_default_handler__(struct Koliseo *kls, ptrdiff_t size, ptrdiff_t count)
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_alloc_ext(ptrdiff_t size, kls_alloc_func alloc_func, kls_free_func free_func, KLS_Hooks ext_handlers, void *user)
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:1423
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)
Takes a ptrdiff_t size, a filepath for the trace output file, an allocation function pointer and a fr...
Definition koliseo.c:496
void kls_clear(Koliseo *kls)
Resets the offset field for the passed Koliseo pointer.
Definition koliseo.c:1541
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:1216
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:1519
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:476
int int_koliseo_version(void)
Returns the constant int representing current version for Koliseo.
Definition koliseo.c:68
Koliseo * kls_new_traced_ext(ptrdiff_t size, const char *output_path, KLS_Hooks ext_handlers, void *user)
Takes a ptrdiff_t size and a filepath for the trace output file.
Definition koliseo.c:549