tal.h (18998B)
1 /* Licensed under BSD-MIT - see LICENSE file for details */ 2 #ifndef CCAN_TAL_H 3 #define CCAN_TAL_H 4 #include "../config.h" 5 #include "../compiler.h" 6 #include "likely.h" 7 #include "typesafe_cb.h" 8 #include "str.h" 9 #include "take.h" 10 11 #include <stdlib.h> 12 #include <stdbool.h> 13 #include <stdarg.h> 14 15 /** 16 * tal_t - convenient alias for void to mark tal pointers. 17 * 18 * Since any pointer can be a tal-allocated pointer, it's often 19 * useful to use this typedef to mark them explicitly. 20 */ 21 typedef void tal_t; 22 23 /** 24 * tal - basic allocator function 25 * @ctx: NULL, or tal allocated object to be parent. 26 * @type: the type to allocate. 27 * 28 * Allocates a specific type, with a given parent context. The name 29 * of the object is a string of the type, but if CCAN_TAL_DEBUG is 30 * defined it also contains the file and line which allocated it. 31 * 32 * tal_count() of the return will be 1. 33 * 34 * Example: 35 * int *p = tal(NULL, int); 36 * *p = 1; 37 */ 38 #define tal(ctx, type) \ 39 tal_label(ctx, type, TAL_LABEL(type, "")) 40 41 /** 42 * talz - zeroing allocator function 43 * @ctx: NULL, or tal allocated object to be parent. 44 * @type: the type to allocate. 45 * 46 * Equivalent to tal() followed by memset() to zero. 47 * 48 * Example: 49 * p = talz(NULL, int); 50 * assert(*p == 0); 51 */ 52 #define talz(ctx, type) \ 53 talz_label(ctx, type, TAL_LABEL(type, "")) 54 55 /** 56 * tal_free - free a tal-allocated pointer. 57 * @p: NULL, or tal allocated object to free. 58 * 59 * This calls the destructors for p (if any), then does the same for all its 60 * children (recursively) before finally freeing the memory. It returns 61 * NULL, for convenience. 62 * 63 * Note: errno is preserved by this call, and also saved and restored 64 * for any destructors or notifiers. 65 * 66 * Example: 67 * p = tal_free(p); 68 */ 69 void *tal_free(const tal_t *p); 70 71 /** 72 * tal_arr - allocate an array of objects. 73 * @ctx: NULL, or tal allocated object to be parent. 74 * @type: the type to allocate. 75 * @count: the number to allocate. 76 * 77 * tal_count() of the returned pointer will be @count. 78 * 79 * Example: 80 * p = tal_arr(NULL, int, 2); 81 * p[0] = 0; 82 * p[1] = 1; 83 */ 84 #define tal_arr(ctx, type, count) \ 85 tal_arr_label(ctx, type, count, TAL_LABEL(type, "[]")) 86 87 /** 88 * tal_arrz - allocate an array of zeroed objects. 89 * @ctx: NULL, or tal allocated object to be parent. 90 * @type: the type to allocate. 91 * @count: the number to allocate. 92 * 93 * Equivalent to tal_arr() followed by memset() to zero. 94 * 95 * Example: 96 * p = tal_arrz(NULL, int, 2); 97 * assert(p[0] == 0 && p[1] == 0); 98 */ 99 #define tal_arrz(ctx, type, count) \ 100 tal_arrz_label(ctx, type, count, TAL_LABEL(type, "[]")) 101 102 /** 103 * tal_resize - enlarge or reduce a tal object. 104 * @p: A pointer to the tal allocated array to resize. 105 * @count: the number to allocate. 106 * 107 * This returns true on success (and may move *@p), or false on failure. 108 * On success, tal_count() of *@p will be @count. 109 * 110 * Note: if *p is take(), it will still be take() upon return, even if it 111 * has been moved. 112 * 113 * Example: 114 * tal_resize(&p, 100); 115 */ 116 #define tal_resize(p, count) \ 117 tal_resize_((void **)(p), sizeof**(p), (count), false) 118 119 /** 120 * tal_resizez - enlarge or reduce a tal object; zero out extra. 121 * @p: A pointer to the tal allocated array to resize. 122 * @count: the number to allocate. 123 * 124 * This returns true on success (and may move *@p), or false on failure. 125 * 126 * Example: 127 * tal_resizez(&p, 200); 128 */ 129 #define tal_resizez(p, count) \ 130 tal_resize_((void **)(p), sizeof**(p), (count), true) 131 132 /** 133 * tal_steal - change the parent of a tal-allocated pointer. 134 * @ctx: The new parent. 135 * @ptr: The tal allocated object to move, or NULL. 136 * 137 * This may need to perform an allocation, in which case it may fail; thus 138 * it can return NULL, otherwise returns @ptr. If @ptr is NULL, this function does 139 * nothing. 140 */ 141 #if HAVE_STATEMENT_EXPR 142 /* Weird macro avoids gcc's 'warning: value computed is not used'. */ 143 #define tal_steal(ctx, ptr) \ 144 ({ (tal_typeof(ptr) tal_steal_((ctx),(ptr))); }) 145 #else 146 #define tal_steal(ctx, ptr) \ 147 (tal_typeof(ptr) tal_steal_((ctx),(ptr))) 148 #endif 149 150 /** 151 * tal_add_destructor - add a callback function when this context is destroyed. 152 * @ptr: The tal allocated object. 153 * @function: the function to call before it's freed. 154 * 155 * This is a more convenient form of tal_add_notifier(@ptr, 156 * TAL_NOTIFY_FREE, ...), in that the function prototype takes only @ptr. 157 * 158 * Note that this can only fail if your allocfn fails and your errorfn returns. 159 */ 160 #define tal_add_destructor(ptr, function) \ 161 tal_add_destructor_((ptr), typesafe_cb(void, void *, (function), (ptr))) 162 163 /** 164 * tal_del_destructor - remove a destructor callback function. 165 * @ptr: The tal allocated object. 166 * @function: the function to call before it's freed. 167 * 168 * If @function has not been successfully added as a destructor, this returns 169 * false. Note that if we're inside the destructor call itself, this will 170 * return false. 171 */ 172 #define tal_del_destructor(ptr, function) \ 173 tal_del_destructor_((ptr), typesafe_cb(void, void *, (function), (ptr))) 174 175 /** 176 * tal_add_destructor2 - add a 2-arg callback function when context is destroyed. 177 * @ptr: The tal allocated object. 178 * @function: the function to call before it's freed. 179 * @arg: the extra argument to the function. 180 * 181 * Sometimes an extra argument is required for a destructor; this 182 * saves the extra argument internally to avoid the caller having to 183 * do an extra allocation. 184 * 185 * Note that this can only fail if your allocfn fails and your errorfn returns. 186 */ 187 #define tal_add_destructor2(ptr, function, arg) \ 188 tal_add_destructor2_((ptr), \ 189 typesafe_cb_cast(void (*)(tal_t *, void *), \ 190 void (*)(__typeof__(ptr), \ 191 __typeof__(arg)), \ 192 (function)), \ 193 (arg)) 194 195 /** 196 * tal_del_destructor - remove a destructor callback function. 197 * @ptr: The tal allocated object. 198 * @function: the function to call before it's freed. 199 * 200 * If @function has not been successfully added as a destructor, this returns 201 * false. Note that if we're inside the destructor call itself, this will 202 * return false. 203 */ 204 #define tal_del_destructor(ptr, function) \ 205 tal_del_destructor_((ptr), typesafe_cb(void, void *, (function), (ptr))) 206 207 /** 208 * tal_del_destructor2 - remove 2-arg callback function. 209 * @ptr: The tal allocated object. 210 * @function: the function to call before it's freed. 211 * @arg: the extra argument to the function. 212 * 213 * If @function has not been successfully added as a destructor with 214 * @arg, this returns false. 215 */ 216 #define tal_del_destructor2(ptr, function, arg) \ 217 tal_del_destructor2_((ptr), \ 218 typesafe_cb_cast(void (*)(tal_t *, void *), \ 219 void (*)(__typeof__(ptr), \ 220 __typeof__(arg)), \ 221 (function)), \ 222 (arg)) 223 enum tal_notify_type { 224 TAL_NOTIFY_FREE = 1, 225 TAL_NOTIFY_STEAL = 2, 226 TAL_NOTIFY_MOVE = 4, 227 TAL_NOTIFY_RESIZE = 8, 228 TAL_NOTIFY_RENAME = 16, 229 TAL_NOTIFY_ADD_CHILD = 32, 230 TAL_NOTIFY_DEL_CHILD = 64, 231 TAL_NOTIFY_ADD_NOTIFIER = 128, 232 TAL_NOTIFY_DEL_NOTIFIER = 256 233 }; 234 235 /** 236 * tal_add_notifier - add a callback function when this context changes. 237 * @ptr: The tal allocated object, or NULL. 238 * @types: Bitwise OR of the types the callback is interested in. 239 * @callback: the function to call. 240 * 241 * Note that this can only fail if your allocfn fails and your errorfn 242 * returns. Also note that notifiers are not reliable in the case 243 * where an allocation fails, as they may be called before any 244 * allocation is actually done. 245 * 246 * TAL_NOTIFY_FREE is called when @ptr is freed, either directly or 247 * because an ancestor is freed: @info is the argument to tal_free(). 248 * It is exactly equivalent to a destructor, with more information. 249 * errno is set to the value it was at the call of tal_free(). 250 * 251 * TAL_NOTIFY_STEAL is called when @ptr's parent changes: @info is the 252 * new parent. 253 * 254 * TAL_NOTIFY_MOVE is called when @ptr is realloced (via tal_resize) 255 * and moved. In this case, @ptr arg here is the new memory, and 256 * @info is the old pointer. 257 * 258 * TAL_NOTIFY_RESIZE is called when @ptr is realloced via tal_resize: 259 * @info is the new size, in bytes. If the pointer has moved, 260 * TAL_NOTIFY_MOVE callbacks are called first. 261 * 262 * TAL_NOTIFY_ADD_CHILD/TAL_NOTIFY_DEL_CHILD are called when @ptr is 263 * the context for a tal() allocating call, or a direct child is 264 * tal_free()d: @info is the child. Note that TAL_NOTIFY_DEL_CHILD is 265 * not called when this context is tal_free()d: TAL_NOTIFY_FREE is 266 * considered sufficient for that case. 267 * 268 * TAL_NOTIFY_ADD_NOTIFIER/TAL_NOTIFIER_DEL_NOTIFIER are called when a 269 * notifier is added or removed (not for this notifier): @info is the 270 * callback. This is also called for tal_add_destructor and 271 * tal_del_destructor. 272 */ 273 #define tal_add_notifier(ptr, types, callback) \ 274 tal_add_notifier_((ptr), (types), \ 275 typesafe_cb_postargs(void, tal_t *, (callback), \ 276 (ptr), \ 277 enum tal_notify_type, void *)) 278 279 /** 280 * tal_del_notifier - remove a notifier callback function. 281 * @ptr: The tal allocated object. 282 * @callback: the function to call. 283 */ 284 #define tal_del_notifier(ptr, callback) \ 285 tal_del_notifier_((ptr), \ 286 typesafe_cb_postargs(void, void *, (callback), \ 287 (ptr), \ 288 enum tal_notify_type, void *), \ 289 false, NULL) 290 291 /** 292 * tal_set_name - attach a name to a tal pointer. 293 * @ptr: The tal allocated object. 294 * @name: The name to use. 295 * 296 * The name is copied, unless we're certain it's a string literal. 297 */ 298 #define tal_set_name(ptr, name) \ 299 tal_set_name_((ptr), (name), TAL_IS_LITERAL(name)) 300 301 /** 302 * tal_name - get the name for a tal pointer. 303 * @ptr: The tal allocated object. 304 * 305 * Returns NULL if no name has been set. 306 */ 307 const char *tal_name(const tal_t *ptr); 308 309 /** 310 * tal_count - get the count of objects in a tal object. 311 * @ptr: The tal allocated object (or NULL) 312 * 313 * Returns 0 if @ptr is NULL. Note that if the allocation was done as a 314 * different type to @ptr, the result may not match the @count argument 315 * (or implied 1) of that allocation! 316 */ 317 #define tal_count(p) (tal_bytelen(p) / sizeof(*p)) 318 319 /** 320 * tal_bytelen - get the count of bytes in a tal object. 321 * @ptr: The tal allocated object (or NULL) 322 * 323 * Returns 0 if @ptr is NULL. 324 */ 325 size_t tal_bytelen(const tal_t *ptr); 326 327 /** 328 * tal_first - get the first immediate tal object child. 329 * @root: The tal allocated object to start with, or NULL. 330 * 331 * Returns NULL if there are no children. 332 */ 333 tal_t *tal_first(const tal_t *root); 334 335 /** 336 * tal_next - get the next immediate tal object child. 337 * @prev: The return value from tal_first or tal_next. 338 * 339 * Returns NULL if there are no more immediate children. This should be safe to 340 * call on an altering tree unless @prev is no longer valid. 341 */ 342 tal_t *tal_next(const tal_t *prev); 343 344 /** 345 * tal_parent - get the parent of a tal object. 346 * @ctx: The tal allocated object. 347 * 348 * Returns the parent, which may be NULL. Returns NULL if @ctx is NULL. 349 */ 350 tal_t *tal_parent(const tal_t *ctx); 351 352 /** 353 * tal_dup - duplicate an object. 354 * @ctx: The tal allocated object to be parent of the result (may be NULL). 355 * @type: the type (should match type of @p!) 356 * @p: the object to copy (or reparented if take()). Must not be NULL. 357 */ 358 #define tal_dup(ctx, type, p) \ 359 tal_dup_label(ctx, type, p, TAL_LABEL(type, ""), false) 360 361 /** 362 * tal_dup_or_null - duplicate an object, or just pass NULL. 363 * @ctx: The tal allocated object to be parent of the result (may be NULL). 364 * @type: the type (should match type of @p!) 365 * @p: the object to copy (or reparented if take()) 366 * 367 * if @p is NULL, just return NULL, otherwise to tal_dup(). 368 */ 369 #define tal_dup_or_null(ctx, type, p) \ 370 tal_dup_label(ctx, type, p, TAL_LABEL(type, ""), true) 371 372 /** 373 * tal_dup_arr - duplicate an array. 374 * @ctx: The tal allocated object to be parent of the result (may be NULL). 375 * @type: the type (should match type of @p!) 376 * @p: the array to copy (or resized & reparented if take()) 377 * @n: the number of sizeof(type) entries to copy. 378 * @extra: the number of extra sizeof(type) entries to allocate. 379 */ 380 #define tal_dup_arr(ctx, type, p, n, extra) \ 381 tal_dup_arr_label(ctx, type, p, n, extra, TAL_LABEL(type, "[]")) 382 383 384 /** 385 * tal_dup_arr - duplicate a tal array. 386 * @ctx: The tal allocated object to be parent of the result (may be NULL). 387 * @type: the type (should match type of @p!) 388 * @p: the tal array to copy (or resized & reparented if take()) 389 * 390 * The common case of duplicating an entire tal array. 391 */ 392 #define tal_dup_talarr(ctx, type, p) \ 393 ((type *)tal_dup_talarr_((ctx), tal_typechk_(p, type *), \ 394 TAL_LABEL(type, "[]"))) 395 /* Lower-level interfaces, where you want to supply your own label string. */ 396 #define tal_label(ctx, type, label) \ 397 ((type *)tal_alloc_((ctx), sizeof(type), false, label)) 398 #define talz_label(ctx, type, label) \ 399 ((type *)tal_alloc_((ctx), sizeof(type), true, label)) 400 #define tal_arr_label(ctx, type, count, label) \ 401 ((type *)tal_alloc_arr_((ctx), sizeof(type), (count), false, label)) 402 #define tal_arrz_label(ctx, type, count, label) \ 403 ((type *)tal_alloc_arr_((ctx), sizeof(type), (count), true, label)) 404 #define tal_dup_label(ctx, type, p, label, nullok) \ 405 ((type *)tal_dup_((ctx), tal_typechk_(p, type *), \ 406 sizeof(type), 1, 0, nullok, \ 407 label)) 408 #define tal_dup_arr_label(ctx, type, p, n, extra, label) \ 409 ((type *)tal_dup_((ctx), tal_typechk_(p, type *), \ 410 sizeof(type), (n), (extra), false, \ 411 label)) 412 413 /** 414 * tal_set_backend - set the allocation or error functions to use 415 * @alloc_fn: allocator or NULL (default is malloc) 416 * @resize_fn: re-allocator or NULL (default is realloc) 417 * @free_fn: free function or NULL (default is free) 418 * @error_fn: called on errors or NULL (default is abort) 419 * 420 * The defaults are set up so tal functions never return NULL, but you 421 * can override error_fn to change that. error_fn can return, and is 422 * called if alloc_fn or resize_fn fail. 423 * 424 * If any parameter is NULL, that function is unchanged. 425 */ 426 void tal_set_backend(void *(*alloc_fn)(size_t size), 427 void *(*resize_fn)(void *, size_t size), 428 void (*free_fn)(void *), 429 void (*error_fn)(const char *msg)); 430 431 /** 432 * tal_expand - expand a tal array with contents. 433 * @a1p: a pointer to the tal array to expand. 434 * @a2: the second array (can be take()). 435 * @num2: the number of elements in the second array. 436 * 437 * Note that *@a1 and @a2 should be the same type. tal_count(@a1) will 438 * be increased by @num2. 439 * 440 * Example: 441 * int *arr1 = tal_arrz(NULL, int, 2); 442 * int arr2[2] = { 1, 3 }; 443 * 444 * tal_expand(&arr1, arr2, 2); 445 * assert(tal_count(arr1) == 4); 446 * assert(arr1[2] == 1); 447 * assert(arr1[3] == 3); 448 */ 449 #define tal_expand(a1p, a2, num2) \ 450 tal_expand_((void **)(a1p), (a2), sizeof**(a1p), \ 451 (num2) + 0*sizeof(*(a1p) == (a2))) 452 453 /** 454 * tal_cleanup - remove pointers from NULL node 455 * 456 * Internally, tal keeps a list of nodes allocated from @ctx NULL; this 457 * prevents valgrind from noticing memory leaks. This re-initializes 458 * that list to empty. 459 * 460 * It also calls take_cleanup() for you. 461 */ 462 void tal_cleanup(void); 463 464 465 /** 466 * tal_check - sanity check a tal context and its children. 467 * @ctx: a tal context, or NULL. 468 * @errorstr: a string to prepend calls to error_fn, or NULL. 469 * 470 * This sanity-checks a tal tree (unless NDEBUG is defined, in which case 471 * it simply returns true). If errorstr is not null, error_fn is called 472 * when a problem is found, otherwise it is not. 473 * 474 * See also: 475 * tal_set_backend() 476 */ 477 bool tal_check(const tal_t *ctx, const char *errorstr); 478 479 #ifdef CCAN_TAL_DEBUG 480 /** 481 * tal_dump - dump entire tal tree to stderr. 482 * 483 * This is a helper for debugging tal itself, which dumps all the tal internal 484 * state. 485 */ 486 void tal_dump(void); 487 #endif 488 489 /* Internal support functions */ 490 #ifndef TAL_LABEL 491 #ifdef CCAN_TAL_NO_LABELS 492 #define TAL_LABEL(type, arr) NULL 493 #else 494 #ifdef CCAN_TAL_DEBUG 495 #define TAL_LABEL(type, arr) \ 496 __FILE__ ":" stringify(__LINE__) ":" stringify(type) arr 497 #else 498 #define TAL_LABEL(type, arr) stringify(type) arr 499 #endif /* CCAN_TAL_DEBUG */ 500 #endif 501 #endif 502 503 #if HAVE_BUILTIN_CONSTANT_P 504 #define TAL_IS_LITERAL(str) __builtin_constant_p(str) 505 #else 506 #define TAL_IS_LITERAL(str) (sizeof(&*(str)) != sizeof(char *)) 507 #endif 508 509 bool tal_set_name_(tal_t *ctx, const char *name, bool literal); 510 511 #if HAVE_TYPEOF 512 #define tal_typeof(ptr) (__typeof__(ptr)) 513 #if HAVE_STATEMENT_EXPR 514 /* Careful: ptr can be const foo *, ptype is foo *. Also, ptr could 515 * be an array, eg "hello". */ 516 #define tal_typechk_(ptr, ptype) ({ __typeof__((ptr)+0) _p = (ptype)(ptr); _p; }) 517 #else 518 #define tal_typechk_(ptr, ptype) (ptr) 519 #endif 520 #else /* !HAVE_TYPEOF */ 521 #define tal_typeof(ptr) 522 #define tal_typechk_(ptr, ptype) (ptr) 523 #endif 524 525 void *tal_alloc_(const tal_t *ctx, size_t bytes, bool clear, const char *label); 526 void *tal_alloc_arr_(const tal_t *ctx, size_t bytes, size_t count, bool clear, 527 const char *label); 528 529 void *tal_dup_(const tal_t *ctx, const void *p TAKES, size_t size, 530 size_t n, size_t extra, bool nullok, const char *label); 531 void *tal_dup_talarr_(const tal_t *ctx, const tal_t *src TAKES, 532 const char *label); 533 534 tal_t *tal_steal_(const tal_t *new_parent, const tal_t *t); 535 536 bool tal_resize_(tal_t **ctxp, size_t size, size_t count, bool clear); 537 bool tal_expand_(tal_t **ctxp, const void *src TAKES, size_t size, size_t count); 538 539 bool tal_add_destructor_(const tal_t *ctx, void (*destroy)(void *me)); 540 bool tal_add_destructor2_(const tal_t *ctx, void (*destroy)(void *me, void *arg), 541 void *arg); 542 bool tal_del_destructor_(const tal_t *ctx, void (*destroy)(void *me)); 543 bool tal_del_destructor2_(const tal_t *ctx, void (*destroy)(void *me, void *arg), 544 void *arg); 545 546 bool tal_add_notifier_(const tal_t *ctx, enum tal_notify_type types, 547 void (*notify)(tal_t *ctx, enum tal_notify_type, 548 void *info)); 549 bool tal_del_notifier_(const tal_t *ctx, 550 void (*notify)(tal_t *ctx, enum tal_notify_type, 551 void *info), 552 bool match_extra_arg, void *arg); 553 #endif /* CCAN_TAL_H */