2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009-2010 The FreeBSD Foundation
7 * This software was developed by Pawel Jakub Dawidek under sponsorship from
8 * the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
49 #define PJDLOG_ASSERT(...) assert(__VA_ARGS__)
52 #define EBUF_MAGIC 0xeb0f41c
54 /* Magic to assert the caller uses valid structure. */
56 /* Address where we did the allocation. */
57 unsigned char *eb_start;
58 /* Allocation end address. */
59 unsigned char *eb_end;
60 /* Start of real data. */
61 unsigned char *eb_used;
62 /* Size of real data. */
66 static int ebuf_head_extend(struct ebuf *eb, size_t size);
67 static int ebuf_tail_extend(struct ebuf *eb, size_t size);
70 ebuf_alloc(size_t size)
75 eb = malloc(sizeof(*eb));
79 eb->eb_start = malloc(size);
80 if (eb->eb_start == NULL) {
86 eb->eb_end = eb->eb_start + size;
88 * We set start address for real data not at the first entry, because
89 * we want to be able to add data at the front.
91 eb->eb_used = eb->eb_start + PAGE_SIZE / 4;
93 eb->eb_magic = EBUF_MAGIC;
99 ebuf_free(struct ebuf *eb)
102 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
111 ebuf_add_head(struct ebuf *eb, const void *data, size_t size)
114 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
116 if (size > (size_t)(eb->eb_used - eb->eb_start)) {
118 * We can't add more entries at the front, so we have to extend
121 if (ebuf_head_extend(eb, size) == -1)
124 PJDLOG_ASSERT(size <= (size_t)(eb->eb_used - eb->eb_start));
129 * If data is NULL the caller just wants to reserve place.
132 bcopy(data, eb->eb_used, size);
138 ebuf_add_tail(struct ebuf *eb, const void *data, size_t size)
141 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
143 if (size > (size_t)(eb->eb_end - (eb->eb_used + eb->eb_size))) {
145 * We can't add more entries at the back, so we have to extend
148 if (ebuf_tail_extend(eb, size) == -1)
151 PJDLOG_ASSERT(size <=
152 (size_t)(eb->eb_end - (eb->eb_used + eb->eb_size)));
155 * If data is NULL the caller just wants to reserve space.
158 bcopy(data, eb->eb_used + eb->eb_size, size);
165 ebuf_del_head(struct ebuf *eb, size_t size)
168 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
169 PJDLOG_ASSERT(size <= eb->eb_size);
176 ebuf_del_tail(struct ebuf *eb, size_t size)
179 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
180 PJDLOG_ASSERT(size <= eb->eb_size);
186 * Return pointer to the data and data size.
189 ebuf_data(struct ebuf *eb, size_t *sizep)
192 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
195 *sizep = eb->eb_size;
196 return (eb->eb_size > 0 ? eb->eb_used : NULL);
203 ebuf_size(struct ebuf *eb)
206 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
208 return (eb->eb_size);
212 * Function adds size + (PAGE_SIZE / 4) bytes at the front of the buffer..
215 ebuf_head_extend(struct ebuf *eb, size_t size)
217 unsigned char *newstart, *newused;
220 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
222 newsize = eb->eb_end - eb->eb_start + (PAGE_SIZE / 4) + size;
224 newstart = malloc(newsize);
225 if (newstart == NULL)
228 newstart + (PAGE_SIZE / 4) + size + (eb->eb_used - eb->eb_start);
230 bcopy(eb->eb_used, newused, eb->eb_size);
232 eb->eb_start = newstart;
233 eb->eb_used = newused;
234 eb->eb_end = newstart + newsize;
240 * Function adds size + ((3 * PAGE_SIZE) / 4) bytes at the back.
243 ebuf_tail_extend(struct ebuf *eb, size_t size)
245 unsigned char *newstart;
248 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
250 newsize = eb->eb_end - eb->eb_start + size + ((3 * PAGE_SIZE) / 4);
252 newstart = realloc(eb->eb_start, newsize);
253 if (newstart == NULL)
256 eb->eb_used = newstart + (eb->eb_used - eb->eb_start);
257 eb->eb_start = newstart;
258 eb->eb_end = newstart + newsize;