1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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: stable/11/sbin/hastd/ebuf.c 330449 2018-03-05 07:26:05Z eadler $");
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
53struct 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
66static int ebuf_head_extend(struct ebuf *eb, size_t size);
67static int ebuf_tail_extend(struct ebuf *eb, size_t size);
68
69struct ebuf *
70ebuf_alloc(size_t size)
71{
72	struct ebuf *eb;
73	int rerrno;
74
75	eb = malloc(sizeof(*eb));
76	if (eb == NULL)
77		return (NULL);
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
98void
99ebuf_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
110int
111ebuf_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
137int
138ebuf_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
164void
165ebuf_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
175void
176ebuf_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 */
188void *
189ebuf_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 */
202size_t
203ebuf_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 */
214static int
215ebuf_head_extend(struct ebuf *eb, size_t size)
216{
217	unsigned char *newstart, *newused;
218	size_t newsize;
219
220	PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
221
222	newsize = eb->eb_end - eb->eb_start + (PAGE_SIZE / 4) + size;
223
224	newstart = malloc(newsize);
225	if (newstart == NULL)
226		return (-1);
227	newused =
228	    newstart + (PAGE_SIZE / 4) + size + (eb->eb_used - eb->eb_start);
229
230	bcopy(eb->eb_used, newused, eb->eb_size);
231
232	eb->eb_start = newstart;
233	eb->eb_used = newused;
234	eb->eb_end = newstart + newsize;
235
236	return (0);
237}
238
239/*
240 * Function adds size + ((3 * PAGE_SIZE) / 4) bytes at the back.
241 */
242static int
243ebuf_tail_extend(struct ebuf *eb, size_t size)
244{
245	unsigned char *newstart;
246	size_t newsize;
247
248	PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC);
249
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