1 /*
2 * Copyright (c) 2002, 2003 MATPOCKuH. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25 #include <stdlib.h>
26
27 #include "mqueue.h"
28
29 mq_queue_t *
mq_makequeue(size_t recordsize)30 mq_makequeue(size_t recordsize)
31 {
32 mq_queue_t *tmp;
33
34 tmp = malloc(sizeof(mq_queue_t));
35
36 tmp->head = tmp->tail = tmp->cur = tmp->next = NULL;
37 tmp->rsize = recordsize;
38
39 return tmp;
40 }
41
42 void
mq_addrecord(mq_queue_t * mq,mq_record_t * record)43 mq_addrecord(mq_queue_t *mq, mq_record_t *record)
44 {
45 record->next = NULL;
46 record->prev = mq->tail;
47
48 if(mq->tail) {
49 mq->tail->next = record;
50 mq->tail = record;
51 } else {
52 mq->head = mq->tail = record;
53 }
54 }
55
56 mq_record_t *
mq_addnewrecord(mq_queue_t * mq)57 mq_addnewrecord(mq_queue_t *mq)
58 {
59 mq_record_t *tmp;
60
61 tmp = malloc(mq->rsize);
62
63 mq_addrecord(mq, tmp);
64
65 return tmp;
66 }
67
68 mq_record_t *
mq_resetcurrent(mq_queue_t * mq)69 mq_resetcurrent(mq_queue_t *mq)
70 {
71 return (mq->next = mq->head);
72 }
73
74 mq_record_t *
mq_getcurrent(mq_queue_t * mq)75 mq_getcurrent(mq_queue_t *mq)
76 {
77 return mq->cur;
78 }
79
80 mq_record_t *
mq_gethead(mq_queue_t * mq)81 mq_gethead(mq_queue_t *mq)
82 {
83 return mq->head;
84 }
85
86 mq_record_t *
mq_gettail(mq_queue_t * mq)87 mq_gettail(mq_queue_t *mq)
88 {
89 return mq->tail;
90 }
91
92 mq_record_t *
mq_nextrecord(mq_queue_t * mq)93 mq_nextrecord(mq_queue_t *mq)
94 {
95 mq->cur = mq->next;
96
97 if(!mq->next)
98 return NULL;
99
100 mq->next = mq->next->next;
101
102 return mq->cur;
103 }
104
105 mq_record_t *
mq_removecurrent(mq_queue_t * mq)106 mq_removecurrent(mq_queue_t *mq)
107 {
108 mq_record_t *tmp = mq->cur;
109
110 if(!tmp)
111 return NULL;
112
113 if(tmp->next)
114 tmp->next->prev = tmp->prev;
115 else
116 mq->tail = tmp->prev;
117
118 if(tmp->prev)
119 tmp->prev->next = tmp->next;
120 else
121 mq->head = tmp->next;
122
123 free(tmp);
124
125 return mq->cur;
126 }
127