58 #define DEBUG_PRINTS 0 62 #define DBG_PRINT(...) printf(__VA_ARGS__) 64 #define DBG_PRINT(...) 67 #define BLOCKSIZE_TOTAL(tb) (tb->blocksize + sizeof(tb_block_t)) 80 int res = pthread_mutex_init(&b->
lock, NULL);
90 if (pthread_mutex_destroy(&b->
lock) == 0) {
99 uint16_t bi = (curidx + 1) % tb->
nblocks;
150 if (nblocks == 0 || blocksize == 0) {
161 if (n->
mem == NULL) {
173 if (pthread_mutex_init(&n->
lock, NULL) != 0) {
182 for (i = 0; i < nblocks; i++) {
194 if (b == NULL || (*b) == NULL) {
201 for (i = 0; i < p->
nblocks; i++) {
214 return pthread_mutex_lock(&b->
lock);
219 return pthread_mutex_unlock(&b->
lock);
224 return pthread_mutex_lock(&bl->
lock);
229 return pthread_mutex_unlock(&bl->
lock);
237 if (((*bl)->data->size + ts) > maxsize) {
238 DBG_PRINT(
"No memory for %" PRIu16
" msg, total size: %" PRIu32
", hdr: %" PRIu32
"\n",
239 size, ((*bl)->data->size + ts), (*bl)->data->size);
241 DBG_PRINT(
"Message is bigger than block.\n");
278 uint16_t *msize = (uint16_t *) bl->
write_data;
279 char *p = (
char *) (msize + 1);
280 (*msize) = htons(
size);
294 uint32_t ts = s1 + s2 +
sizeof(ts);
307 uint16_t *msize = (uint16_t *) bl->
write_data;
308 char *p = (
char *) (msize + 1);
310 (*msize) = htons(ts);
316 DBG_PRINT(
"Saved 2-part-msg %" PRIu16
" B, total: %" PRIu32
" B\n", ts, (bl->
data->
size + ts));
331 DBG_PRINT(
"Not enough memory in %" PRIu32
" block for %" PRIu16
" message, total size: %" PRIu32
", header: %" PRIu32
"\n",
348 uint16_t *msize = (uint16_t *) bl->
read_data;
349 (*
data) = (
const void *) (msize + 1);
350 (*size) = ntohs(*msize);
362 for (i = 0; i < tb->
nblocks; i++) {
uint16_t cur_wr_block_idx
int tb_unlock(trap_buffer_t *b)
void tb_first_rd_block(trap_buffer_t *b)
#define BLOCKSIZE_TOTAL(tb)
static tb_block_t * tb_computenext_blockp(trap_buffer_t *tb, uint16_t curidx)
int tb_pushmess2(trap_buffer_t *b, const void *d1, uint16_t s1, const void *d2, uint16_t s2)
uint16_t cur_rd_block_idx
void tb_next_rd_block(trap_buffer_t *tb)
int tb_block_unlock(tb_block_t *bl)
static void _tb_block_clear(tb_block_t *bl)
int tb_lock(trap_buffer_t *b)
static int _tb_block_destroy(tb_block_t *b)
void tb_first_wr_block(trap_buffer_t *b)
static int _tb_pushmess_checksize(trap_buffer_t *b, tb_block_t **bl, uint32_t ts)
int tb_pushmess(trap_buffer_t *b, const void *data, uint16_t size)
void tb_destroy(trap_buffer_t **b)
int tb_block_lock(tb_block_t *bl)
static int _tb_block_init(tb_block_t *b)
void tb_next_wr_block(trap_buffer_t *tb)
void tb_clear_unused(trap_buffer_t *tb)
trap_buffer_t * tb_init(uint16_t nblocks, uint32_t blocksize)
tb_block_t * cur_wr_block
int tb_getmess(trap_buffer_t *b, const void **data, uint16_t *size)
int tb_isblockfree(tb_block_t *b)
tb_block_t * cur_rd_block
struct tb_block_data_s data[0]
TRAP ring buffer data structure.