ofpbuf: Add ofpbuf_new_with_headroom(), ofpbuf_clone_with_headroom().
[sliver-openvswitch.git] / lib / ofpbuf.h
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 #ifndef OFPBUF_H
18 #define OFPBUF_H 1
19
20 #include <stddef.h>
21
22 #ifdef  __cplusplus
23 extern "C" {
24 #endif
25
26 /* Buffer for holding arbitrary data.  An ofpbuf is automatically reallocated
27  * as necessary if it grows too large for the available memory. */
28 struct ofpbuf {
29     void *base;                 /* First byte of area malloc()'d area. */
30     size_t allocated;           /* Number of bytes allocated. */
31
32     void *data;                 /* First byte actually in use. */
33     size_t size;                /* Number of bytes in use. */
34
35     void *l2;                   /* Link-level header. */
36     void *l3;                   /* Network-level header. */
37     void *l4;                   /* Transport-level header. */
38     void *l7;                   /* Application data. */
39
40     struct ofpbuf *next;        /* Next in a list of ofpbufs. */
41     void *private_p;            /* Private pointer for use by owner. */
42 };
43
44 void ofpbuf_use(struct ofpbuf *, void *, size_t);
45
46 void ofpbuf_init(struct ofpbuf *, size_t);
47 void ofpbuf_uninit(struct ofpbuf *);
48 void ofpbuf_reinit(struct ofpbuf *, size_t);
49
50 struct ofpbuf *ofpbuf_new(size_t);
51 struct ofpbuf *ofpbuf_new_with_headroom(size_t, size_t headroom);
52 struct ofpbuf *ofpbuf_clone(const struct ofpbuf *);
53 struct ofpbuf *ofpbuf_clone_with_headroom(const struct ofpbuf *,
54                                           size_t headroom);
55 struct ofpbuf *ofpbuf_clone_data(const void *, size_t);
56 void ofpbuf_delete(struct ofpbuf *);
57
58 void *ofpbuf_at(const struct ofpbuf *, size_t offset, size_t size);
59 void *ofpbuf_at_assert(const struct ofpbuf *, size_t offset, size_t size);
60 void *ofpbuf_tail(const struct ofpbuf *);
61 void *ofpbuf_end(const struct ofpbuf *);
62
63 void *ofpbuf_put_uninit(struct ofpbuf *, size_t);
64 void *ofpbuf_put_zeros(struct ofpbuf *, size_t);
65 void *ofpbuf_put(struct ofpbuf *, const void *, size_t);
66 void ofpbuf_reserve(struct ofpbuf *, size_t);
67 void *ofpbuf_push_uninit(struct ofpbuf *b, size_t);
68 void *ofpbuf_push_zeros(struct ofpbuf *, size_t);
69 void *ofpbuf_push(struct ofpbuf *b, const void *, size_t);
70
71 size_t ofpbuf_headroom(const struct ofpbuf *);
72 size_t ofpbuf_tailroom(const struct ofpbuf *);
73 void ofpbuf_prealloc_headroom(struct ofpbuf *, size_t);
74 void ofpbuf_prealloc_tailroom(struct ofpbuf *, size_t);
75 void ofpbuf_trim(struct ofpbuf *);
76
77 void ofpbuf_clear(struct ofpbuf *);
78 void *ofpbuf_pull(struct ofpbuf *, size_t);
79 void *ofpbuf_try_pull(struct ofpbuf *, size_t);
80
81 char *ofpbuf_to_string(const struct ofpbuf *, size_t maxbytes);
82
83 #ifdef  __cplusplus
84 }
85 #endif
86
87 #endif /* ofpbuf.h */