xref: /freebsd/sbin/hastd/ebuf.c (revision 7937bfbc0ca53fe7cdd0d54414f9296e273a518e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2009-2010 The FreeBSD Foundation
5  *
6  * This software was developed by Pawel Jakub Dawidek under sponsorship from
7  * the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/param.h>
32 
33 #include <errno.h>
34 #include <stdbool.h>
35 #include <stdint.h>
36 #include <strings.h>
37 #include <unistd.h>
38 
39 #include <pjdlog.h>
40 
41 #include "ebuf.h"
42 
43 #ifndef	PJDLOG_ASSERT
44 #include <assert.h>
45 #define	PJDLOG_ASSERT(...)	assert(__VA_ARGS__)
46 #endif
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 *
66 ebuf_alloc(size_t size)
67 {
68 	struct ebuf *eb;
69 	size_t page_size;
70 	int rerrno;
71 
72 	eb = malloc(sizeof(*eb));
73 	if (eb == NULL)
74 		return (NULL);
75 	page_size = getpagesize();
76 	size += page_size;
77 	eb->eb_start = malloc(size);
78 	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 
96 void
97 ebuf_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 
108 int
109 ebuf_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) == -1)
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 
135 int
136 ebuf_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) == -1)
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 
162 void
163 ebuf_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 
173 void
174 ebuf_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  */
186 void *
187 ebuf_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  */
200 size_t
201 ebuf_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  */
212 static int
213 ebuf_head_extend(struct ebuf *eb, size_t size)
214 {
215 	unsigned char *newstart, *newused;
216 	size_t newsize, page_size;
217 
218 	PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
219 
220 	page_size = getpagesize();
221 	newsize = eb->eb_end - eb->eb_start + (page_size / 4) + size;
222 
223 	newstart = malloc(newsize);
224 	if (newstart == NULL)
225 		return (-1);
226 	newused =
227 	    newstart + (page_size / 4) + size + (eb->eb_used - eb->eb_start);
228 
229 	bcopy(eb->eb_used, newused, eb->eb_size);
230 
231 	eb->eb_start = newstart;
232 	eb->eb_used = newused;
233 	eb->eb_end = newstart + newsize;
234 
235 	return (0);
236 }
237 
238 /*
239  * Function adds size + ((3 * PAGE_SIZE) / 4) bytes at the back.
240  */
241 static int
242 ebuf_tail_extend(struct ebuf *eb, size_t size)
243 {
244 	unsigned char *newstart;
245 	size_t newsize, page_size;
246 
247 	PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
248 
249 	page_size = getpagesize();
250 	newsize = eb->eb_end - eb->eb_start + size + ((3 * page_size) / 4);
251 
252 	newstart = realloc(eb->eb_start, newsize);
253 	if (newstart == NULL)
254 		return (-1);
255 
256 	eb->eb_used = newstart + (eb->eb_used - eb->eb_start);
257 	eb->eb_start = newstart;
258 	eb->eb_end = newstart + newsize;
259 
260 	return (0);
261 }
262