-
Notifications
You must be signed in to change notification settings - Fork 1
/
queue.c
107 lines (91 loc) · 1.86 KB
/
queue.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
#include "mem.h"
#include "log.h"
#include "queue.h"
#define QUEUE_CACHE_SIZE 1024
queue *queue_new(void) {
queue *q;
queue_node **cache;
q = sc_malloc(sizeof(queue));
if (q == NULL) {
return NULL;
}
cache = sc_malloc(sizeof(queue_node*) * QUEUE_CACHE_SIZE);
if (cache == NULL) {
sc_free(q);
return NULL;
}
q->cache = cache;
q->cache_size = 0;
q->head = q->rear = NULL;
return q;
}
void queue_dispose(queue *q) {
int i;
if (q == NULL) {
return;
}
for (i = 0; i < q->cache_size; i++) {
sc_free(q->cache[i]);
}
sc_free(q->cache);
sc_free(q);
}
int queue_isempty(queue *q) {
if (q == NULL) {
return 1;
}
return q->head == NULL;
}
int enqueue(queue *q, object *elem) {
int sz;
queue_node *p;
if (q == NULL) {
return 1;
}
sz = q->cache_size;
if (sz > 0) {
p = q->cache[--sz];
q->cache_size = sz;
/* sc_log("queue cache hit, size=%d\n", sz+1); */
} else {
p = sc_malloc(sizeof(queue_node));
if (p == NULL) {
return -1;
}
}
p->elem = elem;
p->next = NULL;
if (q->rear != NULL) {
q->rear->next = p;
}
q->rear = p;
if (q->head == NULL) {
q->head = p;
}
return 0;
}
object* dequeue(queue *q) {
int sz;
queue_node *p;
object *obj = NULL;
if (q == NULL) {
return NULL;
}
if (!queue_isempty(q)) {
p = q->head;
q->head = q->head->next;
if (q->head == NULL) {
q->rear = NULL;
}
obj = p->elem;
sz = q->cache_size;
if (sz < QUEUE_CACHE_SIZE) {
/* cache this node */
q->cache[sz++] = p;
q->cache_size = sz;
} else {
sc_free(p);
}
}
return obj;
}