ebuf.c revision 225787
153790Sobrien/*-
253790Sobrien * Copyright (c) 2009-2010 The FreeBSD Foundation
353790Sobrien * All rights reserved.
453790Sobrien *
586266Sgroudier * This software was developed by Pawel Jakub Dawidek under sponsorship from
653790Sobrien * the FreeBSD Foundation.
753790Sobrien *
862422Sgroudier * Redistribution and use in source and binary forms, with or without
962422Sgroudier * modification, are permitted provided that the following conditions
1053790Sobrien * are met:
1153790Sobrien * 1. Redistributions of source code must retain the above copyright
1253790Sobrien *    notice, this list of conditions and the following disclaimer.
1353790Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1453790Sobrien *    notice, this list of conditions and the following disclaimer in the
1553790Sobrien *    documentation and/or other materials provided with the distribution.
1653790Sobrien *
1753790Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
1853790Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1953790Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2053790Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
2153790Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2253790Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2353790Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2453790Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2553790Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2653790Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2753790Sobrien * SUCH DAMAGE.
2853790Sobrien */
2953790Sobrien
3053790Sobrien#include <sys/cdefs.h>
3153790Sobrien__FBSDID("$FreeBSD: head/sbin/hastd/ebuf.c 225787 2011-09-27 08:50:37Z pjd $");
3253790Sobrien
3353790Sobrien#include <sys/param.h>
3453790Sobrien
3553790Sobrien#include <errno.h>
3653790Sobrien#include <stdbool.h>
3753790Sobrien#include <stdint.h>
3853790Sobrien#include <strings.h>
3953790Sobrien#include <unistd.h>
4053790Sobrien
4153790Sobrien#include <pjdlog.h>
4253790Sobrien
4353790Sobrien#include "ebuf.h"
4453790Sobrien
4553790Sobrien#ifndef	PJDLOG_ASSERT
4653790Sobrien#include <assert.h>
4753790Sobrien#define	PJDLOG_ASSERT(...)	assert(__VA_ARGS__)
4853790Sobrien#endif
4953790Sobrien
5053790Sobrien#define	EBUF_MAGIC	0xeb0f41c
5153790Sobrienstruct ebuf {
5253790Sobrien	/* Magic to assert the caller uses valid structure. */
5353790Sobrien	int		 eb_magic;
5453790Sobrien	/* Address where we did the allocation. */
5553790Sobrien	unsigned char	*eb_start;
5659868Speter	/* Allocation end address. */
5759868Speter	unsigned char	*eb_end;
5853790Sobrien	/* Start of real data. */
5953790Sobrien	unsigned char	*eb_used;
6062422Sgroudier	/* Size of real data. */
6162422Sgroudier	size_t		 eb_size;
6262422Sgroudier};
6362422Sgroudier
6462422Sgroudierstatic int ebuf_head_extend(struct ebuf *eb, size_t size);
6562422Sgroudierstatic int ebuf_tail_extend(struct ebuf *eb, size_t size);
6662422Sgroudier
6762422Sgroudierstruct ebuf *
6862422Sgroudierebuf_alloc(size_t size)
6953790Sobrien{
7062422Sgroudier	struct ebuf *eb;
7162422Sgroudier	int rerrno;
7262422Sgroudier
7362422Sgroudier	eb = malloc(sizeof(*eb));
7453790Sobrien	if (eb == NULL)
7562422Sgroudier		return (NULL);
7653790Sobrien	size += PAGE_SIZE;
7762422Sgroudier	eb->eb_start = malloc(size);
7886266Sgroudier	if (eb->eb_start == NULL) {
79		rerrno = errno;
80		free(eb);
81		errno = rerrno;
82		return (NULL);
83	}
84	eb->eb_end = eb->eb_start + size;
85	/*
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.
88	 */
89	eb->eb_used = eb->eb_start + PAGE_SIZE / 4;
90	eb->eb_size = 0;
91	eb->eb_magic = EBUF_MAGIC;
92
93	return (eb);
94}
95
96void
97ebuf_free(struct ebuf *eb)
98{
99
100	PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
101
102	eb->eb_magic = 0;
103
104	free(eb->eb_start);
105	free(eb);
106}
107
108int
109ebuf_add_head(struct ebuf *eb, const void *data, size_t size)
110{
111
112	PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
113
114	if (size > (size_t)(eb->eb_used - eb->eb_start)) {
115		/*
116		 * We can't add more entries at the front, so we have to extend
117		 * our buffer.
118		 */
119		if (ebuf_head_extend(eb, size) < 0)
120			return (-1);
121	}
122	PJDLOG_ASSERT(size <= (size_t)(eb->eb_used - eb->eb_start));
123
124	eb->eb_size += size;
125	eb->eb_used -= size;
126	/*
127	 * If data is NULL the caller just wants to reserve place.
128	 */
129	if (data != NULL)
130		bcopy(data, eb->eb_used, size);
131
132	return (0);
133}
134
135int
136ebuf_add_tail(struct ebuf *eb, const void *data, size_t size)
137{
138
139	PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
140
141	if (size > (size_t)(eb->eb_end - (eb->eb_used + eb->eb_size))) {
142		/*
143		 * We can't add more entries at the back, so we have to extend
144		 * our buffer.
145		 */
146		if (ebuf_tail_extend(eb, size) < 0)
147			return (-1);
148	}
149	PJDLOG_ASSERT(size <=
150	    (size_t)(eb->eb_end - (eb->eb_used + eb->eb_size)));
151
152	/*
153	 * If data is NULL the caller just wants to reserve space.
154	 */
155	if (data != NULL)
156		bcopy(data, eb->eb_used + eb->eb_size, size);
157	eb->eb_size += size;
158
159	return (0);
160}
161
162void
163ebuf_del_head(struct ebuf *eb, size_t size)
164{
165
166	PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
167	PJDLOG_ASSERT(size <= eb->eb_size);
168
169	eb->eb_used += size;
170	eb->eb_size -= size;
171}
172
173void
174ebuf_del_tail(struct ebuf *eb, size_t size)
175{
176
177	PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
178	PJDLOG_ASSERT(size <= eb->eb_size);
179
180	eb->eb_size -= size;
181}
182
183/*
184 * Return pointer to the data and data size.
185 */
186void *
187ebuf_data(struct ebuf *eb, size_t *sizep)
188{
189
190	PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
191
192	if (sizep != NULL)
193		*sizep = eb->eb_size;
194	return (eb->eb_size > 0 ? eb->eb_used : NULL);
195}
196
197/*
198 * Return data size.
199 */
200size_t
201ebuf_size(struct ebuf *eb)
202{
203
204	PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
205
206	return (eb->eb_size);
207}
208
209/*
210 * Function adds size + (PAGE_SIZE / 4) bytes at the front of the buffer..
211 */
212static int
213ebuf_head_extend(struct ebuf *eb, size_t size)
214{
215	unsigned char *newstart, *newused;
216	size_t newsize;
217
218	PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
219
220	newsize = eb->eb_end - eb->eb_start + (PAGE_SIZE / 4) + size;
221
222	newstart = malloc(newsize);
223	if (newstart == NULL)
224		return (-1);
225	newused =
226	    newstart + (PAGE_SIZE / 4) + size + (eb->eb_used - eb->eb_start);
227
228	bcopy(eb->eb_used, newused, eb->eb_size);
229
230	eb->eb_start = newstart;
231	eb->eb_used = newused;
232	eb->eb_end = newstart + newsize;
233
234	return (0);
235}
236
237/*
238 * Function adds size + ((3 * PAGE_SIZE) / 4) bytes at the back.
239 */
240static int
241ebuf_tail_extend(struct ebuf *eb, size_t size)
242{
243	unsigned char *newstart;
244	size_t newsize;
245
246	PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
247
248	newsize = eb->eb_end - eb->eb_start + size + ((3 * PAGE_SIZE) / 4);
249
250	newstart = realloc(eb->eb_start, newsize);
251	if (newstart == NULL)
252		return (-1);
253
254	eb->eb_used = newstart + (eb->eb_used - eb->eb_start);
255	eb->eb_start = newstart;
256	eb->eb_end = newstart + newsize;
257
258	return (0);
259}
260