Lines Matching refs:cbuf
20 #define INC_POINTER(cbuf, ptr, inc) \ argument
21 modpow2(((ptr) + (inc)), (cbuf)->len_pow2)
23 void cbuf_initialize(cbuf_t *cbuf, size_t len) { in cbuf_initialize() argument
24 cbuf_initialize_etc(cbuf, len, malloc(len)); in cbuf_initialize()
27 void cbuf_initialize_etc(cbuf_t *cbuf, size_t len, void *buf) { in cbuf_initialize_etc() argument
28 DEBUG_ASSERT(cbuf); in cbuf_initialize_etc()
32 cbuf->head = 0; in cbuf_initialize_etc()
33 cbuf->tail = 0; in cbuf_initialize_etc()
34 cbuf->len_pow2 = log2_uint(len); in cbuf_initialize_etc()
35 cbuf->buf = buf; in cbuf_initialize_etc()
36 event_init(&cbuf->event, false, 0); in cbuf_initialize_etc()
37 spin_lock_init(&cbuf->lock); in cbuf_initialize_etc()
39 LTRACEF("len %zd, len_pow2 %u\n", len, cbuf->len_pow2); in cbuf_initialize_etc()
42 size_t cbuf_space_avail(cbuf_t *cbuf) { in cbuf_space_avail() argument
43 uint consumed = modpow2((uint)(cbuf->head - cbuf->tail), cbuf->len_pow2); in cbuf_space_avail()
44 return valpow2(cbuf->len_pow2) - consumed - 1; in cbuf_space_avail()
47 size_t cbuf_space_used(cbuf_t *cbuf) { in cbuf_space_used() argument
48 return modpow2((uint)(cbuf->head - cbuf->tail), cbuf->len_pow2); in cbuf_space_used()
51 size_t cbuf_write(cbuf_t *cbuf, const void *_buf, size_t len, bool canreschedule) { in cbuf_write() argument
56 DEBUG_ASSERT(cbuf); in cbuf_write()
57 DEBUG_ASSERT(len < valpow2(cbuf->len_pow2)); in cbuf_write()
60 spin_lock_irqsave(&cbuf->lock, state); in cbuf_write()
65 while (pos < len && cbuf_space_avail(cbuf) > 0) { in cbuf_write()
66 if (cbuf->head >= cbuf->tail) { in cbuf_write()
67 if (cbuf->tail == 0) { in cbuf_write()
72 MIN(valpow2(cbuf->len_pow2) - cbuf->head - 1, len - pos); in cbuf_write()
76 MIN(valpow2(cbuf->len_pow2) - cbuf->head, len - pos); in cbuf_write()
80 write_len = MIN(cbuf->tail - cbuf->head - 1, len - pos); in cbuf_write()
89 memset(cbuf->buf + cbuf->head, 0, write_len); in cbuf_write()
91 memcpy(cbuf->buf + cbuf->head, buf + pos, write_len); in cbuf_write()
94 cbuf->head = INC_POINTER(cbuf, cbuf->head, write_len); in cbuf_write()
98 if (cbuf->head != cbuf->tail) in cbuf_write()
99 event_signal(&cbuf->event, false); in cbuf_write()
101 spin_unlock_irqrestore(&cbuf->lock, state); in cbuf_write()
110 size_t cbuf_read(cbuf_t *cbuf, void *_buf, size_t buflen, bool block) { in cbuf_read() argument
113 DEBUG_ASSERT(cbuf); in cbuf_read()
119 event_wait(&cbuf->event); in cbuf_read()
122 spin_lock_irqsave(&cbuf->lock, state); in cbuf_read()
126 if (cbuf->tail != cbuf->head) { in cbuf_read()
131 while (pos < buflen && cbuf->tail != cbuf->head) { in cbuf_read()
133 if (cbuf->head > cbuf->tail) { in cbuf_read()
135 read_len = MIN(cbuf->head - cbuf->tail, buflen - pos); in cbuf_read()
138 read_len = MIN(valpow2(cbuf->len_pow2) - cbuf->tail, buflen - pos); in cbuf_read()
143 memcpy(buf + pos, cbuf->buf + cbuf->tail, read_len); in cbuf_read()
146 cbuf->tail = INC_POINTER(cbuf, cbuf->tail, read_len); in cbuf_read()
150 if (cbuf->tail == cbuf->head) { in cbuf_read()
153 event_unsignal(&cbuf->event); in cbuf_read()
159 spin_unlock_irqrestore(&cbuf->lock, state); in cbuf_read()
168 size_t cbuf_peek(cbuf_t *cbuf, iovec_t *regions) { in cbuf_peek() argument
169 DEBUG_ASSERT(cbuf && regions); in cbuf_peek()
172 spin_lock_irqsave(&cbuf->lock, state); in cbuf_peek()
174 size_t ret = cbuf_space_used(cbuf); in cbuf_peek()
175 size_t sz = cbuf_size(cbuf); in cbuf_peek()
177 DEBUG_ASSERT(cbuf->tail < sz); in cbuf_peek()
180 regions[0].iov_base = ret ? (cbuf->buf + cbuf->tail) : NULL; in cbuf_peek()
181 if (ret + cbuf->tail > sz) { in cbuf_peek()
182 regions[0].iov_len = sz - cbuf->tail; in cbuf_peek()
183 regions[1].iov_base = cbuf->buf; in cbuf_peek()
191 spin_unlock_irqrestore(&cbuf->lock, state); in cbuf_peek()
195 size_t cbuf_write_char(cbuf_t *cbuf, char c, bool canreschedule) { in cbuf_write_char() argument
196 DEBUG_ASSERT(cbuf); in cbuf_write_char()
199 spin_lock_irqsave(&cbuf->lock, state); in cbuf_write_char()
202 if (cbuf_space_avail(cbuf) > 0) { in cbuf_write_char()
203 cbuf->buf[cbuf->head] = c; in cbuf_write_char()
205 cbuf->head = INC_POINTER(cbuf, cbuf->head, 1); in cbuf_write_char()
208 if (cbuf->head != cbuf->tail) in cbuf_write_char()
209 event_signal(&cbuf->event, canreschedule); in cbuf_write_char()
212 spin_unlock_irqrestore(&cbuf->lock, state); in cbuf_write_char()
217 size_t cbuf_read_char(cbuf_t *cbuf, char *c, bool block) { in cbuf_read_char() argument
218 DEBUG_ASSERT(cbuf); in cbuf_read_char()
223 event_wait(&cbuf->event); in cbuf_read_char()
226 spin_lock_irqsave(&cbuf->lock, state); in cbuf_read_char()
230 if (cbuf->tail != cbuf->head) { in cbuf_read_char()
232 *c = cbuf->buf[cbuf->tail]; in cbuf_read_char()
233 cbuf->tail = INC_POINTER(cbuf, cbuf->tail, 1); in cbuf_read_char()
235 if (cbuf->tail == cbuf->head) { in cbuf_read_char()
237 event_unsignal(&cbuf->event); in cbuf_read_char()
243 spin_unlock_irqrestore(&cbuf->lock, state); in cbuf_read_char()