Ignore:
Timestamp:
Nov 14, 2012, 12:59:34 PM (13 years ago)
Author:
Silvan Scherrer
Message:

Samba Server: update vendor to 3.6.0

Location:
vendor/current/lib/talloc
Files:
14 added
13 deleted
12 edited

Legend:

Unmodified
Added
Removed
  • vendor/current/lib/talloc/pytalloc.c

    r414 r740  
    11/*
    22   Unix SMB/CIFS implementation.
    3    Python/Talloc glue
    4    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
    5    
     3   Python Talloc Module
     4   Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2010
     5
    66   This program is free software; you can redistribute it and/or modify
    77   it under the terms of the GNU General Public License as published by
    88   the Free Software Foundation; either version 3 of the License, or
    99   (at your option) any later version.
    10    
     10
    1111   This program is distributed in the hope that it will be useful,
    1212   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1313   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1414   GNU General Public License for more details.
    15    
     15
    1616   You should have received a copy of the GNU General Public License
    1717   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1818*/
    1919
    20 #include "replace.h"
     20#include <Python.h>
    2121#include <talloc.h>
    2222#include <pytalloc.h>
    2323
    24 /**
    25  * Simple dealloc for talloc-wrapping PyObjects
    26  */
    27 void py_talloc_dealloc(PyObject* self)
     24void inittalloc(void);
     25
     26/* print a talloc tree report for a talloc python object */
     27static PyObject *py_talloc_report_full(PyObject *self, PyObject *args)
    2828{
    29         py_talloc_Object *obj = (py_talloc_Object *)self;
    30         talloc_free(obj->talloc_ctx);
    31         obj->talloc_ctx = NULL;
    32         self->ob_type->tp_free(self);
     29        PyObject *py_obj = Py_None;
     30        PyTypeObject *type;
     31
     32        if (!PyArg_ParseTuple(args, "|O", &py_obj))
     33                return NULL;
     34
     35        if (py_obj == Py_None) {
     36                talloc_report_full(NULL, stdout);
     37        } else {
     38                type = (PyTypeObject*)PyObject_Type(py_obj);
     39                talloc_report_full(py_talloc_get_mem_ctx(py_obj), stdout);
     40        }
     41        return Py_None;
    3342}
    3443
    35 /**
    36  * Import an existing talloc pointer into a Python object.
    37  */
    38 PyObject *py_talloc_steal_ex(PyTypeObject *py_type, TALLOC_CTX *mem_ctx,
    39                                                    void *ptr)
     44/* enable null tracking */
     45static PyObject *py_talloc_enable_null_tracking(PyObject *self)
    4046{
    41         py_talloc_Object *ret = (py_talloc_Object *)py_type->tp_alloc(py_type, 0);
    42         ret->talloc_ctx = talloc_new(NULL);
    43         if (ret->talloc_ctx == NULL) {
    44                 return NULL;
    45         }
    46         if (talloc_steal(ret->talloc_ctx, mem_ctx) == NULL) {
    47                 return NULL;
    48         }
    49         ret->ptr = ptr;
    50         return (PyObject *)ret;
     47        talloc_enable_null_tracking();
     48        return Py_None;
    5149}
    5250
     51/* return the number of talloc blocks */
     52static PyObject *py_talloc_total_blocks(PyObject *self, PyObject *args)
     53{
     54        PyObject *py_obj = Py_None;
     55        PyTypeObject *type;
     56
     57        if (!PyArg_ParseTuple(args, "|O", &py_obj))
     58                return NULL;
     59
     60        if (py_obj == Py_None) {
     61                return PyLong_FromLong(talloc_total_blocks(NULL));
     62        }
     63
     64        type = (PyTypeObject*)PyObject_Type(py_obj);
     65
     66        return PyLong_FromLong(talloc_total_blocks(py_talloc_get_mem_ctx(py_obj)));
     67}
     68
     69static PyMethodDef talloc_methods[] = {
     70        { "report_full", (PyCFunction)py_talloc_report_full, METH_VARARGS,
     71                "show a talloc tree for an object"},
     72        { "enable_null_tracking", (PyCFunction)py_talloc_enable_null_tracking, METH_NOARGS,
     73                "enable tracking of the NULL object"},
     74        { "total_blocks", (PyCFunction)py_talloc_total_blocks, METH_VARARGS,
     75                "return talloc block count"},
     76        { NULL }
     77};
    5378
    5479/**
    55  * Import an existing talloc pointer into a Python object, leaving the
    56  * original parent, and creating a reference to the object in the python
    57  * object
     80 * Default (but only slightly more useful than the default) implementation of Repr().
    5881 */
    59 PyObject *py_talloc_reference_ex(PyTypeObject *py_type, TALLOC_CTX *mem_ctx, void *ptr)
    60 {
    61         py_talloc_Object *ret = (py_talloc_Object *)py_type->tp_alloc(py_type, 0);
    62         ret->talloc_ctx = talloc_new(NULL);
    63         if (ret->talloc_ctx == NULL) {
    64                 return NULL;
    65         }
    66         if (talloc_reference(ret->talloc_ctx, mem_ctx) == NULL) {
    67                 return NULL;
    68         }
    69         ret->ptr = ptr;
    70         return (PyObject *)ret;
    71 }
    72 
    73 /**
    74  * Default (but slightly more useful than the default) implementation of Repr().
    75  */
    76 PyObject *py_talloc_default_repr(PyObject *obj)
     82static PyObject *py_talloc_default_repr(PyObject *obj)
    7783{
    7884        py_talloc_Object *talloc_obj = (py_talloc_Object *)obj;
     
    8389}
    8490
    85 static void py_cobject_talloc_free(void *ptr)
     91/**
     92 * Simple dealloc for talloc-wrapping PyObjects
     93 */
     94static void py_talloc_dealloc(PyObject* self)
    8695{
    87         talloc_free(ptr);
     96        py_talloc_Object *obj = (py_talloc_Object *)self;
     97        assert(talloc_unlink(NULL, obj->talloc_ctx) != -1);
     98        obj->talloc_ctx = NULL;
     99        self->ob_type->tp_free(self);
    88100}
    89101
    90 PyObject *PyCObject_FromTallocPtr(void *ptr)
     102/**
     103 * Default (but only slightly more useful than the default) implementation of cmp.
     104 */
     105static int py_talloc_default_cmp(PyObject *_obj1, PyObject *_obj2)
    91106{
    92         return PyCObject_FromVoidPtr(ptr, py_cobject_talloc_free);
     107        py_talloc_Object *obj1 = (py_talloc_Object *)_obj1,
     108                                         *obj2 = (py_talloc_Object *)_obj2;
     109        if (obj1->ob_type != obj2->ob_type)
     110                return (obj1->ob_type - obj2->ob_type);
     111
     112        return ((char *)py_talloc_get_ptr(obj1) - (char *)py_talloc_get_ptr(obj2));
    93113}
     114
     115static PyTypeObject TallocObject_Type = {
     116        .tp_name = "talloc.Object",
     117        .tp_doc = "Python wrapper for a talloc-maintained object.",
     118        .tp_basicsize = sizeof(py_talloc_Object),
     119        .tp_dealloc = (destructor)py_talloc_dealloc,
     120        .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
     121        .tp_repr = py_talloc_default_repr,
     122        .tp_compare = py_talloc_default_cmp,
     123};
     124
     125void inittalloc(void)
     126{
     127        PyObject *m;
     128
     129        if (PyType_Ready(&TallocObject_Type) < 0)
     130                return;
     131
     132        m = Py_InitModule3("talloc", talloc_methods,
     133                                           "Python wrapping of talloc-maintained objects.");
     134        if (m == NULL)
     135                return;
     136
     137        Py_INCREF(&TallocObject_Type);
     138        PyModule_AddObject(m, "Object", (PyObject *)&TallocObject_Type);
     139}
  • vendor/current/lib/talloc/pytalloc.h

    r414 r740  
    3030} py_talloc_Object;
    3131
    32 /* Deallocate a py_talloc_Object */
    33 void py_talloc_dealloc(PyObject* self);
     32PyTypeObject *PyTalloc_GetObjectType(void);
     33int PyTalloc_Check(PyObject *);
    3434
    3535/* Retrieve the pointer for a py_talloc_object. Like talloc_get_type()
     
    4444
    4545PyObject *py_talloc_steal_ex(PyTypeObject *py_type, TALLOC_CTX *mem_ctx, void *ptr);
     46PyObject *py_talloc_steal(PyTypeObject *py_type, void *ptr);
    4647PyObject *py_talloc_reference_ex(PyTypeObject *py_type, TALLOC_CTX *mem_ctx, void *ptr);
    47 #define py_talloc_steal(py_type, talloc_ptr) py_talloc_steal_ex(py_type, talloc_ptr, talloc_ptr)
    4848#define py_talloc_reference(py_type, talloc_ptr) py_talloc_reference_ex(py_type, talloc_ptr, talloc_ptr)
    49 
    50 /* Sane default implementation of reprfunc. */
    51 PyObject *py_talloc_default_repr(PyObject *py_obj);
    5249
    5350#define py_talloc_new(type, typeobj) py_talloc_steal(typeobj, talloc_zero(NULL, type))
     
    5552PyObject *PyCObject_FromTallocPtr(void *);
    5653
     54PyObject *PyString_FromString_check_null(const char *ptr);
     55
    5756#endif /* _PY_TALLOC_H_ */
  • vendor/current/lib/talloc/script/mksigs.pl

    r414 r740  
    2828
    2929my $in_comment = 0;
     30my $in_doxygen = 0;
    3031my $extern_C_block = 0;
    3132
     
    4041                        next;
    4142                }
     43        }
     44
     45        # find end of DOXYGEN section
     46        if ($in_doxygen) {
     47                if ($LINE =~ /^#\s*else(?:\s+.*)?$/) {
     48                        $in_doxygen = 0;
     49                }
     50                next;
    4251        }
    4352
     
    6978                $LINE .= " " . $LINE2;
    7079        }
     80
     81        # remove DOXYGEN sections
     82        if ($LINE =~ /^#\s*ifdef\s+DOXYGEN(?:\s+.*)?$/) {
     83                $in_doxygen = 1;
     84                next;
     85        }
     86
    7187
    7288        # remove all preprocessor directives
  • vendor/current/lib/talloc/script/mksyms.awk

    r414 r740  
    99BEGIN {
    1010        inheader=0;
     11        indoxygen=0;
    1112}
    1213
     
    2122                next;
    2223        }
     24        if (indoxygen) {
     25                if (match($0,"^#[ \t]*else[ \t]*.*$")) {
     26                        indoxygen = 0;
     27                }
     28                next;
     29        }
    2330}
    2431
    25 /^static/ || /^[ \t]*typedef/ || !/^[a-zA-Z\_]/ {
     32/^static/ || /^[ \t]*typedef/ || !/^[a-zA-Z\_\#]/ {
    2633        next;
    2734}
     
    3441}
    3542
     43/^#[ \t]*ifdef[ \t]*DOXYGEN[ \t]*.*$/ {
     44        indoxygen=1;
     45        next;
     46}
     47
    3648# look for function headers:
    3749{
    3850        gotstart = 0;
    3951        if ($0 ~ /^[A-Za-z_][A-Za-z0-9_]+/) {
    40         gotstart = 1;
     52                gotstart = 1;
    4153        }
    4254        if(!gotstart) {
  • vendor/current/lib/talloc/script/release-script.sh

    r414 r740  
    11#!/bin/bash
     2
     3LNAME=talloc
     4LINCLUDE=talloc.h
    25
    36if [ "$1" = "" ]; then
     
    69fi
    710
    8 if [ ! -d "lib/talloc" ]; then
     11if [ ! -d "lib/${LNAME}" ]; then
    912    echo "Run this script from the samba base directory."
    1013    exit 1
    1114fi
    12 
    13 # Check exports and signatures are up to date
    14 pushd lib/talloc
    15 ./script/abi_checks.sh talloc talloc.h
    16 abicheck=$?
    17 popd
    18 if [ ! "$abicheck" = "0" ]; then
    19     echo "ERROR: ABI Checks produced warnings!"
    20     exit 1
    21 fi
    22 
    23 git clean -f -x -d lib/talloc
    24 git clean -f -x -d lib/replace
    2515
    2616curbranch=`git branch |grep "^*" | tr -d "* "`
     
    3020
    3121# Checkout the release tag
    32 git branch -f talloc-release-script-${strver} talloc-${strver}
     22git branch -f ${LNAME}-release-script-${strver} ${LNAME}-${strver}
    3323if [ ! "$?" = "0" ];  then
    34     echo "Unable to checkout talloc-${strver} release"
     24    echo "Unable to checkout ${LNAME}-${strver} release"
    3525    exit 1
    3626fi
    3727
    38 git checkout talloc-release-script-${strver}
     28function cleanquit {
     29    #Clean up
     30    git checkout $curbranch
     31    git branch -d ${LNAME}-release-script-${strver}
     32    exit $1
     33}
     34
     35# NOTE: use cleanquit after this point
     36git checkout ${LNAME}-release-script-${strver}
    3937
    4038# Test configure agrees with us
    41 confver=`grep "^AC_INIT" lib/talloc/configure.ac | tr -d "AC_INIT(talloc, " | tr -d ")"`
     39confver=`grep "^AC_INIT" lib/${LNAME}/configure.ac | tr -d "AC_INIT(${LNAME}, " | tr -d ")"`
    4240if [ ! "$confver" = "$version" ]; then
    4341    echo "Wrong version, requested release for ${version}, found ${confver}"
    44     exit 1
     42    cleanquit 1
    4543fi
    4644
     45# Check exports and signatures are up to date
     46pushd lib/${LNAME}
     47./script/abi_checks.sh ${LNAME} ${LINCLUDE}
     48abicheck=$?
     49popd
     50if [ ! "$abicheck" = "0" ]; then
     51    echo "ERROR: ABI Checks produced warnings!"
     52    cleanquit 1
     53fi
     54
     55git clean -f -x -d lib/${LNAME}
     56git clean -f -x -d lib/replace
     57
    4758# Now build tarball
    48 cp -a lib/talloc talloc-${version}
    49 cp -a lib/replace talloc-${version}/libreplace
    50 pushd talloc-${version}
     59cp -a lib/${LNAME} ${LNAME}-${version}
     60cp -a lib/replace ${LNAME}-${version}/libreplace
     61pushd ${LNAME}-${version}
    5162./autogen.sh
    5263popd
    53 tar cvzf talloc-${version}.tar.gz talloc-${version}
    54 rm -fr talloc-${version}
     64tar cvzf ${LNAME}-${version}.tar.gz ${LNAME}-${version}
     65rm -fr ${LNAME}-${version}
    5566
    56 #Clean up
    57 git checkout $curbranch
    58 git branch -d talloc-release-script-${strver}
     67cleanquit 0
     68
  • vendor/current/lib/talloc/talloc.3.xml

    r414 r740  
    1111  </refnamediv>
    1212  <refsynopsisdiv>
    13 <synopsis>#include &lt;talloc/talloc.h&gt;</synopsis>
     13<synopsis>#include &lt;talloc.h&gt;</synopsis>
    1414  </refsynopsisdiv>
    1515  <refsect1><title>DESCRIPTION</title>
     
    363363        <para>
    364364          This is a utility macro that creates a new memory context hanging
    365           off an exiting context, automatically naming it "talloc_new:
     365          off an existing context, automatically naming it "talloc_new:
    366366          __location__" where __location__ is the source line it is called
    367367          from.  It is particularly useful for creating a new temporary
     
    646646        <programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
    647647    </refsect2>
    648     <refsect2><title>char *talloc_append_string(const void *<emphasis role="italic">t</emphasis>, char *<emphasis role="italic">orig</emphasis>, const char *<emphasis role="italic">append</emphasis>);</title>
    649         <para>
    650           The talloc_append_string() function appends the given formatted
    651           string to the given string.
     648    <refsect2><title>char *talloc_vasprintf(const void *<emphasis role="italic">t</emphasis>, const char *<emphasis role="italic">fmt</emphasis>, va_list <emphasis role="italic">ap</emphasis>);</title>
     649        <para>
     650          The talloc_vasprintf() function is the talloc equivalent of the C
     651          library function vasprintf(3).
    652652        </para>
    653653        <para>
     
    657657        <programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
    658658    </refsect2>
    659     <refsect2><title>char *talloc_vasprintf(const void *<emphasis role="italic">t</emphasis>, const char *<emphasis role="italic">fmt</emphasis>, va_list <emphasis role="italic">ap</emphasis>);</title>
    660         <para>
    661           The talloc_vasprintf() function is the talloc equivalent of the C
    662           library function vasprintf(3).
     659    <refsect2><title>char *talloc_asprintf(const void *<emphasis role="italic">t</emphasis>, const char *<emphasis role="italic">fmt</emphasis>, ...);</title>
     660        <para>
     661          The talloc_asprintf() function is the talloc equivalent of the C
     662          library function asprintf(3).
     663        </para>
     664        <para>
     665          This function sets the name of the new pointer to the passed
     666          string. This is equivalent to:
     667        </para>
     668        <programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
     669    </refsect2>
     670    <refsect2><title>char *talloc_asprintf_append(char *s, const char *fmt, ...);</title>
     671        <para>
     672          The talloc_asprintf_append() function appends the given formatted
     673          string to the given string.
    663674        </para>
    664675        <para>
     
    668679        <programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
    669680    </refsect2>
    670     <refsect2><title>char *talloc_asprintf(const void *<emphasis role="italic">t</emphasis>, const char *<emphasis role="italic">fmt</emphasis>, ...);</title>
    671         <para>
    672           The talloc_asprintf() function is the talloc equivalent of the C
    673           library function asprintf(3).
    674         </para>
    675         <para>
    676           This function sets the name of the new pointer to the passed
    677           string. This is equivalent to:
    678         </para>
    679         <programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
    680     </refsect2>
    681     <refsect2><title>char *talloc_asprintf_append(char *s, const char *fmt, ...);</title>
    682         <para>
    683           The talloc_asprintf_append() function appends the given formatted
    684           string to the given string.
    685         </para>
    686         <para>
    687           This function sets the name of the new pointer to the new
    688           string. This is equivalent to:
    689         </para>
    690         <programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
    691     </refsect2>
    692     <refsect2><title>(type *)talloc_array(const void *ctx, type, uint_t count);</title>
     681    <refsect2><title>(type *)talloc_array(const void *ctx, type, unsigned int count);</title>
    693682        <para>
    694683          The talloc_array() macro is equivalent to:
     
    700689        </para>
    701690    </refsect2>
    702     <refsect2><title>void *talloc_array_size(const void *ctx, size_t size, uint_t count);</title>
     691    <refsect2><title>void *talloc_array_size(const void *ctx, size_t size, unsigned int count);</title>
    703692        <para>
    704693          The talloc_array_size() function is useful when the type is not
     
    707696        </para>
    708697    </refsect2>
    709     <refsect2><title>(typeof(ptr)) talloc_array_ptrtype(const void *ctx, ptr, uint_t count);</title>
     698    <refsect2><title>(typeof(ptr)) talloc_array_ptrtype(const void *ctx, ptr, unsigned int count);</title>
    710699        <para>
    711700          The talloc_ptrtype() macro should be used when you have a pointer to an array
  • vendor/current/lib/talloc/talloc.c

    r414 r740  
    4646#endif
    4747
     48/* Special macros that are no-ops except when run under Valgrind on
     49 * x86.  They've moved a little bit from valgrind 1.0.4 to 1.9.4 */
     50#ifdef HAVE_VALGRIND_MEMCHECK_H
     51        /* memcheck.h includes valgrind.h */
     52#include <valgrind/memcheck.h>
     53#elif defined(HAVE_VALGRIND_H)
     54#include <valgrind.h>
     55#endif
     56
    4857/* use this to force every realloc to change the pointer, to stress test
    4958   code that might not cope */
     
    104113static void *null_context;
    105114static void *autofree_context;
     115
     116/* used to enable fill of memory on free, which can be useful for
     117 * catching use after free errors when valgrind is too slow
     118 */
     119static struct {
     120        bool initialised;
     121        bool enabled;
     122        uint8_t fill_value;
     123} talloc_fill;
     124
     125#define TALLOC_FILL_ENV "TALLOC_FREE_FILL"
     126
     127/*
     128 * do not wipe the header, to allow the
     129 * double-free logic to still work
     130 */
     131#define TC_INVALIDATE_FULL_FILL_CHUNK(_tc) do { \
     132        if (unlikely(talloc_fill.enabled)) { \
     133                size_t _flen = (_tc)->size; \
     134                char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
     135                memset(_fptr, talloc_fill.fill_value, _flen); \
     136        } \
     137} while (0)
     138
     139#if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
     140/* Mark the whole chunk as not accessable */
     141#define TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc) do { \
     142        size_t _flen = TC_HDR_SIZE + (_tc)->size; \
     143        char *_fptr = (char *)(_tc); \
     144        VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
     145} while(0)
     146#else
     147#define TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc) do { } while (0)
     148#endif
     149
     150#define TC_INVALIDATE_FULL_CHUNK(_tc) do { \
     151        TC_INVALIDATE_FULL_FILL_CHUNK(_tc); \
     152        TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc); \
     153} while (0)
     154
     155#define TC_INVALIDATE_SHRINK_FILL_CHUNK(_tc, _new_size) do { \
     156        if (unlikely(talloc_fill.enabled)) { \
     157                size_t _flen = (_tc)->size - (_new_size); \
     158                char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
     159                _fptr += (_new_size); \
     160                memset(_fptr, talloc_fill.fill_value, _flen); \
     161        } \
     162} while (0)
     163
     164#if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
     165/* Mark the unused bytes not accessable */
     166#define TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { \
     167        size_t _flen = (_tc)->size - (_new_size); \
     168        char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
     169        _fptr += (_new_size); \
     170        VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
     171} while (0)
     172#else
     173#define TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
     174#endif
     175
     176#define TC_INVALIDATE_SHRINK_CHUNK(_tc, _new_size) do { \
     177        TC_INVALIDATE_SHRINK_FILL_CHUNK(_tc, _new_size); \
     178        TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size); \
     179} while (0)
     180
     181#define TC_UNDEFINE_SHRINK_FILL_CHUNK(_tc, _new_size) do { \
     182        if (unlikely(talloc_fill.enabled)) { \
     183                size_t _flen = (_tc)->size - (_new_size); \
     184                char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
     185                _fptr += (_new_size); \
     186                memset(_fptr, talloc_fill.fill_value, _flen); \
     187        } \
     188} while (0)
     189
     190#if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
     191/* Mark the unused bytes as undefined */
     192#define TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { \
     193        size_t _flen = (_tc)->size - (_new_size); \
     194        char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
     195        _fptr += (_new_size); \
     196        VALGRIND_MAKE_MEM_UNDEFINED(_fptr, _flen); \
     197} while (0)
     198#else
     199#define TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
     200#endif
     201
     202#define TC_UNDEFINE_SHRINK_CHUNK(_tc, _new_size) do { \
     203        TC_UNDEFINE_SHRINK_FILL_CHUNK(_tc, _new_size); \
     204        TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size); \
     205} while (0)
     206
     207#if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
     208/* Mark the new bytes as undefined */
     209#define TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size) do { \
     210        size_t _old_used = TC_HDR_SIZE + (_tc)->size; \
     211        size_t _new_used = TC_HDR_SIZE + (_new_size); \
     212        size_t _flen = _new_used - _old_used; \
     213        char *_fptr = _old_used + (char *)(_tc); \
     214        VALGRIND_MAKE_MEM_UNDEFINED(_fptr, _flen); \
     215} while (0)
     216#else
     217#define TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
     218#endif
     219
     220#define TC_UNDEFINE_GROW_CHUNK(_tc, _new_size) do { \
     221        TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size); \
     222} while (0)
    106223
    107224struct talloc_reference_handle {
     
    137254
    138255/* 16 byte alignment seems to keep everyone happy */
    139 #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15)
     256#define TC_ALIGN16(s) (((s)+15)&~15)
     257#define TC_HDR_SIZE TC_ALIGN16(sizeof(struct talloc_chunk))
    140258#define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
    141259
    142 int talloc_version_major(void)
     260_PUBLIC_ int talloc_version_major(void)
    143261{
    144262        return TALLOC_VERSION_MAJOR;
    145263}
    146264
    147 int talloc_version_minor(void)
     265_PUBLIC_ int talloc_version_minor(void)
    148266{
    149267        return TALLOC_VERSION_MINOR;
     
    152270static void (*talloc_log_fn)(const char *message);
    153271
    154 void talloc_set_log_fn(void (*log_fn)(const char *message))
     272_PUBLIC_ void talloc_set_log_fn(void (*log_fn)(const char *message))
    155273{
    156274        talloc_log_fn = log_fn;
     
    180298}
    181299
    182 void talloc_set_log_stderr(void)
     300_PUBLIC_ void talloc_set_log_stderr(void)
    183301{
    184302        talloc_set_log_fn(talloc_log_stderr);
     
    187305static void (*talloc_abort_fn)(const char *reason);
    188306
    189 void talloc_set_abort_fn(void (*abort_fn)(const char *reason))
     307_PUBLIC_ void talloc_set_abort_fn(void (*abort_fn)(const char *reason))
    190308{
    191309        talloc_abort_fn = abort_fn;
     
    214332}
    215333
    216 static void talloc_abort_double_free(void)
    217 {
    218         talloc_abort("Bad talloc magic value - double free");
     334static void talloc_abort_access_after_free(void)
     335{
     336        talloc_abort("Bad talloc magic value - access after free");
    219337}
    220338
     
    236354
    237355                if (tc->flags & TALLOC_FLAG_FREE) {
    238                         talloc_log("talloc: double free error - first free may be at %s\n", tc->name);
    239                         talloc_abort_double_free();
     356                        talloc_log("talloc: access after free error - first free may be at %s\n", tc->name);
     357                        talloc_abort_access_after_free();
    240358                        return NULL;
    241359                } else {
     
    292410}
    293411
    294 void *talloc_parent(const void *ptr)
     412_PUBLIC_ void *talloc_parent(const void *ptr)
    295413{
    296414        struct talloc_chunk *tc = talloc_parent_chunk(ptr);
     
    301419  find parents name
    302420*/
    303 const char *talloc_parent_name(const void *ptr)
     421_PUBLIC_ const char *talloc_parent_name(const void *ptr)
    304422{
    305423        struct talloc_chunk *tc = talloc_parent_chunk(ptr);
     
    322440#define TALLOC_POOL_HDR_SIZE 16
    323441
     442#define TC_POOL_SPACE_LEFT(_pool_tc) \
     443        PTR_DIFF(TC_HDR_SIZE + (_pool_tc)->size + (char *)(_pool_tc), \
     444                 (_pool_tc)->pool)
     445
     446#define TC_POOL_FIRST_CHUNK(_pool_tc) \
     447        ((void *)(TC_HDR_SIZE + TALLOC_POOL_HDR_SIZE + (char *)(_pool_tc)))
     448
     449#define TC_POOLMEM_CHUNK_SIZE(_tc) \
     450        TC_ALIGN16(TC_HDR_SIZE + (_tc)->size)
     451
     452#define TC_POOLMEM_NEXT_CHUNK(_tc) \
     453        ((void *)(TC_POOLMEM_CHUNK_SIZE(tc) + (char*)(_tc)))
     454
     455/* Mark the whole remaining pool as not accessable */
     456#define TC_INVALIDATE_FILL_POOL(_pool_tc) do { \
     457        if (unlikely(talloc_fill.enabled)) { \
     458                size_t _flen = TC_POOL_SPACE_LEFT(_pool_tc); \
     459                char *_fptr = (char *)(_pool_tc)->pool; \
     460                memset(_fptr, talloc_fill.fill_value, _flen); \
     461        } \
     462} while(0)
     463
     464#if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
     465/* Mark the whole remaining pool as not accessable */
     466#define TC_INVALIDATE_VALGRIND_POOL(_pool_tc) do { \
     467        size_t _flen = TC_POOL_SPACE_LEFT(_pool_tc); \
     468        char *_fptr = (char *)(_pool_tc)->pool; \
     469        VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
     470} while(0)
     471#else
     472#define TC_INVALIDATE_VALGRIND_POOL(_pool_tc) do { } while (0)
     473#endif
     474
     475#define TC_INVALIDATE_POOL(_pool_tc) do { \
     476        TC_INVALIDATE_FILL_POOL(_pool_tc); \
     477        TC_INVALIDATE_VALGRIND_POOL(_pool_tc); \
     478} while (0)
     479
    324480static unsigned int *talloc_pool_objectcount(struct talloc_chunk *tc)
    325481{
    326         return (unsigned int *)((char *)tc + sizeof(struct talloc_chunk));
     482        return (unsigned int *)((char *)tc + TC_HDR_SIZE);
    327483}
    328484
     
    354510        }
    355511
    356         space_left = ((char *)pool_ctx + TC_HDR_SIZE + pool_ctx->size)
    357                 - ((char *)pool_ctx->pool);
     512        space_left = TC_POOL_SPACE_LEFT(pool_ctx);
    358513
    359514        /*
    360515         * Align size to 16 bytes
    361516         */
    362         chunk_size = ((size + 15) & ~15);
     517        chunk_size = TC_ALIGN16(size);
    363518
    364519        if (space_left < chunk_size) {
     
    439594 */
    440595
    441 void *talloc_pool(const void *context, size_t size)
     596_PUBLIC_ void *talloc_pool(const void *context, size_t size)
    442597{
    443598        void *result = __talloc(context, size + TALLOC_POOL_HDR_SIZE);
     
    451606
    452607        tc->flags |= TALLOC_FLAG_POOL;
    453         tc->pool = (char *)result + TALLOC_POOL_HDR_SIZE;
     608        tc->pool = TC_POOL_FIRST_CHUNK(tc);
    454609
    455610        *talloc_pool_objectcount(tc) = 1;
    456611
    457 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
    458         VALGRIND_MAKE_MEM_NOACCESS(tc->pool, size);
    459 #endif
     612        TC_INVALIDATE_POOL(tc);
    460613
    461614        return result;
     
    468621  be continued to be used
    469622*/
    470 void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
     623_PUBLIC_ void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
    471624{
    472625        struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
     
    477630  increase the reference count on a piece of memory.
    478631*/
    479 int talloc_increase_ref_count(const void *ptr)
     632_PUBLIC_ int talloc_increase_ref_count(const void *ptr)
    480633{
    481634        if (unlikely(!talloc_reference(null_context, ptr))) {
     
    533686  and in either order
    534687*/
    535 void *_talloc_reference_loc(const void *context, const void *ptr, const char *location)
     688_PUBLIC_ void *_talloc_reference_loc(const void *context, const void *ptr, const char *location)
    536689{
    537690        struct talloc_chunk *tc;
     
    557710static void *_talloc_steal_internal(const void *new_ctx, const void *ptr);
    558711
     712static inline void _talloc_free_poolmem(struct talloc_chunk *tc,
     713                                        const char *location)
     714{
     715        struct talloc_chunk *pool;
     716        void *next_tc;
     717        unsigned int *pool_object_count;
     718
     719        pool = (struct talloc_chunk *)tc->pool;
     720        next_tc = TC_POOLMEM_NEXT_CHUNK(tc);
     721
     722        tc->flags |= TALLOC_FLAG_FREE;
     723
     724        /* we mark the freed memory with where we called the free
     725         * from. This means on a double free error we can report where
     726         * the first free came from
     727         */
     728        tc->name = location;
     729
     730        TC_INVALIDATE_FULL_CHUNK(tc);
     731
     732        pool_object_count = talloc_pool_objectcount(pool);
     733
     734        if (unlikely(*pool_object_count == 0)) {
     735                talloc_abort("Pool object count zero!");
     736                return;
     737        }
     738
     739        *pool_object_count -= 1;
     740
     741        if (unlikely(*pool_object_count == 1 && !(pool->flags & TALLOC_FLAG_FREE))) {
     742                /*
     743                 * if there is just one object left in the pool
     744                 * and pool->flags does not have TALLOC_FLAG_FREE,
     745                 * it means this is the pool itself and
     746                 * the rest is available for new objects
     747                 * again.
     748                 */
     749                pool->pool = TC_POOL_FIRST_CHUNK(pool);
     750                TC_INVALIDATE_POOL(pool);
     751        } else if (unlikely(*pool_object_count == 0)) {
     752                /*
     753                 * we mark the freed memory with where we called the free
     754                 * from. This means on a double free error we can report where
     755                 * the first free came from
     756                 */
     757                pool->name = location;
     758
     759                TC_INVALIDATE_FULL_CHUNK(pool);
     760                free(pool);
     761        } else if (pool->pool == next_tc) {
     762                /*
     763                 * if pool->pool still points to end of
     764                 * 'tc' (which is stored in the 'next_tc' variable),
     765                 * we can reclaim the memory of 'tc'.
     766                 */
     767                pool->pool = tc;
     768        }
     769}
     770
     771static inline void _talloc_free_children_internal(struct talloc_chunk *tc,
     772                                                  void *ptr,
     773                                                  const char *location);
     774
    559775/*
    560776   internal talloc_free call
     
    568784        }
    569785
     786        /* possibly initialised the talloc fill value */
     787        if (unlikely(!talloc_fill.initialised)) {
     788                const char *fill = getenv(TALLOC_FILL_ENV);
     789                if (fill != NULL) {
     790                        talloc_fill.enabled = true;
     791                        talloc_fill.fill_value = strtoul(fill, NULL, 0);
     792                }
     793                talloc_fill.initialised = true;
     794        }
     795
    570796        tc = talloc_chunk_from_ptr(ptr);
    571797
    572798        if (unlikely(tc->refs)) {
    573799                int is_child;
    574                 /* check this is a reference from a child or grantchild
    575                  * back to it's parent or grantparent
     800                /* check if this is a reference from a child or
     801                 * grandchild back to it's parent or grandparent
    576802                 *
    577803                 * in that case we need to remove the reference and
     
    617843        tc->flags |= TALLOC_FLAG_LOOP;
    618844
    619         while (tc->child) {
    620                 /* we need to work out who will own an abandoned child
    621                    if it cannot be freed. In priority order, the first
    622                    choice is owner of any remaining reference to this
    623                    pointer, the second choice is our parent, and the
    624                    final choice is the null context. */
    625                 void *child = TC_PTR_FROM_CHUNK(tc->child);
    626                 const void *new_parent = null_context;
    627                 if (unlikely(tc->child->refs)) {
    628                         struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
    629                         if (p) new_parent = TC_PTR_FROM_CHUNK(p);
    630                 }
    631                 if (unlikely(_talloc_free_internal(child, location) == -1)) {
    632                         if (new_parent == null_context) {
    633                                 struct talloc_chunk *p = talloc_parent_chunk(ptr);
    634                                 if (p) new_parent = TC_PTR_FROM_CHUNK(p);
    635                         }
    636                         _talloc_steal_internal(new_parent, child);
    637                 }
    638         }
     845        _talloc_free_children_internal(tc, ptr, location);
    639846
    640847        tc->flags |= TALLOC_FLAG_FREE;
     
    646853        tc->name = location;
    647854
    648         if (tc->flags & (TALLOC_FLAG_POOL|TALLOC_FLAG_POOLMEM)) {
    649                 struct talloc_chunk *pool;
     855        if (tc->flags & TALLOC_FLAG_POOL) {
    650856                unsigned int *pool_object_count;
    651857
    652                 pool = (tc->flags & TALLOC_FLAG_POOL)
    653                         ? tc : (struct talloc_chunk *)tc->pool;
    654 
    655                 pool_object_count = talloc_pool_objectcount(pool);
    656 
    657                 if (*pool_object_count == 0) {
     858                pool_object_count = talloc_pool_objectcount(tc);
     859
     860                if (unlikely(*pool_object_count == 0)) {
    658861                        talloc_abort("Pool object count zero!");
    659862                        return 0;
     
    662865                *pool_object_count -= 1;
    663866
    664                 if (*pool_object_count == 0) {
    665                         free(pool);
    666                 }
    667         }
    668         else {
     867                if (unlikely(*pool_object_count == 0)) {
     868                        TC_INVALIDATE_FULL_CHUNK(tc);
     869                        free(tc);
     870                }
     871        } else if (tc->flags & TALLOC_FLAG_POOLMEM) {
     872                _talloc_free_poolmem(tc, location);
     873        } else {
     874                TC_INVALIDATE_FULL_CHUNK(tc);
    669875                free(tc);
    670876        }
     
    734940   passing NULL as ptr will always return NULL with no side effects.
    735941*/
    736 void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location)
     942_PUBLIC_ void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location)
    737943{
    738944        struct talloc_chunk *tc;
     
    755961                }
    756962        }
     963
     964#if 0
     965        /* this test is probably too expensive to have on in the
     966           normal build, but it useful for debugging */
     967        if (talloc_is_parent(new_ctx, ptr)) {
     968                talloc_log("WARNING: stealing into talloc child at %s\n", location);
     969        }
     970#endif
    757971       
    758972        return _talloc_steal_internal(new_ctx, ptr);
     
    766980   The old parent can be either a reference or a parent
    767981*/
    768 void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr)
     982_PUBLIC_ void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr)
    769983{
    770984        struct talloc_chunk *tc;
     
    8261040  controlled varient of talloc_free()
    8271041*/
    828 int talloc_unlink(const void *context, void *ptr)
     1042_PUBLIC_ int talloc_unlink(const void *context, void *ptr)
    8291043{
    8301044        struct talloc_chunk *tc_p, *new_p;
     
    8931107  add a name to an existing pointer
    8941108*/
    895 const char *talloc_set_name(const void *ptr, const char *fmt, ...)
     1109_PUBLIC_ const char *talloc_set_name(const void *ptr, const char *fmt, ...)
    8961110{
    8971111        const char *name;
     
    9091123  to name the pointer.
    9101124*/
    911 void *talloc_named(const void *context, size_t size, const char *fmt, ...)
     1125_PUBLIC_ void *talloc_named(const void *context, size_t size, const char *fmt, ...)
    9121126{
    9131127        va_list ap;
     
    9331147  return the name of a talloc ptr, or "UNNAMED"
    9341148*/
    935 const char *talloc_get_name(const void *ptr)
     1149_PUBLIC_ const char *talloc_get_name(const void *ptr)
    9361150{
    9371151        struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
     
    9501164  otherwise return NULL
    9511165*/
    952 void *talloc_check_name(const void *ptr, const char *name)
     1166_PUBLIC_ void *talloc_check_name(const void *ptr, const char *name)
    9531167{
    9541168        const char *pname;
     
    9791193}
    9801194
    981 void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location)
     1195_PUBLIC_ void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location)
    9821196{
    9831197        const char *pname;
     
    10001214  this is for compatibility with older versions of talloc
    10011215*/
    1002 void *talloc_init(const char *fmt, ...)
     1216_PUBLIC_ void *talloc_init(const char *fmt, ...)
    10031217{
    10041218        va_list ap;
     
    10061220        const char *name;
    10071221
    1008         /*
    1009          * samba3 expects talloc_report_depth_cb(NULL, ...)
    1010          * reports all talloc'ed memory, so we need to enable
    1011          * null_tracking
    1012          */
    1013         talloc_enable_null_tracking();
    1014 
    10151222        ptr = __talloc(NULL, 0);
    10161223        if (unlikely(ptr == NULL)) return NULL;
     
    10281235}
    10291236
    1030 /*
    1031   this is a replacement for the Samba3 talloc_destroy_pool functionality. It
    1032   should probably not be used in new code. It's in here to keep the talloc
    1033   code consistent across Samba 3 and 4.
    1034 */
    1035 void talloc_free_children(void *ptr)
    1036 {
    1037         struct talloc_chunk *tc;
    1038 
    1039         if (unlikely(ptr == NULL)) {
    1040                 return;
    1041         }
    1042 
    1043         tc = talloc_chunk_from_ptr(ptr);
    1044 
     1237static inline void _talloc_free_children_internal(struct talloc_chunk *tc,
     1238                                                  void *ptr,
     1239                                                  const char *location)
     1240{
    10451241        while (tc->child) {
    10461242                /* we need to work out who will own an abandoned child
     
    10511247                void *child = TC_PTR_FROM_CHUNK(tc->child);
    10521248                const void *new_parent = null_context;
     1249                struct talloc_chunk *old_parent = NULL;
    10531250                if (unlikely(tc->child->refs)) {
    10541251                        struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
    10551252                        if (p) new_parent = TC_PTR_FROM_CHUNK(p);
    10561253                }
    1057                 if (unlikely(talloc_free(child) == -1)) {
     1254                /* finding the parent here is potentially quite
     1255                   expensive, but the alternative, which is to change
     1256                   talloc to always have a valid tc->parent pointer,
     1257                   makes realloc more expensive where there are a
     1258                   large number of children.
     1259
     1260                   The reason we need the parent pointer here is that
     1261                   if _talloc_free_internal() fails due to references
     1262                   or a failing destructor we need to re-parent, but
     1263                   the free call can invalidate the prev pointer.
     1264                */
     1265                if (new_parent == null_context && (tc->child->refs || tc->child->destructor)) {
     1266                        old_parent = talloc_parent_chunk(ptr);
     1267                }
     1268                if (unlikely(_talloc_free_internal(child, location) == -1)) {
    10581269                        if (new_parent == null_context) {
    1059                                 struct talloc_chunk *p = talloc_parent_chunk(ptr);
     1270                                struct talloc_chunk *p = old_parent;
    10601271                                if (p) new_parent = TC_PTR_FROM_CHUNK(p);
    10611272                        }
     
    10631274                }
    10641275        }
    1065 
    1066         if ((tc->flags & TALLOC_FLAG_POOL)
    1067             && (*talloc_pool_objectcount(tc) == 1)) {
    1068                 tc->pool = ((char *)tc + TC_HDR_SIZE + TALLOC_POOL_HDR_SIZE);
    1069 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
    1070                 VALGRIND_MAKE_MEM_NOACCESS(
    1071                         tc->pool, tc->size - TALLOC_POOL_HDR_SIZE);
    1072 #endif
    1073         }
     1276}
     1277
     1278/*
     1279  this is a replacement for the Samba3 talloc_destroy_pool functionality. It
     1280  should probably not be used in new code. It's in here to keep the talloc
     1281  code consistent across Samba 3 and 4.
     1282*/
     1283_PUBLIC_ void talloc_free_children(void *ptr)
     1284{
     1285        struct talloc_chunk *tc;
     1286
     1287        if (unlikely(ptr == NULL)) {
     1288                return;
     1289        }
     1290
     1291        tc = talloc_chunk_from_ptr(ptr);
     1292
     1293        _talloc_free_children_internal(tc, ptr, __location__);
    10741294}
    10751295
     
    10771297   Allocate a bit of memory as a child of an existing pointer
    10781298*/
    1079 void *_talloc(const void *context, size_t size)
     1299_PUBLIC_ void *_talloc(const void *context, size_t size)
    10801300{
    10811301        return __talloc(context, size);
     
    10851305  externally callable talloc_set_name_const()
    10861306*/
    1087 void talloc_set_name_const(const void *ptr, const char *name)
     1307_PUBLIC_ void talloc_set_name_const(const void *ptr, const char *name)
    10881308{
    10891309        _talloc_set_name_const(ptr, name);
     
    10951315  to name the pointer.
    10961316*/
    1097 void *talloc_named_const(const void *context, size_t size, const char *name)
     1317_PUBLIC_ void *talloc_named_const(const void *context, size_t size, const char *name)
    10981318{
    10991319        return _talloc_named_const(context, size, name);
     
    11081328   any) returns non-zero
    11091329*/
    1110 int _talloc_free(void *ptr, const char *location)
     1330_PUBLIC_ int _talloc_free(void *ptr, const char *location)
    11111331{
    11121332        struct talloc_chunk *tc;
     
    11211341                struct talloc_reference_handle *h;
    11221342
     1343                if (talloc_parent(ptr) == null_context && tc->refs->next == NULL) {
     1344                        /* in this case we do know which parent should
     1345                           get this pointer, as there is really only
     1346                           one parent */
     1347                        return talloc_unlink(null_context, ptr);
     1348                }
     1349
    11231350                talloc_log("ERROR: talloc_free with references at %s\n",
    11241351                           location);
     
    11401367  ptr is NULL
    11411368*/
    1142 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
     1369_PUBLIC_ void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
    11431370{
    11441371        struct talloc_chunk *tc;
    11451372        void *new_ptr;
    11461373        bool malloced = false;
     1374        struct talloc_chunk *pool_tc = NULL;
    11471375
    11481376        /* size zero is equivalent to free() */
     
    11731401        }
    11741402
     1403        /* don't let anybody try to realloc a talloc_pool */
     1404        if (unlikely(tc->flags & TALLOC_FLAG_POOLMEM)) {
     1405                pool_tc = (struct talloc_chunk *)tc->pool;
     1406        }
     1407
     1408#if (ALWAYS_REALLOC == 0)
    11751409        /* don't shrink if we have less than 1k to gain */
    1176         if ((size < tc->size) && ((tc->size - size) < 1024)) {
    1177                 tc->size = size;
     1410        if (size < tc->size) {
     1411                if (pool_tc) {
     1412                        void *next_tc = TC_POOLMEM_NEXT_CHUNK(tc);
     1413                        TC_INVALIDATE_SHRINK_CHUNK(tc, size);
     1414                        tc->size = size;
     1415                        if (next_tc == pool_tc->pool) {
     1416                                pool_tc->pool = TC_POOLMEM_NEXT_CHUNK(tc);
     1417                        }
     1418                        return ptr;
     1419                } else if ((tc->size - size) < 1024) {
     1420                        /*
     1421                         * if we call TC_INVALIDATE_SHRINK_CHUNK() here
     1422                         * we would need to call TC_UNDEFINE_GROW_CHUNK()
     1423                         * after each realloc call, which slows down
     1424                         * testing a lot :-(.
     1425                         *
     1426                         * That is why we only mark memory as undefined here.
     1427                         */
     1428                        TC_UNDEFINE_SHRINK_CHUNK(tc, size);
     1429
     1430                        /* do not shrink if we have less than 1k to gain */
     1431                        tc->size = size;
     1432                        return ptr;
     1433                }
     1434        } else if (tc->size == size) {
     1435                /*
     1436                 * do not change the pointer if it is exactly
     1437                 * the same size.
     1438                 */
    11781439                return ptr;
    11791440        }
     1441#endif
    11801442
    11811443        /* by resetting magic we catch users of the old memory */
     
    11831445
    11841446#if ALWAYS_REALLOC
    1185         new_ptr = malloc(size + TC_HDR_SIZE);
    1186         if (new_ptr) {
    1187                 memcpy(new_ptr, tc, MIN(tc->size, size) + TC_HDR_SIZE);
    1188                 free(tc);
    1189         }
    1190 #else
    1191         if (tc->flags & TALLOC_FLAG_POOLMEM) {
    1192 
     1447        if (pool_tc) {
    11931448                new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
    1194                 *talloc_pool_objectcount((struct talloc_chunk *)
    1195                                          (tc->pool)) -= 1;
     1449                *talloc_pool_objectcount(pool_tc) -= 1;
    11961450
    11971451                if (new_ptr == NULL) {
     
    12021456                if (new_ptr) {
    12031457                        memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
     1458                        TC_INVALIDATE_FULL_CHUNK(tc);
     1459                }
     1460        } else {
     1461                new_ptr = malloc(size + TC_HDR_SIZE);
     1462                if (new_ptr) {
     1463                        memcpy(new_ptr, tc, MIN(tc->size, size) + TC_HDR_SIZE);
     1464                        free(tc);
     1465                }
     1466        }
     1467#else
     1468        if (pool_tc) {
     1469                void *next_tc = TC_POOLMEM_NEXT_CHUNK(tc);
     1470                size_t old_chunk_size = TC_POOLMEM_CHUNK_SIZE(tc);
     1471                size_t new_chunk_size = TC_ALIGN16(TC_HDR_SIZE + size);
     1472                size_t space_needed;
     1473                size_t space_left;
     1474                unsigned int chunk_count = *talloc_pool_objectcount(pool_tc);
     1475
     1476                if (!(pool_tc->flags & TALLOC_FLAG_FREE)) {
     1477                        chunk_count -= 1;
     1478                }
     1479
     1480                if (chunk_count == 1) {
     1481                        /*
     1482                         * optimize for the case where 'tc' is the only
     1483                         * chunk in the pool.
     1484                         */
     1485                        space_needed = new_chunk_size;
     1486                        space_left = pool_tc->size - TALLOC_POOL_HDR_SIZE;
     1487
     1488                        if (space_left >= space_needed) {
     1489                                size_t old_used = TC_HDR_SIZE + tc->size;
     1490                                size_t new_used = TC_HDR_SIZE + size;
     1491                                pool_tc->pool = TC_POOL_FIRST_CHUNK(pool_tc);
     1492#if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
     1493                                /*
     1494                                 * we need to prepare the memmove into
     1495                                 * the unaccessable area.
     1496                                 */
     1497                                {
     1498                                        size_t diff = PTR_DIFF(tc, pool_tc->pool);
     1499                                        size_t flen = MIN(diff, old_used);
     1500                                        char *fptr = (char *)pool_tc->pool;
     1501                                        VALGRIND_MAKE_MEM_UNDEFINED(fptr, flen);
     1502                                }
     1503#endif
     1504                                memmove(pool_tc->pool, tc, old_used);
     1505                                new_ptr = pool_tc->pool;
     1506
     1507                                tc = (struct talloc_chunk *)new_ptr;
     1508                                TC_UNDEFINE_GROW_CHUNK(tc, size);
     1509
     1510                                /*
     1511                                 * first we do not align the pool pointer
     1512                                 * because we want to invalidate the padding
     1513                                 * too.
     1514                                 */
     1515                                pool_tc->pool = new_used + (char *)new_ptr;
     1516                                TC_INVALIDATE_POOL(pool_tc);
     1517
     1518                                /* now the aligned pointer */
     1519                                pool_tc->pool = new_chunk_size + (char *)new_ptr;
     1520                                goto got_new_ptr;
     1521                        }
     1522
     1523                        next_tc = NULL;
     1524                }
     1525
     1526                if (new_chunk_size == old_chunk_size) {
     1527                        TC_UNDEFINE_GROW_CHUNK(tc, size);
     1528                        tc->flags &= ~TALLOC_FLAG_FREE;
     1529                        tc->size = size;
     1530                        return ptr;
     1531                }
     1532
     1533                if (next_tc == pool_tc->pool) {
     1534                        /*
     1535                         * optimize for the case where 'tc' is the last
     1536                         * chunk in the pool.
     1537                         */
     1538                        space_needed = new_chunk_size - old_chunk_size;
     1539                        space_left = TC_POOL_SPACE_LEFT(pool_tc);
     1540
     1541                        if (space_left >= space_needed) {
     1542                                TC_UNDEFINE_GROW_CHUNK(tc, size);
     1543                                tc->flags &= ~TALLOC_FLAG_FREE;
     1544                                tc->size = size;
     1545                                pool_tc->pool = TC_POOLMEM_NEXT_CHUNK(tc);
     1546                                return ptr;
     1547                        }
     1548                }
     1549
     1550                new_ptr = talloc_alloc_pool(tc, size + TC_HDR_SIZE);
     1551
     1552                if (new_ptr == NULL) {
     1553                        new_ptr = malloc(TC_HDR_SIZE+size);
     1554                        malloced = true;
     1555                }
     1556
     1557                if (new_ptr) {
     1558                        memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
     1559
     1560                        _talloc_free_poolmem(tc, __location__ "_talloc_realloc");
    12041561                }
    12051562        }
     
    12071564                new_ptr = realloc(tc, size + TC_HDR_SIZE);
    12081565        }
     1566got_new_ptr:
    12091567#endif
    12101568        if (unlikely(!new_ptr)) {       
     
    12421600  between two structures, and want the old pointer to be set to NULL
    12431601*/
    1244 void *_talloc_move(const void *new_ctx, const void *_pptr)
     1602_PUBLIC_ void *_talloc_move(const void *new_ctx, const void *_pptr)
    12451603{
    12461604        const void **pptr = discard_const_p(const void *,_pptr);
     
    12531611  return the total size of a talloc pool (subtree)
    12541612*/
    1255 size_t talloc_total_size(const void *ptr)
     1613_PUBLIC_ size_t talloc_total_size(const void *ptr)
    12561614{
    12571615        size_t total = 0;
     
    12881646  return the total number of blocks in a talloc pool (subtree)
    12891647*/
    1290 size_t talloc_total_blocks(const void *ptr)
     1648_PUBLIC_ size_t talloc_total_blocks(const void *ptr)
    12911649{
    12921650        size_t total = 0;
     
    13211679  return the number of external references to a pointer
    13221680*/
    1323 size_t talloc_reference_count(const void *ptr)
     1681_PUBLIC_ size_t talloc_reference_count(const void *ptr)
    13241682{
    13251683        struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
     
    13361694  report on memory usage by all children of a pointer, giving a full tree view
    13371695*/
    1338 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
     1696_PUBLIC_ void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
    13391697                            void (*callback)(const void *ptr,
    13401698                                             int depth, int max_depth,
     
    14201778  report on memory usage by all children of a pointer, giving a full tree view
    14211779*/
    1422 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
     1780_PUBLIC_ void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
    14231781{
    14241782        if (f) {
     
    14311789  report on memory usage by all children of a pointer, giving a full tree view
    14321790*/
    1433 void talloc_report_full(const void *ptr, FILE *f)
     1791_PUBLIC_ void talloc_report_full(const void *ptr, FILE *f)
    14341792{
    14351793        talloc_report_depth_file(ptr, 0, -1, f);
     
    14391797  report on memory usage by all children of a pointer
    14401798*/
    1441 void talloc_report(const void *ptr, FILE *f)
     1799_PUBLIC_ void talloc_report(const void *ptr, FILE *f)
    14421800{
    14431801        talloc_report_depth_file(ptr, 0, 1, f);
     
    14671825  enable tracking of the NULL context
    14681826*/
    1469 void talloc_enable_null_tracking(void)
     1827_PUBLIC_ void talloc_enable_null_tracking(void)
    14701828{
    14711829        if (null_context == NULL) {
     
    14811839  into the NULL context. This is needed for the talloc testsuite
    14821840*/
    1483 void talloc_enable_null_tracking_no_autofree(void)
     1841_PUBLIC_ void talloc_enable_null_tracking_no_autofree(void)
    14841842{
    14851843        if (null_context == NULL) {
     
    14911849  disable tracking of the NULL context
    14921850*/
    1493 void talloc_disable_null_tracking(void)
     1851_PUBLIC_ void talloc_disable_null_tracking(void)
    14941852{
    14951853        if (null_context != NULL) {
     
    15161874  enable leak reporting on exit
    15171875*/
    1518 void talloc_enable_leak_report(void)
     1876_PUBLIC_ void talloc_enable_leak_report(void)
    15191877{
    15201878        talloc_enable_null_tracking();
     
    15251883  enable full leak reporting on exit
    15261884*/
    1527 void talloc_enable_leak_report_full(void)
     1885_PUBLIC_ void talloc_enable_leak_report_full(void)
    15281886{
    15291887        talloc_enable_null_tracking();
     
    15341892   talloc and zero memory.
    15351893*/
    1536 void *_talloc_zero(const void *ctx, size_t size, const char *name)
     1894_PUBLIC_ void *_talloc_zero(const void *ctx, size_t size, const char *name)
    15371895{
    15381896        void *p = _talloc_named_const(ctx, size, name);
     
    15481906  memdup with a talloc.
    15491907*/
    1550 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
     1908_PUBLIC_ void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
    15511909{
    15521910        void *newp = _talloc_named_const(t, size, name);
     
    15761934  strdup with a talloc
    15771935*/
    1578 char *talloc_strdup(const void *t, const char *p)
     1936_PUBLIC_ char *talloc_strdup(const void *t, const char *p)
    15791937{
    15801938        if (unlikely(!p)) return NULL;
     
    15851943  strndup with a talloc
    15861944*/
    1587 char *talloc_strndup(const void *t, const char *p, size_t n)
     1945_PUBLIC_ char *talloc_strndup(const void *t, const char *p, size_t n)
    15881946{
    15891947        if (unlikely(!p)) return NULL;
     
    16101968 * Appends at the end of the string.
    16111969 */
    1612 char *talloc_strdup_append(char *s, const char *a)
     1970_PUBLIC_ char *talloc_strdup_append(char *s, const char *a)
    16131971{
    16141972        if (unlikely(!s)) {
     
    16271985 * not the end of the string.
    16281986 */
    1629 char *talloc_strdup_append_buffer(char *s, const char *a)
     1987_PUBLIC_ char *talloc_strdup_append_buffer(char *s, const char *a)
    16301988{
    16311989        size_t slen;
     
    16502008 * Appends at the end of the string.
    16512009 */
    1652 char *talloc_strndup_append(char *s, const char *a, size_t n)
     2010_PUBLIC_ char *talloc_strndup_append(char *s, const char *a, size_t n)
    16532011{
    16542012        if (unlikely(!s)) {
     
    16672025 * not the end of the string.
    16682026 */
    1669 char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
     2027_PUBLIC_ char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
    16702028{
    16712029        size_t slen;
     
    16952053#endif
    16962054
    1697 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
     2055_PUBLIC_ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
    16982056{
    16992057        int len;
     
    17262084  memory holding the result, inside a memory pool.
    17272085 */
    1728 char *talloc_asprintf(const void *t, const char *fmt, ...)
     2086_PUBLIC_ char *talloc_asprintf(const void *t, const char *fmt, ...)
    17292087{
    17302088        va_list ap;
     
    17792137 * of the string.
    17802138 **/
    1781 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
     2139_PUBLIC_ char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
    17822140{
    17832141        if (unlikely(!s)) {
     
    17932151 * end of the talloc'ed buffer, not the end of the string.
    17942152 **/
    1795 char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
     2153_PUBLIC_ char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
    17962154{
    17972155        size_t slen;
     
    18142172  into a string buffer.
    18152173 */
    1816 char *talloc_asprintf_append(char *s, const char *fmt, ...)
     2174_PUBLIC_ char *talloc_asprintf_append(char *s, const char *fmt, ...)
    18172175{
    18182176        va_list ap;
     
    18292187  into a buffer.
    18302188 */
    1831 char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
     2189_PUBLIC_ char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
    18322190{
    18332191        va_list ap;
     
    18422200  alloc an array, checking for integer overflow in the array size
    18432201*/
    1844 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
     2202_PUBLIC_ void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
    18452203{
    18462204        if (count >= MAX_TALLOC_SIZE/el_size) {
     
    18532211  alloc an zero array, checking for integer overflow in the array size
    18542212*/
    1855 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
     2213_PUBLIC_ void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
    18562214{
    18572215        if (count >= MAX_TALLOC_SIZE/el_size) {
     
    18642222  realloc an array, checking for integer overflow in the array size
    18652223*/
    1866 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
     2224_PUBLIC_ void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
    18672225{
    18682226        if (count >= MAX_TALLOC_SIZE/el_size) {
     
    18772235  all the basic capabilities of an allocation library, which is why this is useful)
    18782236*/
    1879 void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
     2237_PUBLIC_ void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
    18802238{
    18812239        return _talloc_realloc(context, ptr, size, NULL);
     
    18982256  this is useful for reducing the noise in leak reports
    18992257*/
    1900 void *talloc_autofree_context(void)
     2258_PUBLIC_ void *talloc_autofree_context(void)
    19012259{
    19022260        if (autofree_context == NULL) {
     
    19082266}
    19092267
    1910 size_t talloc_get_size(const void *context)
     2268_PUBLIC_ size_t talloc_get_size(const void *context)
    19112269{
    19122270        struct talloc_chunk *tc;
     
    19272285  find a parent of this context that has the given name, if any
    19282286*/
    1929 void *talloc_find_parent_byname(const void *context, const char *name)
     2287_PUBLIC_ void *talloc_find_parent_byname(const void *context, const char *name)
    19302288{
    19312289        struct talloc_chunk *tc;
     
    19512309  show the parentage of a context
    19522310*/
    1953 void talloc_show_parents(const void *context, FILE *file)
     2311_PUBLIC_ void talloc_show_parents(const void *context, FILE *file)
    19542312{
    19552313        struct talloc_chunk *tc;
     
    19752333  return 1 if ptr is a parent of context
    19762334*/
    1977 int talloc_is_parent(const void *context, const void *ptr)
     2335static int _talloc_is_parent(const void *context, const void *ptr, int depth)
    19782336{
    19792337        struct talloc_chunk *tc;
     
    19842342
    19852343        tc = talloc_chunk_from_ptr(context);
    1986         while (tc) {
     2344        while (tc && depth > 0) {
    19872345                if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
    19882346                while (tc && tc->prev) tc = tc->prev;
    19892347                if (tc) {
    19902348                        tc = tc->parent;
     2349                        depth--;
    19912350                }
    19922351        }
    19932352        return 0;
    19942353}
     2354
     2355/*
     2356  return 1 if ptr is a parent of context
     2357*/
     2358_PUBLIC_ int talloc_is_parent(const void *context, const void *ptr)
     2359{
     2360        return _talloc_is_parent(context, ptr, TALLOC_MAX_DEPTH);
     2361}
  • vendor/current/lib/talloc/talloc.h

    r414 r740  
    3030#include <stdarg.h>
    3131
     32#ifdef __cplusplus
     33extern "C" {
     34#endif
     35
     36/**
     37 * @defgroup talloc The talloc API
     38 *
     39 * talloc is a hierarchical, reference counted memory pool system with
     40 * destructors. It is the core memory allocator used in Samba.
     41 *
     42 * @{
     43 */
     44
    3245#define TALLOC_VERSION_MAJOR 2
    3346#define TALLOC_VERSION_MINOR 0
     
    3649int talloc_version_minor(void);
    3750
    38 /* this is only needed for compatibility with the old talloc */
     51/**
     52 * @brief Define a talloc parent type
     53 *
     54 * As talloc is a hierarchial memory allocator, every talloc chunk is a
     55 * potential parent to other talloc chunks. So defining a separate type for a
     56 * talloc chunk is not strictly necessary. TALLOC_CTX is defined nevertheless,
     57 * as it provides an indicator for function arguments. You will frequently
     58 * write code like
     59 *
     60 * @code
     61 *      struct foo *foo_create(TALLOC_CTX *mem_ctx)
     62 *      {
     63 *              struct foo *result;
     64 *              result = talloc(mem_ctx, struct foo);
     65 *              if (result == NULL) return NULL;
     66 *                      ... initialize foo ...
     67 *              return result;
     68 *      }
     69 * @endcode
     70 *
     71 * In this type of allocating functions it is handy to have a general
     72 * TALLOC_CTX type to indicate which parent to put allocated structures on.
     73 */
    3974typedef void TALLOC_CTX;
    4075
     
    65100#endif
    66101
     102#ifdef DOXYGEN
     103/**
     104 * @brief Create a new talloc context.
     105 *
     106 * The talloc() macro is the core of the talloc library. It takes a memory
     107 * context and a type, and returns a pointer to a new area of memory of the
     108 * given type.
     109 *
     110 * The returned pointer is itself a talloc context, so you can use it as the
     111 * context argument to more calls to talloc if you wish.
     112 *
     113 * The returned pointer is a "child" of the supplied context. This means that if
     114 * you talloc_free() the context then the new child disappears as well.
     115 * Alternatively you can free just the child.
     116 *
     117 * @param[in]  ctx      A talloc context to create a new reference on or NULL to
     118 *                      create a new top level context.
     119 *
     120 * @param[in]  type     The type of memory to allocate.
     121 *
     122 * @return              A type casted talloc context or NULL on error.
     123 *
     124 * @code
     125 *      unsigned int *a, *b;
     126 *
     127 *      a = talloc(NULL, unsigned int);
     128 *      b = talloc(a, unsigned int);
     129 * @endcode
     130 *
     131 * @see talloc_zero
     132 * @see talloc_array
     133 * @see talloc_steal
     134 * @see talloc_free
     135 */
     136void *talloc(const void *ctx, #type);
     137#else
     138#define talloc(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type)
     139void *_talloc(const void *context, size_t size);
     140#endif
     141
     142/**
     143 * @brief Create a new top level talloc context.
     144 *
     145 * This function creates a zero length named talloc context as a top level
     146 * context. It is equivalent to:
     147 *
     148 * @code
     149 *      talloc_named(NULL, 0, fmt, ...);
     150 * @endcode
     151 * @param[in]  fmt      Format string for the name.
     152 *
     153 * @param[in]  ...      Additional printf-style arguments.
     154 *
     155 * @return              The allocated memory chunk, NULL on error.
     156 *
     157 * @see talloc_named()
     158 */
     159void *talloc_init(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
     160
     161#ifdef DOXYGEN
     162/**
     163 * @brief Free a chunk of talloc memory.
     164 *
     165 * The talloc_free() function frees a piece of talloc memory, and all its
     166 * children. You can call talloc_free() on any pointer returned by
     167 * talloc().
     168 *
     169 * The return value of talloc_free() indicates success or failure, with 0
     170 * returned for success and -1 for failure. A possible failure condition
     171 * is if the pointer had a destructor attached to it and the destructor
     172 * returned -1. See talloc_set_destructor() for details on
     173 * destructors. Likewise, if "ptr" is NULL, then the function will make
     174 * no modifications and return -1.
     175 *
     176 * If this pointer has an additional parent when talloc_free() is called
     177 * then the memory is not actually released, but instead the most
     178 * recently established parent is destroyed. See talloc_reference() for
     179 * details on establishing additional parents.
     180 *
     181 * For more control on which parent is removed, see talloc_unlink()
     182 *
     183 * talloc_free() operates recursively on its children.
     184 *
     185 * From the 2.0 version of talloc, as a special case, talloc_free() is
     186 * refused on pointers that have more than one parent, as talloc would
     187 * have no way of knowing which parent should be removed. To free a
     188 * pointer that has more than one parent please use talloc_unlink().
     189 *
     190 * To help you find problems in your code caused by this behaviour, if
     191 * you do try and free a pointer with more than one parent then the
     192 * talloc logging function will be called to give output like this:
     193 *
     194 * @code
     195 *   ERROR: talloc_free with references at some_dir/source/foo.c:123
     196 *     reference at some_dir/source/other.c:325
     197 *     reference at some_dir/source/third.c:121
     198 * @endcode
     199 *
     200 * Please see the documentation for talloc_set_log_fn() and
     201 * talloc_set_log_stderr() for more information on talloc logging
     202 * functions.
     203 *
     204 * @param[in]  ptr      The chunk to be freed.
     205 *
     206 * @return              Returns 0 on success and -1 on error. A possible
     207 *                      failure condition is if the pointer had a destructor
     208 *                      attached to it and the destructor returned -1. Likewise,
     209 *                      if "ptr" is NULL, then the function will make no
     210 *                      modifications and returns -1.
     211 *
     212 * Example:
     213 * @code
     214 *      unsigned int *a, *b;
     215 *      a = talloc(NULL, unsigned int);
     216 *      b = talloc(a, unsigned int);
     217 *
     218 *      talloc_free(a); // Frees a and b
     219 * @endcode
     220 *
     221 * @see talloc_set_destructor()
     222 * @see talloc_unlink()
     223 */
     224int talloc_free(void *ptr);
     225#else
     226#define talloc_free(ctx) _talloc_free(ctx, __location__)
     227int _talloc_free(void *ptr, const char *location);
     228#endif
     229
     230/**
     231 * @brief Free a talloc chunk's children.
     232 *
     233 * The function walks along the list of all children of a talloc context and
     234 * talloc_free()s only the children, not the context itself.
     235 *
     236 * @param[in]  ptr      The chunk that you want to free the children of.
     237 */
     238void talloc_free_children(void *ptr);
     239
     240#ifdef DOXYGEN
     241/**
     242 * @brief Assign a destructor function to be called when a chunk is freed.
     243 *
     244 * The function talloc_set_destructor() sets the "destructor" for the pointer
     245 * "ptr". A destructor is a function that is called when the memory used by a
     246 * pointer is about to be released. The destructor receives the pointer as an
     247 * argument, and should return 0 for success and -1 for failure.
     248 *
     249 * The destructor can do anything it wants to, including freeing other pieces
     250 * of memory. A common use for destructors is to clean up operating system
     251 * resources (such as open file descriptors) contained in the structure the
     252 * destructor is placed on.
     253 *
     254 * You can only place one destructor on a pointer. If you need more than one
     255 * destructor then you can create a zero-length child of the pointer and place
     256 * an additional destructor on that.
     257 *
     258 * To remove a destructor call talloc_set_destructor() with NULL for the
     259 * destructor.
     260 *
     261 * If your destructor attempts to talloc_free() the pointer that it is the
     262 * destructor for then talloc_free() will return -1 and the free will be
     263 * ignored. This would be a pointless operation anyway, as the destructor is
     264 * only called when the memory is just about to go away.
     265 *
     266 * @param[in]  ptr      The talloc chunk to add a destructor to.
     267 *
     268 * @param[in]  destructor  The destructor function to be called. NULL to remove
     269 *                         it.
     270 *
     271 * Example:
     272 * @code
     273 *      static int destroy_fd(int *fd) {
     274 *              close(*fd);
     275 *              return 0;
     276 *      }
     277 *
     278 *      int *open_file(const char *filename) {
     279 *              int *fd = talloc(NULL, int);
     280 *              *fd = open(filename, O_RDONLY);
     281 *              if (*fd < 0) {
     282 *                      talloc_free(fd);
     283 *                      return NULL;
     284 *              }
     285 *              // Whenever they free this, we close the file.
     286 *              talloc_set_destructor(fd, destroy_fd);
     287 *              return fd;
     288 *      }
     289 * @endcode
     290 *
     291 * @see talloc()
     292 * @see talloc_free()
     293 */
     294void talloc_set_destructor(const void *ptr, int (*destructor)(void *));
     295
     296/**
     297 * @brief Change a talloc chunk's parent.
     298 *
     299 * The talloc_steal() function changes the parent context of a talloc
     300 * pointer. It is typically used when the context that the pointer is
     301 * currently a child of is going to be freed and you wish to keep the
     302 * memory for a longer time.
     303 *
     304 * To make the changed hierarchy less error-prone, you might consider to use
     305 * talloc_move().
     306 *
     307 * If you try and call talloc_steal() on a pointer that has more than one
     308 * parent then the result is ambiguous. Talloc will choose to remove the
     309 * parent that is currently indicated by talloc_parent() and replace it with
     310 * the chosen parent. You will also get a message like this via the talloc
     311 * logging functions:
     312 *
     313 * @code
     314 *   WARNING: talloc_steal with references at some_dir/source/foo.c:123
     315 *     reference at some_dir/source/other.c:325
     316 *     reference at some_dir/source/third.c:121
     317 * @endcode
     318 *
     319 * To unambiguously change the parent of a pointer please see the function
     320 * talloc_reparent(). See the talloc_set_log_fn() documentation for more
     321 * information on talloc logging.
     322 *
     323 * @param[in]  new_ctx  The new parent context.
     324 *
     325 * @param[in]  ptr      The talloc chunk to move.
     326 *
     327 * @return              Returns the pointer that you pass it. It does not have
     328 *                      any failure modes.
     329 *
     330 * @note It is possible to produce loops in the parent/child relationship
     331 * if you are not careful with talloc_steal(). No guarantees are provided
     332 * as to your sanity or the safety of your data if you do this.
     333 */
     334void *talloc_steal(const void *new_ctx, const void *ptr);
     335#else /* DOXYGEN */
    67336/* try to make talloc_set_destructor() and talloc_steal() type safe,
    68337   if we have a recent gcc */
     
    77346   stupidity in gcc 4.1.x */
    78347#define talloc_steal(ctx, ptr) ({ _TALLOC_TYPEOF(ptr) __talloc_steal_ret = (_TALLOC_TYPEOF(ptr))_talloc_steal_loc((ctx),(ptr), __location__); __talloc_steal_ret; })
    79 #else
     348#else /* __GNUC__ >= 3 */
    80349#define talloc_set_destructor(ptr, function) \
    81350        _talloc_set_destructor((ptr), (int (*)(void *))(function))
    82351#define _TALLOC_TYPEOF(ptr) void *
    83352#define talloc_steal(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_steal_loc((ctx),(ptr), __location__)
    84 #endif
    85 
    86 #define talloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_reference_loc((ctx),(ptr), __location__)
     353#endif /* __GNUC__ >= 3 */
     354void _talloc_set_destructor(const void *ptr, int (*_destructor)(void *));
     355void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location);
     356#endif /* DOXYGEN */
     357
     358/**
     359 * @brief Assign a name to a talloc chunk.
     360 *
     361 * Each talloc pointer has a "name". The name is used principally for
     362 * debugging purposes, although it is also possible to set and get the name on
     363 * a pointer in as a way of "marking" pointers in your code.
     364 *
     365 * The main use for names on pointer is for "talloc reports". See
     366 * talloc_report() and talloc_report_full() for details. Also see
     367 * talloc_enable_leak_report() and talloc_enable_leak_report_full().
     368 *
     369 * The talloc_set_name() function allocates memory as a child of the
     370 * pointer. It is logically equivalent to:
     371 *
     372 * @code
     373 *      talloc_set_name_const(ptr, talloc_asprintf(ptr, fmt, ...));
     374 * @endcode
     375 *
     376 * @param[in]  ptr      The talloc chunk to assign a name to.
     377 *
     378 * @param[in]  fmt      Format string for the name.
     379 *
     380 * @param[in]  ...      Add printf-style additional arguments.
     381 *
     382 * @return              The assigned name, NULL on error.
     383 *
     384 * @note Multiple calls to talloc_set_name() will allocate more memory without
     385 * releasing the name. All of the memory is released when the ptr is freed
     386 * using talloc_free().
     387 */
     388const char *talloc_set_name(const void *ptr, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
     389
     390#ifdef DOXYGEN
     391/**
     392 * @brief Change a talloc chunk's parent.
     393 *
     394 * This function has the same effect as talloc_steal(), and additionally sets
     395 * the source pointer to NULL. You would use it like this:
     396 *
     397 * @code
     398 *      struct foo *X = talloc(tmp_ctx, struct foo);
     399 *      struct foo *Y;
     400 *      Y = talloc_move(new_ctx, &X);
     401 * @endcode
     402 *
     403 * @param[in]  new_ctx  The new parent context.
     404 *
     405 * @param[in]  ptr      Pointer to the talloc chunk to move.
     406 *
     407 * @return              The pointer of the talloc chunk it has been moved to,
     408 *                      NULL on error.
     409 */
     410void *talloc_move(const void *new_ctx, const void *ptr);
     411#else
    87412#define talloc_move(ctx, ptr) (_TALLOC_TYPEOF(*(ptr)))_talloc_move((ctx),(void *)(ptr))
    88 
    89 /* useful macros for creating type checked pointers */
    90 #define talloc(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type)
     413void *_talloc_move(const void *new_ctx, const void *pptr);
     414#endif
     415
     416/**
     417 * @brief Assign a name to a talloc chunk.
     418 *
     419 * The function is just like talloc_set_name(), but it takes a string constant,
     420 * and is much faster. It is extensively used by the "auto naming" macros, such
     421 * as talloc_p().
     422 *
     423 * This function does not allocate any memory. It just copies the supplied
     424 * pointer into the internal representation of the talloc ptr. This means you
     425 * must not pass a name pointer to memory that will disappear before the ptr
     426 * is freed with talloc_free().
     427 *
     428 * @param[in]  ptr      The talloc chunk to assign a name to.
     429 *
     430 * @param[in]  name     Format string for the name.
     431 */
     432void talloc_set_name_const(const void *ptr, const char *name);
     433
     434/**
     435 * @brief Create a named talloc chunk.
     436 *
     437 * The talloc_named() function creates a named talloc pointer. It is
     438 * equivalent to:
     439 *
     440 * @code
     441 *      ptr = talloc_size(context, size);
     442 *      talloc_set_name(ptr, fmt, ....);
     443 * @endcode
     444 *
     445 * @param[in]  context  The talloc context to hang the result off.
     446 *
     447 * @param[in]  size     Number of char's that you want to allocate.
     448 *
     449 * @param[in]  fmt      Format string for the name.
     450 *
     451 * @param[in]  ...      Additional printf-style arguments.
     452 *
     453 * @return              The allocated memory chunk, NULL on error.
     454 *
     455 * @see talloc_set_name()
     456 */
     457void *talloc_named(const void *context, size_t size,
     458                   const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
     459
     460/**
     461 * @brief Basic routine to allocate a chunk of memory.
     462 *
     463 * This is equivalent to:
     464 *
     465 * @code
     466 *      ptr = talloc_size(context, size);
     467 *      talloc_set_name_const(ptr, name);
     468 * @endcode
     469 *
     470 * @param[in]  context  The parent context.
     471 *
     472 * @param[in]  size     The number of char's that we want to allocate.
     473 *
     474 * @param[in]  name     The name the talloc block has.
     475 *
     476 * @return             The allocated memory chunk, NULL on error.
     477 */
     478void *talloc_named_const(const void *context, size_t size, const char *name);
     479
     480#ifdef DOXYGEN
     481/**
     482 * @brief Untyped allocation.
     483 *
     484 * The function should be used when you don't have a convenient type to pass to
     485 * talloc(). Unlike talloc(), it is not type safe (as it returns a void *), so
     486 * you are on your own for type checking.
     487 *
     488 * Best to use talloc() or talloc_array() instead.
     489 *
     490 * @param[in]  ctx     The talloc context to hang the result off.
     491 *
     492 * @param[in]  size    Number of char's that you want to allocate.
     493 *
     494 * @return             The allocated memory chunk, NULL on error.
     495 *
     496 * Example:
     497 * @code
     498 *      void *mem = talloc_size(NULL, 100);
     499 * @endcode
     500 */
     501void *talloc_size(const void *ctx, size_t size);
     502#else
    91503#define talloc_size(ctx, size) talloc_named_const(ctx, size, __location__)
     504#endif
     505
     506#ifdef DOXYGEN
     507/**
     508 * @brief Allocate into a typed pointer.
     509 *
     510 * The talloc_ptrtype() macro should be used when you have a pointer and want
     511 * to allocate memory to point at with this pointer. When compiling with
     512 * gcc >= 3 it is typesafe. Note this is a wrapper of talloc_size() and
     513 * talloc_get_name() will return the current location in the source file and
     514 * not the type.
     515 *
     516 * @param[in]  ctx      The talloc context to hang the result off.
     517 *
     518 * @param[in]  type     The pointer you want to assign the result to.
     519 *
     520 * @return              The properly casted allocated memory chunk, NULL on
     521 *                      error.
     522 *
     523 * Example:
     524 * @code
     525 *       unsigned int *a = talloc_ptrtype(NULL, a);
     526 * @endcode
     527 */
     528void *talloc_ptrtype(const void *ctx, #type);
     529#else
    92530#define talloc_ptrtype(ctx, ptr) (_TALLOC_TYPEOF(ptr))talloc_size(ctx, sizeof(*(ptr)))
    93 
     531#endif
     532
     533#ifdef DOXYGEN
     534/**
     535 * @brief Allocate a new 0-sized talloc chunk.
     536 *
     537 * This is a utility macro that creates a new memory context hanging off an
     538 * existing context, automatically naming it "talloc_new: __location__" where
     539 * __location__ is the source line it is called from. It is particularly
     540 * useful for creating a new temporary working context.
     541 *
     542 * @param[in]  ctx      The talloc parent context.
     543 *
     544 * @return              A new talloc chunk, NULL on error.
     545 */
     546void *talloc_new(const void *ctx);
     547#else
    94548#define talloc_new(ctx) talloc_named_const(ctx, 0, "talloc_new: " __location__)
    95 
     549#endif
     550
     551#ifdef DOXYGEN
     552/**
     553 * @brief Allocate a 0-initizialized structure.
     554 *
     555 * The macro is equivalent to:
     556 *
     557 * @code
     558 *      ptr = talloc(ctx, type);
     559 *      if (ptr) memset(ptr, 0, sizeof(type));
     560 * @endcode
     561 *
     562 * @param[in]  ctx      The talloc context to hang the result off.
     563 *
     564 * @param[in]  type     The type that we want to allocate.
     565 *
     566 * @return              Pointer to a piece of memory, properly cast to 'type *',
     567 *                      NULL on error.
     568 *
     569 * Example:
     570 * @code
     571 *      unsigned int *a, *b;
     572 *      a = talloc_zero(NULL, unsigned int);
     573 *      b = talloc_zero(a, unsigned int);
     574 * @endcode
     575 *
     576 * @see talloc()
     577 * @see talloc_zero_size()
     578 * @see talloc_zero_array()
     579 */
     580void *talloc_zero(const void *ctx, #type);
     581
     582/**
     583 * @brief Allocate untyped, 0-initialized memory.
     584 *
     585 * @param[in]  ctx      The talloc context to hang the result off.
     586 *
     587 * @param[in]  size     Number of char's that you want to allocate.
     588 *
     589 * @return              The allocated memory chunk.
     590 */
     591void *talloc_zero_size(const void *ctx, size_t size);
     592#else
    96593#define talloc_zero(ctx, type) (type *)_talloc_zero(ctx, sizeof(type), #type)
    97594#define talloc_zero_size(ctx, size) _talloc_zero(ctx, size, __location__)
    98 
    99 #define talloc_zero_array(ctx, type, count) (type *)_talloc_zero_array(ctx, sizeof(type), count, #type)
    100 #define talloc_array(ctx, type, count) (type *)_talloc_array(ctx, sizeof(type), count, #type)
    101 #define talloc_array_size(ctx, size, count) _talloc_array(ctx, size, count, __location__)
    102 #define talloc_array_ptrtype(ctx, ptr, count) (_TALLOC_TYPEOF(ptr))talloc_array_size(ctx, sizeof(*(ptr)), count)
    103 #define talloc_array_length(ctx) (talloc_get_size(ctx)/sizeof(*ctx))
    104 
    105 #define talloc_realloc(ctx, p, type, count) (type *)_talloc_realloc_array(ctx, p, sizeof(type), count, #type)
    106 #define talloc_realloc_size(ctx, ptr, size) _talloc_realloc(ctx, ptr, size, __location__)
    107 
     595void *_talloc_zero(const void *ctx, size_t size, const char *name);
     596#endif
     597
     598/**
     599 * @brief Return the name of a talloc chunk.
     600 *
     601 * @param[in]  ptr      The talloc chunk.
     602 *
     603 * @return              The current name for the given talloc pointer.
     604 *
     605 * @see talloc_set_name()
     606 */
     607const char *talloc_get_name(const void *ptr);
     608
     609/**
     610 * @brief Verify that a talloc chunk carries a specified name.
     611 *
     612 * This function checks if a pointer has the specified name. If it does
     613 * then the pointer is returned.
     614 *
     615 * @param[in]  ptr       The talloc chunk to check.
     616 *
     617 * @param[in]  name      The name to check against.
     618 *
     619 * @return               The pointer if the name matches, NULL if it doesn't.
     620 */
     621void *talloc_check_name(const void *ptr, const char *name);
     622
     623/**
     624 * @brief Get the parent chunk of a pointer.
     625 *
     626 * @param[in]  ptr      The talloc pointer to inspect.
     627 *
     628 * @return              The talloc parent of ptr, NULL on error.
     629 */
     630void *talloc_parent(const void *ptr);
     631
     632/**
     633 * @brief Get a talloc chunk's parent name.
     634 *
     635 * @param[in]  ptr      The talloc pointer to inspect.
     636 *
     637 * @return              The name of ptr's parent chunk.
     638 */
     639const char *talloc_parent_name(const void *ptr);
     640
     641/**
     642 * @brief Get the total size of a talloc chunk including its children.
     643 *
     644 * The function returns the total size in bytes used by this pointer and all
     645 * child pointers. Mostly useful for debugging.
     646 *
     647 * Passing NULL is allowed, but it will only give a meaningful result if
     648 * talloc_enable_leak_report() or talloc_enable_leak_report_full() has
     649 * been called.
     650 *
     651 * @param[in]  ptr      The talloc chunk.
     652 *
     653 * @return              The total size.
     654 */
     655size_t talloc_total_size(const void *ptr);
     656
     657/**
     658 * @brief Get the number of talloc chunks hanging off a chunk.
     659 *
     660 * The talloc_total_blocks() function returns the total memory block
     661 * count used by this pointer and all child pointers. Mostly useful for
     662 * debugging.
     663 *
     664 * Passing NULL is allowed, but it will only give a meaningful result if
     665 * talloc_enable_leak_report() or talloc_enable_leak_report_full() has
     666 * been called.
     667 *
     668 * @param[in]  ptr      The talloc chunk.
     669 *
     670 * @return              The total size.
     671 */
     672size_t talloc_total_blocks(const void *ptr);
     673
     674#ifdef DOXYGEN
     675/**
     676 * @brief Duplicate a memory area into a talloc chunk.
     677 *
     678 * The function is equivalent to:
     679 *
     680 * @code
     681 *      ptr = talloc_size(ctx, size);
     682 *      if (ptr) memcpy(ptr, p, size);
     683 * @endcode
     684 *
     685 * @param[in]  t        The talloc context to hang the result off.
     686 *
     687 * @param[in]  p        The memory chunk you want to duplicate.
     688 *
     689 * @param[in]  size     Number of char's that you want copy.
     690 *
     691 * @return              The allocated memory chunk.
     692 *
     693 * @see talloc_size()
     694 */
     695void *talloc_memdup(const void *t, const void *p, size_t size);
     696#else
    108697#define talloc_memdup(t, p, size) _talloc_memdup(t, p, size, __location__)
    109 
     698void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name);
     699#endif
     700
     701#ifdef DOXYGEN
     702/**
     703 * @brief Assign a type to a talloc chunk.
     704 *
     705 * This macro allows you to force the name of a pointer to be a particular type.
     706 * This can be used in conjunction with talloc_get_type() to do type checking on
     707 * void* pointers.
     708 *
     709 * It is equivalent to this:
     710 *
     711 * @code
     712 *      talloc_set_name_const(ptr, #type)
     713 * @endcode
     714 *
     715 * @param[in]  ptr      The talloc chunk to assign the type to.
     716 *
     717 * @param[in]  type     The type to assign.
     718 */
     719void talloc_set_type(const char *ptr, #type);
     720
     721/**
     722 * @brief Get a typed pointer out of a talloc pointer.
     723 *
     724 * This macro allows you to do type checking on talloc pointers. It is
     725 * particularly useful for void* private pointers. It is equivalent to
     726 * this:
     727 *
     728 * @code
     729 *      (type *)talloc_check_name(ptr, #type)
     730 * @endcode
     731 *
     732 * @param[in]  ptr      The talloc pointer to check.
     733 *
     734 * @param[in]  type     The type to check against.
     735 *
     736 * @return              The properly casted pointer given by ptr, NULL on error.
     737 */
     738type *talloc_get_type(const void *ptr, #type);
     739#else
    110740#define talloc_set_type(ptr, type) talloc_set_name_const(ptr, #type)
    111741#define talloc_get_type(ptr, type) (type *)talloc_check_name(ptr, #type)
     742#endif
     743
     744#ifdef DOXYGEN
     745/**
     746 * @brief Safely turn a void pointer into a typed pointer.
     747 *
     748 * This macro is used together with talloc(mem_ctx, struct foo). If you had to
     749 * assing the talloc chunk pointer to some void pointer variable,
     750 * talloc_get_type_abort() is the recommended way to get the convert the void
     751 * pointer back to a typed pointer.
     752 *
     753 * @param[in]  ptr      The void pointer to convert.
     754 *
     755 * @param[in]  type     The type that this chunk contains
     756 *
     757 * @return              The same value as ptr, type-checked and properly cast.
     758 */
     759void *talloc_get_type_abort(const void *ptr, #type);
     760#else
    112761#define talloc_get_type_abort(ptr, type) (type *)_talloc_get_type_abort(ptr, #type, __location__)
    113 
     762void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location);
     763#endif
     764
     765/**
     766 * @brief Find a parent context by name.
     767 *
     768 * Find a parent memory context of the current context that has the given
     769 * name. This can be very useful in complex programs where it may be
     770 * difficult to pass all information down to the level you need, but you
     771 * know the structure you want is a parent of another context.
     772 *
     773 * @param[in]  ctx      The talloc chunk to start from.
     774 *
     775 * @param[in]  name     The name of the parent we look for.
     776 *
     777 * @return              The memory context we are looking for, NULL if not
     778 *                      found.
     779 */
     780void *talloc_find_parent_byname(const void *ctx, const char *name);
     781
     782#ifdef DOXYGEN
     783/**
     784 * @brief Find a parent context by type.
     785 *
     786 * Find a parent memory context of the current context that has the given
     787 * name. This can be very useful in complex programs where it may be
     788 * difficult to pass all information down to the level you need, but you
     789 * know the structure you want is a parent of another context.
     790 *
     791 * Like talloc_find_parent_byname() but takes a type, making it typesafe.
     792 *
     793 * @param[in]  ptr      The talloc chunk to start from.
     794 *
     795 * @param[in]  type     The type of the parent to look for.
     796 *
     797 * @return              The memory context we are looking for, NULL if not
     798 *                      found.
     799 */
     800void *talloc_find_parent_bytype(const void *ptr, #type);
     801#else
    114802#define talloc_find_parent_bytype(ptr, type) (type *)talloc_find_parent_byname(ptr, #type)
    115 #define talloc_free(ctx) _talloc_free(ctx, __location__)
    116 
     803#endif
     804
     805/**
     806 * @brief Allocate a talloc pool.
     807 *
     808 * A talloc pool is a pure optimization for specific situations. In the
     809 * release process for Samba 3.2 we found out that we had become considerably
     810 * slower than Samba 3.0 was. Profiling showed that malloc(3) was a large CPU
     811 * consumer in benchmarks. For Samba 3.2 we have internally converted many
     812 * static buffers to dynamically allocated ones, so malloc(3) being beaten
     813 * more was no surprise. But it made us slower.
     814 *
     815 * talloc_pool() is an optimization to call malloc(3) a lot less for the use
     816 * pattern Samba has: The SMB protocol is mainly a request/response protocol
     817 * where we have to allocate a certain amount of memory per request and free
     818 * that after the SMB reply is sent to the client.
     819 *
     820 * talloc_pool() creates a talloc chunk that you can use as a talloc parent
     821 * exactly as you would use any other ::TALLOC_CTX. The difference is that
     822 * when you talloc a child of this pool, no malloc(3) is done. Instead, talloc
     823 * just increments a pointer inside the talloc_pool. This also works
     824 * recursively. If you use the child of the talloc pool as a parent for
     825 * grand-children, their memory is also taken from the talloc pool.
     826 *
     827 * If you talloc_free() children of a talloc pool, the memory is not given
     828 * back to the system. Instead, free(3) is only called if the talloc_pool()
     829 * itself is released with talloc_free().
     830 *
     831 * The downside of a talloc pool is that if you talloc_move() a child of a
     832 * talloc pool to a talloc parent outside the pool, the whole pool memory is
     833 * not free(3)'ed until that moved chunk is also talloc_free()ed.
     834 *
     835 * @param[in]  context  The talloc context to hang the result off.
     836 *
     837 * @param[in]  size     Size of the talloc pool.
     838 *
     839 * @return              The allocated talloc pool, NULL on error.
     840 */
     841void *talloc_pool(const void *context, size_t size);
     842
     843/**
     844 * @brief Free a talloc chunk and NULL out the pointer.
     845 *
     846 * TALLOC_FREE() frees a pointer and sets it to NULL. Use this if you want
     847 * immediate feedback (i.e. crash) if you use a pointer after having free'ed
     848 * it.
     849 *
     850 * @param[in]  ctx      The chunk to be freed.
     851 */
     852#define TALLOC_FREE(ctx) do { talloc_free(ctx); ctx=NULL; } while(0)
     853
     854/* @} ******************************************************************/
     855
     856/**
     857 * \defgroup talloc_ref The talloc reference function.
     858 * @ingroup talloc
     859 *
     860 * This module contains the definitions around talloc references
     861 *
     862 * @{
     863 */
     864
     865/**
     866 * @brief Increase the reference count of a talloc chunk.
     867 *
     868 * The talloc_increase_ref_count(ptr) function is exactly equivalent to:
     869 *
     870 * @code
     871 *      talloc_reference(NULL, ptr);
     872 * @endcode
     873 *
     874 * You can use either syntax, depending on which you think is clearer in
     875 * your code.
     876 *
     877 * @param[in]  ptr      The pointer to increase the reference count.
     878 *
     879 * @return              0 on success, -1 on error.
     880 */
     881int talloc_increase_ref_count(const void *ptr);
     882
     883/**
     884 * @brief Get the number of references to a talloc chunk.
     885 *
     886 * @param[in]  ptr      The pointer to retrieve the reference count from.
     887 *
     888 * @return              The number of references.
     889 */
     890size_t talloc_reference_count(const void *ptr);
     891
     892#ifdef DOXYGEN
     893/**
     894 * @brief Create an additional talloc parent to a pointer.
     895 *
     896 * The talloc_reference() function makes "context" an additional parent of
     897 * ptr. Each additional reference consumes around 48 bytes of memory on intel
     898 * x86 platforms.
     899 *
     900 * If ptr is NULL, then the function is a no-op, and simply returns NULL.
     901 *
     902 * After creating a reference you can free it in one of the following ways:
     903 *
     904 * - you can talloc_free() any parent of the original pointer. That
     905 *   will reduce the number of parents of this pointer by 1, and will
     906 *   cause this pointer to be freed if it runs out of parents.
     907 *
     908 * - you can talloc_free() the pointer itself. That will destroy the
     909 *   most recently established parent to the pointer and leave the
     910 *   pointer as a child of its current parent.
     911 *
     912 * For more control on which parent to remove, see talloc_unlink()
     913 * @param[in]  ctx      The additional parent.
     914 *
     915 * @param[in]  ptr      The pointer you want to create an additional parent for.
     916 *
     917 * @return              The original pointer 'ptr', NULL if talloc ran out of
     918 *                      memory in creating the reference.
     919 *
     920 * Example:
     921 * @code
     922 *      unsigned int *a, *b, *c;
     923 *      a = talloc(NULL, unsigned int);
     924 *      b = talloc(NULL, unsigned int);
     925 *      c = talloc(a, unsigned int);
     926 *      // b also serves as a parent of c.
     927 *      talloc_reference(b, c);
     928 * @endcode
     929 *
     930 * @see talloc_unlink()
     931 */
     932void *talloc_reference(const void *ctx, const void *ptr);
     933#else
     934#define talloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_reference_loc((ctx),(ptr), __location__)
     935void *_talloc_reference_loc(const void *context, const void *ptr, const char *location);
     936#endif
     937
     938/**
     939 * @brief Remove a specific parent from a talloc chunk.
     940 *
     941 * The function removes a specific parent from ptr. The context passed must
     942 * either be a context used in talloc_reference() with this pointer, or must be
     943 * a direct parent of ptr.
     944 *
     945 * Usually you can just use talloc_free() instead of talloc_unlink(), but
     946 * sometimes it is useful to have the additional control on which parent is
     947 * removed.
     948 *
     949 * @param[in]  context  The talloc parent to remove.
     950 *
     951 * @param[in]  ptr      The talloc ptr you want to remove the parent from.
     952 *
     953 * @return              0 on success, -1 on error.
     954 *
     955 * @note If the parent has already been removed using talloc_free() then
     956 * this function will fail and will return -1.  Likewise, if ptr is NULL,
     957 * then the function will make no modifications and return -1.
     958 *
     959 * Example:
     960 * @code
     961 *      unsigned int *a, *b, *c;
     962 *      a = talloc(NULL, unsigned int);
     963 *      b = talloc(NULL, unsigned int);
     964 *      c = talloc(a, unsigned int);
     965 *      // b also serves as a parent of c.
     966 *      talloc_reference(b, c);
     967 *      talloc_unlink(b, c);
     968 * @endcode
     969 */
     970int talloc_unlink(const void *context, void *ptr);
     971
     972/**
     973 * @brief Provide a talloc context that is freed at program exit.
     974 *
     975 * This is a handy utility function that returns a talloc context
     976 * which will be automatically freed on program exit. This can be used
     977 * to reduce the noise in memory leak reports.
     978 *
     979 * Never use this in code that might be used in objects loaded with
     980 * dlopen and unloaded with dlclose. talloc_autofree_context()
     981 * internally uses atexit(3). Some platforms like modern Linux handles
     982 * this fine, but for example FreeBSD does not deal well with dlopen()
     983 * and atexit() used simultaneously: dlclose() does not clean up the
     984 * list of atexit-handlers, so when the program exits the code that
     985 * was registered from within talloc_autofree_context() is gone, the
     986 * program crashes at exit.
     987 *
     988 * @return              A talloc context, NULL on error.
     989 */
     990void *talloc_autofree_context(void);
     991
     992/**
     993 * @brief Get the size of a talloc chunk.
     994 *
     995 * This function lets you know the amount of memory alloced so far by
     996 * this context. It does NOT account for subcontext memory.
     997 * This can be used to calculate the size of an array.
     998 *
     999 * @param[in]  ctx      The talloc chunk.
     1000 *
     1001 * @return              The size of the talloc chunk.
     1002 */
     1003size_t talloc_get_size(const void *ctx);
     1004
     1005/**
     1006 * @brief Show the parentage of a context.
     1007 *
     1008 * @param[in]  context            The talloc context to look at.
     1009 *
     1010 * @param[in]  file               The output to use, a file, stdout or stderr.
     1011 */
     1012void talloc_show_parents(const void *context, FILE *file);
     1013
     1014/**
     1015 * @brief Check if a context is parent of a talloc chunk.
     1016 *
     1017 * This checks if context is referenced in the talloc hierarchy above ptr.
     1018 *
     1019 * @param[in]  context  The assumed talloc context.
     1020 *
     1021 * @param[in]  ptr      The talloc chunk to check.
     1022 *
     1023 * @return              Return 1 if this is the case, 0 if not.
     1024 */
     1025int talloc_is_parent(const void *context, const void *ptr);
     1026
     1027/**
     1028 * @brief Change the parent context of a talloc pointer.
     1029 *
     1030 * The function changes the parent context of a talloc pointer. It is typically
     1031 * used when the context that the pointer is currently a child of is going to be
     1032 * freed and you wish to keep the memory for a longer time.
     1033 *
     1034 * The difference between talloc_reparent() and talloc_steal() is that
     1035 * talloc_reparent() can specify which parent you wish to change. This is
     1036 * useful when a pointer has multiple parents via references.
     1037 *
     1038 * @param[in]  old_parent
     1039 * @param[in]  new_parent
     1040 * @param[in]  ptr
     1041 *
     1042 * @return              Return the pointer you passed. It does not have any
     1043 *                      failure modes.
     1044 */
     1045void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr);
     1046
     1047/* @} ******************************************************************/
     1048
     1049/**
     1050 * @defgroup talloc_array The talloc array functions
     1051 * @ingroup talloc
     1052 *
     1053 * Talloc contains some handy helpers for handling Arrays conveniently
     1054 *
     1055 * @{
     1056 */
     1057
     1058#ifdef DOXYGEN
     1059/**
     1060 * @brief Allocate an array.
     1061 *
     1062 * The macro is equivalent to:
     1063 *
     1064 * @code
     1065 *      (type *)talloc_size(ctx, sizeof(type) * count);
     1066 * @endcode
     1067 *
     1068 * except that it provides integer overflow protection for the multiply,
     1069 * returning NULL if the multiply overflows.
     1070 *
     1071 * @param[in]  ctx      The talloc context to hang the result off.
     1072 *
     1073 * @param[in]  type     The type that we want to allocate.
     1074 *
     1075 * @param[in]  count    The number of 'type' elements you want to allocate.
     1076 *
     1077 * @return              The allocated result, properly cast to 'type *', NULL on
     1078 *                      error.
     1079 *
     1080 * Example:
     1081 * @code
     1082 *      unsigned int *a, *b;
     1083 *      a = talloc_zero(NULL, unsigned int);
     1084 *      b = talloc_array(a, unsigned int, 100);
     1085 * @endcode
     1086 *
     1087 * @see talloc()
     1088 * @see talloc_zero_array()
     1089 */
     1090void *talloc_array(const void *ctx, #type, unsigned count);
     1091#else
     1092#define talloc_array(ctx, type, count) (type *)_talloc_array(ctx, sizeof(type), count, #type)
     1093void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name);
     1094#endif
     1095
     1096#ifdef DOXYGEN
     1097/**
     1098 * @brief Allocate an array.
     1099 *
     1100 * @param[in]  ctx      The talloc context to hang the result off.
     1101 *
     1102 * @param[in]  size     The size of an array element.
     1103 *
     1104 * @param[in]  count    The number of elements you want to allocate.
     1105 *
     1106 * @return              The allocated result, NULL on error.
     1107 */
     1108void *talloc_array_size(const void *ctx, size_t size, unsigned count);
     1109#else
     1110#define talloc_array_size(ctx, size, count) _talloc_array(ctx, size, count, __location__)
     1111#endif
     1112
     1113#ifdef DOXYGEN
     1114/**
     1115 * @brief Allocate an array into a typed pointer.
     1116 *
     1117 * The macro should be used when you have a pointer to an array and want to
     1118 * allocate memory of an array to point at with this pointer. When compiling
     1119 * with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_array_size()
     1120 * and talloc_get_name() will return the current location in the source file
     1121 * and not the type.
     1122 *
     1123 * @param[in]  ctx      The talloc context to hang the result off.
     1124 *
     1125 * @param[in]  ptr      The pointer you want to assign the result to.
     1126 *
     1127 * @param[in]  count    The number of elements you want to allocate.
     1128 *
     1129 * @return              The allocated memory chunk, properly casted. NULL on
     1130 *                      error.
     1131 */
     1132void *talloc_array_ptrtype(const void *ctx, const void *ptr, unsigned count);
     1133#else
     1134#define talloc_array_ptrtype(ctx, ptr, count) (_TALLOC_TYPEOF(ptr))talloc_array_size(ctx, sizeof(*(ptr)), count)
     1135#endif
     1136
     1137#ifdef DOXYGEN
     1138/**
     1139 * @brief Get the number of elements in a talloc'ed array.
     1140 *
     1141 * A talloc chunk carries its own size, so for talloc'ed arrays it is not
     1142 * necessary to store the number of elements explicitly.
     1143 *
     1144 * @param[in]  ctx      The allocated array.
     1145 *
     1146 * @return              The number of elements in ctx.
     1147 */
     1148size_t talloc_array_length(const void *ctx);
     1149#else
     1150#define talloc_array_length(ctx) (talloc_get_size(ctx)/sizeof(*ctx))
     1151#endif
     1152
     1153#ifdef DOXYGEN
     1154/**
     1155 * @brief Allocate a zero-initialized array
     1156 *
     1157 * @param[in]  ctx      The talloc context to hang the result off.
     1158 *
     1159 * @param[in]  type     The type that we want to allocate.
     1160 *
     1161 * @param[in]  count    The number of "type" elements you want to allocate.
     1162 *
     1163 * @return              The allocated result casted to "type *", NULL on error.
     1164 *
     1165 * The talloc_zero_array() macro is equivalent to:
     1166 *
     1167 * @code
     1168 *     ptr = talloc_array(ctx, type, count);
     1169 *     if (ptr) memset(ptr, sizeof(type) * count);
     1170 * @endcode
     1171 */
     1172void *talloc_zero_array(const void *ctx, #type, unsigned count);
     1173#else
     1174#define talloc_zero_array(ctx, type, count) (type *)_talloc_zero_array(ctx, sizeof(type), count, #type)
     1175void *_talloc_zero_array(const void *ctx,
     1176                         size_t el_size,
     1177                         unsigned count,
     1178                         const char *name);
     1179#endif
     1180
     1181#ifdef DOXYGEN
     1182/**
     1183 * @brief Change the size of a talloc array.
     1184 *
     1185 * The macro changes the size of a talloc pointer. The 'count' argument is the
     1186 * number of elements of type 'type' that you want the resulting pointer to
     1187 * hold.
     1188 *
     1189 * talloc_realloc() has the following equivalences:
     1190 *
     1191 * @code
     1192 *      talloc_realloc(ctx, NULL, type, 1) ==> talloc(ctx, type);
     1193 *      talloc_realloc(ctx, NULL, type, N) ==> talloc_array(ctx, type, N);
     1194 *      talloc_realloc(ctx, ptr, type, 0)  ==> talloc_free(ptr);
     1195 * @endcode
     1196 *
     1197 * The "context" argument is only used if "ptr" is NULL, otherwise it is
     1198 * ignored.
     1199 *
     1200 * @param[in]  ctx      The parent context used if ptr is NULL.
     1201 *
     1202 * @param[in]  ptr      The chunk to be resized.
     1203 *
     1204 * @param[in]  type     The type of the array element inside ptr.
     1205 *
     1206 * @param[in]  count    The intended number of array elements.
     1207 *
     1208 * @return              The new array, NULL on error. The call will fail either
     1209 *                      due to a lack of memory, or because the pointer has more
     1210 *                      than one parent (see talloc_reference()).
     1211 */
     1212void *talloc_realloc(const void *ctx, void *ptr, #type, size_t count);
     1213#else
     1214#define talloc_realloc(ctx, p, type, count) (type *)_talloc_realloc_array(ctx, p, sizeof(type), count, #type)
     1215void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name);
     1216#endif
     1217
     1218#ifdef DOXYGEN
     1219/**
     1220 * @brief Untyped realloc to change the size of a talloc array.
     1221 *
     1222 * The macro is useful when the type is not known so the typesafe
     1223 * talloc_realloc() cannot be used.
     1224 *
     1225 * @param[in]  ctx      The parent context used if 'ptr' is NULL.
     1226 *
     1227 * @param[in]  ptr      The chunk to be resized.
     1228 *
     1229 * @param[in]  size     The new chunk size.
     1230 *
     1231 * @return              The new array, NULL on error.
     1232 */
     1233void *talloc_realloc_size(const void *ctx, void *ptr, size_t size);
     1234#else
     1235#define talloc_realloc_size(ctx, ptr, size) _talloc_realloc(ctx, ptr, size, __location__)
     1236void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name);
     1237#endif
     1238
     1239/**
     1240 * @brief Provide a function version of talloc_realloc_size.
     1241 *
     1242 * This is a non-macro version of talloc_realloc(), which is useful as
     1243 * libraries sometimes want a ralloc function pointer. A realloc()
     1244 * implementation encapsulates the functionality of malloc(), free() and
     1245 * realloc() in one call, which is why it is useful to be able to pass around
     1246 * a single function pointer.
     1247 *
     1248 * @param[in]  context  The parent context used if ptr is NULL.
     1249 *
     1250 * @param[in]  ptr      The chunk to be resized.
     1251 *
     1252 * @param[in]  size     The new chunk size.
     1253 *
     1254 * @return              The new chunk, NULL on error.
     1255 */
     1256void *talloc_realloc_fn(const void *context, void *ptr, size_t size);
     1257
     1258/* @} ******************************************************************/
     1259
     1260/**
     1261 * @defgroup talloc_string The talloc string functions.
     1262 * @ingroup talloc
     1263 *
     1264 * talloc string allocation and manipulation functions.
     1265 * @{
     1266 */
     1267
     1268/**
     1269 * @brief Duplicate a string into a talloc chunk.
     1270 *
     1271 * This function is equivalent to:
     1272 *
     1273 * @code
     1274 *      ptr = talloc_size(ctx, strlen(p)+1);
     1275 *      if (ptr) memcpy(ptr, p, strlen(p)+1);
     1276 * @endcode
     1277 *
     1278 * This functions sets the name of the new pointer to the passed
     1279 * string. This is equivalent to:
     1280 *
     1281 * @code
     1282 *      talloc_set_name_const(ptr, ptr)
     1283 * @endcode
     1284 *
     1285 * @param[in]  t        The talloc context to hang the result off.
     1286 *
     1287 * @param[in]  p        The string you want to duplicate.
     1288 *
     1289 * @return              The duplicated string, NULL on error.
     1290 */
     1291char *talloc_strdup(const void *t, const char *p);
     1292
     1293/**
     1294 * @brief Append a string to given string and duplicate the result.
     1295 *
     1296 * @param[in]  s        The destination to append to.
     1297 *
     1298 * @param[in]  a        The string you want to append.
     1299 *
     1300 * @return              The duplicated string, NULL on error.
     1301 *
     1302 * @see talloc_strdup()
     1303 */
     1304char *talloc_strdup_append(char *s, const char *a);
     1305
     1306/**
     1307 * @brief Append a string to a given buffer and duplicate the result.
     1308 *
     1309 * @param[in]  s        The destination buffer to append to.
     1310 *
     1311 * @param[in]  a        The string you want to append.
     1312 *
     1313 * @return              The duplicated string, NULL on error.
     1314 *
     1315 * @see talloc_strdup()
     1316 */
     1317char *talloc_strdup_append_buffer(char *s, const char *a);
     1318
     1319/**
     1320 * @brief Duplicate a length-limited string into a talloc chunk.
     1321 *
     1322 * This function is the talloc equivalent of the C library function strndup(3).
     1323 *
     1324 * This functions sets the name of the new pointer to the passed string. This is
     1325 * equivalent to:
     1326 *
     1327 * @code
     1328 *      talloc_set_name_const(ptr, ptr)
     1329 * @endcode
     1330 *
     1331 * @param[in]  t        The talloc context to hang the result off.
     1332 *
     1333 * @param[in]  p        The string you want to duplicate.
     1334 *
     1335 * @param[in]  n        The maximum string length to duplicate.
     1336 *
     1337 * @return              The duplicated string, NULL on error.
     1338 */
     1339char *talloc_strndup(const void *t, const char *p, size_t n);
     1340
     1341/**
     1342 * @brief Append at most n characters of a string to given string and duplicate
     1343 *        the result.
     1344 *
     1345 * @param[in]  s        The destination string to append to.
     1346 *
     1347 * @param[in]  a        The source string you want to append.
     1348 *
     1349 * @param[in]  n        The number of characters you want to append from the
     1350 *                      string.
     1351 *
     1352 * @return              The duplicated string, NULL on error.
     1353 *
     1354 * @see talloc_strndup()
     1355 */
     1356char *talloc_strndup_append(char *s, const char *a, size_t n);
     1357
     1358/**
     1359 * @brief Append at most n characters of a string to given buffer and duplicate
     1360 *        the result.
     1361 *
     1362 * @param[in]  s        The destination buffer to append to.
     1363 *
     1364 * @param[in]  a        The source string you want to append.
     1365 *
     1366 * @param[in]  n        The number of characters you want to append from the
     1367 *                      string.
     1368 *
     1369 * @return              The duplicated string, NULL on error.
     1370 *
     1371 * @see talloc_strndup()
     1372 */
     1373char *talloc_strndup_append_buffer(char *s, const char *a, size_t n);
     1374
     1375/**
     1376 * @brief Format a string given a va_list.
     1377 *
     1378 * This function is the talloc equivalent of the C library function
     1379 * vasprintf(3).
     1380 *
     1381 * This functions sets the name of the new pointer to the new string. This is
     1382 * equivalent to:
     1383 *
     1384 * @code
     1385 *      talloc_set_name_const(ptr, ptr)
     1386 * @endcode
     1387 *
     1388 * @param[in]  t        The talloc context to hang the result off.
     1389 *
     1390 * @param[in]  fmt      The format string.
     1391 *
     1392 * @param[in]  ap       The parameters used to fill fmt.
     1393 *
     1394 * @return              The formatted string, NULL on error.
     1395 */
     1396char *talloc_vasprintf(const void *t, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
     1397
     1398/**
     1399 * @brief Format a string given a va_list and append it to the given destination
     1400 *        string.
     1401 *
     1402 * @param[in]  s        The destination string to append to.
     1403 *
     1404 * @param[in]  fmt      The format string.
     1405 *
     1406 * @param[in]  ap       The parameters used to fill fmt.
     1407 *
     1408 * @return              The formatted string, NULL on error.
     1409 *
     1410 * @see talloc_vasprintf()
     1411 */
     1412char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
     1413
     1414/**
     1415 * @brief Format a string given a va_list and append it to the given destination
     1416 *        buffer.
     1417 *
     1418 * @param[in]  s        The destination buffer to append to.
     1419 *
     1420 * @param[in]  fmt      The format string.
     1421 *
     1422 * @param[in]  ap       The parameters used to fill fmt.
     1423 *
     1424 * @return              The formatted string, NULL on error.
     1425 *
     1426 * @see talloc_vasprintf()
     1427 */
     1428char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
     1429
     1430/**
     1431 * @brief Format a string.
     1432 *
     1433 * This function is the talloc equivalent of the C library function asprintf(3).
     1434 *
     1435 * This functions sets the name of the new pointer to the new string. This is
     1436 * equivalent to:
     1437 *
     1438 * @code
     1439 *      talloc_set_name_const(ptr, ptr)
     1440 * @endcode
     1441 *
     1442 * @param[in]  t        The talloc context to hang the result off.
     1443 *
     1444 * @param[in]  fmt      The format string.
     1445 *
     1446 * @param[in]  ...      The parameters used to fill fmt.
     1447 *
     1448 * @return              The formatted string, NULL on error.
     1449 */
     1450char *talloc_asprintf(const void *t, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
     1451
     1452/**
     1453 * @brief Append a formatted string to another string.
     1454 *
     1455 * This function appends the given formatted string to the given string. Use
     1456 * this varient when the string in the current talloc buffer may have been
     1457 * truncated in length.
     1458 *
     1459 * This functions sets the name of the new pointer to the new
     1460 * string. This is equivalent to:
     1461 *
     1462 * @code
     1463 *      talloc_set_name_const(ptr, ptr)
     1464 * @endcode
     1465 *
     1466 * @param[in]  s        The string to append to.
     1467 *
     1468 * @param[in]  fmt      The format string.
     1469 *
     1470 * @param[in]  ...      The parameters used to fill fmt.
     1471 *
     1472 * @return              The formatted string, NULL on error.
     1473 */
     1474char *talloc_asprintf_append(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
     1475
     1476/**
     1477 * @brief Append a formatted string to another string.
     1478 *
     1479 * @param[in]  s        The string to append to
     1480 *
     1481 * @param[in]  fmt      The format string.
     1482 *
     1483 * @param[in]  ...      The parameters used to fill fmt.
     1484 *
     1485 * @return              The formatted string, NULL on error.
     1486 */
     1487char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
     1488
     1489/* @} ******************************************************************/
     1490
     1491/**
     1492 * @defgroup talloc_debug The talloc debugging support functions
     1493 * @ingroup talloc
     1494 *
     1495 * To aid memory debugging, talloc contains routines to inspect the currently
     1496 * allocated memory hierarchy.
     1497 *
     1498 * @{
     1499 */
     1500
     1501/**
     1502 * @brief Walk a complete talloc hierarchy.
     1503 *
     1504 * This provides a more flexible reports than talloc_report(). It
     1505 * will recursively call the callback for the entire tree of memory
     1506 * referenced by the pointer. References in the tree are passed with
     1507 * is_ref = 1 and the pointer that is referenced.
     1508 *
     1509 * You can pass NULL for the pointer, in which case a report is
     1510 * printed for the top level memory context, but only if
     1511 * talloc_enable_leak_report() or talloc_enable_leak_report_full()
     1512 * has been called.
     1513 *
     1514 * The recursion is stopped when depth >= max_depth.
     1515 * max_depth = -1 means only stop at leaf nodes.
     1516 *
     1517 * @param[in]  ptr      The talloc chunk.
     1518 *
     1519 * @param[in]  depth    Internal parameter to control recursion. Call with 0.
     1520 *
     1521 * @param[in]  max_depth  Maximum recursion level.
     1522 *
     1523 * @param[in]  callback  Function to be called on every chunk.
     1524 *
     1525 * @param[in]  private_data  Private pointer passed to callback.
     1526 */
     1527void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
     1528                            void (*callback)(const void *ptr,
     1529                                             int depth, int max_depth,
     1530                                             int is_ref,
     1531                                             void *private_data),
     1532                            void *private_data);
     1533
     1534/**
     1535 * @brief Print a talloc hierarchy.
     1536 *
     1537 * This provides a more flexible reports than talloc_report(). It
     1538 * will let you specify the depth and max_depth.
     1539 *
     1540 * @param[in]  ptr      The talloc chunk.
     1541 *
     1542 * @param[in]  depth    Internal parameter to control recursion. Call with 0.
     1543 *
     1544 * @param[in]  max_depth  Maximum recursion level.
     1545 *
     1546 * @param[in]  f        The file handle to print to.
     1547 */
     1548void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f);
     1549
     1550/**
     1551 * @brief Print a summary report of all memory used by ptr.
     1552 *
     1553 * This provides a more detailed report than talloc_report(). It will
     1554 * recursively print the ensire tree of memory referenced by the
     1555 * pointer. References in the tree are shown by giving the name of the
     1556 * pointer that is referenced.
     1557 *
     1558 * You can pass NULL for the pointer, in which case a report is printed
     1559 * for the top level memory context, but only if
     1560 * talloc_enable_leak_report() or talloc_enable_leak_report_full() has
     1561 * been called.
     1562 *
     1563 * @param[in]  ptr      The talloc chunk.
     1564 *
     1565 * @param[in]  f        The file handle to print to.
     1566 *
     1567 * Example:
     1568 * @code
     1569 *      unsigned int *a, *b;
     1570 *      a = talloc(NULL, unsigned int);
     1571 *      b = talloc(a, unsigned int);
     1572 *      fprintf(stderr, "Dumping memory tree for a:\n");
     1573 *      talloc_report_full(a, stderr);
     1574 * @endcode
     1575 *
     1576 * @see talloc_report()
     1577 */
     1578void talloc_report_full(const void *ptr, FILE *f);
     1579
     1580/**
     1581 * @brief Print a summary report of all memory used by ptr.
     1582 *
     1583 * This function prints a summary report of all memory used by ptr. One line of
     1584 * report is printed for each immediate child of ptr, showing the total memory
     1585 * and number of blocks used by that child.
     1586 *
     1587 * You can pass NULL for the pointer, in which case a report is printed
     1588 * for the top level memory context, but only if talloc_enable_leak_report()
     1589 * or talloc_enable_leak_report_full() has been called.
     1590 *
     1591 * @param[in]  ptr      The talloc chunk.
     1592 *
     1593 * @param[in]  f        The file handle to print to.
     1594 *
     1595 * Example:
     1596 * @code
     1597 *      unsigned int *a, *b;
     1598 *      a = talloc(NULL, unsigned int);
     1599 *      b = talloc(a, unsigned int);
     1600 *      fprintf(stderr, "Summary of memory tree for a:\n");
     1601 *      talloc_report(a, stderr);
     1602 * @endcode
     1603 *
     1604 * @see talloc_report_full()
     1605 */
     1606void talloc_report(const void *ptr, FILE *f);
     1607
     1608/**
     1609 * @brief Enable tracking the use of NULL memory contexts.
     1610 *
     1611 * This enables tracking of the NULL memory context without enabling leak
     1612 * reporting on exit. Useful for when you want to do your own leak
     1613 * reporting call via talloc_report_null_full();
     1614 */
     1615void talloc_enable_null_tracking(void);
     1616
     1617/**
     1618 * @brief Enable tracking the use of NULL memory contexts.
     1619 *
     1620 * This enables tracking of the NULL memory context without enabling leak
     1621 * reporting on exit. Useful for when you want to do your own leak
     1622 * reporting call via talloc_report_null_full();
     1623 */
     1624void talloc_enable_null_tracking_no_autofree(void);
     1625
     1626/**
     1627 * @brief Disable tracking of the NULL memory context.
     1628 *
     1629 * This disables tracking of the NULL memory context.
     1630 */
     1631void talloc_disable_null_tracking(void);
     1632
     1633/**
     1634 * @brief Enable leak report when a program exits.
     1635 *
     1636 * This enables calling of talloc_report(NULL, stderr) when the program
     1637 * exits. In Samba4 this is enabled by using the --leak-report command
     1638 * line option.
     1639 *
     1640 * For it to be useful, this function must be called before any other
     1641 * talloc function as it establishes a "null context" that acts as the
     1642 * top of the tree. If you don't call this function first then passing
     1643 * NULL to talloc_report() or talloc_report_full() won't give you the
     1644 * full tree printout.
     1645 *
     1646 * Here is a typical talloc report:
     1647 *
     1648 * @code
     1649 * talloc report on 'null_context' (total 267 bytes in 15 blocks)
     1650 *      libcli/auth/spnego_parse.c:55  contains     31 bytes in   2 blocks
     1651 *      libcli/auth/spnego_parse.c:55  contains     31 bytes in   2 blocks
     1652 *      iconv(UTF8,CP850)              contains     42 bytes in   2 blocks
     1653 *      libcli/auth/spnego_parse.c:55  contains     31 bytes in   2 blocks
     1654 *      iconv(CP850,UTF8)              contains     42 bytes in   2 blocks
     1655 *      iconv(UTF8,UTF-16LE)           contains     45 bytes in   2 blocks
     1656 *      iconv(UTF-16LE,UTF8)           contains     45 bytes in   2 blocks
     1657 * @endcode
     1658 */
     1659void talloc_enable_leak_report(void);
     1660
     1661/**
     1662 * @brief Enable full leak report when a program exits.
     1663 *
     1664 * This enables calling of talloc_report_full(NULL, stderr) when the
     1665 * program exits. In Samba4 this is enabled by using the
     1666 * --leak-report-full command line option.
     1667 *
     1668 * For it to be useful, this function must be called before any other
     1669 * talloc function as it establishes a "null context" that acts as the
     1670 * top of the tree. If you don't call this function first then passing
     1671 * NULL to talloc_report() or talloc_report_full() won't give you the
     1672 * full tree printout.
     1673 *
     1674 * Here is a typical full report:
     1675 *
     1676 * @code
     1677 * full talloc report on 'root' (total 18 bytes in 8 blocks)
     1678 *      p1                             contains     18 bytes in   7 blocks (ref 0)
     1679 *      r1                             contains     13 bytes in   2 blocks (ref 0)
     1680 *      reference to: p2
     1681 *      p2                             contains      1 bytes in   1 blocks (ref 1)
     1682 *      x3                             contains      1 bytes in   1 blocks (ref 0)
     1683 *      x2                             contains      1 bytes in   1 blocks (ref 0)
     1684 *      x1                             contains      1 bytes in   1 blocks (ref 0)
     1685 * @endcode
     1686 */
     1687void talloc_enable_leak_report_full(void);
     1688
     1689/* @} ******************************************************************/
     1690
     1691void talloc_set_abort_fn(void (*abort_fn)(const char *reason));
     1692void talloc_set_log_fn(void (*log_fn)(const char *message));
     1693void talloc_set_log_stderr(void);
    1171694
    1181695#if TALLOC_DEPRECATED
     
    1251702#endif
    1261703
    127 #define TALLOC_FREE(ctx) do { talloc_free(ctx); ctx=NULL; } while(0)
    128 
    129 /* The following definitions come from talloc.c  */
    130 void *_talloc(const void *context, size_t size);
    131 void *talloc_pool(const void *context, size_t size);
    132 void _talloc_set_destructor(const void *ptr, int (*_destructor)(void *));
    133 int talloc_increase_ref_count(const void *ptr);
    134 size_t talloc_reference_count(const void *ptr);
    135 void *_talloc_reference_loc(const void *context, const void *ptr, const char *location);
    136 int talloc_unlink(const void *context, void *ptr);
    137 const char *talloc_set_name(const void *ptr, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
    138 void talloc_set_name_const(const void *ptr, const char *name);
    139 void *talloc_named(const void *context, size_t size,
    140                    const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
    141 void *talloc_named_const(const void *context, size_t size, const char *name);
    142 const char *talloc_get_name(const void *ptr);
    143 void *talloc_check_name(const void *ptr, const char *name);
    144 void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location);
    145 void *talloc_parent(const void *ptr);
    146 const char *talloc_parent_name(const void *ptr);
    147 void *talloc_init(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
    148 int _talloc_free(void *ptr, const char *location);
    149 void talloc_free_children(void *ptr);
    150 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name);
    151 void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location);
    152 void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr);
    153 void *_talloc_move(const void *new_ctx, const void *pptr);
    154 size_t talloc_total_size(const void *ptr);
    155 size_t talloc_total_blocks(const void *ptr);
    156 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
    157                             void (*callback)(const void *ptr,
    158                                              int depth, int max_depth,
    159                                              int is_ref,
    160                                              void *private_data),
    161                             void *private_data);
    162 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f);
    163 void talloc_report_full(const void *ptr, FILE *f);
    164 void talloc_report(const void *ptr, FILE *f);
    165 void talloc_enable_null_tracking(void);
    166 void talloc_enable_null_tracking_no_autofree(void);
    167 void talloc_disable_null_tracking(void);
    168 void talloc_enable_leak_report(void);
    169 void talloc_enable_leak_report_full(void);
    170 void *_talloc_zero(const void *ctx, size_t size, const char *name);
    171 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name);
    172 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name);
    173 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name);
    174 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name);
    175 void *talloc_realloc_fn(const void *context, void *ptr, size_t size);
    176 void *talloc_autofree_context(void);
    177 size_t talloc_get_size(const void *ctx);
    178 void *talloc_find_parent_byname(const void *ctx, const char *name);
    179 void talloc_show_parents(const void *context, FILE *file);
    180 int talloc_is_parent(const void *context, const void *ptr);
    181 
    182 char *talloc_strdup(const void *t, const char *p);
    183 char *talloc_strdup_append(char *s, const char *a);
    184 char *talloc_strdup_append_buffer(char *s, const char *a);
    185 
    186 char *talloc_strndup(const void *t, const char *p, size_t n);
    187 char *talloc_strndup_append(char *s, const char *a, size_t n);
    188 char *talloc_strndup_append_buffer(char *s, const char *a, size_t n);
    189 
    190 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
    191 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
    192 char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
    193 
    194 char *talloc_asprintf(const void *t, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
    195 char *talloc_asprintf_append(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
    196 char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
    197 
    198 void talloc_set_abort_fn(void (*abort_fn)(const char *reason));
    199 void talloc_set_log_fn(void (*log_fn)(const char *message));
    200 void talloc_set_log_stderr(void);
    201 
    202 #endif
     1704#ifndef TALLOC_MAX_DEPTH
     1705#define TALLOC_MAX_DEPTH 10000
     1706#endif
     1707
     1708#ifdef __cplusplus
     1709} /* end of extern "C" */
     1710#endif
     1711
     1712#endif
  • vendor/current/lib/talloc/talloc.pc.in

    r414 r740  
    77Description: A hierarchical pool based memory system with destructors
    88Version: @TALLOC_VERSION@
    9 Libs: -L${libdir} -ltalloc
     9Libs: @LIB_RPATH@ -L${libdir} -ltalloc
    1010Cflags: -I${includedir}
    1111URL: http://talloc.samba.org/
  • vendor/current/lib/talloc/talloc_guide.txt

    r414 r740  
    7575synchronization.
    7676
     77talloc and shared objects
     78-------------------------
     79
     80talloc can be used in shared objects. Special care needs to be taken
     81to never use talloc_autofree_context() in code that might be loaded
     82with dlopen() and unloaded with dlclose(), as talloc_autofree_context()
     83internally uses atexit(3). Some platforms like modern Linux handles
     84this fine, but for example FreeBSD does not deal well with dlopen()
     85and atexit() used simultaneously: dlclose() does not clean up the list
     86of atexit-handlers, so when the program exits the code that was
     87registered from within talloc_autofree_context() is gone, the program
     88crashes at exit.
     89
    7790
    7891=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
     
    118131
    119132The return value of talloc_free() indicates success or failure, with 0
    120 returned for success and -1 for failure. The only possible failure
    121 condition is if the pointer had a destructor attached to it and the
    122 destructor returned -1. See talloc_set_destructor() for details on
    123 destructors.
     133returned for success and -1 for failure. A possible failure condition
     134is if the pointer had a destructor attached to it and the destructor
     135returned -1. See talloc_set_destructor() for details on
     136destructors. Likewise, if "ptr" is NULL, then the function will make
     137no modifications and returns -1.
    124138
    125139If this pointer has an additional parent when talloc_free() is called
     
    653667
    654668=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    655 ((type *)talloc_array(const void *ctx, type, uint_t count);
     669((type *)talloc_array(const void *ctx, type, unsigned int count);
    656670
    657671The talloc_array() macro is equivalent to::
     
    664678
    665679=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    666 void *talloc_array_size(const void *ctx, size_t size, uint_t count);
     680void *talloc_array_size(const void *ctx, size_t size, unsigned int count);
    667681
    668682The talloc_array_size() function is useful when the type is not
     
    671685
    672686=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    673 (typeof(ptr)) talloc_array_ptrtype(const void *ctx, ptr, uint_t count);
     687(typeof(ptr)) talloc_array_ptrtype(const void *ctx, ptr, unsigned int count);
    674688
    675689The talloc_ptrtype() macro should be used when you have a pointer to an array
  • vendor/current/lib/talloc/testsuite.c

    r414 r740  
    2626#include "replace.h"
    2727#include "system/time.h"
    28 #include "talloc.h"
     28#include <talloc.h>
     29
     30#include "talloc_testsuite.h"
    2931
    3032static struct timeval timeval_current(void)
     
    102104static unsigned int test_abort_count;
    103105
     106#if 0
    104107static void test_abort_fn(const char *reason)
    105108{
     
    113116        talloc_set_abort_fn(test_abort_fn);
    114117}
     118#endif
    115119
    116120static void test_abort_stop(void)
     
    11201124        void *pool;
    11211125        void *p1, *p2, *p3, *p4;
     1126        void *p2_2;
    11221127
    11231128        pool = talloc_pool(NULL, 1024);
    11241129
    11251130        p1 = talloc_size(pool, 80);
     1131        memset(p1, 0x11, talloc_get_size(p1));
    11261132        p2 = talloc_size(pool, 20);
     1133        memset(p2, 0x11, talloc_get_size(p2));
    11271134        p3 = talloc_size(p1, 50);
     1135        memset(p3, 0x11, talloc_get_size(p3));
    11281136        p4 = talloc_size(p3, 1000);
     1137        memset(p4, 0x11, talloc_get_size(p4));
     1138
     1139#if 1 /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
     1140        p2_2 = talloc_realloc_size(pool, p2, 20+1);
     1141        torture_assert("pool realloc 20+1", p2_2 == p2, "failed: pointer changed");
     1142        memset(p2, 0x11, talloc_get_size(p2));
     1143        p2_2 = talloc_realloc_size(pool, p2, 20-1);
     1144        torture_assert("pool realloc 20-1", p2_2 == p2, "failed: pointer changed");
     1145        memset(p2, 0x11, talloc_get_size(p2));
     1146        p2_2 = talloc_realloc_size(pool, p2, 20-1);
     1147        torture_assert("pool realloc 20-1", p2_2 == p2, "failed: pointer changed");
     1148        memset(p2, 0x11, talloc_get_size(p2));
     1149
     1150        talloc_free(p3);
     1151
     1152        /* this should reclaim the memory of p4 and p3 */
     1153        p2_2 = talloc_realloc_size(pool, p2, 400);
     1154        torture_assert("pool realloc 400", p2_2 == p2, "failed: pointer changed");
     1155        memset(p2, 0x11, talloc_get_size(p2));
     1156
     1157        talloc_free(p1);
     1158
     1159        /* this should reclaim the memory of p1 */
     1160        p2_2 = talloc_realloc_size(pool, p2, 800);
     1161        torture_assert("pool realloc 800", p2_2 == p1, "failed: pointer not changed");
     1162        p2 = p2_2;
     1163        memset(p2, 0x11, talloc_get_size(p2));
     1164
     1165        /* this should do a malloc */
     1166        p2_2 = talloc_realloc_size(pool, p2, 1800);
     1167        torture_assert("pool realloc 1800", p2_2 != p2, "failed: pointer not changed");
     1168        p2 = p2_2;
     1169        memset(p2, 0x11, talloc_get_size(p2));
     1170
     1171        /* this should reclaim the memory from the pool */
     1172        p3 = talloc_size(pool, 80);
     1173        torture_assert("pool alloc 80", p3 == p1, "failed: pointer changed");
     1174        memset(p3, 0x11, talloc_get_size(p3));
     1175
     1176        talloc_free(p2);
     1177        talloc_free(p3);
     1178
     1179        p1 = talloc_size(pool, 80);
     1180        memset(p1, 0x11, talloc_get_size(p1));
     1181        p2 = talloc_size(pool, 20);
     1182        memset(p2, 0x11, talloc_get_size(p2));
     1183
     1184        talloc_free(p1);
     1185
     1186        p2_2 = talloc_realloc_size(pool, p2, 20-1);
     1187        torture_assert("pool realloc 20-1", p2_2 == p2, "failed: pointer changed");
     1188        memset(p2, 0x11, talloc_get_size(p2));
     1189        p2_2 = talloc_realloc_size(pool, p2, 20-1);
     1190        torture_assert("pool realloc 20-1", p2_2 == p2, "failed: pointer changed");
     1191        memset(p2, 0x11, talloc_get_size(p2));
     1192
     1193        /* this should do a malloc */
     1194        p2_2 = talloc_realloc_size(pool, p2, 1800);
     1195        torture_assert("pool realloc 1800", p2_2 != p2, "failed: pointer not changed");
     1196        p2 = p2_2;
     1197        memset(p2, 0x11, talloc_get_size(p2));
     1198
     1199        /* this should reclaim the memory from the pool */
     1200        p3 = talloc_size(pool, 800);
     1201        torture_assert("pool alloc 800", p3 == p1, "failed: pointer changed");
     1202        memset(p3, 0x11, talloc_get_size(p3));
     1203
     1204#endif /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
    11291205
    11301206        talloc_free(pool);
     
    11321208        return true;
    11331209}
     1210
     1211static bool test_pool_steal(void)
     1212{
     1213        void *root;
     1214        void *pool;
     1215        void *p1, *p2;
     1216        void *p1_2, *p2_2;
     1217        size_t hdr;
     1218        size_t ofs1, ofs2;
     1219
     1220        root = talloc_new(NULL);
     1221        pool = talloc_pool(root, 1024);
     1222
     1223        p1 = talloc_size(pool, 4 * 16);
     1224        torture_assert("pool allocate 4 * 16", p1 != NULL, "failed ");
     1225        memset(p1, 0x11, talloc_get_size(p1));
     1226        p2 = talloc_size(pool, 4 * 16);
     1227        torture_assert("pool allocate 4 * 16", p2 > p1, "failed: !(p2 > p1) ");
     1228        memset(p2, 0x11, talloc_get_size(p2));
     1229
     1230        ofs1 = PTR_DIFF(p2, p1);
     1231        hdr = ofs1 - talloc_get_size(p1);
     1232
     1233        talloc_steal(root, p1);
     1234        talloc_steal(root, p2);
     1235
     1236        talloc_free(pool);
     1237
     1238        p1_2 = p1;
     1239
     1240#if 1 /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
     1241        p1_2 = talloc_realloc_size(root, p1, 5 * 16);
     1242        torture_assert("pool realloc 5 * 16", p1_2 > p2, "failed: pointer not changed");
     1243        memset(p1_2, 0x11, talloc_get_size(p1_2));
     1244        ofs1 = PTR_DIFF(p1_2, p2);
     1245        ofs2 = talloc_get_size(p2) + hdr;
     1246
     1247        torture_assert("pool realloc ", ofs1 == ofs2, "failed: pointer offset unexpected");
     1248
     1249        p2_2 = talloc_realloc_size(root, p2, 3 * 16);
     1250        torture_assert("pool realloc 5 * 16", p2_2 == p2, "failed: pointer changed");
     1251        memset(p2_2, 0x11, talloc_get_size(p2_2));
     1252#endif /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
     1253
     1254        talloc_free(p1_2);
     1255
     1256        p2_2 = p2;
     1257
     1258#if 1 /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
     1259        /* now we should reclaim the full pool */
     1260        p2_2 = talloc_realloc_size(root, p2, 8 * 16);
     1261        torture_assert("pool realloc 8 * 16", p2_2 == p1, "failed: pointer not expected");
     1262        p2 = p2_2;
     1263        memset(p2_2, 0x11, talloc_get_size(p2_2));
     1264
     1265        /* now we malloc and free the full pool space */
     1266        p2_2 = talloc_realloc_size(root, p2, 2 * 1024);
     1267        torture_assert("pool realloc 2 * 1024", p2_2 != p1, "failed: pointer not expected");
     1268        memset(p2_2, 0x11, talloc_get_size(p2_2));
     1269
     1270#endif /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
     1271
     1272        talloc_free(p2_2);
     1273
     1274        talloc_free(root);
     1275
     1276        return true;
     1277}
     1278
     1279static bool test_free_ref_null_context(void)
     1280{
     1281        void *p1, *p2, *p3;
     1282        int ret;
     1283
     1284        talloc_disable_null_tracking();
     1285        p1 = talloc_new(NULL);
     1286        p2 = talloc_new(NULL);
     1287
     1288        p3 = talloc_reference(p2, p1);
     1289        torture_assert("reference", p3 == p1, "failed: reference on null");
     1290
     1291        ret = talloc_free(p1);
     1292        torture_assert("ref free with null parent", ret == 0, "failed: free with null parent");
     1293        talloc_free(p2);
     1294
     1295        talloc_enable_null_tracking_no_autofree();
     1296        p1 = talloc_new(NULL);
     1297        p2 = talloc_new(NULL);
     1298
     1299        p3 = talloc_reference(p2, p1);
     1300        torture_assert("reference", p3 == p1, "failed: reference on null");
     1301
     1302        ret = talloc_free(p1);
     1303        torture_assert("ref free with null tracked parent", ret == 0, "failed: free with null parent");
     1304        talloc_free(p2);
     1305
     1306        return true;
     1307}
     1308
     1309static bool test_rusty(void)
     1310{
     1311        void *root;
     1312        const char *p1;
     1313
     1314        talloc_enable_null_tracking();
     1315        root = talloc_new(NULL);
     1316        p1 = talloc_strdup(root, "foo");
     1317        talloc_increase_ref_count(p1);
     1318        talloc_report_full(root, stdout);
     1319        talloc_free(root);
     1320        return true;
     1321}
     1322
    11341323
    11351324static void test_reset(void)
     
    11411330}
    11421331
    1143 struct torture_context;
    11441332bool torture_local_talloc(struct torture_context *tctx)
    11451333{
     
    11861374        test_reset();
    11871375        ret &= test_pool();
     1376        test_reset();
     1377        ret &= test_pool_steal();
     1378        test_reset();
     1379        ret &= test_free_ref_null_context();
     1380        test_reset();
     1381        ret &= test_rusty();
    11881382
    11891383        if (ret) {
     
    11951389
    11961390        test_reset();
    1197 
     1391        talloc_disable_null_tracking();
    11981392        return ret;
    11991393}
  • vendor/current/lib/talloc/testsuite_main.c

    r414 r740  
    2626#include "replace.h"
    2727
    28 struct torture_context;
    29 bool torture_local_talloc(struct torture_context *tctx);
     28#include "talloc_testsuite.h"
    3029
    3130int main(void)
Note: See TracChangeset for help on using the changeset viewer.