-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
240 lines (191 loc) · 4.51 KB
/
Copy pathqueue.c
File metadata and controls
240 lines (191 loc) · 4.51 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include "queue.h"
/* can be modified with semaphore */
void queue_lock(QUEUE_T *q)
{
q->isLocked = 1;
}
void queue_unlock(QUEUE_T *q)
{
q->isLocked = 0;
}
/**
* @brief Put object to queue.
* @param q pointer to queue struct.
* @param obj Pointer to object you want put, It can be *uint8_t, *uint16_t,
* *uint32_t, or any size struct pointer, but it must match with
* objSize that was set when queue init.
*
* @retval Error if not 0.
*/
QUEUE_ERROR queue_put(QUEUE_T *q, void *obj)
{
if (!q->isInit)
return QUEUE_INIT_ERR;
if (q->numObjs == q->maxObjs)
return QUEUE_NO_MEM_ERR;
while(q->isLocked)
{;}
queue_lock(q);
#if QUEUE_OPTIMISE
if(1 == q->qSize)
{
*((uint8_t*)(q->putPtr)) = *((uint8_t*)(obj));
}
else if (2 == q->qSize)
{
*((uint16_t*)(q->putPtr))=*((uint16_t*)(obj));
}
else if (4 == q->qSize)
{
*((uint32_t*)(q->putPtr))=*((uint32_t*)(obj));
}
else
{
memcpy(q->putPtr, obj, q->qSize);
}
#else
memcpy(q->putPtr, obj, q->qSize);
#endif
q->numObjs++;
//loop queue
q->putPtr = (q->endPtr==q->putPtr)? (q->startPtr) : (q->putPtr+q->qSize);
queue_unlock(q);
return QUEUE_OK;
}
/**
* @brief Put object to head queue.
* @param q pointer to queue struct.
* @param obj Pointer to object you want put, It can be *uint8_t, *uint16_t,
* *uint32_t, or any size struct pointer, but it must match with
* objSize that was set when queue init.
*
* @note if queue is not full you can put object in head (like stack)
*
* @retval Error if not 0.
*/
QUEUE_ERROR queue_putToHead(QUEUE_T *q, void *obj)
{
if (!q->isInit)
return QUEUE_INIT_ERR;
if (q->numObjs == q->maxObjs)
return QUEUE_NO_MEM_ERR;
while(q->isLocked)
{;}
queue_lock(q);
/* shift getPtr back and write data into *(getPtr) */
q->getPtr = (q->getPtr==q->startPtr)? (q->endPtr) : (q->getPtr-q->qSize);
#if QUEUE_OPTIMISE
if(1 == q->qSize)
{
*((uint8_t*)(q->getPtr)) = *((uint8_t*)(obj));
}
else if (2 == q->qSize)
{
*((uint16_t*)(q->getPtr))=*((uint16_t*)(obj));
}
else if (4 == q->qSize)
{
*((uint32_t*)(q->getPtr))=*((uint32_t*)(obj));
}
else
{
memcpy(q->getPtr, obj, q->qSize);
}
#else
memcpy(q->getPtr, obj, q->qSize);
#endif
q->numObjs++;
/* put ptr is not shifting */
//q->putPtr = (q->endPtr==q->putPtr)? (q->startPtr) : (q->putPtr+q->qSize);
queue_unlock(q);
return QUEUE_OK;
}
/**
* @brief Get object from queue.
* @param q pointer to queue struct.
* @param obj pointer to object you want get, It can be *uint8_t, *uint16_t,
* *uint32_t, or any size struct pointer, but it must match with
* objSize that was set when queue init.
*
* @retval Error if not 0.
*/
QUEUE_ERROR queue_get(QUEUE_T *q, void *obj)
{
if (!q->isInit)
return QUEUE_INIT_ERR;
if (0 == q->numObjs)
return QUEUE_EMPTY_ERR;
while(q->isLocked)
{;}
queue_lock(q);
#if QUEUE_OPTIMISE
if(1 == q->qSize)
{
*((uint8_t*)(obj)) = *((uint8_t*)(q->getPtr));
}
else if (2 == q->qSize)
{
*((uint16_t*)(obj)) = *((uint16_t*)(q->getPtr));
}
else if (4 == q->qSize)
{
*((uint32_t*)(obj)) = *((uint32_t*)(q->getPtr));
}
else
{
memcpy(q->getPtr, obj, q->qSize);
}
#else
memcpy(obj, q->getPtr, q->qSize);
#endif
#if QUEUE_DEBUG
memset(q->getPtr, 0xAA , q->qSize);
#endif
q->getPtr = (q->getPtr==q->endPtr)? (q->startPtr) : (q->getPtr+q->qSize);
q->numObjs--;
queue_unlock(q);
return QUEUE_OK;
}
/**
* @brief Get num of putt objects in queue.
* @retval Num objects in queue.
*/
uint32_t queue_get_num(QUEUE_T *q)
{
if (NULL==q)
return 0;
return q->numObjs;
}
/**
* @brief Init queue.
* @param q pointer to queue struct
* @param buf pointer to queue buffer
* @param buf_size_bytes size of buf
* @param objSize size of one object in queue in bytes
* @retval Error if not 0.
*
* @todo align end pointer
*/
QUEUE_ERROR queue_init(QUEUE_T *q, void *buf, uint32_t buf_size_bytes, uint32_t objSize)
{
uint8_t endPtr;
if ((NULL==q) || (NULL == buf))
return QUEUE_NULL_ERR;
if (objSize > buf_size_bytes)
return QUEUE_INIT_WRONG_SIZE_ERR;
if(buf_size_bytes%objSize)
printf("buf size not equal to objects\r\n");
q->qSize = objSize;
q->getPtr = buf;
q->putPtr = buf;
q->startPtr = buf;
q->numObjs = 0;
q->maxObjs = buf_size_bytes/objSize;
q->endPtr = ((uint8_t*)buf) + ((q->maxObjs-1)*(objSize));
q->isInit = 1;
q->isLocked = 0;
return QUEUE_OK;
}