From e149c878682306f183bdc4c827167a7f97f29d03 Mon Sep 17 00:00:00 2001 From: falsycat Date: Mon, 12 Feb 2024 23:13:46 +0900 Subject: [PATCH] add functions to write chunks --- allcing.h | 124 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 92 insertions(+), 32 deletions(-) diff --git a/allcing.h b/allcing.h index b4746e5..0ddf284 100644 --- a/allcing.h +++ b/allcing.h @@ -5,8 +5,62 @@ #include #include #include +#include +// ---- macro utilities +#define ACG_UTIL_STR(v) #v +#define ACG_UTIL_STR2(v) ACG_UTIL_STR(v) + + +// ---- variable points +#if !defined(ACG_NOW) +# define ACG_NOW ((uint64_t) (clock()/CLOCKS_PER_SEC*10000000U)) +#endif +#if !defined(ACG_TID) +# define ACG_TID (0U) // you should replace ACG_TID to log an actual TID +#endif +#if !defined(ACG_LOC_FULL) +# define ACG_LOC_FULL (__FILE__ ":" ACG_UTIL_STR2(__func__) ":" ACG_UTIL_STR2(__LINE__)) +#endif +#if !defined(ACG_LOC_SHORT) +# define ACG_LOC_SHORT ACG_UTIL_STR2(__LINE__) +#endif +#if !defined(ACG_FLUSH) +# define ACG_FLUSH() (true) +#endif + + +// ---- sugar syntax for logging +#define ACG_BLOB_LITERAL(s, data) ( \ + acg_stream_write_blob((s), sizeof((data)), (const uint8_t*) (data)) && \ + ACG_FLUSH((s)) \ + ) +#define ACG_BLOB(s, size, data) ( \ + acg_stream_write_blob((s), (size), (const uint8_t*) (data)) && \ + ACG_FLUSH((s)) \ + ) +#define ACG_BEGIN(s) ( \ + acg_stream_write_chunk_begin((s)) && \ + acg_stream_write_chunk_loc((s), sizeof(ACG_LOC_FULL)-1U, (const uint8_t*) ACG_LOC_FULL) && \ + acg_stream_write_chunk_ctx((s), ACG_NOW, ACG_TID) && \ + ACG_FLUSH((s)) \ + ) +#define ACG_END(s) ( \ + acg_stream_write_chunk_end((s)) && \ + acg_stream_write_chunk_loc((s), sizeof(ACG_LOC_FULL)-1U,(const uint8_t*) ACG_LOC_FULL) && \ + acg_stream_write_chunk_ctx((s), ACG_NOW, ACG_TID) && \ + ACG_FLUSH((s)) \ + ) +#define ACG_CHECK(s) ( \ + acg_stream_write_chunk_check((s)) && \ + acg_stream_write_chunk_loc((s), sizeof(ACG_LOC_SHORT)-1U,(const uint8_t*) ACG_LOC_SHORT) && \ + acg_stream_write_chunk_ctx((s), ACG_NOW, ACG_TID) && \ + ACG_FLUSH((s)) \ + ) + + +// ---- constants for binary format #define ACG_CHUNK_PAD 0x00U #define ACG_CHUNK_BLOB 0x01U #define ACG_CHUNK_CTX 0x02U @@ -18,6 +72,7 @@ #define ACG_UNITBITS_CHUNK_ID 4U +// ---- utility functions static inline uint64_t acg_util_fullbits(uint8_t bits) { return (((uint64_t) 1U) << bits) - 1U; } @@ -26,11 +81,12 @@ static inline uint64_t acg_util_umin(uint64_t a, uint64_t b) { } +// ---- stream type definition and its functions struct acg_stream { uint8_t* buffer; uint64_t size_bytes; - uint64_t cursor_bits; + void* udata; }; static inline bool acg_stream_write_pad(struct acg_stream* s) { @@ -97,26 +153,9 @@ static inline bool acg_stream_write_int(struct acg_stream* s, int64_t v, uint8_t acg_stream_write_uint(s, (uint64_t) -v, unit_bits); } } - -static inline bool acg_stream_write_chunk_pad(struct acg_stream* s) { +static inline bool acg_stream_write_blob(struct acg_stream* s, uint64_t size_bytes, const uint8_t* buf) { assert(NULL != s); - if (s->cursor_bits%8U == 0U) { - return true; - } - if (s->size_bytes <= s->cursor_bits/8U) { - return false; - } - return - acg_stream_write_uint(s, ACG_CHUNK_PAD, ACG_UNITBITS_CHUNK_ID) && - acg_stream_write_pad(s); -} -static inline bool acg_stream_write_chunk_blob(struct acg_stream* s, uint64_t size_bytes, const uint8_t* buf) { - assert(NULL != s); - - if (!acg_stream_write_uint(s, ACG_CHUNK_BLOB, ACG_UNITBITS_CHUNK_ID)) { - return false; - } if (!acg_stream_write_uint(s, size_bytes, 8U)) { return false; } @@ -136,27 +175,48 @@ static inline bool acg_stream_write_chunk_blob(struct acg_stream* s, uint64_t si *c += size_bits; return true; } -static inline bool acg_stream_write_chunk_begin(struct acg_stream* s) { + +static inline bool acg_stream_write_chunk_pad(struct acg_stream* s) { assert(NULL != s); - if (!acg_stream_write_uint(s, ACG_CHUNK_BEGIN, ACG_UNITBITS_CHUNK_ID)) { + if (s->cursor_bits%8U == 0U) { + return true; + } + if (s->size_bytes <= s->cursor_bits/8U) { return false; } - return true; + return + acg_stream_write_uint(s, ACG_CHUNK_PAD, ACG_UNITBITS_CHUNK_ID) && + acg_stream_write_pad(s); +} +static inline bool acg_stream_write_chunk_blob(struct acg_stream* s, uint64_t size_bytes, const uint8_t* buf) { + assert(NULL != s); + return + acg_stream_write_uint(s, ACG_CHUNK_BLOB, ACG_UNITBITS_CHUNK_ID) && + acg_stream_write_blob(s, size_bytes, buf); +} +static inline bool acg_stream_write_chunk_ctx(struct acg_stream* s, uint64_t time, uint64_t tid) { + assert(NULL != s); + return + acg_stream_write_uint(s, ACG_CHUNK_LOC, ACG_UNITBITS_CHUNK_ID) && + acg_stream_write_uint(s, time, 64U) && + acg_stream_write_uint(s, tid, 16U); +} +static inline bool acg_stream_write_chunk_loc(struct acg_stream* s, uint64_t size_bytes, const uint8_t* buf) { + assert(NULL != s); + return + acg_stream_write_uint(s, ACG_CHUNK_LOC, ACG_UNITBITS_CHUNK_ID) && + acg_stream_write_blob(s, size_bytes, buf); +} +static inline bool acg_stream_write_chunk_begin(struct acg_stream* s) { + assert(NULL != s); + return acg_stream_write_uint(s, ACG_CHUNK_BEGIN, ACG_UNITBITS_CHUNK_ID); } static inline bool acg_stream_write_chunk_end(struct acg_stream* s) { assert(NULL != s); - - if (!acg_stream_write_uint(s, ACG_CHUNK_END, ACG_UNITBITS_CHUNK_ID)) { - return false; - } - return true; + return acg_stream_write_uint(s, ACG_CHUNK_END, ACG_UNITBITS_CHUNK_ID); } static inline bool acg_stream_write_chunk_check(struct acg_stream* s) { assert(NULL != s); - - if (!acg_stream_write_uint(s, ACG_CHUNK_CHECK, ACG_UNITBITS_CHUNK_ID)) { - return false; - } - return true; + return acg_stream_write_uint(s, ACG_CHUNK_CHECK, ACG_UNITBITS_CHUNK_ID); }