69 cbuf = ocs_malloc(os,
sizeof(*cbuf), OCS_M_NOWAIT | OCS_M_ZERO);
75 cbuf->entry_count = entry_count;
79 ocs_lock_init(NULL, &cbuf->cbuf_clock,
"cbuf_c:%p", cbuf);
80 ocs_lock_init(NULL, &cbuf->cbuf_plock,
"cbuf_p:%p", cbuf);
81 ocs_sem_init(&cbuf->cbuf_csem, 0,
"cbuf:%p", cbuf);
82 ocs_sem_init(&cbuf->cbuf_psem, cbuf->entry_count,
"cbuf:%p", cbuf);
84 cbuf->array = ocs_malloc(os, entry_count *
sizeof(*cbuf->array), OCS_M_NOWAIT | OCS_M_ZERO);
85 if (cbuf->array == NULL) {
106 if (cbuf->array != NULL) {
107 ocs_free(cbuf->os, cbuf->array,
sizeof(*cbuf->array) * cbuf->entry_count);
109 ocs_lock_free(&cbuf->cbuf_clock);
110 ocs_lock_free(&cbuf->cbuf_plock);
111 ocs_free(cbuf->os, cbuf,
sizeof(*cbuf));
130 if (likely(ocs_sem_p(&cbuf->cbuf_csem, timeout_usec) == 0)) {
131 ocs_lock(&cbuf->cbuf_clock);
132 ret = cbuf->array[cbuf->cidx];
133 if (unlikely(++cbuf->cidx >= cbuf->entry_count)) {
136 ocs_unlock(&cbuf->cbuf_clock);
137 ocs_sem_v(&cbuf->cbuf_psem);
157 if (likely(ocs_sem_p(&cbuf->cbuf_psem, -1) == 0)) {
158 ocs_lock(&cbuf->cbuf_plock);
159 cbuf->array[cbuf->pidx] = elem;
160 if (unlikely(++cbuf->pidx >= cbuf->entry_count)) {
163 ocs_unlock(&cbuf->cbuf_plock);
164 ocs_sem_v(&cbuf->cbuf_csem);
187 for (i = 0; i < count; i++) {
void ocs_cbuf_free(ocs_cbuf_t *cbuf)
Free a circular buffer.
uint32_t ocs_array_get_count(ocs_array_t *array)
Return number of elements in an array.
void * ocs_cbuf_get(ocs_cbuf_t *cbuf, int32_t timeout_usec)
Get pointer to buffer.
int32_t ocs_cbuf_prime(ocs_cbuf_t *cbuf, ocs_array_t *array)
Prime a circular buffer data.
ocs_cbuf_t * ocs_cbuf_alloc(ocs_os_handle_t os, uint32_t entry_count)
Initialize a circular buffer queue.
int32_t ocs_cbuf_put(ocs_cbuf_t *cbuf, void *elem)
write a buffer
void * ocs_array_get(ocs_array_t *array, uint32_t idx)
Return reference to an element of an array object.