e9ad400ceaec31520a1217b7fdd5279ddc5954cc
[sliver-openvswitch.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 struct ofpbuf *
79 ofpbuf_clone(const struct ofpbuf *buffer)
80 {
81     return ofpbuf_clone_data(buffer->data, buffer->size);
82 }
83
84 struct ofpbuf *
85 ofpbuf_clone_data(const void *data, size_t size)
86 {
87     struct ofpbuf *b = ofpbuf_new(size);
88     ofpbuf_put(b, data, size);
89     return b;
90 }
91
92 /* Frees memory that 'b' points to, as well as 'b' itself. */
93 void
94 ofpbuf_delete(struct ofpbuf *b) 
95 {
96     if (b) {
97         ofpbuf_uninit(b);
98         free(b);
99     }
100 }
101
102 /* Returns the number of bytes of headroom in 'b', that is, the number of bytes
103  * of unused space in ofpbuf 'b' before the data that is in use.  (Most
104  * commonly, the data in a ofpbuf is at its beginning, and thus the ofpbuf's
105  * headroom is 0.) */
106 size_t
107 ofpbuf_headroom(const struct ofpbuf *b)
108 {
109     return (char*)b->data - (char*)b->base;
110 }
111
112 /* Returns the number of bytes that may be appended to the tail end of ofpbuf
113  * 'b' before the ofpbuf must be reallocated. */
114 size_t
115 ofpbuf_tailroom(const struct ofpbuf *b)
116 {
117     return (char*)ofpbuf_end(b) - (char*)ofpbuf_tail(b);
118 }
119
120 /* Changes 'b->base' to 'new_base' and adjusts all of 'b''s internal pointers
121  * to reflect the change. */
122 static void
123 ofpbuf_rebase__(struct ofpbuf *b, void *new_base)
124 {
125     if (b->base != new_base) {
126         uintptr_t base_delta = (char*)new_base - (char*)b->base;
127         b->base = new_base;
128         b->data = (char*)b->data + base_delta;
129         if (b->l2) {
130             b->l2 = (char*)b->l2 + base_delta;
131         }
132         if (b->l3) {
133             b->l3 = (char*)b->l3 + base_delta;
134         }
135         if (b->l4) {
136             b->l4 = (char*)b->l4 + base_delta;
137         }
138         if (b->l7) {
139             b->l7 = (char*)b->l7 + base_delta;
140         }
141     }
142 }
143
144 /* Reallocates 'b' so that it has exactly 'new_tailroom' bytes of tailroom. */
145 static void
146 ofpbuf_resize_tailroom__(struct ofpbuf *b, size_t new_tailroom)
147 {
148     b->allocated = ofpbuf_headroom(b) + b->size + new_tailroom;
149     ofpbuf_rebase__(b, xrealloc(b->base, b->allocated));
150 }
151
152 /* Ensures that 'b' has room for at least 'size' bytes at its tail end,
153  * reallocating and copying its data if necessary.  Its headroom, if any, is
154  * preserved. */
155 void
156 ofpbuf_prealloc_tailroom(struct ofpbuf *b, size_t size) 
157 {
158     if (size > ofpbuf_tailroom(b)) {
159         ofpbuf_resize_tailroom__(b, MAX(size, 64));
160     }
161 }
162
163 void
164 ofpbuf_prealloc_headroom(struct ofpbuf *b, size_t size) 
165 {
166     assert(size <= ofpbuf_headroom(b));
167 }
168
169 /* Trims the size of 'b' to fit its actual content, reducing its tailroom to
170  * 0.  Its headroom, if any, is preserved. */
171 void
172 ofpbuf_trim(struct ofpbuf *b)
173 {
174     if (ofpbuf_tailroom(b) > 0) {
175         ofpbuf_resize_tailroom__(b, 0);
176     }
177 }
178
179 /* Appends 'size' bytes of data to the tail end of 'b', reallocating and
180  * copying its data if necessary.  Returns a pointer to the first byte of the
181  * new data, which is left uninitialized. */
182 void *
183 ofpbuf_put_uninit(struct ofpbuf *b, size_t size) 
184 {
185     void *p;
186     ofpbuf_prealloc_tailroom(b, size);
187     p = ofpbuf_tail(b);
188     b->size += size;
189     return p;
190 }
191
192 /* Appends 'size' zeroed bytes to the tail end of 'b'.  Data in 'b' is
193  * reallocated and copied if necessary.  Returns a pointer to the first byte of
194  * the data's location in the ofpbuf. */
195 void *
196 ofpbuf_put_zeros(struct ofpbuf *b, size_t size)
197 {
198     void *dst = ofpbuf_put_uninit(b, size);
199     memset(dst, 0, size);
200     return dst;
201 }
202
203 /* Appends the 'size' bytes of data in 'p' to the tail end of 'b'.  Data in 'b'
204  * is reallocated and copied if necessary.  Returns a pointer to the first
205  * byte of the data's location in the ofpbuf. */
206 void *
207 ofpbuf_put(struct ofpbuf *b, const void *p, size_t size) 
208 {
209     void *dst = ofpbuf_put_uninit(b, size);
210     memcpy(dst, p, size);
211     return dst;
212 }
213
214 /* Reserves 'size' bytes of headroom so that they can be later allocated with
215  * ofpbuf_push_uninit() without reallocating the ofpbuf. */
216 void
217 ofpbuf_reserve(struct ofpbuf *b, size_t size) 
218 {
219     assert(!b->size);
220     ofpbuf_prealloc_tailroom(b, size);
221     b->data = (char*)b->data + size;
222 }
223
224 void *
225 ofpbuf_push_uninit(struct ofpbuf *b, size_t size) 
226 {
227     ofpbuf_prealloc_headroom(b, size);
228     b->data = (char*)b->data - size;
229     b->size += size;
230     return b->data;
231 }
232
233 /* Prefixes 'size' zeroed bytes to the head end of 'b'.  'b' must have at least
234  * 'size' bytes of headroom.  Returns a pointer to the first byte of the data's
235  * location in the ofpbuf. */
236 void *
237 ofpbuf_push_zeros(struct ofpbuf *b, size_t size)
238 {
239     void *dst = ofpbuf_push_uninit(b, size);
240     memset(dst, 0, size);
241     return dst;
242 }
243
244 void *
245 ofpbuf_push(struct ofpbuf *b, const void *p, size_t size) 
246 {
247     void *dst = ofpbuf_push_uninit(b, size);
248     memcpy(dst, p, size);
249     return dst;
250 }
251
252 /* If 'b' contains at least 'offset + size' bytes of data, returns a pointer to
253  * byte 'offset'.  Otherwise, returns a null pointer. */
254 void *
255 ofpbuf_at(const struct ofpbuf *b, size_t offset, size_t size) 
256 {
257     return offset + size <= b->size ? (char *) b->data + offset : NULL;
258 }
259
260 /* Returns a pointer to byte 'offset' in 'b', which must contain at least
261  * 'offset + size' bytes of data. */
262 void *
263 ofpbuf_at_assert(const struct ofpbuf *b, size_t offset, size_t size) 
264 {
265     assert(offset + size <= b->size);
266     return ((char *) b->data) + offset;
267 }
268
269 /* Returns the byte following the last byte of data in use in 'b'. */
270 void *
271 ofpbuf_tail(const struct ofpbuf *b) 
272 {
273     return (char *) b->data + b->size;
274 }
275
276 /* Returns the byte following the last byte allocated for use (but not
277  * necessarily in use) by 'b'. */
278 void *
279 ofpbuf_end(const struct ofpbuf *b) 
280 {
281     return (char *) b->base + b->allocated;
282 }
283
284 /* Clears any data from 'b'. */
285 void
286 ofpbuf_clear(struct ofpbuf *b) 
287 {
288     b->data = b->base;
289     b->size = 0;
290 }
291
292 /* Removes 'size' bytes from the head end of 'b', which must contain at least
293  * 'size' bytes of data.  Returns the first byte of data removed. */
294 void *
295 ofpbuf_pull(struct ofpbuf *b, size_t size) 
296 {
297     void *data = b->data;
298     assert(b->size >= size);
299     b->data = (char*)b->data + size;
300     b->size -= size;
301     return data;
302 }
303
304 /* If 'b' has at least 'size' bytes of data, removes that many bytes from the
305  * head end of 'b' and returns the first byte removed.  Otherwise, returns a
306  * null pointer without modifying 'b'. */
307 void *
308 ofpbuf_try_pull(struct ofpbuf *b, size_t size) 
309 {
310     return b->size >= size ? ofpbuf_pull(b, size) : NULL;
311 }
312
313 /* Returns a string that describes some of 'b''s metadata plus a hex dump of up
314  * to 'maxbytes' from the start of the buffer. */
315 char *
316 ofpbuf_to_string(const struct ofpbuf *b, size_t maxbytes)
317 {
318     struct ds s;
319
320     ds_init(&s);
321     ds_put_format(&s, "size=%zu, allocated=%zu, head=%zu, tail=%zu\n",
322                   b->size, b->allocated,
323                   ofpbuf_headroom(b), ofpbuf_tailroom(b));
324     ds_put_hex_dump(&s, b->data, MIN(b->size, maxbytes), 0, false);
325     return ds_cstr(&s);
326 }