-
Notifications
You must be signed in to change notification settings - Fork 7
/
bs_cqueue.c
108 lines (85 loc) · 1.99 KB
/
bs_cqueue.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* file : bs_cqueue.c
* author : bushaofeng
* create : 2014-01-22 18:24
* func :
* history:
*/
#include "bs_cqueue.h"
#include "bs_common.h"
state_t bs_cqueue_init(_cqueue_t* c, uint32_t size, uint32_t esize, bool_t islock){
assert(c!=NULL);
memset(c, 0, sizeof(_cqueue_t));
c->esize = esize;
c->size = size;
c->mem = malloc(c->size * c->esize);
if(c->mem == NULL){
return BS_NOMEM;
}
memset(c->mem, 0, c->size*c->esize);
c->islock = islock;
if(islock){
bs_lock_init(&c->lock);
}
return BS_SUCCESS;
}
void* bs_cqueue_push(_cqueue_t* c, void* e, uint32_t size){
void* ptr;
if(c == NULL){
return NULL;
}
if(c->islock){
bs_wrlock(&c->lock);
}
if(cqueue_full(c)){
c->head = cqueue_next(c, c->head);
}
ptr = cqueue_addr(c, c->rear);
if(e != NULL){
bs_memcpy(ptr, c->esize, e, size);
}
else{
memset(ptr, 0, c->esize);
}
c->rear = cqueue_next(c, c->rear);
if(c->islock){
bs_wrlock_unlock(&c->lock);
}
return ptr;
}
state_t bs_cqueue_pop(_cqueue_t* c, void* e, uint32_t size){
state_t st = BS_SUCCESS;
if(c==NULL || e==NULL){
return BS_PARANULL;
}
if(c->islock){
bs_wrlock(&c->lock);
}
if(cqueue_empty(c)){
st = BS_EMPTY;
}
else{
bs_memcpy(e, size, cqueue_addr(c, c->head), c->esize);
c->head = cqueue_next(c, c->head);
}
if(c->islock){
bs_wrlock_unlock(&c->lock);
}
return st;
}
void* bs_cqueue_get(_cqueue_t* c, uint32_t i){
uint32_t pos = cqueue_index(c, i);
if (!cqueue_contain(c, pos)) {
return NULL;
}
return cqueue_addr(c, pos);
}
void* bs_cqueue_search(_cqueue_t* c, void* e, compare_f cmp){
uint32_t i;
for(i=c->head; i!=c->rear; i=(i+1)%c->size){
if(cmp(e, cqueue_addr(c, i))==0){
return cqueue_addr(c, i);
}
}
return NULL;
}