| 1 | /* |
| 2 | * Copyright 2009 Red Hat Inc., Durham, North Carolina. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | * Authors: |
| 20 | * "Daniel Kopecek" <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | /* |
| 24 | * Sparse buffer API implementation |
| 25 | */ |
| 26 | |
| 27 | #ifdef HAVE_CONFIG_H |
| 28 | #include <config.h> |
| 29 | #endif |
| 30 | |
| 31 | #include <string.h> |
| 32 | #include <errno.h> |
| 33 | |
| 34 | #include "spb.h" |
| 35 | |
| 36 | spb_t *spb_new (void *buffer, size_t buflen, uint32_t balloc) |
| 37 | { |
| 38 | spb_t *spb = malloc(sizeof(spb_t)); |
| 39 | |
| 40 | spb->balloc = (balloc == 0 ? SPB_DEFAULT_BALLOC : balloc); |
| 41 | spb->buffer = malloc(sizeof(spb_item_t) * spb->balloc); |
| 42 | spb->bflags = 0; |
| 43 | |
| 44 | if (buffer != NULL && buflen > 0) { |
| 45 | spb->btotal = 1; |
| 46 | spb->buffer[0].base = buffer; |
| 47 | spb->buffer[0].gend = (spb_size_t)(buflen - 1); |
| 48 | } else { |
| 49 | spb->btotal = 0; |
| 50 | } |
| 51 | |
| 52 | return (spb); |
| 53 | } |
| 54 | |
| 55 | uint32_t spb_bindex (spb_t *spb, spb_size_t idx) |
| 56 | { |
| 57 | uint32_t w, s; |
| 58 | |
| 59 | w = spb->btotal; |
| 60 | s = spb_size (spb); |
| 61 | |
| 62 | if (idx < s) { |
| 63 | s = 0; |
| 64 | |
| 65 | /* Find the correct buffer using binary search */ |
| 66 | while (w > 0) { |
| 67 | if (idx > spb->buffer[s + w/2].gend) { |
| 68 | s += w/2 + 1; |
| 69 | w = w - w/2 - 1; |
| 70 | } else { |
| 71 | w = w/2; |
| 72 | } |
| 73 | } |
| 74 | } else |
| 75 | s = w; |
| 76 | /* |
| 77 | * `s' should now contain the index of the spb array item |
| 78 | * that contains the buffer that represents the byte range |
| 79 | * in the global buffer that cointains the requested index. |
| 80 | * If `index' was out of range then `s' will hold a value |
| 81 | * equal to or greater than the total number of buffers in |
| 82 | * the spb. |
| 83 | */ |
| 84 | return (s); |
| 85 | } |
| 86 | |
| 87 | spb_size_t spb_size (spb_t *spb) |
| 88 | { |
| 89 | return (spb->btotal > 0 ? spb->buffer[spb->btotal - 1].gend + 1 : 0); |
| 90 | } |
| 91 | |
| 92 | int spb_add (spb_t *spb, void *buffer, size_t buflen) |
| 93 | { |
| 94 | spb_size_t gend; |
| 95 | |
| 96 | if (spb->btotal >= spb->balloc) { |
| 97 | uint32_t current_balloc = spb->balloc; |
| 98 | if (spb->balloc < SPB_BALLOC_HIGHTRESH) |
| 99 | spb->balloc <<= 1; |
| 100 | else |
| 101 | spb->balloc += SPB_BALLOC_ADD; |
| 102 | |
| 103 | void *new_buffer = realloc(spb->buffer, sizeof(spb_item_t) * spb->balloc); |
| 104 | if (new_buffer == NULL) { |
| 105 | spb->balloc = current_balloc; |
| 106 | return (-1); |
| 107 | } |
| 108 | spb->buffer = new_buffer; |
| 109 | } |
| 110 | |
| 111 | /* XXX: assume that btotal > 0 if btotal > balloc? */ |
| 112 | if (spb->btotal > 0) |
| 113 | gend = spb->buffer[spb->btotal - 1].gend + buflen; |
| 114 | else |
| 115 | gend = buflen - 1; |
| 116 | |
| 117 | spb->buffer[spb->btotal].base = buffer; |
| 118 | spb->buffer[spb->btotal].gend = gend; |
| 119 | ++spb->btotal; |
| 120 | |
| 121 | return (0); |
| 122 | } |
| 123 | |
| 124 | int spb_pick (spb_t *spb, spb_size_t start, spb_size_t size, void *dst) |
| 125 | { |
| 126 | register uint32_t b_idx; |
| 127 | register uint8_t *b_dst = (uint8_t *)dst; |
| 128 | |
| 129 | b_idx = spb_bindex (spb, start); |
| 130 | |
| 131 | if (size == 0) |
| 132 | return (0); /* No bytes to copy, return as success */ |
| 133 | |
| 134 | if (b_idx < spb->btotal) { |
| 135 | size_t l_off; |
| 136 | size_t l_len; |
| 137 | |
| 138 | if (b_idx > 0) { |
| 139 | l_off = (size_t)(start - spb->buffer[b_idx - 1].gend - 1); |
| 140 | l_len = (size_t)(spb->buffer[b_idx].gend - spb->buffer[b_idx - 1].gend) - l_off; |
| 141 | } else { |
| 142 | l_off = (size_t)(start); |
| 143 | l_len = (size_t)(spb->buffer[b_idx].gend + 1) - l_off; |
| 144 | } |
| 145 | |
| 146 | if (size < l_len) |
| 147 | l_len = (size_t)size; |
| 148 | |
| 149 | memcpy (b_dst, (char *)(spb->buffer[b_idx].base) + l_off, l_len); |
| 150 | |
| 151 | b_dst += l_len; |
| 152 | size -= l_len; |
| 153 | |
| 154 | while (++b_idx < spb->btotal && size > 0) { |
| 155 | l_len = (size_t)(spb->buffer[b_idx].gend - spb->buffer[b_idx - 1].gend); |
| 156 | |
| 157 | if (size < l_len) |
| 158 | l_len = (size_t)size; |
| 159 | |
| 160 | memcpy (b_dst, spb->buffer[b_idx].base, l_len); |
| 161 | |
| 162 | b_dst += l_len; |
| 163 | size -= l_len; |
| 164 | } |
| 165 | |
| 166 | if (size > 0) |
| 167 | return (1); /* less than size bytes were copyied */ |
| 168 | else |
| 169 | return (0); |
| 170 | } |
| 171 | /* else - the start offset is out of range*/ |
| 172 | |
| 173 | errno = ERANGE; |
| 174 | return (-1); |
| 175 | } |
| 176 | |
| 177 | int spb_pick_raw (spb_t *spb, uint32_t bindex, spb_size_t start, spb_size_t size, void *dst) |
| 178 | { |
| 179 | /* For now, just ignore the bindex argument and use spb_pick */ |
| 180 | return spb_pick (spb, start, size, dst); |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * (cbarg) void *cb (void *cbarg, void *src, size_t size) |
| 185 | */ |
| 186 | int spb_pick_cb (spb_t *spb, spb_size_t start, spb_size_t size, void *cb (void *, void *, size_t), void *cbarg) |
| 187 | { |
| 188 | register uint32_t b_idx; |
| 189 | |
| 190 | b_idx = spb_bindex (spb, start); |
| 191 | |
| 192 | if (b_idx < spb->btotal) { |
| 193 | size_t l_off; |
| 194 | size_t l_len; |
| 195 | |
| 196 | if (b_idx > 0) { |
| 197 | l_off = (size_t)(start - spb->buffer[b_idx - 1].gend - 1); |
| 198 | l_len = (size_t)(spb->buffer[b_idx].gend - spb->buffer[b_idx - 1].gend) - l_off; |
| 199 | } else { |
| 200 | l_off = (size_t)(start); |
| 201 | l_len = (size_t)(spb->buffer[b_idx].gend + 1) - l_off; |
| 202 | } |
| 203 | |
| 204 | if (size < l_len) |
| 205 | l_len = (size_t)size; |
| 206 | |
| 207 | cbarg = cb (cbarg, (char *)(spb->buffer[b_idx].base) + l_off, l_len); |
| 208 | size -= l_len; |
| 209 | |
| 210 | while (++b_idx < spb->btotal && size > 0) { |
| 211 | l_len = (size_t)(spb->buffer[b_idx].gend - spb->buffer[b_idx - 1].gend); |
| 212 | |
| 213 | if (size < l_len) |
| 214 | l_len = (size_t)size; |
| 215 | |
| 216 | cbarg = cb (cbarg, spb->buffer[b_idx].base, l_len); |
| 217 | size -= l_len; |
| 218 | } |
| 219 | |
| 220 | if (size > 0) |
| 221 | return (1); /* less than size bytes were copyied */ |
| 222 | else |
| 223 | return (0); |
| 224 | } |
| 225 | /* else - the start offset is out of range*/ |
| 226 | |
| 227 | errno = ERANGE; |
| 228 | return (-1); |
| 229 | } |
| 230 | |
| 231 | void spb_free (spb_t *spb, spb_flags_t flags) |
| 232 | { |
| 233 | if (spb == NULL) { |
| 234 | errno = EFAULT; |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | if (flags & SPB_FLAG_FREE) { |
| 239 | register uint32_t i; |
| 240 | |
| 241 | for (i = 0; i < spb->btotal; ++i) |
| 242 | free(spb->buffer[i].base); |
| 243 | } |
| 244 | #ifndef NDEBUG |
| 245 | memset (spb->buffer, 0, sizeof (spb_item_t) * spb->balloc); |
| 246 | #endif |
| 247 | free(spb->buffer); |
| 248 | #ifndef NDEBUG |
| 249 | memset (spb, 0, sizeof (spb_t)); |
| 250 | #endif |
| 251 | free(spb); |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | spb_size_t spb_drop_head (spb_t *spb, spb_size_t size, spb_flags_t flags) |
| 256 | { |
| 257 | spb_size_t e_sub = 0; |
| 258 | uint32_t b_idx; |
| 259 | |
| 260 | b_idx = spb_bindex (spb, size); |
| 261 | |
| 262 | if (b_idx > 0) { |
| 263 | if (b_idx >= spb->btotal) { |
| 264 | /* free everything */ |
| 265 | e_sub = spb->buffer[spb->btotal - 1].gend + 1; |
| 266 | |
| 267 | if (flags & SPB_FLAG_FREE) { |
| 268 | register uint32_t i; |
| 269 | |
| 270 | for (i = spb->btotal; i > 0; --i) { |
| 271 | free(spb->buffer[i - 1].base); |
| 272 | spb->buffer[i - 1].base = NULL; |
| 273 | } |
| 274 | } |
| 275 | spb->btotal = 0; |
| 276 | |
| 277 | void *new_buffer = realloc(spb->buffer, sizeof(spb_item_t) * SPB_DEFAULT_BALLOC); |
| 278 | if (new_buffer == NULL) |
| 279 | return (0); |
| 280 | spb->buffer = new_buffer; |
| 281 | spb->balloc = SPB_DEFAULT_BALLOC; |
| 282 | } else { |
| 283 | /* free buffer 0..(b_idx - 1) */ |
| 284 | register uint32_t i; |
| 285 | |
| 286 | e_sub = spb->buffer[b_idx - 1].gend + 1; |
| 287 | |
| 288 | if (flags & SPB_FLAG_FREE) { |
| 289 | for (i = b_idx - 1; i > 0; --i) { |
| 290 | free(spb->buffer[i - 1].base); |
| 291 | spb->buffer[i - 1].base = NULL; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | spb->btotal -= b_idx; |
| 296 | memmove (spb->buffer, spb->buffer + b_idx, sizeof (spb_item_t) * spb->btotal); |
| 297 | |
| 298 | /* |
| 299 | * Update the global end index member in each remaining buffer item |
| 300 | */ |
| 301 | for (i = spb->btotal; i > 0; --i) |
| 302 | spb->buffer[i - 1].gend -= e_sub; |
| 303 | |
| 304 | /* |
| 305 | * Shrink the pre-allocated memory if it's more than 2 times larger than |
| 306 | * the space needed for the remaining buffers. |
| 307 | */ |
| 308 | if ((spb->balloc >> 1) > spb->btotal) { |
| 309 | uint32_t current_balloc = spb->balloc; |
| 310 | spb->balloc >>= 1; |
| 311 | void *new_buffer = spb->buffer = realloc(spb->buffer, sizeof(spb_item_t) * spb->balloc); |
| 312 | if (new_buffer == NULL && spb->balloc > 0) { |
| 313 | spb->balloc = current_balloc; |
| 314 | } else { |
| 315 | spb->buffer = new_buffer; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | return (e_sub); |
| 322 | } |
| 323 | |
| 324 | uint8_t spb_octet (spb_t *spb, spb_size_t idx) |
| 325 | { |
| 326 | uint32_t b_idx; |
| 327 | size_t l_off; |
| 328 | |
| 329 | b_idx = spb_bindex (spb, idx); |
| 330 | |
| 331 | if (b_idx < spb->btotal) { |
| 332 | l_off = (size_t)(b_idx > 0 ? idx - spb->buffer[b_idx - 1].gend - 1 : idx); |
| 333 | return *((uint8_t *)(spb->buffer[b_idx].base) + l_off); |
| 334 | } |
| 335 | |
| 336 | errno = ERANGE; |
| 337 | |
| 338 | return (uint8_t)(-1); |
| 339 | } |
| 340 | |
| 341 | const uint8_t *spb_direct (spb_t *spb, spb_size_t start, spb_size_t size) |
| 342 | { |
| 343 | uint32_t b_idx; |
| 344 | size_t l_off; |
| 345 | |
| 346 | b_idx = spb_bindex (spb, start); |
| 347 | |
| 348 | if (b_idx < spb->btotal) { |
| 349 | if (start + size - 1 <= spb->buffer[b_idx].gend) { |
| 350 | l_off = (size_t)(b_idx > 0 ? start - spb->buffer[b_idx - 1].gend - 1 : start); |
| 351 | return ((const uint8_t *)(spb->buffer[b_idx].base) + l_off); |
| 352 | } |
| 353 | } else |
| 354 | errno = ERANGE; |
| 355 | |
| 356 | return (NULL); |
| 357 | } |