2 * Copyright (c) 2009-2010 The FreeBSD Foundation
5 * This software was developed by Pawel Jakub Dawidek under sponsorship from
6 * the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
47 #define PJDLOG_ASSERT(...) assert(__VA_ARGS__)
50 #define EBUF_MAGIC 0xeb0f41c
52 /* Magic to assert the caller uses valid structure. */
54 /* Address where we did the allocation. */
55 unsigned char *eb_start;
56 /* Allocation end address. */
57 unsigned char *eb_end;
58 /* Start of real data. */
59 unsigned char *eb_used;
60 /* Size of real data. */
64 static int ebuf_head_extend(struct ebuf *eb, size_t size);
65 static int ebuf_tail_extend(struct ebuf *eb, size_t size);
68 ebuf_alloc(size_t size)
73 eb = malloc(sizeof(*eb));
77 eb->eb_start = malloc(size);
78 if (eb->eb_start == NULL) {
84 eb->eb_end = eb->eb_start + size;
86 * We set start address for real data not at the first entry, because
87 * we want to be able to add data at the front.
89 eb->eb_used = eb->eb_start + PAGE_SIZE / 4;
91 eb->eb_magic = EBUF_MAGIC;
97 ebuf_free(struct ebuf *eb)
100 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
109 ebuf_add_head(struct ebuf *eb, const void *data, size_t size)
112 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
114 if (size > (size_t)(eb->eb_used - eb->eb_start)) {
116 * We can't add more entries at the front, so we have to extend
119 if (ebuf_head_extend(eb, size) == -1)
122 PJDLOG_ASSERT(size <= (size_t)(eb->eb_used - eb->eb_start));
127 * If data is NULL the caller just wants to reserve place.
130 bcopy(data, eb->eb_used, size);
136 ebuf_add_tail(struct ebuf *eb, const void *data, size_t size)
139 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
141 if (size > (size_t)(eb->eb_end - (eb->eb_used + eb->eb_size))) {
143 * We can't add more entries at the back, so we have to extend
146 if (ebuf_tail_extend(eb, size) == -1)
149 PJDLOG_ASSERT(size <=
150 (size_t)(eb->eb_end - (eb->eb_used + eb->eb_size)));
153 * If data is NULL the caller just wants to reserve space.
156 bcopy(data, eb->eb_used + eb->eb_size, size);
163 ebuf_del_head(struct ebuf *eb, size_t size)
166 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
167 PJDLOG_ASSERT(size <= eb->eb_size);
174 ebuf_del_tail(struct ebuf *eb, size_t size)
177 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
178 PJDLOG_ASSERT(size <= eb->eb_size);
184 * Return pointer to the data and data size.
187 ebuf_data(struct ebuf *eb, size_t *sizep)
190 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
193 *sizep = eb->eb_size;
194 return (eb->eb_size > 0 ? eb->eb_used : NULL);
201 ebuf_size(struct ebuf *eb)
204 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
206 return (eb->eb_size);
210 * Function adds size + (PAGE_SIZE / 4) bytes at the front of the buffer..
213 ebuf_head_extend(struct ebuf *eb, size_t size)
215 unsigned char *newstart, *newused;
218 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
220 newsize = eb->eb_end - eb->eb_start + (PAGE_SIZE / 4) + size;
222 newstart = malloc(newsize);
223 if (newstart == NULL)
226 newstart + (PAGE_SIZE / 4) + size + (eb->eb_used - eb->eb_start);
228 bcopy(eb->eb_used, newused, eb->eb_size);
230 eb->eb_start = newstart;
231 eb->eb_used = newused;
232 eb->eb_end = newstart + newsize;
238 * Function adds size + ((3 * PAGE_SIZE) / 4) bytes at the back.
241 ebuf_tail_extend(struct ebuf *eb, size_t size)
243 unsigned char *newstart;
246 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
248 newsize = eb->eb_end - eb->eb_start + size + ((3 * PAGE_SIZE) / 4);
250 newstart = realloc(eb->eb_start, newsize);
251 if (newstart == NULL)
254 eb->eb_used = newstart + (eb->eb_used - eb->eb_start);
255 eb->eb_start = newstart;
256 eb->eb_end = newstart + newsize;