-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinfifo.c
53 lines (46 loc) · 2.11 KB
/
linfifo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "linfifo/linfifo.h"
#include "linfifo_os.h"
size_t linfifo_mem_page_size(void) { return linfifo_os_mem_page_size(); }
linfifo_retval_t linfifo_create(size_t len, linfifo_t *out_lf) {
size_t const ps = linfifo_mem_page_size();
if (LINFIFO_UNLIKELY(!out_lf || !len)) { return LINFIFO_RETVAL_ERR_ARG; }
if (LINFIFO_UNLIKELY(((len / ps) * ps) != len)) { return LINFIFO_RETVAL_ERR_ARG; }
if (LINFIFO_UNLIKELY((len & (len - 1)) != 0)) { return LINFIFO_RETVAL_ERR_ARG; }
out_lf->head = out_lf->tail = 0;
out_lf->capacity = len;
out_lf->seat = NULL;
return linfifo_os_mbuf_create(out_lf);
}
linfifo_retval_t linfifo_destroy(linfifo_t *lf) {
if (LINFIFO_UNLIKELY(!lf)) { return LINFIFO_RETVAL_ERR_ARG; }
linfifo_retval_t const rv = linfifo_os_mbuf_free(lf);
lf->head = lf->tail = lf->capacity = 0;
lf->seat = NULL;
return rv;
}
linfifo_retval_t linfifo_put_acquire(linfifo_t *lf, void **out_buf, size_t *out_avail) {
if (LINFIFO_UNLIKELY(!lf || !out_buf || !out_avail)) { return LINFIFO_RETVAL_ERR_ARG; }
*out_buf = &((char *)lf->seat)[lf->head & (lf->capacity - 1)];
*out_avail = lf->capacity - (lf->head - lf->tail);
return *out_avail ? LINFIFO_RETVAL_SUCCESS : LINFIFO_RETVAL_ERR_NO_MEM;
}
linfifo_retval_t linfifo_put_commit(linfifo_t *lf, size_t len) {
if (LINFIFO_UNLIKELY(!lf)) { return LINFIFO_RETVAL_ERR_ARG; }
if (LINFIFO_UNLIKELY(len > (lf->capacity - (lf->head - lf->tail)))) {
return LINFIFO_RETVAL_ERR_NO_MEM;
}
lf->head += len;
return LINFIFO_RETVAL_SUCCESS;
}
linfifo_retval_t linfifo_get_acquire(linfifo_t *lf, void **out_buf, size_t *out_avail) {
if (LINFIFO_UNLIKELY(!lf || !out_buf || !out_avail)) { return LINFIFO_RETVAL_ERR_ARG; }
*out_buf = &((char *)lf->seat)[lf->tail & (lf->capacity - 1)];
*out_avail = lf->head - lf->tail;
return *out_avail ? LINFIFO_RETVAL_SUCCESS : LINFIFO_RETVAL_ERR_NO_MEM;
}
linfifo_retval_t linfifo_get_commit(linfifo_t *lf, size_t len) {
if (LINFIFO_UNLIKELY(!lf)) { return LINFIFO_RETVAL_ERR_ARG; }
if (LINFIFO_UNLIKELY(len > (lf->head - lf->tail))) { return LINFIFO_RETVAL_ERR_NO_MEM; }
lf->tail += len;
return LINFIFO_RETVAL_SUCCESS;
}