Merge pull request #2066 from cesanta/queue

Refactor queue
This commit is contained in:
Sergey Lyubka authored and GitHub committed 2023-02-15 22:33:07 +00:00
commit 2ece24e077
17 files changed
+246 -212

No files matched your search

+70 -28
View File
@@ -3496,7 +3496,7 @@ void mg_mgr_init(struct mg_mgr *mgr) {
size_t mg_queue_vprintf(struct mg_queue *q, const char *fmt, va_list *ap) {
size_t len = mg_snprintf(NULL, 0, fmt, ap);
char *buf;
if (mg_queue_space(q, &buf) < len + 1) {
if (len == 0 || mg_queue_book(q, &buf, len + 1) < len + 1) {
len = 0; // Nah. Not enough space
} else {
len = mg_vsnprintf((char *)buf, len + 1, fmt, ap);
@@ -3612,43 +3612,85 @@ size_t mg_print_mac(void (*out)(char, void *), void *arg, va_list *ap) {
#endif
// Every message in the queue is prepended by the message length (ML)
// ML is sizeof(size_t) in size
// Tail points to the message data
//
// |------| ML | message1 | ML | message2 |--- free space ---|
// ^ ^ ^ ^
// buf tail head len
size_t mg_queue_space(struct mg_queue *q, char **buf) {
size_t ofs;
if (q->head > 0 && q->tail >= q->head) { // All messages read?
q->head = 0; // Yes. Reset head first
q->tail = 0; // Now reset the tail
#if defined(__GNUC__) || defined(__clang__)
#define MG_MEMORY_BARRIER() __sync_synchronize()
#elif defined(_MSC_VER) && _MSC_VER >= 1700
#define MG_MEMORY_BARRIER() MemoryBarrier()
#elif !defined(MG_MEMORY_BARRIER)
#define MG_MEMORY_BARRIER()
#endif
// Every message in a queue is prepended by a 32-bit message length (ML).
// If ML is 0, then it is the end, and reader must wrap to the beginning.
//
// Queue when q->tail <= q->head:
// |----- free -----| ML | message1 | ML | message2 | ----- free ------|
// ^ ^ ^ ^
// buf tail head len
//
// Queue when q->tail > q->head:
// | ML | message2 |----- free ------| ML | message1 | 0 |---- free ----|
// ^ ^ ^ ^
// buf head tail len
void mg_queue_init(struct mg_queue *q, char *buf, size_t size) {
q->size = size;
q->buf = buf;
q->head = q->tail = 0;
}
static size_t mg_queue_read_len(struct mg_queue *q) {
uint32_t n = 0;
MG_MEMORY_BARRIER();
memcpy(&n, q->buf + q->tail, sizeof(n));
assert(q->tail + n + sizeof(n) <= q->size);
return n;
}
static void mg_queue_write_len(struct mg_queue *q, size_t len) {
uint32_t n = (uint32_t) len;
memcpy(q->buf + q->head, &n, sizeof(n));
MG_MEMORY_BARRIER();
}
size_t mg_queue_book(struct mg_queue *q, char **buf, size_t len) {
size_t space = 0, hs = sizeof(uint32_t) * 2; // *2 is for the 0 marker
if (q->head >= q->tail && q->head + len + hs <= q->size) {
space = q->size - q->head - hs; // There is enough space
} else if (q->head >= q->tail && q->tail > hs) {
mg_queue_write_len(q, 0); // Not enough space ahead
q->head = 0; // Wrap head to the beginning
}
ofs = q->head + sizeof(size_t);
if (buf != NULL) *buf = q->buf + ofs;
return ofs > q->len ? 0 : q->len - ofs;
if (q->head + hs + len < q->tail) space = q->tail - q->head - hs;
if (buf != NULL) *buf = q->buf + q->head + sizeof(uint32_t);
return space;
}
size_t mg_queue_next(struct mg_queue *q, char **buf) {
size_t len = MG_QUEUE_EMPTY;
if (q->tail < q->head) memcpy(&len, &q->buf[q->tail], sizeof(len));
if (buf != NULL) *buf = &q->buf[q->tail + sizeof(len)];
size_t len = 0;
if (q->tail != q->head) {
len = mg_queue_read_len(q);
if (len == 0) { // Zero (head wrapped) ?
q->tail = 0; // Reset tail to the start
if (q->head > q->tail) len = mg_queue_read_len(q); // Read again
}
}
if (buf != NULL) *buf = q->buf + q->tail + sizeof(uint32_t);
assert(q->tail + len <= q->size);
return len;
}
void mg_queue_add(struct mg_queue *q, size_t len) {
size_t head = q->head + len + (size_t) sizeof(head); // New head
if (head <= q->len) { // Have space ?
memcpy(q->buf + q->head, &len, sizeof(len)); // Yes. Store ML
q->head = head; // Advance head
}
assert(len > 0);
mg_queue_write_len(q, len);
assert(q->head + sizeof(uint32_t) * 2 + len <= q->size);
q->head += len + sizeof(uint32_t);
}
void mg_queue_del(struct mg_queue *q) {
size_t len = mg_queue_next(q, NULL), tail = q->tail + len + sizeof(size_t);
if (len != MG_QUEUE_EMPTY) q->tail = tail;
void mg_queue_del(struct mg_queue *q, size_t len) {
q->tail += len + sizeof(uint32_t);
assert(q->tail + sizeof(uint32_t) <= q->size);
}
#ifdef MG_ENABLE_LINES
@@ -3806,7 +3848,7 @@ static uint32_t blk0(union char64long16 *block, int i) {
w = rol(w, 30);
static void mg_sha1_transform(uint32_t state[5],
const unsigned char buffer[64]) {
const unsigned char *buffer) {
uint32_t a, b, c, d, e;
union char64long16 block[1];