add functions to write chunks

This commit is contained in:
falsycat 2024-02-12 23:13:46 +09:00
parent 34075fe42d
commit e149c87868
1 changed files with 92 additions and 32 deletions

124
allcing.h
View File

@ -5,8 +5,62 @@
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
// ---- 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);
}