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