2 * Copyright (c) 2008, 2009 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "dynamic-string.h"
25 /* Initializes 'b' as an empty ofpbuf that contains the 'allocated' bytes of
26 * memory starting at 'base'.
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
33 ofpbuf_use(struct ofpbuf *b, void *base, size_t allocated)
35 b->base = b->data = base;
36 b->allocated = allocated;
38 b->l2 = b->l3 = b->l4 = b->l7 = NULL;
43 /* Initializes 'b' as an empty ofpbuf with an initial capacity of 'size'
46 ofpbuf_init(struct ofpbuf *b, size_t size)
48 ofpbuf_use(b, size ? xmalloc(size) : NULL, size);
51 /* Frees memory that 'b' points to. */
53 ofpbuf_uninit(struct ofpbuf *b)
60 /* Frees memory that 'b' points to and allocates a new ofpbuf */
62 ofpbuf_reinit(struct ofpbuf *b, size_t size)
68 /* Creates and returns a new ofpbuf with an initial capacity of 'size'
71 ofpbuf_new(size_t size)
73 struct ofpbuf *b = xmalloc(sizeof *b);
79 ofpbuf_clone(const struct ofpbuf *buffer)
81 return ofpbuf_clone_data(buffer->data, buffer->size);
85 ofpbuf_clone_data(const void *data, size_t size)
87 struct ofpbuf *b = ofpbuf_new(size);
88 ofpbuf_put(b, data, size);
92 /* Frees memory that 'b' points to, as well as 'b' itself. */
94 ofpbuf_delete(struct ofpbuf *b)
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
107 ofpbuf_headroom(const struct ofpbuf *b)
109 return (char*)b->data - (char*)b->base;
112 /* Returns the number of bytes that may be appended to the tail end of ofpbuf
113 * 'b' before the ofpbuf must be reallocated. */
115 ofpbuf_tailroom(const struct ofpbuf *b)
117 return (char*)ofpbuf_end(b) - (char*)ofpbuf_tail(b);
120 /* Ensures that 'b' has room for at least 'size' bytes at its tail end,
121 * reallocating and copying its data if necessary. */
123 ofpbuf_prealloc_tailroom(struct ofpbuf *b, size_t size)
125 if (size > ofpbuf_tailroom(b)) {
126 size_t new_allocated = b->allocated + MAX(size, 64);
127 void *new_base = xmalloc(new_allocated);
128 uintptr_t base_delta = (char*)new_base - (char*)b->base;
129 memcpy(new_base, b->base, b->allocated);
132 b->allocated = new_allocated;
133 b->data = (char*)b->data + base_delta;
135 b->l2 = (char*)b->l2 + base_delta;
138 b->l3 = (char*)b->l3 + base_delta;
141 b->l4 = (char*)b->l4 + base_delta;
144 b->l7 = (char*)b->l7 + base_delta;
150 ofpbuf_prealloc_headroom(struct ofpbuf *b, size_t size)
152 assert(size <= ofpbuf_headroom(b));
155 /* Trims the size of 'b' to fit its actual content. */
157 ofpbuf_trim(struct ofpbuf *b)
159 /* XXX These could be supported, but the current client doesn't care. */
160 assert(b->data == b->base);
161 assert(b->l2 == NULL && b->l3 == NULL && b->l4 == NULL && b->l7 == NULL);
162 if (b->allocated > b->size) {
163 b->base = b->data = xrealloc(b->base, b->size);
164 b->allocated = b->size;
168 /* Appends 'size' bytes of data to the tail end of 'b', reallocating and
169 * copying its data if necessary. Returns a pointer to the first byte of the
170 * new data, which is left uninitialized. */
172 ofpbuf_put_uninit(struct ofpbuf *b, size_t size)
175 ofpbuf_prealloc_tailroom(b, size);
181 /* Appends 'size' zeroed bytes to the tail end of 'b'. Data in 'b' is
182 * reallocated and copied if necessary. Returns a pointer to the first byte of
183 * the data's location in the ofpbuf. */
185 ofpbuf_put_zeros(struct ofpbuf *b, size_t size)
187 void *dst = ofpbuf_put_uninit(b, size);
188 memset(dst, 0, size);
192 /* Appends the 'size' bytes of data in 'p' to the tail end of 'b'. Data in 'b'
193 * is reallocated and copied if necessary. Returns a pointer to the first
194 * byte of the data's location in the ofpbuf. */
196 ofpbuf_put(struct ofpbuf *b, const void *p, size_t size)
198 void *dst = ofpbuf_put_uninit(b, size);
199 memcpy(dst, p, size);
203 /* Reserves 'size' bytes of headroom so that they can be later allocated with
204 * ofpbuf_push_uninit() without reallocating the ofpbuf. */
206 ofpbuf_reserve(struct ofpbuf *b, size_t size)
209 ofpbuf_prealloc_tailroom(b, size);
210 b->data = (char*)b->data + size;
214 ofpbuf_push_uninit(struct ofpbuf *b, size_t size)
216 ofpbuf_prealloc_headroom(b, size);
217 b->data = (char*)b->data - size;
223 ofpbuf_push(struct ofpbuf *b, const void *p, size_t size)
225 void *dst = ofpbuf_push_uninit(b, size);
226 memcpy(dst, p, size);
230 /* If 'b' contains at least 'offset + size' bytes of data, returns a pointer to
231 * byte 'offset'. Otherwise, returns a null pointer. */
233 ofpbuf_at(const struct ofpbuf *b, size_t offset, size_t size)
235 return offset + size <= b->size ? (char *) b->data + offset : NULL;
238 /* Returns a pointer to byte 'offset' in 'b', which must contain at least
239 * 'offset + size' bytes of data. */
241 ofpbuf_at_assert(const struct ofpbuf *b, size_t offset, size_t size)
243 assert(offset + size <= b->size);
244 return ((char *) b->data) + offset;
247 /* Returns the byte following the last byte of data in use in 'b'. */
249 ofpbuf_tail(const struct ofpbuf *b)
251 return (char *) b->data + b->size;
254 /* Returns the byte following the last byte allocated for use (but not
255 * necessarily in use) by 'b'. */
257 ofpbuf_end(const struct ofpbuf *b)
259 return (char *) b->base + b->allocated;
262 /* Clears any data from 'b'. */
264 ofpbuf_clear(struct ofpbuf *b)
270 /* Removes 'size' bytes from the head end of 'b', which must contain at least
271 * 'size' bytes of data. Returns the first byte of data removed. */
273 ofpbuf_pull(struct ofpbuf *b, size_t size)
275 void *data = b->data;
276 assert(b->size >= size);
277 b->data = (char*)b->data + size;
282 /* If 'b' has at least 'size' bytes of data, removes that many bytes from the
283 * head end of 'b' and returns the first byte removed. Otherwise, returns a
284 * null pointer without modifying 'b'. */
286 ofpbuf_try_pull(struct ofpbuf *b, size_t size)
288 return b->size >= size ? ofpbuf_pull(b, size) : NULL;
291 /* Returns a string that describes some of 'b''s metadata plus a hex dump of up
292 * to 'maxbytes' from the start of the buffer. */
294 ofpbuf_to_string(const struct ofpbuf *b, size_t maxbytes)
299 ds_put_format(&s, "size=%zu, allocated=%zu, head=%zu, tail=%zu\n",
300 b->size, b->allocated,
301 ofpbuf_headroom(b), ofpbuf_tailroom(b));
302 ds_put_hex_dump(&s, b->data, MIN(b->size, maxbytes), 0, false);