/** \brief Dump content of a ring buffer * * \param[in] rng Ring buffer * \param[in] dump_msg_fn Function used to dump a message */ void examsgRngDump(exa_ringbuf_t *rng, ExaRingDumpMsgFn dump_msg_fn) { unsigned char *data = (unsigned char *)rng->data; int pRd, pWr; struct examsg_blkhead h; examsg_blktail t; int mc; /* dump general stats */ exalog_trace("ring buffer %p, read %d, write %d, size %" PRIzu, rng, rng->pRd, rng->pWr, rng->size); if (rng->pRd == rng->pWr) { exalog_trace("EMPTY"); return; } /* save content of ring buffer */ pRd = rng->pRd; pWr = rng->pWr; /* iterate over messages */ mc = 0; while (rng->pRd != rng->pWr) { mc++; /* read header */ readwrap(rng, (char *)&h, sizeof(h)); if (dump_msg_fn) dump_msg_fn(mc, data + rng->pRd, h.size); rng->pRd = (rng->pRd + h.size) % rng->size; readwrap(rng, (char*)&t, sizeof(t)); } /* restore buffer */ rng->pRd = pRd; rng->pWr = pWr; }
int main() { int shm_size; { shm_size = (1 << 20); printf("%d read write testing...\n", shm_size); writewrap(shm_size); readwrap(shm_size); } { shm_size = (1 << 20) * 10; printf("%d read write testing...\n", shm_size); writewrap(shm_size); readwrap(shm_size); } { shm_size = (1 << 20) * 100; printf("%d read write testing...\n", shm_size); writewrap(shm_size); readwrap(shm_size); } { shm_size = (1 << 20) * 300; printf("%d read write testing...\n", shm_size); writewrap(shm_size); readwrap(shm_size); } { shm_size = (1 << 20) * 500; printf("%d read write testing...\n", shm_size); writewrap(shm_size); readwrap(shm_size); } printf("finished!\n"); return 0; }
int examsgRngGetVaList(exa_ringbuf_t *rng, va_list ap) { va_list ap2; struct examsg_blkhead h; examsg_blktail t; int r, avail; size_t maxbytes = 0; char *buff; /* sanity checks */ EXA_ASSERT(rng); EXA_ASSERT(rng->magic == EXAMSG_RNG_MAGIC); if (rng->pWr == rng->pRd) return 0; /* no message */ /* compute max read size */ if (rng->pRd < rng->pWr) avail = rng->pWr - rng->pRd; else avail = rng->size - rng->pRd + rng->pWr; /* read header */ r = rng->pRd; EXA_ASSERT(avail > sizeof(h)); readwrap(rng, (char *)&h, sizeof(h)); EXA_ASSERT(h.magic == EXAMSG_HEAD_MAGIC); EXA_ASSERT(h.size > 0); /* enough place? */ EXA_ASSERT(avail >= h.size + sizeof(h) + sizeof(t)); /* save the va list as it is resued later in the function */ va_copy(ap2, ap); /* compute total size of provided buffers */ while (va_arg(ap2, void *) != NULL) maxbytes += va_arg(ap2, size_t); /* Check buffer size if a buffer is provided */ if (h.size > maxbytes) { /* restore read pointer (let buffer in ring) */ rng->pRd = r; return -EMSGSIZE; } /* The number of bytes to available is put in maxbytes */ maxbytes = h.size; /* compute total size of provided buffers */ while ((buff = va_arg(ap, char *)) != NULL) { size_t s = va_arg(ap, size_t); readwrap(rng, buff, s < maxbytes ? s: maxbytes); if (maxbytes <= s) break; maxbytes -= s; } readwrap(rng, (char*)&t, sizeof(t)); EXA_ASSERT(!examsg_blktail_check(&t)); return h.size; }