Debian Codegraph @

Xpjproject/pjlib/src/pj/fifobuf.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include <pj/fifobuf.h>
20 #include <pj/errno.h>
21 #include <pj/log.h>
22 #include <pj/assert.h>
23 #include <pj/os.h>
24 #include <pj/string.h>
25
26 #define THIS_FILE "fifobuf"
27
28 #define SZ sizeof(unsigned)
29
30 /* put and get size at arbitrary, possibly unaligned location */
31 PJ_INLINE(void) put_size(void *ptr, unsigned size)
32 {
33 pj_memcpy(ptr, &size, sizeof(size));
34 }
35
36 PJ_INLINE(unsigned) get_size(const void *ptr)
37 {
38 unsigned size;
39 pj_memcpy(&size, ptr, sizeof(size));
40 return size;
41 }
42
43 PJ_DEF(void) pj_fifobuf_init (pj_fifobuf_t *fifobuf, void *buffer, unsigned size)
44 {
45 PJ_CHECK_STACK();
46
47 PJ_LOG(6, (THIS_FILE,
48 "fifobuf_init fifobuf=%p buffer=%p, size=%d",
49 fifobuf, buffer, size));
50
51 fifobuf->first = (char*)buffer;
52 fifobuf->last = fifobuf->first + size;
53 fifobuf->ubegin = fifobuf->uend = fifobuf->first;
54 fifobuf->full = (fifobuf->last==fifobuf->first);
55 }
56
57 PJ_DEF(unsigned) pj_fifobuf_capacity (pj_fifobuf_t *fifobuf)
58 {
59 unsigned cap = (unsigned)(fifobuf->last - fifobuf->first);
60 return (cap > 0) ? cap-SZ : 0;
61 }
62
63 PJ_DEF(unsigned) pj_fifobuf_available_size (pj_fifobuf_t *fifobuf)
64 {
65 PJ_CHECK_STACK();
66
67 if (fifobuf->full)
68 return 0;
69
70 if (fifobuf->uend >= fifobuf->ubegin) {
71 unsigned s;
72 unsigned s1 = (unsigned)(fifobuf->last - fifobuf->uend);
73 unsigned s2 = (unsigned)(fifobuf->ubegin - fifobuf->first);
74 if (s1 <= SZ)
75 s = s2;
76 else if (s2 <= SZ)
77 s = s1;
78 else
79 s = s1<s2 ? s2 : s1;
80
81 return (s>=SZ) ? s-SZ : 0;
82 } else {
83 unsigned s = (unsigned)(fifobuf->ubegin - fifobuf->uend);
84 return (s>=SZ) ? s-SZ : 0;
85 }
86 }
87
88 PJ_DEF(void*) pj_fifobuf_alloc (pj_fifobuf_t *fifobuf, unsigned size)
89 {
90 unsigned available;
91 char *start;
92
93 PJ_CHECK_STACK();
94
95 if (fifobuf->full) {
96 PJ_LOG(6, (THIS_FILE,
97 "fifobuf_alloc fifobuf=%p, size=%d: full!",
98 fifobuf, size));
99 return NULL;
100 }
101
102 /* try to allocate from the end part of the fifo */
103 if (fifobuf->uend >= fifobuf->ubegin) {
104 /* If we got here, then first <= ubegin <= uend <= last, and
105 * the the buffer layout is like this:
106 *
107 * <--free0---> <--- used --> <-free1->
108 * | |#############| |
109 * ^ ^ ^ ^
110 * first ubegin uend last
111 *
112 * where the size of free0, used, and/or free1 may be zero.
113 */
114 available = (unsigned)(fifobuf->last - fifobuf->uend);
115 if (available >= size+SZ) {
116 char *ptr = fifobuf->uend;
117 fifobuf->uend += (size+SZ);
118 if (fifobuf->uend == fifobuf->last)
119 fifobuf->uend = fifobuf->first;
120 if (fifobuf->uend == fifobuf->ubegin)
121 fifobuf->full = 1;
122 put_size(ptr, size+SZ);
123 ptr += SZ;
124
125 PJ_LOG(6, (THIS_FILE,
126 "fifobuf_alloc fifobuf=%p, size=%d: returning %p, p1=%p, p2=%p",
127 fifobuf, size, ptr, fifobuf->ubegin, fifobuf->uend));
128 return ptr;
129 }
130 }
131
132 /* If we got here, then either there is not enough space in free1 above,
133 * or the the buffer layout is like this:
134 *
135 * <-used0-> <--free--> <- used1 ->
136 * |#########| |###########|
137 * ^ ^ ^ ^
138 * first uend ubegin last
139 *
140 * where the size of used0, used1, and/or free may be zero.
141 */
142 start = (fifobuf->uend <= fifobuf->ubegin) ? fifobuf->uend : fifobuf->first;
143 available = (unsigned)(fifobuf->ubegin - start);
144 if (available >= size+SZ) {
145 char *ptr = start;
146 fifobuf->uend = start + size + SZ;
147 if (fifobuf->uend == fifobuf->ubegin)
148 fifobuf->full = 1;
149 put_size(ptr, size+SZ);
150 ptr += SZ;
151
152 PJ_LOG(6, (THIS_FILE,
153 "fifobuf_alloc fifobuf=%p, size=%d: returning %p, p1=%p, p2=%p",
154 fifobuf, size, ptr, fifobuf->ubegin, fifobuf->uend));
155 return ptr;
156 }
157
158 PJ_LOG(6, (THIS_FILE,
159 "fifobuf_alloc fifobuf=%p, size=%d: no space left! p1=%p, p2=%p",
160 fifobuf, size, fifobuf->ubegin, fifobuf->uend));
161 return NULL;
162 }
163
164 PJ_DEF(pj_status_t) pj_fifobuf_free (pj_fifobuf_t *fifobuf, void *buf)
165 {
166 char *ptr = (char*)buf;
167 char *end;
168 unsigned sz;
169
170 PJ_CHECK_STACK();
171
172 ptr -= SZ;
173 if (ptr < fifobuf->first || ptr >= fifobuf->last) {
174 pj_assert(!"Invalid pointer to free");
175 return PJ_EINVAL;
176 }
177
178 if (ptr != fifobuf->ubegin && ptr != fifobuf->first) {
179 pj_assert(!"Invalid free() sequence!");
180 return PJ_EINVAL;
181 }
182
183 end = (fifobuf->uend > fifobuf->ubegin) ? fifobuf->uend : fifobuf->last;
184 sz = get_size(ptr);
185 if (ptr+sz > end) {
186 pj_assert(!"Invalid size!");
187 return PJ_EINVAL;
188 }
189
190 fifobuf->ubegin = ptr + sz;
191
192 /* Rollover */
193 if (fifobuf->ubegin == fifobuf->last)
194 fifobuf->ubegin = fifobuf->first;
195
196 /* Reset if fifobuf is empty */
197 if (fifobuf->ubegin == fifobuf->uend)
198 fifobuf->ubegin = fifobuf->uend = fifobuf->first;
199
200 fifobuf->full = 0;
201
202 PJ_LOG(6, (THIS_FILE,
203 "fifobuf_free fifobuf=%p, ptr=%p, size=%d, p1=%p, p2=%p",
204 fifobuf, buf, sz, fifobuf->ubegin, fifobuf->uend));
205
206 return 0;
207 }