ofpbuf: Add ofpbuf_new_with_headroom(), ofpbuf_clone_with_headroom().
[cascardo/ovs.git] / lib / ofpbuf.c
1 /*
2  * Copyright (c) 2008, 2009, 2010 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "ofpbuf.h"
19 #include <assert.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include "dynamic-string.h"
23 #include "util.h"
24
25 /* Initializes 'b' as an empty ofpbuf that contains the 'allocated' bytes of
26  * memory starting at 'base'.
27  *
28  * 'base' should ordinarily be the first byte of a region obtained from
29  * malloc(), but in circumstances where it can be guaranteed that 'b' will
30  * never need to be expanded or freed, it can be a pointer into arbitrary
31  * memory. */
32 void
33 ofpbuf_use(struct ofpbuf *b, void *base, size_t allocated)
34 {
35     b->base = b->data = base;
36     b->allocated = allocated;
37     b->size = 0;
38     b->l2 = b->l3 = b->l4 = b->l7 = NULL;
39     b->next = NULL;
40     b->private_p = NULL;
41 }
42
43 /* Initializes 'b' as an empty ofpbuf with an initial capacity of 'size'
44  * bytes. */
45 void
46 ofpbuf_init(struct ofpbuf *b, size_t size)
47 {
48     ofpbuf_use(b, size ? xmalloc(size) : NULL, size);
49 }
50
51 /* Frees memory that 'b' points to. */
52 void
53 ofpbuf_uninit(struct ofpbuf *b)
54 {
55     if (b) {
56         free(b->base);
57     }
58 }
59
60 /* Frees memory that 'b' points to and allocates a new ofpbuf */
61 void
62 ofpbuf_reinit(struct ofpbuf *b, size_t size)
63 {
64     ofpbuf_uninit(b);
65     ofpbuf_init(b, size);
66 }
67
68 /* Creates and returns a new ofpbuf with an initial capacity of 'size'
69  * bytes. */
70 struct ofpbuf *
71 ofpbuf_new(size_t size)
72 {
73     struct ofpbuf *b = xmalloc(sizeof *b);
74     ofpbuf_init(b, size);
75     return b;
76 }
77
78 /* Creates and returns a new ofpbuf with an initial capacity of 'size +
79  * headroom' bytes, reserving the first 'headroom' bytes as headroom. */
80 struct ofpbuf *
81 ofpbuf_new_with_headroom(size_t size, size_t headroom)
82 {
83     struct ofpbuf *b = ofpbuf_new(size + headroom);
84     ofpbuf_reserve(b, headroom);
85     return b;
86 }
87
88 struct ofpbuf *
89 ofpbuf_clone(const struct ofpbuf *buffer)
90 {
91     return ofpbuf_clone_data(buffer->data, buffer->size);
92 }
93
94 /* Creates and returns a new ofpbuf whose data are copied from 'buffer'.   The
95  * returned ofpbuf will additionally have 'headroom' bytes of headroom. */
96 struct ofpbuf *
97 ofpbuf_clone_with_headroom(const struct ofpbuf *buffer, size_t headroom)
98 {
99     struct ofpbuf *b = ofpbuf_new_with_headroom(buffer->size, headroom);
100     ofpbuf_put(b, buffer->data, buffer->size);
101     return b;
102 }
103
104 struct ofpbuf *
105 ofpbuf_clone_data(const void *data, size_t size)
106 {
107     struct ofpbuf *b = ofpbuf_new(size);
108     ofpbuf_put(b, data, size);
109     return b;
110 }
111
112 /* Frees memory that 'b' points to, as well as 'b' itself. */
113 void
114 ofpbuf_delete(struct ofpbuf *b)
115 {
116     if (b) {
117         ofpbuf_uninit(b);
118         free(b);
119     }
120 }
121
122 /* Returns the number of bytes of headroom in 'b', that is, the number of bytes
123  * of unused space in ofpbuf 'b' before the data that is in use.  (Most
124  * commonly, the data in a ofpbuf is at its beginning, and thus the ofpbuf's
125  * headroom is 0.) */
126 size_t
127 ofpbuf_headroom(const struct ofpbuf *b)
128 {
129     return (char*)b->data - (char*)b->base;
130 }
131
132 /* Returns the number of bytes that may be appended to the tail end of ofpbuf
133  * 'b' before the ofpbuf must be reallocated. */
134 size_t
135 ofpbuf_tailroom(const struct ofpbuf *b)
136 {
137     return (char*)ofpbuf_end(b) - (char*)ofpbuf_tail(b);
138 }
139
140 /* Changes 'b->base' to 'new_base' and adjusts all of 'b''s internal pointers
141  * to reflect the change. */
142 static void
143 ofpbuf_rebase__(struct ofpbuf *b, void *new_base)
144 {
145     if (b->base != new_base) {
146         uintptr_t base_delta = (char*)new_base - (char*)b->base;
147         b->base = new_base;
148         b->data = (char*)b->data + base_delta;
149         if (b->l2) {
150             b->l2 = (char*)b->l2 + base_delta;
151         }
152         if (b->l3) {
153             b->l3 = (char*)b->l3 + base_delta;
154         }
155         if (b->l4) {
156             b->l4 = (char*)b->l4 + base_delta;
157         }
158         if (b->l7) {
159             b->l7 = (char*)b->l7 + base_delta;
160         }
161     }
162 }
163
164 /* Reallocates 'b' so that it has exactly 'new_tailroom' bytes of tailroom. */
165 static void
166 ofpbuf_resize_tailroom__(struct ofpbuf *b, size_t new_tailroom)
167 {
168     b->allocated = ofpbuf_headroom(b) + b->size + new_tailroom;
169     ofpbuf_rebase__(b, xrealloc(b->base, b->allocated));
170 }
171
172 /* Ensures that 'b' has room for at least 'size' bytes at its tail end,
173  * reallocating and copying its data if necessary.  Its headroom, if any, is
174  * preserved. */
175 void
176 ofpbuf_prealloc_tailroom(struct ofpbuf *b, size_t size)
177 {
178     if (size > ofpbuf_tailroom(b)) {
179         ofpbuf_resize_tailroom__(b, MAX(size, 64));
180     }
181 }
182
183 void
184 ofpbuf_prealloc_headroom(struct ofpbuf *b, size_t size)
185 {
186     assert(size <= ofpbuf_headroom(b));
187 }
188
189 /* Trims the size of 'b' to fit its actual content, reducing its tailroom to
190  * 0.  Its headroom, if any, is preserved. */
191 void
192 ofpbuf_trim(struct ofpbuf *b)
193 {
194     if (ofpbuf_tailroom(b) > 0) {
195         ofpbuf_resize_tailroom__(b, 0);
196     }
197 }
198
199 /* Appends 'size' bytes of data to the tail end of 'b', reallocating and
200  * copying its data if necessary.  Returns a pointer to the first byte of the
201  * new data, which is left uninitialized. */
202 void *
203 ofpbuf_put_uninit(struct ofpbuf *b, size_t size)
204 {
205     void *p;
206     ofpbuf_prealloc_tailroom(b, size);
207     p = ofpbuf_tail(b);
208     b->size += size;
209     return p;
210 }
211
212 /* Appends 'size' zeroed bytes to the tail end of 'b'.  Data in 'b' is
213  * reallocated and copied if necessary.  Returns a pointer to the first byte of
214  * the data's location in the ofpbuf. */
215 void *
216 ofpbuf_put_zeros(struct ofpbuf *b, size_t size)
217 {
218     void *dst = ofpbuf_put_uninit(b, size);
219     memset(dst, 0, size);
220     return dst;
221 }
222
223 /* Appends the 'size' bytes of data in 'p' to the tail end of 'b'.  Data in 'b'
224  * is reallocated and copied if necessary.  Returns a pointer to the first
225  * byte of the data's location in the ofpbuf. */
226 void *
227 ofpbuf_put(struct ofpbuf *b, const void *p, size_t size)
228 {
229     void *dst = ofpbuf_put_uninit(b, size);
230     memcpy(dst, p, size);
231     return dst;
232 }
233
234 /* Reserves 'size' bytes of headroom so that they can be later allocated with
235  * ofpbuf_push_uninit() without reallocating the ofpbuf. */
236 void
237 ofpbuf_reserve(struct ofpbuf *b, size_t size)
238 {
239     assert(!b->size);
240     ofpbuf_prealloc_tailroom(b, size);
241     b->data = (char*)b->data + size;
242 }
243
244 void *
245 ofpbuf_push_uninit(struct ofpbuf *b, size_t size)
246 {
247     ofpbuf_prealloc_headroom(b, size);
248     b->data = (char*)b->data - size;
249     b->size += size;
250     return b->data;
251 }
252
253 /* Prefixes 'size' zeroed bytes to the head end of 'b'.  'b' must have at least
254  * 'size' bytes of headroom.  Returns a pointer to the first byte of the data's
255  * location in the ofpbuf. */
256 void *
257 ofpbuf_push_zeros(struct ofpbuf *b, size_t size)
258 {
259     void *dst = ofpbuf_push_uninit(b, size);
260     memset(dst, 0, size);
261     return dst;
262 }
263
264 void *
265 ofpbuf_push(struct ofpbuf *b, const void *p, size_t size)
266 {
267     void *dst = ofpbuf_push_uninit(b, size);
268     memcpy(dst, p, size);
269     return dst;
270 }
271
272 /* If 'b' contains at least 'offset + size' bytes of data, returns a pointer to
273  * byte 'offset'.  Otherwise, returns a null pointer. */
274 void *
275 ofpbuf_at(const struct ofpbuf *b, size_t offset, size_t size)
276 {
277     return offset + size <= b->size ? (char *) b->data + offset : NULL;
278 }
279
280 /* Returns a pointer to byte 'offset' in 'b', which must contain at least
281  * 'offset + size' bytes of data. */
282 void *
283 ofpbuf_at_assert(const struct ofpbuf *b, size_t offset, size_t size)
284 {
285     assert(offset + size <= b->size);
286     return ((char *) b->data) + offset;
287 }
288
289 /* Returns the byte following the last byte of data in use in 'b'. */
290 void *
291 ofpbuf_tail(const struct ofpbuf *b)
292 {
293     return (char *) b->data + b->size;
294 }
295
296 /* Returns the byte following the last byte allocated for use (but not
297  * necessarily in use) by 'b'. */
298 void *
299 ofpbuf_end(const struct ofpbuf *b)
300 {
301     return (char *) b->base + b->allocated;
302 }
303
304 /* Clears any data from 'b'. */
305 void
306 ofpbuf_clear(struct ofpbuf *b)
307 {
308     b->data = b->base;
309     b->size = 0;
310 }
311
312 /* Removes 'size' bytes from the head end of 'b', which must contain at least
313  * 'size' bytes of data.  Returns the first byte of data removed. */
314 void *
315 ofpbuf_pull(struct ofpbuf *b, size_t size)
316 {
317     void *data = b->data;
318     assert(b->size >= size);
319     b->data = (char*)b->data + size;
320     b->size -= size;
321     return data;
322 }
323
324 /* If 'b' has at least 'size' bytes of data, removes that many bytes from the
325  * head end of 'b' and returns the first byte removed.  Otherwise, returns a
326  * null pointer without modifying 'b'. */
327 void *
328 ofpbuf_try_pull(struct ofpbuf *b, size_t size)
329 {
330     return b->size >= size ? ofpbuf_pull(b, size) : NULL;
331 }
332
333 /* Returns a string that describes some of 'b''s metadata plus a hex dump of up
334  * to 'maxbytes' from the start of the buffer. */
335 char *
336 ofpbuf_to_string(const struct ofpbuf *b, size_t maxbytes)
337 {
338     struct ds s;
339
340     ds_init(&s);
341     ds_put_format(&s, "size=%zu, allocated=%zu, head=%zu, tail=%zu\n",
342                   b->size, b->allocated,
343                   ofpbuf_headroom(b), ofpbuf_tailroom(b));
344     ds_put_hex_dump(&s, b->data, MIN(b->size, maxbytes), 0, false);
345     return ds_cstr(&s);
346 }