<p>Harald Welte <strong>merged</strong> this change.</p><p><a href="https://gerrit.osmocom.org/13993">View Change</a></p><div style="white-space:pre-wrap">Approvals:
  Jenkins Builder: Verified
  Harald Welte: Looks good to me, approved

</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">Import libtalloc-2.1.14 into the tree<br><br>talloc is a hirearchical memory allocator which Osmocom uses a lot<br>for its userspace programs on Linux.  Experiments show that it's<br>actually very much possible to use it inside of microcontroller firmware<br>projects, particularly the small subset which we generally use.<br><br>Change-Id: Ibd31b40d5b75b3fc1783a6b02b0e97da430e0f60<br>---<br>M sysmoOCTSIM/gcc/Makefile<br>A sysmoOCTSIM/replace.h<br>A sysmoOCTSIM/talloc.c<br>A sysmoOCTSIM/talloc.h<br>4 files changed, 4,989 insertions(+), 0 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/sysmoOCTSIM/gcc/Makefile b/sysmoOCTSIM/gcc/Makefile</span><br><span>index cf0484c..2dba3e3 100644</span><br><span>--- a/sysmoOCTSIM/gcc/Makefile</span><br><span>+++ b/sysmoOCTSIM/gcc/Makefile</span><br><span>@@ -79,6 +79,7 @@</span><br><span> hal/src/hal_usb_device.o \</span><br><span> main.o \</span><br><span> manual_test.o \</span><br><span style="color: hsl(120, 100%, 40%);">+talloc.o \</span><br><span> i2c_bitbang.o \</span><br><span> octsim_i2c.o \</span><br><span> ncn8025.o \</span><br><span>@@ -129,6 +130,7 @@</span><br><span> "hal/src/hal_usb_device.o" \</span><br><span> "main.o" \</span><br><span> "manual_test.o" \</span><br><span style="color: hsl(120, 100%, 40%);">+"talloc.o" \</span><br><span> "i2c_bitbang.o" \</span><br><span> "octsim_i2c.o" \</span><br><span> "ncn8025.o" \</span><br><span>@@ -186,6 +188,7 @@</span><br><span> "hpl/osc32kctrl/hpl_osc32kctrl.d" \</span><br><span> "main.d" \</span><br><span> "manual_test.d" \</span><br><span style="color: hsl(120, 100%, 40%);">+"talloc.d" \</span><br><span> "i2c_bitbang.d" \</span><br><span> "octsim_i2c.d" \</span><br><span> "ncn8025.d" \</span><br><span>diff --git a/sysmoOCTSIM/replace.h b/sysmoOCTSIM/replace.h</span><br><span>new file mode 100644</span><br><span>index 0000000..711f3a5</span><br><span>--- /dev/null</span><br><span>+++ b/sysmoOCTSIM/replace.h</span><br><span>@@ -0,0 +1,27 @@</span><br><span style="color: hsl(120, 100%, 40%);">+#pragma once</span><br><span style="color: hsl(120, 100%, 40%);">+/* whatever talloc 2.1.14 (from Debian talloc-2.1.14-2) required to build it</span><br><span style="color: hsl(120, 100%, 40%);">+ * with gcc-arm-none-eabi on a Debian unstable system */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#include <stdbool.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <string.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <errno.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <limits.h></span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifndef _PUBLIC_</span><br><span style="color: hsl(120, 100%, 40%);">+#define _PUBLIC_ __attribute__((visibility("default")))</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifndef MIN</span><br><span style="color: hsl(120, 100%, 40%);">+#define MIN(a,b) ((a)<(b)?(a):(b))</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifndef MAX</span><br><span style="color: hsl(120, 100%, 40%);">+#define MAX(a,b) ((a)>(b)?(a):(b))</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#define TALLOC_BUILD_VERSION_MAJOR 2</span><br><span style="color: hsl(120, 100%, 40%);">+#define TALLOC_BUILD_VERSION_MINOR 1</span><br><span style="color: hsl(120, 100%, 40%);">+#define TALLOC_BUILD_VERSION_RELEASE 14</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#define HAVE_VA_COPY</span><br><span style="color: hsl(120, 100%, 40%);">+#define HAVE_CONSTRUCTOR_ATTRIBUTE</span><br><span>diff --git a/sysmoOCTSIM/talloc.c b/sysmoOCTSIM/talloc.c</span><br><span>new file mode 100644</span><br><span>index 0000000..54be634</span><br><span>--- /dev/null</span><br><span>+++ b/sysmoOCTSIM/talloc.c</span><br><span>@@ -0,0 +1,3025 @@</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+   Samba Unix SMB/CIFS implementation.</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   Samba trivial allocation library - new interface</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   NOTE: Please read talloc_guide.txt for full documentation</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   Copyright (C) Andrew Tridgell 2004</span><br><span style="color: hsl(120, 100%, 40%);">+   Copyright (C) Stefan Metzmacher 2006</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     ** NOTE! The following LGPL license applies to the talloc</span><br><span style="color: hsl(120, 100%, 40%);">+     ** library. This does NOT imply that all of Samba is released</span><br><span style="color: hsl(120, 100%, 40%);">+     ** under the LGPL</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   This library is free software; you can redistribute it and/or</span><br><span style="color: hsl(120, 100%, 40%);">+   modify it under the terms of the GNU Lesser General Public</span><br><span style="color: hsl(120, 100%, 40%);">+   License as published by the Free Software Foundation; either</span><br><span style="color: hsl(120, 100%, 40%);">+   version 3 of the License, or (at your option) any later version.</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   This library is distributed in the hope that it will be useful,</span><br><span style="color: hsl(120, 100%, 40%);">+   but WITHOUT ANY WARRANTY; without even the implied warranty of</span><br><span style="color: hsl(120, 100%, 40%);">+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU</span><br><span style="color: hsl(120, 100%, 40%);">+   Lesser General Public License for more details.</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   You should have received a copy of the GNU Lesser General Public</span><br><span style="color: hsl(120, 100%, 40%);">+   License along with this library; if not, see <http://www.gnu.org/licenses/>.</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  inspired by http://swapped.cc/halloc/</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#include "replace.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "talloc.h"</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef HAVE_SYS_AUXV_H</span><br><span style="color: hsl(120, 100%, 40%);">+#include <sys/auxv.h></span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#if (TALLOC_VERSION_MAJOR != TALLOC_BUILD_VERSION_MAJOR)</span><br><span style="color: hsl(120, 100%, 40%);">+#error "TALLOC_VERSION_MAJOR != TALLOC_BUILD_VERSION_MAJOR"</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#if (TALLOC_VERSION_MINOR != TALLOC_BUILD_VERSION_MINOR)</span><br><span style="color: hsl(120, 100%, 40%);">+#error "TALLOC_VERSION_MINOR != TALLOC_BUILD_VERSION_MINOR"</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* Special macros that are no-ops except when run under Valgrind on</span><br><span style="color: hsl(120, 100%, 40%);">+ * x86.  They've moved a little bit from valgrind 1.0.4 to 1.9.4 */</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef HAVE_VALGRIND_MEMCHECK_H</span><br><span style="color: hsl(120, 100%, 40%);">+        /* memcheck.h includes valgrind.h */</span><br><span style="color: hsl(120, 100%, 40%);">+#include <valgrind/memcheck.h></span><br><span style="color: hsl(120, 100%, 40%);">+#elif defined(HAVE_VALGRIND_H)</span><br><span style="color: hsl(120, 100%, 40%);">+#include <valgrind.h></span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* use this to force every realloc to change the pointer, to stress test</span><br><span style="color: hsl(120, 100%, 40%);">+   code that might not cope */</span><br><span style="color: hsl(120, 100%, 40%);">+#define ALWAYS_REALLOC 0</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#define MAX_TALLOC_SIZE 0x10000000</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#define TALLOC_FLAG_FREE 0x01</span><br><span style="color: hsl(120, 100%, 40%);">+#define TALLOC_FLAG_LOOP 0x02</span><br><span style="color: hsl(120, 100%, 40%);">+#define TALLOC_FLAG_POOL 0x04          /* This is a talloc pool */</span><br><span style="color: hsl(120, 100%, 40%);">+#define TALLOC_FLAG_POOLMEM 0x08   /* This is allocated in a pool */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+ * Bits above this are random, used to make it harder to fake talloc</span><br><span style="color: hsl(120, 100%, 40%);">+ * headers during an attack.  Try not to change this without good reason.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+#define TALLOC_FLAG_MASK 0x0F</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#define TALLOC_MAGIC_REFERENCE ((const char *)1)</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#define TALLOC_MAGIC_BASE 0xe814ec70</span><br><span style="color: hsl(120, 100%, 40%);">+#define TALLOC_MAGIC_NON_RANDOM ( \</span><br><span style="color: hsl(120, 100%, 40%);">+ ~TALLOC_FLAG_MASK & ( \</span><br><span style="color: hsl(120, 100%, 40%);">+           TALLOC_MAGIC_BASE + \</span><br><span style="color: hsl(120, 100%, 40%);">+         (TALLOC_BUILD_VERSION_MAJOR << 24) + \</span><br><span style="color: hsl(120, 100%, 40%);">+          (TALLOC_BUILD_VERSION_MINOR << 16) + \</span><br><span style="color: hsl(120, 100%, 40%);">+          (TALLOC_BUILD_VERSION_RELEASE << 8)))</span><br><span style="color: hsl(120, 100%, 40%);">+static unsigned int talloc_magic = TALLOC_MAGIC_NON_RANDOM;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* by default we abort when given a bad pointer (such as when talloc_free() is called</span><br><span style="color: hsl(120, 100%, 40%);">+   on a pointer that came from malloc() */</span><br><span style="color: hsl(120, 100%, 40%);">+#ifndef TALLOC_ABORT</span><br><span style="color: hsl(120, 100%, 40%);">+#define TALLOC_ABORT(reason) abort()</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifndef discard_const_p</span><br><span style="color: hsl(120, 100%, 40%);">+#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)</span><br><span style="color: hsl(120, 100%, 40%);">+# define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+# define discard_const_p(type, ptr) ((type *)(ptr))</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* these macros gain us a few percent of speed on gcc */</span><br><span style="color: hsl(120, 100%, 40%);">+#if (__GNUC__ >= 3)</span><br><span style="color: hsl(120, 100%, 40%);">+/* the strange !! is to ensure that __builtin_expect() takes either 0 or 1</span><br><span style="color: hsl(120, 100%, 40%);">+   as its first argument */</span><br><span style="color: hsl(120, 100%, 40%);">+#ifndef likely</span><br><span style="color: hsl(120, 100%, 40%);">+#define likely(x)   __builtin_expect(!!(x), 1)</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+#ifndef unlikely</span><br><span style="color: hsl(120, 100%, 40%);">+#define unlikely(x) __builtin_expect(!!(x), 0)</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+#ifndef likely</span><br><span style="color: hsl(120, 100%, 40%);">+#define likely(x) (x)</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+#ifndef unlikely</span><br><span style="color: hsl(120, 100%, 40%);">+#define unlikely(x) (x)</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* this null_context is only used if talloc_enable_leak_report() or</span><br><span style="color: hsl(120, 100%, 40%);">+   talloc_enable_leak_report_full() is called, otherwise it remains</span><br><span style="color: hsl(120, 100%, 40%);">+   NULL</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+static void *null_context;</span><br><span style="color: hsl(120, 100%, 40%);">+static bool talloc_report_null;</span><br><span style="color: hsl(120, 100%, 40%);">+static bool talloc_report_null_full;</span><br><span style="color: hsl(120, 100%, 40%);">+static void *autofree_context;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void talloc_setup_atexit(void);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* used to enable fill of memory on free, which can be useful for</span><br><span style="color: hsl(120, 100%, 40%);">+ * catching use after free errors when valgrind is too slow</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static struct {</span><br><span style="color: hsl(120, 100%, 40%);">+    bool initialised;</span><br><span style="color: hsl(120, 100%, 40%);">+     bool enabled;</span><br><span style="color: hsl(120, 100%, 40%);">+ uint8_t fill_value;</span><br><span style="color: hsl(120, 100%, 40%);">+} talloc_fill;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#define TALLOC_FILL_ENV "TALLOC_FREE_FILL"</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+ * do not wipe the header, to allow the</span><br><span style="color: hsl(120, 100%, 40%);">+ * double-free logic to still work</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+#define TC_INVALIDATE_FULL_FILL_CHUNK(_tc) do { \</span><br><span style="color: hsl(120, 100%, 40%);">+        if (unlikely(talloc_fill.enabled)) { \</span><br><span style="color: hsl(120, 100%, 40%);">+                size_t _flen = (_tc)->size; \</span><br><span style="color: hsl(120, 100%, 40%);">+              char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \</span><br><span style="color: hsl(120, 100%, 40%);">+               memset(_fptr, talloc_fill.fill_value, _flen); \</span><br><span style="color: hsl(120, 100%, 40%);">+       } \</span><br><span style="color: hsl(120, 100%, 40%);">+} while (0)</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)</span><br><span style="color: hsl(120, 100%, 40%);">+/* Mark the whole chunk as not accessable */</span><br><span style="color: hsl(120, 100%, 40%);">+#define TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc) do { \</span><br><span style="color: hsl(120, 100%, 40%);">+        size_t _flen = TC_HDR_SIZE + (_tc)->size; \</span><br><span style="color: hsl(120, 100%, 40%);">+        char *_fptr = (char *)(_tc); \</span><br><span style="color: hsl(120, 100%, 40%);">+        VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \</span><br><span style="color: hsl(120, 100%, 40%);">+} while(0)</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+#define TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc) do { } while (0)</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#define TC_INVALIDATE_FULL_CHUNK(_tc) do { \</span><br><span style="color: hsl(120, 100%, 40%);">+       TC_INVALIDATE_FULL_FILL_CHUNK(_tc); \</span><br><span style="color: hsl(120, 100%, 40%);">+ TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc); \</span><br><span style="color: hsl(120, 100%, 40%);">+} while (0)</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#define TC_INVALIDATE_SHRINK_FILL_CHUNK(_tc, _new_size) do { \</span><br><span style="color: hsl(120, 100%, 40%);">+      if (unlikely(talloc_fill.enabled)) { \</span><br><span style="color: hsl(120, 100%, 40%);">+                size_t _flen = (_tc)->size - (_new_size); \</span><br><span style="color: hsl(120, 100%, 40%);">+                char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \</span><br><span style="color: hsl(120, 100%, 40%);">+               _fptr += (_new_size); \</span><br><span style="color: hsl(120, 100%, 40%);">+               memset(_fptr, talloc_fill.fill_value, _flen); \</span><br><span style="color: hsl(120, 100%, 40%);">+       } \</span><br><span style="color: hsl(120, 100%, 40%);">+} while (0)</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)</span><br><span style="color: hsl(120, 100%, 40%);">+/* Mark the unused bytes not accessable */</span><br><span style="color: hsl(120, 100%, 40%);">+#define TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { \</span><br><span style="color: hsl(120, 100%, 40%);">+     size_t _flen = (_tc)->size - (_new_size); \</span><br><span style="color: hsl(120, 100%, 40%);">+        char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \</span><br><span style="color: hsl(120, 100%, 40%);">+       _fptr += (_new_size); \</span><br><span style="color: hsl(120, 100%, 40%);">+       VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \</span><br><span style="color: hsl(120, 100%, 40%);">+} while (0)</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+#define TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#define TC_INVALIDATE_SHRINK_CHUNK(_tc, _new_size) do { \</span><br><span style="color: hsl(120, 100%, 40%);">+    TC_INVALIDATE_SHRINK_FILL_CHUNK(_tc, _new_size); \</span><br><span style="color: hsl(120, 100%, 40%);">+    TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size); \</span><br><span style="color: hsl(120, 100%, 40%);">+} while (0)</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#define TC_UNDEFINE_SHRINK_FILL_CHUNK(_tc, _new_size) do { \</span><br><span style="color: hsl(120, 100%, 40%);">+   if (unlikely(talloc_fill.enabled)) { \</span><br><span style="color: hsl(120, 100%, 40%);">+                size_t _flen = (_tc)->size - (_new_size); \</span><br><span style="color: hsl(120, 100%, 40%);">+                char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \</span><br><span style="color: hsl(120, 100%, 40%);">+               _fptr += (_new_size); \</span><br><span style="color: hsl(120, 100%, 40%);">+               memset(_fptr, talloc_fill.fill_value, _flen); \</span><br><span style="color: hsl(120, 100%, 40%);">+       } \</span><br><span style="color: hsl(120, 100%, 40%);">+} while (0)</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)</span><br><span style="color: hsl(120, 100%, 40%);">+/* Mark the unused bytes as undefined */</span><br><span style="color: hsl(120, 100%, 40%);">+#define TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { \</span><br><span style="color: hsl(120, 100%, 40%);">+        size_t _flen = (_tc)->size - (_new_size); \</span><br><span style="color: hsl(120, 100%, 40%);">+        char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \</span><br><span style="color: hsl(120, 100%, 40%);">+       _fptr += (_new_size); \</span><br><span style="color: hsl(120, 100%, 40%);">+       VALGRIND_MAKE_MEM_UNDEFINED(_fptr, _flen); \</span><br><span style="color: hsl(120, 100%, 40%);">+} while (0)</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+#define TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#define TC_UNDEFINE_SHRINK_CHUNK(_tc, _new_size) do { \</span><br><span style="color: hsl(120, 100%, 40%);">+       TC_UNDEFINE_SHRINK_FILL_CHUNK(_tc, _new_size); \</span><br><span style="color: hsl(120, 100%, 40%);">+      TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size); \</span><br><span style="color: hsl(120, 100%, 40%);">+} while (0)</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)</span><br><span style="color: hsl(120, 100%, 40%);">+/* Mark the new bytes as undefined */</span><br><span style="color: hsl(120, 100%, 40%);">+#define TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size) do { \</span><br><span style="color: hsl(120, 100%, 40%);">+    size_t _old_used = TC_HDR_SIZE + (_tc)->size; \</span><br><span style="color: hsl(120, 100%, 40%);">+    size_t _new_used = TC_HDR_SIZE + (_new_size); \</span><br><span style="color: hsl(120, 100%, 40%);">+       size_t _flen = _new_used - _old_used; \</span><br><span style="color: hsl(120, 100%, 40%);">+       char *_fptr = _old_used + (char *)(_tc); \</span><br><span style="color: hsl(120, 100%, 40%);">+    VALGRIND_MAKE_MEM_UNDEFINED(_fptr, _flen); \</span><br><span style="color: hsl(120, 100%, 40%);">+} while (0)</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+#define TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#define TC_UNDEFINE_GROW_CHUNK(_tc, _new_size) do { \</span><br><span style="color: hsl(120, 100%, 40%);">+   TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size); \</span><br><span style="color: hsl(120, 100%, 40%);">+} while (0)</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+struct talloc_reference_handle {</span><br><span style="color: hsl(120, 100%, 40%);">+   struct talloc_reference_handle *next, *prev;</span><br><span style="color: hsl(120, 100%, 40%);">+  void *ptr;</span><br><span style="color: hsl(120, 100%, 40%);">+    const char *location;</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+struct talloc_memlimit {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct talloc_chunk *parent;</span><br><span style="color: hsl(120, 100%, 40%);">+  struct talloc_memlimit *upper;</span><br><span style="color: hsl(120, 100%, 40%);">+        size_t max_size;</span><br><span style="color: hsl(120, 100%, 40%);">+      size_t cur_size;</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static inline bool talloc_memlimit_check(struct talloc_memlimit *limit, size_t size);</span><br><span style="color: hsl(120, 100%, 40%);">+static inline void talloc_memlimit_grow(struct talloc_memlimit *limit,</span><br><span style="color: hsl(120, 100%, 40%);">+                         size_t size);</span><br><span style="color: hsl(120, 100%, 40%);">+static inline void talloc_memlimit_shrink(struct talloc_memlimit *limit,</span><br><span style="color: hsl(120, 100%, 40%);">+                               size_t size);</span><br><span style="color: hsl(120, 100%, 40%);">+static inline void tc_memlimit_update_on_free(struct talloc_chunk *tc);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static inline void _tc_set_name_const(struct talloc_chunk *tc,</span><br><span style="color: hsl(120, 100%, 40%);">+                              const char *name);</span><br><span style="color: hsl(120, 100%, 40%);">+static struct talloc_chunk *_vasprintf_tc(const void *t,</span><br><span style="color: hsl(120, 100%, 40%);">+                          const char *fmt,</span><br><span style="color: hsl(120, 100%, 40%);">+                              va_list ap);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+typedef int (*talloc_destructor_t)(void *);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+struct talloc_pool_hdr;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+struct talloc_chunk {</span><br><span style="color: hsl(120, 100%, 40%);">+       /*</span><br><span style="color: hsl(120, 100%, 40%);">+     * flags includes the talloc magic, which is randomised to</span><br><span style="color: hsl(120, 100%, 40%);">+     * make overwrite attacks harder</span><br><span style="color: hsl(120, 100%, 40%);">+       */</span><br><span style="color: hsl(120, 100%, 40%);">+   unsigned flags;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     /*</span><br><span style="color: hsl(120, 100%, 40%);">+     * If you have a logical tree like:</span><br><span style="color: hsl(120, 100%, 40%);">+    *</span><br><span style="color: hsl(120, 100%, 40%);">+     *           <parent></span><br><span style="color: hsl(120, 100%, 40%);">+    *           /   |   \</span><br><span style="color: hsl(120, 100%, 40%);">+         *          /    |    \</span><br><span style="color: hsl(120, 100%, 40%);">+        *         /     |     \</span><br><span style="color: hsl(120, 100%, 40%);">+       * <child 1> <child 2> <child 3></span><br><span style="color: hsl(120, 100%, 40%);">+     *</span><br><span style="color: hsl(120, 100%, 40%);">+     * The actual talloc tree is:</span><br><span style="color: hsl(120, 100%, 40%);">+  *</span><br><span style="color: hsl(120, 100%, 40%);">+     *  <parent></span><br><span style="color: hsl(120, 100%, 40%);">+     *     |</span><br><span style="color: hsl(120, 100%, 40%);">+       *  <child 1> - <child 2> - <child 3></span><br><span style="color: hsl(120, 100%, 40%);">+        *</span><br><span style="color: hsl(120, 100%, 40%);">+     * The children are linked with next/prev pointers, and</span><br><span style="color: hsl(120, 100%, 40%);">+        * child 1 is linked to the parent with parent/child</span><br><span style="color: hsl(120, 100%, 40%);">+   * pointers.</span><br><span style="color: hsl(120, 100%, 40%);">+   */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ struct talloc_chunk *next, *prev;</span><br><span style="color: hsl(120, 100%, 40%);">+     struct talloc_chunk *parent, *child;</span><br><span style="color: hsl(120, 100%, 40%);">+  struct talloc_reference_handle *refs;</span><br><span style="color: hsl(120, 100%, 40%);">+ talloc_destructor_t destructor;</span><br><span style="color: hsl(120, 100%, 40%);">+       const char *name;</span><br><span style="color: hsl(120, 100%, 40%);">+     size_t size;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        /*</span><br><span style="color: hsl(120, 100%, 40%);">+     * limit semantics:</span><br><span style="color: hsl(120, 100%, 40%);">+    * if 'limit' is set it means all *new* children of the context will</span><br><span style="color: hsl(120, 100%, 40%);">+   * be limited to a total aggregate size ox max_size for memory</span><br><span style="color: hsl(120, 100%, 40%);">+         * allocations.</span><br><span style="color: hsl(120, 100%, 40%);">+        * cur_size is used to keep track of the current use</span><br><span style="color: hsl(120, 100%, 40%);">+   */</span><br><span style="color: hsl(120, 100%, 40%);">+   struct talloc_memlimit *limit;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      /*</span><br><span style="color: hsl(120, 100%, 40%);">+     * For members of a pool (i.e. TALLOC_FLAG_POOLMEM is set), "pool"</span><br><span style="color: hsl(120, 100%, 40%);">+   * is a pointer to the struct talloc_chunk of the pool that it was</span><br><span style="color: hsl(120, 100%, 40%);">+     * allocated from. This way children can quickly find the pool to chew</span><br><span style="color: hsl(120, 100%, 40%);">+         * from.</span><br><span style="color: hsl(120, 100%, 40%);">+       */</span><br><span style="color: hsl(120, 100%, 40%);">+   struct talloc_pool_hdr *pool;</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* 16 byte alignment seems to keep everyone happy */</span><br><span style="color: hsl(120, 100%, 40%);">+#define TC_ALIGN16(s) (((s)+15)&~15)</span><br><span style="color: hsl(120, 100%, 40%);">+#define TC_HDR_SIZE TC_ALIGN16(sizeof(struct talloc_chunk))</span><br><span style="color: hsl(120, 100%, 40%);">+#define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ int talloc_version_major(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+     return TALLOC_VERSION_MAJOR;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ int talloc_version_minor(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ return TALLOC_VERSION_MINOR;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ int talloc_test_get_magic(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        return talloc_magic;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static inline void _talloc_chunk_set_free(struct talloc_chunk *tc,</span><br><span style="color: hsl(120, 100%, 40%);">+                       const char *location)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        /*</span><br><span style="color: hsl(120, 100%, 40%);">+     * Mark this memory as free, and also over-stamp the talloc</span><br><span style="color: hsl(120, 100%, 40%);">+    * magic with the old-style magic.</span><br><span style="color: hsl(120, 100%, 40%);">+     *</span><br><span style="color: hsl(120, 100%, 40%);">+     * Why?  This tries to avoid a memory read use-after-free from</span><br><span style="color: hsl(120, 100%, 40%);">+         * disclosing our talloc magic, which would then allow an</span><br><span style="color: hsl(120, 100%, 40%);">+      * attacker to prepare a valid header and so run a destructor.</span><br><span style="color: hsl(120, 100%, 40%);">+         *</span><br><span style="color: hsl(120, 100%, 40%);">+     */</span><br><span style="color: hsl(120, 100%, 40%);">+   tc->flags = TALLOC_MAGIC_NON_RANDOM | TALLOC_FLAG_FREE</span><br><span style="color: hsl(120, 100%, 40%);">+             | (tc->flags & TALLOC_FLAG_MASK);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    /* we mark the freed memory with where we called the free</span><br><span style="color: hsl(120, 100%, 40%);">+      * from. This means on a double free error we can report where</span><br><span style="color: hsl(120, 100%, 40%);">+         * the first free came from</span><br><span style="color: hsl(120, 100%, 40%);">+    */</span><br><span style="color: hsl(120, 100%, 40%);">+   if (location) {</span><br><span style="color: hsl(120, 100%, 40%);">+               tc->name = location;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static inline void _talloc_chunk_set_not_free(struct talloc_chunk *tc)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+     /*</span><br><span style="color: hsl(120, 100%, 40%);">+     * Mark this memory as not free.</span><br><span style="color: hsl(120, 100%, 40%);">+       *</span><br><span style="color: hsl(120, 100%, 40%);">+     * Why? This is memory either in a pool (and so available for</span><br><span style="color: hsl(120, 100%, 40%);">+  * talloc's re-use or after the realloc().  We need to mark</span><br><span style="color: hsl(120, 100%, 40%);">+        * the memory as free() before any realloc() call as we can't</span><br><span style="color: hsl(120, 100%, 40%);">+      * write to the memory after that.</span><br><span style="color: hsl(120, 100%, 40%);">+     *</span><br><span style="color: hsl(120, 100%, 40%);">+     * We put back the normal magic instead of the 'not random'</span><br><span style="color: hsl(120, 100%, 40%);">+    * magic.</span><br><span style="color: hsl(120, 100%, 40%);">+      */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ tc->flags = talloc_magic |</span><br><span style="color: hsl(120, 100%, 40%);">+         ((tc->flags & TALLOC_FLAG_MASK) & ~TALLOC_FLAG_FREE);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void (*talloc_log_fn)(const char *message);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void talloc_set_log_fn(void (*log_fn)(const char *message))</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  talloc_log_fn = log_fn;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef HAVE_CONSTRUCTOR_ATTRIBUTE</span><br><span style="color: hsl(120, 100%, 40%);">+void talloc_lib_init(void) __attribute__((constructor));</span><br><span style="color: hsl(120, 100%, 40%);">+void talloc_lib_init(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+      uint32_t random_value;</span><br><span style="color: hsl(120, 100%, 40%);">+#if defined(HAVE_GETAUXVAL) && defined(AT_RANDOM)</span><br><span style="color: hsl(120, 100%, 40%);">+     uint8_t *p;</span><br><span style="color: hsl(120, 100%, 40%);">+   /*</span><br><span style="color: hsl(120, 100%, 40%);">+     * Use the kernel-provided random values used for</span><br><span style="color: hsl(120, 100%, 40%);">+      * ASLR.  This won't change per-exec, which is ideal for us</span><br><span style="color: hsl(120, 100%, 40%);">+        */</span><br><span style="color: hsl(120, 100%, 40%);">+   p = (uint8_t *) getauxval(AT_RANDOM);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (p) {</span><br><span style="color: hsl(120, 100%, 40%);">+              /*</span><br><span style="color: hsl(120, 100%, 40%);">+             * We get 16 bytes from getauxval.  By calling rand(),</span><br><span style="color: hsl(120, 100%, 40%);">+                 * a totally insecure PRNG, but one that will</span><br><span style="color: hsl(120, 100%, 40%);">+          * deterministically have a different value when called</span><br><span style="color: hsl(120, 100%, 40%);">+                * twice, we ensure that if two talloc-like libraries</span><br><span style="color: hsl(120, 100%, 40%);">+          * are somehow loaded in the same address space, that</span><br><span style="color: hsl(120, 100%, 40%);">+          * because we choose different bytes, we will keep the</span><br><span style="color: hsl(120, 100%, 40%);">+                 * protection against collision of multiple talloc</span><br><span style="color: hsl(120, 100%, 40%);">+             * libs.</span><br><span style="color: hsl(120, 100%, 40%);">+               *</span><br><span style="color: hsl(120, 100%, 40%);">+             * This protection is important because the effects of</span><br><span style="color: hsl(120, 100%, 40%);">+                 * passing a talloc pointer from one to the other may</span><br><span style="color: hsl(120, 100%, 40%);">+          * be very hard to determine.</span><br><span style="color: hsl(120, 100%, 40%);">+          */</span><br><span style="color: hsl(120, 100%, 40%);">+           int offset = rand() % (16 - sizeof(random_value));</span><br><span style="color: hsl(120, 100%, 40%);">+            memcpy(&random_value, p + offset, sizeof(random_value));</span><br><span style="color: hsl(120, 100%, 40%);">+  } else</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+        {</span><br><span style="color: hsl(120, 100%, 40%);">+             /*</span><br><span style="color: hsl(120, 100%, 40%);">+             * Otherwise, hope the location we are loaded in</span><br><span style="color: hsl(120, 100%, 40%);">+               * memory is randomised by someone else</span><br><span style="color: hsl(120, 100%, 40%);">+                */</span><br><span style="color: hsl(120, 100%, 40%);">+           random_value = ((uintptr_t)talloc_lib_init & 0xFFFFFFFF);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+     talloc_magic = random_value & ~TALLOC_FLAG_MASK;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+#warning "No __attribute__((constructor)) support found on this platform, additional talloc security measures not available"</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void talloc_lib_atexit(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+      TALLOC_FREE(autofree_context);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      if (talloc_total_size(null_context) == 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+           return;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (talloc_report_null_full) {</span><br><span style="color: hsl(120, 100%, 40%);">+                talloc_report_full(null_context, stderr);</span><br><span style="color: hsl(120, 100%, 40%);">+     } else if (talloc_report_null) {</span><br><span style="color: hsl(120, 100%, 40%);">+              talloc_report(null_context, stderr);</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void talloc_setup_atexit(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+      static bool done;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (done) {</span><br><span style="color: hsl(120, 100%, 40%);">+           return;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   atexit(talloc_lib_atexit);</span><br><span style="color: hsl(120, 100%, 40%);">+    done = true;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void talloc_log(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);</span><br><span style="color: hsl(120, 100%, 40%);">+static void talloc_log(const char *fmt, ...)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       va_list ap;</span><br><span style="color: hsl(120, 100%, 40%);">+   char *message;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      if (!talloc_log_fn) {</span><br><span style="color: hsl(120, 100%, 40%);">+         return;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   va_start(ap, fmt);</span><br><span style="color: hsl(120, 100%, 40%);">+    message = talloc_vasprintf(NULL, fmt, ap);</span><br><span style="color: hsl(120, 100%, 40%);">+    va_end(ap);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ talloc_log_fn(message);</span><br><span style="color: hsl(120, 100%, 40%);">+       talloc_free(message);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void talloc_log_stderr(const char *message)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+     fprintf(stderr, "%s", message);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void talloc_set_log_stderr(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  talloc_set_log_fn(talloc_log_stderr);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void (*talloc_abort_fn)(const char *reason);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void talloc_set_abort_fn(void (*abort_fn)(const char *reason))</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ talloc_abort_fn = abort_fn;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void talloc_abort(const char *reason)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+     talloc_log("%s\n", reason);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       if (!talloc_abort_fn) {</span><br><span style="color: hsl(120, 100%, 40%);">+               TALLOC_ABORT(reason);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   talloc_abort_fn(reason);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void talloc_abort_access_after_free(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+    talloc_abort("Bad talloc magic value - access after free");</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void talloc_abort_unknown_value(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+   talloc_abort("Bad talloc magic value - unknown value");</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* panic if we get a bad magic value */</span><br><span style="color: hsl(120, 100%, 40%);">+static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ const char *pp = (const char *)ptr;</span><br><span style="color: hsl(120, 100%, 40%);">+   struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);</span><br><span style="color: hsl(120, 100%, 40%);">+     if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~TALLOC_FLAG_MASK)) != talloc_magic)) {</span><br><span style="color: hsl(120, 100%, 40%);">+          if ((tc->flags & (TALLOC_FLAG_FREE | ~TALLOC_FLAG_MASK))</span><br><span style="color: hsl(120, 100%, 40%);">+                   == (TALLOC_MAGIC_NON_RANDOM | TALLOC_FLAG_FREE)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                        talloc_log("talloc: access after free error - first free may be at %s\n", tc->name);</span><br><span style="color: hsl(120, 100%, 40%);">+                     talloc_abort_access_after_free();</span><br><span style="color: hsl(120, 100%, 40%);">+                     return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+          }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+           talloc_abort_unknown_value();</span><br><span style="color: hsl(120, 100%, 40%);">+         return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+     return tc;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* hook into the front of the list */</span><br><span style="color: hsl(120, 100%, 40%);">+#define _TLIST_ADD(list, p) \</span><br><span style="color: hsl(120, 100%, 40%);">+do { \</span><br><span style="color: hsl(120, 100%, 40%);">+        if (!(list)) { \</span><br><span style="color: hsl(120, 100%, 40%);">+               (list) = (p); \</span><br><span style="color: hsl(120, 100%, 40%);">+               (p)->next = (p)->prev = NULL; \</span><br><span style="color: hsl(120, 100%, 40%);">+ } else { \</span><br><span style="color: hsl(120, 100%, 40%);">+            (list)->prev = (p); \</span><br><span style="color: hsl(120, 100%, 40%);">+              (p)->next = (list); \</span><br><span style="color: hsl(120, 100%, 40%);">+              (p)->prev = NULL; \</span><br><span style="color: hsl(120, 100%, 40%);">+                (list) = (p); \</span><br><span style="color: hsl(120, 100%, 40%);">+       }\</span><br><span style="color: hsl(120, 100%, 40%);">+} while (0)</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* remove an element from a list - element doesn't have to be in list. */</span><br><span style="color: hsl(120, 100%, 40%);">+#define _TLIST_REMOVE(list, p) \</span><br><span style="color: hsl(120, 100%, 40%);">+do { \</span><br><span style="color: hsl(120, 100%, 40%);">+    if ((p) == (list)) { \</span><br><span style="color: hsl(120, 100%, 40%);">+                (list) = (p)->next; \</span><br><span style="color: hsl(120, 100%, 40%);">+              if (list) (list)->prev = NULL; \</span><br><span style="color: hsl(120, 100%, 40%);">+   } else { \</span><br><span style="color: hsl(120, 100%, 40%);">+            if ((p)->prev) (p)->prev->next = (p)->next; \</span><br><span style="color: hsl(120, 100%, 40%);">+             if ((p)->next) (p)->next->prev = (p)->prev; \</span><br><span style="color: hsl(120, 100%, 40%);">+     } \</span><br><span style="color: hsl(120, 100%, 40%);">+   if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \</span><br><span style="color: hsl(120, 100%, 40%);">+} while (0)</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  return the parent chunk of a pointer</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        struct talloc_chunk *tc;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    if (unlikely(ptr == NULL)) {</span><br><span style="color: hsl(120, 100%, 40%);">+          return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   tc = talloc_chunk_from_ptr(ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+      while (tc->prev) tc=tc->prev;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return tc->parent;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void *talloc_parent(const void *ptr)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  struct talloc_chunk *tc = talloc_parent_chunk(ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+   return tc? TC_PTR_FROM_CHUNK(tc) : NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  find parents name</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ const char *talloc_parent_name(const void *ptr)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       struct talloc_chunk *tc = talloc_parent_chunk(ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+   return tc? tc->name : NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  A pool carries an in-pool object count count in the first 16 bytes.</span><br><span style="color: hsl(120, 100%, 40%);">+  bytes. This is done to support talloc_steal() to a parent outside of the</span><br><span style="color: hsl(120, 100%, 40%);">+  pool. The count includes the pool itself, so a talloc_free() on a pool will</span><br><span style="color: hsl(120, 100%, 40%);">+  only destroy the pool if the count has dropped to zero. A talloc_free() of a</span><br><span style="color: hsl(120, 100%, 40%);">+  pool member will reduce the count, and eventually also call free(3) on the</span><br><span style="color: hsl(120, 100%, 40%);">+  pool memory.</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  The object count is not put into "struct talloc_chunk" because it is only</span><br><span style="color: hsl(120, 100%, 40%);">+  relevant for talloc pools and the alignment to 16 bytes would increase the</span><br><span style="color: hsl(120, 100%, 40%);">+  memory footprint of each talloc chunk by those 16 bytes.</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+struct talloc_pool_hdr {</span><br><span style="color: hsl(120, 100%, 40%);">+    void *end;</span><br><span style="color: hsl(120, 100%, 40%);">+    unsigned int object_count;</span><br><span style="color: hsl(120, 100%, 40%);">+    size_t poolsize;</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#define TP_HDR_SIZE TC_ALIGN16(sizeof(struct talloc_pool_hdr))</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static inline struct talloc_pool_hdr *talloc_pool_from_chunk(struct talloc_chunk *c)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+     return (struct talloc_pool_hdr *)((char *)c - TP_HDR_SIZE);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static inline struct talloc_chunk *talloc_chunk_from_pool(struct talloc_pool_hdr *h)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+     return (struct talloc_chunk *)((char *)h + TP_HDR_SIZE);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static inline void *tc_pool_end(struct talloc_pool_hdr *pool_hdr)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+   struct talloc_chunk *tc = talloc_chunk_from_pool(pool_hdr);</span><br><span style="color: hsl(120, 100%, 40%);">+   return (char *)tc + TC_HDR_SIZE + pool_hdr->poolsize;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static inline size_t tc_pool_space_left(struct talloc_pool_hdr *pool_hdr)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+   return (char *)tc_pool_end(pool_hdr) - (char *)pool_hdr->end;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* If tc is inside a pool, this gives the next neighbour. */</span><br><span style="color: hsl(120, 100%, 40%);">+static inline void *tc_next_chunk(struct talloc_chunk *tc)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+    return (char *)tc + TC_ALIGN16(TC_HDR_SIZE + tc->size);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static inline void *tc_pool_first_chunk(struct talloc_pool_hdr *pool_hdr)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct talloc_chunk *tc = talloc_chunk_from_pool(pool_hdr);</span><br><span style="color: hsl(120, 100%, 40%);">+   return tc_next_chunk(tc);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* Mark the whole remaining pool as not accessable */</span><br><span style="color: hsl(120, 100%, 40%);">+static inline void tc_invalidate_pool(struct talloc_pool_hdr *pool_hdr)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+     size_t flen = tc_pool_space_left(pool_hdr);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (unlikely(talloc_fill.enabled)) {</span><br><span style="color: hsl(120, 100%, 40%);">+          memset(pool_hdr->end, talloc_fill.fill_value, flen);</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)</span><br><span style="color: hsl(120, 100%, 40%);">+    VALGRIND_MAKE_MEM_NOACCESS(pool_hdr->end, flen);</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  Allocate from a pool</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static inline struct talloc_chunk *tc_alloc_pool(struct talloc_chunk *parent,</span><br><span style="color: hsl(120, 100%, 40%);">+                                                  size_t size, size_t prefix_len)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       struct talloc_pool_hdr *pool_hdr = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+      size_t space_left;</span><br><span style="color: hsl(120, 100%, 40%);">+    struct talloc_chunk *result;</span><br><span style="color: hsl(120, 100%, 40%);">+  size_t chunk_size;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  if (parent == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+         return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (parent->flags & TALLOC_FLAG_POOL) {</span><br><span style="color: hsl(120, 100%, 40%);">+                pool_hdr = talloc_pool_from_chunk(parent);</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+     else if (parent->flags & TALLOC_FLAG_POOLMEM) {</span><br><span style="color: hsl(120, 100%, 40%);">+                pool_hdr = parent->pool;</span><br><span style="color: hsl(120, 100%, 40%);">+   }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (pool_hdr == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+               return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   space_left = tc_pool_space_left(pool_hdr);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  /*</span><br><span style="color: hsl(120, 100%, 40%);">+     * Align size to 16 bytes</span><br><span style="color: hsl(120, 100%, 40%);">+      */</span><br><span style="color: hsl(120, 100%, 40%);">+   chunk_size = TC_ALIGN16(size + prefix_len);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (space_left < chunk_size) {</span><br><span style="color: hsl(120, 100%, 40%);">+             return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   result = (struct talloc_chunk *)((char *)pool_hdr->end + prefix_len);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)</span><br><span style="color: hsl(120, 100%, 40%);">+    VALGRIND_MAKE_MEM_UNDEFINED(pool_hdr->end, chunk_size);</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  pool_hdr->end = (void *)((char *)pool_hdr->end + chunk_size);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ result->flags = talloc_magic | TALLOC_FLAG_POOLMEM;</span><br><span style="color: hsl(120, 100%, 40%);">+        result->pool = pool_hdr;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ pool_hdr->object_count++;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        return result;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+   Allocate a bit of memory as a child of an existing pointer</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+static inline void *__talloc_with_prefix(const void *context,</span><br><span style="color: hsl(120, 100%, 40%);">+                                     size_t size,</span><br><span style="color: hsl(120, 100%, 40%);">+                                  size_t prefix_len,</span><br><span style="color: hsl(120, 100%, 40%);">+                                    struct talloc_chunk **tc_ret)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+      struct talloc_chunk *tc = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+       struct talloc_memlimit *limit = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ size_t total_len = TC_HDR_SIZE + size + prefix_len;</span><br><span style="color: hsl(120, 100%, 40%);">+   struct talloc_chunk *parent = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (unlikely(context == NULL)) {</span><br><span style="color: hsl(120, 100%, 40%);">+              context = null_context;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (unlikely(size >= MAX_TALLOC_SIZE)) {</span><br><span style="color: hsl(120, 100%, 40%);">+           return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (unlikely(total_len < TC_HDR_SIZE)) {</span><br><span style="color: hsl(120, 100%, 40%);">+           return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (likely(context != NULL)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                parent = talloc_chunk_from_ptr(context);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+            if (parent->limit != NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+                       limit = parent->limit;</span><br><span style="color: hsl(120, 100%, 40%);">+             }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+           tc = tc_alloc_pool(parent, TC_HDR_SIZE+size, prefix_len);</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (tc == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+             char *ptr;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+          /*</span><br><span style="color: hsl(120, 100%, 40%);">+             * Only do the memlimit check/update on actual allocation.</span><br><span style="color: hsl(120, 100%, 40%);">+             */</span><br><span style="color: hsl(120, 100%, 40%);">+           if (!talloc_memlimit_check(limit, total_len)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                       errno = ENOMEM;</span><br><span style="color: hsl(120, 100%, 40%);">+                       return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+          }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+           ptr = malloc(total_len);</span><br><span style="color: hsl(120, 100%, 40%);">+              if (unlikely(ptr == NULL)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                  return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+          }</span><br><span style="color: hsl(120, 100%, 40%);">+             tc = (struct talloc_chunk *)(ptr + prefix_len);</span><br><span style="color: hsl(120, 100%, 40%);">+               tc->flags = talloc_magic;</span><br><span style="color: hsl(120, 100%, 40%);">+          tc->pool  = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                talloc_memlimit_grow(limit, total_len);</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   tc->limit = limit;</span><br><span style="color: hsl(120, 100%, 40%);">+ tc->size = size;</span><br><span style="color: hsl(120, 100%, 40%);">+   tc->destructor = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+     tc->child = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  tc->name = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+   tc->refs = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (likely(context != NULL)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                if (parent->child) {</span><br><span style="color: hsl(120, 100%, 40%);">+                       parent->child->parent = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+                   tc->next = parent->child;</span><br><span style="color: hsl(120, 100%, 40%);">+                       tc->next->prev = tc;</span><br><span style="color: hsl(120, 100%, 40%);">+            } else {</span><br><span style="color: hsl(120, 100%, 40%);">+                      tc->next = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+           }</span><br><span style="color: hsl(120, 100%, 40%);">+             tc->parent = parent;</span><br><span style="color: hsl(120, 100%, 40%);">+               tc->prev = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+           parent->child = tc;</span><br><span style="color: hsl(120, 100%, 40%);">+        } else {</span><br><span style="color: hsl(120, 100%, 40%);">+              tc->next = tc->prev = tc->parent = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   *tc_ret = tc;</span><br><span style="color: hsl(120, 100%, 40%);">+ return TC_PTR_FROM_CHUNK(tc);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static inline void *__talloc(const void *context,</span><br><span style="color: hsl(120, 100%, 40%);">+                 size_t size,</span><br><span style="color: hsl(120, 100%, 40%);">+                  struct talloc_chunk **tc)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  return __talloc_with_prefix(context, size, 0, tc);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+ * Create a talloc pool</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static inline void *_talloc_pool(const void *context, size_t size)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+    struct talloc_chunk *tc;</span><br><span style="color: hsl(120, 100%, 40%);">+      struct talloc_pool_hdr *pool_hdr;</span><br><span style="color: hsl(120, 100%, 40%);">+     void *result;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       result = __talloc_with_prefix(context, size, TP_HDR_SIZE, &tc);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (unlikely(result == NULL)) {</span><br><span style="color: hsl(120, 100%, 40%);">+               return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   pool_hdr = talloc_pool_from_chunk(tc);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      tc->flags |= TALLOC_FLAG_POOL;</span><br><span style="color: hsl(120, 100%, 40%);">+     tc->size = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    pool_hdr->object_count = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+        pool_hdr->end = result;</span><br><span style="color: hsl(120, 100%, 40%);">+    pool_hdr->poolsize = size;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       tc_invalidate_pool(pool_hdr);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       return result;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void *talloc_pool(const void *context, size_t size)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  return _talloc_pool(context, size);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+ * Create a talloc pool correctly sized for a basic size plus</span><br><span style="color: hsl(120, 100%, 40%);">+ * a number of subobjects whose total size is given. Essentially</span><br><span style="color: hsl(120, 100%, 40%);">+ * a custom allocator for talloc to reduce fragmentation.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void *_talloc_pooled_object(const void *ctx,</span><br><span style="color: hsl(120, 100%, 40%);">+                                     size_t type_size,</span><br><span style="color: hsl(120, 100%, 40%);">+                                     const char *type_name,</span><br><span style="color: hsl(120, 100%, 40%);">+                                unsigned num_subobjects,</span><br><span style="color: hsl(120, 100%, 40%);">+                              size_t total_subobjects_size)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ size_t poolsize, subobjects_slack, tmp;</span><br><span style="color: hsl(120, 100%, 40%);">+       struct talloc_chunk *tc;</span><br><span style="color: hsl(120, 100%, 40%);">+      struct talloc_pool_hdr *pool_hdr;</span><br><span style="color: hsl(120, 100%, 40%);">+     void *ret;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  poolsize = type_size + total_subobjects_size;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       if ((poolsize < type_size) || (poolsize < total_subobjects_size)) {</span><br><span style="color: hsl(120, 100%, 40%);">+             goto overflow;</span><br><span style="color: hsl(120, 100%, 40%);">+        }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (num_subobjects == UINT_MAX) {</span><br><span style="color: hsl(120, 100%, 40%);">+             goto overflow;</span><br><span style="color: hsl(120, 100%, 40%);">+        }</span><br><span style="color: hsl(120, 100%, 40%);">+     num_subobjects += 1;       /* the object body itself */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     /*</span><br><span style="color: hsl(120, 100%, 40%);">+     * Alignment can increase the pool size by at most 15 bytes per object</span><br><span style="color: hsl(120, 100%, 40%);">+         * plus alignment for the object itself</span><br><span style="color: hsl(120, 100%, 40%);">+        */</span><br><span style="color: hsl(120, 100%, 40%);">+   subobjects_slack = (TC_HDR_SIZE + TP_HDR_SIZE + 15) * num_subobjects;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (subobjects_slack < num_subobjects) {</span><br><span style="color: hsl(120, 100%, 40%);">+           goto overflow;</span><br><span style="color: hsl(120, 100%, 40%);">+        }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   tmp = poolsize + subobjects_slack;</span><br><span style="color: hsl(120, 100%, 40%);">+    if ((tmp < poolsize) || (tmp < subobjects_slack)) {</span><br><span style="color: hsl(120, 100%, 40%);">+             goto overflow;</span><br><span style="color: hsl(120, 100%, 40%);">+        }</span><br><span style="color: hsl(120, 100%, 40%);">+     poolsize = tmp;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     ret = _talloc_pool(ctx, poolsize);</span><br><span style="color: hsl(120, 100%, 40%);">+    if (ret == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+            return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   tc = talloc_chunk_from_ptr(ret);</span><br><span style="color: hsl(120, 100%, 40%);">+      tc->size = type_size;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    pool_hdr = talloc_pool_from_chunk(tc);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)</span><br><span style="color: hsl(120, 100%, 40%);">+      VALGRIND_MAKE_MEM_UNDEFINED(pool_hdr->end, type_size);</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   pool_hdr->end = ((char *)pool_hdr->end + TC_ALIGN16(type_size));</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      _tc_set_name_const(tc, type_name);</span><br><span style="color: hsl(120, 100%, 40%);">+    return ret;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+overflow:</span><br><span style="color: hsl(120, 100%, 40%);">+      return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  setup a destructor to be called on free of a pointer</span><br><span style="color: hsl(120, 100%, 40%);">+  the destructor should return 0 on success, or -1 on failure.</span><br><span style="color: hsl(120, 100%, 40%);">+  if the destructor fails then the free is failed, and the memory can</span><br><span style="color: hsl(120, 100%, 40%);">+  be continued to be used</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+      struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+ tc->destructor = destructor;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  increase the reference count on a piece of memory.</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ int talloc_increase_ref_count(const void *ptr)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        if (unlikely(!talloc_reference(null_context, ptr))) {</span><br><span style="color: hsl(120, 100%, 40%);">+         return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+     return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  helper for talloc_reference()</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  this is referenced by a function pointer and should not be inline</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+static int talloc_reference_destructor(struct talloc_reference_handle *handle)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+     struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+  _TLIST_REMOVE(ptr_tc->refs, handle);</span><br><span style="color: hsl(120, 100%, 40%);">+       return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+   more efficient way to add a name to a pointer - the name must point to a</span><br><span style="color: hsl(120, 100%, 40%);">+   true string constant</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+static inline void _tc_set_name_const(struct talloc_chunk *tc,</span><br><span style="color: hsl(120, 100%, 40%);">+                                  const char *name)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  tc->name = name;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  internal talloc_named_const()</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+static inline void *_talloc_named_const(const void *context, size_t size, const char *name)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+     void *ptr;</span><br><span style="color: hsl(120, 100%, 40%);">+    struct talloc_chunk *tc;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    ptr = __talloc(context, size, &tc);</span><br><span style="color: hsl(120, 100%, 40%);">+       if (unlikely(ptr == NULL)) {</span><br><span style="color: hsl(120, 100%, 40%);">+          return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   _tc_set_name_const(tc, name);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       return ptr;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  make a secondary reference to a pointer, hanging off the given context.</span><br><span style="color: hsl(120, 100%, 40%);">+  the pointer remains valid until both the original caller and this given</span><br><span style="color: hsl(120, 100%, 40%);">+  context are freed.</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  the major use for this is when two different structures need to reference the</span><br><span style="color: hsl(120, 100%, 40%);">+  same underlying data, and you want to be able to free the two instances separately,</span><br><span style="color: hsl(120, 100%, 40%);">+  and in either order</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void *_talloc_reference_loc(const void *context, const void *ptr, const char *location)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+    struct talloc_chunk *tc;</span><br><span style="color: hsl(120, 100%, 40%);">+      struct talloc_reference_handle *handle;</span><br><span style="color: hsl(120, 100%, 40%);">+       if (unlikely(ptr == NULL)) return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     tc = talloc_chunk_from_ptr(ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+      handle = (struct talloc_reference_handle *)_talloc_named_const(context,</span><br><span style="color: hsl(120, 100%, 40%);">+                                                  sizeof(struct talloc_reference_handle),</span><br><span style="color: hsl(120, 100%, 40%);">+                                               TALLOC_MAGIC_REFERENCE);</span><br><span style="color: hsl(120, 100%, 40%);">+   if (unlikely(handle == NULL)) return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  /* note that we hang the destructor off the handle, not the</span><br><span style="color: hsl(120, 100%, 40%);">+      main context as that allows the caller to still setup their</span><br><span style="color: hsl(120, 100%, 40%);">+           own destructor on the context if they want to */</span><br><span style="color: hsl(120, 100%, 40%);">+   talloc_set_destructor(handle, talloc_reference_destructor);</span><br><span style="color: hsl(120, 100%, 40%);">+   handle->ptr = discard_const_p(void, ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+  handle->location = location;</span><br><span style="color: hsl(120, 100%, 40%);">+       _TLIST_ADD(tc->refs, handle);</span><br><span style="color: hsl(120, 100%, 40%);">+      return handle->ptr;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void *_talloc_steal_internal(const void *new_ctx, const void *ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static inline void _tc_free_poolmem(struct talloc_chunk *tc,</span><br><span style="color: hsl(120, 100%, 40%);">+                                       const char *location)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+      struct talloc_pool_hdr *pool;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct talloc_chunk *pool_tc;</span><br><span style="color: hsl(120, 100%, 40%);">+ void *next_tc;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      pool = tc->pool;</span><br><span style="color: hsl(120, 100%, 40%);">+   pool_tc = talloc_chunk_from_pool(pool);</span><br><span style="color: hsl(120, 100%, 40%);">+       next_tc = tc_next_chunk(tc);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        _talloc_chunk_set_free(tc, location);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       TC_INVALIDATE_FULL_CHUNK(tc);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       if (unlikely(pool->object_count == 0)) {</span><br><span style="color: hsl(120, 100%, 40%);">+           talloc_abort("Pool object count zero!");</span><br><span style="color: hsl(120, 100%, 40%);">+            return;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   pool->object_count--;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    if (unlikely(pool->object_count == 1</span><br><span style="color: hsl(120, 100%, 40%);">+                    && !(pool_tc->flags & TALLOC_FLAG_FREE))) {</span><br><span style="color: hsl(120, 100%, 40%);">+               /*</span><br><span style="color: hsl(120, 100%, 40%);">+             * if there is just one object left in the pool</span><br><span style="color: hsl(120, 100%, 40%);">+                * and pool->flags does not have TALLOC_FLAG_FREE,</span><br><span style="color: hsl(120, 100%, 40%);">+          * it means this is the pool itself and</span><br><span style="color: hsl(120, 100%, 40%);">+                * the rest is available for new objects</span><br><span style="color: hsl(120, 100%, 40%);">+               * again.</span><br><span style="color: hsl(120, 100%, 40%);">+              */</span><br><span style="color: hsl(120, 100%, 40%);">+           pool->end = tc_pool_first_chunk(pool);</span><br><span style="color: hsl(120, 100%, 40%);">+             tc_invalidate_pool(pool);</span><br><span style="color: hsl(120, 100%, 40%);">+             return;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (unlikely(pool->object_count == 0)) {</span><br><span style="color: hsl(120, 100%, 40%);">+           /*</span><br><span style="color: hsl(120, 100%, 40%);">+             * we mark the freed memory with where we called the free</span><br><span style="color: hsl(120, 100%, 40%);">+              * from. This means on a double free error we can report where</span><br><span style="color: hsl(120, 100%, 40%);">+                 * the first free came from</span><br><span style="color: hsl(120, 100%, 40%);">+            */</span><br><span style="color: hsl(120, 100%, 40%);">+           pool_tc->name = location;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                if (pool_tc->flags & TALLOC_FLAG_POOLMEM) {</span><br><span style="color: hsl(120, 100%, 40%);">+                    _tc_free_poolmem(pool_tc, location);</span><br><span style="color: hsl(120, 100%, 40%);">+          } else {</span><br><span style="color: hsl(120, 100%, 40%);">+                      /*</span><br><span style="color: hsl(120, 100%, 40%);">+                     * The tc_memlimit_update_on_free()</span><br><span style="color: hsl(120, 100%, 40%);">+                    * call takes into account the</span><br><span style="color: hsl(120, 100%, 40%);">+                         * prefix TP_HDR_SIZE allocated before</span><br><span style="color: hsl(120, 100%, 40%);">+                         * the pool talloc_chunk.</span><br><span style="color: hsl(120, 100%, 40%);">+                      */</span><br><span style="color: hsl(120, 100%, 40%);">+                   tc_memlimit_update_on_free(pool_tc);</span><br><span style="color: hsl(120, 100%, 40%);">+                  TC_INVALIDATE_FULL_CHUNK(pool_tc);</span><br><span style="color: hsl(120, 100%, 40%);">+                    free(pool);</span><br><span style="color: hsl(120, 100%, 40%);">+           }</span><br><span style="color: hsl(120, 100%, 40%);">+             return;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (pool->end == next_tc) {</span><br><span style="color: hsl(120, 100%, 40%);">+                /*</span><br><span style="color: hsl(120, 100%, 40%);">+             * if pool->pool still points to end of</span><br><span style="color: hsl(120, 100%, 40%);">+             * 'tc' (which is stored in the 'next_tc' variable),</span><br><span style="color: hsl(120, 100%, 40%);">+           * we can reclaim the memory of 'tc'.</span><br><span style="color: hsl(120, 100%, 40%);">+          */</span><br><span style="color: hsl(120, 100%, 40%);">+           pool->end = tc;</span><br><span style="color: hsl(120, 100%, 40%);">+            return;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   /*</span><br><span style="color: hsl(120, 100%, 40%);">+     * Do nothing. The memory is just "wasted", waiting for the pool</span><br><span style="color: hsl(120, 100%, 40%);">+     * itself to be freed.</span><br><span style="color: hsl(120, 100%, 40%);">+         */</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static inline void _tc_free_children_internal(struct talloc_chunk *tc,</span><br><span style="color: hsl(120, 100%, 40%);">+                                                void *ptr,</span><br><span style="color: hsl(120, 100%, 40%);">+                                            const char *location);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static inline int _talloc_free_internal(void *ptr, const char *location);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+   internal free call that takes a struct talloc_chunk *.</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+static inline int _tc_free_internal(struct talloc_chunk *tc,</span><br><span style="color: hsl(120, 100%, 40%);">+                              const char *location)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+      void *ptr_to_free;</span><br><span style="color: hsl(120, 100%, 40%);">+    void *ptr = TC_PTR_FROM_CHUNK(tc);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  if (unlikely(tc->refs)) {</span><br><span style="color: hsl(120, 100%, 40%);">+          int is_child;</span><br><span style="color: hsl(120, 100%, 40%);">+         /* check if this is a reference from a child or</span><br><span style="color: hsl(120, 100%, 40%);">+                * grandchild back to it's parent or grandparent</span><br><span style="color: hsl(120, 100%, 40%);">+           *</span><br><span style="color: hsl(120, 100%, 40%);">+             * in that case we need to remove the reference and</span><br><span style="color: hsl(120, 100%, 40%);">+            * call another instance of talloc_free() on the current</span><br><span style="color: hsl(120, 100%, 40%);">+               * pointer.</span><br><span style="color: hsl(120, 100%, 40%);">+            */</span><br><span style="color: hsl(120, 100%, 40%);">+           is_child = talloc_is_parent(tc->refs, ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+                _talloc_free_internal(tc->refs, location);</span><br><span style="color: hsl(120, 100%, 40%);">+         if (is_child) {</span><br><span style="color: hsl(120, 100%, 40%);">+                       return _talloc_free_internal(ptr, location);</span><br><span style="color: hsl(120, 100%, 40%);">+          }</span><br><span style="color: hsl(120, 100%, 40%);">+             return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {</span><br><span style="color: hsl(120, 100%, 40%);">+          /* we have a free loop - stop looping */</span><br><span style="color: hsl(120, 100%, 40%);">+              return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (unlikely(tc->destructor)) {</span><br><span style="color: hsl(120, 100%, 40%);">+            talloc_destructor_t d = tc->destructor;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+          /*</span><br><span style="color: hsl(120, 100%, 40%);">+             * Protect the destructor against some overwrite</span><br><span style="color: hsl(120, 100%, 40%);">+               * attacks, by explicitly checking it has the right</span><br><span style="color: hsl(120, 100%, 40%);">+            * magic here.</span><br><span style="color: hsl(120, 100%, 40%);">+                 */</span><br><span style="color: hsl(120, 100%, 40%);">+           if (talloc_chunk_from_ptr(ptr) != tc) {</span><br><span style="color: hsl(120, 100%, 40%);">+                       /*</span><br><span style="color: hsl(120, 100%, 40%);">+                     * This can't actually happen, the</span><br><span style="color: hsl(120, 100%, 40%);">+                         * call itself will panic.</span><br><span style="color: hsl(120, 100%, 40%);">+                     */</span><br><span style="color: hsl(120, 100%, 40%);">+                   TALLOC_ABORT("talloc_chunk_from_ptr failed!");</span><br><span style="color: hsl(120, 100%, 40%);">+              }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+           if (d == (talloc_destructor_t)-1) {</span><br><span style="color: hsl(120, 100%, 40%);">+                   return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+            }</span><br><span style="color: hsl(120, 100%, 40%);">+             tc->destructor = (talloc_destructor_t)-1;</span><br><span style="color: hsl(120, 100%, 40%);">+          if (d(ptr) == -1) {</span><br><span style="color: hsl(120, 100%, 40%);">+                   /*</span><br><span style="color: hsl(120, 100%, 40%);">+                     * Only replace the destructor pointer if</span><br><span style="color: hsl(120, 100%, 40%);">+                      * calling the destructor didn't modify it.</span><br><span style="color: hsl(120, 100%, 40%);">+                        */</span><br><span style="color: hsl(120, 100%, 40%);">+                   if (tc->destructor == (talloc_destructor_t)-1) {</span><br><span style="color: hsl(120, 100%, 40%);">+                           tc->destructor = d;</span><br><span style="color: hsl(120, 100%, 40%);">+                        }</span><br><span style="color: hsl(120, 100%, 40%);">+                     return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+            }</span><br><span style="color: hsl(120, 100%, 40%);">+             tc->destructor = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (tc->parent) {</span><br><span style="color: hsl(120, 100%, 40%);">+          _TLIST_REMOVE(tc->parent->child, tc);</span><br><span style="color: hsl(120, 100%, 40%);">+           if (tc->parent->child) {</span><br><span style="color: hsl(120, 100%, 40%);">+                        tc->parent->child->parent = tc->parent;</span><br><span style="color: hsl(120, 100%, 40%);">+           }</span><br><span style="color: hsl(120, 100%, 40%);">+     } else {</span><br><span style="color: hsl(120, 100%, 40%);">+              if (tc->prev) tc->prev->next = tc->next;</span><br><span style="color: hsl(120, 100%, 40%);">+          if (tc->next) tc->next->prev = tc->prev;</span><br><span style="color: hsl(120, 100%, 40%);">+          tc->prev = tc->next = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   tc->flags |= TALLOC_FLAG_LOOP;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   _tc_free_children_internal(tc, ptr, location);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      _talloc_chunk_set_free(tc, location);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       if (tc->flags & TALLOC_FLAG_POOL) {</span><br><span style="color: hsl(120, 100%, 40%);">+            struct talloc_pool_hdr *pool;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+               pool = talloc_pool_from_chunk(tc);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+          if (unlikely(pool->object_count == 0)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                   talloc_abort("Pool object count zero!");</span><br><span style="color: hsl(120, 100%, 40%);">+                    return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+             }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+           pool->object_count--;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+            if (likely(pool->object_count != 0)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                     return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+             }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+           /*</span><br><span style="color: hsl(120, 100%, 40%);">+             * With object_count==0, a pool becomes a normal piece of</span><br><span style="color: hsl(120, 100%, 40%);">+              * memory to free. If it's allocated inside a pool, it needs</span><br><span style="color: hsl(120, 100%, 40%);">+               * to be freed as poolmem, else it needs to be just freed.</span><br><span style="color: hsl(120, 100%, 40%);">+            */</span><br><span style="color: hsl(120, 100%, 40%);">+            ptr_to_free = pool;</span><br><span style="color: hsl(120, 100%, 40%);">+   } else {</span><br><span style="color: hsl(120, 100%, 40%);">+              ptr_to_free = tc;</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (tc->flags & TALLOC_FLAG_POOLMEM) {</span><br><span style="color: hsl(120, 100%, 40%);">+         _tc_free_poolmem(tc, location);</span><br><span style="color: hsl(120, 100%, 40%);">+               return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   tc_memlimit_update_on_free(tc);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     TC_INVALIDATE_FULL_CHUNK(tc);</span><br><span style="color: hsl(120, 100%, 40%);">+ free(ptr_to_free);</span><br><span style="color: hsl(120, 100%, 40%);">+    return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+   internal talloc_free call</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+static inline int _talloc_free_internal(void *ptr, const char *location)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+     struct talloc_chunk *tc;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    if (unlikely(ptr == NULL)) {</span><br><span style="color: hsl(120, 100%, 40%);">+          return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   /* possibly initialised the talloc fill value */</span><br><span style="color: hsl(120, 100%, 40%);">+      if (unlikely(!talloc_fill.initialised)) {</span><br><span style="color: hsl(120, 100%, 40%);">+             const char *fill = getenv(TALLOC_FILL_ENV);</span><br><span style="color: hsl(120, 100%, 40%);">+           if (fill != NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+                   talloc_fill.enabled = true;</span><br><span style="color: hsl(120, 100%, 40%);">+                   talloc_fill.fill_value = strtoul(fill, NULL, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+              }</span><br><span style="color: hsl(120, 100%, 40%);">+             talloc_fill.initialised = true;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   tc = talloc_chunk_from_ptr(ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+      return _tc_free_internal(tc, location);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static inline size_t _talloc_total_limit_size(const void *ptr,</span><br><span style="color: hsl(120, 100%, 40%);">+                                  struct talloc_memlimit *old_limit,</span><br><span style="color: hsl(120, 100%, 40%);">+                                    struct talloc_memlimit *new_limit);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+   move a lump of memory from one talloc context to another return the</span><br><span style="color: hsl(120, 100%, 40%);">+   ptr on success, or NULL if it could not be transferred.</span><br><span style="color: hsl(120, 100%, 40%);">+   passing NULL as ptr will always return NULL with no side effects.</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+static void *_talloc_steal_internal(const void *new_ctx, const void *ptr)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct talloc_chunk *tc, *new_tc;</span><br><span style="color: hsl(120, 100%, 40%);">+     size_t ctx_size = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        if (unlikely(!ptr)) {</span><br><span style="color: hsl(120, 100%, 40%);">+         return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (unlikely(new_ctx == NULL)) {</span><br><span style="color: hsl(120, 100%, 40%);">+              new_ctx = null_context;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   tc = talloc_chunk_from_ptr(ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    if (tc->limit != NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+         ctx_size = _talloc_total_limit_size(ptr, NULL, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+               /* Decrement the memory limit from the source .. */</span><br><span style="color: hsl(120, 100%, 40%);">+           talloc_memlimit_shrink(tc->limit->upper, ctx_size);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+           if (tc->limit->parent == tc) {</span><br><span style="color: hsl(120, 100%, 40%);">+                  tc->limit->upper = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+                } else {</span><br><span style="color: hsl(120, 100%, 40%);">+                      tc->limit = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+          }</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (unlikely(new_ctx == NULL)) {</span><br><span style="color: hsl(120, 100%, 40%);">+              if (tc->parent) {</span><br><span style="color: hsl(120, 100%, 40%);">+                  _TLIST_REMOVE(tc->parent->child, tc);</span><br><span style="color: hsl(120, 100%, 40%);">+                   if (tc->parent->child) {</span><br><span style="color: hsl(120, 100%, 40%);">+                                tc->parent->child->parent = tc->parent;</span><br><span style="color: hsl(120, 100%, 40%);">+                   }</span><br><span style="color: hsl(120, 100%, 40%);">+             } else {</span><br><span style="color: hsl(120, 100%, 40%);">+                      if (tc->prev) tc->prev->next = tc->next;</span><br><span style="color: hsl(120, 100%, 40%);">+                  if (tc->next) tc->next->prev = tc->prev;</span><br><span style="color: hsl(120, 100%, 40%);">+          }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+           tc->parent = tc->next = tc->prev = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+             return discard_const_p(void, ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   new_tc = talloc_chunk_from_ptr(new_ctx);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    if (unlikely(tc == new_tc || tc->parent == new_tc)) {</span><br><span style="color: hsl(120, 100%, 40%);">+              return discard_const_p(void, ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (tc->parent) {</span><br><span style="color: hsl(120, 100%, 40%);">+          _TLIST_REMOVE(tc->parent->child, tc);</span><br><span style="color: hsl(120, 100%, 40%);">+           if (tc->parent->child) {</span><br><span style="color: hsl(120, 100%, 40%);">+                        tc->parent->child->parent = tc->parent;</span><br><span style="color: hsl(120, 100%, 40%);">+           }</span><br><span style="color: hsl(120, 100%, 40%);">+     } else {</span><br><span style="color: hsl(120, 100%, 40%);">+              if (tc->prev) tc->prev->next = tc->next;</span><br><span style="color: hsl(120, 100%, 40%);">+          if (tc->next) tc->next->prev = tc->prev;</span><br><span style="color: hsl(120, 100%, 40%);">+          tc->prev = tc->next = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   tc->parent = new_tc;</span><br><span style="color: hsl(120, 100%, 40%);">+       if (new_tc->child) new_tc->child->parent = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+     _TLIST_ADD(new_tc->child, tc);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (tc->limit || new_tc->limit) {</span><br><span style="color: hsl(120, 100%, 40%);">+               ctx_size = _talloc_total_limit_size(ptr, tc->limit,</span><br><span style="color: hsl(120, 100%, 40%);">+                                                    new_tc->limit);</span><br><span style="color: hsl(120, 100%, 40%);">+                /* .. and increment it in the destination. */</span><br><span style="color: hsl(120, 100%, 40%);">+         if (new_tc->limit) {</span><br><span style="color: hsl(120, 100%, 40%);">+                       talloc_memlimit_grow(new_tc->limit, ctx_size);</span><br><span style="color: hsl(120, 100%, 40%);">+             }</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   return discard_const_p(void, ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+   move a lump of memory from one talloc context to another return the</span><br><span style="color: hsl(120, 100%, 40%);">+   ptr on success, or NULL if it could not be transferred.</span><br><span style="color: hsl(120, 100%, 40%);">+   passing NULL as ptr will always return NULL with no side effects.</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+    struct talloc_chunk *tc;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    if (unlikely(ptr == NULL)) {</span><br><span style="color: hsl(120, 100%, 40%);">+          return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   tc = talloc_chunk_from_ptr(ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    if (unlikely(tc->refs != NULL) && talloc_parent(ptr) != new_ctx) {</span><br><span style="color: hsl(120, 100%, 40%);">+         struct talloc_reference_handle *h;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+          talloc_log("WARNING: talloc_steal with references at %s\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                    location);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+               for (h=tc->refs; h; h=h->next) {</span><br><span style="color: hsl(120, 100%, 40%);">+                        talloc_log("\treference at %s\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                              h->location);</span><br><span style="color: hsl(120, 100%, 40%);">+           }</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#if 0</span><br><span style="color: hsl(120, 100%, 40%);">+    /* this test is probably too expensive to have on in the</span><br><span style="color: hsl(120, 100%, 40%);">+         normal build, but it useful for debugging */</span><br><span style="color: hsl(120, 100%, 40%);">+       if (talloc_is_parent(new_ctx, ptr)) {</span><br><span style="color: hsl(120, 100%, 40%);">+         talloc_log("WARNING: stealing into talloc child at %s\n", location);</span><br><span style="color: hsl(120, 100%, 40%);">+        }</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   return _talloc_steal_internal(new_ctx, ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+   this is like a talloc_steal(), but you must supply the old</span><br><span style="color: hsl(120, 100%, 40%);">+   parent. This resolves the ambiguity in a talloc_steal() which is</span><br><span style="color: hsl(120, 100%, 40%);">+   called on a context that has more than one parent (via references)</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   The old parent can be either a reference or a parent</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+   struct talloc_chunk *tc;</span><br><span style="color: hsl(120, 100%, 40%);">+      struct talloc_reference_handle *h;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  if (unlikely(ptr == NULL)) {</span><br><span style="color: hsl(120, 100%, 40%);">+          return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (old_parent == talloc_parent(ptr)) {</span><br><span style="color: hsl(120, 100%, 40%);">+               return _talloc_steal_internal(new_parent, ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   tc = talloc_chunk_from_ptr(ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+      for (h=tc->refs;h;h=h->next) {</span><br><span style="color: hsl(120, 100%, 40%);">+          if (talloc_parent(h) == old_parent) {</span><br><span style="color: hsl(120, 100%, 40%);">+                 if (_talloc_steal_internal(new_parent, h) != h) {</span><br><span style="color: hsl(120, 100%, 40%);">+                             return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+                  }</span><br><span style="color: hsl(120, 100%, 40%);">+                     return discard_const_p(void, ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+            }</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   /* it wasn't a parent */</span><br><span style="color: hsl(120, 100%, 40%);">+  return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  remove a secondary reference to a pointer. This undo's what</span><br><span style="color: hsl(120, 100%, 40%);">+  talloc_reference() has done. The context and pointer arguments</span><br><span style="color: hsl(120, 100%, 40%);">+  must match those given to a talloc_reference()</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+static inline int talloc_unreference(const void *context, const void *ptr)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+ struct talloc_reference_handle *h;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  if (unlikely(context == NULL)) {</span><br><span style="color: hsl(120, 100%, 40%);">+              context = null_context;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   for (h=tc->refs;h;h=h->next) {</span><br><span style="color: hsl(120, 100%, 40%);">+          struct talloc_chunk *p = talloc_parent_chunk(h);</span><br><span style="color: hsl(120, 100%, 40%);">+              if (p == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+                      if (context == NULL) break;</span><br><span style="color: hsl(120, 100%, 40%);">+           } else if (TC_PTR_FROM_CHUNK(p) == context) {</span><br><span style="color: hsl(120, 100%, 40%);">+                 break;</span><br><span style="color: hsl(120, 100%, 40%);">+                }</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+     if (h == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+              return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   return _talloc_free_internal(h, __location__);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  remove a specific parent context from a pointer. This is a more</span><br><span style="color: hsl(120, 100%, 40%);">+  controlled variant of talloc_free()</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ int talloc_unlink(const void *context, void *ptr)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  struct talloc_chunk *tc_p, *new_p, *tc_c;</span><br><span style="color: hsl(120, 100%, 40%);">+     void *new_parent;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (ptr == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+            return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (context == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+                context = null_context;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (talloc_unreference(context, ptr) == 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+          return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (context != NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+                tc_c = talloc_chunk_from_ptr(context);</span><br><span style="color: hsl(120, 100%, 40%);">+        } else {</span><br><span style="color: hsl(120, 100%, 40%);">+              tc_c = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+     if (tc_c != talloc_parent_chunk(ptr)) {</span><br><span style="color: hsl(120, 100%, 40%);">+               return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   tc_p = talloc_chunk_from_ptr(ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  if (tc_p->refs == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+          return _talloc_free_internal(ptr, __location__);</span><br><span style="color: hsl(120, 100%, 40%);">+      }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   new_p = talloc_parent_chunk(tc_p->refs);</span><br><span style="color: hsl(120, 100%, 40%);">+   if (new_p) {</span><br><span style="color: hsl(120, 100%, 40%);">+          new_parent = TC_PTR_FROM_CHUNK(new_p);</span><br><span style="color: hsl(120, 100%, 40%);">+        } else {</span><br><span style="color: hsl(120, 100%, 40%);">+              new_parent = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (talloc_unreference(new_parent, ptr) != 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+               return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   _talloc_steal_internal(new_parent, ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  add a name to an existing pointer - va_list version</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+static inline const char *tc_set_name_v(struct talloc_chunk *tc,</span><br><span style="color: hsl(120, 100%, 40%);">+                               const char *fmt,</span><br><span style="color: hsl(120, 100%, 40%);">+                              va_list ap) PRINTF_ATTRIBUTE(2,0);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static inline const char *tc_set_name_v(struct talloc_chunk *tc,</span><br><span style="color: hsl(120, 100%, 40%);">+                                const char *fmt,</span><br><span style="color: hsl(120, 100%, 40%);">+                              va_list ap)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        struct talloc_chunk *name_tc = _vasprintf_tc(TC_PTR_FROM_CHUNK(tc),</span><br><span style="color: hsl(120, 100%, 40%);">+                                                   fmt,</span><br><span style="color: hsl(120, 100%, 40%);">+                                                  ap);</span><br><span style="color: hsl(120, 100%, 40%);">+  if (likely(name_tc)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                tc->name = TC_PTR_FROM_CHUNK(name_tc);</span><br><span style="color: hsl(120, 100%, 40%);">+             _tc_set_name_const(name_tc, ".name");</span><br><span style="color: hsl(120, 100%, 40%);">+       } else {</span><br><span style="color: hsl(120, 100%, 40%);">+              tc->name = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+   }</span><br><span style="color: hsl(120, 100%, 40%);">+     return tc->name;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  add a name to an existing pointer</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ const char *talloc_set_name(const void *ptr, const char *fmt, ...)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+ const char *name;</span><br><span style="color: hsl(120, 100%, 40%);">+     va_list ap;</span><br><span style="color: hsl(120, 100%, 40%);">+   va_start(ap, fmt);</span><br><span style="color: hsl(120, 100%, 40%);">+    name = tc_set_name_v(tc, fmt, ap);</span><br><span style="color: hsl(120, 100%, 40%);">+    va_end(ap);</span><br><span style="color: hsl(120, 100%, 40%);">+   return name;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  create a named talloc pointer. Any talloc pointer can be named, and</span><br><span style="color: hsl(120, 100%, 40%);">+  talloc_named() operates just like talloc() except that it allows you</span><br><span style="color: hsl(120, 100%, 40%);">+  to name the pointer.</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void *talloc_named(const void *context, size_t size, const char *fmt, ...)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+    va_list ap;</span><br><span style="color: hsl(120, 100%, 40%);">+   void *ptr;</span><br><span style="color: hsl(120, 100%, 40%);">+    const char *name;</span><br><span style="color: hsl(120, 100%, 40%);">+     struct talloc_chunk *tc;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    ptr = __talloc(context, size, &tc);</span><br><span style="color: hsl(120, 100%, 40%);">+       if (unlikely(ptr == NULL)) return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     va_start(ap, fmt);</span><br><span style="color: hsl(120, 100%, 40%);">+    name = tc_set_name_v(tc, fmt, ap);</span><br><span style="color: hsl(120, 100%, 40%);">+    va_end(ap);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (unlikely(name == NULL)) {</span><br><span style="color: hsl(120, 100%, 40%);">+         _talloc_free_internal(ptr, __location__);</span><br><span style="color: hsl(120, 100%, 40%);">+             return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   return ptr;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  return the name of a talloc ptr, or "UNNAMED"</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+static inline const char *__talloc_get_name(const void *ptr)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                return ".reference";</span><br><span style="color: hsl(120, 100%, 40%);">+        }</span><br><span style="color: hsl(120, 100%, 40%);">+     if (likely(tc->name)) {</span><br><span style="color: hsl(120, 100%, 40%);">+            return tc->name;</span><br><span style="color: hsl(120, 100%, 40%);">+   }</span><br><span style="color: hsl(120, 100%, 40%);">+     return "UNNAMED";</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ const char *talloc_get_name(const void *ptr)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+    return __talloc_get_name(ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  check if a pointer has the given name. If it does, return the pointer,</span><br><span style="color: hsl(120, 100%, 40%);">+  otherwise return NULL</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void *talloc_check_name(const void *ptr, const char *name)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        const char *pname;</span><br><span style="color: hsl(120, 100%, 40%);">+    if (unlikely(ptr == NULL)) return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+       pname = __talloc_get_name(ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+       if (likely(pname == name || strcmp(pname, name) == 0)) {</span><br><span style="color: hsl(120, 100%, 40%);">+              return discard_const_p(void, ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+     return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void talloc_abort_type_mismatch(const char *location,</span><br><span style="color: hsl(120, 100%, 40%);">+                                       const char *name,</span><br><span style="color: hsl(120, 100%, 40%);">+                                     const char *expected)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+      const char *reason;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ reason = talloc_asprintf(NULL,</span><br><span style="color: hsl(120, 100%, 40%);">+                                 "%s: Type mismatch: name[%s] expected[%s]",</span><br><span style="color: hsl(120, 100%, 40%);">+                                 location,</span><br><span style="color: hsl(120, 100%, 40%);">+                             name?name:"NULL",</span><br><span style="color: hsl(120, 100%, 40%);">+                           expected);</span><br><span style="color: hsl(120, 100%, 40%);">+   if (!reason) {</span><br><span style="color: hsl(120, 100%, 40%);">+                reason = "Type mismatch";</span><br><span style="color: hsl(120, 100%, 40%);">+   }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   talloc_abort(reason);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ const char *pname;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  if (unlikely(ptr == NULL)) {</span><br><span style="color: hsl(120, 100%, 40%);">+          talloc_abort_type_mismatch(location, NULL, name);</span><br><span style="color: hsl(120, 100%, 40%);">+             return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   pname = __talloc_get_name(ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+       if (likely(pname == name || strcmp(pname, name) == 0)) {</span><br><span style="color: hsl(120, 100%, 40%);">+              return discard_const_p(void, ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   talloc_abort_type_mismatch(location, pname, name);</span><br><span style="color: hsl(120, 100%, 40%);">+    return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  this is for compatibility with older versions of talloc</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void *talloc_init(const char *fmt, ...)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+     va_list ap;</span><br><span style="color: hsl(120, 100%, 40%);">+   void *ptr;</span><br><span style="color: hsl(120, 100%, 40%);">+    const char *name;</span><br><span style="color: hsl(120, 100%, 40%);">+     struct talloc_chunk *tc;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    ptr = __talloc(NULL, 0, &tc);</span><br><span style="color: hsl(120, 100%, 40%);">+     if (unlikely(ptr == NULL)) return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     va_start(ap, fmt);</span><br><span style="color: hsl(120, 100%, 40%);">+    name = tc_set_name_v(tc, fmt, ap);</span><br><span style="color: hsl(120, 100%, 40%);">+    va_end(ap);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (unlikely(name == NULL)) {</span><br><span style="color: hsl(120, 100%, 40%);">+         _talloc_free_internal(ptr, __location__);</span><br><span style="color: hsl(120, 100%, 40%);">+             return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   return ptr;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static inline void _tc_free_children_internal(struct talloc_chunk *tc,</span><br><span style="color: hsl(120, 100%, 40%);">+                                                void *ptr,</span><br><span style="color: hsl(120, 100%, 40%);">+                                            const char *location)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+    while (tc->child) {</span><br><span style="color: hsl(120, 100%, 40%);">+                /* we need to work out who will own an abandoned child</span><br><span style="color: hsl(120, 100%, 40%);">+                   if it cannot be freed. In priority order, the first</span><br><span style="color: hsl(120, 100%, 40%);">+                   choice is owner of any remaining reference to this</span><br><span style="color: hsl(120, 100%, 40%);">+            pointer, the second choice is our parent, and the</span><br><span style="color: hsl(120, 100%, 40%);">+             final choice is the null context. */</span><br><span style="color: hsl(120, 100%, 40%);">+               void *child = TC_PTR_FROM_CHUNK(tc->child);</span><br><span style="color: hsl(120, 100%, 40%);">+                const void *new_parent = null_context;</span><br><span style="color: hsl(120, 100%, 40%);">+                if (unlikely(tc->child->refs)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                        struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);</span><br><span style="color: hsl(120, 100%, 40%);">+                  if (p) new_parent = TC_PTR_FROM_CHUNK(p);</span><br><span style="color: hsl(120, 100%, 40%);">+             }</span><br><span style="color: hsl(120, 100%, 40%);">+             if (unlikely(_tc_free_internal(tc->child, location) == -1)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                      if (talloc_parent_chunk(child) != tc) {</span><br><span style="color: hsl(120, 100%, 40%);">+                               /*</span><br><span style="color: hsl(120, 100%, 40%);">+                             * Destructor already reparented this child.</span><br><span style="color: hsl(120, 100%, 40%);">+                           * No further reparenting needed.</span><br><span style="color: hsl(120, 100%, 40%);">+                              */</span><br><span style="color: hsl(120, 100%, 40%);">+                           continue;</span><br><span style="color: hsl(120, 100%, 40%);">+                     }</span><br><span style="color: hsl(120, 100%, 40%);">+                     if (new_parent == null_context) {</span><br><span style="color: hsl(120, 100%, 40%);">+                             struct talloc_chunk *p = talloc_parent_chunk(ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+                            if (p) new_parent = TC_PTR_FROM_CHUNK(p);</span><br><span style="color: hsl(120, 100%, 40%);">+                     }</span><br><span style="color: hsl(120, 100%, 40%);">+                     _talloc_steal_internal(new_parent, child);</span><br><span style="color: hsl(120, 100%, 40%);">+            }</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  this is a replacement for the Samba3 talloc_destroy_pool functionality. It</span><br><span style="color: hsl(120, 100%, 40%);">+  should probably not be used in new code. It's in here to keep the talloc</span><br><span style="color: hsl(120, 100%, 40%);">+  code consistent across Samba 3 and 4.</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void talloc_free_children(void *ptr)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       struct talloc_chunk *tc_name = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  struct talloc_chunk *tc;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    if (unlikely(ptr == NULL)) {</span><br><span style="color: hsl(120, 100%, 40%);">+          return;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   tc = talloc_chunk_from_ptr(ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    /* we do not want to free the context name if it is a child .. */</span><br><span style="color: hsl(120, 100%, 40%);">+     if (likely(tc->child)) {</span><br><span style="color: hsl(120, 100%, 40%);">+           for (tc_name = tc->child; tc_name; tc_name = tc_name->next) {</span><br><span style="color: hsl(120, 100%, 40%);">+                   if (tc->name == TC_PTR_FROM_CHUNK(tc_name)) break;</span><br><span style="color: hsl(120, 100%, 40%);">+         }</span><br><span style="color: hsl(120, 100%, 40%);">+             if (tc_name) {</span><br><span style="color: hsl(120, 100%, 40%);">+                        _TLIST_REMOVE(tc->child, tc_name);</span><br><span style="color: hsl(120, 100%, 40%);">+                 if (tc->child) {</span><br><span style="color: hsl(120, 100%, 40%);">+                           tc->child->parent = tc;</span><br><span style="color: hsl(120, 100%, 40%);">+                 }</span><br><span style="color: hsl(120, 100%, 40%);">+             }</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   _tc_free_children_internal(tc, ptr, __location__);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  /* .. so we put it back after all other children have been freed */</span><br><span style="color: hsl(120, 100%, 40%);">+   if (tc_name) {</span><br><span style="color: hsl(120, 100%, 40%);">+                if (tc->child) {</span><br><span style="color: hsl(120, 100%, 40%);">+                   tc->child->parent = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+               }</span><br><span style="color: hsl(120, 100%, 40%);">+             tc_name->parent = tc;</span><br><span style="color: hsl(120, 100%, 40%);">+              _TLIST_ADD(tc->child, tc_name);</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+   Allocate a bit of memory as a child of an existing pointer</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void *_talloc(const void *context, size_t size)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+    struct talloc_chunk *tc;</span><br><span style="color: hsl(120, 100%, 40%);">+      return __talloc(context, size, &tc);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  externally callable talloc_set_name_const()</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void talloc_set_name_const(const void *ptr, const char *name)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       _tc_set_name_const(talloc_chunk_from_ptr(ptr), name);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  create a named talloc pointer. Any talloc pointer can be named, and</span><br><span style="color: hsl(120, 100%, 40%);">+  talloc_named() operates just like talloc() except that it allows you</span><br><span style="color: hsl(120, 100%, 40%);">+  to name the pointer.</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void *talloc_named_const(const void *context, size_t size, const char *name)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+   return _talloc_named_const(context, size, name);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+   free a talloc pointer. This also frees all child pointers of this</span><br><span style="color: hsl(120, 100%, 40%);">+   pointer recursively</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   return 0 if the memory is actually freed, otherwise -1. The memory</span><br><span style="color: hsl(120, 100%, 40%);">+   will not be freed if the ref_count is > 1 or the destructor (if</span><br><span style="color: hsl(120, 100%, 40%);">+   any) returns non-zero</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ int _talloc_free(void *ptr, const char *location)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  struct talloc_chunk *tc;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    if (unlikely(ptr == NULL)) {</span><br><span style="color: hsl(120, 100%, 40%);">+          return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   tc = talloc_chunk_from_ptr(ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    if (unlikely(tc->refs != NULL)) {</span><br><span style="color: hsl(120, 100%, 40%);">+          struct talloc_reference_handle *h;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+          if (talloc_parent(ptr) == null_context && tc->refs->next == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+                     /* in this case we do know which parent should</span><br><span style="color: hsl(120, 100%, 40%);">+                           get this pointer, as there is really only</span><br><span style="color: hsl(120, 100%, 40%);">+                     one parent */</span><br><span style="color: hsl(120, 100%, 40%);">+                      return talloc_unlink(null_context, ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+              }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+           talloc_log("ERROR: talloc_free with references at %s\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                       location);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+               for (h=tc->refs; h; h=h->next) {</span><br><span style="color: hsl(120, 100%, 40%);">+                        talloc_log("\treference at %s\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                              h->location);</span><br><span style="color: hsl(120, 100%, 40%);">+           }</span><br><span style="color: hsl(120, 100%, 40%);">+             return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   return _talloc_free_internal(ptr, location);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  A talloc version of realloc. The context argument is only used if</span><br><span style="color: hsl(120, 100%, 40%);">+  ptr is NULL</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+   struct talloc_chunk *tc;</span><br><span style="color: hsl(120, 100%, 40%);">+      void *new_ptr;</span><br><span style="color: hsl(120, 100%, 40%);">+        bool malloced = false;</span><br><span style="color: hsl(120, 100%, 40%);">+        struct talloc_pool_hdr *pool_hdr = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+      size_t old_size = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+  size_t new_size = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        /* size zero is equivalent to free() */</span><br><span style="color: hsl(120, 100%, 40%);">+       if (unlikely(size == 0)) {</span><br><span style="color: hsl(120, 100%, 40%);">+            talloc_unlink(context, ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+          return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (unlikely(size >= MAX_TALLOC_SIZE)) {</span><br><span style="color: hsl(120, 100%, 40%);">+           return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   /* realloc(NULL) is equivalent to malloc() */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ptr == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+            return _talloc_named_const(context, size, name);</span><br><span style="color: hsl(120, 100%, 40%);">+      }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   tc = talloc_chunk_from_ptr(ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    /* don't allow realloc on referenced pointers */</span><br><span style="color: hsl(120, 100%, 40%);">+  if (unlikely(tc->refs)) {</span><br><span style="color: hsl(120, 100%, 40%);">+          return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   /* don't let anybody try to realloc a talloc_pool */</span><br><span style="color: hsl(120, 100%, 40%);">+      if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {</span><br><span style="color: hsl(120, 100%, 40%);">+          return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (tc->limit && (size > tc->size)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                if (!talloc_memlimit_check(tc->limit, (size - tc->size))) {</span><br><span style="color: hsl(120, 100%, 40%);">+                     errno = ENOMEM;</span><br><span style="color: hsl(120, 100%, 40%);">+                       return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+          }</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   /* handle realloc inside a talloc_pool */</span><br><span style="color: hsl(120, 100%, 40%);">+     if (unlikely(tc->flags & TALLOC_FLAG_POOLMEM)) {</span><br><span style="color: hsl(120, 100%, 40%);">+               pool_hdr = tc->pool;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#if (ALWAYS_REALLOC == 0)</span><br><span style="color: hsl(120, 100%, 40%);">+        /* don't shrink if we have less than 1k to gain */</span><br><span style="color: hsl(120, 100%, 40%);">+        if (size < tc->size && tc->limit == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+          if (pool_hdr) {</span><br><span style="color: hsl(120, 100%, 40%);">+                       void *next_tc = tc_next_chunk(tc);</span><br><span style="color: hsl(120, 100%, 40%);">+                    TC_INVALIDATE_SHRINK_CHUNK(tc, size);</span><br><span style="color: hsl(120, 100%, 40%);">+                 tc->size = size;</span><br><span style="color: hsl(120, 100%, 40%);">+                   if (next_tc == pool_hdr->end) {</span><br><span style="color: hsl(120, 100%, 40%);">+                            /* note: tc->size has changed, so this works */</span><br><span style="color: hsl(120, 100%, 40%);">+                            pool_hdr->end = tc_next_chunk(tc);</span><br><span style="color: hsl(120, 100%, 40%);">+                 }</span><br><span style="color: hsl(120, 100%, 40%);">+                     return ptr;</span><br><span style="color: hsl(120, 100%, 40%);">+           } else if ((tc->size - size) < 1024) {</span><br><span style="color: hsl(120, 100%, 40%);">+                  /*</span><br><span style="color: hsl(120, 100%, 40%);">+                     * if we call TC_INVALIDATE_SHRINK_CHUNK() here</span><br><span style="color: hsl(120, 100%, 40%);">+                        * we would need to call TC_UNDEFINE_GROW_CHUNK()</span><br><span style="color: hsl(120, 100%, 40%);">+                      * after each realloc call, which slows down</span><br><span style="color: hsl(120, 100%, 40%);">+                   * testing a lot :-(.</span><br><span style="color: hsl(120, 100%, 40%);">+                  *</span><br><span style="color: hsl(120, 100%, 40%);">+                     * That is why we only mark memory as undefined here.</span><br><span style="color: hsl(120, 100%, 40%);">+                  */</span><br><span style="color: hsl(120, 100%, 40%);">+                   TC_UNDEFINE_SHRINK_CHUNK(tc, size);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                 /* do not shrink if we have less than 1k to gain */</span><br><span style="color: hsl(120, 100%, 40%);">+                   tc->size = size;</span><br><span style="color: hsl(120, 100%, 40%);">+                   return ptr;</span><br><span style="color: hsl(120, 100%, 40%);">+           }</span><br><span style="color: hsl(120, 100%, 40%);">+     } else if (tc->size == size) {</span><br><span style="color: hsl(120, 100%, 40%);">+             /*</span><br><span style="color: hsl(120, 100%, 40%);">+             * do not change the pointer if it is exactly</span><br><span style="color: hsl(120, 100%, 40%);">+          * the same size.</span><br><span style="color: hsl(120, 100%, 40%);">+              */</span><br><span style="color: hsl(120, 100%, 40%);">+           return ptr;</span><br><span style="color: hsl(120, 100%, 40%);">+   }</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   /*</span><br><span style="color: hsl(120, 100%, 40%);">+     * by resetting magic we catch users of the old memory</span><br><span style="color: hsl(120, 100%, 40%);">+         *</span><br><span style="color: hsl(120, 100%, 40%);">+     * We mark this memory as free, and also over-stamp the talloc</span><br><span style="color: hsl(120, 100%, 40%);">+         * magic with the old-style magic.</span><br><span style="color: hsl(120, 100%, 40%);">+     *</span><br><span style="color: hsl(120, 100%, 40%);">+     * Why?  This tries to avoid a memory read use-after-free from</span><br><span style="color: hsl(120, 100%, 40%);">+         * disclosing our talloc magic, which would then allow an</span><br><span style="color: hsl(120, 100%, 40%);">+      * attacker to prepare a valid header and so run a destructor.</span><br><span style="color: hsl(120, 100%, 40%);">+         *</span><br><span style="color: hsl(120, 100%, 40%);">+     * What else?  We have to re-stamp back a valid normal magic</span><br><span style="color: hsl(120, 100%, 40%);">+   * on this memory once realloc() is done, as it will have done</span><br><span style="color: hsl(120, 100%, 40%);">+         * a memcpy() into the new valid memory.  We can't do this in</span><br><span style="color: hsl(120, 100%, 40%);">+      * reverse as that would be a real use-after-free.</span><br><span style="color: hsl(120, 100%, 40%);">+     */</span><br><span style="color: hsl(120, 100%, 40%);">+   _talloc_chunk_set_free(tc, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#if ALWAYS_REALLOC</span><br><span style="color: hsl(120, 100%, 40%);">+       if (pool_hdr) {</span><br><span style="color: hsl(120, 100%, 40%);">+               new_ptr = tc_alloc_pool(tc, size + TC_HDR_SIZE, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+           pool_hdr->object_count--;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                if (new_ptr == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+                        new_ptr = malloc(TC_HDR_SIZE+size);</span><br><span style="color: hsl(120, 100%, 40%);">+                   malloced = true;</span><br><span style="color: hsl(120, 100%, 40%);">+                      new_size = size;</span><br><span style="color: hsl(120, 100%, 40%);">+              }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+           if (new_ptr) {</span><br><span style="color: hsl(120, 100%, 40%);">+                        memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);</span><br><span style="color: hsl(120, 100%, 40%);">+                     TC_INVALIDATE_FULL_CHUNK(tc);</span><br><span style="color: hsl(120, 100%, 40%);">+         }</span><br><span style="color: hsl(120, 100%, 40%);">+     } else {</span><br><span style="color: hsl(120, 100%, 40%);">+              /* We're doing malloc then free here, so record the difference. */</span><br><span style="color: hsl(120, 100%, 40%);">+                old_size = tc->size;</span><br><span style="color: hsl(120, 100%, 40%);">+               new_size = size;</span><br><span style="color: hsl(120, 100%, 40%);">+              new_ptr = malloc(size + TC_HDR_SIZE);</span><br><span style="color: hsl(120, 100%, 40%);">+         if (new_ptr) {</span><br><span style="color: hsl(120, 100%, 40%);">+                        memcpy(new_ptr, tc, MIN(tc->size, size) + TC_HDR_SIZE);</span><br><span style="color: hsl(120, 100%, 40%);">+                    free(tc);</span><br><span style="color: hsl(120, 100%, 40%);">+             }</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+      if (pool_hdr) {</span><br><span style="color: hsl(120, 100%, 40%);">+               struct talloc_chunk *pool_tc;</span><br><span style="color: hsl(120, 100%, 40%);">+         void *next_tc = tc_next_chunk(tc);</span><br><span style="color: hsl(120, 100%, 40%);">+            size_t old_chunk_size = TC_ALIGN16(TC_HDR_SIZE + tc->size);</span><br><span style="color: hsl(120, 100%, 40%);">+                size_t new_chunk_size = TC_ALIGN16(TC_HDR_SIZE + size);</span><br><span style="color: hsl(120, 100%, 40%);">+               size_t space_needed;</span><br><span style="color: hsl(120, 100%, 40%);">+          size_t space_left;</span><br><span style="color: hsl(120, 100%, 40%);">+            unsigned int chunk_count = pool_hdr->object_count;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+               pool_tc = talloc_chunk_from_pool(pool_hdr);</span><br><span style="color: hsl(120, 100%, 40%);">+           if (!(pool_tc->flags & TALLOC_FLAG_FREE)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                    chunk_count -= 1;</span><br><span style="color: hsl(120, 100%, 40%);">+             }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+           if (chunk_count == 1) {</span><br><span style="color: hsl(120, 100%, 40%);">+                       /*</span><br><span style="color: hsl(120, 100%, 40%);">+                     * optimize for the case where 'tc' is the only</span><br><span style="color: hsl(120, 100%, 40%);">+                        * chunk in the pool.</span><br><span style="color: hsl(120, 100%, 40%);">+                  */</span><br><span style="color: hsl(120, 100%, 40%);">+                   char *start = tc_pool_first_chunk(pool_hdr);</span><br><span style="color: hsl(120, 100%, 40%);">+                  space_needed = new_chunk_size;</span><br><span style="color: hsl(120, 100%, 40%);">+                        space_left = (char *)tc_pool_end(pool_hdr) - start;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                 if (space_left >= space_needed) {</span><br><span style="color: hsl(120, 100%, 40%);">+                          size_t old_used = TC_HDR_SIZE + tc->size;</span><br><span style="color: hsl(120, 100%, 40%);">+                          size_t new_used = TC_HDR_SIZE + size;</span><br><span style="color: hsl(120, 100%, 40%);">+                         new_ptr = start;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)</span><br><span style="color: hsl(120, 100%, 40%);">+                            {</span><br><span style="color: hsl(120, 100%, 40%);">+                                     /*</span><br><span style="color: hsl(120, 100%, 40%);">+                                     * The area from</span><br><span style="color: hsl(120, 100%, 40%);">+                                       * start -> tc may have</span><br><span style="color: hsl(120, 100%, 40%);">+                                     * been freed and thus been marked as</span><br><span style="color: hsl(120, 100%, 40%);">+                                  * VALGRIND_MEM_NOACCESS. Set it to</span><br><span style="color: hsl(120, 100%, 40%);">+                                    * VALGRIND_MEM_UNDEFINED so we can</span><br><span style="color: hsl(120, 100%, 40%);">+                                    * copy into it without valgrind errors.</span><br><span style="color: hsl(120, 100%, 40%);">+                                       * We can't just mark</span><br><span style="color: hsl(120, 100%, 40%);">+                                      * new_ptr -> new_ptr + old_used</span><br><span style="color: hsl(120, 100%, 40%);">+                                    * as this may overlap on top of tc,</span><br><span style="color: hsl(120, 100%, 40%);">+                                   * (which is why we use memmove, not</span><br><span style="color: hsl(120, 100%, 40%);">+                                   * memcpy below) hence the MIN.</span><br><span style="color: hsl(120, 100%, 40%);">+                                        */</span><br><span style="color: hsl(120, 100%, 40%);">+                                   size_t undef_len = MIN((((char *)tc) - ((char *)new_ptr)),old_used);</span><br><span style="color: hsl(120, 100%, 40%);">+                                  VALGRIND_MAKE_MEM_UNDEFINED(new_ptr, undef_len);</span><br><span style="color: hsl(120, 100%, 40%);">+                              }</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                           memmove(new_ptr, tc, old_used);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                             tc = (struct talloc_chunk *)new_ptr;</span><br><span style="color: hsl(120, 100%, 40%);">+                          TC_UNDEFINE_GROW_CHUNK(tc, size);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                           /*</span><br><span style="color: hsl(120, 100%, 40%);">+                             * first we do not align the pool pointer</span><br><span style="color: hsl(120, 100%, 40%);">+                              * because we want to invalidate the padding</span><br><span style="color: hsl(120, 100%, 40%);">+                           * too.</span><br><span style="color: hsl(120, 100%, 40%);">+                                */</span><br><span style="color: hsl(120, 100%, 40%);">+                           pool_hdr->end = new_used + (char *)new_ptr;</span><br><span style="color: hsl(120, 100%, 40%);">+                                tc_invalidate_pool(pool_hdr);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                               /* now the aligned pointer */</span><br><span style="color: hsl(120, 100%, 40%);">+                         pool_hdr->end = new_chunk_size + (char *)new_ptr;</span><br><span style="color: hsl(120, 100%, 40%);">+                          goto got_new_ptr;</span><br><span style="color: hsl(120, 100%, 40%);">+                     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                   next_tc = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+               }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+           if (new_chunk_size == old_chunk_size) {</span><br><span style="color: hsl(120, 100%, 40%);">+                       TC_UNDEFINE_GROW_CHUNK(tc, size);</span><br><span style="color: hsl(120, 100%, 40%);">+                     _talloc_chunk_set_not_free(tc);</span><br><span style="color: hsl(120, 100%, 40%);">+                       tc->size = size;</span><br><span style="color: hsl(120, 100%, 40%);">+                   return ptr;</span><br><span style="color: hsl(120, 100%, 40%);">+           }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+           if (next_tc == pool_hdr->end) {</span><br><span style="color: hsl(120, 100%, 40%);">+                    /*</span><br><span style="color: hsl(120, 100%, 40%);">+                     * optimize for the case where 'tc' is the last</span><br><span style="color: hsl(120, 100%, 40%);">+                        * chunk in the pool.</span><br><span style="color: hsl(120, 100%, 40%);">+                  */</span><br><span style="color: hsl(120, 100%, 40%);">+                   space_needed = new_chunk_size - old_chunk_size;</span><br><span style="color: hsl(120, 100%, 40%);">+                       space_left = tc_pool_space_left(pool_hdr);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                  if (space_left >= space_needed) {</span><br><span style="color: hsl(120, 100%, 40%);">+                          TC_UNDEFINE_GROW_CHUNK(tc, size);</span><br><span style="color: hsl(120, 100%, 40%);">+                             _talloc_chunk_set_not_free(tc);</span><br><span style="color: hsl(120, 100%, 40%);">+                               tc->size = size;</span><br><span style="color: hsl(120, 100%, 40%);">+                           pool_hdr->end = tc_next_chunk(tc);</span><br><span style="color: hsl(120, 100%, 40%);">+                         return ptr;</span><br><span style="color: hsl(120, 100%, 40%);">+                   }</span><br><span style="color: hsl(120, 100%, 40%);">+             }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+           new_ptr = tc_alloc_pool(tc, size + TC_HDR_SIZE, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+         if (new_ptr == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+                        new_ptr = malloc(TC_HDR_SIZE+size);</span><br><span style="color: hsl(120, 100%, 40%);">+                   malloced = true;</span><br><span style="color: hsl(120, 100%, 40%);">+                      new_size = size;</span><br><span style="color: hsl(120, 100%, 40%);">+              }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+           if (new_ptr) {</span><br><span style="color: hsl(120, 100%, 40%);">+                        memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                   _tc_free_poolmem(tc, __location__ "_talloc_realloc");</span><br><span style="color: hsl(120, 100%, 40%);">+               }</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+     else {</span><br><span style="color: hsl(120, 100%, 40%);">+                /* We're doing realloc here, so record the difference. */</span><br><span style="color: hsl(120, 100%, 40%);">+         old_size = tc->size;</span><br><span style="color: hsl(120, 100%, 40%);">+               new_size = size;</span><br><span style="color: hsl(120, 100%, 40%);">+              new_ptr = realloc(tc, size + TC_HDR_SIZE);</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+got_new_ptr:</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+       if (unlikely(!new_ptr)) {</span><br><span style="color: hsl(120, 100%, 40%);">+             /*</span><br><span style="color: hsl(120, 100%, 40%);">+             * Ok, this is a strange spot.  We have to put back</span><br><span style="color: hsl(120, 100%, 40%);">+            * the old talloc_magic and any flags, except the</span><br><span style="color: hsl(120, 100%, 40%);">+              * TALLOC_FLAG_FREE as this was not free'ed by the</span><br><span style="color: hsl(120, 100%, 40%);">+                 * realloc() call after all</span><br><span style="color: hsl(120, 100%, 40%);">+            */</span><br><span style="color: hsl(120, 100%, 40%);">+           _talloc_chunk_set_not_free(tc);</span><br><span style="color: hsl(120, 100%, 40%);">+               return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   /*</span><br><span style="color: hsl(120, 100%, 40%);">+     * tc is now the new value from realloc(), the old memory we</span><br><span style="color: hsl(120, 100%, 40%);">+   * can't access any more and was preemptively marked as</span><br><span style="color: hsl(120, 100%, 40%);">+    * TALLOC_FLAG_FREE before the call.  Now we mark it as not</span><br><span style="color: hsl(120, 100%, 40%);">+    * free again</span><br><span style="color: hsl(120, 100%, 40%);">+  */</span><br><span style="color: hsl(120, 100%, 40%);">+   tc = (struct talloc_chunk *)new_ptr;</span><br><span style="color: hsl(120, 100%, 40%);">+  _talloc_chunk_set_not_free(tc);</span><br><span style="color: hsl(120, 100%, 40%);">+       if (malloced) {</span><br><span style="color: hsl(120, 100%, 40%);">+               tc->flags &= ~TALLOC_FLAG_POOLMEM;</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+     if (tc->parent) {</span><br><span style="color: hsl(120, 100%, 40%);">+          tc->parent->child = tc;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+     if (tc->child) {</span><br><span style="color: hsl(120, 100%, 40%);">+           tc->child->parent = tc;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (tc->prev) {</span><br><span style="color: hsl(120, 100%, 40%);">+            tc->prev->next = tc;</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+     if (tc->next) {</span><br><span style="color: hsl(120, 100%, 40%);">+            tc->next->prev = tc;</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (new_size > old_size) {</span><br><span style="color: hsl(120, 100%, 40%);">+         talloc_memlimit_grow(tc->limit, new_size - old_size);</span><br><span style="color: hsl(120, 100%, 40%);">+      } else if (new_size < old_size) {</span><br><span style="color: hsl(120, 100%, 40%);">+          talloc_memlimit_shrink(tc->limit, old_size - new_size);</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   tc->size = size;</span><br><span style="color: hsl(120, 100%, 40%);">+   _tc_set_name_const(tc, name);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       return TC_PTR_FROM_CHUNK(tc);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  a wrapper around talloc_steal() for situations where you are moving a pointer</span><br><span style="color: hsl(120, 100%, 40%);">+  between two structures, and want the old pointer to be set to NULL</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void *_talloc_move(const void *new_ctx, const void *_pptr)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+     const void **pptr = discard_const_p(const void *,_pptr);</span><br><span style="color: hsl(120, 100%, 40%);">+      void *ret = talloc_steal(new_ctx, discard_const_p(void, *pptr));</span><br><span style="color: hsl(120, 100%, 40%);">+      (*pptr) = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+       return ret;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+enum talloc_mem_count_type {</span><br><span style="color: hsl(120, 100%, 40%);">+        TOTAL_MEM_SIZE,</span><br><span style="color: hsl(120, 100%, 40%);">+       TOTAL_MEM_BLOCKS,</span><br><span style="color: hsl(120, 100%, 40%);">+     TOTAL_MEM_LIMIT,</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static inline size_t _talloc_total_mem_internal(const void *ptr,</span><br><span style="color: hsl(120, 100%, 40%);">+                                       enum talloc_mem_count_type type,</span><br><span style="color: hsl(120, 100%, 40%);">+                                      struct talloc_memlimit *old_limit,</span><br><span style="color: hsl(120, 100%, 40%);">+                                    struct talloc_memlimit *new_limit)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        size_t total = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+     struct talloc_chunk *c, *tc;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        if (ptr == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+            ptr = null_context;</span><br><span style="color: hsl(120, 100%, 40%);">+   }</span><br><span style="color: hsl(120, 100%, 40%);">+     if (ptr == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+            return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   tc = talloc_chunk_from_ptr(ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    if (old_limit || new_limit) {</span><br><span style="color: hsl(120, 100%, 40%);">+         if (tc->limit && tc->limit->upper == old_limit) {</span><br><span style="color: hsl(120, 100%, 40%);">+                    tc->limit->upper = new_limit;</span><br><span style="color: hsl(120, 100%, 40%);">+           }</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   /* optimize in the memlimits case */</span><br><span style="color: hsl(120, 100%, 40%);">+  if (type == TOTAL_MEM_LIMIT &&</span><br><span style="color: hsl(120, 100%, 40%);">+            tc->limit != NULL &&</span><br><span style="color: hsl(120, 100%, 40%);">+       tc->limit != old_limit &&</span><br><span style="color: hsl(120, 100%, 40%);">+          tc->limit->parent == tc) {</span><br><span style="color: hsl(120, 100%, 40%);">+          return tc->limit->cur_size;</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (tc->flags & TALLOC_FLAG_LOOP) {</span><br><span style="color: hsl(120, 100%, 40%);">+            return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   tc->flags |= TALLOC_FLAG_LOOP;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (old_limit || new_limit) {</span><br><span style="color: hsl(120, 100%, 40%);">+         if (old_limit == tc->limit) {</span><br><span style="color: hsl(120, 100%, 40%);">+                      tc->limit = new_limit;</span><br><span style="color: hsl(120, 100%, 40%);">+             }</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   switch (type) {</span><br><span style="color: hsl(120, 100%, 40%);">+       case TOTAL_MEM_SIZE:</span><br><span style="color: hsl(120, 100%, 40%);">+          if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                  total = tc->size;</span><br><span style="color: hsl(120, 100%, 40%);">+          }</span><br><span style="color: hsl(120, 100%, 40%);">+             break;</span><br><span style="color: hsl(120, 100%, 40%);">+        case TOTAL_MEM_BLOCKS:</span><br><span style="color: hsl(120, 100%, 40%);">+                total++;</span><br><span style="color: hsl(120, 100%, 40%);">+              break;</span><br><span style="color: hsl(120, 100%, 40%);">+        case TOTAL_MEM_LIMIT:</span><br><span style="color: hsl(120, 100%, 40%);">+         if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                  /*</span><br><span style="color: hsl(120, 100%, 40%);">+                     * Don't count memory allocated from a pool</span><br><span style="color: hsl(120, 100%, 40%);">+                        * when calculating limits. Only count the</span><br><span style="color: hsl(120, 100%, 40%);">+                     * pool itself.</span><br><span style="color: hsl(120, 100%, 40%);">+                        */</span><br><span style="color: hsl(120, 100%, 40%);">+                   if (!(tc->flags & TALLOC_FLAG_POOLMEM)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                              if (tc->flags & TALLOC_FLAG_POOL) {</span><br><span style="color: hsl(120, 100%, 40%);">+                                    /*</span><br><span style="color: hsl(120, 100%, 40%);">+                                     * If this is a pool, the allocated</span><br><span style="color: hsl(120, 100%, 40%);">+                                    * size is in the pool header, and</span><br><span style="color: hsl(120, 100%, 40%);">+                                     * remember to add in the prefix</span><br><span style="color: hsl(120, 100%, 40%);">+                                       * length.</span><br><span style="color: hsl(120, 100%, 40%);">+                                     */</span><br><span style="color: hsl(120, 100%, 40%);">+                                   struct talloc_pool_hdr *pool_hdr</span><br><span style="color: hsl(120, 100%, 40%);">+                                                      = talloc_pool_from_chunk(tc);</span><br><span style="color: hsl(120, 100%, 40%);">+                                 total = pool_hdr->poolsize +</span><br><span style="color: hsl(120, 100%, 40%);">+                                                       TC_HDR_SIZE +</span><br><span style="color: hsl(120, 100%, 40%);">+                                                 TP_HDR_SIZE;</span><br><span style="color: hsl(120, 100%, 40%);">+                          } else {</span><br><span style="color: hsl(120, 100%, 40%);">+                                      total = tc->size + TC_HDR_SIZE;</span><br><span style="color: hsl(120, 100%, 40%);">+                            }</span><br><span style="color: hsl(120, 100%, 40%);">+                     }</span><br><span style="color: hsl(120, 100%, 40%);">+             }</span><br><span style="color: hsl(120, 100%, 40%);">+             break;</span><br><span style="color: hsl(120, 100%, 40%);">+        }</span><br><span style="color: hsl(120, 100%, 40%);">+     for (c = tc->child; c; c = c->next) {</span><br><span style="color: hsl(120, 100%, 40%);">+           total += _talloc_total_mem_internal(TC_PTR_FROM_CHUNK(c), type,</span><br><span style="color: hsl(120, 100%, 40%);">+                                                   old_limit, new_limit);</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   tc->flags &= ~TALLOC_FLAG_LOOP;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      return total;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  return the total size of a talloc pool (subtree)</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ size_t talloc_total_size(const void *ptr)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ return _talloc_total_mem_internal(ptr, TOTAL_MEM_SIZE, NULL, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  return the total number of blocks in a talloc pool (subtree)</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ size_t talloc_total_blocks(const void *ptr)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+     return _talloc_total_mem_internal(ptr, TOTAL_MEM_BLOCKS, NULL, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  return the number of external references to a pointer</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ size_t talloc_reference_count(const void *ptr)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+ struct talloc_reference_handle *h;</span><br><span style="color: hsl(120, 100%, 40%);">+    size_t ret = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     for (h=tc->refs;h;h=h->next) {</span><br><span style="color: hsl(120, 100%, 40%);">+          ret++;</span><br><span style="color: hsl(120, 100%, 40%);">+        }</span><br><span style="color: hsl(120, 100%, 40%);">+     return ret;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  report on memory usage by all children of a pointer, giving a full tree view</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,</span><br><span style="color: hsl(120, 100%, 40%);">+                         void (*callback)(const void *ptr,</span><br><span style="color: hsl(120, 100%, 40%);">+                                      int depth, int max_depth,</span><br><span style="color: hsl(120, 100%, 40%);">+                                             int is_ref,</span><br><span style="color: hsl(120, 100%, 40%);">+                                           void *private_data),</span><br><span style="color: hsl(120, 100%, 40%);">+                         void *private_data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+    struct talloc_chunk *c, *tc;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        if (ptr == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+            ptr = null_context;</span><br><span style="color: hsl(120, 100%, 40%);">+   }</span><br><span style="color: hsl(120, 100%, 40%);">+     if (ptr == NULL) return;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    tc = talloc_chunk_from_ptr(ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    if (tc->flags & TALLOC_FLAG_LOOP) {</span><br><span style="color: hsl(120, 100%, 40%);">+            return;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   callback(ptr, depth, max_depth, 0, private_data);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (max_depth >= 0 && depth >= max_depth) {</span><br><span style="color: hsl(120, 100%, 40%);">+             return;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   tc->flags |= TALLOC_FLAG_LOOP;</span><br><span style="color: hsl(120, 100%, 40%);">+     for (c=tc->child;c;c=c->next) {</span><br><span style="color: hsl(120, 100%, 40%);">+         if (c->name == TALLOC_MAGIC_REFERENCE) {</span><br><span style="color: hsl(120, 100%, 40%);">+                   struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);</span><br><span style="color: hsl(120, 100%, 40%);">+                   callback(h->ptr, depth + 1, max_depth, 1, private_data);</span><br><span style="color: hsl(120, 100%, 40%);">+           } else {</span><br><span style="color: hsl(120, 100%, 40%);">+                      talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);</span><br><span style="color: hsl(120, 100%, 40%);">+           }</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+     tc->flags &= ~TALLOC_FLAG_LOOP;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  const char *name = __talloc_get_name(ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+    struct talloc_chunk *tc;</span><br><span style="color: hsl(120, 100%, 40%);">+      FILE *f = (FILE *)_f;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       if (is_ref) {</span><br><span style="color: hsl(120, 100%, 40%);">+         fprintf(f, "%*sreference to: %s\n", depth*4, "", name);</span><br><span style="color: hsl(120, 100%, 40%);">+           return;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   tc = talloc_chunk_from_ptr(ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+      if (tc->limit && tc->limit->parent == tc) {</span><br><span style="color: hsl(120, 100%, 40%);">+          fprintf(f, "%*s%-30s is a memlimit context"</span><br><span style="color: hsl(120, 100%, 40%);">+                 " (max_size = %lu bytes, cur_size = %lu bytes)\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                  depth*4, "",</span><br><span style="color: hsl(120, 100%, 40%);">+                        name,</span><br><span style="color: hsl(120, 100%, 40%);">+                 (unsigned long)tc->limit->max_size,</span><br><span style="color: hsl(120, 100%, 40%);">+                     (unsigned long)tc->limit->cur_size);</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (depth == 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+             fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                    (max_depth < 0 ? "full " :""), name,</span><br><span style="color: hsl(120, 100%, 40%);">+                   (unsigned long)talloc_total_size(ptr),</span><br><span style="color: hsl(120, 100%, 40%);">+                        (unsigned long)talloc_total_blocks(ptr));</span><br><span style="color: hsl(120, 100%, 40%);">+             return;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",</span><br><span style="color: hsl(120, 100%, 40%);">+             depth*4, "",</span><br><span style="color: hsl(120, 100%, 40%);">+                name,</span><br><span style="color: hsl(120, 100%, 40%);">+         (unsigned long)talloc_total_size(ptr),</span><br><span style="color: hsl(120, 100%, 40%);">+                (unsigned long)talloc_total_blocks(ptr),</span><br><span style="color: hsl(120, 100%, 40%);">+              (int)talloc_reference_count(ptr), ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#if 0</span><br><span style="color: hsl(120, 100%, 40%);">+      fprintf(f, "content: ");</span><br><span style="color: hsl(120, 100%, 40%);">+    if (talloc_total_size(ptr)) {</span><br><span style="color: hsl(120, 100%, 40%);">+         int tot = talloc_total_size(ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+             int i;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+              for (i = 0; i < tot; i++) {</span><br><span style="color: hsl(120, 100%, 40%);">+                        if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                              fprintf(f, "%c", ((char *)ptr)[i]);</span><br><span style="color: hsl(120, 100%, 40%);">+                 } else {</span><br><span style="color: hsl(120, 100%, 40%);">+                              fprintf(f, "~%02x", ((char *)ptr)[i]);</span><br><span style="color: hsl(120, 100%, 40%);">+                      }</span><br><span style="color: hsl(120, 100%, 40%);">+             }</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+     fprintf(f, "\n");</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  report on memory usage by all children of a pointer, giving a full tree view</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       if (f) {</span><br><span style="color: hsl(120, 100%, 40%);">+              talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);</span><br><span style="color: hsl(120, 100%, 40%);">+            fflush(f);</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  report on memory usage by all children of a pointer, giving a full tree view</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void talloc_report_full(const void *ptr, FILE *f)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ talloc_report_depth_file(ptr, 0, -1, f);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  report on memory usage by all children of a pointer</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void talloc_report(const void *ptr, FILE *f)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        talloc_report_depth_file(ptr, 0, 1, f);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  enable tracking of the NULL context</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void talloc_enable_null_tracking(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       if (null_context == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+           null_context = _talloc_named_const(NULL, 0, "null_context");</span><br><span style="color: hsl(120, 100%, 40%);">+                if (autofree_context != NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+                       talloc_reparent(NULL, null_context, autofree_context);</span><br><span style="color: hsl(120, 100%, 40%);">+                }</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  enable tracking of the NULL context, not moving the autofree context</span><br><span style="color: hsl(120, 100%, 40%);">+  into the NULL context. This is needed for the talloc testsuite</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void talloc_enable_null_tracking_no_autofree(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+      if (null_context == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+           null_context = _talloc_named_const(NULL, 0, "null_context");</span><br><span style="color: hsl(120, 100%, 40%);">+        }</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  disable tracking of the NULL context</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void talloc_disable_null_tracking(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+   if (null_context != NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+           /* we have to move any children onto the real NULL</span><br><span style="color: hsl(120, 100%, 40%);">+               context */</span><br><span style="color: hsl(120, 100%, 40%);">+         struct talloc_chunk *tc, *tc2;</span><br><span style="color: hsl(120, 100%, 40%);">+                tc = talloc_chunk_from_ptr(null_context);</span><br><span style="color: hsl(120, 100%, 40%);">+             for (tc2 = tc->child; tc2; tc2=tc2->next) {</span><br><span style="color: hsl(120, 100%, 40%);">+                     if (tc2->parent == tc) tc2->parent = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+                      if (tc2->prev == tc) tc2->prev = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+          }</span><br><span style="color: hsl(120, 100%, 40%);">+             for (tc2 = tc->next; tc2; tc2=tc2->next) {</span><br><span style="color: hsl(120, 100%, 40%);">+                      if (tc2->parent == tc) tc2->parent = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+                      if (tc2->prev == tc) tc2->prev = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+          }</span><br><span style="color: hsl(120, 100%, 40%);">+             tc->child = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+          tc->next = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+   }</span><br><span style="color: hsl(120, 100%, 40%);">+     talloc_free(null_context);</span><br><span style="color: hsl(120, 100%, 40%);">+    null_context = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  enable leak reporting on exit</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void talloc_enable_leak_report(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  talloc_enable_null_tracking();</span><br><span style="color: hsl(120, 100%, 40%);">+        talloc_report_null = true;</span><br><span style="color: hsl(120, 100%, 40%);">+    talloc_setup_atexit();</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  enable full leak reporting on exit</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void talloc_enable_leak_report_full(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+      talloc_enable_null_tracking();</span><br><span style="color: hsl(120, 100%, 40%);">+        talloc_report_null_full = true;</span><br><span style="color: hsl(120, 100%, 40%);">+       talloc_setup_atexit();</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+   talloc and zero memory.</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void *_talloc_zero(const void *ctx, size_t size, const char *name)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       void *p = _talloc_named_const(ctx, size, name);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     if (p) {</span><br><span style="color: hsl(120, 100%, 40%);">+              memset(p, '\0', size);</span><br><span style="color: hsl(120, 100%, 40%);">+        }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   return p;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  memdup with a talloc.</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        void *newp = _talloc_named_const(t, size, name);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    if (likely(newp)) {</span><br><span style="color: hsl(120, 100%, 40%);">+           memcpy(newp, p, size);</span><br><span style="color: hsl(120, 100%, 40%);">+        }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   return newp;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        char *ret;</span><br><span style="color: hsl(120, 100%, 40%);">+    struct talloc_chunk *tc;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    ret = (char *)__talloc(t, len + 1, &tc);</span><br><span style="color: hsl(120, 100%, 40%);">+  if (unlikely(!ret)) return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    memcpy(ret, p, len);</span><br><span style="color: hsl(120, 100%, 40%);">+  ret[len] = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       _tc_set_name_const(tc, ret);</span><br><span style="color: hsl(120, 100%, 40%);">+  return ret;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  strdup with a talloc</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ char *talloc_strdup(const void *t, const char *p)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       if (unlikely(!p)) return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+        return __talloc_strlendup(t, p, strlen(p));</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  strndup with a talloc</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ char *talloc_strndup(const void *t, const char *p, size_t n)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+   if (unlikely(!p)) return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+        return __talloc_strlendup(t, p, strnlen(p, n));</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static inline char *__talloc_strlendup_append(char *s, size_t slen,</span><br><span style="color: hsl(120, 100%, 40%);">+                                           const char *a, size_t alen)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  char *ret;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  ret = talloc_realloc(NULL, s, char, slen + alen + 1);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (unlikely(!ret)) return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    /* append the string and the trailing \0 */</span><br><span style="color: hsl(120, 100%, 40%);">+   memcpy(&ret[slen], a, alen);</span><br><span style="color: hsl(120, 100%, 40%);">+      ret[slen+alen] = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ _tc_set_name_const(talloc_chunk_from_ptr(ret), ret);</span><br><span style="color: hsl(120, 100%, 40%);">+  return ret;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+ * Appends at the end of the string.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ char *talloc_strdup_append(char *s, const char *a)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       if (unlikely(!s)) {</span><br><span style="color: hsl(120, 100%, 40%);">+           return talloc_strdup(NULL, a);</span><br><span style="color: hsl(120, 100%, 40%);">+        }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (unlikely(!a)) {</span><br><span style="color: hsl(120, 100%, 40%);">+           return s;</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   return __talloc_strlendup_append(s, strlen(s), a, strlen(a));</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+ * Appends at the end of the talloc'ed buffer,</span><br><span style="color: hsl(120, 100%, 40%);">+ * not the end of the string.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ char *talloc_strdup_append_buffer(char *s, const char *a)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ size_t slen;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        if (unlikely(!s)) {</span><br><span style="color: hsl(120, 100%, 40%);">+           return talloc_strdup(NULL, a);</span><br><span style="color: hsl(120, 100%, 40%);">+        }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (unlikely(!a)) {</span><br><span style="color: hsl(120, 100%, 40%);">+           return s;</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   slen = talloc_get_size(s);</span><br><span style="color: hsl(120, 100%, 40%);">+    if (likely(slen > 0)) {</span><br><span style="color: hsl(120, 100%, 40%);">+            slen--;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   return __talloc_strlendup_append(s, slen, a, strlen(a));</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+ * Appends at the end of the string.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ char *talloc_strndup_append(char *s, const char *a, size_t n)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       if (unlikely(!s)) {</span><br><span style="color: hsl(120, 100%, 40%);">+           return talloc_strndup(NULL, a, n);</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (unlikely(!a)) {</span><br><span style="color: hsl(120, 100%, 40%);">+           return s;</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+ * Appends at the end of the talloc'ed buffer,</span><br><span style="color: hsl(120, 100%, 40%);">+ * not the end of the string.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  size_t slen;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        if (unlikely(!s)) {</span><br><span style="color: hsl(120, 100%, 40%);">+           return talloc_strndup(NULL, a, n);</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (unlikely(!a)) {</span><br><span style="color: hsl(120, 100%, 40%);">+           return s;</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   slen = talloc_get_size(s);</span><br><span style="color: hsl(120, 100%, 40%);">+    if (likely(slen > 0)) {</span><br><span style="color: hsl(120, 100%, 40%);">+            slen--;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   return __talloc_strlendup_append(s, slen, a, strnlen(a, n));</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifndef HAVE_VA_COPY</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef HAVE___VA_COPY</span><br><span style="color: hsl(120, 100%, 40%);">+#define va_copy(dest, src) __va_copy(dest, src)</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+#define va_copy(dest, src) (dest) = (src)</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static struct talloc_chunk *_vasprintf_tc(const void *t,</span><br><span style="color: hsl(120, 100%, 40%);">+                                   const char *fmt,</span><br><span style="color: hsl(120, 100%, 40%);">+                                      va_list ap) PRINTF_ATTRIBUTE(2,0);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static struct talloc_chunk *_vasprintf_tc(const void *t,</span><br><span style="color: hsl(120, 100%, 40%);">+                                        const char *fmt,</span><br><span style="color: hsl(120, 100%, 40%);">+                                      va_list ap)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+      int vlen;</span><br><span style="color: hsl(120, 100%, 40%);">+     size_t len;</span><br><span style="color: hsl(120, 100%, 40%);">+   char *ret;</span><br><span style="color: hsl(120, 100%, 40%);">+    va_list ap2;</span><br><span style="color: hsl(120, 100%, 40%);">+  struct talloc_chunk *tc;</span><br><span style="color: hsl(120, 100%, 40%);">+      char buf[1024];</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     /* this call looks strange, but it makes it work on older solaris boxes */</span><br><span style="color: hsl(120, 100%, 40%);">+    va_copy(ap2, ap);</span><br><span style="color: hsl(120, 100%, 40%);">+     vlen = vsnprintf(buf, sizeof(buf), fmt, ap2);</span><br><span style="color: hsl(120, 100%, 40%);">+ va_end(ap2);</span><br><span style="color: hsl(120, 100%, 40%);">+  if (unlikely(vlen < 0)) {</span><br><span style="color: hsl(120, 100%, 40%);">+          return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+     len = vlen;</span><br><span style="color: hsl(120, 100%, 40%);">+   if (unlikely(len + 1 < len)) {</span><br><span style="color: hsl(120, 100%, 40%);">+             return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   ret = (char *)__talloc(t, len+1, &tc);</span><br><span style="color: hsl(120, 100%, 40%);">+    if (unlikely(!ret)) return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    if (len < sizeof(buf)) {</span><br><span style="color: hsl(120, 100%, 40%);">+           memcpy(ret, buf, len+1);</span><br><span style="color: hsl(120, 100%, 40%);">+      } else {</span><br><span style="color: hsl(120, 100%, 40%);">+              va_copy(ap2, ap);</span><br><span style="color: hsl(120, 100%, 40%);">+             vsnprintf(ret, len+1, fmt, ap2);</span><br><span style="color: hsl(120, 100%, 40%);">+              va_end(ap2);</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   _tc_set_name_const(tc, ret);</span><br><span style="color: hsl(120, 100%, 40%);">+  return tc;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       struct talloc_chunk *tc = _vasprintf_tc(t, fmt, ap);</span><br><span style="color: hsl(120, 100%, 40%);">+  if (tc == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+             return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+     return TC_PTR_FROM_CHUNK(tc);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  Perform string formatting, and return a pointer to newly allocated</span><br><span style="color: hsl(120, 100%, 40%);">+  memory holding the result, inside a memory pool.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ char *talloc_asprintf(const void *t, const char *fmt, ...)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       va_list ap;</span><br><span style="color: hsl(120, 100%, 40%);">+   char *ret;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  va_start(ap, fmt);</span><br><span style="color: hsl(120, 100%, 40%);">+    ret = talloc_vasprintf(t, fmt, ap);</span><br><span style="color: hsl(120, 100%, 40%);">+   va_end(ap);</span><br><span style="color: hsl(120, 100%, 40%);">+   return ret;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,</span><br><span style="color: hsl(120, 100%, 40%);">+                                               const char *fmt, va_list ap)</span><br><span style="color: hsl(120, 100%, 40%);">+                                          PRINTF_ATTRIBUTE(3,0);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,</span><br><span style="color: hsl(120, 100%, 40%);">+                                              const char *fmt, va_list ap)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+      ssize_t alen;</span><br><span style="color: hsl(120, 100%, 40%);">+ va_list ap2;</span><br><span style="color: hsl(120, 100%, 40%);">+  char c;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     va_copy(ap2, ap);</span><br><span style="color: hsl(120, 100%, 40%);">+     alen = vsnprintf(&c, 1, fmt, ap2);</span><br><span style="color: hsl(120, 100%, 40%);">+        va_end(ap2);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        if (alen <= 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+           /* Either the vsnprintf failed or the format resulted in</span><br><span style="color: hsl(120, 100%, 40%);">+               * no characters being formatted. In the former case, we</span><br><span style="color: hsl(120, 100%, 40%);">+               * ought to return NULL, in the latter we ought to return</span><br><span style="color: hsl(120, 100%, 40%);">+              * the original string. Most current callers of this</span><br><span style="color: hsl(120, 100%, 40%);">+           * function expect it to never return NULL.</span><br><span style="color: hsl(120, 100%, 40%);">+            */</span><br><span style="color: hsl(120, 100%, 40%);">+           return s;</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   s = talloc_realloc(NULL, s, char, slen + alen + 1);</span><br><span style="color: hsl(120, 100%, 40%);">+   if (!s) return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        va_copy(ap2, ap);</span><br><span style="color: hsl(120, 100%, 40%);">+     vsnprintf(s + slen, alen + 1, fmt, ap2);</span><br><span style="color: hsl(120, 100%, 40%);">+      va_end(ap2);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        _tc_set_name_const(talloc_chunk_from_ptr(s), s);</span><br><span style="color: hsl(120, 100%, 40%);">+      return s;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * Realloc @p s to append the formatted result of @p fmt and @p ap,</span><br><span style="color: hsl(120, 100%, 40%);">+ * and return @p s, which may have moved.  Good for gradually</span><br><span style="color: hsl(120, 100%, 40%);">+ * accumulating output into a string buffer. Appends at the end</span><br><span style="color: hsl(120, 100%, 40%);">+ * of the string.</span><br><span style="color: hsl(120, 100%, 40%);">+ **/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+    if (unlikely(!s)) {</span><br><span style="color: hsl(120, 100%, 40%);">+           return talloc_vasprintf(NULL, fmt, ap);</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * Realloc @p s to append the formatted result of @p fmt and @p ap,</span><br><span style="color: hsl(120, 100%, 40%);">+ * and return @p s, which may have moved. Always appends at the</span><br><span style="color: hsl(120, 100%, 40%);">+ * end of the talloc'ed buffer, not the end of the string.</span><br><span style="color: hsl(120, 100%, 40%);">+ **/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+     size_t slen;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        if (unlikely(!s)) {</span><br><span style="color: hsl(120, 100%, 40%);">+           return talloc_vasprintf(NULL, fmt, ap);</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   slen = talloc_get_size(s);</span><br><span style="color: hsl(120, 100%, 40%);">+    if (likely(slen > 0)) {</span><br><span style="color: hsl(120, 100%, 40%);">+            slen--;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   return __talloc_vaslenprintf_append(s, slen, fmt, ap);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  Realloc @p s to append the formatted result of @p fmt and return @p</span><br><span style="color: hsl(120, 100%, 40%);">+  s, which may have moved.  Good for gradually accumulating output</span><br><span style="color: hsl(120, 100%, 40%);">+  into a string buffer.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ char *talloc_asprintf_append(char *s, const char *fmt, ...)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+     va_list ap;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ va_start(ap, fmt);</span><br><span style="color: hsl(120, 100%, 40%);">+    s = talloc_vasprintf_append(s, fmt, ap);</span><br><span style="color: hsl(120, 100%, 40%);">+      va_end(ap);</span><br><span style="color: hsl(120, 100%, 40%);">+   return s;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  Realloc @p s to append the formatted result of @p fmt and return @p</span><br><span style="color: hsl(120, 100%, 40%);">+  s, which may have moved.  Good for gradually accumulating output</span><br><span style="color: hsl(120, 100%, 40%);">+  into a buffer.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  va_list ap;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ va_start(ap, fmt);</span><br><span style="color: hsl(120, 100%, 40%);">+    s = talloc_vasprintf_append_buffer(s, fmt, ap);</span><br><span style="color: hsl(120, 100%, 40%);">+       va_end(ap);</span><br><span style="color: hsl(120, 100%, 40%);">+   return s;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  alloc an array, checking for integer overflow in the array size</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ if (count >= MAX_TALLOC_SIZE/el_size) {</span><br><span style="color: hsl(120, 100%, 40%);">+            return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+     return _talloc_named_const(ctx, el_size * count, name);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  alloc an zero array, checking for integer overflow in the array size</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ if (count >= MAX_TALLOC_SIZE/el_size) {</span><br><span style="color: hsl(120, 100%, 40%);">+            return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+     return _talloc_zero(ctx, el_size * count, name);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  realloc an array, checking for integer overflow in the array size</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+     if (count >= MAX_TALLOC_SIZE/el_size) {</span><br><span style="color: hsl(120, 100%, 40%);">+            return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+     return _talloc_realloc(ctx, ptr, el_size * count, name);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  a function version of talloc_realloc(), so it can be passed as a function pointer</span><br><span style="color: hsl(120, 100%, 40%);">+  to libraries that want a realloc function (a realloc function encapsulates</span><br><span style="color: hsl(120, 100%, 40%);">+  all the basic capabilities of an allocation library, which is why this is useful)</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void *talloc_realloc_fn(const void *context, void *ptr, size_t size)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       return _talloc_realloc(context, ptr, size, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static int talloc_autofree_destructor(void *ptr)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ autofree_context = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+      return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  return a context which will be auto-freed on exit</span><br><span style="color: hsl(120, 100%, 40%);">+  this is useful for reducing the noise in leak reports</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void *talloc_autofree_context(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ if (autofree_context == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+               autofree_context = _talloc_named_const(NULL, 0, "autofree_context");</span><br><span style="color: hsl(120, 100%, 40%);">+                talloc_set_destructor(autofree_context, talloc_autofree_destructor);</span><br><span style="color: hsl(120, 100%, 40%);">+          talloc_setup_atexit();</span><br><span style="color: hsl(120, 100%, 40%);">+        }</span><br><span style="color: hsl(120, 100%, 40%);">+     return autofree_context;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ size_t talloc_get_size(const void *context)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        struct talloc_chunk *tc;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    if (context == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+                return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   tc = talloc_chunk_from_ptr(context);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        return tc->size;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  find a parent of this context that has the given name, if any</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void *talloc_find_parent_byname(const void *context, const char *name)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct talloc_chunk *tc;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    if (context == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+                return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   tc = talloc_chunk_from_ptr(context);</span><br><span style="color: hsl(120, 100%, 40%);">+  while (tc) {</span><br><span style="color: hsl(120, 100%, 40%);">+          if (tc->name && strcmp(tc->name, name) == 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+                  return TC_PTR_FROM_CHUNK(tc);</span><br><span style="color: hsl(120, 100%, 40%);">+         }</span><br><span style="color: hsl(120, 100%, 40%);">+             while (tc && tc->prev) tc = tc->prev;</span><br><span style="color: hsl(120, 100%, 40%);">+           if (tc) {</span><br><span style="color: hsl(120, 100%, 40%);">+                     tc = tc->parent;</span><br><span style="color: hsl(120, 100%, 40%);">+           }</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+     return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  show the parentage of a context</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ void talloc_show_parents(const void *context, FILE *file)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+   struct talloc_chunk *tc;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    if (context == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+                fprintf(file, "talloc no parents for NULL\n");</span><br><span style="color: hsl(120, 100%, 40%);">+              return;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   tc = talloc_chunk_from_ptr(context);</span><br><span style="color: hsl(120, 100%, 40%);">+  fprintf(file, "talloc parents of '%s'\n", __talloc_get_name(context));</span><br><span style="color: hsl(120, 100%, 40%);">+      while (tc) {</span><br><span style="color: hsl(120, 100%, 40%);">+          fprintf(file, "\t'%s'\n", __talloc_get_name(TC_PTR_FROM_CHUNK(tc)));</span><br><span style="color: hsl(120, 100%, 40%);">+                while (tc && tc->prev) tc = tc->prev;</span><br><span style="color: hsl(120, 100%, 40%);">+           if (tc) {</span><br><span style="color: hsl(120, 100%, 40%);">+                     tc = tc->parent;</span><br><span style="color: hsl(120, 100%, 40%);">+           }</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+     fflush(file);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  return 1 if ptr is a parent of context</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+static int _talloc_is_parent(const void *context, const void *ptr, int depth)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        struct talloc_chunk *tc;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    if (context == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+                return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   tc = talloc_chunk_from_ptr(context);</span><br><span style="color: hsl(120, 100%, 40%);">+  while (tc) {</span><br><span style="color: hsl(120, 100%, 40%);">+          if (depth <= 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+                  return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+             }</span><br><span style="color: hsl(120, 100%, 40%);">+             if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;</span><br><span style="color: hsl(120, 100%, 40%);">+           while (tc && tc->prev) tc = tc->prev;</span><br><span style="color: hsl(120, 100%, 40%);">+           if (tc) {</span><br><span style="color: hsl(120, 100%, 40%);">+                     tc = tc->parent;</span><br><span style="color: hsl(120, 100%, 40%);">+                   depth--;</span><br><span style="color: hsl(120, 100%, 40%);">+              }</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+     return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  return 1 if ptr is a parent of context</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ int talloc_is_parent(const void *context, const void *ptr)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+      return _talloc_is_parent(context, ptr, TALLOC_MAX_DEPTH);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  return the total size of memory used by this context and all children</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+static inline size_t _talloc_total_limit_size(const void *ptr,</span><br><span style="color: hsl(120, 100%, 40%);">+                                       struct talloc_memlimit *old_limit,</span><br><span style="color: hsl(120, 100%, 40%);">+                                    struct talloc_memlimit *new_limit)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ return _talloc_total_mem_internal(ptr, TOTAL_MEM_LIMIT,</span><br><span style="color: hsl(120, 100%, 40%);">+                                         old_limit, new_limit);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static inline bool talloc_memlimit_check(struct talloc_memlimit *limit, size_t size)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        struct talloc_memlimit *l;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  for (l = limit; l != NULL; l = l->upper) {</span><br><span style="color: hsl(120, 100%, 40%);">+         if (l->max_size != 0 &&</span><br><span style="color: hsl(120, 100%, 40%);">+                ((l->max_size <= l->cur_size) ||</span><br><span style="color: hsl(120, 100%, 40%);">+              (l->max_size - l->cur_size < size))) {</span><br><span style="color: hsl(120, 100%, 40%);">+                  return false;</span><br><span style="color: hsl(120, 100%, 40%);">+         }</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   return true;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  Update memory limits when freeing a talloc_chunk.</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+static void tc_memlimit_update_on_free(struct talloc_chunk *tc)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+    size_t limit_shrink_size;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (!tc->limit) {</span><br><span style="color: hsl(120, 100%, 40%);">+          return;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   /*</span><br><span style="color: hsl(120, 100%, 40%);">+     * Pool entries don't count. Only the pools</span><br><span style="color: hsl(120, 100%, 40%);">+        * themselves are counted as part of the memory</span><br><span style="color: hsl(120, 100%, 40%);">+        * limits. Note that this also takes care of</span><br><span style="color: hsl(120, 100%, 40%);">+   * nested pools which have both flags</span><br><span style="color: hsl(120, 100%, 40%);">+  * TALLOC_FLAG_POOLMEM|TALLOC_FLAG_POOL set.</span><br><span style="color: hsl(120, 100%, 40%);">+   */</span><br><span style="color: hsl(120, 100%, 40%);">+   if (tc->flags & TALLOC_FLAG_POOLMEM) {</span><br><span style="color: hsl(120, 100%, 40%);">+         return;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   /*</span><br><span style="color: hsl(120, 100%, 40%);">+     * If we are part of a memory limited context hierarchy</span><br><span style="color: hsl(120, 100%, 40%);">+        * we need to subtract the memory used from the counters</span><br><span style="color: hsl(120, 100%, 40%);">+       */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ limit_shrink_size = tc->size+TC_HDR_SIZE;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        /*</span><br><span style="color: hsl(120, 100%, 40%);">+     * If we're deallocating a pool, take into</span><br><span style="color: hsl(120, 100%, 40%);">+         * account the prefix size added for the pool.</span><br><span style="color: hsl(120, 100%, 40%);">+         */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (tc->flags & TALLOC_FLAG_POOL) {</span><br><span style="color: hsl(120, 100%, 40%);">+            limit_shrink_size += TP_HDR_SIZE;</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   talloc_memlimit_shrink(tc->limit, limit_shrink_size);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    if (tc->limit->parent == tc) {</span><br><span style="color: hsl(120, 100%, 40%);">+          free(tc->limit);</span><br><span style="color: hsl(120, 100%, 40%);">+   }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   tc->limit = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  Increase memory limit accounting after a malloc/realloc.</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+static void talloc_memlimit_grow(struct talloc_memlimit *limit,</span><br><span style="color: hsl(120, 100%, 40%);">+                                size_t size)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       struct talloc_memlimit *l;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  for (l = limit; l != NULL; l = l->upper) {</span><br><span style="color: hsl(120, 100%, 40%);">+         size_t new_cur_size = l->cur_size + size;</span><br><span style="color: hsl(120, 100%, 40%);">+          if (new_cur_size < l->cur_size) {</span><br><span style="color: hsl(120, 100%, 40%);">+                       talloc_abort("logic error in talloc_memlimit_grow\n");</span><br><span style="color: hsl(120, 100%, 40%);">+                      return;</span><br><span style="color: hsl(120, 100%, 40%);">+               }</span><br><span style="color: hsl(120, 100%, 40%);">+             l->cur_size = new_cur_size;</span><br><span style="color: hsl(120, 100%, 40%);">+        }</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  Decrease memory limit accounting after a free/realloc.</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+static void talloc_memlimit_shrink(struct talloc_memlimit *limit,</span><br><span style="color: hsl(120, 100%, 40%);">+                           size_t size)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       struct talloc_memlimit *l;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  for (l = limit; l != NULL; l = l->upper) {</span><br><span style="color: hsl(120, 100%, 40%);">+         if (l->cur_size < size) {</span><br><span style="color: hsl(120, 100%, 40%);">+                       talloc_abort("logic error in talloc_memlimit_shrink\n");</span><br><span style="color: hsl(120, 100%, 40%);">+                    return;</span><br><span style="color: hsl(120, 100%, 40%);">+               }</span><br><span style="color: hsl(120, 100%, 40%);">+             l->cur_size = l->cur_size - size;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+_PUBLIC_ int talloc_set_memlimit(const void *ctx, size_t max_size)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct talloc_chunk *tc = talloc_chunk_from_ptr(ctx);</span><br><span style="color: hsl(120, 100%, 40%);">+ struct talloc_memlimit *orig_limit;</span><br><span style="color: hsl(120, 100%, 40%);">+   struct talloc_memlimit *limit = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       if (tc->limit && tc->limit->parent == tc) {</span><br><span style="color: hsl(120, 100%, 40%);">+          tc->limit->max_size = max_size;</span><br><span style="color: hsl(120, 100%, 40%);">+         return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+     orig_limit = tc->limit;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  limit = malloc(sizeof(struct talloc_memlimit));</span><br><span style="color: hsl(120, 100%, 40%);">+       if (limit == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+          return 1;</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+     limit->parent = tc;</span><br><span style="color: hsl(120, 100%, 40%);">+        limit->max_size = max_size;</span><br><span style="color: hsl(120, 100%, 40%);">+        limit->cur_size = _talloc_total_limit_size(ctx, tc->limit, limit);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    if (orig_limit) {</span><br><span style="color: hsl(120, 100%, 40%);">+             limit->upper = orig_limit;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+              limit->upper = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span>diff --git a/sysmoOCTSIM/talloc.h b/sysmoOCTSIM/talloc.h</span><br><span>new file mode 100644</span><br><span>index 0000000..7372df1</span><br><span>--- /dev/null</span><br><span>+++ b/sysmoOCTSIM/talloc.h</span><br><span>@@ -0,0 +1,1934 @@</span><br><span style="color: hsl(120, 100%, 40%);">+#ifndef _TALLOC_H_</span><br><span style="color: hsl(120, 100%, 40%);">+#define _TALLOC_H_</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+   Unix SMB/CIFS implementation.</span><br><span style="color: hsl(120, 100%, 40%);">+   Samba temporary memory allocation functions</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   Copyright (C) Andrew Tridgell 2004-2005</span><br><span style="color: hsl(120, 100%, 40%);">+   Copyright (C) Stefan Metzmacher 2006</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     ** NOTE! The following LGPL license applies to the talloc</span><br><span style="color: hsl(120, 100%, 40%);">+     ** library. This does NOT imply that all of Samba is released</span><br><span style="color: hsl(120, 100%, 40%);">+     ** under the LGPL</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   This library is free software; you can redistribute it and/or</span><br><span style="color: hsl(120, 100%, 40%);">+   modify it under the terms of the GNU Lesser General Public</span><br><span style="color: hsl(120, 100%, 40%);">+   License as published by the Free Software Foundation; either</span><br><span style="color: hsl(120, 100%, 40%);">+   version 3 of the License, or (at your option) any later version.</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   This library is distributed in the hope that it will be useful,</span><br><span style="color: hsl(120, 100%, 40%);">+   but WITHOUT ANY WARRANTY; without even the implied warranty of</span><br><span style="color: hsl(120, 100%, 40%);">+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU</span><br><span style="color: hsl(120, 100%, 40%);">+   Lesser General Public License for more details.</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   You should have received a copy of the GNU Lesser General Public</span><br><span style="color: hsl(120, 100%, 40%);">+   License along with this library; if not, see <http://www.gnu.org/licenses/>.</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#include <stdlib.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <stdio.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <stdarg.h></span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef __cplusplus</span><br><span style="color: hsl(120, 100%, 40%);">+extern "C" {</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @defgroup talloc The talloc API</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc is a hierarchical, reference counted memory pool system with</span><br><span style="color: hsl(120, 100%, 40%);">+ * destructors. It is the core memory allocator used in Samba.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @{</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#define TALLOC_VERSION_MAJOR 2</span><br><span style="color: hsl(120, 100%, 40%);">+#define TALLOC_VERSION_MINOR 1</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+int talloc_version_major(void);</span><br><span style="color: hsl(120, 100%, 40%);">+int talloc_version_minor(void);</span><br><span style="color: hsl(120, 100%, 40%);">+/* This is mostly useful only for testing */</span><br><span style="color: hsl(120, 100%, 40%);">+int talloc_test_get_magic(void);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Define a talloc parent type</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * As talloc is a hierarchial memory allocator, every talloc chunk is a</span><br><span style="color: hsl(120, 100%, 40%);">+ * potential parent to other talloc chunks. So defining a separate type for a</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc chunk is not strictly necessary. TALLOC_CTX is defined nevertheless,</span><br><span style="color: hsl(120, 100%, 40%);">+ * as it provides an indicator for function arguments. You will frequently</span><br><span style="color: hsl(120, 100%, 40%);">+ * write code like</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      struct foo *foo_create(TALLOC_CTX *mem_ctx)</span><br><span style="color: hsl(120, 100%, 40%);">+ *      {</span><br><span style="color: hsl(120, 100%, 40%);">+ *              struct foo *result;</span><br><span style="color: hsl(120, 100%, 40%);">+ *              result = talloc(mem_ctx, struct foo);</span><br><span style="color: hsl(120, 100%, 40%);">+ *              if (result == NULL) return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ *                      ... initialize foo ...</span><br><span style="color: hsl(120, 100%, 40%);">+ *              return result;</span><br><span style="color: hsl(120, 100%, 40%);">+ *      }</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * In this type of allocating functions it is handy to have a general</span><br><span style="color: hsl(120, 100%, 40%);">+ * TALLOC_CTX type to indicate which parent to put allocated structures on.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+typedef void TALLOC_CTX;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+  this uses a little trick to allow __LINE__ to be stringified</span><br><span style="color: hsl(120, 100%, 40%);">+*/</span><br><span style="color: hsl(120, 100%, 40%);">+#ifndef __location__</span><br><span style="color: hsl(120, 100%, 40%);">+#define __TALLOC_STRING_LINE1__(s)    #s</span><br><span style="color: hsl(120, 100%, 40%);">+#define __TALLOC_STRING_LINE2__(s)   __TALLOC_STRING_LINE1__(s)</span><br><span style="color: hsl(120, 100%, 40%);">+#define __TALLOC_STRING_LINE3__  __TALLOC_STRING_LINE2__(__LINE__)</span><br><span style="color: hsl(120, 100%, 40%);">+#define __location__ __FILE__ ":" __TALLOC_STRING_LINE3__</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifndef TALLOC_DEPRECATED</span><br><span style="color: hsl(120, 100%, 40%);">+#define TALLOC_DEPRECATED 0</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifndef PRINTF_ATTRIBUTE</span><br><span style="color: hsl(120, 100%, 40%);">+#if (__GNUC__ >= 3)</span><br><span style="color: hsl(120, 100%, 40%);">+/** Use gcc attribute to check printf fns.  a1 is the 1-based index of</span><br><span style="color: hsl(120, 100%, 40%);">+ * the parameter containing the format, and a2 the index of the first</span><br><span style="color: hsl(120, 100%, 40%);">+ * argument. Note that some gcc 2.x versions don't handle this</span><br><span style="color: hsl(120, 100%, 40%);">+ * properly **/</span><br><span style="color: hsl(120, 100%, 40%);">+#define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+#define PRINTF_ATTRIBUTE(a1, a2)</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef DOXYGEN</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Create a new talloc context.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The talloc() macro is the core of the talloc library. It takes a memory</span><br><span style="color: hsl(120, 100%, 40%);">+ * context and a type, and returns a pointer to a new area of memory of the</span><br><span style="color: hsl(120, 100%, 40%);">+ * given type.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The returned pointer is itself a talloc context, so you can use it as the</span><br><span style="color: hsl(120, 100%, 40%);">+ * context argument to more calls to talloc if you wish.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The returned pointer is a "child" of the supplied context. This means that if</span><br><span style="color: hsl(120, 100%, 40%);">+ * you talloc_free() the context then the new child disappears as well.</span><br><span style="color: hsl(120, 100%, 40%);">+ * Alternatively you can free just the child.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ctx      A talloc context to create a new reference on or NULL to</span><br><span style="color: hsl(120, 100%, 40%);">+ *                      create a new top level context.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  type     The type of memory to allocate.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              A type casted talloc context or NULL on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      unsigned int *a, *b;</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ *      a = talloc(NULL, unsigned int);</span><br><span style="color: hsl(120, 100%, 40%);">+ *      b = talloc(a, unsigned int);</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_zero</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_array</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_steal</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_free</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void *talloc(const void *ctx, #type);</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type)</span><br><span style="color: hsl(120, 100%, 40%);">+void *_talloc(const void *context, size_t size);</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Create a new top level talloc context.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This function creates a zero length named talloc context as a top level</span><br><span style="color: hsl(120, 100%, 40%);">+ * context. It is equivalent to:</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      talloc_named(NULL, 0, fmt, ...);</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  fmt      Format string for the name.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ...      Additional printf-style arguments.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The allocated memory chunk, NULL on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_named()</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void *talloc_init(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef DOXYGEN</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Free a chunk of talloc memory.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The talloc_free() function frees a piece of talloc memory, and all its</span><br><span style="color: hsl(120, 100%, 40%);">+ * children. You can call talloc_free() on any pointer returned by</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc().</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The return value of talloc_free() indicates success or failure, with 0</span><br><span style="color: hsl(120, 100%, 40%);">+ * returned for success and -1 for failure. A possible failure condition</span><br><span style="color: hsl(120, 100%, 40%);">+ * is if the pointer had a destructor attached to it and the destructor</span><br><span style="color: hsl(120, 100%, 40%);">+ * returned -1. See talloc_set_destructor() for details on</span><br><span style="color: hsl(120, 100%, 40%);">+ * destructors. Likewise, if "ptr" is NULL, then the function will make</span><br><span style="color: hsl(120, 100%, 40%);">+ * no modifications and return -1.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * From version 2.0 and onwards, as a special case, talloc_free() is</span><br><span style="color: hsl(120, 100%, 40%);">+ * refused on pointers that have more than one parent associated, as talloc</span><br><span style="color: hsl(120, 100%, 40%);">+ * would have no way of knowing which parent should be removed. This is</span><br><span style="color: hsl(120, 100%, 40%);">+ * different from older versions in the sense that always the reference to</span><br><span style="color: hsl(120, 100%, 40%);">+ * the most recently established parent has been destroyed. Hence to free a</span><br><span style="color: hsl(120, 100%, 40%);">+ * pointer that has more than one parent please use talloc_unlink().</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * To help you find problems in your code caused by this behaviour, if</span><br><span style="color: hsl(120, 100%, 40%);">+ * you do try and free a pointer with more than one parent then the</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc logging function will be called to give output like this:</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *   ERROR: talloc_free with references at some_dir/source/foo.c:123</span><br><span style="color: hsl(120, 100%, 40%);">+ *     reference at some_dir/source/other.c:325</span><br><span style="color: hsl(120, 100%, 40%);">+ *     reference at some_dir/source/third.c:121</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Please see the documentation for talloc_set_log_fn() and</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc_set_log_stderr() for more information on talloc logging</span><br><span style="color: hsl(120, 100%, 40%);">+ * functions.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * If <code>TALLOC_FREE_FILL</code> environment variable is set,</span><br><span style="color: hsl(120, 100%, 40%);">+ * the memory occupied by the context is filled with the value of this variable.</span><br><span style="color: hsl(120, 100%, 40%);">+ * The value should be a numeric representation of the character you want to</span><br><span style="color: hsl(120, 100%, 40%);">+ * use.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc_free() operates recursively on its children.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ptr      The chunk to be freed.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              Returns 0 on success and -1 on error. A possible</span><br><span style="color: hsl(120, 100%, 40%);">+ *                      failure condition is if the pointer had a destructor</span><br><span style="color: hsl(120, 100%, 40%);">+ *                      attached to it and the destructor returned -1. Likewise,</span><br><span style="color: hsl(120, 100%, 40%);">+ *                      if "ptr" is NULL, then the function will make no</span><br><span style="color: hsl(120, 100%, 40%);">+ *                      modifications and returns -1.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Example:</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      unsigned int *a, *b;</span><br><span style="color: hsl(120, 100%, 40%);">+ *      a = talloc(NULL, unsigned int);</span><br><span style="color: hsl(120, 100%, 40%);">+ *      b = talloc(a, unsigned int);</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ *      talloc_free(a); // Frees a and b</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_set_destructor()</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_unlink()</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+int talloc_free(void *ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_free(ctx) _talloc_free(ctx, __location__)</span><br><span style="color: hsl(120, 100%, 40%);">+int _talloc_free(void *ptr, const char *location);</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Free a talloc chunk's children.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The function walks along the list of all children of a talloc context and</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc_free()s only the children, not the context itself.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * A NULL argument is handled as no-op.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ptr      The chunk that you want to free the children of</span><br><span style="color: hsl(120, 100%, 40%);">+ *                      (NULL is allowed too)</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void talloc_free_children(void *ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef DOXYGEN</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Assign a destructor function to be called when a chunk is freed.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The function talloc_set_destructor() sets the "destructor" for the pointer</span><br><span style="color: hsl(120, 100%, 40%);">+ * "ptr". A destructor is a function that is called when the memory used by a</span><br><span style="color: hsl(120, 100%, 40%);">+ * pointer is about to be released. The destructor receives the pointer as an</span><br><span style="color: hsl(120, 100%, 40%);">+ * argument, and should return 0 for success and -1 for failure.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The destructor can do anything it wants to, including freeing other pieces</span><br><span style="color: hsl(120, 100%, 40%);">+ * of memory. A common use for destructors is to clean up operating system</span><br><span style="color: hsl(120, 100%, 40%);">+ * resources (such as open file descriptors) contained in the structure the</span><br><span style="color: hsl(120, 100%, 40%);">+ * destructor is placed on.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * You can only place one destructor on a pointer. If you need more than one</span><br><span style="color: hsl(120, 100%, 40%);">+ * destructor then you can create a zero-length child of the pointer and place</span><br><span style="color: hsl(120, 100%, 40%);">+ * an additional destructor on that.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * To remove a destructor call talloc_set_destructor() with NULL for the</span><br><span style="color: hsl(120, 100%, 40%);">+ * destructor.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * If your destructor attempts to talloc_free() the pointer that it is the</span><br><span style="color: hsl(120, 100%, 40%);">+ * destructor for then talloc_free() will return -1 and the free will be</span><br><span style="color: hsl(120, 100%, 40%);">+ * ignored. This would be a pointless operation anyway, as the destructor is</span><br><span style="color: hsl(120, 100%, 40%);">+ * only called when the memory is just about to go away.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ptr      The talloc chunk to add a destructor to.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  destructor  The destructor function to be called. NULL to remove</span><br><span style="color: hsl(120, 100%, 40%);">+ *                         it.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Example:</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      static int destroy_fd(int *fd) {</span><br><span style="color: hsl(120, 100%, 40%);">+ *              close(*fd);</span><br><span style="color: hsl(120, 100%, 40%);">+ *              return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ *      }</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ *      int *open_file(const char *filename) {</span><br><span style="color: hsl(120, 100%, 40%);">+ *              int *fd = talloc(NULL, int);</span><br><span style="color: hsl(120, 100%, 40%);">+ *              *fd = open(filename, O_RDONLY);</span><br><span style="color: hsl(120, 100%, 40%);">+ *              if (*fd < 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ *                      talloc_free(fd);</span><br><span style="color: hsl(120, 100%, 40%);">+ *                      return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ *              }</span><br><span style="color: hsl(120, 100%, 40%);">+ *              // Whenever they free this, we close the file.</span><br><span style="color: hsl(120, 100%, 40%);">+ *              talloc_set_destructor(fd, destroy_fd);</span><br><span style="color: hsl(120, 100%, 40%);">+ *              return fd;</span><br><span style="color: hsl(120, 100%, 40%);">+ *      }</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc()</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_free()</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void talloc_set_destructor(const void *ptr, int (*destructor)(void *));</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Change a talloc chunk's parent.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The talloc_steal() function changes the parent context of a talloc</span><br><span style="color: hsl(120, 100%, 40%);">+ * pointer. It is typically used when the context that the pointer is</span><br><span style="color: hsl(120, 100%, 40%);">+ * currently a child of is going to be freed and you wish to keep the</span><br><span style="color: hsl(120, 100%, 40%);">+ * memory for a longer time.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * To make the changed hierarchy less error-prone, you might consider to use</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc_move().</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * If you try and call talloc_steal() on a pointer that has more than one</span><br><span style="color: hsl(120, 100%, 40%);">+ * parent then the result is ambiguous. Talloc will choose to remove the</span><br><span style="color: hsl(120, 100%, 40%);">+ * parent that is currently indicated by talloc_parent() and replace it with</span><br><span style="color: hsl(120, 100%, 40%);">+ * the chosen parent. You will also get a message like this via the talloc</span><br><span style="color: hsl(120, 100%, 40%);">+ * logging functions:</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *   WARNING: talloc_steal with references at some_dir/source/foo.c:123</span><br><span style="color: hsl(120, 100%, 40%);">+ *     reference at some_dir/source/other.c:325</span><br><span style="color: hsl(120, 100%, 40%);">+ *     reference at some_dir/source/third.c:121</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * To unambiguously change the parent of a pointer please see the function</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc_reparent(). See the talloc_set_log_fn() documentation for more</span><br><span style="color: hsl(120, 100%, 40%);">+ * information on talloc logging.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  new_ctx  The new parent context.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ptr      The talloc chunk to move.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              Returns the pointer that you pass it. It does not have</span><br><span style="color: hsl(120, 100%, 40%);">+ *                      any failure modes.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @note It is possible to produce loops in the parent/child relationship</span><br><span style="color: hsl(120, 100%, 40%);">+ * if you are not careful with talloc_steal(). No guarantees are provided</span><br><span style="color: hsl(120, 100%, 40%);">+ * as to your sanity or the safety of your data if you do this.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void *talloc_steal(const void *new_ctx, const void *ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+#else /* DOXYGEN */</span><br><span style="color: hsl(120, 100%, 40%);">+/* try to make talloc_set_destructor() and talloc_steal() type safe,</span><br><span style="color: hsl(120, 100%, 40%);">+   if we have a recent gcc */</span><br><span style="color: hsl(120, 100%, 40%);">+#if (__GNUC__ >= 3)</span><br><span style="color: hsl(120, 100%, 40%);">+#define _TALLOC_TYPEOF(ptr) __typeof__(ptr)</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_set_destructor(ptr, function)                                \</span><br><span style="color: hsl(120, 100%, 40%);">+       do {                                                                  \</span><br><span style="color: hsl(120, 100%, 40%);">+               int (*_talloc_destructor_fn)(_TALLOC_TYPEOF(ptr)) = (function);       \</span><br><span style="color: hsl(120, 100%, 40%);">+               _talloc_set_destructor((ptr), (int (*)(void *))_talloc_destructor_fn); \</span><br><span style="color: hsl(120, 100%, 40%);">+      } while(0)</span><br><span style="color: hsl(120, 100%, 40%);">+/* this extremely strange macro is to avoid some braindamaged warning</span><br><span style="color: hsl(120, 100%, 40%);">+   stupidity in gcc 4.1.x */</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_steal(ctx, ptr) ({ _TALLOC_TYPEOF(ptr) __talloc_steal_ret = (_TALLOC_TYPEOF(ptr))_talloc_steal_loc((ctx),(ptr), __location__); __talloc_steal_ret; })</span><br><span style="color: hsl(120, 100%, 40%);">+#else /* __GNUC__ >= 3 */</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_set_destructor(ptr, function) \</span><br><span style="color: hsl(120, 100%, 40%);">+   _talloc_set_destructor((ptr), (int (*)(void *))(function))</span><br><span style="color: hsl(120, 100%, 40%);">+#define _TALLOC_TYPEOF(ptr) void *</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_steal(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_steal_loc((ctx),(ptr), __location__)</span><br><span style="color: hsl(120, 100%, 40%);">+#endif /* __GNUC__ >= 3 */</span><br><span style="color: hsl(120, 100%, 40%);">+void _talloc_set_destructor(const void *ptr, int (*_destructor)(void *));</span><br><span style="color: hsl(120, 100%, 40%);">+void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location);</span><br><span style="color: hsl(120, 100%, 40%);">+#endif /* DOXYGEN */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Assign a name to a talloc chunk.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Each talloc pointer has a "name". The name is used principally for</span><br><span style="color: hsl(120, 100%, 40%);">+ * debugging purposes, although it is also possible to set and get the name on</span><br><span style="color: hsl(120, 100%, 40%);">+ * a pointer in as a way of "marking" pointers in your code.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The main use for names on pointer is for "talloc reports". See</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc_report() and talloc_report_full() for details. Also see</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc_enable_leak_report() and talloc_enable_leak_report_full().</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The talloc_set_name() function allocates memory as a child of the</span><br><span style="color: hsl(120, 100%, 40%);">+ * pointer. It is logically equivalent to:</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      talloc_set_name_const(ptr, talloc_asprintf(ptr, fmt, ...));</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ptr      The talloc chunk to assign a name to.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  fmt      Format string for the name.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ...      Add printf-style additional arguments.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The assigned name, NULL on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @note Multiple calls to talloc_set_name() will allocate more memory without</span><br><span style="color: hsl(120, 100%, 40%);">+ * releasing the name. All of the memory is released when the ptr is freed</span><br><span style="color: hsl(120, 100%, 40%);">+ * using talloc_free().</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+const char *talloc_set_name(const void *ptr, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef DOXYGEN</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Change a talloc chunk's parent.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This function has the same effect as talloc_steal(), and additionally sets</span><br><span style="color: hsl(120, 100%, 40%);">+ * the source pointer to NULL. You would use it like this:</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      struct foo *X = talloc(tmp_ctx, struct foo);</span><br><span style="color: hsl(120, 100%, 40%);">+ *      struct foo *Y;</span><br><span style="color: hsl(120, 100%, 40%);">+ *      Y = talloc_move(new_ctx, &X);</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  new_ctx  The new parent context.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  pptr     Pointer to a pointer to the talloc chunk to move.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The pointer to the talloc chunk that moved.</span><br><span style="color: hsl(120, 100%, 40%);">+ *                      It does not have any failure modes.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void *talloc_move(const void *new_ctx, void **pptr);</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_move(ctx, pptr) (_TALLOC_TYPEOF(*(pptr)))_talloc_move((ctx),(void *)(pptr))</span><br><span style="color: hsl(120, 100%, 40%);">+void *_talloc_move(const void *new_ctx, const void *pptr);</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Assign a name to a talloc chunk.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The function is just like talloc_set_name(), but it takes a string constant,</span><br><span style="color: hsl(120, 100%, 40%);">+ * and is much faster. It is extensively used by the "auto naming" macros, such</span><br><span style="color: hsl(120, 100%, 40%);">+ * as talloc_p().</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This function does not allocate any memory. It just copies the supplied</span><br><span style="color: hsl(120, 100%, 40%);">+ * pointer into the internal representation of the talloc ptr. This means you</span><br><span style="color: hsl(120, 100%, 40%);">+ * must not pass a name pointer to memory that will disappear before the ptr</span><br><span style="color: hsl(120, 100%, 40%);">+ * is freed with talloc_free().</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ptr      The talloc chunk to assign a name to.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  name     Format string for the name.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void talloc_set_name_const(const void *ptr, const char *name);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Create a named talloc chunk.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The talloc_named() function creates a named talloc pointer. It is</span><br><span style="color: hsl(120, 100%, 40%);">+ * equivalent to:</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      ptr = talloc_size(context, size);</span><br><span style="color: hsl(120, 100%, 40%);">+ *      talloc_set_name(ptr, fmt, ....);</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  context  The talloc context to hang the result off.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  size     Number of char's that you want to allocate.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  fmt      Format string for the name.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ...      Additional printf-style arguments.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The allocated memory chunk, NULL on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_set_name()</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void *talloc_named(const void *context, size_t size,</span><br><span style="color: hsl(120, 100%, 40%);">+                 const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Basic routine to allocate a chunk of memory.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This is equivalent to:</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      ptr = talloc_size(context, size);</span><br><span style="color: hsl(120, 100%, 40%);">+ *      talloc_set_name_const(ptr, name);</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  context  The parent context.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  size     The number of char's that we want to allocate.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  name     The name the talloc block has.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return             The allocated memory chunk, NULL on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void *talloc_named_const(const void *context, size_t size, const char *name);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef DOXYGEN</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Untyped allocation.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The function should be used when you don't have a convenient type to pass to</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc(). Unlike talloc(), it is not type safe (as it returns a void *), so</span><br><span style="color: hsl(120, 100%, 40%);">+ * you are on your own for type checking.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Best to use talloc() or talloc_array() instead.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ctx     The talloc context to hang the result off.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  size    Number of char's that you want to allocate.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return             The allocated memory chunk, NULL on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Example:</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      void *mem = talloc_size(NULL, 100);</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void *talloc_size(const void *ctx, size_t size);</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_size(ctx, size) talloc_named_const(ctx, size, __location__)</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef DOXYGEN</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Allocate into a typed pointer.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The talloc_ptrtype() macro should be used when you have a pointer and want</span><br><span style="color: hsl(120, 100%, 40%);">+ * to allocate memory to point at with this pointer. When compiling with</span><br><span style="color: hsl(120, 100%, 40%);">+ * gcc >= 3 it is typesafe. Note this is a wrapper of talloc_size() and</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc_get_name() will return the current location in the source file and</span><br><span style="color: hsl(120, 100%, 40%);">+ * not the type.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ctx      The talloc context to hang the result off.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  type     The pointer you want to assign the result to.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The properly casted allocated memory chunk, NULL on</span><br><span style="color: hsl(120, 100%, 40%);">+ *                      error.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Example:</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *       unsigned int *a = talloc_ptrtype(NULL, a);</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void *talloc_ptrtype(const void *ctx, #type);</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_ptrtype(ctx, ptr) (_TALLOC_TYPEOF(ptr))talloc_size(ctx, sizeof(*(ptr)))</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef DOXYGEN</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Allocate a new 0-sized talloc chunk.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This is a utility macro that creates a new memory context hanging off an</span><br><span style="color: hsl(120, 100%, 40%);">+ * existing context, automatically naming it "talloc_new: __location__" where</span><br><span style="color: hsl(120, 100%, 40%);">+ * __location__ is the source line it is called from. It is particularly</span><br><span style="color: hsl(120, 100%, 40%);">+ * useful for creating a new temporary working context.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ctx      The talloc parent context.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              A new talloc chunk, NULL on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void *talloc_new(const void *ctx);</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_new(ctx) talloc_named_const(ctx, 0, "talloc_new: " __location__)</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef DOXYGEN</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Allocate a 0-initizialized structure.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The macro is equivalent to:</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      ptr = talloc(ctx, type);</span><br><span style="color: hsl(120, 100%, 40%);">+ *      if (ptr) memset(ptr, 0, sizeof(type));</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ctx      The talloc context to hang the result off.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  type     The type that we want to allocate.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              Pointer to a piece of memory, properly cast to 'type *',</span><br><span style="color: hsl(120, 100%, 40%);">+ *                      NULL on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Example:</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      unsigned int *a, *b;</span><br><span style="color: hsl(120, 100%, 40%);">+ *      a = talloc_zero(NULL, unsigned int);</span><br><span style="color: hsl(120, 100%, 40%);">+ *      b = talloc_zero(a, unsigned int);</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc()</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_zero_size()</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_zero_array()</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void *talloc_zero(const void *ctx, #type);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Allocate untyped, 0-initialized memory.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ctx      The talloc context to hang the result off.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  size     Number of char's that you want to allocate.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The allocated memory chunk.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void *talloc_zero_size(const void *ctx, size_t size);</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_zero(ctx, type) (type *)_talloc_zero(ctx, sizeof(type), #type)</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_zero_size(ctx, size) _talloc_zero(ctx, size, __location__)</span><br><span style="color: hsl(120, 100%, 40%);">+void *_talloc_zero(const void *ctx, size_t size, const char *name);</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Return the name of a talloc chunk.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ptr      The talloc chunk.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The current name for the given talloc pointer.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_set_name()</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+const char *talloc_get_name(const void *ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Verify that a talloc chunk carries a specified name.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This function checks if a pointer has the specified name. If it does</span><br><span style="color: hsl(120, 100%, 40%);">+ * then the pointer is returned.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ptr       The talloc chunk to check.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  name      The name to check against.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return               The pointer if the name matches, NULL if it doesn't.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void *talloc_check_name(const void *ptr, const char *name);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Get the parent chunk of a pointer.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ptr      The talloc pointer to inspect.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The talloc parent of ptr, NULL on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void *talloc_parent(const void *ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Get a talloc chunk's parent name.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ptr      The talloc pointer to inspect.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The name of ptr's parent chunk.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+const char *talloc_parent_name(const void *ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Get the total size of a talloc chunk including its children.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The function returns the total size in bytes used by this pointer and all</span><br><span style="color: hsl(120, 100%, 40%);">+ * child pointers. Mostly useful for debugging.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Passing NULL is allowed, but it will only give a meaningful result if</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc_enable_leak_report() or talloc_enable_leak_report_full() has</span><br><span style="color: hsl(120, 100%, 40%);">+ * been called.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ptr      The talloc chunk.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The total size.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+size_t talloc_total_size(const void *ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Get the number of talloc chunks hanging off a chunk.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The talloc_total_blocks() function returns the total memory block</span><br><span style="color: hsl(120, 100%, 40%);">+ * count used by this pointer and all child pointers. Mostly useful for</span><br><span style="color: hsl(120, 100%, 40%);">+ * debugging.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Passing NULL is allowed, but it will only give a meaningful result if</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc_enable_leak_report() or talloc_enable_leak_report_full() has</span><br><span style="color: hsl(120, 100%, 40%);">+ * been called.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ptr      The talloc chunk.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The total size.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+size_t talloc_total_blocks(const void *ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef DOXYGEN</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Duplicate a memory area into a talloc chunk.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The function is equivalent to:</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      ptr = talloc_size(ctx, size);</span><br><span style="color: hsl(120, 100%, 40%);">+ *      if (ptr) memcpy(ptr, p, size);</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  t        The talloc context to hang the result off.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  p        The memory chunk you want to duplicate.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  size     Number of char's that you want copy.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The allocated memory chunk.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_size()</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void *talloc_memdup(const void *t, const void *p, size_t size);</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_memdup(t, p, size) _talloc_memdup(t, p, size, __location__)</span><br><span style="color: hsl(120, 100%, 40%);">+void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name);</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef DOXYGEN</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Assign a type to a talloc chunk.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This macro allows you to force the name of a pointer to be of a particular</span><br><span style="color: hsl(120, 100%, 40%);">+ * type. This can be used in conjunction with talloc_get_type() to do type</span><br><span style="color: hsl(120, 100%, 40%);">+ * checking on void* pointers.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * It is equivalent to this:</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      talloc_set_name_const(ptr, #type)</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ptr      The talloc chunk to assign the type to.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  type     The type to assign.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void talloc_set_type(const char *ptr, #type);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Get a typed pointer out of a talloc pointer.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This macro allows you to do type checking on talloc pointers. It is</span><br><span style="color: hsl(120, 100%, 40%);">+ * particularly useful for void* private pointers. It is equivalent to</span><br><span style="color: hsl(120, 100%, 40%);">+ * this:</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      (type *)talloc_check_name(ptr, #type)</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ptr      The talloc pointer to check.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  type     The type to check against.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The properly casted pointer given by ptr, NULL on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+type *talloc_get_type(const void *ptr, #type);</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_set_type(ptr, type) talloc_set_name_const(ptr, #type)</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_get_type(ptr, type) (type *)talloc_check_name(ptr, #type)</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef DOXYGEN</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Safely turn a void pointer into a typed pointer.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This macro is used together with talloc(mem_ctx, struct foo). If you had to</span><br><span style="color: hsl(120, 100%, 40%);">+ * assign the talloc chunk pointer to some void pointer variable,</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc_get_type_abort() is the recommended way to get the convert the void</span><br><span style="color: hsl(120, 100%, 40%);">+ * pointer back to a typed pointer.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ptr      The void pointer to convert.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  type     The type that this chunk contains</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The same value as ptr, type-checked and properly cast.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void *talloc_get_type_abort(const void *ptr, #type);</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef TALLOC_GET_TYPE_ABORT_NOOP</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_get_type_abort(ptr, type) (type *)(ptr)</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_get_type_abort(ptr, type) (type *)_talloc_get_type_abort(ptr, #type, __location__)</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location);</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Find a parent context by name.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Find a parent memory context of the current context that has the given</span><br><span style="color: hsl(120, 100%, 40%);">+ * name. This can be very useful in complex programs where it may be</span><br><span style="color: hsl(120, 100%, 40%);">+ * difficult to pass all information down to the level you need, but you</span><br><span style="color: hsl(120, 100%, 40%);">+ * know the structure you want is a parent of another context.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ctx      The talloc chunk to start from.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  name     The name of the parent we look for.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The memory context we are looking for, NULL if not</span><br><span style="color: hsl(120, 100%, 40%);">+ *                      found.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void *talloc_find_parent_byname(const void *ctx, const char *name);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef DOXYGEN</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Find a parent context by type.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Find a parent memory context of the current context that has the given</span><br><span style="color: hsl(120, 100%, 40%);">+ * name. This can be very useful in complex programs where it may be</span><br><span style="color: hsl(120, 100%, 40%);">+ * difficult to pass all information down to the level you need, but you</span><br><span style="color: hsl(120, 100%, 40%);">+ * know the structure you want is a parent of another context.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Like talloc_find_parent_byname() but takes a type, making it typesafe.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ptr      The talloc chunk to start from.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  type     The type of the parent to look for.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The memory context we are looking for, NULL if not</span><br><span style="color: hsl(120, 100%, 40%);">+ *                      found.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void *talloc_find_parent_bytype(const void *ptr, #type);</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_find_parent_bytype(ptr, type) (type *)talloc_find_parent_byname(ptr, #type)</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Allocate a talloc pool.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * A talloc pool is a pure optimization for specific situations. In the</span><br><span style="color: hsl(120, 100%, 40%);">+ * release process for Samba 3.2 we found out that we had become considerably</span><br><span style="color: hsl(120, 100%, 40%);">+ * slower than Samba 3.0 was. Profiling showed that malloc(3) was a large CPU</span><br><span style="color: hsl(120, 100%, 40%);">+ * consumer in benchmarks. For Samba 3.2 we have internally converted many</span><br><span style="color: hsl(120, 100%, 40%);">+ * static buffers to dynamically allocated ones, so malloc(3) being beaten</span><br><span style="color: hsl(120, 100%, 40%);">+ * more was no surprise. But it made us slower.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc_pool() is an optimization to call malloc(3) a lot less for the use</span><br><span style="color: hsl(120, 100%, 40%);">+ * pattern Samba has: The SMB protocol is mainly a request/response protocol</span><br><span style="color: hsl(120, 100%, 40%);">+ * where we have to allocate a certain amount of memory per request and free</span><br><span style="color: hsl(120, 100%, 40%);">+ * that after the SMB reply is sent to the client.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc_pool() creates a talloc chunk that you can use as a talloc parent</span><br><span style="color: hsl(120, 100%, 40%);">+ * exactly as you would use any other ::TALLOC_CTX. The difference is that</span><br><span style="color: hsl(120, 100%, 40%);">+ * when you talloc a child of this pool, no malloc(3) is done. Instead, talloc</span><br><span style="color: hsl(120, 100%, 40%);">+ * just increments a pointer inside the talloc_pool. This also works</span><br><span style="color: hsl(120, 100%, 40%);">+ * recursively. If you use the child of the talloc pool as a parent for</span><br><span style="color: hsl(120, 100%, 40%);">+ * grand-children, their memory is also taken from the talloc pool.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * If there is not enough memory in the pool to allocate the new child,</span><br><span style="color: hsl(120, 100%, 40%);">+ * it will create a new talloc chunk as if the parent was a normal talloc</span><br><span style="color: hsl(120, 100%, 40%);">+ * context.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * If you talloc_free() children of a talloc pool, the memory is not given</span><br><span style="color: hsl(120, 100%, 40%);">+ * back to the system. Instead, free(3) is only called if the talloc_pool()</span><br><span style="color: hsl(120, 100%, 40%);">+ * itself is released with talloc_free().</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The downside of a talloc pool is that if you talloc_move() a child of a</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc pool to a talloc parent outside the pool, the whole pool memory is</span><br><span style="color: hsl(120, 100%, 40%);">+ * not free(3)'ed until that moved chunk is also talloc_free()ed.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  context  The talloc context to hang the result off.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  size     Size of the talloc pool.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The allocated talloc pool, NULL on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void *talloc_pool(const void *context, size_t size);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef DOXYGEN</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Allocate a talloc object as/with an additional pool.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This is like talloc_pool(), but's it's more flexible</span><br><span style="color: hsl(120, 100%, 40%);">+ * and allows an object to be a pool for its children.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in] ctx                   The talloc context to hang the result off.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in] type                  The type that we want to allocate.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in] num_subobjects        The expected number of subobjects, which will</span><br><span style="color: hsl(120, 100%, 40%);">+ *                                  be allocated within the pool. This allocates</span><br><span style="color: hsl(120, 100%, 40%);">+ *                                  space for talloc_chunk headers.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in] total_subobjects_size The size that all subobjects can use in total.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The allocated talloc object, NULL on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void *talloc_pooled_object(const void *ctx, #type,</span><br><span style="color: hsl(120, 100%, 40%);">+                    unsigned num_subobjects,</span><br><span style="color: hsl(120, 100%, 40%);">+                      size_t total_subobjects_size);</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_pooled_object(_ctx, _type, \</span><br><span style="color: hsl(120, 100%, 40%);">+                      _num_subobjects, \</span><br><span style="color: hsl(120, 100%, 40%);">+                            _total_subobjects_size) \</span><br><span style="color: hsl(120, 100%, 40%);">+        (_type *)_talloc_pooled_object((_ctx), sizeof(_type), #_type, \</span><br><span style="color: hsl(120, 100%, 40%);">+                                       (_num_subobjects), \</span><br><span style="color: hsl(120, 100%, 40%);">+                                  (_total_subobjects_size))</span><br><span style="color: hsl(120, 100%, 40%);">+void *_talloc_pooled_object(const void *ctx,</span><br><span style="color: hsl(120, 100%, 40%);">+                           size_t type_size,</span><br><span style="color: hsl(120, 100%, 40%);">+                     const char *type_name,</span><br><span style="color: hsl(120, 100%, 40%);">+                        unsigned num_subobjects,</span><br><span style="color: hsl(120, 100%, 40%);">+                      size_t total_subobjects_size);</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Free a talloc chunk and NULL out the pointer.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * TALLOC_FREE() frees a pointer and sets it to NULL. Use this if you want</span><br><span style="color: hsl(120, 100%, 40%);">+ * immediate feedback (i.e. crash) if you use a pointer after having free'ed</span><br><span style="color: hsl(120, 100%, 40%);">+ * it.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ctx      The chunk to be freed.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+#define TALLOC_FREE(ctx) do { if (ctx != NULL) { talloc_free(ctx); ctx=NULL; } } while(0)</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* @} ******************************************************************/</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * \defgroup talloc_ref The talloc reference function.</span><br><span style="color: hsl(120, 100%, 40%);">+ * @ingroup talloc</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This module contains the definitions around talloc references</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @{</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Increase the reference count of a talloc chunk.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The talloc_increase_ref_count(ptr) function is exactly equivalent to:</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      talloc_reference(NULL, ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * You can use either syntax, depending on which you think is clearer in</span><br><span style="color: hsl(120, 100%, 40%);">+ * your code.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ptr      The pointer to increase the reference count.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              0 on success, -1 on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+int talloc_increase_ref_count(const void *ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Get the number of references to a talloc chunk.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ptr      The pointer to retrieve the reference count from.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The number of references.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+size_t talloc_reference_count(const void *ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef DOXYGEN</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Create an additional talloc parent to a pointer.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The talloc_reference() function makes "context" an additional parent of</span><br><span style="color: hsl(120, 100%, 40%);">+ * ptr. Each additional reference consumes around 48 bytes of memory on intel</span><br><span style="color: hsl(120, 100%, 40%);">+ * x86 platforms.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * If ptr is NULL, then the function is a no-op, and simply returns NULL.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * After creating a reference you can free it in one of the following ways:</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * - you can talloc_free() any parent of the original pointer. That</span><br><span style="color: hsl(120, 100%, 40%);">+ *   will reduce the number of parents of this pointer by 1, and will</span><br><span style="color: hsl(120, 100%, 40%);">+ *   cause this pointer to be freed if it runs out of parents.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * - you can talloc_free() the pointer itself if it has at maximum one</span><br><span style="color: hsl(120, 100%, 40%);">+ *   parent. This behaviour has been changed since the release of version</span><br><span style="color: hsl(120, 100%, 40%);">+ *   2.0. Further informations in the description of "talloc_free".</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * For more control on which parent to remove, see talloc_unlink()</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ctx      The additional parent.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ptr      The pointer you want to create an additional parent for.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The original pointer 'ptr', NULL if talloc ran out of</span><br><span style="color: hsl(120, 100%, 40%);">+ *                      memory in creating the reference.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @warning You should try to avoid using this interface. It turns a beautiful</span><br><span style="color: hsl(120, 100%, 40%);">+ *          talloc-tree into a graph. It is often really hard to debug if you</span><br><span style="color: hsl(120, 100%, 40%);">+ *          screw something up by accident.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Example:</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      unsigned int *a, *b, *c;</span><br><span style="color: hsl(120, 100%, 40%);">+ *      a = talloc(NULL, unsigned int);</span><br><span style="color: hsl(120, 100%, 40%);">+ *      b = talloc(NULL, unsigned int);</span><br><span style="color: hsl(120, 100%, 40%);">+ *      c = talloc(a, unsigned int);</span><br><span style="color: hsl(120, 100%, 40%);">+ *      // b also serves as a parent of c.</span><br><span style="color: hsl(120, 100%, 40%);">+ *      talloc_reference(b, c);</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_unlink()</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void *talloc_reference(const void *ctx, const void *ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_reference_loc((ctx),(ptr), __location__)</span><br><span style="color: hsl(120, 100%, 40%);">+void *_talloc_reference_loc(const void *context, const void *ptr, const char *location);</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Remove a specific parent from a talloc chunk.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The function removes a specific parent from ptr. The context passed must</span><br><span style="color: hsl(120, 100%, 40%);">+ * either be a context used in talloc_reference() with this pointer, or must be</span><br><span style="color: hsl(120, 100%, 40%);">+ * a direct parent of ptr.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * You can just use talloc_free() instead of talloc_unlink() if there</span><br><span style="color: hsl(120, 100%, 40%);">+ * is at maximum one parent. This behaviour has been changed since the</span><br><span style="color: hsl(120, 100%, 40%);">+ * release of version 2.0. Further informations in the description of</span><br><span style="color: hsl(120, 100%, 40%);">+ * "talloc_free".</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  context  The talloc parent to remove.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ptr      The talloc ptr you want to remove the parent from.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              0 on success, -1 on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @note If the parent has already been removed using talloc_free() then</span><br><span style="color: hsl(120, 100%, 40%);">+ * this function will fail and will return -1.  Likewise, if ptr is NULL,</span><br><span style="color: hsl(120, 100%, 40%);">+ * then the function will make no modifications and return -1.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @warning You should try to avoid using this interface. It turns a beautiful</span><br><span style="color: hsl(120, 100%, 40%);">+ *          talloc-tree into a graph. It is often really hard to debug if you</span><br><span style="color: hsl(120, 100%, 40%);">+ *          screw something up by accident.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Example:</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      unsigned int *a, *b, *c;</span><br><span style="color: hsl(120, 100%, 40%);">+ *      a = talloc(NULL, unsigned int);</span><br><span style="color: hsl(120, 100%, 40%);">+ *      b = talloc(NULL, unsigned int);</span><br><span style="color: hsl(120, 100%, 40%);">+ *      c = talloc(a, unsigned int);</span><br><span style="color: hsl(120, 100%, 40%);">+ *      // b also serves as a parent of c.</span><br><span style="color: hsl(120, 100%, 40%);">+ *      talloc_reference(b, c);</span><br><span style="color: hsl(120, 100%, 40%);">+ *      talloc_unlink(b, c);</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+int talloc_unlink(const void *context, void *ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Provide a talloc context that is freed at program exit.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This is a handy utility function that returns a talloc context</span><br><span style="color: hsl(120, 100%, 40%);">+ * which will be automatically freed on program exit. This can be used</span><br><span style="color: hsl(120, 100%, 40%);">+ * to reduce the noise in memory leak reports.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Never use this in code that might be used in objects loaded with</span><br><span style="color: hsl(120, 100%, 40%);">+ * dlopen and unloaded with dlclose. talloc_autofree_context()</span><br><span style="color: hsl(120, 100%, 40%);">+ * internally uses atexit(3). Some platforms like modern Linux handles</span><br><span style="color: hsl(120, 100%, 40%);">+ * this fine, but for example FreeBSD does not deal well with dlopen()</span><br><span style="color: hsl(120, 100%, 40%);">+ * and atexit() used simultaneously: dlclose() does not clean up the</span><br><span style="color: hsl(120, 100%, 40%);">+ * list of atexit-handlers, so when the program exits the code that</span><br><span style="color: hsl(120, 100%, 40%);">+ * was registered from within talloc_autofree_context() is gone, the</span><br><span style="color: hsl(120, 100%, 40%);">+ * program crashes at exit.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              A talloc context, NULL on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void *talloc_autofree_context(void);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Get the size of a talloc chunk.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This function lets you know the amount of memory allocated so far by</span><br><span style="color: hsl(120, 100%, 40%);">+ * this context. It does NOT account for subcontext memory.</span><br><span style="color: hsl(120, 100%, 40%);">+ * This can be used to calculate the size of an array.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ctx      The talloc chunk.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The size of the talloc chunk.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+size_t talloc_get_size(const void *ctx);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Show the parentage of a context.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  context            The talloc context to look at.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  file               The output to use, a file, stdout or stderr.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void talloc_show_parents(const void *context, FILE *file);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Check if a context is parent of a talloc chunk.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This checks if context is referenced in the talloc hierarchy above ptr.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  context  The assumed talloc context.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ptr      The talloc chunk to check.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              Return 1 if this is the case, 0 if not.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+int talloc_is_parent(const void *context, const void *ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Change the parent context of a talloc pointer.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The function changes the parent context of a talloc pointer. It is typically</span><br><span style="color: hsl(120, 100%, 40%);">+ * used when the context that the pointer is currently a child of is going to be</span><br><span style="color: hsl(120, 100%, 40%);">+ * freed and you wish to keep the memory for a longer time.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The difference between talloc_reparent() and talloc_steal() is that</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc_reparent() can specify which parent you wish to change. This is</span><br><span style="color: hsl(120, 100%, 40%);">+ * useful when a pointer has multiple parents via references.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  old_parent</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  new_parent</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ptr</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              Return the pointer you passed. It does not have any</span><br><span style="color: hsl(120, 100%, 40%);">+ *                      failure modes.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* @} ******************************************************************/</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @defgroup talloc_array The talloc array functions</span><br><span style="color: hsl(120, 100%, 40%);">+ * @ingroup talloc</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Talloc contains some handy helpers for handling Arrays conveniently</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @{</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef DOXYGEN</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Allocate an array.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The macro is equivalent to:</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      (type *)talloc_size(ctx, sizeof(type) * count);</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * except that it provides integer overflow protection for the multiply,</span><br><span style="color: hsl(120, 100%, 40%);">+ * returning NULL if the multiply overflows.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ctx      The talloc context to hang the result off.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  type     The type that we want to allocate.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  count    The number of 'type' elements you want to allocate.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The allocated result, properly cast to 'type *', NULL on</span><br><span style="color: hsl(120, 100%, 40%);">+ *                      error.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Example:</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      unsigned int *a, *b;</span><br><span style="color: hsl(120, 100%, 40%);">+ *      a = talloc_zero(NULL, unsigned int);</span><br><span style="color: hsl(120, 100%, 40%);">+ *      b = talloc_array(a, unsigned int, 100);</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc()</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_zero_array()</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void *talloc_array(const void *ctx, #type, unsigned count);</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_array(ctx, type, count) (type *)_talloc_array(ctx, sizeof(type), count, #type)</span><br><span style="color: hsl(120, 100%, 40%);">+void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name);</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef DOXYGEN</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Allocate an array.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ctx      The talloc context to hang the result off.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  size     The size of an array element.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  count    The number of elements you want to allocate.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The allocated result, NULL on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void *talloc_array_size(const void *ctx, size_t size, unsigned count);</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_array_size(ctx, size, count) _talloc_array(ctx, size, count, __location__)</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef DOXYGEN</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Allocate an array into a typed pointer.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The macro should be used when you have a pointer to an array and want to</span><br><span style="color: hsl(120, 100%, 40%);">+ * allocate memory of an array to point at with this pointer. When compiling</span><br><span style="color: hsl(120, 100%, 40%);">+ * with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_array_size()</span><br><span style="color: hsl(120, 100%, 40%);">+ * and talloc_get_name() will return the current location in the source file</span><br><span style="color: hsl(120, 100%, 40%);">+ * and not the type.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ctx      The talloc context to hang the result off.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ptr      The pointer you want to assign the result to.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  count    The number of elements you want to allocate.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The allocated memory chunk, properly casted. NULL on</span><br><span style="color: hsl(120, 100%, 40%);">+ *                      error.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void *talloc_array_ptrtype(const void *ctx, const void *ptr, unsigned count);</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_array_ptrtype(ctx, ptr, count) (_TALLOC_TYPEOF(ptr))talloc_array_size(ctx, sizeof(*(ptr)), count)</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef DOXYGEN</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Get the number of elements in a talloc'ed array.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * A talloc chunk carries its own size, so for talloc'ed arrays it is not</span><br><span style="color: hsl(120, 100%, 40%);">+ * necessary to store the number of elements explicitly.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ctx      The allocated array.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The number of elements in ctx.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+size_t talloc_array_length(const void *ctx);</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_array_length(ctx) (talloc_get_size(ctx)/sizeof(*ctx))</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef DOXYGEN</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Allocate a zero-initialized array</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ctx      The talloc context to hang the result off.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  type     The type that we want to allocate.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  count    The number of "type" elements you want to allocate.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The allocated result casted to "type *", NULL on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The talloc_zero_array() macro is equivalent to:</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *     ptr = talloc_array(ctx, type, count);</span><br><span style="color: hsl(120, 100%, 40%);">+ *     if (ptr) memset(ptr, 0, sizeof(type) * count);</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void *talloc_zero_array(const void *ctx, #type, unsigned count);</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_zero_array(ctx, type, count) (type *)_talloc_zero_array(ctx, sizeof(type), count, #type)</span><br><span style="color: hsl(120, 100%, 40%);">+void *_talloc_zero_array(const void *ctx,</span><br><span style="color: hsl(120, 100%, 40%);">+                     size_t el_size,</span><br><span style="color: hsl(120, 100%, 40%);">+                       unsigned count,</span><br><span style="color: hsl(120, 100%, 40%);">+                       const char *name);</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef DOXYGEN</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Change the size of a talloc array.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The macro changes the size of a talloc pointer. The 'count' argument is the</span><br><span style="color: hsl(120, 100%, 40%);">+ * number of elements of type 'type' that you want the resulting pointer to</span><br><span style="color: hsl(120, 100%, 40%);">+ * hold.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc_realloc() has the following equivalences:</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      talloc_realloc(ctx, NULL, type, 1) ==> talloc(ctx, type);</span><br><span style="color: hsl(120, 100%, 40%);">+ *      talloc_realloc(ctx, NULL, type, N) ==> talloc_array(ctx, type, N);</span><br><span style="color: hsl(120, 100%, 40%);">+ *      talloc_realloc(ctx, ptr, type, 0)  ==> talloc_free(ptr);</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The "context" argument is only used if "ptr" is NULL, otherwise it is</span><br><span style="color: hsl(120, 100%, 40%);">+ * ignored.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ctx      The parent context used if ptr is NULL.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ptr      The chunk to be resized.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  type     The type of the array element inside ptr.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  count    The intended number of array elements.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The new array, NULL on error. The call will fail either</span><br><span style="color: hsl(120, 100%, 40%);">+ *                      due to a lack of memory, or because the pointer has more</span><br><span style="color: hsl(120, 100%, 40%);">+ *                      than one parent (see talloc_reference()).</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void *talloc_realloc(const void *ctx, void *ptr, #type, size_t count);</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_realloc(ctx, p, type, count) (type *)_talloc_realloc_array(ctx, p, sizeof(type), count, #type)</span><br><span style="color: hsl(120, 100%, 40%);">+void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name);</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef DOXYGEN</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Untyped realloc to change the size of a talloc array.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The macro is useful when the type is not known so the typesafe</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc_realloc() cannot be used.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ctx      The parent context used if 'ptr' is NULL.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ptr      The chunk to be resized.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  size     The new chunk size.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The new array, NULL on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void *talloc_realloc_size(const void *ctx, void *ptr, size_t size);</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_realloc_size(ctx, ptr, size) _talloc_realloc(ctx, ptr, size, __location__)</span><br><span style="color: hsl(120, 100%, 40%);">+void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name);</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Provide a function version of talloc_realloc_size.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This is a non-macro version of talloc_realloc(), which is useful as</span><br><span style="color: hsl(120, 100%, 40%);">+ * libraries sometimes want a ralloc function pointer. A realloc()</span><br><span style="color: hsl(120, 100%, 40%);">+ * implementation encapsulates the functionality of malloc(), free() and</span><br><span style="color: hsl(120, 100%, 40%);">+ * realloc() in one call, which is why it is useful to be able to pass around</span><br><span style="color: hsl(120, 100%, 40%);">+ * a single function pointer.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  context  The parent context used if ptr is NULL.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ptr      The chunk to be resized.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  size     The new chunk size.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The new chunk, NULL on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void *talloc_realloc_fn(const void *context, void *ptr, size_t size);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* @} ******************************************************************/</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @defgroup talloc_string The talloc string functions.</span><br><span style="color: hsl(120, 100%, 40%);">+ * @ingroup talloc</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc string allocation and manipulation functions.</span><br><span style="color: hsl(120, 100%, 40%);">+ * @{</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Duplicate a string into a talloc chunk.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This function is equivalent to:</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      ptr = talloc_size(ctx, strlen(p)+1);</span><br><span style="color: hsl(120, 100%, 40%);">+ *      if (ptr) memcpy(ptr, p, strlen(p)+1);</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This functions sets the name of the new pointer to the passed</span><br><span style="color: hsl(120, 100%, 40%);">+ * string. This is equivalent to:</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      talloc_set_name_const(ptr, ptr)</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  t        The talloc context to hang the result off.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  p        The string you want to duplicate.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The duplicated string, NULL on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+char *talloc_strdup(const void *t, const char *p);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Append a string to given string.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The destination string is reallocated to take</span><br><span style="color: hsl(120, 100%, 40%);">+ * <code>strlen(s) + strlen(a) + 1</code> characters.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This functions sets the name of the new pointer to the new</span><br><span style="color: hsl(120, 100%, 40%);">+ * string. This is equivalent to:</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      talloc_set_name_const(ptr, ptr)</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * If <code>s == NULL</code> then new context is created.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  s        The destination to append to.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  a        The string you want to append.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The concatenated strings, NULL on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_strdup()</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_strdup_append_buffer()</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+char *talloc_strdup_append(char *s, const char *a);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Append a string to a given buffer.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This is a more efficient version of talloc_strdup_append(). It determines the</span><br><span style="color: hsl(120, 100%, 40%);">+ * length of the destination string by the size of the talloc context.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Use this very carefully as it produces a different result than</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc_strdup_append() when a zero character is in the middle of the</span><br><span style="color: hsl(120, 100%, 40%);">+ * destination string.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      char *str_a = talloc_strdup(NULL, "hello world");</span><br><span style="color: hsl(120, 100%, 40%);">+ *      char *str_b = talloc_strdup(NULL, "hello world");</span><br><span style="color: hsl(120, 100%, 40%);">+ *      str_a[5] = str_b[5] = '\0'</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ *      char *app = talloc_strdup_append(str_a, ", hello");</span><br><span style="color: hsl(120, 100%, 40%);">+ *      char *buf = talloc_strdup_append_buffer(str_b, ", hello");</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ *      printf("%s\n", app); // hello, hello (app = "hello, hello")</span><br><span style="color: hsl(120, 100%, 40%);">+ *      printf("%s\n", buf); // hello (buf = "hello\0world, hello")</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * If <code>s == NULL</code> then new context is created.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  s        The destination buffer to append to.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  a        The string you want to append.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The concatenated strings, NULL on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_strdup()</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_strdup_append()</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_array_length()</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+char *talloc_strdup_append_buffer(char *s, const char *a);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Duplicate a length-limited string into a talloc chunk.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This function is the talloc equivalent of the C library function strndup(3).</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This functions sets the name of the new pointer to the passed string. This is</span><br><span style="color: hsl(120, 100%, 40%);">+ * equivalent to:</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      talloc_set_name_const(ptr, ptr)</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  t        The talloc context to hang the result off.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  p        The string you want to duplicate.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  n        The maximum string length to duplicate.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The duplicated string, NULL on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+char *talloc_strndup(const void *t, const char *p, size_t n);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Append at most n characters of a string to given string.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The destination string is reallocated to take</span><br><span style="color: hsl(120, 100%, 40%);">+ * <code>strlen(s) + strnlen(a, n) + 1</code> characters.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This functions sets the name of the new pointer to the new</span><br><span style="color: hsl(120, 100%, 40%);">+ * string. This is equivalent to:</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      talloc_set_name_const(ptr, ptr)</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * If <code>s == NULL</code> then new context is created.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  s        The destination string to append to.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  a        The source string you want to append.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  n        The number of characters you want to append from the</span><br><span style="color: hsl(120, 100%, 40%);">+ *                      string.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The concatenated strings, NULL on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_strndup()</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_strndup_append_buffer()</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+char *talloc_strndup_append(char *s, const char *a, size_t n);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Append at most n characters of a string to given buffer</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This is a more efficient version of talloc_strndup_append(). It determines</span><br><span style="color: hsl(120, 100%, 40%);">+ * the length of the destination string by the size of the talloc context.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Use this very carefully as it produces a different result than</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc_strndup_append() when a zero character is in the middle of the</span><br><span style="color: hsl(120, 100%, 40%);">+ * destination string.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      char *str_a = talloc_strdup(NULL, "hello world");</span><br><span style="color: hsl(120, 100%, 40%);">+ *      char *str_b = talloc_strdup(NULL, "hello world");</span><br><span style="color: hsl(120, 100%, 40%);">+ *      str_a[5] = str_b[5] = '\0'</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ *      char *app = talloc_strndup_append(str_a, ", hello", 7);</span><br><span style="color: hsl(120, 100%, 40%);">+ *      char *buf = talloc_strndup_append_buffer(str_b, ", hello", 7);</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ *      printf("%s\n", app); // hello, hello (app = "hello, hello")</span><br><span style="color: hsl(120, 100%, 40%);">+ *      printf("%s\n", buf); // hello (buf = "hello\0world, hello")</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * If <code>s == NULL</code> then new context is created.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  s        The destination buffer to append to.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  a        The source string you want to append.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  n        The number of characters you want to append from the</span><br><span style="color: hsl(120, 100%, 40%);">+ *                      string.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The concatenated strings, NULL on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_strndup()</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_strndup_append()</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_array_length()</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+char *talloc_strndup_append_buffer(char *s, const char *a, size_t n);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Format a string given a va_list.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This function is the talloc equivalent of the C library function</span><br><span style="color: hsl(120, 100%, 40%);">+ * vasprintf(3).</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This functions sets the name of the new pointer to the new string. This is</span><br><span style="color: hsl(120, 100%, 40%);">+ * equivalent to:</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      talloc_set_name_const(ptr, ptr)</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  t        The talloc context to hang the result off.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  fmt      The format string.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ap       The parameters used to fill fmt.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The formatted string, NULL on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+char *talloc_vasprintf(const void *t, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Format a string given a va_list and append it to the given destination</span><br><span style="color: hsl(120, 100%, 40%);">+ *        string.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  s        The destination string to append to.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  fmt      The format string.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ap       The parameters used to fill fmt.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The formatted string, NULL on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_vasprintf()</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Format a string given a va_list and append it to the given destination</span><br><span style="color: hsl(120, 100%, 40%);">+ *        buffer.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  s        The destination buffer to append to.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  fmt      The format string.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ap       The parameters used to fill fmt.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The formatted string, NULL on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_vasprintf()</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Format a string.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This function is the talloc equivalent of the C library function asprintf(3).</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This functions sets the name of the new pointer to the new string. This is</span><br><span style="color: hsl(120, 100%, 40%);">+ * equivalent to:</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      talloc_set_name_const(ptr, ptr)</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  t        The talloc context to hang the result off.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  fmt      The format string.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ...      The parameters used to fill fmt.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The formatted string, NULL on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+char *talloc_asprintf(const void *t, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Append a formatted string to another string.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This function appends the given formatted string to the given string. Use</span><br><span style="color: hsl(120, 100%, 40%);">+ * this variant when the string in the current talloc buffer may have been</span><br><span style="color: hsl(120, 100%, 40%);">+ * truncated in length.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This functions sets the name of the new pointer to the new</span><br><span style="color: hsl(120, 100%, 40%);">+ * string. This is equivalent to:</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      talloc_set_name_const(ptr, ptr)</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * If <code>s == NULL</code> then new context is created.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  s        The string to append to.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  fmt      The format string.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ...      The parameters used to fill fmt.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The formatted string, NULL on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+char *talloc_asprintf_append(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Append a formatted string to another string.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This is a more efficient version of talloc_asprintf_append(). It determines</span><br><span style="color: hsl(120, 100%, 40%);">+ * the length of the destination string by the size of the talloc context.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Use this very carefully as it produces a different result than</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc_asprintf_append() when a zero character is in the middle of the</span><br><span style="color: hsl(120, 100%, 40%);">+ * destination string.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      char *str_a = talloc_strdup(NULL, "hello world");</span><br><span style="color: hsl(120, 100%, 40%);">+ *      char *str_b = talloc_strdup(NULL, "hello world");</span><br><span style="color: hsl(120, 100%, 40%);">+ *      str_a[5] = str_b[5] = '\0'</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ *      char *app = talloc_asprintf_append(str_a, "%s", ", hello");</span><br><span style="color: hsl(120, 100%, 40%);">+ *      char *buf = talloc_strdup_append_buffer(str_b, "%s", ", hello");</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ *      printf("%s\n", app); // hello, hello (app = "hello, hello")</span><br><span style="color: hsl(120, 100%, 40%);">+ *      printf("%s\n", buf); // hello (buf = "hello\0world, hello")</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * If <code>s == NULL</code> then new context is created.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  s        The string to append to</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  fmt      The format string.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ...      The parameters used to fill fmt.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @return              The formatted string, NULL on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_asprintf()</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_asprintf_append()</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* @} ******************************************************************/</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @defgroup talloc_debug The talloc debugging support functions</span><br><span style="color: hsl(120, 100%, 40%);">+ * @ingroup talloc</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * To aid memory debugging, talloc contains routines to inspect the currently</span><br><span style="color: hsl(120, 100%, 40%);">+ * allocated memory hierarchy.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @{</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Walk a complete talloc hierarchy.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This provides a more flexible reports than talloc_report(). It</span><br><span style="color: hsl(120, 100%, 40%);">+ * will recursively call the callback for the entire tree of memory</span><br><span style="color: hsl(120, 100%, 40%);">+ * referenced by the pointer. References in the tree are passed with</span><br><span style="color: hsl(120, 100%, 40%);">+ * is_ref = 1 and the pointer that is referenced.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * You can pass NULL for the pointer, in which case a report is</span><br><span style="color: hsl(120, 100%, 40%);">+ * printed for the top level memory context, but only if</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc_enable_leak_report() or talloc_enable_leak_report_full()</span><br><span style="color: hsl(120, 100%, 40%);">+ * has been called.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The recursion is stopped when depth >= max_depth.</span><br><span style="color: hsl(120, 100%, 40%);">+ * max_depth = -1 means only stop at leaf nodes.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ptr      The talloc chunk.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  depth    Internal parameter to control recursion. Call with 0.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  max_depth  Maximum recursion level.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  callback  Function to be called on every chunk.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  private_data  Private pointer passed to callback.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,</span><br><span style="color: hsl(120, 100%, 40%);">+                       void (*callback)(const void *ptr,</span><br><span style="color: hsl(120, 100%, 40%);">+                                      int depth, int max_depth,</span><br><span style="color: hsl(120, 100%, 40%);">+                                             int is_ref,</span><br><span style="color: hsl(120, 100%, 40%);">+                                           void *private_data),</span><br><span style="color: hsl(120, 100%, 40%);">+                         void *private_data);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Print a talloc hierarchy.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This provides a more flexible reports than talloc_report(). It</span><br><span style="color: hsl(120, 100%, 40%);">+ * will let you specify the depth and max_depth.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ptr      The talloc chunk.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  depth    Internal parameter to control recursion. Call with 0.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  max_depth  Maximum recursion level.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  f        The file handle to print to.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Print a summary report of all memory used by ptr.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This provides a more detailed report than talloc_report(). It will</span><br><span style="color: hsl(120, 100%, 40%);">+ * recursively print the entire tree of memory referenced by the</span><br><span style="color: hsl(120, 100%, 40%);">+ * pointer. References in the tree are shown by giving the name of the</span><br><span style="color: hsl(120, 100%, 40%);">+ * pointer that is referenced.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * You can pass NULL for the pointer, in which case a report is printed</span><br><span style="color: hsl(120, 100%, 40%);">+ * for the top level memory context, but only if</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc_enable_leak_report() or talloc_enable_leak_report_full() has</span><br><span style="color: hsl(120, 100%, 40%);">+ * been called.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ptr      The talloc chunk.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  f        The file handle to print to.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Example:</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      unsigned int *a, *b;</span><br><span style="color: hsl(120, 100%, 40%);">+ *      a = talloc(NULL, unsigned int);</span><br><span style="color: hsl(120, 100%, 40%);">+ *      b = talloc(a, unsigned int);</span><br><span style="color: hsl(120, 100%, 40%);">+ *      fprintf(stderr, "Dumping memory tree for a:\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ *      talloc_report_full(a, stderr);</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_report()</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void talloc_report_full(const void *ptr, FILE *f);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Print a summary report of all memory used by ptr.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This function prints a summary report of all memory used by ptr. One line of</span><br><span style="color: hsl(120, 100%, 40%);">+ * report is printed for each immediate child of ptr, showing the total memory</span><br><span style="color: hsl(120, 100%, 40%);">+ * and number of blocks used by that child.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * You can pass NULL for the pointer, in which case a report is printed</span><br><span style="color: hsl(120, 100%, 40%);">+ * for the top level memory context, but only if talloc_enable_leak_report()</span><br><span style="color: hsl(120, 100%, 40%);">+ * or talloc_enable_leak_report_full() has been called.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  ptr      The talloc chunk.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  f        The file handle to print to.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Example:</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ *      unsigned int *a, *b;</span><br><span style="color: hsl(120, 100%, 40%);">+ *      a = talloc(NULL, unsigned int);</span><br><span style="color: hsl(120, 100%, 40%);">+ *      b = talloc(a, unsigned int);</span><br><span style="color: hsl(120, 100%, 40%);">+ *      fprintf(stderr, "Summary of memory tree for a:\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ *      talloc_report(a, stderr);</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_report_full()</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void talloc_report(const void *ptr, FILE *f);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Enable tracking the use of NULL memory contexts.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This enables tracking of the NULL memory context without enabling leak</span><br><span style="color: hsl(120, 100%, 40%);">+ * reporting on exit. Useful for when you want to do your own leak</span><br><span style="color: hsl(120, 100%, 40%);">+ * reporting call via talloc_report_null_full();</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void talloc_enable_null_tracking(void);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Enable tracking the use of NULL memory contexts.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This enables tracking of the NULL memory context without enabling leak</span><br><span style="color: hsl(120, 100%, 40%);">+ * reporting on exit. Useful for when you want to do your own leak</span><br><span style="color: hsl(120, 100%, 40%);">+ * reporting call via talloc_report_null_full();</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void talloc_enable_null_tracking_no_autofree(void);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Disable tracking of the NULL memory context.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This disables tracking of the NULL memory context.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void talloc_disable_null_tracking(void);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Enable leak report when a program exits.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This enables calling of talloc_report(NULL, stderr) when the program</span><br><span style="color: hsl(120, 100%, 40%);">+ * exits. In Samba4 this is enabled by using the --leak-report command</span><br><span style="color: hsl(120, 100%, 40%);">+ * line option.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * For it to be useful, this function must be called before any other</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc function as it establishes a "null context" that acts as the</span><br><span style="color: hsl(120, 100%, 40%);">+ * top of the tree. If you don't call this function first then passing</span><br><span style="color: hsl(120, 100%, 40%);">+ * NULL to talloc_report() or talloc_report_full() won't give you the</span><br><span style="color: hsl(120, 100%, 40%);">+ * full tree printout.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Here is a typical talloc report:</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc report on 'null_context' (total 267 bytes in 15 blocks)</span><br><span style="color: hsl(120, 100%, 40%);">+ *      libcli/auth/spnego_parse.c:55  contains     31 bytes in   2 blocks</span><br><span style="color: hsl(120, 100%, 40%);">+ *      libcli/auth/spnego_parse.c:55  contains     31 bytes in   2 blocks</span><br><span style="color: hsl(120, 100%, 40%);">+ *      iconv(UTF8,CP850)              contains     42 bytes in   2 blocks</span><br><span style="color: hsl(120, 100%, 40%);">+ *      libcli/auth/spnego_parse.c:55  contains     31 bytes in   2 blocks</span><br><span style="color: hsl(120, 100%, 40%);">+ *      iconv(CP850,UTF8)              contains     42 bytes in   2 blocks</span><br><span style="color: hsl(120, 100%, 40%);">+ *      iconv(UTF8,UTF-16LE)           contains     45 bytes in   2 blocks</span><br><span style="color: hsl(120, 100%, 40%);">+ *      iconv(UTF-16LE,UTF8)           contains     45 bytes in   2 blocks</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void talloc_enable_leak_report(void);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Enable full leak report when a program exits.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This enables calling of talloc_report_full(NULL, stderr) when the</span><br><span style="color: hsl(120, 100%, 40%);">+ * program exits. In Samba4 this is enabled by using the</span><br><span style="color: hsl(120, 100%, 40%);">+ * --leak-report-full command line option.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * For it to be useful, this function must be called before any other</span><br><span style="color: hsl(120, 100%, 40%);">+ * talloc function as it establishes a "null context" that acts as the</span><br><span style="color: hsl(120, 100%, 40%);">+ * top of the tree. If you don't call this function first then passing</span><br><span style="color: hsl(120, 100%, 40%);">+ * NULL to talloc_report() or talloc_report_full() won't give you the</span><br><span style="color: hsl(120, 100%, 40%);">+ * full tree printout.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Here is a typical full report:</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ * full talloc report on 'root' (total 18 bytes in 8 blocks)</span><br><span style="color: hsl(120, 100%, 40%);">+ *      p1                             contains     18 bytes in   7 blocks (ref 0)</span><br><span style="color: hsl(120, 100%, 40%);">+ *      r1                             contains     13 bytes in   2 blocks (ref 0)</span><br><span style="color: hsl(120, 100%, 40%);">+ *      reference to: p2</span><br><span style="color: hsl(120, 100%, 40%);">+ *      p2                             contains      1 bytes in   1 blocks (ref 1)</span><br><span style="color: hsl(120, 100%, 40%);">+ *      x3                             contains      1 bytes in   1 blocks (ref 0)</span><br><span style="color: hsl(120, 100%, 40%);">+ *      x2                             contains      1 bytes in   1 blocks (ref 0)</span><br><span style="color: hsl(120, 100%, 40%);">+ *      x1                             contains      1 bytes in   1 blocks (ref 0)</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void talloc_enable_leak_report_full(void);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Set a custom "abort" function that is called on serious error.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The default "abort" function is <code>abort()</code>.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The "abort" function is called when:</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * <ul></span><br><span style="color: hsl(120, 100%, 40%);">+ *  <li>talloc_get_type_abort() fails</li></span><br><span style="color: hsl(120, 100%, 40%);">+ *  <li>the provided pointer is not a valid talloc context</li></span><br><span style="color: hsl(120, 100%, 40%);">+ *  <li>when the context meta data are invalid</li></span><br><span style="color: hsl(120, 100%, 40%);">+ *  <li>when access after free is detected</li></span><br><span style="color: hsl(120, 100%, 40%);">+ * </ul></span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Example:</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @code</span><br><span style="color: hsl(120, 100%, 40%);">+ * void my_abort(const char *reason)</span><br><span style="color: hsl(120, 100%, 40%);">+ * {</span><br><span style="color: hsl(120, 100%, 40%);">+ *      fprintf(stderr, "talloc abort: %s\n", reason);</span><br><span style="color: hsl(120, 100%, 40%);">+ *      abort();</span><br><span style="color: hsl(120, 100%, 40%);">+ * }</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ *      talloc_set_abort_fn(my_abort);</span><br><span style="color: hsl(120, 100%, 40%);">+ * @endcode</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  abort_fn      The new "abort" function.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_set_log_fn()</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_get_type()</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void talloc_set_abort_fn(void (*abort_fn)(const char *reason));</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Set a logging function.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]  log_fn      The logging function.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_set_log_stderr()</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_set_abort_fn()</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void talloc_set_log_fn(void (*log_fn)(const char *message));</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Set stderr as the output for logs.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_set_log_fn()</span><br><span style="color: hsl(120, 100%, 40%);">+ * @see talloc_set_abort_fn()</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void talloc_set_log_stderr(void);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * @brief Set a max memory limit for the current context hierarchy</span><br><span style="color: hsl(120, 100%, 40%);">+ *      This affects all children of this context and constrain any</span><br><span style="color: hsl(120, 100%, 40%);">+ *         allocation in the hierarchy to never exceed the limit set.</span><br><span style="color: hsl(120, 100%, 40%);">+ *          The limit can be removed by setting 0 (unlimited) as the</span><br><span style="color: hsl(120, 100%, 40%);">+ *    max_size by calling the function again on the same context.</span><br><span style="color: hsl(120, 100%, 40%);">+ *         Memory limits can also be nested, meaning a child can have</span><br><span style="color: hsl(120, 100%, 40%);">+ *          a stricter memory limit than a parent.</span><br><span style="color: hsl(120, 100%, 40%);">+ *      Memory limits are enforced only at memory allocation time.</span><br><span style="color: hsl(120, 100%, 40%);">+ *          Stealing a context into a 'limited' hierarchy properly</span><br><span style="color: hsl(120, 100%, 40%);">+ *      updates memory usage but does *not* cause failure if the</span><br><span style="color: hsl(120, 100%, 40%);">+ *    move causes the new parent to exceed its limits. However</span><br><span style="color: hsl(120, 100%, 40%);">+ *    any further allocation on that hierarchy will then fail.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]   ctx             The talloc context to set the limit on</span><br><span style="color: hsl(120, 100%, 40%);">+ * @param[in]   max_size        The (new) max_size</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+int talloc_set_memlimit(const void *ctx, size_t max_size);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* @} ******************************************************************/</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#if TALLOC_DEPRECATED</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_zero_p(ctx, type) talloc_zero(ctx, type)</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_p(ctx, type) talloc(ctx, type)</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_array_p(ctx, type, count) talloc_array(ctx, type, count)</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_realloc_p(ctx, p, type, count) talloc_realloc(ctx, p, type, count)</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_destroy(ctx) talloc_free(ctx)</span><br><span style="color: hsl(120, 100%, 40%);">+#define talloc_append_string(c, s, a) (s?talloc_strdup_append(s,a):talloc_strdup(c, a))</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifndef TALLOC_MAX_DEPTH</span><br><span style="color: hsl(120, 100%, 40%);">+#define TALLOC_MAX_DEPTH 10000</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef __cplusplus</span><br><span style="color: hsl(120, 100%, 40%);">+} /* end of extern "C" */</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/13993">change 13993</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/13993"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: osmo-ccid-firmware </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-MessageType: merged </div>
<div style="display:none"> Gerrit-Change-Id: Ibd31b40d5b75b3fc1783a6b02b0e97da430e0f60 </div>
<div style="display:none"> Gerrit-Change-Number: 13993 </div>
<div style="display:none"> Gerrit-PatchSet: 2 </div>
<div style="display:none"> Gerrit-Owner: Harald Welte <laforge@gnumonks.org> </div>
<div style="display:none"> Gerrit-Reviewer: Harald Welte <laforge@gnumonks.org> </div>
<div style="display:none"> Gerrit-Reviewer: Jenkins Builder (1000002) </div>