47600e6ae3346d6befc3b98305be48df7067fc25
[sliver-openvswitch.git] / lib / buffer.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 "buffer.h"
36 #include <assert.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include "util.h"
40
41 /* Initializes 'b' as an empty buffer 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 buffer_use(struct buffer *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 a buffer with an initial capacity of 'size' bytes. */
60 void
61 buffer_init(struct buffer *b, size_t size)
62 {
63     buffer_use(b, size ? xmalloc(size) : NULL, size);
64 }
65
66 /* Frees memory that 'b' points to. */
67 void
68 buffer_uninit(struct buffer *b) 
69 {
70     if (b) {
71         free(b->base);
72     }
73 }
74
75 /* Frees memory that 'b' points to and allocates a new buffer */
76 void
77 buffer_reinit(struct buffer *b, size_t size)
78 {
79     buffer_uninit(b);
80     buffer_init(b, size);
81 }
82
83 /* Creates and returns a new buffer with an initial capacity of 'size'
84  * bytes. */
85 struct buffer *
86 buffer_new(size_t size)
87 {
88     struct buffer *b = xmalloc(sizeof *b);
89     buffer_init(b, size);
90     return b;
91 }
92
93 struct buffer *
94 buffer_clone(const struct buffer *buffer) 
95 {
96     /* FIXME: reference counting. */
97     struct buffer *b = buffer_new(buffer->size);
98     buffer_put(b, buffer->data, buffer->size);
99     return b;
100 }
101
102 /* Frees memory that 'b' points to, as well as 'b' itself. */
103 void
104 buffer_delete(struct buffer *b) 
105 {
106     if (b) {
107         buffer_uninit(b);
108         free(b);
109     }
110 }
111
112 /* Returns the number of bytes of headroom in 'b', that is, the number of bytes
113  * of unused space in buffer 'b' before the data that is in use.  (Most
114  * commonly, the data in a buffer is at its beginning, and thus the buffer's
115  * headroom is 0.) */
116 size_t
117 buffer_headroom(struct buffer *b) 
118 {
119     return b->data - b->base;
120 }
121
122 /* Returns the number of bytes that may be appended to the tail end of buffer
123  * 'b' before the buffer must be reallocated. */
124 size_t
125 buffer_tailroom(struct buffer *b) 
126 {
127     return buffer_end(b) - buffer_tail(b);
128 }
129
130 /* Ensures that 'b' has room for at least 'size' bytes at its tail end,
131  * reallocating and copying its data if necessary. */
132 void
133 buffer_prealloc_tailroom(struct buffer *b, size_t size) 
134 {
135     if (size > buffer_tailroom(b)) {
136         size_t new_allocated = b->allocated + MAX(size, 64);
137         void *new_base = xmalloc(new_allocated);
138         uintptr_t base_delta = new_base - b->base;
139         memcpy(new_base, b->base, b->allocated);
140         free(b->base);
141         b->base = new_base;
142         b->allocated = new_allocated;
143         b->data += base_delta;
144         if (b->l2) {
145             b->l2 += base_delta;
146         }
147         if (b->l3) {
148             b->l3 += base_delta;
149         }
150         if (b->l4) {
151             b->l4 += base_delta;
152         }
153         if (b->l7) {
154             b->l7 += base_delta;
155         }
156     }
157 }
158
159 void
160 buffer_prealloc_headroom(struct buffer *b, size_t size) 
161 {
162     assert(size <= buffer_headroom(b));
163 }
164
165 /* Appends 'size' bytes of data to the tail end of 'b', reallocating and
166  * copying its data if necessary.  Returns a pointer to the first byte of the
167  * new data, which is left uninitialized. */
168 void *
169 buffer_put_uninit(struct buffer *b, size_t size) 
170 {
171     void *p;
172     buffer_prealloc_tailroom(b, size);
173     p = buffer_tail(b);
174     b->size += size;
175     return p;
176 }
177
178 /* Appends the 'size' bytes of data in 'p' to the tail end of 'b'.  Data in 'b'
179  * is reallocated and copied if necessary.  Returns a pointer to the first
180  * byte of the data's location in the buffer. */
181 void *
182 buffer_put(struct buffer *b, const void *p, size_t size) 
183 {
184     void *dst = buffer_put_uninit(b, size);
185     memcpy(dst, p, size);
186     return dst;
187 }
188
189 /* Reserves 'size' bytes of headroom so that they can be later allocated with
190  * buffer_push_uninit() without reallocating the buffer. */
191 void
192 buffer_reserve(struct buffer *b, size_t size) 
193 {
194     assert(!b->size);
195     buffer_prealloc_tailroom(b, size);
196     b->data += size;
197 }
198
199 void *
200 buffer_push_uninit(struct buffer *b, size_t size) 
201 {
202     buffer_prealloc_headroom(b, size);
203     b->data -= size;
204     b->size += size;
205     return b->data;
206 }
207
208 void *
209 buffer_push(struct buffer *b, const void *p, size_t size) 
210 {
211     void *dst = buffer_push_uninit(b, size);
212     memcpy(dst, p, size);
213     return dst;
214 }
215
216 /* If 'b' contains at least 'offset + size' bytes of data, returns a pointer to
217  * byte 'offset'.  Otherwise, returns a null pointer. */
218 void *
219 buffer_at(const struct buffer *b, size_t offset, size_t size) 
220 {
221     return offset + size <= b->size ? (char *) b->data + offset : NULL;
222 }
223
224 /* Returns a pointer to byte 'offset' in 'b', which must contain at least
225  * 'offset + size' bytes of data. */
226 void *
227 buffer_at_assert(const struct buffer *b, size_t offset, size_t size) 
228 {
229     assert(offset + size <= b->size);
230     return ((char *) b->data) + offset;
231 }
232
233 /* Returns the byte following the last byte of data in use in 'b'. */
234 void *
235 buffer_tail(const struct buffer *b) 
236 {
237     return (char *) b->data + b->size;
238 }
239
240 /* Returns the byte following the last byte allocated for use (but not
241  * necessarily in use) by 'b'. */
242 void *
243 buffer_end(const struct buffer *b) 
244 {
245     return (char *) b->base + b->allocated;
246 }
247
248 /* Clears any data from 'b'. */
249 void
250 buffer_clear(struct buffer *b) 
251 {
252     b->data = b->base;
253     b->size = 0;
254 }
255
256 /* Removes 'size' bytes from the head end of 'b', which must contain at least
257  * 'size' bytes of data.  Returns the first byte of data removed. */
258 void *
259 buffer_pull(struct buffer *b, size_t size) 
260 {
261     void *data = b->data;
262     assert(b->size >= size);
263     b->data += size;
264     b->size -= size;
265     return data;
266 }
267
268 /* If 'b' has at least 'size' bytes of data, removes that many bytes from the
269  * head end of 'b' and returns the first byte removed.  Otherwise, returns a
270  * null pointer without modifying 'b'. */
271 void *
272 buffer_try_pull(struct buffer *b, size_t size) 
273 {
274     return b->size >= size ? buffer_pull(b, size) : NULL;
275 }