koliseo 0.4.9
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 0
81
82typedef void*(kls_alloc_func)(size_t);
83
84#define STRINGIFY_2(x) #x
85
86#define STRINGIFY(x) STRINGIFY_2(x)
87
91static const int KOLISEO_API_VERSION_INT =
92 (KLS_MAJOR * 1000000 + KLS_MINOR * 10000 + KLS_PATCH * 100);
94
98static const char KOLISEO_API_VERSION_STRING[] = "0.5.0";
99
103const char *string_koliseo_version(void);
104
108int int_koliseo_version(void);
109
110#define KLS_DEFAULT_SIZE (16*1024)
111
112#ifndef KLS_DEFAULT_ALIGNMENT
113#define KLS_DEFAULT_ALIGNMENT (2*sizeof(void *))
114#endif
115
116struct Koliseo_Temp; //Forward declaration for Koliseo itself
117struct Koliseo; //Forward declaration for KLS_OOM_Handler
118
119#ifndef KOLISEO_HAS_LOCATE
120typedef void(KLS_OOM_Handler)(struct Koliseo* kls, ptrdiff_t available, ptrdiff_t padding, ptrdiff_t size, ptrdiff_t count);
121#else
122typedef void(KLS_OOM_Handler)(struct Koliseo* kls, ptrdiff_t available, ptrdiff_t padding, ptrdiff_t size, ptrdiff_t count, Koliseo_Loc loc);
123#endif
124
125#ifndef KOLISEO_HAS_LOCATE
126typedef void(KLS_PTRDIFF_MAX_Handler)(struct Koliseo* kls, ptrdiff_t size, ptrdiff_t count);
127#else
128typedef void(KLS_PTRDIFF_MAX_Handler)(struct Koliseo* kls, ptrdiff_t size, ptrdiff_t count, Koliseo_Loc loc);
129#endif
130
131#ifndef KOLISEO_HAS_LOCATE
132typedef void(KLS_ZEROCOUNT_Handler)(struct Koliseo* kls, ptrdiff_t available, ptrdiff_t padding, ptrdiff_t size);
133#else
134typedef void(KLS_ZEROCOUNT_Handler)(struct Koliseo* kls, ptrdiff_t available, ptrdiff_t padding, ptrdiff_t size, Koliseo_Loc loc);
135#endif
136
137#ifndef KOLISEO_HAS_LOCATE
138void KLS_OOM_default_handler__(struct Koliseo* kls, ptrdiff_t available, ptrdiff_t padding, ptrdiff_t size, ptrdiff_t count);
139#else
140void KLS_OOM_default_handler_dbg__(struct Koliseo* kls, ptrdiff_t available, ptrdiff_t padding, ptrdiff_t size, ptrdiff_t count, Koliseo_Loc loc);
141#endif
142
143#ifndef KOLISEO_HAS_LOCATE
144void KLS_PTRDIFF_MAX_default_handler__(struct Koliseo* kls, ptrdiff_t size, ptrdiff_t count);
145#else
146void KLS_PTRDIFF_MAX_default_handler_dbg__(struct Koliseo* kls, ptrdiff_t size, ptrdiff_t count, Koliseo_Loc loc);
147#endif
148
149#ifndef KOLISEO_HAS_LOCATE
150void KLS_ZEROCOUNT_default_handler__(struct Koliseo* kls, ptrdiff_t available, ptrdiff_t padding, ptrdiff_t size);
151#else
152void KLS_ZEROCOUNT_default_handler_dbg__(struct Koliseo* kls, ptrdiff_t available, ptrdiff_t padding, ptrdiff_t size, Koliseo_Loc loc);
153#endif
154
159typedef struct KLS_Err_Handlers {
160 KLS_OOM_Handler* OOM_handler;
161 KLS_PTRDIFF_MAX_Handler* PTRDIFF_MAX_handler;
162 KLS_ZEROCOUNT_Handler* ZEROCOUNT_handler;
163} KLS_Err_Handlers;
164
165#ifndef KOLISEO_HAS_LOCATE
166#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__, }
167#else
168#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__, }
169#endif // KOLISEO_HAS_LOCATE
170
171typedef void(KLS_hook_on_new)(struct Koliseo* kls);
172
173typedef void(KLS_hook_on_free)(struct Koliseo* kls);
174
175typedef void(KLS_hook_on_push)(struct Koliseo* kls, ptrdiff_t padding, const char* caller, void* user);
176
177typedef void(KLS_hook_on_temp_start)(struct Koliseo_Temp* t_kls);
178
179typedef void(KLS_hook_on_temp_free)(struct Koliseo_Temp* t_kls);
180
181typedef void(KLS_hook_on_temp_push)(struct Koliseo_Temp* t_kls, ptrdiff_t padding, const char* caller, void* user);
182
183typedef struct KLS_Hooks {
184 KLS_hook_on_new* on_new_handler;
185 KLS_hook_on_free* on_free_handler;
186 KLS_hook_on_push* on_push_handler;
187 KLS_hook_on_temp_start* on_temp_start_handler;
188 KLS_hook_on_temp_free* on_temp_free_handler;
189 KLS_hook_on_temp_push* on_temp_push_handler;
190} KLS_Hooks;
191
192#ifndef KLS_DEFAULT_HOOKS
193#define KLS_DEFAULT_HOOKS (KLS_Hooks){0}
194#endif // KLS_DEFAULT_HOOKS
195
196#ifndef KLS_DEFAULT_EXTENSION_DATA
197#define KLS_DEFAULT_EXTENSION_DATA NULL
198#endif // KLS_DEFAULT_EXTENSION_DATA
199
204typedef struct KLS_Conf {
205 int kls_collect_stats;
206 int kls_verbose_lvl;
207 FILE *kls_log_fp;
208 const char *kls_log_filepath;
209 int kls_block_while_has_temp;
210 int kls_allow_zerocount_push;
211 KLS_Err_Handlers err_handlers;
212} KLS_Conf;
213
214KLS_Conf kls_conf_init_handled(int autoset_regions, int alloc_backend, ptrdiff_t reglist_kls_size, int autoset_temp_regions, int collect_stats, int verbose_lvl, int block_while_has_temp, int allow_zerocount_push, FILE* log_fp, const char* log_filepath, KLS_Err_Handlers err_handlers);
215
216KLS_Conf kls_conf_init(int autoset_regions, int alloc_backend, ptrdiff_t reglist_kls_size, int autoset_temp_regions, int collect_stats, int verbose_lvl, int block_while_has_temp, int allow_zerocount_push, FILE* log_fp, const char* log_filepath);
217
218void kls_dbg_features(void);
219
224typedef struct KLS_Stats {
225 int tot_pushes;
226 int tot_temp_pushes;
227 int tot_pops;
228 int tot_temp_pops;
229 int tot_logcalls;
230 int tot_hiccups;
231#ifdef KLS_DEBUG_CORE
232 double worst_pushcall_time;
233#endif
234} KLS_Stats;
235
241extern KLS_Conf KLS_DEFAULT_CONF;
242
248extern KLS_Stats KLS_STATS_DEFAULT;
249
254#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 }"
255
260#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)
261
266#ifdef KLS_DEBUG_CORE
267#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 }"
268#else
269#define KLS_Stats_Fmt "KLS_Stats { tot_pushes: %i, tot_pops: %i, tot_temp_pushes: %i, tot_temp_pops: %i, tot_hiccups: %i }"
270#endif // KLS_DEBUG_CORE
271
276#ifdef KLS_DEBUG_CORE
277#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)
278#else
279#define KLS_Stats_Arg(stats) (stats.tot_pushes),(stats.tot_pops),(stats.tot_temp_pushes),(stats.tot_temp_pops),(stats.tot_hiccups)
280#endif // KLS_DEBUG_CORE
281
286#ifndef _WIN32
287#define KLS_Temp_Conf_Fmt "KLS_Temp_Conf {autoset_regions: %i, tkls_reglist_alloc_backend: %i, kls_reglist_kls_size: %li}"
288#else
289#define KLS_Temp_Conf_Fmt "KLS_Temp_Conf {autoset_regions: %i, tkls_reglist_alloc_backend: %i, kls_reglist_kls_size: %lli}"
290#endif
291
296#define KLS_Temp_Conf_Arg(conf) (conf.kls_autoset_regions),(conf.tkls_reglist_alloc_backend),(conf.kls_reglist_kls_size)
297
306typedef struct Koliseo {
307 char *data;
308 ptrdiff_t size;
309 ptrdiff_t offset;
310 ptrdiff_t prev_offset;
311 int has_temp;
312 KLS_Conf conf;
313 KLS_Stats stats;
314 struct Koliseo_Temp *t_kls;
315 KLS_Hooks hooks;
316 void* extension_data;
317} Koliseo;
318
323#ifndef _WIN32
324#define KLSFmt "KLS {begin: %p, curr: %p, size: %li, offset: %li, has_temp: %i}"
325#else
326#define KLSFmt "KLS {begin: %p, curr: %p, size: %lli, offset: %lli, has_temp: %i}"
327#endif
328
333#define KLS_Arg(kls) (void*)(kls),(void*)((kls)+(kls->offset)),(kls->size),(kls->offset),(kls->has_temp)
334
342typedef struct Koliseo_Temp {
343 Koliseo *kls;
344 ptrdiff_t offset;
345 ptrdiff_t prev_offset;
346} Koliseo_Temp;
347
348void kls_log(Koliseo * kls, const char *tag, const char *format, ...);
349ptrdiff_t kls_get_pos(const Koliseo * kls);
350
351#ifndef KOLISEO_HAS_LOCATE
352Koliseo *kls_new_alloc_ext(ptrdiff_t size, kls_alloc_func alloc_func, KLS_Hooks ext_handlers, void* user);
353#else
354Koliseo *kls_new_alloc_ext_dbg(ptrdiff_t size, kls_alloc_func alloc_func, KLS_Hooks ext_handlers, void* user, Koliseo_Loc loc);
355#define kls_new_alloc_ext(size, alloc_func, ext_handlers, user) kls_new_alloc_ext_dbg((size), (alloc_func), (ext_handlers), (user), KLS_HERE)
356#endif // KOLISEO_HAS_LOCATE
357
358#ifndef KOLISEO_HAS_LOCATE
359Koliseo *kls_new_alloc(ptrdiff_t size, kls_alloc_func alloc_func);
360#else
361Koliseo *kls_new_alloc_dbg(ptrdiff_t size, kls_alloc_func alloc_func, Koliseo_Loc loc);
362#define kls_new_alloc(size, alloc_func) kls_new_alloc_dbg((size), (alloc_func), KLS_HERE)
363#endif // KOLISEO_HAS_LOCATE
364
365#ifndef KLS_DEFAULT_ALLOCF
366#define KLS_DEFAULT_ALLOCF malloc
367#endif
368
369#define kls_new(size) kls_new_alloc((size), KLS_DEFAULT_ALLOCF)
370//bool kls_set_conf(Koliseo* kls, KLS_Conf conf);
371Koliseo *kls_new_conf_alloc_ext(ptrdiff_t size, KLS_Conf conf, kls_alloc_func alloc_func, KLS_Hooks ext_handlers, void* user);
372Koliseo *kls_new_conf_alloc(ptrdiff_t size, KLS_Conf conf, kls_alloc_func alloc_func);
373#define kls_new_conf_ext(size, conf, ext_handlers, user) kls_new_conf_alloc_ext((size), (conf), KLS_DEFAULT_ALLOCF, (ext_handlers), (user))
374#define kls_new_conf(size, conf) kls_new_conf_alloc((size), (conf), KLS_DEFAULT_ALLOCF)
375
376Koliseo *kls_new_traced_alloc_handled(ptrdiff_t size, const char *output_path, kls_alloc_func alloc_func, KLS_Err_Handlers err_handlers);
377Koliseo *kls_new_traced_alloc(ptrdiff_t size, const char *output_path, kls_alloc_func alloc_func);
378#define kls_new_traced(size, output_path) kls_new_traced_alloc((size), (output_path), KLS_DEFAULT_ALLOCF)
379#define kls_new_traced_handled(size, output_path, err_handlers) kls_new_traced_alloc_handled((size), (output_path), KLS_DEFAULT_ALLOCF, (err_handlers))
380Koliseo *kls_new_dbg_alloc_handled(ptrdiff_t size, kls_alloc_func alloc_func, KLS_Err_Handlers err_handlers);
381Koliseo *kls_new_dbg_alloc(ptrdiff_t size, kls_alloc_func alloc_func);
382#define kls_new_dbg(size) kls_new_dbg_alloc((size), KLS_DEFAULT_ALLOCF)
383#define kls_new_dbg_handled(size, err_handlers) kls_new_dbg_alloc_handled((size), KLS_DEFAULT_ALLOCF,(err_handlers))
384
385#ifndef KOLISEO_HAS_LOCATE
386int kls__check_available_failable(Koliseo* kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count, const char* caller_name);
387#else
388int kls__check_available_failable_dbg(Koliseo* kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count, const char* caller_name, Koliseo_Loc loc);
389#endif // KOLISEO_HAS_LOCATE
390
395#ifndef KOLISEO_HAS_LOCATE
396#define kls__check_available(kls, size, align, count) do { \
397 int res = kls__check_available_failable((kls), (size), (align), (count), __func__); \
398 if (res != 0) return NULL; \
399} while(0)
400#else
401#define kls__check_available_dbg(kls, size, align, count, loc) do { \
402 int res = kls__check_available_failable_dbg((kls), (size), (align), (count), __func__, (loc)); \
403 if (res != 0) return NULL; \
404} while(0)
405#endif // KOLISEO_HAS_LOCATE
406
407//void* kls_push(Koliseo* kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count);
408
409#ifndef KOLISEO_HAS_LOCATE
410void *kls_push_zero(Koliseo * kls, ptrdiff_t size, ptrdiff_t align,
411 ptrdiff_t count);
412#else
413void *kls_push_zero_dbg(Koliseo * kls, ptrdiff_t size, ptrdiff_t align,
414 ptrdiff_t count, Koliseo_Loc loc);
415
416#define kls_push_zero(kls, size, align, count) kls_push_zero_dbg((kls), (size), (align), (count), KLS_HERE)
417#endif // KOLISEO_HAS_LOCATE
418
419#ifndef KOLISEO_HAS_LOCATE
420void *kls_push_zero_ext(Koliseo * kls, ptrdiff_t size, ptrdiff_t align,
421 ptrdiff_t count);
422#else
423void *kls_push_zero_ext_dbg(Koliseo * kls, ptrdiff_t size, ptrdiff_t align,
424 ptrdiff_t count, Koliseo_Loc loc);
425#define kls_push_zero_ext(kls, size, align, count) kls_push_zero_ext_dbg((kls), (size), (align), (count), KLS_HERE)
426#endif // KOLISEO_HAS_LOCATE
427
431#define KLS_PUSH_ARR(kls, type, count) (type*)kls_push_zero_ext((kls), sizeof(type), _Alignof(type), (count))
432
437#define KLS_PUSH_STR(kls, cstr) KLS_PUSH_ARR((kls), char, strlen((cstr))+1)
438
442#define KLS_PUSH_ARR_NAMED(kls, type, count, name, desc) KLS_PUSH_ARR((kls),type,(count))
443
447#define KLS_PUSH_STR_NAMED(kls, cstr, name, desc) KLS_PUSH_ARR_NAMED((kls), char, strlen((cstr))+1, (name), (desc))
448
452#define KLS_PUSH_ARR_TYPED(kls, type, count, region_type, name, desc) KLS_PUSH_ARR((kls),type,(count))
453
457#define KLS_PUSH_STR_TYPED(kls, cstr, region_type, name, desc) KLS_PUSH_ARR_TYPED((kls), char, strlen((cstr))+1, (region_type), (name), (desc))
458
462#define KLS_PUSH(kls, type) KLS_PUSH_ARR((kls), type, 1)
463
467#define KLS_PUSH_NAMED(kls, type, name, desc) KLS_PUSH_ARR_NAMED((kls), type, 1, (name), (desc))
468
473#define KLS_PUSH_EX(kls, type, name) KLS_PUSH_NAMED((kls), type, (name), STRINGIFY(type))
474
478#define KLS_PUSH_TYPED(kls, type, region_type, name, desc) KLS_PUSH_ARR_TYPED((kls), type, 1, (region_type), (name), (desc))
479
484#define KLS_PUSH_TYPED_EX(kls, type, region_type, name) KLS_PUSH_TYPED((kls), type, (region_type), (name), STRINGIFY(type))
485
486void kls_clear(Koliseo * kls);
487void kls_free(Koliseo * kls);
488void print_kls_2file(FILE * fp, const Koliseo * kls);
489void print_dbg_kls(const Koliseo * kls);
490void kls_formatSize(ptrdiff_t size, char *outputBuffer, size_t bufferSize);
491
492#ifndef KOLISEO_HAS_LOCATE
493Koliseo_Temp *kls_temp_start(Koliseo * kls);
494#else
495Koliseo_Temp *kls_temp_start_dbg(Koliseo * kls, Koliseo_Loc loc);
496#define kls_temp_start(kls) kls_temp_start_dbg((kls), KLS_HERE)
497#endif // KOLISEO_HAS_LOCATE
498//bool kls_temp_set_conf(Koliseo_Temp* t_kls, KLS_Temp_Conf conf);
499void kls_temp_end(Koliseo_Temp * tmp_kls);
500
501#ifndef KOLISEO_HAS_LOCATE
502void *kls_temp_push_zero_ext(Koliseo_Temp * t_kls, ptrdiff_t size,
503 ptrdiff_t align, ptrdiff_t count);
504#else
505void *kls_temp_push_zero_ext_dbg(Koliseo_Temp * t_kls, ptrdiff_t size,
506 ptrdiff_t align, ptrdiff_t count, Koliseo_Loc loc);
507#define kls_temp_push_zero_ext(t_kls, size, align, count) kls_temp_push_zero_ext_dbg((t_kls), (size), (align), (count), KLS_HERE)
508#endif // KOLISEO_HAS_LOCATE
509
510void print_temp_kls_2file(FILE * fp, const Koliseo_Temp * t_kls);
511void print_dbg_temp_kls(const Koliseo_Temp * t_kls);
512
516#define KLS_PUSH_ARR_T(kls_temp, type, count) (type*)kls_temp_push_zero_ext((kls_temp), sizeof(type), _Alignof(type), (count))
517
522#define KLS_PUSH_STR_T(kls_temp, cstr) KLS_PUSH_ARR_T((kls_temp), char, strlen((cstr))+1)
523
527#define KLS_PUSH_ARR_T_NAMED(kls_temp, type, count, name, desc) KLS_PUSH_ARR_T((kls_temp),type,(count))
528
532#define KLS_PUSH_STR_T_NAMED(kls_temp, cstr, name, desc) KLS_PUSH_ARR_T_NAMED((kls_temp), char, strlen((cstr))+1, (name), (desc))
533
537#define KLS_PUSH_ARR_T_TYPED(kls_temp, type, count, region_type, name, desc) KLS_PUSH_ARR_T((kls_temp),type,(count))
538
542#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))
543
547#define KLS_PUSH_T(kls_temp, type) KLS_PUSH_ARR_T((kls_temp), type, 1)
548
552#define KLS_PUSH_T_NAMED(kls_temp, type, name, desc) KLS_PUSH_ARR_T_NAMED((kls_temp), type, 1, (name), (desc))
553
558#define KLS_PUSH_T_EX(kls_temp, type, name) KLS_PUSH_T_NAMED((kls_temp), type, (name), STRINGIFY(type))
559
563#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))
564
569#define KLS_PUSH_T_TYPED_EX(kls_temp, type, region_type, name) KLS_PUSH_T_TYPED((kls_temp), type, (region_type), (name), STRINFIGY(type))
570
571#ifdef KOLISEO_HAS_EXPER
572
573void *kls_pop(Koliseo * kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count);
574void *kls_pop_AR(Koliseo *kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count);
575
580#define KLS_POP_ARR(kls, type, count) (type*)kls_pop_AR((kls), sizeof(type), _Alignof(type), (count))
581
586#define KLS_POP_STR(kls, cstr) KLS_POP_ARR((kls), char, strlen((cstr)))
587
592#define KLS_POP(kls, type) KLS_POP_ARR((kls), type, 1)
593
594void *kls_temp_pop(Koliseo_Temp * t_kls, ptrdiff_t size, ptrdiff_t align,
595 ptrdiff_t count);
596void *kls_temp_pop_AR(Koliseo_Temp *t_kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count);
597
602#define KLS_POP_ARR_T(kls_temp, type, count) (type*)kls_temp_pop_AR((kls_temp), sizeof(type), _Alignof(type), (count))
603
608#define KLS_POP_STR_T(kls_temp, cstr) KLS_POP_ARR_T((kls_temp), char, strlen((cstr)))
609
614#define KLS_POP_T(kls_temp, type) KLS_POP_ARR_T((kls_temp), type, 1)
615
616char* kls_strdup(Koliseo* kls, char* source);
617char** kls_strdup_arr(Koliseo* kls, size_t count, char** source);
618
626#define KLS__STRCPY(dest, source) do {\
627 strcpy((dest), (source));\
628} while (0)
629
630/*
631 * Macro to dupe a C string to a passed Koliseo, returns a pointer to the allocated string.
632 * Unsafe, do not use.
633 * @see kls_strdup()
634 */
635#define KLS_STRDUP(kls, source) kls_strdup((kls), (source))
636
637char* kls_t_strdup(Koliseo_Temp* t_kls, char* source);
638char** kls_t_strdup_arr(Koliseo_Temp* t_kls, size_t count, char** source);
639
640/*
641 * Macro to dupe a C string to a passed Koliseo_Temp, returns a pointer to the allocated string.
642 * Unsafe, do not use.
643 * @see kls_t_strdup()
644 */
645#define KLS_STRDUP_T(t_kls, source) kls_t_strdup((t_kls), (source))
646
647#endif // KOLISEO_HAS_EXPER
648
649#else
650#error "This code requires C11 or later.\n _Alignof() is not available"
651#endif // __STDC_VERSION__ && __STDC_VERSION__ >= 201112L //We need C11
652
653#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:1759
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:1670
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:1503
void print_dbg_temp_kls(const Koliseo_Temp *t_kls)
Prints header fields from the passed Koliseo_Temp pointer, to stderr.
Definition koliseo.c:1395
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:1356
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:1788
KLS_Stats KLS_STATS_DEFAULT
Definition koliseo.c:40
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:292
void kls_temp_end(Koliseo_Temp *tmp_kls)
Ends passed Koliseo_Temp pointer.
Definition koliseo.c:1553
void kls_dbg_features(void)
Prints enabled Koliseo features to stderr.
Definition koliseo.c:228
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:1772
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:1224
void print_dbg_kls(const Koliseo *kls)
Prints header fields from the passed Koliseo pointer, to stderr.
Definition koliseo.c:1342
const char * string_koliseo_version(void)
Returns the constant string representing current version for Koliseo.
Definition koliseo.c:58
Koliseo * kls_new_dbg_alloc(ptrdiff_t size, kls_alloc_func alloc_func)
Takes a ptrdiff_t size and an allocation function pointer, and returns a pointer to the prepared Koli...
Definition koliseo.c:575
ptrdiff_t kls_get_pos(const Koliseo *kls)
Returns the current offset (position of pointer bumper) for the passed Koliseo.
Definition koliseo.c:281
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:1716
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:1633
Koliseo * kls_new_conf_alloc(ptrdiff_t size, KLS_Conf conf, kls_alloc_func alloc_func)
Takes a ptrdiff_t size, a KLS_Conf to configure the new Koliseo, and an allocation function pointer.
Definition koliseo.c:474
void kls_free(Koliseo *kls)
Calls kls_clear() on the passed Koliseo pointer and the frees the actual Koliseo.
Definition koliseo.c:1449
Koliseo * kls_new_traced_alloc(ptrdiff_t size, const char *output_path, kls_alloc_func alloc_func)
Takes a ptrdiff_t size, a filepath for the trace output file, and an allocation function pointer.
Definition koliseo.c:526
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:1801
Koliseo * kls_new_dbg_alloc_handled(ptrdiff_t size, kls_alloc_func alloc_func, KLS_Err_Handlers err_handlers)
Takes a ptrdiff_t size and an allocation function pointer, and returns a pointer to the prepared Koli...
Definition koliseo.c:543
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:1023
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:702
Koliseo * kls_new_alloc(ptrdiff_t size, kls_alloc_func alloc_func)
Takes a ptrdiff_t size and a function pointer to the allocation function.
Definition koliseo.c:419
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:1596
void KLS_PTRDIFF_MAX_default_handler__(struct Koliseo *kls, ptrdiff_t size, ptrdiff_t count)
Definition koliseo.c:102
KLS_Conf KLS_DEFAULT_CONF
Config used by any new Koliseo by default.
Definition koliseo.c:20
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:144
Koliseo * kls_new_conf_alloc_ext(ptrdiff_t size, KLS_Conf conf, kls_alloc_func alloc_func, KLS_Hooks ext_handlers, void *user)
Takes a ptrdiff_t size, a KLS_Conf to configure the new Koliseo, and an allocation function pointer.
Definition koliseo.c:446
Koliseo * kls_new_traced_alloc_handled(ptrdiff_t size, const char *output_path, kls_alloc_func alloc_func, KLS_Err_Handlers err_handlers)
Takes a ptrdiff_t size, a filepath for the trace output file, and an allocation function pointer.
Definition koliseo.c:492
KLS_Conf kls_conf_init(int autoset_regions, int alloc_backend, ptrdiff_t reglist_kls_size, int autoset_temp_regions, int collect_stats, int verbose_lvl, int block_while_has_temp, int allow_zerocount_push, FILE *log_fp, const char *log_filepath)
Used to prepare a KLS_Conf without caring about KOLISEO_HAS_REGIONS.
Definition koliseo.c:219
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:1311
KLS_Conf kls_conf_init_handled(int autoset_regions, int alloc_backend, ptrdiff_t reglist_kls_size, int autoset_temp_regions, int collect_stats, int verbose_lvl, int block_while_has_temp, int allow_zerocount_push, 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:168
Koliseo * kls_new_alloc_ext(ptrdiff_t size, kls_alloc_func alloc_func, KLS_Hooks ext_handlers, void *user)
Takes a ptrdiff_t size and a function pointer to the allocation function.
Definition koliseo.c:338
void kls_clear(Koliseo *kls)
Resets the offset field for the passed Koliseo pointer.
Definition koliseo.c:1429
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:1112
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:1407
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:82
int int_koliseo_version(void)
Returns the constant int representing current version for Koliseo.
Definition koliseo.c:67