tal: add tal_dup_or_null and tal_dup_talarr helpers. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
tal: tal_dump() now prints to stderr. This is more widely available than stdout, which can interfere with other things. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
tal: document tal_steal(ctx, NULL) as OK. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
tal: don't defer-after-free if a destructor deletes itself. ==10868== at 0x109A96: notify (tal.c:220) ==10868== by 0x109F7E: del_tree (tal.c:397) ==10868== by 0x10A31A: tal_free (tal.c:481) ==10868== by 0x10BE73: main (run-destructor.c:75) ==10868== Address 0x4a60bd8 is 8 bytes inside a block of size 32 free'd ==10868== at 0x483BA3F: free (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so) ==10868== by 0x109D4F: del_notifier_property (tal.c:340) ==10868== by 0x10A610: tal_del_notifier_ (tal.c:564) ==10868== by 0x10A687: tal_del_destructor_ (tal.c:576) ==10868== by 0x10B653: remove_own_destructor (run-destructor.c:35) ==10868== by 0x109A67: notify (tal.c:235) ==10868== by 0x109F7E: del_tree (tal.c:397) ==10868== by 0x10A31A: tal_free (tal.c:481) ==10868== by 0x10BE73: main (run-destructor.c:75) ==10868== Block was alloc'd at ==10868== at 0x483A7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so) ==10868== by 0x109AD5: allocate (tal.c:245) ==10868== by 0x109C3E: add_notifier_property (tal.c:300) ==10868== by 0x10A467: tal_add_destructor_ (tal.c:516) ==10868== by 0x10BDFE: main (run-destructor.c:74) Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
tal: handle take() pointers more carefully. take() applies to the literal pointer value, so tal_free() means it may randomly apply to a future allocation. Similarly, tal_resize() should preserve it. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
tal: allow notifiers on NULL. A destructor on NULL doesn't make sense, but notifiers (eg. new children) do. We fix up a mistake in run-notifier (comparing ctx with itself) and loose typing in tal.c's tal_add_notifier_ too. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/tal: always include a length field. The current semantics of tal_count() / tal_bytelen() are to return 0 for anything not allocated using tal_arr*. This is because we tried to save a native-length word in the header, but produces an awkward API. (To make it worse, defining CCAN_TAL_DEBUG turns length to always on, and we enable that for c-lightning developer mode, which hides bugs!). However, for c-lightning, just over half of allocations want a length: these use 3 words each, so we're actually worse off overall. The answer is to always have a length field in the header. This also simplfies the tal code. samba-allocs stats before: Tal time: 1237102-1305755(1.251e+06+/-2.1e+04)ns Tal_free time: 1346871-1514514(1.37844e+06+/-5.2e+04)ns After: Tal time: 1115180-1180633(1.1351e+06+/-2.1e+04)ns Tal_free time: 1334381-1465933(1.39148e+06+/-4.7e+04)ns Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
tal: add _label interfaces. There are a number of other utilities which use the tal_alloc_ and tal_dup_arr_ internal interfaces directly, because they want to set the label themselves. We're about to break them all by changing those internal interfaces, so give them a mid-level interface to use. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
tal: rename tal_len to tal_bytelen. I had a bug caused by using tal_len instead of tal_count: let's make it explicit. @jb55 commented "ha. I always forget which one does which... Ack" Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
take, tal, tal/path, tal/str, tal/talloc: annotate APIs with TAKES. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
tal: make tal_len/tal_count(NULL) return 0. Previously it crashed, but if you're always dealing with tal arrays, this is painful. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
tal: support destructors with an extra argument. There are several times I've wanted an extra arg to the destructor, and had to embed it in the thing destroyed. It's more efficient to put it into tal itself (since it allocates space anyway), but we make it conditional on a flag to avoid bloating every destructor. The infrastructure makes it easier to add an extra arg to the general notifiers later if we want. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
tal: save and restore errno across all notifiers. So the errno when you call tal_free() is handed to all the notifiers, independent of what the others do. This makes sense, but also helps for the upcoming ccan/io change. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Remove duplicate const qualifier This causes ccanlint to fail the 'no warnings' check under clang. Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
tal: store length in bytes, not count, and always store if CCAN_TAL_DEBUG. Useful for scanning all the memory, or tallying it. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
tal: make tal_next() only return immediate children. I tried to use it and got this wrong: moreover, I wanted to control topology, which requires nested iteration, and skip children of a node which I knew was changing. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
tal: Fix a comment typo Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
tal: rename tal_dup to tal_dup_arr, make tal_dup simpler. It's a bit too powerful, if you just want to memdup one thing, it's hard to remember what the extra args are for. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
tal: fix tal_check description Cut & paste bug. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
tal: add tal_resizez for zero-padded expansion. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>