Compare commits

...

3 Commits

Author SHA1 Message Date
f0ae9692fc add an example program 2024-02-12 23:14:07 +09:00
d75ef72583 update format descriptions 2024-02-12 23:13:57 +09:00
e149c87868 add functions to write chunks 2024-02-12 23:13:46 +09:00
4 changed files with 168 additions and 33 deletions

View File

@ -43,3 +43,9 @@ if (BUILD_TESTING)
target_link_libraries(allcing_test PRIVATE allcing)
add_test(NAME allcing_test COMMAND $<TARGET_FILE:allcing_test>)
endif()
# ---- example binary
add_executable(allcing_example EXCLUDE_FROM_ALL)
target_sources(allcing_example PRIVATE example.c)
target_link_libraries(allcing_example PRIVATE allcing)

View File

@ -29,7 +29,7 @@ CHUNK_DATA := CHUNK_PAD | CHUNK_BLOB | CHUNK_CTX | CHUNK_LOC | CHUNK_DATA
CHUNK_PAD := <ACG_CHUNK_PAD> PAD
CHUNK_BLOB := <ACG_CHUNK_BLOB> BLOB
CHUNK_CTX := <ACG_CHUNK_CTX> UINT(64) UINT(16)
CHUNK_LOC := <ACG_CHUNK_LOC> UINT(8) BLOB
CHUNK_LOC := <ACG_CHUNK_LOC> BLOB
PAD :=
BLOB :=

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);
}

69
example.c Normal file
View File

@ -0,0 +1,69 @@
// No copyright
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
// ----
#include "allcing.h"
static inline bool acg_flush(struct acg_stream* s) {
assert(NULL != s);
if (s->cursor_bits < 8U) {
return false;
}
FILE** fp = (void*) &s->udata;
if (NULL == *fp) {
*fp = fopen("log.acg", "wb");
if (NULL == *fp) {
abort();
}
}
if (1U != fwrite(s->buffer, s->cursor_bits/8U, 1U, *fp)) {
return false;
}
if (0U != fflush(*fp)) {
return false;
}
return true;
}
#undef ACG_LOC_FULL
#define ACG_LOC_FULL ACG_LOC_SHORT
#undef ACG_FLUSH
#define ACG_FLUSH(s) acg_flush((s))
struct acg_stream logstream = {
.buffer = (uint8_t[1024U]) {0U},
.size_bytes = 1024U,
};
// ----
int collatz(int n) {
ACG_BEGIN(&logstream);
ACG_BLOB(&logstream, sizeof(n), &n);
if (n == 1U) {
ACG_CHECK(&logstream);
ACG_BLOB_LITERAL(&logstream, "it's finished!");
} else if (n%2U) {
ACG_CHECK(&logstream);
n = collatz(3U*n + 1U);
} else {
ACG_CHECK(&logstream);
n = collatz(n / 2U);
}
ACG_END(&logstream);
ACG_BLOB(&logstream, sizeof(n), &n);
return n;
}
int main(int, char**) {
ACG_BEGIN(&logstream);
collatz(100);
ACG_END(&logstream);
}