ebuf.c (209184) | ebuf.c (225787) |
---|---|
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 --- 14 unchanged lines hidden (view full) --- 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> | 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 --- 14 unchanged lines hidden (view full) --- 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__FBSDID("$FreeBSD: head/sbin/hastd/ebuf.c 209184 2010-06-14 21:44:58Z pjd $"); | 31__FBSDID("$FreeBSD: head/sbin/hastd/ebuf.c 225787 2011-09-27 08:50:37Z pjd $"); |
32 33#include <sys/param.h> 34 | 32 33#include <sys/param.h> 34 |
35#include <assert.h> | |
36#include <errno.h> 37#include <stdbool.h> 38#include <stdint.h> 39#include <strings.h> 40#include <unistd.h> 41 | 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 |
|
42#include "ebuf.h" 43 | 43#include "ebuf.h" 44 |
45#ifndef PJDLOG_ASSERT 46#include <assert.h> 47#define PJDLOG_ASSERT(...) assert(__VA_ARGS__) 48#endif 49 |
|
44#define EBUF_MAGIC 0xeb0f41c 45struct ebuf { 46 /* Magic to assert the caller uses valid structure. */ 47 int eb_magic; 48 /* Address where we did the allocation. */ 49 unsigned char *eb_start; 50 /* Allocation end address. */ 51 unsigned char *eb_end; --- 34 unchanged lines hidden (view full) --- 86 87 return (eb); 88} 89 90void 91ebuf_free(struct ebuf *eb) 92{ 93 | 50#define EBUF_MAGIC 0xeb0f41c 51struct 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; --- 34 unchanged lines hidden (view full) --- 92 93 return (eb); 94} 95 96void 97ebuf_free(struct ebuf *eb) 98{ 99 |
94 assert(eb != NULL && eb->eb_magic == EBUF_MAGIC); | 100 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC); |
95 96 eb->eb_magic = 0; 97 98 free(eb->eb_start); 99 free(eb); 100} 101 102int 103ebuf_add_head(struct ebuf *eb, const void *data, size_t size) 104{ 105 | 101 102 eb->eb_magic = 0; 103 104 free(eb->eb_start); 105 free(eb); 106} 107 108int 109ebuf_add_head(struct ebuf *eb, const void *data, size_t size) 110{ 111 |
106 assert(eb != NULL && eb->eb_magic == EBUF_MAGIC); | 112 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC); |
107 108 if (size > (size_t)(eb->eb_used - eb->eb_start)) { 109 /* 110 * We can't add more entries at the front, so we have to extend 111 * our buffer. 112 */ 113 if (ebuf_head_extend(eb, size) < 0) 114 return (-1); 115 } | 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) < 0) 120 return (-1); 121 } |
116 assert(size <= (size_t)(eb->eb_used - eb->eb_start)); | 122 PJDLOG_ASSERT(size <= (size_t)(eb->eb_used - eb->eb_start)); |
117 118 eb->eb_size += size; 119 eb->eb_used -= size; 120 /* 121 * If data is NULL the caller just wants to reserve place. 122 */ 123 if (data != NULL) 124 bcopy(data, eb->eb_used, size); 125 126 return (0); 127} 128 129int 130ebuf_add_tail(struct ebuf *eb, const void *data, size_t size) 131{ 132 | 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 135int 136ebuf_add_tail(struct ebuf *eb, const void *data, size_t size) 137{ 138 |
133 assert(eb != NULL && eb->eb_magic == EBUF_MAGIC); | 139 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC); |
134 135 if (size > (size_t)(eb->eb_end - (eb->eb_used + eb->eb_size))) { 136 /* 137 * We can't add more entries at the back, so we have to extend 138 * our buffer. 139 */ 140 if (ebuf_tail_extend(eb, size) < 0) 141 return (-1); 142 } | 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) < 0) 147 return (-1); 148 } |
143 assert(size <= (size_t)(eb->eb_end - (eb->eb_used + eb->eb_size))); | 149 PJDLOG_ASSERT(size <= 150 (size_t)(eb->eb_end - (eb->eb_used + eb->eb_size))); |
144 145 /* 146 * If data is NULL the caller just wants to reserve space. 147 */ 148 if (data != NULL) 149 bcopy(data, eb->eb_used + eb->eb_size, size); 150 eb->eb_size += size; 151 152 return (0); 153} 154 155void 156ebuf_del_head(struct ebuf *eb, size_t size) 157{ 158 | 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 162void 163ebuf_del_head(struct ebuf *eb, size_t size) 164{ 165 |
159 assert(eb != NULL && eb->eb_magic == EBUF_MAGIC); 160 assert(size <= eb->eb_size); | 166 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC); 167 PJDLOG_ASSERT(size <= eb->eb_size); |
161 162 eb->eb_used += size; 163 eb->eb_size -= size; 164} 165 166void 167ebuf_del_tail(struct ebuf *eb, size_t size) 168{ 169 | 168 169 eb->eb_used += size; 170 eb->eb_size -= size; 171} 172 173void 174ebuf_del_tail(struct ebuf *eb, size_t size) 175{ 176 |
170 assert(eb != NULL && eb->eb_magic == EBUF_MAGIC); 171 assert(size <= eb->eb_size); | 177 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC); 178 PJDLOG_ASSERT(size <= eb->eb_size); |
172 173 eb->eb_size -= size; 174} 175 176/* 177 * Return pointer to the data and data size. 178 */ 179void * 180ebuf_data(struct ebuf *eb, size_t *sizep) 181{ 182 | 179 180 eb->eb_size -= size; 181} 182 183/* 184 * Return pointer to the data and data size. 185 */ 186void * 187ebuf_data(struct ebuf *eb, size_t *sizep) 188{ 189 |
183 assert(eb != NULL && eb->eb_magic == EBUF_MAGIC); | 190 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC); |
184 185 if (sizep != NULL) 186 *sizep = eb->eb_size; 187 return (eb->eb_size > 0 ? eb->eb_used : NULL); 188} 189 190/* 191 * Return data size. 192 */ 193size_t 194ebuf_size(struct ebuf *eb) 195{ 196 | 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 */ 200size_t 201ebuf_size(struct ebuf *eb) 202{ 203 |
197 assert(eb != NULL && eb->eb_magic == EBUF_MAGIC); | 204 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC); |
198 199 return (eb->eb_size); 200} 201 202/* 203 * Function adds size + (PAGE_SIZE / 4) bytes at the front of the buffer.. 204 */ 205static int 206ebuf_head_extend(struct ebuf *eb, size_t size) 207{ 208 unsigned char *newstart, *newused; 209 size_t newsize; 210 | 205 206 return (eb->eb_size); 207} 208 209/* 210 * Function adds size + (PAGE_SIZE / 4) bytes at the front of the buffer.. 211 */ 212static int 213ebuf_head_extend(struct ebuf *eb, size_t size) 214{ 215 unsigned char *newstart, *newused; 216 size_t newsize; 217 |
211 assert(eb != NULL && eb->eb_magic == EBUF_MAGIC); | 218 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC); |
212 213 newsize = eb->eb_end - eb->eb_start + (PAGE_SIZE / 4) + size; 214 215 newstart = malloc(newsize); 216 if (newstart == NULL) 217 return (-1); 218 newused = 219 newstart + (PAGE_SIZE / 4) + size + (eb->eb_used - eb->eb_start); --- 11 unchanged lines hidden (view full) --- 231 * Function adds size + ((3 * PAGE_SIZE) / 4) bytes at the back. 232 */ 233static int 234ebuf_tail_extend(struct ebuf *eb, size_t size) 235{ 236 unsigned char *newstart; 237 size_t newsize; 238 | 219 220 newsize = eb->eb_end - eb->eb_start + (PAGE_SIZE / 4) + size; 221 222 newstart = malloc(newsize); 223 if (newstart == NULL) 224 return (-1); 225 newused = 226 newstart + (PAGE_SIZE / 4) + size + (eb->eb_used - eb->eb_start); --- 11 unchanged lines hidden (view full) --- 238 * Function adds size + ((3 * PAGE_SIZE) / 4) bytes at the back. 239 */ 240static int 241ebuf_tail_extend(struct ebuf *eb, size_t size) 242{ 243 unsigned char *newstart; 244 size_t newsize; 245 |
239 assert(eb != NULL && eb->eb_magic == EBUF_MAGIC); | 246 PJDLOG_ASSERT(eb != NULL && eb->eb_magic == EBUF_MAGIC); |
240 241 newsize = eb->eb_end - eb->eb_start + size + ((3 * PAGE_SIZE) / 4); 242 243 newstart = realloc(eb->eb_start, newsize); 244 if (newstart == NULL) 245 return (-1); 246 247 eb->eb_used = newstart + (eb->eb_used - eb->eb_start); 248 eb->eb_start = newstart; 249 eb->eb_end = newstart + newsize; 250 251 return (0); 252} | 247 248 newsize = eb->eb_end - eb->eb_start + size + ((3 * PAGE_SIZE) / 4); 249 250 newstart = realloc(eb->eb_start, newsize); 251 if (newstart == NULL) 252 return (-1); 253 254 eb->eb_used = newstart + (eb->eb_used - eb->eb_start); 255 eb->eb_start = newstart; 256 eb->eb_end = newstart + newsize; 257 258 return (0); 259} |