Loading...
Searching...
No Matches
mbuf.h
1/*
2 * Copyright (c) 2014-2018 Cesanta Software Limited
3 * All rights reserved
4 *
5 * Licensed under the Apache License, Version 2.0 (the ""License"");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an ""AS IS"" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/*
19 * Mbufs are mutable/growing memory buffers, like C++ strings.
20 * Mbuf can append data to the end of a buffer or insert data into arbitrary
21 * position in the middle of a buffer. The buffer grows automatically when
22 * needed.
23 */
24
25#ifndef CS_COMMON_MBUF_H_
26#define CS_COMMON_MBUF_H_
27
28#include <stdlib.h>
29#include "platform.h"
30
31#if defined(__cplusplus)
32extern "C" {
33#endif
34
35#ifndef MBUF_SIZE_MULTIPLIER
36#define MBUF_SIZE_MULTIPLIER 1.5
37#endif
38
39#ifndef MBUF_SIZE_MAX_HEADROOM
40#ifdef BUFSIZ
41#define MBUF_SIZE_MAX_HEADROOM BUFSIZ
42#else
43#define MBUF_SIZE_MAX_HEADROOM 1024
44#endif
45#endif
46
47/* Memory buffer descriptor */
48struct mbuf {
49 char *buf; /* Buffer pointer */
50 size_t len; /* Data length. Data is located between offset 0 and len. */
51 size_t size; /* Buffer size allocated by realloc(1). Must be >= len */
52};
53
54/*
55 * Initialises an Mbuf.
56 * `initial_capacity` specifies the initial capacity of the mbuf.
57 */
58void mbuf_init(struct mbuf *, size_t initial_capacity);
59
60/* Frees the space allocated for the mbuffer and resets the mbuf structure. */
61void mbuf_free(struct mbuf *);
62
63/*
64 * Appends data to the Mbuf.
65 *
66 * Returns the number of bytes appended or 0 if out of memory.
67 */
68size_t mbuf_append(struct mbuf *, const void *data, size_t data_size);
69
70/*
71 * Appends data to the Mbuf and frees it (data must be heap-allocated).
72 *
73 * Returns the number of bytes appended or 0 if out of memory.
74 * data is freed irrespective of return value.
75 */
76size_t mbuf_append_and_free(struct mbuf *, void *data, size_t data_size);
77
78/*
79 * Inserts data at a specified offset in the Mbuf.
80 *
81 * Existing data will be shifted forwards and the buffer will
82 * be grown if necessary.
83 * Returns the number of bytes inserted.
84 */
85size_t mbuf_insert(struct mbuf *, size_t, const void *, size_t);
86
87/* Removes `data_size` bytes from the beginning of the buffer. */
88void mbuf_remove(struct mbuf *, size_t data_size);
89
90/*
91 * Resizes an Mbuf.
92 *
93 * If `new_size` is smaller than buffer's `len`, the
94 * resize is not performed.
95 */
96void mbuf_resize(struct mbuf *, size_t new_size);
97
98/* Moves the state from one mbuf to the other. */
99void mbuf_move(struct mbuf *from, struct mbuf *to);
100
101/* Removes all the data from mbuf (if any). */
102void mbuf_clear(struct mbuf *);
103
104/* Shrinks an Mbuf by resizing its `size` to `len`. */
105void mbuf_trim(struct mbuf *);
106
107#if defined(__cplusplus)
108}
109#endif /* __cplusplus */
110
111#endif /* CS_COMMON_MBUF_H_ */
Definition mbuf.h:48