-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrqueue.h
More file actions
67 lines (58 loc) · 1.19 KB
/
rqueue.h
File metadata and controls
67 lines (58 loc) · 1.19 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
#ifndef __RQUEUE_H_
#define __RQUEUE_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdlib.h>
#include <string.h>
#define RQUEUE_QUEUE_FULL -1
#define RQUEUE_QUEUE_EMPTY -2
struct rqueue {
unsigned int len;
unsigned int head;
unsigned int tail;
void *queue[0];
};
static struct rqueue *
rqueue_init(unsigned int queue_len)
{
struct rqueue *rqueue = NULL;
int malloc_size = sizeof(struct rqueue)+sizeof(void *)*queue_len;
rqueue = (struct rqueue *)malloc(malloc_size);
if (rqueue != NULL) {
memset(rqueue, 0, malloc_size);
rqueue->len = queue_len;
rqueue->head = 0;
rqueue->tail = 0;
}
return rqueue;
}
static void
rqueue_destroy(struct rqueue *rqueue)
{
free(rqueue);
}
static int
rqueue_enqueue(struct rqueue *rqueue, void *item)
{
if((unsigned int)(rqueue->head - rqueue->tail) < rqueue->len) {
rqueue->queue[rqueue->head % rqueue->len] = item;
rqueue->head += 1;
return 0;
}
return RQUEUE_QUEUE_FULL;
}
static int
rqueue_dequeue(struct rqueue *rqueue, void **item)
{
if ((unsigned int)(rqueue->head - rqueue->tail) > 0) {
*item = rqueue->queue[rqueue->tail % rqueue->len];
rqueue->tail += 1;
return 0;
}
return RQUEUE_QUEUE_EMPTY;
}
#ifdef __cplusplus
}
#endif
#endif