From 77aa9a3259ddfd6c64534d63424d5b0fa3010900 Mon Sep 17 00:00:00 2001 From: TazyFoundSoup Date: Fri, 22 May 2026 09:43:59 +1000 Subject: refactor: move benches to include directory --- include/bridges/stdio/stream_bridge.h | 2 +- include/tinyff/bench/bench.h | 80 +++++++++++++++++++++++++++++++++++ include/tinyff/common.h | 2 +- include/tinyff/dbg.h | 6 +-- include/tinyff/result.h | 1 + include/tinyff/stream.h | 5 +-- 6 files changed, 87 insertions(+), 9 deletions(-) create mode 100644 include/tinyff/bench/bench.h (limited to 'include') diff --git a/include/bridges/stdio/stream_bridge.h b/include/bridges/stdio/stream_bridge.h index 219b101..561fa8a 100644 --- a/include/bridges/stdio/stream_bridge.h +++ b/include/bridges/stdio/stream_bridge.h @@ -4,7 +4,7 @@ // Default reads for common types (just FILE for now, memory later) size_t ff_file_read(void *ptr, size_t size, void *user); -size_t ff_file_write(const void *ptr, size_t size, const void *user); +size_t ff_file_write(const void *ptr, size_t size, void *user); // Stream creation helpers ff_stream ff_create_file_stream(FILE *f); \ No newline at end of file diff --git a/include/tinyff/bench/bench.h b/include/tinyff/bench/bench.h new file mode 100644 index 0000000..f623e0f --- /dev/null +++ b/include/tinyff/bench/bench.h @@ -0,0 +1,80 @@ +#ifdef USE_BENCH + +#ifndef TINYFF_BENCH_H_ +#define TINYFF_BENCH_H_ + +#include +#include + +// Forward declarations +typedef struct ff_ctx ff_ctx; +ff_result ff_dprintf(ff_ctx* ctx, const char *msg, ...); + + +#define FF_BENCH_MAX_MARKERS 32 + +typedef struct ff_ctx ff_ctx; + +typedef struct { + const char *label; + clock_t tick; +} ff_bench_marker; + +typedef struct { + const char *label; + clock_t _start; + clock_t _end; + + ff_bench_marker markers[FF_BENCH_MAX_MARKERS]; + int _mcount; +} ff_bench; + +// These need to be inline so they are fast + +static inline void ff_bench_start(ff_bench *b, const char* label) { + b->label = label; + b->_start = clock(); + b->_end = 0; + b->_mcount = 0; + +} + + +static inline void ff_bench_end(ff_bench *b) { + b->_end = clock(); +} + +static inline ff_result ff_bench_mark(ff_bench *b, const char *label) { + if (b->_mcount >= FF_BENCH_MAX_MARKERS) return FF_RESULT_ERROR_OUT_OF_BOUNDS; + + ff_bench_marker m; + m.label = label; + m.tick = clock(); + + b->markers[b->_mcount] = m; + b->_mcount++; + + return FF_RESULT_OK; +} + +static inline double ff_bench_seconds(const ff_bench *b) { + return (double)(b->_end - b->_start) / CLOCKS_PER_SEC; +} + +static inline void ff_bench_print(ff_ctx* ctx, const ff_bench *b) +{ + double total_ms = (double)(b->_end - b->_start) * 1000.0 / CLOCKS_PER_SEC; + + ff_dprintf(ctx, "== bench: %s ==\n", b->label); + + for (int i = 0; i < b->_mcount; i++) { + double ms = (double)(b->markers[i].tick - b->_start) * 1000.0 / CLOCKS_PER_SEC; + ff_dprintf(ctx, " [%s] +%.3f ms\n", b->markers[i].label, ms); + } + + ff_dprintf(ctx, "total: %.3f ms\n", total_ms); +} + + +#endif // TINYFF_BENCH_H +#endif // USE_BENCH \ No newline at end of file diff --git a/include/tinyff/common.h b/include/tinyff/common.h index 1efe930..d4215ed 100644 --- a/include/tinyff/common.h +++ b/include/tinyff/common.h @@ -10,7 +10,7 @@ #ifdef USE_BENCH -#include +#include #define FF_BENCH_MARK(ctx, label) \ do { \ diff --git a/include/tinyff/dbg.h b/include/tinyff/dbg.h index 3fac931..379e6ce 100644 --- a/include/tinyff/dbg.h +++ b/include/tinyff/dbg.h @@ -14,13 +14,13 @@ #include #include +#include + // Sets the debug stream. If stream is NULL, debug is disabled. ff_result ff_set_debug_stream(ff_stream* stream, ff_ctx* ctx); // Prints a string to the debug stream if debug is enabled. -// Note: I removed stdarg. That crap nearly made me kill myself. -// 0/10, would not recommend. Jokes on you though, I don't have a soul to kill. -ff_result ff_dprintf(ff_ctx* ctx, const char *msg); +ff_result ff_dprintf(ff_ctx* ctx, const char *msg, ...); #endif \ No newline at end of file diff --git a/include/tinyff/result.h b/include/tinyff/result.h index f15af1b..f167277 100644 --- a/include/tinyff/result.h +++ b/include/tinyff/result.h @@ -24,6 +24,7 @@ typedef enum { // Generic FF_RESULT_ERROR_OUT_OF_BOUNDS, + FF_RESULT_ERROR_NULL_PTR, // Media specific diff --git a/include/tinyff/stream.h b/include/tinyff/stream.h index 95c5884..88cea69 100644 --- a/include/tinyff/stream.h +++ b/include/tinyff/stream.h @@ -9,10 +9,7 @@ typedef size_t (*ff_read_cb)(void *ptr, size_t size, void *user); -typedef size_t (*ff_write_cb)(const void *ptr, size_t size, const void *user); -// ^ -// | -// there clang, happy now? +typedef size_t (*ff_write_cb)(const void *ptr, size_t size, void *user); typedef struct { ff_read_cb read; -- cgit v1.2.3