ralloc.c revision af69d88d
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 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 atexit(autofree); 300 } 301 return autofree_context; 302} 303 304void 305ralloc_set_destructor(const void *ptr, void(*destructor)(void *)) 306{ 307 ralloc_header *info = get_header(ptr); 308 info->destructor = destructor; 309} 310 311char * 312ralloc_strdup(const void *ctx, const char *str) 313{ 314 size_t n; 315 char *ptr; 316 317 if (unlikely(str == NULL)) 318 return NULL; 319 320 n = strlen(str); 321 ptr = ralloc_array(ctx, char, n + 1); 322 memcpy(ptr, str, n); 323 ptr[n] = '\0'; 324 return ptr; 325} 326 327char * 328ralloc_strndup(const void *ctx, const char *str, size_t max) 329{ 330 size_t n; 331 char *ptr; 332 333 if (unlikely(str == NULL)) 334 return NULL; 335 336 n = strlen(str); 337 if (n > max) 338 n = max; 339 340 ptr = ralloc_array(ctx, char, n + 1); 341 memcpy(ptr, str, n); 342 ptr[n] = '\0'; 343 return ptr; 344} 345 346/* helper routine for strcat/strncat - n is the exact amount to copy */ 347static bool 348cat(char **dest, const char *str, size_t n) 349{ 350 char *both; 351 size_t existing_length; 352 assert(dest != NULL && *dest != NULL); 353 354 existing_length = strlen(*dest); 355 both = resize(*dest, existing_length + n + 1); 356 if (unlikely(both == NULL)) 357 return false; 358 359 memcpy(both + existing_length, str, n); 360 both[existing_length + n] = '\0'; 361 362 *dest = both; 363 return true; 364} 365 366 367bool 368ralloc_strcat(char **dest, const char *str) 369{ 370 return cat(dest, str, strlen(str)); 371} 372 373bool 374ralloc_strncat(char **dest, const char *str, size_t n) 375{ 376 /* Clamp n to the string length */ 377 size_t str_length = strlen(str); 378 if (str_length < n) 379 n = str_length; 380 381 return cat(dest, str, n); 382} 383 384char * 385ralloc_asprintf(const void *ctx, const char *fmt, ...) 386{ 387 char *ptr; 388 va_list args; 389 va_start(args, fmt); 390 ptr = ralloc_vasprintf(ctx, fmt, args); 391 va_end(args); 392 return ptr; 393} 394 395/* Return the length of the string that would be generated by a printf-style 396 * format and argument list, not including the \0 byte. 397 */ 398static size_t 399printf_length(const char *fmt, va_list untouched_args) 400{ 401 int size; 402 char junk; 403 404 /* Make a copy of the va_list so the original caller can still use it */ 405 va_list args; 406 va_copy(args, untouched_args); 407 408#ifdef _WIN32 409 /* We need to use _vcsprintf to calculate the size as vsnprintf returns -1 410 * if the number of characters to write is greater than count. 411 */ 412 size = _vscprintf(fmt, args); 413 (void)junk; 414#else 415 size = vsnprintf(&junk, 1, fmt, args); 416#endif 417 assert(size >= 0); 418 419 va_end(args); 420 421 return size; 422} 423 424char * 425ralloc_vasprintf(const void *ctx, const char *fmt, va_list args) 426{ 427 size_t size = printf_length(fmt, args) + 1; 428 429 char *ptr = ralloc_size(ctx, size); 430 if (ptr != NULL) 431 vsnprintf(ptr, size, fmt, args); 432 433 return ptr; 434} 435 436bool 437ralloc_asprintf_append(char **str, const char *fmt, ...) 438{ 439 bool success; 440 va_list args; 441 va_start(args, fmt); 442 success = ralloc_vasprintf_append(str, fmt, args); 443 va_end(args); 444 return success; 445} 446 447bool 448ralloc_vasprintf_append(char **str, const char *fmt, va_list args) 449{ 450 size_t existing_length; 451 assert(str != NULL); 452 existing_length = *str ? strlen(*str) : 0; 453 return ralloc_vasprintf_rewrite_tail(str, &existing_length, fmt, args); 454} 455 456bool 457ralloc_asprintf_rewrite_tail(char **str, size_t *start, const char *fmt, ...) 458{ 459 bool success; 460 va_list args; 461 va_start(args, fmt); 462 success = ralloc_vasprintf_rewrite_tail(str, start, fmt, args); 463 va_end(args); 464 return success; 465} 466 467bool 468ralloc_vasprintf_rewrite_tail(char **str, size_t *start, const char *fmt, 469 va_list args) 470{ 471 size_t new_length; 472 char *ptr; 473 474 assert(str != NULL); 475 476 if (unlikely(*str == NULL)) { 477 // Assuming a NULL context is probably bad, but it's expected behavior. 478 *str = ralloc_vasprintf(NULL, fmt, args); 479 return true; 480 } 481 482 new_length = printf_length(fmt, args); 483 484 ptr = resize(*str, *start + new_length + 1); 485 if (unlikely(ptr == NULL)) 486 return false; 487 488 vsnprintf(ptr + *start, new_length + 1, fmt, args); 489 *str = ptr; 490 *start += new_length; 491 return true; 492} 493