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)
76 eb = malloc(sizeof(*eb));
79 page_size = getpagesize();
81 eb->eb_start = malloc(size);
82 if (eb->eb_start == NULL) {
88 eb->eb_end = eb->eb_start + size;
90 * We set start address for real data not at the first entry, because
91 * we want to be able to add data at the front.
93 eb->eb_used = eb->eb_start + page_size / 4;
95 eb->eb_magic = EBUF_MAGIC;
101 ebuf_free(struct ebuf *eb)
104 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
113 ebuf_add_head(struct ebuf *eb, const void *data, size_t size)
116 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
118 if (size > (size_t)(eb->eb_used - eb->eb_start)) {
120 * We can't add more entries at the front, so we have to extend
123 if (ebuf_head_extend(eb, size) == -1)
126 PJDLOG_ASSERT(size <= (size_t)(eb->eb_used - eb->eb_start));
131 * If data is NULL the caller just wants to reserve place.
134 bcopy(data, eb->eb_used, size);
140 ebuf_add_tail(struct ebuf *eb, const void *data, size_t size)
143 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
145 if (size > (size_t)(eb->eb_end - (eb->eb_used + eb->eb_size))) {
147 * We can't add more entries at the back, so we have to extend
150 if (ebuf_tail_extend(eb, size) == -1)
153 PJDLOG_ASSERT(size <=
154 (size_t)(eb->eb_end - (eb->eb_used + eb->eb_size)));
157 * If data is NULL the caller just wants to reserve space.
160 bcopy(data, eb->eb_used + eb->eb_size, size);
167 ebuf_del_head(struct ebuf *eb, size_t size)
170 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
171 PJDLOG_ASSERT(size <= eb->eb_size);
178 ebuf_del_tail(struct ebuf *eb, size_t size)
181 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
182 PJDLOG_ASSERT(size <= eb->eb_size);
188 * Return pointer to the data and data size.
191 ebuf_data(struct ebuf *eb, size_t *sizep)
194 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
197 *sizep = eb->eb_size;
198 return (eb->eb_size > 0 ? eb->eb_used : NULL);
205 ebuf_size(struct ebuf *eb)
208 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
210 return (eb->eb_size);
214 * Function adds size + (PAGE_SIZE / 4) bytes at the front of the buffer..
217 ebuf_head_extend(struct ebuf *eb, size_t size)
219 unsigned char *newstart, *newused;
220 size_t newsize, page_size;
222 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
224 page_size = getpagesize();
225 newsize = eb->eb_end - eb->eb_start + (page_size / 4) + size;
227 newstart = malloc(newsize);
228 if (newstart == NULL)
231 newstart + (page_size / 4) + size + (eb->eb_used - eb->eb_start);
233 bcopy(eb->eb_used, newused, eb->eb_size);
235 eb->eb_start = newstart;
236 eb->eb_used = newused;
237 eb->eb_end = newstart + newsize;
243 * Function adds size + ((3 * PAGE_SIZE) / 4) bytes at the back.
246 ebuf_tail_extend(struct ebuf *eb, size_t size)
248 unsigned char *newstart;
249 size_t newsize, page_size;
251 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
253 page_size = getpagesize();
254 newsize = eb->eb_end - eb->eb_start + size + ((3 * page_size) / 4);
256 newstart = realloc(eb->eb_start, newsize);
257 if (newstart == NULL)
260 eb->eb_used = newstart + (eb->eb_used - eb->eb_start);
261 eb->eb_start = newstart;
262 eb->eb_end = newstart + newsize;