Merge changes from citrix branch into master.
[cascardo/ovs.git] / lib / ofpbuf.c
1 /*
2  * Copyright (c) 2008, 2009 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 "util.h"
23
24 /* Initializes 'b' as an empty ofpbuf that contains the 'allocated' bytes of
25  * memory starting at 'base'.
26  *
27  * 'base' should ordinarily be the first byte of a region obtained from
28  * malloc(), but in circumstances where it can be guaranteed that 'b' will
29  * never need to be expanded or freed, it can be a pointer into arbitrary
30  * memory. */
31 void
32 ofpbuf_use(struct ofpbuf *b, void *base, size_t allocated)
33 {
34     b->base = b->data = base;
35     b->allocated = allocated;
36     b->size = 0;
37     b->l2 = b->l3 = b->l4 = b->l7 = NULL;
38     b->next = NULL;
39     b->private = NULL;
40 }
41
42 /* Initializes 'b' as an empty ofpbuf with an initial capacity of 'size'
43  * bytes. */
44 void
45 ofpbuf_init(struct ofpbuf *b, size_t size)
46 {
47     ofpbuf_use(b, size ? xmalloc(size) : NULL, size);
48 }
49
50 /* Frees memory that 'b' points to. */
51 void
52 ofpbuf_uninit(struct ofpbuf *b) 
53 {
54     if (b) {
55         free(b->base);
56     }
57 }
58
59 /* Frees memory that 'b' points to and allocates a new ofpbuf */
60 void
61 ofpbuf_reinit(struct ofpbuf *b, size_t size)
62 {
63     ofpbuf_uninit(b);
64     ofpbuf_init(b, size);
65 }
66
67 /* Creates and returns a new ofpbuf with an initial capacity of 'size'
68  * bytes. */
69 struct ofpbuf *
70 ofpbuf_new(size_t size)
71 {
72     struct ofpbuf *b = xmalloc(sizeof *b);
73     ofpbuf_init(b, size);
74     return b;
75 }
76
77 struct ofpbuf *
78 ofpbuf_clone(const struct ofpbuf *buffer)
79 {
80     return ofpbuf_clone_data(buffer->data, buffer->size);
81 }
82
83 struct ofpbuf *
84 ofpbuf_clone_data(const void *data, size_t size)
85 {
86     struct ofpbuf *b = ofpbuf_new(size);
87     ofpbuf_put(b, data, size);
88     return b;
89 }
90
91 /* Frees memory that 'b' points to, as well as 'b' itself. */
92 void
93 ofpbuf_delete(struct ofpbuf *b) 
94 {
95     if (b) {
96         ofpbuf_uninit(b);
97         free(b);
98     }
99 }
100
101 /* Returns the number of bytes of headroom in 'b', that is, the number of bytes
102  * of unused space in ofpbuf 'b' before the data that is in use.  (Most
103  * commonly, the data in a ofpbuf is at its beginning, and thus the ofpbuf's
104  * headroom is 0.) */
105 size_t
106 ofpbuf_headroom(struct ofpbuf *b) 
107 {
108     return (char*)b->data - (char*)b->base;
109 }
110
111 /* Returns the number of bytes that may be appended to the tail end of ofpbuf
112  * 'b' before the ofpbuf must be reallocated. */
113 size_t
114 ofpbuf_tailroom(struct ofpbuf *b) 
115 {
116     return (char*)ofpbuf_end(b) - (char*)ofpbuf_tail(b);
117 }
118
119 /* Ensures that 'b' has room for at least 'size' bytes at its tail end,
120  * reallocating and copying its data if necessary. */
121 void
122 ofpbuf_prealloc_tailroom(struct ofpbuf *b, size_t size) 
123 {
124     if (size > ofpbuf_tailroom(b)) {
125         size_t new_allocated = b->allocated + MAX(size, 64);
126         void *new_base = xmalloc(new_allocated);
127         uintptr_t base_delta = (char*)new_base - (char*)b->base;
128         memcpy(new_base, b->base, b->allocated);
129         free(b->base);
130         b->base = new_base;
131         b->allocated = new_allocated;
132         b->data = (char*)b->data + base_delta;
133         if (b->l2) {
134             b->l2 = (char*)b->l2 + base_delta;
135         }
136         if (b->l3) {
137             b->l3 = (char*)b->l3 + base_delta;
138         }
139         if (b->l4) {
140             b->l4 = (char*)b->l4 + base_delta;
141         }
142         if (b->l7) {
143             b->l7 = (char*)b->l7 + base_delta;
144         }
145     }
146 }
147
148 void
149 ofpbuf_prealloc_headroom(struct ofpbuf *b, size_t size) 
150 {
151     assert(size <= ofpbuf_headroom(b));
152 }
153
154 /* Trims the size of 'b' to fit its actual content. */
155 void
156 ofpbuf_trim(struct ofpbuf *b)
157 {
158     /* XXX These could be supported, but the current client doesn't care. */
159     assert(b->data == b->base);
160     assert(b->l2 == NULL && b->l3 == NULL && b->l4 == NULL && b->l7 == NULL);
161     if (b->allocated > b->size) {
162         b->base = b->data = xrealloc(b->base, b->size);
163         b->allocated = b->size;
164     }
165 }
166
167 /* Appends 'size' bytes of data to the tail end of 'b', reallocating and
168  * copying its data if necessary.  Returns a pointer to the first byte of the
169  * new data, which is left uninitialized. */
170 void *
171 ofpbuf_put_uninit(struct ofpbuf *b, size_t size) 
172 {
173     void *p;
174     ofpbuf_prealloc_tailroom(b, size);
175     p = ofpbuf_tail(b);
176     b->size += size;
177     return p;
178 }
179
180 /* Appends 'size' zeroed bytes to the tail end of 'b'.  Data in 'b' is
181  * reallocated and copied if necessary.  Returns a pointer to the first byte of
182  * the data's location in the ofpbuf. */
183 void *
184 ofpbuf_put_zeros(struct ofpbuf *b, size_t size)
185 {
186     void *dst = ofpbuf_put_uninit(b, size);
187     memset(dst, 0, size);
188     return dst;
189 }
190
191 /* Appends the 'size' bytes of data in 'p' to the tail end of 'b'.  Data in 'b'
192  * is reallocated and copied if necessary.  Returns a pointer to the first
193  * byte of the data's location in the ofpbuf. */
194 void *
195 ofpbuf_put(struct ofpbuf *b, const void *p, size_t size) 
196 {
197     void *dst = ofpbuf_put_uninit(b, size);
198     memcpy(dst, p, size);
199     return dst;
200 }
201
202 /* Reserves 'size' bytes of headroom so that they can be later allocated with
203  * ofpbuf_push_uninit() without reallocating the ofpbuf. */
204 void
205 ofpbuf_reserve(struct ofpbuf *b, size_t size) 
206 {
207     assert(!b->size);
208     ofpbuf_prealloc_tailroom(b, size);
209     b->data = (char*)b->data + size;
210 }
211
212 void *
213 ofpbuf_push_uninit(struct ofpbuf *b, size_t size) 
214 {
215     ofpbuf_prealloc_headroom(b, size);
216     b->data = (char*)b->data - size;
217     b->size += size;
218     return b->data;
219 }
220
221 void *
222 ofpbuf_push(struct ofpbuf *b, const void *p, size_t size) 
223 {
224     void *dst = ofpbuf_push_uninit(b, size);
225     memcpy(dst, p, size);
226     return dst;
227 }
228
229 /* If 'b' contains at least 'offset + size' bytes of data, returns a pointer to
230  * byte 'offset'.  Otherwise, returns a null pointer. */
231 void *
232 ofpbuf_at(const struct ofpbuf *b, size_t offset, size_t size) 
233 {
234     return offset + size <= b->size ? (char *) b->data + offset : NULL;
235 }
236
237 /* Returns a pointer to byte 'offset' in 'b', which must contain at least
238  * 'offset + size' bytes of data. */
239 void *
240 ofpbuf_at_assert(const struct ofpbuf *b, size_t offset, size_t size) 
241 {
242     assert(offset + size <= b->size);
243     return ((char *) b->data) + offset;
244 }
245
246 /* Returns the byte following the last byte of data in use in 'b'. */
247 void *
248 ofpbuf_tail(const struct ofpbuf *b) 
249 {
250     return (char *) b->data + b->size;
251 }
252
253 /* Returns the byte following the last byte allocated for use (but not
254  * necessarily in use) by 'b'. */
255 void *
256 ofpbuf_end(const struct ofpbuf *b) 
257 {
258     return (char *) b->base + b->allocated;
259 }
260
261 /* Clears any data from 'b'. */
262 void
263 ofpbuf_clear(struct ofpbuf *b) 
264 {
265     b->data = b->base;
266     b->size = 0;
267 }
268
269 /* Removes 'size' bytes from the head end of 'b', which must contain at least
270  * 'size' bytes of data.  Returns the first byte of data removed. */
271 void *
272 ofpbuf_pull(struct ofpbuf *b, size_t size) 
273 {
274     void *data = b->data;
275     assert(b->size >= size);
276     b->data = (char*)b->data + size;
277     b->size -= size;
278     return data;
279 }
280
281 /* If 'b' has at least 'size' bytes of data, removes that many bytes from the
282  * head end of 'b' and returns the first byte removed.  Otherwise, returns a
283  * null pointer without modifying 'b'. */
284 void *
285 ofpbuf_try_pull(struct ofpbuf *b, size_t size) 
286 {
287     return b->size >= size ? ofpbuf_pull(b, size) : NULL;
288 }