ralloc.c revision 848b8605
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/* Android defines SIZE_MAX in limits.h, instead of the standard stdint.h */ 32#ifdef ANDROID 33#include <limits.h> 34#endif 35 36/* Some versions of MinGW are missing _vscprintf's declaration, although they 37 * still provide the symbol in the import library. */ 38#ifdef __MINGW32__ 39_CRTIMP int _vscprintf(const char *format, va_list argptr); 40#endif 41 42#include "ralloc.h" 43 44#ifndef va_copy 45#ifdef __va_copy 46#define va_copy(dest, src) __va_copy((dest), (src)) 47#else 48#define va_copy(dest, src) (dest) = (src) 49#endif 50#endif 51 52#define CANARY 0x5A1106 53 54struct ralloc_header 55{ 56#ifdef DEBUG 57 /* A canary value used to determine whether a pointer is ralloc'd. */ 58 unsigned canary; 59#endif 60 61 struct ralloc_header *parent; 62 63 /* The first child (head of a linked list) */ 64 struct ralloc_header *child; 65 66 /* Linked list of siblings */ 67 struct ralloc_header *prev; 68 struct ralloc_header *next; 69 70 void (*destructor)(void *); 71}; 72 73typedef struct ralloc_header ralloc_header; 74 75static void unlink_block(ralloc_header *info); 76static void unsafe_free(ralloc_header *info); 77 78static ralloc_header * 79get_header(const void *ptr) 80{ 81 ralloc_header *info = (ralloc_header *) (((char *) ptr) - 82 sizeof(ralloc_header)); 83#ifdef DEBUG 84 assert(info->canary == CANARY); 85#endif 86 return info; 87} 88 89#define PTR_FROM_HEADER(info) (((char *) info) + sizeof(ralloc_header)) 90 91static void 92add_child(ralloc_header *parent, ralloc_header *info) 93{ 94 if (parent != NULL) { 95 info->parent = parent; 96 info->next = parent->child; 97 parent->child = info; 98 99 if (info->next != NULL) 100 info->next->prev = info; 101 } 102} 103 104void * 105ralloc_context(const void *ctx) 106{ 107 return ralloc_size(ctx, 0); 108} 109 110void * 111ralloc_size(const void *ctx, size_t size) 112{ 113 void *block = calloc(1, size + sizeof(ralloc_header)); 114 ralloc_header *info; 115 ralloc_header *parent; 116 117 if (unlikely(block == NULL)) 118 return NULL; 119 info = (ralloc_header *) block; 120 parent = ctx != NULL ? get_header(ctx) : NULL; 121 122 add_child(parent, info); 123 124#ifdef DEBUG 125 info->canary = CANARY; 126#endif 127 128 return PTR_FROM_HEADER(info); 129} 130 131void * 132rzalloc_size(const void *ctx, size_t size) 133{ 134 void *ptr = ralloc_size(ctx, size); 135 if (likely(ptr != NULL)) 136 memset(ptr, 0, size); 137 return ptr; 138} 139 140/* helper function - assumes ptr != NULL */ 141static void * 142resize(void *ptr, size_t size) 143{ 144 ralloc_header *child, *old, *info; 145 146 old = get_header(ptr); 147 info = realloc(old, size + sizeof(ralloc_header)); 148 149 if (info == NULL) 150 return NULL; 151 152 /* Update parent and sibling's links to the reallocated node. */ 153 if (info != old && info->parent != NULL) { 154 if (info->parent->child == old) 155 info->parent->child = info; 156 157 if (info->prev != NULL) 158 info->prev->next = info; 159 160 if (info->next != NULL) 161 info->next->prev = info; 162 } 163 164 /* Update child->parent links for all children */ 165 for (child = info->child; child != NULL; child = child->next) 166 child->parent = info; 167 168 return PTR_FROM_HEADER(info); 169} 170 171void * 172reralloc_size(const void *ctx, void *ptr, size_t size) 173{ 174 if (unlikely(ptr == NULL)) 175 return ralloc_size(ctx, size); 176 177 assert(ralloc_parent(ptr) == ctx); 178 return resize(ptr, size); 179} 180 181void * 182ralloc_array_size(const void *ctx, size_t size, unsigned count) 183{ 184 if (count > SIZE_MAX/size) 185 return NULL; 186 187 return ralloc_size(ctx, size * count); 188} 189 190void * 191rzalloc_array_size(const void *ctx, size_t size, unsigned count) 192{ 193 if (count > SIZE_MAX/size) 194 return NULL; 195 196 return rzalloc_size(ctx, size * count); 197} 198 199void * 200reralloc_array_size(const void *ctx, void *ptr, size_t size, unsigned count) 201{ 202 if (count > SIZE_MAX/size) 203 return NULL; 204 205 return reralloc_size(ctx, ptr, size * count); 206} 207 208void 209ralloc_free(void *ptr) 210{ 211 ralloc_header *info; 212 213 if (ptr == NULL) 214 return; 215 216 info = get_header(ptr); 217 unlink_block(info); 218 unsafe_free(info); 219} 220 221static void 222unlink_block(ralloc_header *info) 223{ 224 /* Unlink from parent & siblings */ 225 if (info->parent != NULL) { 226 if (info->parent->child == info) 227 info->parent->child = info->next; 228 229 if (info->prev != NULL) 230 info->prev->next = info->next; 231 232 if (info->next != NULL) 233 info->next->prev = info->prev; 234 } 235 info->parent = NULL; 236 info->prev = NULL; 237 info->next = NULL; 238} 239 240static void 241unsafe_free(ralloc_header *info) 242{ 243 /* Recursively free any children...don't waste time unlinking them. */ 244 ralloc_header *temp; 245 while (info->child != NULL) { 246 temp = info->child; 247 info->child = temp->next; 248 unsafe_free(temp); 249 } 250 251 /* Free the block itself. Call the destructor first, if any. */ 252 if (info->destructor != NULL) 253 info->destructor(PTR_FROM_HEADER(info)); 254 255 free(info); 256} 257 258void 259ralloc_steal(const void *new_ctx, void *ptr) 260{ 261 ralloc_header *info, *parent; 262 263 if (unlikely(ptr == NULL)) 264 return; 265 266 info = get_header(ptr); 267 parent = get_header(new_ctx); 268 269 unlink_block(info); 270 271 add_child(parent, info); 272} 273 274void * 275ralloc_parent(const void *ptr) 276{ 277 ralloc_header *info; 278 279 if (unlikely(ptr == NULL)) 280 return NULL; 281 282 info = get_header(ptr); 283 return info->parent ? PTR_FROM_HEADER(info->parent) : NULL; 284} 285 286static void *autofree_context = NULL; 287 288static void __attribute__((__destructor__)) 289autofree(void) 290{ 291 ralloc_free(autofree_context); 292} 293 294void * 295ralloc_autofree_context(void) 296{ 297 if (unlikely(autofree_context == NULL)) { 298 autofree_context = ralloc_context(NULL); 299 } 300 return autofree_context; 301} 302 303void 304ralloc_set_destructor(const void *ptr, void(*destructor)(void *)) 305{ 306 ralloc_header *info = get_header(ptr); 307 info->destructor = destructor; 308} 309 310char * 311ralloc_strdup(const void *ctx, const char *str) 312{ 313 size_t n; 314 char *ptr; 315 316 if (unlikely(str == NULL)) 317 return NULL; 318 319 n = strlen(str); 320 ptr = ralloc_array(ctx, char, n + 1); 321 memcpy(ptr, str, n); 322 ptr[n] = '\0'; 323 return ptr; 324} 325 326char * 327ralloc_strndup(const void *ctx, const char *str, size_t max) 328{ 329 size_t n; 330 char *ptr; 331 332 if (unlikely(str == NULL)) 333 return NULL; 334 335 n = strlen(str); 336 if (n > max) 337 n = max; 338 339 ptr = ralloc_array(ctx, char, n + 1); 340 memcpy(ptr, str, n); 341 ptr[n] = '\0'; 342 return ptr; 343} 344 345/* helper routine for strcat/strncat - n is the exact amount to copy */ 346static bool 347cat(char **dest, const char *str, size_t n) 348{ 349 char *both; 350 size_t existing_length; 351 assert(dest != NULL && *dest != NULL); 352 353 existing_length = strlen(*dest); 354 both = resize(*dest, existing_length + n + 1); 355 if (unlikely(both == NULL)) 356 return false; 357 358 memcpy(both + existing_length, str, n); 359 both[existing_length + n] = '\0'; 360 361 *dest = both; 362 return true; 363} 364 365 366bool 367ralloc_strcat(char **dest, const char *str) 368{ 369 return cat(dest, str, strlen(str)); 370} 371 372bool 373ralloc_strncat(char **dest, const char *str, size_t n) 374{ 375 /* Clamp n to the string length */ 376 size_t str_length = strlen(str); 377 if (str_length < n) 378 n = str_length; 379 380 return cat(dest, str, n); 381} 382 383char * 384ralloc_asprintf(const void *ctx, const char *fmt, ...) 385{ 386 char *ptr; 387 va_list args; 388 va_start(args, fmt); 389 ptr = ralloc_vasprintf(ctx, fmt, args); 390 va_end(args); 391 return ptr; 392} 393 394/* Return the length of the string that would be generated by a printf-style 395 * format and argument list, not including the \0 byte. 396 */ 397static size_t 398printf_length(const char *fmt, va_list untouched_args) 399{ 400 int size; 401 char junk; 402 403 /* Make a copy of the va_list so the original caller can still use it */ 404 va_list args; 405 va_copy(args, untouched_args); 406 407#ifdef _WIN32 408 /* We need to use _vcsprintf to calculate the size as vsnprintf returns -1 409 * if the number of characters to write is greater than count. 410 */ 411 size = _vscprintf(fmt, args); 412 (void)junk; 413#else 414 size = vsnprintf(&junk, 1, fmt, args); 415#endif 416 assert(size >= 0); 417 418 va_end(args); 419 420 return size; 421} 422 423char * 424ralloc_vasprintf(const void *ctx, const char *fmt, va_list args) 425{ 426 size_t size = printf_length(fmt, args) + 1; 427 428 char *ptr = ralloc_size(ctx, size); 429 if (ptr != NULL) 430 vsnprintf(ptr, size, fmt, args); 431 432 return ptr; 433} 434 435bool 436ralloc_asprintf_append(char **str, const char *fmt, ...) 437{ 438 bool success; 439 va_list args; 440 va_start(args, fmt); 441 success = ralloc_vasprintf_append(str, fmt, args); 442 va_end(args); 443 return success; 444} 445 446bool 447ralloc_vasprintf_append(char **str, const char *fmt, va_list args) 448{ 449 size_t existing_length; 450 assert(str != NULL); 451 existing_length = *str ? strlen(*str) : 0; 452 return ralloc_vasprintf_rewrite_tail(str, &existing_length, fmt, args); 453} 454 455bool 456ralloc_asprintf_rewrite_tail(char **str, size_t *start, const char *fmt, ...) 457{ 458 bool success; 459 va_list args; 460 va_start(args, fmt); 461 success = ralloc_vasprintf_rewrite_tail(str, start, fmt, args); 462 va_end(args); 463 return success; 464} 465 466bool 467ralloc_vasprintf_rewrite_tail(char **str, size_t *start, const char *fmt, 468 va_list args) 469{ 470 size_t new_length; 471 char *ptr; 472 473 assert(str != NULL); 474 475 if (unlikely(*str == NULL)) { 476 // Assuming a NULL context is probably bad, but it's expected behavior. 477 *str = ralloc_vasprintf(NULL, fmt, args); 478 return true; 479 } 480 481 new_length = printf_length(fmt, args); 482 483 ptr = resize(*str, *start + new_length + 1); 484 if (unlikely(ptr == NULL)) 485 return false; 486 487 vsnprintf(ptr + *start, new_length + 1, fmt, args); 488 *str = ptr; 489 *start += new_length; 490 return true; 491} 492