size_t ringbuf_memset(ringbuf_t dst, int c, size_t len) { const uint8_t *bufend = ringbuf_end(dst); size_t nwritten = 0; size_t count = MIN(len, ringbuf_buffer_size(dst)); int overflow = count > ringbuf_bytes_free(dst); while (nwritten != count) { /* don't copy beyond the end of the buffer */ assert(bufend > dst->head); size_t n = MIN(bufend - dst->head, count - nwritten); memset(dst->head, c, n); dst->head += n; nwritten += n; /* wrap? */ if (dst->head == bufend) dst->head = dst->buf; } if (overflow) { dst->tail = ringbuf_nextp(dst, dst->head); assert(ringbuf_is_full(dst)); } return nwritten; }
size_t ringbuf_memset(ringbuf_t dst, int c, size_t len) { const uint8_t* bufend = ringbuf_end(dst); size_t nwritten = 0; size_t count = LWIP_MIN(len, ringbuf_buffer_size(dst)); int overflow = count > ringbuf_bytes_free(dst); while (nwritten != count) { lwIP_ASSERT(bufend > dst->head); size_t n = LWIP_MIN(bufend - dst->head, count - nwritten); os_memset(dst->head, c, n); dst->head += n; nwritten += n; if (dst->head == bufend) { dst->head = dst->buf; } } if (overflow) { dst->tail = ringbuf_nextp(dst, dst->head); lwIP_ASSERT(ringbuf_is_full(dst)); } return nwritten; }
/* * Given a ring buffer rb and a pointer to a location within its * contiguous buffer, return the a pointer to the next logical * location in the ring buffer. */ static uint8_t * ringbuf_nextp(ringbuf_t rb, const uint8_t *p) { /* * The assert guarantees the expression (++p - rb->buf) is * non-negative; therefore, the modulus operation is safe and * portable. */ assert((p >= rb->buf) && (p < ringbuf_end(rb))); return rb->buf + ((++p - rb->buf) % ringbuf_buffer_size(rb)); }
size_t ringbuf_findchr(const struct ringbuf_t *rb, int c, size_t offset) { const uint8_t *bufend = ringbuf_end(rb); size_t bytes_used = ringbuf_bytes_used(rb); if (offset >= bytes_used) return bytes_used; const uint8_t *start = rb ->buf + (((rb->tail - rb->buf) + offset) % ringbuf_buffer_size(rb)); lwIP_ASSERT0(bufend > start); size_t n = LWIP_MIN(bufend - start, bytes_used - offset); const uint8_t *found = (const uint8_t *)memchr(start, c, n); if (found) return offset + (found - start); else return ringbuf_findchr(rb, c, offset + n); }
static const uint8_t* ringbuf_end(const struct ringbuf_t* rb) { return rb->buf + ringbuf_buffer_size(rb); }
size_t ringbuf_capacity(const struct ringbuf_t* rb) { return ringbuf_buffer_size(rb) - 1; }
static uint8_t* ringbuf_nextp(ringbuf_t rb, const uint8_t* p) { lwIP_ASSERT((p >= rb->buf) && (p < ringbuf_end(rb))); return rb->buf + ((++p - rb->buf) % ringbuf_buffer_size(rb)); }
static const uint8_t* ICACHE_FLASH_ATTR ringbuf_end(const struct ringbuf_t *rb) { return rb->buf + ringbuf_buffer_size(rb); }
size_t ICACHE_FLASH_ATTR ringbuf_capacity(const struct ringbuf_t *rb) { return ringbuf_buffer_size(rb) - 1; }