Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 105 lines (91 sloc) 2.09 kb
2ffe0e9 @stash rinq: a ring-queue
authored
1
2 // read "rinq" as "ring-queue"
3
4 struct rinq {
5 struct rinq *next;
6 struct rinq *prev;
7 void *ref;
8 };
9
10 #define RINQ_IS_UNINIT(x_) ((x_)->next == NULL && (x_)->prev == NULL)
11 #define RINQ_IS_DETACHED(x_) ((x_)->next == (x_))
12 #define RINQ_IS_ATTACHED(x_) ((x_)->next != (x_))
13
14 #define RINQ_NEW(x_,ref_) do { \
15 x_ = (struct rinq *)malloc(sizeof(struct rinq)); \
16 x_->next = x_->prev = x_; \
17 x_->ref = ref_; \
18 } while(0)
19
20 #define RINQ_DETACH(x_) do { \
21 (x_)->next->prev = (x_)->prev; \
22 (x_)->prev->next = (x_)->next; \
23 (x_)->next = (x_)->prev = (x_); \
24 } while(0)
25
ebe1730 @stash Comment-out unused operations in rinq.c (so it's clear these aren't test...
authored
26 // INLINE_UNLESS_DEBUG
27 // static void
28 // rinq_unshift(struct rinq **head, void *ref)
29 // {
30 // struct rinq *x;
31 // RINQ_NEW(x,ref);
32 //
33 // if ((*head) != NULL) {
34 // x->next = (*head)->next;
35 // x->prev = (*head);
36 // x->next->prev = x->prev->next = x;
37 // }
38 // (*head) = x;
39 // }
2ffe0e9 @stash rinq: a ring-queue
authored
40
ebe1730 @stash Comment-out unused operations in rinq.c (so it's clear these aren't test...
authored
41 INLINE_UNLESS_DEBUG
2ffe0e9 @stash rinq: a ring-queue
authored
42 static void
43 rinq_push (struct rinq **head, void *ref)
44 {
45 struct rinq *x;
46 RINQ_NEW(x,ref);
47
48 if ((*head) == NULL) {
49 (*head) = x;
50 }
51 else {
52 x->next = (*head);
53 x->prev = (*head)->prev;
54 x->next->prev = x->prev->next = x;
55 }
56 }
57
58 // remove element from tail of rinq
ebe1730 @stash Comment-out unused operations in rinq.c (so it's clear these aren't test...
authored
59 // not actually used
60 // INLINE_UNLESS_DEBUG
61 // static void *
62 // rinq_pop (struct rinq **head) {
63 // void *ref;
64 // struct rinq *x;
65 //
66 // if ((*head) == NULL) return NULL;
67 //
68 // if (RINQ_IS_DETACHED((*head))) {
69 // x = (*head);
70 // (*head) = NULL;
71 // }
72 // else {
73 // x = (*head)->prev;
74 // RINQ_DETACH(x);
75 // }
76 //
77 // ref = x->ref;
78 // free(x);
79 // return ref;
80 // }
2ffe0e9 @stash rinq: a ring-queue
authored
81
82 // remove element from head of rinq
ebe1730 @stash Comment-out unused operations in rinq.c (so it's clear these aren't test...
authored
83 INLINE_UNLESS_DEBUG
2ffe0e9 @stash rinq: a ring-queue
authored
84 static void *
85 rinq_shift (struct rinq **head) {
86 void *ref;
87 struct rinq *x;
88
89 if ((*head) == NULL) return NULL;
90
91 if (RINQ_IS_DETACHED((*head))) {
92 x = (*head);
93 (*head) = NULL;
94 }
95 else {
96 x = (*head);
97 (*head) = (*head)->next;
98 RINQ_DETACH(x);
99 }
100
101 ref = x->ref;
102 free(x);
103 return ref;
104 }
Something went wrong with that request. Please try again.