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