| 1 |
/* |
| 2 |
* Copyright © 2010 Intel Corporation |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
* copy of this software and associated documentation files (the "Software"), |
| 6 |
* to deal in the Software without restriction, including without limitation |
| 7 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
* Software is furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice (including the next |
| 12 |
* paragraph) shall be included in all copies or substantial portions of the |
| 13 |
* Software. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 |
* DEALINGS IN THE SOFTWARE. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include <assert.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <stdarg.h> |
| 27 |
#include <stdio.h> |
| 28 |
#include <string.h> |
| 29 |
#include <stdint.h> |
| 30 |
|
| 31 |
/* Some versions of MinGW are missing _vscprintf's declaration, although they |
| 32 |
* still provide the symbol in the import library. */ |
| 33 |
#ifdef __MINGW32__ |
| 34 |
_CRTIMP int _vscprintf(const char *format, va_list argptr); |
| 35 |
#endif |
| 36 |
|
| 37 |
#include "ralloc.h" |
| 38 |
|
| 39 |
#ifndef va_copy |
| 40 |
#ifdef __va_copy |
| 41 |
#define va_copy(dest, src) __va_copy((dest), (src)) |
| 42 |
#else |
| 43 |
#define va_copy(dest, src) (dest) = (src) |
| 44 |
#endif |
| 45 |
#endif |
| 46 |
|
| 47 |
#define CANARY 0x5A1106 |
| 48 |
|
| 49 |
/* Align the header's size so that ralloc() allocations will return with the |
| 50 |
* same alignment as a libc malloc would have (8 on 32-bit GLIBC, 16 on |
| 51 |
* 64-bit), avoiding performance penalities on x86 and alignment faults on |
| 52 |
* ARM. |
| 53 |
*/ |
| 54 |
struct |
| 55 |
#ifdef _MSC_VER |
| 56 |
#if _WIN64 |
| 57 |
__declspec(align(16)) |
| 58 |
#else |
| 59 |
__declspec(align(8)) |
| 60 |
#endif |
| 61 |
#elif defined(__LP64__) |
| 62 |
__attribute__((aligned(16))) |
| 63 |
#else |
| 64 |
__attribute__((aligned(8))) |
| 65 |
#endif |
| 66 |
ralloc_header |
| 67 |
{ |
| 68 |
#ifndef NDEBUG |
| 69 |
/* A canary value used to determine whether a pointer is ralloc'd. */ |
| 70 |
unsigned canary; |
| 71 |
#endif |
| 72 |
|
| 73 |
struct ralloc_header *parent; |
| 74 |
|
| 75 |
/* The first child (head of a linked list) */ |
| 76 |
struct ralloc_header *child; |
| 77 |
|
| 78 |
/* Linked list of siblings */ |
| 79 |
struct ralloc_header *prev; |
| 80 |
struct ralloc_header *next; |
| 81 |
|
| 82 |
void (*destructor)(void *); |
| 83 |
}; |
| 84 |
|
| 85 |
typedef struct ralloc_header ralloc_header; |
| 86 |
|
| 87 |
static void unlink_block(ralloc_header *info); |
| 88 |
static void unsafe_free(ralloc_header *info); |
| 89 |
|
| 90 |
static ralloc_header * |
| 91 |
get_header(const void *ptr) |
| 92 |
{ |
| 93 |
ralloc_header *info = (ralloc_header *) (((char *) ptr) - |
| 94 |
sizeof(ralloc_header)); |
| 95 |
assert(info->canary == CANARY); |
| 96 |
return info; |
| 97 |
} |
| 98 |
|
| 99 |
#define PTR_FROM_HEADER(info) (((char *) info) + sizeof(ralloc_header)) |
| 100 |
|
| 101 |
static void |
| 102 |
add_child(ralloc_header *parent, ralloc_header *info) |
| 103 |
{ |
| 104 |
if (parent != NULL) { |
| 105 |
info->parent = parent; |
| 106 |
info->next = parent->child; |
| 107 |
parent->child = info; |
| 108 |
|
| 109 |
if (info->next != NULL) |
| 110 |
info->next->prev = info; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
void * |
| 115 |
ralloc_context(const void *ctx) |
| 116 |
{ |
| 117 |
return ralloc_size(ctx, 0); |
| 118 |
} |
| 119 |
|
| 120 |
void * |
| 121 |
ralloc_size(const void *ctx, size_t size) |
| 122 |
{ |
| 123 |
void *block = malloc(size + sizeof(ralloc_header)); |
| 124 |
ralloc_header *info; |
| 125 |
ralloc_header *parent; |
| 126 |
|
| 127 |
if (unlikely(block == NULL)) |
| 128 |
return NULL; |
| 129 |
|
| 130 |
info = (ralloc_header *) block; |
| 131 |
/* measurements have shown that calloc is slower (because of |
| 132 |
* the multiplication overflow checking?), so clear things |
| 133 |
* manually |
| 134 |
*/ |
| 135 |
info->parent = NULL; |
| 136 |
info->child = NULL; |
| 137 |
info->prev = NULL; |
| 138 |
info->next = NULL; |
| 139 |
info->destructor = NULL; |
| 140 |
|
| 141 |
parent = ctx != NULL ? get_header(ctx) : NULL; |
| 142 |
|
| 143 |
add_child(parent, info); |
| 144 |
|
| 145 |
#ifndef NDEBUG |
| 146 |
info->canary = CANARY; |
| 147 |
#endif |
| 148 |
|
| 149 |
return PTR_FROM_HEADER(info); |
| 150 |
} |
| 151 |
|
| 152 |
void * |
| 153 |
rzalloc_size(const void *ctx, size_t size) |
| 154 |
{ |
| 155 |
void *ptr = ralloc_size(ctx, size); |
| 156 |
|
| 157 |
if (likely(ptr)) |
| 158 |
memset(ptr, 0, size); |
| 159 |
|
| 160 |
return ptr; |
| 161 |
} |
| 162 |
|
| 163 |
/* helper function - assumes ptr != NULL */ |
| 164 |
static void * |
| 165 |
resize(void *ptr, size_t size) |
| 166 |
{ |
| 167 |
ralloc_header *child, *old, *info; |
| 168 |
|
| 169 |
old = get_header(ptr); |
| 170 |
info = realloc(old, size + sizeof(ralloc_header)); |
| 171 |
|
| 172 |
if (info == NULL) |
| 173 |
return NULL; |
| 174 |
|
| 175 |
/* Update parent and sibling's links to the reallocated node. */ |
| 176 |
if (info != old && info->parent != NULL) { |
| 177 |
if (info->parent->child == old) |
| 178 |
info->parent->child = info; |
| 179 |
|
| 180 |
if (info->prev != NULL) |
| 181 |
info->prev->next = info; |
| 182 |
|
| 183 |
if (info->next != NULL) |
| 184 |
info->next->prev = info; |
| 185 |
} |
| 186 |
|
| 187 |
/* Update child->parent links for all children */ |
| 188 |
for (child = info->child; child != NULL; child = child->next) |
| 189 |
child->parent = info; |
| 190 |
|
| 191 |
return PTR_FROM_HEADER(info); |
| 192 |
} |
| 193 |
|
| 194 |
void * |
| 195 |
reralloc_size(const void *ctx, void *ptr, size_t size) |
| 196 |
{ |
| 197 |
if (unlikely(ptr == NULL)) |
| 198 |
return ralloc_size(ctx, size); |
| 199 |
|
| 200 |
assert(ralloc_parent(ptr) == ctx); |
| 201 |
return resize(ptr, size); |
| 202 |
} |
| 203 |
|
| 204 |
void * |
| 205 |
rerzalloc_size(const void *ctx, void *ptr, size_t old_size, size_t new_size) |
| 206 |
{ |
| 207 |
if (unlikely(ptr == NULL)) |
| 208 |
return rzalloc_size(ctx, new_size); |
| 209 |
|
| 210 |
assert(ralloc_parent(ptr) == ctx); |
| 211 |
ptr = resize(ptr, new_size); |
| 212 |
|
| 213 |
if (new_size > old_size) |
| 214 |
memset((char *)ptr + old_size, 0, new_size - old_size); |
| 215 |
|
| 216 |
return ptr; |
| 217 |
} |
| 218 |
|
| 219 |
void * |
| 220 |
ralloc_array_size(const void *ctx, size_t size, unsigned count) |
| 221 |
{ |
| 222 |
if (count > SIZE_MAX/size) |
| 223 |
return NULL; |
| 224 |
|
| 225 |
return ralloc_size(ctx, size * count); |
| 226 |
} |
| 227 |
|
| 228 |
void * |
| 229 |
rzalloc_array_size(const void *ctx, size_t size, unsigned count) |
| 230 |
{ |
| 231 |
if (count > SIZE_MAX/size) |
| 232 |
return NULL; |
| 233 |
|
| 234 |
return rzalloc_size(ctx, size * count); |
| 235 |
} |
| 236 |
|
| 237 |
void * |
| 238 |
reralloc_array_size(const void *ctx, void *ptr, size_t size, unsigned count) |
| 239 |
{ |
| 240 |
if (count > SIZE_MAX/size) |
| 241 |
return NULL; |
| 242 |
|
| 243 |
return reralloc_size(ctx, ptr, size * count); |
| 244 |
} |
| 245 |
|
| 246 |
void * |
| 247 |
rerzalloc_array_size(const void *ctx, void *ptr, size_t size, |
| 248 |
unsigned old_count, unsigned new_count) |
| 249 |
{ |
| 250 |
if (new_count > SIZE_MAX/size) |
| 251 |
return NULL; |
| 252 |
|
| 253 |
return rerzalloc_size(ctx, ptr, size * old_count, size * new_count); |
| 254 |
} |
| 255 |
|
| 256 |
void |
| 257 |
ralloc_free(void *ptr) |
| 258 |
{ |
| 259 |
ralloc_header *info; |
| 260 |
|
| 261 |
if (ptr == NULL) |
| 262 |
return; |
| 263 |
|
| 264 |
info = get_header(ptr); |
| 265 |
unlink_block(info); |
| 266 |
unsafe_free(info); |
| 267 |
} |
| 268 |
|
| 269 |
static void |
| 270 |
unlink_block(ralloc_header *info) |
| 271 |
{ |
| 272 |
/* Unlink from parent & siblings */ |
| 273 |
if (info->parent != NULL) { |
| 274 |
if (info->parent->child == info) |
| 275 |
info->parent->child = info->next; |
| 276 |
|
| 277 |
if (info->prev != NULL) |
| 278 |
info->prev->next = info->next; |
| 279 |
|
| 280 |
if (info->next != NULL) |
| 281 |
info->next->prev = info->prev; |
| 282 |
} |
| 283 |
info->parent = NULL; |
| 284 |
info->prev = NULL; |
| 285 |
info->next = NULL; |
| 286 |
} |
| 287 |
|
| 288 |
static void |
| 289 |
unsafe_free(ralloc_header *info) |
| 290 |
{ |
| 291 |
/* Recursively free any children...don't waste time unlinking them. */ |
| 292 |
ralloc_header *temp; |
| 293 |
while (info->child != NULL) { |
| 294 |
temp = info->child; |
| 295 |
info->child = temp->next; |
| 296 |
unsafe_free(temp); |
| 297 |
} |
| 298 |
|
| 299 |
/* Free the block itself. Call the destructor first, if any. */ |
| 300 |
if (info->destructor != NULL) |
| 301 |
info->destructor(PTR_FROM_HEADER(info)); |
| 302 |
|
| 303 |
free(info); |
| 304 |
} |
| 305 |
|
| 306 |
void |
| 307 |
ralloc_steal(const void *new_ctx, void *ptr) |
| 308 |
{ |
| 309 |
ralloc_header *info, *parent; |
| 310 |
|
| 311 |
if (unlikely(ptr == NULL)) |
| 312 |
return; |
| 313 |
|
| 314 |
info = get_header(ptr); |
| 315 |
parent = new_ctx ? get_header(new_ctx) : NULL; |
| 316 |
|
| 317 |
unlink_block(info); |
| 318 |
|
| 319 |
add_child(parent, info); |
| 320 |
} |
| 321 |
|
| 322 |
void |
| 323 |
ralloc_adopt(const void *new_ctx, void *old_ctx) |
| 324 |
{ |
| 325 |
ralloc_header *new_info, *old_info, *child; |
| 326 |
|
| 327 |
if (unlikely(old_ctx == NULL)) |
| 328 |
return; |
| 329 |
|
| 330 |
old_info = get_header(old_ctx); |
| 331 |
new_info = get_header(new_ctx); |
| 332 |
|
| 333 |
/* If there are no children, bail. */ |
| 334 |
if (unlikely(old_info->child == NULL)) |
| 335 |
return; |
| 336 |
|
| 337 |
/* Set all the children's parent to new_ctx; get a pointer to the last child. */ |
| 338 |
for (child = old_info->child; child->next != NULL; child = child->next) { |
| 339 |
child->parent = new_info; |
| 340 |
} |
| 341 |
child->parent = new_info; |
| 342 |
|
| 343 |
/* Connect the two lists together; parent them to new_ctx; make old_ctx empty. */ |
| 344 |
child->next = new_info->child; |
| 345 |
if (child->next) |
| 346 |
child->next->prev = child; |
| 347 |
new_info->child = old_info->child; |
| 348 |
old_info->child = NULL; |
| 349 |
} |
| 350 |
|
| 351 |
void * |
| 352 |
ralloc_parent(const void *ptr) |
| 353 |
{ |
| 354 |
ralloc_header *info; |
| 355 |
|
| 356 |
if (unlikely(ptr == NULL)) |
| 357 |
return NULL; |
| 358 |
|
| 359 |
info = get_header(ptr); |
| 360 |
return info->parent ? PTR_FROM_HEADER(info->parent) : NULL; |
| 361 |
} |
| 362 |
|
| 363 |
void |
| 364 |
ralloc_set_destructor(const void *ptr, void(*destructor)(void *)) |
| 365 |
{ |
| 366 |
ralloc_header *info = get_header(ptr); |
| 367 |
info->destructor = destructor; |
| 368 |
} |
| 369 |
|
| 370 |
char * |
| 371 |
ralloc_strdup(const void *ctx, const char *str) |
| 372 |
{ |
| 373 |
size_t n; |
| 374 |
char *ptr; |
| 375 |
|
| 376 |
if (unlikely(str == NULL)) |
| 377 |
return NULL; |
| 378 |
|
| 379 |
n = strlen(str); |
| 380 |
ptr = ralloc_array(ctx, char, n + 1); |
| 381 |
memcpy(ptr, str, n); |
| 382 |
ptr[n] = '\0'; |
| 383 |
return ptr; |
| 384 |
} |
| 385 |
|
| 386 |
char * |
| 387 |
ralloc_strndup(const void *ctx, const char *str, size_t max) |
| 388 |
{ |
| 389 |
size_t n; |
| 390 |
char *ptr; |
| 391 |
|
| 392 |
if (unlikely(str == NULL)) |
| 393 |
return NULL; |
| 394 |
|
| 395 |
n = strnlen(str, max); |
| 396 |
ptr = ralloc_array(ctx, char, n + 1); |
| 397 |
memcpy(ptr, str, n); |
| 398 |
ptr[n] = '\0'; |
| 399 |
return ptr; |
| 400 |
} |
| 401 |
|
| 402 |
/* helper routine for strcat/strncat - n is the exact amount to copy */ |
| 403 |
static bool |
| 404 |
cat(char **dest, const char *str, size_t n) |
| 405 |
{ |
| 406 |
char *both; |
| 407 |
size_t existing_length; |
| 408 |
assert(dest != NULL && *dest != NULL); |
| 409 |
|
| 410 |
existing_length = strlen(*dest); |
| 411 |
both = resize(*dest, existing_length + n + 1); |
| 412 |
if (unlikely(both == NULL)) |
| 413 |
return false; |
| 414 |
|
| 415 |
memcpy(both + existing_length, str, n); |
| 416 |
both[existing_length + n] = '\0'; |
| 417 |
|
| 418 |
*dest = both; |
| 419 |
return true; |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
bool |
| 424 |
ralloc_strcat(char **dest, const char *str) |
| 425 |
{ |
| 426 |
return cat(dest, str, strlen(str)); |
| 427 |
} |
| 428 |
|
| 429 |
bool |
| 430 |
ralloc_strncat(char **dest, const char *str, size_t n) |
| 431 |
{ |
| 432 |
return cat(dest, str, strnlen(str, n)); |
| 433 |
} |
| 434 |
|
| 435 |
bool |
| 436 |
ralloc_str_append(char **dest, const char *str, |
| 437 |
size_t existing_length, size_t str_size) |
| 438 |
{ |
| 439 |
char *both; |
| 440 |
assert(dest != NULL && *dest != NULL); |
| 441 |
|
| 442 |
both = resize(*dest, existing_length + str_size + 1); |
| 443 |
if (unlikely(both == NULL)) |
| 444 |
return false; |
| 445 |
|
| 446 |
memcpy(both + existing_length, str, str_size); |
| 447 |
both[existing_length + str_size] = '\0'; |
| 448 |
|
| 449 |
*dest = both; |
| 450 |
|
| 451 |
return true; |
| 452 |
} |
| 453 |
|
| 454 |
char * |
| 455 |
ralloc_asprintf(const void *ctx, const char *fmt, ...) |
| 456 |
{ |
| 457 |
char *ptr; |
| 458 |
va_list args; |
| 459 |
va_start(args, fmt); |
| 460 |
ptr = ralloc_vasprintf(ctx, fmt, args); |
| 461 |
va_end(args); |
| 462 |
return ptr; |
| 463 |
} |
| 464 |
|
| 465 |
size_t |
| 466 |
printf_length(const char *fmt, va_list untouched_args) |
| 467 |
{ |
| 468 |
int size; |
| 469 |
char junk; |
| 470 |
|
| 471 |
/* Make a copy of the va_list so the original caller can still use it */ |
| 472 |
va_list args; |
| 473 |
va_copy(args, untouched_args); |
| 474 |
|
| 475 |
#ifdef _WIN32 |
| 476 |
/* We need to use _vcsprintf to calculate the size as vsnprintf returns -1 |
| 477 |
* if the number of characters to write is greater than count. |
| 478 |
*/ |
| 479 |
size = _vscprintf(fmt, args); |
| 480 |
(void)junk; |
| 481 |
#else |
| 482 |
size = vsnprintf(&junk, 1, fmt, args); |
| 483 |
#endif |
| 484 |
assert(size >= 0); |
| 485 |
|
| 486 |
va_end(args); |
| 487 |
|
| 488 |
return size; |
| 489 |
} |
| 490 |
|
| 491 |
char * |
| 492 |
ralloc_vasprintf(const void *ctx, const char *fmt, va_list args) |
| 493 |
{ |
| 494 |
size_t size = printf_length(fmt, args) + 1; |
| 495 |
|
| 496 |
char *ptr = ralloc_size(ctx, size); |
| 497 |
if (ptr != NULL) |
| 498 |
vsnprintf(ptr, size, fmt, args); |
| 499 |
|
| 500 |
return ptr; |
| 501 |
} |
| 502 |
|
| 503 |
bool |
| 504 |
ralloc_asprintf_append(char **str, const char *fmt, ...) |
| 505 |
{ |
| 506 |
bool success; |
| 507 |
va_list args; |
| 508 |
va_start(args, fmt); |
| 509 |
success = ralloc_vasprintf_append(str, fmt, args); |
| 510 |
va_end(args); |
| 511 |
return success; |
| 512 |
} |
| 513 |
|
| 514 |
bool |
| 515 |
ralloc_vasprintf_append(char **str, const char *fmt, va_list args) |
| 516 |
{ |
| 517 |
size_t existing_length; |
| 518 |
assert(str != NULL); |
| 519 |
existing_length = *str ? strlen(*str) : 0; |
| 520 |
return ralloc_vasprintf_rewrite_tail(str, &existing_length, fmt, args); |
| 521 |
} |
| 522 |
|
| 523 |
bool |
| 524 |
ralloc_asprintf_rewrite_tail(char **str, size_t *start, const char *fmt, ...) |
| 525 |
{ |
| 526 |
bool success; |
| 527 |
va_list args; |
| 528 |
va_start(args, fmt); |
| 529 |
success = ralloc_vasprintf_rewrite_tail(str, start, fmt, args); |
| 530 |
va_end(args); |
| 531 |
return success; |
| 532 |
} |
| 533 |
|
| 534 |
bool |
| 535 |
ralloc_vasprintf_rewrite_tail(char **str, size_t *start, const char *fmt, |
| 536 |
va_list args) |
| 537 |
{ |
| 538 |
size_t new_length; |
| 539 |
char *ptr; |
| 540 |
|
| 541 |
assert(str != NULL); |
| 542 |
|
| 543 |
if (unlikely(*str == NULL)) { |
| 544 |
// Assuming a NULL context is probably bad, but it's expected behavior. |
| 545 |
*str = ralloc_vasprintf(NULL, fmt, args); |
| 546 |
*start = strlen(*str); |
| 547 |
return true; |
| 548 |
} |
| 549 |
|
| 550 |
new_length = printf_length(fmt, args); |
| 551 |
|
| 552 |
ptr = resize(*str, *start + new_length + 1); |
| 553 |
if (unlikely(ptr == NULL)) |
| 554 |
return false; |
| 555 |
|
| 556 |
vsnprintf(ptr + *start, new_length + 1, fmt, args); |
| 557 |
*str = ptr; |
| 558 |
*start += new_length; |
| 559 |
return true; |
| 560 |
} |
| 561 |
|
| 562 |
/*************************************************************************** |
| 563 |
* Linear allocator for short-lived allocations. |
| 564 |
*************************************************************************** |
| 565 |
* |
| 566 |
* The allocator consists of a parent node (2K buffer), which requires |
| 567 |
* a ralloc parent, and child nodes (allocations). Child nodes can't be freed |
| 568 |
* directly, because the parent doesn't track them. You have to release |
| 569 |
* the parent node in order to release all its children. |
| 570 |
* |
| 571 |
* The allocator uses a fixed-sized buffer with a monotonically increasing |
| 572 |
* offset after each allocation. If the buffer is all used, another buffer |
| 573 |
* is allocated, sharing the same ralloc parent, so all buffers are at |
| 574 |
* the same level in the ralloc hierarchy. |
| 575 |
* |
| 576 |
* The linear parent node is always the first buffer and keeps track of all |
| 577 |
* other buffers. |
| 578 |
*/ |
| 579 |
|
| 580 |
#define MIN_LINEAR_BUFSIZE 2048 |
| 581 |
#define SUBALLOC_ALIGNMENT 8 |
| 582 |
#define LMAGIC 0x87b9c7d3 |
| 583 |
|
| 584 |
struct |
| 585 |
#ifdef _MSC_VER |
| 586 |
__declspec(align(8)) |
| 587 |
#elif defined(__LP64__) |
| 588 |
__attribute__((aligned(16))) |
| 589 |
#else |
| 590 |
__attribute__((aligned(8))) |
| 591 |
#endif |
| 592 |
linear_header { |
| 593 |
#ifndef NDEBUG |
| 594 |
unsigned magic; /* for debugging */ |
| 595 |
#endif |
| 596 |
unsigned offset; /* points to the first unused byte in the buffer */ |
| 597 |
unsigned size; /* size of the buffer */ |
| 598 |
void *ralloc_parent; /* new buffers will use this */ |
| 599 |
struct linear_header *next; /* next buffer if we have more */ |
| 600 |
struct linear_header *latest; /* the only buffer that has free space */ |
| 601 |
|
| 602 |
/* After this structure, the buffer begins. |
| 603 |
* Each suballocation consists of linear_size_chunk as its header followed |
| 604 |
* by the suballocation, so it goes: |
| 605 |
* |
| 606 |
* - linear_size_chunk |
| 607 |
* - allocated space |
| 608 |
* - linear_size_chunk |
| 609 |
* - allocated space |
| 610 |
* etc. |
| 611 |
* |
| 612 |
* linear_size_chunk is only needed by linear_realloc. |
| 613 |
*/ |
| 614 |
}; |
| 615 |
|
| 616 |
struct linear_size_chunk { |
| 617 |
unsigned size; /* for realloc */ |
| 618 |
unsigned _padding; |
| 619 |
}; |
| 620 |
|
| 621 |
typedef struct linear_header linear_header; |
| 622 |
typedef struct linear_size_chunk linear_size_chunk; |
| 623 |
|
| 624 |
#define LINEAR_PARENT_TO_HEADER(parent) \ |
| 625 |
(linear_header*) \ |
| 626 |
((char*)(parent) - sizeof(linear_size_chunk) - sizeof(linear_header)) |
| 627 |
|
| 628 |
/* Allocate the linear buffer with its header. */ |
| 629 |
static linear_header * |
| 630 |
create_linear_node(void *ralloc_ctx, unsigned min_size) |
| 631 |
{ |
| 632 |
linear_header *node; |
| 633 |
|
| 634 |
min_size += sizeof(linear_size_chunk); |
| 635 |
|
| 636 |
if (likely(min_size < MIN_LINEAR_BUFSIZE)) |
| 637 |
min_size = MIN_LINEAR_BUFSIZE; |
| 638 |
|
| 639 |
node = ralloc_size(ralloc_ctx, sizeof(linear_header) + min_size); |
| 640 |
if (unlikely(!node)) |
| 641 |
return NULL; |
| 642 |
|
| 643 |
#ifndef NDEBUG |
| 644 |
node->magic = LMAGIC; |
| 645 |
#endif |
| 646 |
node->offset = 0; |
| 647 |
node->size = min_size; |
| 648 |
node->ralloc_parent = ralloc_ctx; |
| 649 |
node->next = NULL; |
| 650 |
node->latest = node; |
| 651 |
return node; |
| 652 |
} |
| 653 |
|
| 654 |
void * |
| 655 |
linear_alloc_child(void *parent, unsigned size) |
| 656 |
{ |
| 657 |
linear_header *first = LINEAR_PARENT_TO_HEADER(parent); |
| 658 |
linear_header *latest = first->latest; |
| 659 |
linear_header *new_node; |
| 660 |
linear_size_chunk *ptr; |
| 661 |
unsigned full_size; |
| 662 |
|
| 663 |
assert(first->magic == LMAGIC); |
| 664 |
assert(!latest->next); |
| 665 |
|
| 666 |
size = ALIGN_POT(size, SUBALLOC_ALIGNMENT); |
| 667 |
full_size = sizeof(linear_size_chunk) + size; |
| 668 |
|
| 669 |
if (unlikely(latest->offset + full_size > latest->size)) { |
| 670 |
/* allocate a new node */ |
| 671 |
new_node = create_linear_node(latest->ralloc_parent, size); |
| 672 |
if (unlikely(!new_node)) |
| 673 |
return NULL; |
| 674 |
|
| 675 |
first->latest = new_node; |
| 676 |
latest->latest = new_node; |
| 677 |
latest->next = new_node; |
| 678 |
latest = new_node; |
| 679 |
} |
| 680 |
|
| 681 |
ptr = (linear_size_chunk *)((char*)&latest[1] + latest->offset); |
| 682 |
ptr->size = size; |
| 683 |
latest->offset += full_size; |
| 684 |
|
| 685 |
assert((uintptr_t)&ptr[1] % SUBALLOC_ALIGNMENT == 0); |
| 686 |
return &ptr[1]; |
| 687 |
} |
| 688 |
|
| 689 |
void * |
| 690 |
linear_alloc_parent(void *ralloc_ctx, unsigned size) |
| 691 |
{ |
| 692 |
linear_header *node; |
| 693 |
|
| 694 |
if (unlikely(!ralloc_ctx)) |
| 695 |
return NULL; |
| 696 |
|
| 697 |
size = ALIGN_POT(size, SUBALLOC_ALIGNMENT); |
| 698 |
|
| 699 |
node = create_linear_node(ralloc_ctx, size); |
| 700 |
if (unlikely(!node)) |
| 701 |
return NULL; |
| 702 |
|
| 703 |
return linear_alloc_child((char*)node + |
| 704 |
sizeof(linear_header) + |
| 705 |
sizeof(linear_size_chunk), size); |
| 706 |
} |
| 707 |
|
| 708 |
void * |
| 709 |
linear_zalloc_child(void *parent, unsigned size) |
| 710 |
{ |
| 711 |
void *ptr = linear_alloc_child(parent, size); |
| 712 |
|
| 713 |
if (likely(ptr)) |
| 714 |
memset(ptr, 0, size); |
| 715 |
return ptr; |
| 716 |
} |
| 717 |
|
| 718 |
void * |
| 719 |
linear_zalloc_parent(void *parent, unsigned size) |
| 720 |
{ |
| 721 |
void *ptr = linear_alloc_parent(parent, size); |
| 722 |
|
| 723 |
if (likely(ptr)) |
| 724 |
memset(ptr, 0, size); |
| 725 |
return ptr; |
| 726 |
} |
| 727 |
|
| 728 |
void |
| 729 |
linear_free_parent(void *ptr) |
| 730 |
{ |
| 731 |
linear_header *node; |
| 732 |
|
| 733 |
if (unlikely(!ptr)) |
| 734 |
return; |
| 735 |
|
| 736 |
node = LINEAR_PARENT_TO_HEADER(ptr); |
| 737 |
assert(node->magic == LMAGIC); |
| 738 |
|
| 739 |
while (node) { |
| 740 |
void *ptr = node; |
| 741 |
|
| 742 |
node = node->next; |
| 743 |
ralloc_free(ptr); |
| 744 |
} |
| 745 |
} |
| 746 |
|
| 747 |
void |
| 748 |
ralloc_steal_linear_parent(void *new_ralloc_ctx, void *ptr) |
| 749 |
{ |
| 750 |
linear_header *node; |
| 751 |
|
| 752 |
if (unlikely(!ptr)) |
| 753 |
return; |
| 754 |
|
| 755 |
node = LINEAR_PARENT_TO_HEADER(ptr); |
| 756 |
assert(node->magic == LMAGIC); |
| 757 |
|
| 758 |
while (node) { |
| 759 |
ralloc_steal(new_ralloc_ctx, node); |
| 760 |
node->ralloc_parent = new_ralloc_ctx; |
| 761 |
node = node->next; |
| 762 |
} |
| 763 |
} |
| 764 |
|
| 765 |
void * |
| 766 |
ralloc_parent_of_linear_parent(void *ptr) |
| 767 |
{ |
| 768 |
linear_header *node = LINEAR_PARENT_TO_HEADER(ptr); |
| 769 |
assert(node->magic == LMAGIC); |
| 770 |
return node->ralloc_parent; |
| 771 |
} |
| 772 |
|
| 773 |
void * |
| 774 |
linear_realloc(void *parent, void *old, unsigned new_size) |
| 775 |
{ |
| 776 |
unsigned old_size = 0; |
| 777 |
ralloc_header *new_ptr; |
| 778 |
|
| 779 |
new_ptr = linear_alloc_child(parent, new_size); |
| 780 |
|
| 781 |
if (unlikely(!old)) |
| 782 |
return new_ptr; |
| 783 |
|
| 784 |
old_size = ((linear_size_chunk*)old)[-1].size; |
| 785 |
|
| 786 |
if (likely(new_ptr && old_size)) |
| 787 |
memcpy(new_ptr, old, MIN2(old_size, new_size)); |
| 788 |
|
| 789 |
return new_ptr; |
| 790 |
} |
| 791 |
|
| 792 |
/* All code below is pretty much copied from ralloc and only the alloc |
| 793 |
* calls are different. |
| 794 |
*/ |
| 795 |
|
| 796 |
char * |
| 797 |
linear_strdup(void *parent, const char *str) |
| 798 |
{ |
| 799 |
unsigned n; |
| 800 |
char *ptr; |
| 801 |
|
| 802 |
if (unlikely(!str)) |
| 803 |
return NULL; |
| 804 |
|
| 805 |
n = strlen(str); |
| 806 |
ptr = linear_alloc_child(parent, n + 1); |
| 807 |
if (unlikely(!ptr)) |
| 808 |
return NULL; |
| 809 |
|
| 810 |
memcpy(ptr, str, n); |
| 811 |
ptr[n] = '\0'; |
| 812 |
return ptr; |
| 813 |
} |
| 814 |
|
| 815 |
char * |
| 816 |
linear_asprintf(void *parent, const char *fmt, ...) |
| 817 |
{ |
| 818 |
char *ptr; |
| 819 |
va_list args; |
| 820 |
va_start(args, fmt); |
| 821 |
ptr = linear_vasprintf(parent, fmt, args); |
| 822 |
va_end(args); |
| 823 |
return ptr; |
| 824 |
} |
| 825 |
|
| 826 |
char * |
| 827 |
linear_vasprintf(void *parent, const char *fmt, va_list args) |
| 828 |
{ |
| 829 |
unsigned size = printf_length(fmt, args) + 1; |
| 830 |
|
| 831 |
char *ptr = linear_alloc_child(parent, size); |
| 832 |
if (ptr != NULL) |
| 833 |
vsnprintf(ptr, size, fmt, args); |
| 834 |
|
| 835 |
return ptr; |
| 836 |
} |
| 837 |
|
| 838 |
bool |
| 839 |
linear_asprintf_append(void *parent, char **str, const char *fmt, ...) |
| 840 |
{ |
| 841 |
bool success; |
| 842 |
va_list args; |
| 843 |
va_start(args, fmt); |
| 844 |
success = linear_vasprintf_append(parent, str, fmt, args); |
| 845 |
va_end(args); |
| 846 |
return success; |
| 847 |
} |
| 848 |
|
| 849 |
bool |
| 850 |
linear_vasprintf_append(void *parent, char **str, const char *fmt, va_list args) |
| 851 |
{ |
| 852 |
size_t existing_length; |
| 853 |
assert(str != NULL); |
| 854 |
existing_length = *str ? strlen(*str) : 0; |
| 855 |
return linear_vasprintf_rewrite_tail(parent, str, &existing_length, fmt, args); |
| 856 |
} |
| 857 |
|
| 858 |
bool |
| 859 |
linear_asprintf_rewrite_tail(void *parent, char **str, size_t *start, |
| 860 |
const char *fmt, ...) |
| 861 |
{ |
| 862 |
bool success; |
| 863 |
va_list args; |
| 864 |
va_start(args, fmt); |
| 865 |
success = linear_vasprintf_rewrite_tail(parent, str, start, fmt, args); |
| 866 |
va_end(args); |
| 867 |
return success; |
| 868 |
} |
| 869 |
|
| 870 |
bool |
| 871 |
linear_vasprintf_rewrite_tail(void *parent, char **str, size_t *start, |
| 872 |
const char *fmt, va_list args) |
| 873 |
{ |
| 874 |
size_t new_length; |
| 875 |
char *ptr; |
| 876 |
|
| 877 |
assert(str != NULL); |
| 878 |
|
| 879 |
if (unlikely(*str == NULL)) { |
| 880 |
*str = linear_vasprintf(parent, fmt, args); |
| 881 |
*start = strlen(*str); |
| 882 |
return true; |
| 883 |
} |
| 884 |
|
| 885 |
new_length = printf_length(fmt, args); |
| 886 |
|
| 887 |
ptr = linear_realloc(parent, *str, *start + new_length + 1); |
| 888 |
if (unlikely(ptr == NULL)) |
| 889 |
return false; |
| 890 |
|
| 891 |
vsnprintf(ptr + *start, new_length + 1, fmt, args); |
| 892 |
*str = ptr; |
| 893 |
*start += new_length; |
| 894 |
return true; |
| 895 |
} |
| 896 |
|
| 897 |
/* helper routine for strcat/strncat - n is the exact amount to copy */ |
| 898 |
static bool |
| 899 |
linear_cat(void *parent, char **dest, const char *str, unsigned n) |
| 900 |
{ |
| 901 |
char *both; |
| 902 |
unsigned existing_length; |
| 903 |
assert(dest != NULL && *dest != NULL); |
| 904 |
|
| 905 |
existing_length = strlen(*dest); |
| 906 |
both = linear_realloc(parent, *dest, existing_length + n + 1); |
| 907 |
if (unlikely(both == NULL)) |
| 908 |
return false; |
| 909 |
|
| 910 |
memcpy(both + existing_length, str, n); |
| 911 |
both[existing_length + n] = '\0'; |
| 912 |
|
| 913 |
*dest = both; |
| 914 |
return true; |
| 915 |
} |
| 916 |
|
| 917 |
bool |
| 918 |
linear_strcat(void *parent, char **dest, const char *str) |
| 919 |
{ |
| 920 |
return linear_cat(parent, dest, str, strlen(str)); |
| 921 |
} |