Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc2ad45.
[cascardo/ovs.git] / lib / ofpbuf.h
1 /*
2  * Copyright (c) 2008, 2009 Nicira Networks.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #ifndef OFPBUF_H
18 #define OFPBUF_H 1
19
20 #include <stddef.h>
21
22 /* Buffer for holding arbitrary data.  An ofpbuf is automatically reallocated
23  * as necessary if it grows too large for the available memory. */
24 struct ofpbuf {
25     void *base;                 /* First byte of area malloc()'d area. */
26     size_t allocated;           /* Number of bytes allocated. */
27
28     void *data;                 /* First byte actually in use. */
29     size_t size;                /* Number of bytes in use. */
30
31     void *l2;                   /* Link-level header. */
32     void *l3;                   /* Network-level header. */
33     void *l4;                   /* Transport-level header. */
34     void *l7;                   /* Application data. */
35
36     struct ofpbuf *next;        /* Next in a list of ofpbufs. */
37     void *private;              /* Private pointer for use by owner. */
38 };
39
40 void ofpbuf_use(struct ofpbuf *, void *, size_t);
41
42 void ofpbuf_init(struct ofpbuf *, size_t);
43 void ofpbuf_uninit(struct ofpbuf *);
44 void ofpbuf_reinit(struct ofpbuf *, size_t);
45
46 struct ofpbuf *ofpbuf_new(size_t);
47 struct ofpbuf *ofpbuf_clone(const struct ofpbuf *);
48 struct ofpbuf *ofpbuf_clone_data(const void *, size_t);
49 void ofpbuf_delete(struct ofpbuf *);
50
51 void *ofpbuf_at(const struct ofpbuf *, size_t offset, size_t size);
52 void *ofpbuf_at_assert(const struct ofpbuf *, size_t offset, size_t size);
53 void *ofpbuf_tail(const struct ofpbuf *);
54 void *ofpbuf_end(const struct ofpbuf *);
55
56 void *ofpbuf_put_uninit(struct ofpbuf *, size_t);
57 void *ofpbuf_put_zeros(struct ofpbuf *, size_t);
58 void *ofpbuf_put(struct ofpbuf *, const void *, size_t);
59 void ofpbuf_reserve(struct ofpbuf *, size_t);
60 void *ofpbuf_push_uninit(struct ofpbuf *b, size_t);
61 void *ofpbuf_push(struct ofpbuf *b, const void *, size_t);
62
63 size_t ofpbuf_headroom(struct ofpbuf *);
64 size_t ofpbuf_tailroom(struct ofpbuf *);
65 void ofpbuf_prealloc_headroom(struct ofpbuf *, size_t);
66 void ofpbuf_prealloc_tailroom(struct ofpbuf *, size_t);
67 void ofpbuf_trim(struct ofpbuf *);
68
69 void ofpbuf_clear(struct ofpbuf *);
70 void *ofpbuf_pull(struct ofpbuf *, size_t);
71 void *ofpbuf_try_pull(struct ofpbuf *, size_t);
72
73 #endif /* ofpbuf.h */