Initial import
[sliver-openvswitch.git] / include / buffer.h
1 /* Copyright (C) 2007 Board of Trustees, Leland Stanford Jr. University.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE.
20  */
21
22 #ifndef BUFFER_H
23 #define BUFFER_H 1
24
25 #include <stddef.h>
26
27 /* Buffer for holding arbitrary data.  A buffer is automatically reallocated as
28  * necessary if it grows too large for the available memory. */
29 struct buffer {
30     void *base;                 /* First byte of area malloc()'d area. */
31     size_t allocated;           /* Number of bytes allocated. */
32
33     void *data;                 /* First byte actually in use. */
34     size_t size;                /* Number of bytes in use. */
35
36     struct buffer *next;        /* Next in a list of buffers. */
37 };
38
39 void buffer_use(struct buffer *, void *, size_t);
40
41 void buffer_init(struct buffer *, size_t);
42 void buffer_uninit(struct buffer *);
43 void buffer_reinit(struct buffer *, size_t);
44
45 struct buffer *buffer_new(size_t);
46 void buffer_delete(struct buffer *);
47
48 void *buffer_at(const struct buffer *, size_t offset, size_t size);
49 void *buffer_at_assert(const struct buffer *, size_t offset, size_t size);
50 void *buffer_tail(const struct buffer *);
51 void *buffer_end(const struct buffer *);
52
53 void *buffer_put_uninit(struct buffer *, size_t);
54 void buffer_put(struct buffer *, const void *, size_t);
55
56 size_t buffer_headroom(struct buffer *);
57 size_t buffer_tailroom(struct buffer *);
58 void buffer_reserve_tailroom(struct buffer *, size_t);
59
60 void buffer_clear(struct buffer *);
61 void buffer_pull(struct buffer *, size_t);
62
63 #endif /* buffer.h */