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