1 /*-
2  * Copyright (c) 2009-2010 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Pawel Jakub Dawidek under sponsorship from
6  * the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 
33 #ifdef HAVE_HEADER_MACHINE_VMPARAM_H
34 #include <machine/vmparam.h>
35 #elif defined(HAVE_HEADER_SYS_USER_H)
36 #include <sys/user.h>
37 #endif
38 
39 #include <assert.h>
40 #include <errno.h>
41 #include <stdbool.h>
42 #include <stdint.h>
43 #include <strings.h>
44 #include <unistd.h>
45 
46 #include "ebuf.h"
47 
48 #define	EBUF_MAGIC	0xeb0f41c
49 struct ebuf {
50 	/* Magic to assert the caller uses valid structure. */
51 	int		 eb_magic;
52 	/* Address where we did the allocation. */
53 	unsigned char	*eb_start;
54 	/* Allocation end address. */
55 	unsigned char	*eb_end;
56 	/* Start of real data. */
57 	unsigned char	*eb_used;
58 	/* Size of real data. */
59 	size_t		 eb_size;
60 };
61 
62 static int ebuf_head_extend(struct ebuf *eb, size_t size);
63 static int ebuf_tail_extend(struct ebuf *eb, size_t size);
64 
65 struct ebuf *
ebuf_alloc(size_t size)66 ebuf_alloc(size_t size)
67 {
68 	struct ebuf *eb;
69 	int rerrno;
70 
71 	eb = malloc(sizeof(*eb));
72 	if (eb == NULL)
73 		return (NULL);
74 	size += PAGE_SIZE;
75 	eb->eb_start = malloc(size);
76 	if (eb->eb_start == NULL) {
77 		rerrno = errno;
78 		free(eb);
79 		errno = rerrno;
80 		return (NULL);
81 	}
82 	eb->eb_end = eb->eb_start + size;
83 	/*
84 	 * We set start address for real data not at the first entry, because
85 	 * we want to be able to add data at the front.
86 	 */
87 	eb->eb_used = eb->eb_start + PAGE_SIZE / 4;
88 	eb->eb_size = 0;
89 	eb->eb_magic = EBUF_MAGIC;
90 
91 	return (eb);
92 }
93 
94 void
ebuf_free(struct ebuf * eb)95 ebuf_free(struct ebuf *eb)
96 {
97 
98 	assert(eb != NULL && eb->eb_magic == EBUF_MAGIC);
99 
100 	eb->eb_magic = 0;
101 
102 	free(eb->eb_start);
103 	free(eb);
104 }
105 
106 int
ebuf_add_head(struct ebuf * eb,const void * data,size_t size)107 ebuf_add_head(struct ebuf *eb, const void *data, size_t size)
108 {
109 
110 	assert(eb != NULL && eb->eb_magic == EBUF_MAGIC);
111 
112 	if (size > (size_t)(eb->eb_used - eb->eb_start)) {
113 		/*
114 		 * We can't add more entries at the front, so we have to extend
115 		 * our buffer.
116 		 */
117 		if (ebuf_head_extend(eb, size) < 0)
118 			return (-1);
119 	}
120 	assert(size <= (size_t)(eb->eb_used - eb->eb_start));
121 
122 	eb->eb_size += size;
123 	eb->eb_used -= size;
124 	/*
125 	 * If data is NULL the caller just wants to reserve place.
126 	 */
127 	if (data != NULL)
128 		bcopy(data, eb->eb_used, size);
129 
130 	return (0);
131 }
132 
133 int
ebuf_add_tail(struct ebuf * eb,const void * data,size_t size)134 ebuf_add_tail(struct ebuf *eb, const void *data, size_t size)
135 {
136 
137 	assert(eb != NULL && eb->eb_magic == EBUF_MAGIC);
138 
139 	if (size > (size_t)(eb->eb_end - (eb->eb_used + eb->eb_size))) {
140 		/*
141 		 * We can't add more entries at the back, so we have to extend
142 		 * our buffer.
143 		 */
144 		if (ebuf_tail_extend(eb, size) < 0)
145 			return (-1);
146 	}
147 	assert(size <= (size_t)(eb->eb_end - (eb->eb_used + eb->eb_size)));
148 
149 	/*
150 	 * If data is NULL the caller just wants to reserve space.
151 	 */
152 	if (data != NULL)
153 		bcopy(data, eb->eb_used + eb->eb_size, size);
154 	eb->eb_size += size;
155 
156 	return (0);
157 }
158 
159 void
ebuf_del_head(struct ebuf * eb,size_t size)160 ebuf_del_head(struct ebuf *eb, size_t size)
161 {
162 
163 	assert(eb != NULL && eb->eb_magic == EBUF_MAGIC);
164 	assert(size <= eb->eb_size);
165 
166 	eb->eb_used += size;
167 	eb->eb_size -= size;
168 }
169 
170 void
ebuf_del_tail(struct ebuf * eb,size_t size)171 ebuf_del_tail(struct ebuf *eb, size_t size)
172 {
173 
174 	assert(eb != NULL && eb->eb_magic == EBUF_MAGIC);
175 	assert(size <= eb->eb_size);
176 
177 	eb->eb_size -= size;
178 }
179 
180 /*
181  * Return pointer to the data and data size.
182  */
183 void *
ebuf_data(struct ebuf * eb,size_t * sizep)184 ebuf_data(struct ebuf *eb, size_t *sizep)
185 {
186 
187 	assert(eb != NULL && eb->eb_magic == EBUF_MAGIC);
188 
189 	if (sizep != NULL)
190 		*sizep = eb->eb_size;
191 	return (eb->eb_size > 0 ? eb->eb_used : NULL);
192 }
193 
194 /*
195  * Return data size.
196  */
197 size_t
ebuf_size(struct ebuf * eb)198 ebuf_size(struct ebuf *eb)
199 {
200 
201 	assert(eb != NULL && eb->eb_magic == EBUF_MAGIC);
202 
203 	return (eb->eb_size);
204 }
205 
206 /*
207  * Function adds size + (PAGE_SIZE / 4) bytes at the front of the buffer..
208  */
209 static int
ebuf_head_extend(struct ebuf * eb,size_t size)210 ebuf_head_extend(struct ebuf *eb, size_t size)
211 {
212 	unsigned char *newstart, *newused;
213 	size_t newsize;
214 
215 	assert(eb != NULL && eb->eb_magic == EBUF_MAGIC);
216 
217 	newsize = eb->eb_end - eb->eb_start + (PAGE_SIZE / 4) + size;
218 
219 	newstart = malloc(newsize);
220 	if (newstart == NULL)
221 		return (-1);
222 	newused =
223 	    newstart + (PAGE_SIZE / 4) + size + (eb->eb_used - eb->eb_start);
224 
225 	bcopy(eb->eb_used, newused, eb->eb_size);
226 
227 	eb->eb_start = newstart;
228 	eb->eb_used = newused;
229 	eb->eb_end = newstart + newsize;
230 
231 	return (0);
232 }
233 
234 /*
235  * Function adds size + ((3 * PAGE_SIZE) / 4) bytes at the back.
236  */
237 static int
ebuf_tail_extend(struct ebuf * eb,size_t size)238 ebuf_tail_extend(struct ebuf *eb, size_t size)
239 {
240 	unsigned char *newstart;
241 	size_t newsize;
242 
243 	assert(eb != NULL && eb->eb_magic == EBUF_MAGIC);
244 
245 	newsize = eb->eb_end - eb->eb_start + size + ((3 * PAGE_SIZE) / 4);
246 
247 	newstart = realloc(eb->eb_start, newsize);
248 	if (newstart == NULL)
249 		return (-1);
250 
251 	eb->eb_used = newstart + (eb->eb_used - eb->eb_start);
252 	eb->eb_start = newstart;
253 	eb->eb_end = newstart + newsize;
254 
255 	return (0);
256 }
257