Update required Autoconf version in INSTALL.
[sliver-openvswitch.git] / lib / ofpbuf.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  * 
4  * We are making the OpenFlow specification and associated documentation
5  * (Software) available for public use and benefit with the expectation
6  * that others will use, modify and enhance the Software and contribute
7  * those enhancements back to the community. However, since we would
8  * like to make the Software available for broadest use, with as few
9  * restrictions as possible permission is hereby granted, free of
10  * charge, to any person obtaining a copy of this Software to deal in
11  * the Software under the copyrights without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  * 
29  * The name and trademarks of copyright holder(s) may NOT be used in
30  * advertising or publicity pertaining to the Software or any
31  * derivatives without specific, written prior permission.
32  */
33
34 #include <config.h>
35 #include "ofpbuf.h"
36 #include <assert.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include "util.h"
40
41 /* Initializes 'b' as an empty ofpbuf that contains the 'allocated' bytes of
42  * memory starting at 'base'.
43  *
44  * 'base' should ordinarily be the first byte of a region obtained from
45  * malloc(), but in circumstances where it can be guaranteed that 'b' will
46  * never need to be expanded or freed, it can be a pointer into arbitrary
47  * memory. */
48 void
49 ofpbuf_use(struct ofpbuf *b, void *base, size_t allocated)
50 {
51     b->base = b->data = base;
52     b->allocated = allocated;
53     b->size = 0;
54     b->l2 = b->l3 = b->l4 = b->l7 = NULL;
55     b->next = NULL;
56     b->private = NULL;
57 }
58
59 /* Initializes 'b' as an empty ofpbuf with an initial capacity of 'size'
60  * bytes. */
61 void
62 ofpbuf_init(struct ofpbuf *b, size_t size)
63 {
64     ofpbuf_use(b, size ? xmalloc(size) : NULL, size);
65 }
66
67 /* Frees memory that 'b' points to. */
68 void
69 ofpbuf_uninit(struct ofpbuf *b) 
70 {
71     if (b) {
72         free(b->base);
73     }
74 }
75
76 /* Frees memory that 'b' points to and allocates a new ofpbuf */
77 void
78 ofpbuf_reinit(struct ofpbuf *b, size_t size)
79 {
80     ofpbuf_uninit(b);
81     ofpbuf_init(b, size);
82 }
83
84 /* Creates and returns a new ofpbuf with an initial capacity of 'size'
85  * bytes. */
86 struct ofpbuf *
87 ofpbuf_new(size_t size)
88 {
89     struct ofpbuf *b = xmalloc(sizeof *b);
90     ofpbuf_init(b, size);
91     return b;
92 }
93
94 struct ofpbuf *
95 ofpbuf_clone(const struct ofpbuf *buffer) 
96 {
97     /* FIXME: reference counting. */
98     struct ofpbuf *b = ofpbuf_new(buffer->size);
99     ofpbuf_put(b, buffer->data, buffer->size);
100     return b;
101 }
102
103 /* Frees memory that 'b' points to, as well as 'b' itself. */
104 void
105 ofpbuf_delete(struct ofpbuf *b) 
106 {
107     if (b) {
108         ofpbuf_uninit(b);
109         free(b);
110     }
111 }
112
113 /* Returns the number of bytes of headroom in 'b', that is, the number of bytes
114  * of unused space in ofpbuf 'b' before the data that is in use.  (Most
115  * commonly, the data in a ofpbuf is at its beginning, and thus the ofpbuf's
116  * headroom is 0.) */
117 size_t
118 ofpbuf_headroom(struct ofpbuf *b) 
119 {
120     return (char*)b->data - (char*)b->base;
121 }
122
123 /* Returns the number of bytes that may be appended to the tail end of ofpbuf
124  * 'b' before the ofpbuf must be reallocated. */
125 size_t
126 ofpbuf_tailroom(struct ofpbuf *b) 
127 {
128     return (char*)ofpbuf_end(b) - (char*)ofpbuf_tail(b);
129 }
130
131 /* Ensures that 'b' has room for at least 'size' bytes at its tail end,
132  * reallocating and copying its data if necessary. */
133 void
134 ofpbuf_prealloc_tailroom(struct ofpbuf *b, size_t size) 
135 {
136     if (size > ofpbuf_tailroom(b)) {
137         size_t new_allocated = b->allocated + MAX(size, 64);
138         void *new_base = xmalloc(new_allocated);
139         uintptr_t base_delta = (char*)new_base - (char*)b->base;
140         memcpy(new_base, b->base, b->allocated);
141         free(b->base);
142         b->base = new_base;
143         b->allocated = new_allocated;
144         b->data = (char*)b->data + base_delta;
145         if (b->l2) {
146             b->l2 = (char*)b->l2 + base_delta;
147         }
148         if (b->l3) {
149             b->l3 = (char*)b->l3 + base_delta;
150         }
151         if (b->l4) {
152             b->l4 = (char*)b->l4 + base_delta;
153         }
154         if (b->l7) {
155             b->l7 = (char*)b->l7 + base_delta;
156         }
157     }
158 }
159
160 void
161 ofpbuf_prealloc_headroom(struct ofpbuf *b, size_t size) 
162 {
163     assert(size <= ofpbuf_headroom(b));
164 }
165
166 /* Appends 'size' bytes of data to the tail end of 'b', reallocating and
167  * copying its data if necessary.  Returns a pointer to the first byte of the
168  * new data, which is left uninitialized. */
169 void *
170 ofpbuf_put_uninit(struct ofpbuf *b, size_t size) 
171 {
172     void *p;
173     ofpbuf_prealloc_tailroom(b, size);
174     p = ofpbuf_tail(b);
175     b->size += size;
176     return p;
177 }
178
179 /* Appends 'size' zeroed bytes to the tail end of 'b'.  Data in 'b' is
180  * reallocated and copied if necessary.  Returns a pointer to the first byte of
181  * the data's location in the ofpbuf. */
182 void *
183 ofpbuf_put_zeros(struct ofpbuf *b, size_t size)
184 {
185     void *dst = ofpbuf_put_uninit(b, size);
186     memset(dst, 0, size);
187     return dst;
188 }
189
190 /* Appends the 'size' bytes of data in 'p' to the tail end of 'b'.  Data in 'b'
191  * is reallocated and copied if necessary.  Returns a pointer to the first
192  * byte of the data's location in the ofpbuf. */
193 void *
194 ofpbuf_put(struct ofpbuf *b, const void *p, size_t size) 
195 {
196     void *dst = ofpbuf_put_uninit(b, size);
197     memcpy(dst, p, size);
198     return dst;
199 }
200
201 /* Reserves 'size' bytes of headroom so that they can be later allocated with
202  * ofpbuf_push_uninit() without reallocating the ofpbuf. */
203 void
204 ofpbuf_reserve(struct ofpbuf *b, size_t size) 
205 {
206     assert(!b->size);
207     ofpbuf_prealloc_tailroom(b, size);
208     b->data = (char*)b->data + size;
209 }
210
211 void *
212 ofpbuf_push_uninit(struct ofpbuf *b, size_t size) 
213 {
214     ofpbuf_prealloc_headroom(b, size);
215     b->data = (char*)b->data - size;
216     b->size += size;
217     return b->data;
218 }
219
220 void *
221 ofpbuf_push(struct ofpbuf *b, const void *p, size_t size) 
222 {
223     void *dst = ofpbuf_push_uninit(b, size);
224     memcpy(dst, p, size);
225     return dst;
226 }
227
228 /* If 'b' contains at least 'offset + size' bytes of data, returns a pointer to
229  * byte 'offset'.  Otherwise, returns a null pointer. */
230 void *
231 ofpbuf_at(const struct ofpbuf *b, size_t offset, size_t size) 
232 {
233     return offset + size <= b->size ? (char *) b->data + offset : NULL;
234 }
235
236 /* Returns a pointer to byte 'offset' in 'b', which must contain at least
237  * 'offset + size' bytes of data. */
238 void *
239 ofpbuf_at_assert(const struct ofpbuf *b, size_t offset, size_t size) 
240 {
241     assert(offset + size <= b->size);
242     return ((char *) b->data) + offset;
243 }
244
245 /* Returns the byte following the last byte of data in use in 'b'. */
246 void *
247 ofpbuf_tail(const struct ofpbuf *b) 
248 {
249     return (char *) b->data + b->size;
250 }
251
252 /* Returns the byte following the last byte allocated for use (but not
253  * necessarily in use) by 'b'. */
254 void *
255 ofpbuf_end(const struct ofpbuf *b) 
256 {
257     return (char *) b->base + b->allocated;
258 }
259
260 /* Clears any data from 'b'. */
261 void
262 ofpbuf_clear(struct ofpbuf *b) 
263 {
264     b->data = b->base;
265     b->size = 0;
266 }
267
268 /* Removes 'size' bytes from the head end of 'b', which must contain at least
269  * 'size' bytes of data.  Returns the first byte of data removed. */
270 void *
271 ofpbuf_pull(struct ofpbuf *b, size_t size) 
272 {
273     void *data = b->data;
274     assert(b->size >= size);
275     b->data = (char*)b->data + size;
276     b->size -= size;
277     return data;
278 }
279
280 /* If 'b' has at least 'size' bytes of data, removes that many bytes from the
281  * head end of 'b' and returns the first byte removed.  Otherwise, returns a
282  * null pointer without modifying 'b'. */
283 void *
284 ofpbuf_try_pull(struct ofpbuf *b, size_t size) 
285 {
286     return b->size >= size ? ofpbuf_pull(b, size) : NULL;
287 }