mirror of
https://github.com/samba-team/samba.git
synced 2024-12-22 13:34:15 +03:00
r18810: use a copy of samba4's talloc under lib/talloc/
to make mergeing easier.
metze
(This used to be commit d49ffbc19b
)
This commit is contained in:
parent
200d238302
commit
21931b1ca8
@ -217,7 +217,7 @@ LIBNDR_GEN_OBJ = librpc/gen_ndr/ndr_unixinfo.o librpc/gen_ndr/ndr_lsa.o \
|
||||
|
||||
RPC_PARSE_OBJ0 = rpc_parse/parse_prs.o rpc_parse/parse_misc.o
|
||||
|
||||
TALLOC_OBJ = lib/talloc.o
|
||||
TALLOC_OBJ = lib/talloc/talloc.o
|
||||
|
||||
LIB_WITHOUT_PROTO_OBJ = @LIBREPLACE_OBJS@ @SOCKET_WRAPPER_OBJS@
|
||||
|
||||
@ -688,7 +688,7 @@ SHARESEC_OBJ = $(SHARESEC_OBJ0) $(PARAM_OBJ) $(LIB_NONSMBD_OBJ) $(REGOBJS_OBJ)
|
||||
$(ERRORMAP_OBJ) $(RPC_PARSE_OBJ1) $(LIBSAMBA_OBJ) $(DOSERR_OBJ) \
|
||||
$(POPT_LIB_OBJ) $(SECRETS_OBJ)
|
||||
|
||||
TALLOCTORT_OBJ = lib/talloctort.o $(PARAM_OBJ) $(LIB_NONSMBD_OBJ) \
|
||||
TALLOCTORT_OBJ = lib/talloc/testsuite.o $(PARAM_OBJ) $(LIB_NONSMBD_OBJ) \
|
||||
$(RPC_PARSE_OBJ1) $(DOSERR_OBJ) $(LIBSAMBA_OBJ) $(SECRETS_OBJ)
|
||||
|
||||
RPCTORTURE_OBJ = torture/rpctorture.o \
|
||||
|
@ -231,6 +231,7 @@ done
|
||||
|
||||
SAMBA_CPPFLAGS="-Iinclude -I${srcdir-.}/include -I. -I${srcdir-.}"
|
||||
SAMBA_CPPFLAGS="${SAMBA_CPPFLAGS} -I${srcdir-.}/lib/replace"
|
||||
SAMBA_CPPFLAGS="${SAMBA_CPPFLAGS} -I${srcdir-.}/lib/talloc"
|
||||
SAMBA_CPPFLAGS="${SAMBA_CPPFLAGS} -I${srcdir-.}/tdb/include"
|
||||
SAMBA_CPPFLAGS="${SAMBA_CPPFLAGS} -I${srcdir-.}/libaddns"
|
||||
SAMBA_CPPFLAGS="${SAMBA_CPPFLAGS} -I${srcdir-.}/librpc"
|
||||
|
@ -638,7 +638,7 @@ typedef int BOOL;
|
||||
#include "tdbutil.h"
|
||||
#include "tdbback.h"
|
||||
|
||||
#include "talloc.h"
|
||||
#include "lib/talloc/talloc.h"
|
||||
/* And a little extension. Abort on type mismatch */
|
||||
#define talloc_get_type_abort(ptr, type) \
|
||||
(type *)talloc_check_name_abort(ptr, #type)
|
||||
|
71
source3/lib/talloc/Makefile.in
Normal file
71
source3/lib/talloc/Makefile.in
Normal file
@ -0,0 +1,71 @@
|
||||
#!gmake
|
||||
#
|
||||
prefix = @prefix@
|
||||
datarootdir = @datarootdir@
|
||||
exec_prefix = @exec_prefix@
|
||||
includedir = @includedir@
|
||||
libdir = @libdir@
|
||||
mandir = @mandir@
|
||||
VPATH = @srcdir@:@libreplacedir@
|
||||
srcdir = @srcdir@
|
||||
builddir = @builddir@
|
||||
XSLTPROC = @XSLTPROC@
|
||||
INSTALLCMD = @INSTALL@
|
||||
CC = @CC@
|
||||
CFLAGS = @CFLAGS@ -DHAVE_CONFIG_H= -I. -I@srcdir@ -I@libreplacedir@
|
||||
EXTRA_TARGETS = @DOC_TARGET@
|
||||
|
||||
.SUFFIXES: .c .o .3 .3.xml .xml .html
|
||||
|
||||
LIBOBJ = @TALLOCOBJ@ @LIBREPLACEOBJ@
|
||||
|
||||
all: showflags libtalloc.a testsuite $(EXTRA_TARGETS)
|
||||
|
||||
showflags:
|
||||
@echo 'talloc will be compiled with flags:'
|
||||
@echo ' CFLAGS = $(CFLAGS)'
|
||||
@echo ' LIBS = $(LIBS)'
|
||||
|
||||
testsuite: $(LIBOBJ) testsuite.o
|
||||
$(CC) $(CFLAGS) -o testsuite testsuite.o $(LIBOBJ) $(LIBS)
|
||||
|
||||
libtalloc.a: $(LIBOBJ)
|
||||
ar -rv $@ $(LIBOBJ)
|
||||
@-ranlib $@
|
||||
|
||||
install: all
|
||||
${INSTALLCMD} -d ${libdir}
|
||||
${INSTALLCMD} -m 755 libtalloc.a $(libdir)
|
||||
${INSTALLCMD} -d ${includedir}
|
||||
${INSTALLCMD} -m 644 $(srcdir)/talloc.h $(includedir)
|
||||
${INSTALLCMD} -m 644 talloc.pc $(libdir)/pkgconfig
|
||||
if [ -f talloc.3 ];then ${INSTALLCMD} -d ${mandir}/man3; fi
|
||||
if [ -f talloc.3 ];then ${INSTALLCMD} -m 644 talloc.3 $(mandir)/man3; fi
|
||||
|
||||
doc: talloc.3 talloc.3.html
|
||||
|
||||
.3.xml.3:
|
||||
-test -z "$(XSLTPROC)" || $(XSLTPROC) --nonet -o $@ http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl $<
|
||||
|
||||
.xml.html:
|
||||
-test -z "$(XSLTPROC)" || $(XSLTPROC) --nonet -o $@ http://docbook.sourceforge.net/release/xsl/current/html/docbook.xsl $<
|
||||
|
||||
clean:
|
||||
rm -f *~ $(LIBOBJ) libtalloc.a testsuite testsuite.o *.gc?? talloc.3 talloc.3.html
|
||||
|
||||
test: testsuite
|
||||
./testsuite
|
||||
|
||||
gcov:
|
||||
gcov talloc.c
|
||||
|
||||
installcheck:
|
||||
$(MAKE) test
|
||||
|
||||
distclean: clean
|
||||
rm -f *~ */*~
|
||||
rm -f Makefile
|
||||
rm -f config.log config.status config.h config.cache
|
||||
|
||||
realdistclean: distclean
|
||||
rm -f configure config.h.in
|
1
source3/lib/talloc/aclocal.m4
vendored
Normal file
1
source3/lib/talloc/aclocal.m4
vendored
Normal file
@ -0,0 +1 @@
|
||||
m4_include(libreplace.m4)
|
14
source3/lib/talloc/autogen.sh
Executable file
14
source3/lib/talloc/autogen.sh
Executable file
@ -0,0 +1,14 @@
|
||||
#!/bin/sh
|
||||
|
||||
rm -rf autom4te.cache
|
||||
rm -f configure config.h.in
|
||||
|
||||
IPATHS="-I libreplace -I lib/replace -I ../libreplace -I ../replace"
|
||||
autoconf $IPATHS || exit 1
|
||||
autoheader $IPATHS || exit 1
|
||||
|
||||
rm -rf autom4te.cache
|
||||
|
||||
echo "Now run ./configure and then make."
|
||||
exit 0
|
||||
|
1466
source3/lib/talloc/config.guess
vendored
Executable file
1466
source3/lib/talloc/config.guess
vendored
Executable file
File diff suppressed because it is too large
Load Diff
14
source3/lib/talloc/config.mk
Normal file
14
source3/lib/talloc/config.mk
Normal file
@ -0,0 +1,14 @@
|
||||
################################################
|
||||
# Start LIBRARY LIBTALLOC
|
||||
[LIBRARY::LIBTALLOC]
|
||||
VERSION = 0.0.1
|
||||
SO_VERSION = 0
|
||||
OBJ_FILES = talloc.o
|
||||
MANPAGE = talloc.3
|
||||
CFLAGS = -Ilib/talloc
|
||||
PUBLIC_HEADERS = talloc.h
|
||||
DESCRIPTION = A hierarchical pool based memory system with destructors
|
||||
#
|
||||
# End LIBRARY LIBTALLOC
|
||||
################################################
|
||||
|
1579
source3/lib/talloc/config.sub
vendored
Executable file
1579
source3/lib/talloc/config.sub
vendored
Executable file
File diff suppressed because it is too large
Load Diff
18
source3/lib/talloc/configure.ac
Normal file
18
source3/lib/talloc/configure.ac
Normal file
@ -0,0 +1,18 @@
|
||||
AC_PREREQ(2.50)
|
||||
AC_INIT(talloc.h)
|
||||
AC_CONFIG_SRCDIR([talloc.c])
|
||||
AC_SUBST(datarootdir)
|
||||
AC_CONFIG_HEADER(config.h)
|
||||
|
||||
AC_LIBREPLACE_ALL_CHECKS
|
||||
|
||||
m4_include(libtalloc.m4)
|
||||
|
||||
AC_PATH_PROG(XSLTPROC,xsltproc)
|
||||
DOC_TARGET=""
|
||||
if test -n "$XSLTPROC"; then
|
||||
DOC_TARGET=doc
|
||||
fi
|
||||
AC_SUBST(DOC_TARGET)
|
||||
|
||||
AC_OUTPUT(Makefile talloc.pc)
|
238
source3/lib/talloc/install-sh
Executable file
238
source3/lib/talloc/install-sh
Executable file
@ -0,0 +1,238 @@
|
||||
#! /bin/sh
|
||||
#
|
||||
# install - install a program, script, or datafile
|
||||
# This comes from X11R5.
|
||||
#
|
||||
# Calling this script install-sh is preferred over install.sh, to prevent
|
||||
# `make' implicit rules from creating a file called install from it
|
||||
# when there is no Makefile.
|
||||
#
|
||||
# This script is compatible with the BSD install script, but was written
|
||||
# from scratch.
|
||||
#
|
||||
|
||||
|
||||
# set DOITPROG to echo to test this script
|
||||
|
||||
# Don't use :- since 4.3BSD and earlier shells don't like it.
|
||||
doit="${DOITPROG-}"
|
||||
|
||||
|
||||
# put in absolute paths if you don't have them in your path; or use env. vars.
|
||||
|
||||
mvprog="${MVPROG-mv}"
|
||||
cpprog="${CPPROG-cp}"
|
||||
chmodprog="${CHMODPROG-chmod}"
|
||||
chownprog="${CHOWNPROG-chown}"
|
||||
chgrpprog="${CHGRPPROG-chgrp}"
|
||||
stripprog="${STRIPPROG-strip}"
|
||||
rmprog="${RMPROG-rm}"
|
||||
mkdirprog="${MKDIRPROG-mkdir}"
|
||||
|
||||
transformbasename=""
|
||||
transform_arg=""
|
||||
instcmd="$mvprog"
|
||||
chmodcmd="$chmodprog 0755"
|
||||
chowncmd=""
|
||||
chgrpcmd=""
|
||||
stripcmd=""
|
||||
rmcmd="$rmprog -f"
|
||||
mvcmd="$mvprog"
|
||||
src=""
|
||||
dst=""
|
||||
dir_arg=""
|
||||
|
||||
while [ x"$1" != x ]; do
|
||||
case $1 in
|
||||
-c) instcmd="$cpprog"
|
||||
shift
|
||||
continue;;
|
||||
|
||||
-d) dir_arg=true
|
||||
shift
|
||||
continue;;
|
||||
|
||||
-m) chmodcmd="$chmodprog $2"
|
||||
shift
|
||||
shift
|
||||
continue;;
|
||||
|
||||
-o) chowncmd="$chownprog $2"
|
||||
shift
|
||||
shift
|
||||
continue;;
|
||||
|
||||
-g) chgrpcmd="$chgrpprog $2"
|
||||
shift
|
||||
shift
|
||||
continue;;
|
||||
|
||||
-s) stripcmd="$stripprog"
|
||||
shift
|
||||
continue;;
|
||||
|
||||
-t=*) transformarg=`echo $1 | sed 's/-t=//'`
|
||||
shift
|
||||
continue;;
|
||||
|
||||
-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
|
||||
shift
|
||||
continue;;
|
||||
|
||||
*) if [ x"$src" = x ]
|
||||
then
|
||||
src=$1
|
||||
else
|
||||
# this colon is to work around a 386BSD /bin/sh bug
|
||||
:
|
||||
dst=$1
|
||||
fi
|
||||
shift
|
||||
continue;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [ x"$src" = x ]
|
||||
then
|
||||
echo "install: no input file specified"
|
||||
exit 1
|
||||
else
|
||||
true
|
||||
fi
|
||||
|
||||
if [ x"$dir_arg" != x ]; then
|
||||
dst=$src
|
||||
src=""
|
||||
|
||||
if [ -d $dst ]; then
|
||||
instcmd=:
|
||||
else
|
||||
instcmd=mkdir
|
||||
fi
|
||||
else
|
||||
|
||||
# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
|
||||
# might cause directories to be created, which would be especially bad
|
||||
# if $src (and thus $dsttmp) contains '*'.
|
||||
|
||||
if [ -f $src -o -d $src ]
|
||||
then
|
||||
true
|
||||
else
|
||||
echo "install: $src does not exist"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ x"$dst" = x ]
|
||||
then
|
||||
echo "install: no destination specified"
|
||||
exit 1
|
||||
else
|
||||
true
|
||||
fi
|
||||
|
||||
# If destination is a directory, append the input filename; if your system
|
||||
# does not like double slashes in filenames, you may need to add some logic
|
||||
|
||||
if [ -d $dst ]
|
||||
then
|
||||
dst="$dst"/`basename $src`
|
||||
else
|
||||
true
|
||||
fi
|
||||
fi
|
||||
|
||||
## this sed command emulates the dirname command
|
||||
dstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
|
||||
|
||||
# Make sure that the destination directory exists.
|
||||
# this part is taken from Noah Friedman's mkinstalldirs script
|
||||
|
||||
# Skip lots of stat calls in the usual case.
|
||||
if [ ! -d "$dstdir" ]; then
|
||||
defaultIFS='
|
||||
'
|
||||
IFS="${IFS-${defaultIFS}}"
|
||||
|
||||
oIFS="${IFS}"
|
||||
# Some sh's can't handle IFS=/ for some reason.
|
||||
IFS='%'
|
||||
set - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
|
||||
IFS="${oIFS}"
|
||||
|
||||
pathcomp=''
|
||||
|
||||
while [ $# -ne 0 ] ; do
|
||||
pathcomp="${pathcomp}${1}"
|
||||
shift
|
||||
|
||||
if [ ! -d "${pathcomp}" ] ;
|
||||
then
|
||||
$mkdirprog "${pathcomp}"
|
||||
else
|
||||
true
|
||||
fi
|
||||
|
||||
pathcomp="${pathcomp}/"
|
||||
done
|
||||
fi
|
||||
|
||||
if [ x"$dir_arg" != x ]
|
||||
then
|
||||
$doit $instcmd $dst &&
|
||||
|
||||
if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi &&
|
||||
if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&
|
||||
if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi &&
|
||||
if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi
|
||||
else
|
||||
|
||||
# If we're going to rename the final executable, determine the name now.
|
||||
|
||||
if [ x"$transformarg" = x ]
|
||||
then
|
||||
dstfile=`basename $dst`
|
||||
else
|
||||
dstfile=`basename $dst $transformbasename |
|
||||
sed $transformarg`$transformbasename
|
||||
fi
|
||||
|
||||
# don't allow the sed command to completely eliminate the filename
|
||||
|
||||
if [ x"$dstfile" = x ]
|
||||
then
|
||||
dstfile=`basename $dst`
|
||||
else
|
||||
true
|
||||
fi
|
||||
|
||||
# Make a temp file name in the proper directory.
|
||||
|
||||
dsttmp=$dstdir/#inst.$$#
|
||||
|
||||
# Move or copy the file name to the temp name
|
||||
|
||||
$doit $instcmd $src $dsttmp &&
|
||||
|
||||
trap "rm -f ${dsttmp}" 0 &&
|
||||
|
||||
# and set any options; do chmod last to preserve setuid bits
|
||||
|
||||
# If any of these fail, we abort the whole thing. If we want to
|
||||
# ignore errors from any of these, just make sure not to ignore
|
||||
# errors from the above "$doit $instcmd $src $dsttmp" command.
|
||||
|
||||
if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&
|
||||
if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&
|
||||
if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&
|
||||
if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&
|
||||
|
||||
# Now rename the file to the real destination.
|
||||
|
||||
$doit $rmcmd -f $dstdir/$dstfile &&
|
||||
$doit $mvcmd $dsttmp $dstdir/$dstfile
|
||||
|
||||
fi &&
|
||||
|
||||
|
||||
exit 0
|
27
source3/lib/talloc/libtalloc.m4
Normal file
27
source3/lib/talloc/libtalloc.m4
Normal file
@ -0,0 +1,27 @@
|
||||
dnl find the talloc sources. This is meant to work both for
|
||||
dnl talloc standalone builds, and builds of packages using talloc
|
||||
tallocdir=""
|
||||
tallocpaths="$srcdir $srcdir/lib/talloc $srcdir/talloc $srcdir/../talloc"
|
||||
for d in $tallocpaths; do
|
||||
if test -f "$d/talloc.c"; then
|
||||
tallocdir="$d"
|
||||
AC_SUBST(tallocdir)
|
||||
break;
|
||||
fi
|
||||
done
|
||||
if test x"$tallocdir" = "x"; then
|
||||
AC_MSG_ERROR([cannot find talloc source in $tallocpaths])
|
||||
fi
|
||||
TALLOCOBJ="talloc.o"
|
||||
AC_SUBST(TALLOCOBJ)
|
||||
|
||||
AC_CHECK_SIZEOF(size_t,cross)
|
||||
AC_CHECK_SIZEOF(void *,cross)
|
||||
|
||||
if test $ac_cv_sizeof_size_t -lt $ac_cv_sizeof_void_p; then
|
||||
AC_WARN([size_t cannot represent the amount of used memory of a process])
|
||||
AC_WARN([please report this to <samba-technical@samba.org>])
|
||||
AC_WARN([sizeof(size_t) = $ac_cv_sizeof_size_t])
|
||||
AC_WARN([sizeof(void *) = $ac_cv_sizeof_void_p])
|
||||
AC_ERROR([sizeof(size_t) < sizeof(void *)])
|
||||
fi
|
718
source3/lib/talloc/talloc.3.xml
Normal file
718
source3/lib/talloc/talloc.3.xml
Normal file
@ -0,0 +1,718 @@
|
||||
<?xml version="1.0"?>
|
||||
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
|
||||
<refentry>
|
||||
<refmeta>
|
||||
<refentrytitle>talloc</refentrytitle>
|
||||
<manvolnum>3</manvolnum>
|
||||
</refmeta>
|
||||
<refnamediv>
|
||||
<refname>talloc</refname>
|
||||
<refpurpose>hierarchical reference counted memory pool system with destructors</refpurpose>
|
||||
</refnamediv>
|
||||
<refsynopsisdiv>
|
||||
<synopsis>#include <talloc/talloc.h></synopsis>
|
||||
</refsynopsisdiv>
|
||||
<refsect1><title>DESCRIPTION</title>
|
||||
<para>
|
||||
If you are used to talloc from Samba3 then please read this
|
||||
carefully, as talloc has changed a lot.
|
||||
</para>
|
||||
<para>
|
||||
The new talloc is a hierarchical, reference counted memory pool
|
||||
system with destructors. Quite a mouthful really, but not too bad
|
||||
once you get used to it.
|
||||
</para>
|
||||
<para>
|
||||
Perhaps the biggest change from Samba3 is that there is no
|
||||
distinction between a "talloc context" and a "talloc pointer". Any
|
||||
pointer returned from talloc() is itself a valid talloc context.
|
||||
This means you can do this:
|
||||
</para>
|
||||
<programlisting>
|
||||
struct foo *X = talloc(mem_ctx, struct foo);
|
||||
X->name = talloc_strdup(X, "foo");
|
||||
</programlisting>
|
||||
<para>
|
||||
and the pointer <literal role="code">X->name</literal>
|
||||
would be a "child" of the talloc context <literal
|
||||
role="code">X</literal> which is itself a child of
|
||||
<literal role="code">mem_ctx</literal>. So if you do
|
||||
<literal role="code">talloc_free(mem_ctx)</literal> then
|
||||
it is all destroyed, whereas if you do <literal
|
||||
role="code">talloc_free(X)</literal> then just <literal
|
||||
role="code">X</literal> and <literal
|
||||
role="code">X->name</literal> are destroyed, and if
|
||||
you do <literal
|
||||
role="code">talloc_free(X->name)</literal> then just
|
||||
the name element of <literal role="code">X</literal> is
|
||||
destroyed.
|
||||
</para>
|
||||
<para>
|
||||
If you think about this, then what this effectively gives you is an
|
||||
n-ary tree, where you can free any part of the tree with
|
||||
talloc_free().
|
||||
</para>
|
||||
<para>
|
||||
If you find this confusing, then I suggest you run the <literal
|
||||
role="code">testsuite</literal> program to watch talloc
|
||||
in action. You may also like to add your own tests to <literal
|
||||
role="code">testsuite.c</literal> to clarify how some
|
||||
particular situation is handled.
|
||||
</para>
|
||||
</refsect1>
|
||||
<refsect1><title>TALLOC API</title>
|
||||
<para>
|
||||
The following is a complete guide to the talloc API. Read it all at
|
||||
least twice.
|
||||
</para>
|
||||
<refsect2><title>(type *)talloc(const void *ctx, type);</title>
|
||||
<para>
|
||||
The talloc() macro is the core of the talloc library. It takes a
|
||||
memory <emphasis role="italic">ctx</emphasis> and a <emphasis
|
||||
role="italic">type</emphasis>, and returns a pointer to a new
|
||||
area of memory of the given <emphasis
|
||||
role="italic">type</emphasis>.
|
||||
</para>
|
||||
<para>
|
||||
The returned pointer is itself a talloc context, so you can use
|
||||
it as the <emphasis role="italic">ctx</emphasis> argument to more
|
||||
calls to talloc() if you wish.
|
||||
</para>
|
||||
<para>
|
||||
The returned pointer is a "child" of the supplied context. This
|
||||
means that if you talloc_free() the <emphasis
|
||||
role="italic">ctx</emphasis> then the new child disappears as
|
||||
well. Alternatively you can free just the child.
|
||||
</para>
|
||||
<para>
|
||||
The <emphasis role="italic">ctx</emphasis> argument to talloc()
|
||||
can be NULL, in which case a new top level context is created.
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2><title>void *talloc_size(const void *ctx, size_t size);</title>
|
||||
<para>
|
||||
The function talloc_size() should be used when you don't have a
|
||||
convenient type to pass to talloc(). Unlike talloc(), it is not
|
||||
type safe (as it returns a void *), so you are on your own for
|
||||
type checking.
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2><title>(typeof(ptr)) talloc_ptrtype(const void *ctx, ptr);</title>
|
||||
<para>
|
||||
The talloc_ptrtype() macro should be used when you have a pointer and
|
||||
want to allocate memory to point at with this pointer. When compiling
|
||||
with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_size()
|
||||
and talloc_get_name() will return the current location in the source file.
|
||||
and not the type.
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2><title>int talloc_free(void *ptr);</title>
|
||||
<para>
|
||||
The talloc_free() function frees a piece of talloc memory, and
|
||||
all its children. You can call talloc_free() on any pointer
|
||||
returned by talloc().
|
||||
</para>
|
||||
<para>
|
||||
The return value of talloc_free() indicates success or failure,
|
||||
with 0 returned for success and -1 for failure. The only
|
||||
possible failure condition is if <emphasis
|
||||
role="italic">ptr</emphasis> had a destructor attached to it and
|
||||
the destructor returned -1. See <link
|
||||
linkend="talloc_set_destructor"><quote>talloc_set_destructor()</quote></link>
|
||||
for details on destructors.
|
||||
</para>
|
||||
<para>
|
||||
If this pointer has an additional parent when talloc_free() is
|
||||
called then the memory is not actually released, but instead the
|
||||
most recently established parent is destroyed. See <link
|
||||
linkend="talloc_reference"><quote>talloc_reference()</quote></link>
|
||||
for details on establishing additional parents.
|
||||
</para>
|
||||
<para>
|
||||
For more control on which parent is removed, see <link
|
||||
linkend="talloc_unlink"><quote>talloc_unlink()</quote></link>.
|
||||
</para>
|
||||
<para>
|
||||
talloc_free() operates recursively on its children.
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2 id="talloc_reference"><title>void *talloc_reference(const void *ctx, const void *ptr);</title>
|
||||
<para>
|
||||
The talloc_reference() function makes <emphasis
|
||||
role="italic">ctx</emphasis> an additional parent of <emphasis
|
||||
role="italic">ptr</emphasis>.
|
||||
</para>
|
||||
<para>
|
||||
The return value of talloc_reference() is always the original
|
||||
pointer <emphasis role="italic">ptr</emphasis>, unless talloc ran
|
||||
out of memory in creating the reference in which case it will
|
||||
return NULL (each additional reference consumes around 48 bytes
|
||||
of memory on intel x86 platforms).
|
||||
</para>
|
||||
<para>
|
||||
If <emphasis role="italic">ptr</emphasis> is NULL, then the
|
||||
function is a no-op, and simply returns NULL.
|
||||
</para>
|
||||
<para>
|
||||
After creating a reference you can free it in one of the
|
||||
following ways:
|
||||
</para>
|
||||
<para>
|
||||
<itemizedlist>
|
||||
<listitem>
|
||||
<para>
|
||||
you can talloc_free() any parent of the original pointer.
|
||||
That will reduce the number of parents of this pointer by 1,
|
||||
and will cause this pointer to be freed if it runs out of
|
||||
parents.
|
||||
</para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para>
|
||||
you can talloc_free() the pointer itself. That will destroy
|
||||
the most recently established parent to the pointer and leave
|
||||
the pointer as a child of its current parent.
|
||||
</para>
|
||||
</listitem>
|
||||
</itemizedlist>
|
||||
</para>
|
||||
<para>
|
||||
For more control on which parent to remove, see <link
|
||||
linkend="talloc_unlink"><quote>talloc_unlink()</quote></link>.
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2 id="talloc_unlink"><title>int talloc_unlink(const void *ctx, const void *ptr);</title>
|
||||
<para>
|
||||
The talloc_unlink() function removes a specific parent from
|
||||
<emphasis role="italic">ptr</emphasis>. The <emphasis
|
||||
role="italic">ctx</emphasis> passed must either be a context used
|
||||
in talloc_reference() with this pointer, or must be a direct
|
||||
parent of ptr.
|
||||
</para>
|
||||
<para>
|
||||
Note that if the parent has already been removed using
|
||||
talloc_free() then this function will fail and will return -1.
|
||||
Likewise, if <emphasis role="italic">ptr</emphasis> is NULL, then
|
||||
the function will make no modifications and return -1.
|
||||
</para>
|
||||
<para>
|
||||
Usually you can just use talloc_free() instead of
|
||||
talloc_unlink(), but sometimes it is useful to have the
|
||||
additional control on which parent is removed.
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2 id="talloc_set_destructor"><title>void talloc_set_destructor(const void *ptr, int (*destructor)(void *));</title>
|
||||
<para>
|
||||
The function talloc_set_destructor() sets the <emphasis
|
||||
role="italic">destructor</emphasis> for the pointer <emphasis
|
||||
role="italic">ptr</emphasis>. A <emphasis
|
||||
role="italic">destructor</emphasis> is a function that is called
|
||||
when the memory used by a pointer is about to be released. The
|
||||
destructor receives <emphasis role="italic">ptr</emphasis> as an
|
||||
argument, and should return 0 for success and -1 for failure.
|
||||
</para>
|
||||
<para>
|
||||
The <emphasis role="italic">destructor</emphasis> can do anything
|
||||
it wants to, including freeing other pieces of memory. A common
|
||||
use for destructors is to clean up operating system resources
|
||||
(such as open file descriptors) contained in the structure the
|
||||
destructor is placed on.
|
||||
</para>
|
||||
<para>
|
||||
You can only place one destructor on a pointer. If you need more
|
||||
than one destructor then you can create a zero-length child of
|
||||
the pointer and place an additional destructor on that.
|
||||
</para>
|
||||
<para>
|
||||
To remove a destructor call talloc_set_destructor() with NULL for
|
||||
the destructor.
|
||||
</para>
|
||||
<para>
|
||||
If your destructor attempts to talloc_free() the pointer that it
|
||||
is the destructor for then talloc_free() will return -1 and the
|
||||
free will be ignored. This would be a pointless operation
|
||||
anyway, as the destructor is only called when the memory is just
|
||||
about to go away.
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2><title>int talloc_increase_ref_count(const void *<emphasis role="italic">ptr</emphasis>);</title>
|
||||
<para>
|
||||
The talloc_increase_ref_count(<emphasis
|
||||
role="italic">ptr</emphasis>) function is exactly equivalent to:
|
||||
</para>
|
||||
<programlisting>talloc_reference(NULL, ptr);</programlisting>
|
||||
<para>
|
||||
You can use either syntax, depending on which you think is
|
||||
clearer in your code.
|
||||
</para>
|
||||
<para>
|
||||
It returns 0 on success and -1 on failure.
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2><title>size_t talloc_reference_count(const void *<emphasis role="italic">ptr</emphasis>);</title>
|
||||
<para>
|
||||
Return the number of references to the pointer.
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2 id="talloc_set_name"><title>void talloc_set_name(const void *ptr, const char *fmt, ...);</title>
|
||||
<para>
|
||||
Each talloc pointer has a "name". The name is used principally
|
||||
for debugging purposes, although it is also possible to set and
|
||||
get the name on a pointer in as a way of "marking" pointers in
|
||||
your code.
|
||||
</para>
|
||||
<para>
|
||||
The main use for names on pointer is for "talloc reports". See
|
||||
<link
|
||||
linkend="talloc_report"><quote>talloc_report_depth_cb()</quote></link>,
|
||||
<link
|
||||
linkend="talloc_report"><quote>talloc_report_depth_file()</quote></link>,
|
||||
<link
|
||||
linkend="talloc_report"><quote>talloc_report()</quote></link>
|
||||
<link
|
||||
linkend="talloc_report"><quote>talloc_report()</quote></link>
|
||||
and <link
|
||||
linkend="talloc_report_full"><quote>talloc_report_full()</quote></link>
|
||||
for details. Also see <link
|
||||
linkend="talloc_enable_leak_report"><quote>talloc_enable_leak_report()</quote></link>
|
||||
and <link
|
||||
linkend="talloc_enable_leak_report_full"><quote>talloc_enable_leak_report_full()</quote></link>.
|
||||
</para>
|
||||
<para>
|
||||
The talloc_set_name() function allocates memory as a child of the
|
||||
pointer. It is logically equivalent to:
|
||||
</para>
|
||||
<programlisting>talloc_set_name_const(ptr, talloc_asprintf(ptr, fmt, ...));</programlisting>
|
||||
<para>
|
||||
Note that multiple calls to talloc_set_name() will allocate more
|
||||
memory without releasing the name. All of the memory is released
|
||||
when the ptr is freed using talloc_free().
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2><title>void talloc_set_name_const(const void *<emphasis role="italic">ptr</emphasis>, const char *<emphasis role="italic">name</emphasis>);</title>
|
||||
<para>
|
||||
The function talloc_set_name_const() is just like
|
||||
talloc_set_name(), but it takes a string constant, and is much
|
||||
faster. It is extensively used by the "auto naming" macros, such
|
||||
as talloc_p().
|
||||
</para>
|
||||
<para>
|
||||
This function does not allocate any memory. It just copies the
|
||||
supplied pointer into the internal representation of the talloc
|
||||
ptr. This means you must not pass a <emphasis
|
||||
role="italic">name</emphasis> pointer to memory that will
|
||||
disappear before <emphasis role="italic">ptr</emphasis> is freed
|
||||
with talloc_free().
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2><title>void *talloc_named(const void *<emphasis role="italic">ctx</emphasis>, size_t <emphasis role="italic">size</emphasis>, const char *<emphasis role="italic">fmt</emphasis>, ...);</title>
|
||||
<para>
|
||||
The talloc_named() function creates a named talloc pointer. It
|
||||
is equivalent to:
|
||||
</para>
|
||||
<programlisting>ptr = talloc_size(ctx, size);
|
||||
talloc_set_name(ptr, fmt, ....);</programlisting>
|
||||
</refsect2>
|
||||
<refsect2><title>void *talloc_named_const(const void *<emphasis role="italic">ctx</emphasis>, size_t <emphasis role="italic">size</emphasis>, const char *<emphasis role="italic">name</emphasis>);</title>
|
||||
<para>
|
||||
This is equivalent to:
|
||||
</para>
|
||||
<programlisting>ptr = talloc_size(ctx, size);
|
||||
talloc_set_name_const(ptr, name);</programlisting>
|
||||
</refsect2>
|
||||
<refsect2><title>const char *talloc_get_name(const void *<emphasis role="italic">ptr</emphasis>);</title>
|
||||
<para>
|
||||
This returns the current name for the given talloc pointer,
|
||||
<emphasis role="italic">ptr</emphasis>. See <link
|
||||
linkend="talloc_set_name"><quote>talloc_set_name()</quote></link>
|
||||
for details.
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2><title>void *talloc_init(const char *<emphasis role="italic">fmt</emphasis>, ...);</title>
|
||||
<para>
|
||||
This function creates a zero length named talloc context as a top
|
||||
level context. It is equivalent to:
|
||||
</para>
|
||||
<programlisting>talloc_named(NULL, 0, fmt, ...);</programlisting>
|
||||
</refsect2>
|
||||
<refsect2><title>void *talloc_new(void *<emphasis role="italic">ctx</emphasis>);</title>
|
||||
<para>
|
||||
This is a utility macro that creates a new memory context hanging
|
||||
off an exiting context, automatically naming it "talloc_new:
|
||||
__location__" where __location__ is the source line it is called
|
||||
from. It is particularly useful for creating a new temporary
|
||||
working context.
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2><title>(<emphasis role="italic">type</emphasis> *)talloc_realloc(const void *<emphasis role="italic">ctx</emphasis>, void *<emphasis role="italic">ptr</emphasis>, <emphasis role="italic">type</emphasis>, <emphasis role="italic">count</emphasis>);</title>
|
||||
<para>
|
||||
The talloc_realloc() macro changes the size of a talloc pointer.
|
||||
It has the following equivalences:
|
||||
</para>
|
||||
<programlisting>talloc_realloc(ctx, NULL, type, 1) ==> talloc(ctx, type);
|
||||
talloc_realloc(ctx, ptr, type, 0) ==> talloc_free(ptr);</programlisting>
|
||||
<para>
|
||||
The <emphasis role="italic">ctx</emphasis> argument is only used
|
||||
if <emphasis role="italic">ptr</emphasis> is not NULL, otherwise
|
||||
it is ignored.
|
||||
</para>
|
||||
<para>
|
||||
talloc_realloc() returns the new pointer, or NULL on failure.
|
||||
The call will fail either due to a lack of memory, or because the
|
||||
pointer has more than one parent (see <link
|
||||
linkend="talloc_reference"><quote>talloc_reference()</quote></link>).
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2><title>void *talloc_realloc_size(const void *ctx, void *ptr, size_t size);</title>
|
||||
<para>
|
||||
the talloc_realloc_size() function is useful when the type is not
|
||||
known so the type-safe talloc_realloc() cannot be used.
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2><title>TYPE *talloc_steal(const void *<emphasis role="italic">new_ctx</emphasis>, const TYPE *<emphasis role="italic">ptr</emphasis>);</title>
|
||||
<para>
|
||||
The talloc_steal() function changes the parent context of a
|
||||
talloc pointer. It is typically used when the context that the
|
||||
pointer is currently a child of is going to be freed and you wish
|
||||
to keep the memory for a longer time.
|
||||
</para>
|
||||
<para>
|
||||
The talloc_steal() function returns the pointer that you pass it.
|
||||
It does not have any failure modes.
|
||||
</para>
|
||||
<para>
|
||||
NOTE: It is possible to produce loops in the parent/child
|
||||
relationship if you are not careful with talloc_steal(). No
|
||||
guarantees are provided as to your sanity or the safety of your
|
||||
data if you do this.
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2><title>TYPE *talloc_move(const void *<emphasis role="italic">new_ctx</emphasis>, TYPE **<emphasis role="italic">ptr</emphasis>);</title>
|
||||
<para>
|
||||
The talloc_move() function is a wrapper around
|
||||
talloc_steal() which zeros the source pointer after the
|
||||
move. This avoids a potential source of bugs where a
|
||||
programmer leaves a pointer in two structures, and uses the
|
||||
pointer from the old structure after it has been moved to a
|
||||
new one.
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2><title>size_t talloc_total_size(const void *<emphasis role="italic">ptr</emphasis>);</title>
|
||||
<para>
|
||||
The talloc_total_size() function returns the total size in bytes
|
||||
used by this pointer and all child pointers. Mostly useful for
|
||||
debugging.
|
||||
</para>
|
||||
<para>
|
||||
Passing NULL is allowed, but it will only give a meaningful
|
||||
result if talloc_enable_leak_report() or
|
||||
talloc_enable_leak_report_full() has been called.
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2><title>size_t talloc_total_blocks(const void *<emphasis role="italic">ptr</emphasis>);</title>
|
||||
<para>
|
||||
The talloc_total_blocks() function returns the total memory block
|
||||
count used by this pointer and all child pointers. Mostly useful
|
||||
for debugging.
|
||||
</para>
|
||||
<para>
|
||||
Passing NULL is allowed, but it will only give a meaningful
|
||||
result if talloc_enable_leak_report() or
|
||||
talloc_enable_leak_report_full() has been called.
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2 id="talloc_report"><title>void talloc_report(const void *ptr, FILE *f);</title>
|
||||
<para>
|
||||
The talloc_report() function prints a summary report of all
|
||||
memory used by <emphasis role="italic">ptr</emphasis>. One line
|
||||
of report is printed for each immediate child of ptr, showing the
|
||||
total memory and number of blocks used by that child.
|
||||
</para>
|
||||
<para>
|
||||
You can pass NULL for the pointer, in which case a report is
|
||||
printed for the top level memory context, but only if
|
||||
talloc_enable_leak_report() or talloc_enable_leak_report_full()
|
||||
has been called.
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2 id="talloc_report_full"><title>void talloc_report_full(const void *<emphasis role="italic">ptr</emphasis>, FILE *<emphasis role="italic">f</emphasis>);</title>
|
||||
<para>
|
||||
This provides a more detailed report than talloc_report(). It
|
||||
will recursively print the entire tree of memory referenced by
|
||||
the pointer. References in the tree are shown by giving the name
|
||||
of the pointer that is referenced.
|
||||
</para>
|
||||
<para>
|
||||
You can pass NULL for the pointer, in which case a report is
|
||||
printed for the top level memory context, but only if
|
||||
talloc_enable_leak_report() or talloc_enable_leak_report_full()
|
||||
has been called.
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2 id="talloc_report_depth_cb">
|
||||
<funcsynopsis><funcprototype>
|
||||
<funcdef>void <function>talloc_report_depth_cb</function></funcdef>
|
||||
<paramdef><parameter>const void *ptr</parameter></paramdef>
|
||||
<paramdef><parameter>int depth</parameter></paramdef>
|
||||
<paramdef><parameter>int max_depth</parameter></paramdef>
|
||||
<paramdef><parameter>void (*callback)(const void *ptr, int depth, int max_depth, int is_ref, void *priv)</parameter></paramdef>
|
||||
<paramdef><parameter>void *priv</parameter></paramdef>
|
||||
</funcprototype></funcsynopsis>
|
||||
<para>
|
||||
This provides a more flexible reports than talloc_report(). It
|
||||
will recursively call the callback for the entire tree of memory
|
||||
referenced by the pointer. References in the tree are passed with
|
||||
<emphasis role="italic">is_ref = 1</emphasis> and the pointer that is referenced.
|
||||
</para>
|
||||
<para>
|
||||
You can pass NULL for the pointer, in which case a report is
|
||||
printed for the top level memory context, but only if
|
||||
talloc_enable_leak_report() or talloc_enable_leak_report_full()
|
||||
has been called.
|
||||
</para>
|
||||
<para>
|
||||
The recursion is stopped when depth >= max_depth.
|
||||
max_depth = -1 means only stop at leaf nodes.
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2 id="talloc_report_depth_file">
|
||||
<funcsynopsis><funcprototype>
|
||||
<funcdef>void <function>talloc_report_depth_file</function></funcdef>
|
||||
<paramdef><parameter>const void *ptr</parameter></paramdef>
|
||||
<paramdef><parameter>int depth</parameter></paramdef>
|
||||
<paramdef><parameter>int max_depth</parameter></paramdef>
|
||||
<paramdef><parameter>FILE *f</parameter></paramdef>
|
||||
</funcprototype></funcsynopsis>
|
||||
<para>
|
||||
This provides a more flexible reports than talloc_report(). It
|
||||
will let you specify the depth and max_depth.
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2 id="talloc_enable_leak_report"><title>void talloc_enable_leak_report(void);</title>
|
||||
<para>
|
||||
This enables calling of talloc_report(NULL, stderr) when the
|
||||
program exits. In Samba4 this is enabled by using the
|
||||
--leak-report command line option.
|
||||
</para>
|
||||
<para>
|
||||
For it to be useful, this function must be called before any
|
||||
other talloc function as it establishes a "null context" that
|
||||
acts as the top of the tree. If you don't call this function
|
||||
first then passing NULL to talloc_report() or
|
||||
talloc_report_full() won't give you the full tree printout.
|
||||
</para>
|
||||
<para>
|
||||
Here is a typical talloc report:
|
||||
</para>
|
||||
<screen format="linespecific">talloc report on 'null_context' (total 267 bytes in 15 blocks)
|
||||
libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
|
||||
libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
|
||||
iconv(UTF8,CP850) contains 42 bytes in 2 blocks
|
||||
libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
|
||||
iconv(CP850,UTF8) contains 42 bytes in 2 blocks
|
||||
iconv(UTF8,UTF-16LE) contains 45 bytes in 2 blocks
|
||||
iconv(UTF-16LE,UTF8) contains 45 bytes in 2 blocks
|
||||
</screen>
|
||||
</refsect2>
|
||||
<refsect2 id="talloc_enable_leak_report_full"><title>void talloc_enable_leak_report_full(void);</title>
|
||||
<para>
|
||||
This enables calling of talloc_report_full(NULL, stderr) when the
|
||||
program exits. In Samba4 this is enabled by using the
|
||||
--leak-report-full command line option.
|
||||
</para>
|
||||
<para>
|
||||
For it to be useful, this function must be called before any
|
||||
other talloc function as it establishes a "null context" that
|
||||
acts as the top of the tree. If you don't call this function
|
||||
first then passing NULL to talloc_report() or
|
||||
talloc_report_full() won't give you the full tree printout.
|
||||
</para>
|
||||
<para>
|
||||
Here is a typical full report:
|
||||
</para>
|
||||
<screen format="linespecific">full talloc report on 'root' (total 18 bytes in 8 blocks)
|
||||
p1 contains 18 bytes in 7 blocks (ref 0)
|
||||
r1 contains 13 bytes in 2 blocks (ref 0)
|
||||
reference to: p2
|
||||
p2 contains 1 bytes in 1 blocks (ref 1)
|
||||
x3 contains 1 bytes in 1 blocks (ref 0)
|
||||
x2 contains 1 bytes in 1 blocks (ref 0)
|
||||
x1 contains 1 bytes in 1 blocks (ref 0)
|
||||
</screen>
|
||||
</refsect2>
|
||||
<refsect2><title>(<emphasis role="italic">type</emphasis> *)talloc_zero(const void *<emphasis role="italic">ctx</emphasis>, <emphasis role="italic">type</emphasis>);</title>
|
||||
<para>
|
||||
The talloc_zero() macro is equivalent to:
|
||||
</para>
|
||||
<programlisting>ptr = talloc(ctx, type);
|
||||
if (ptr) memset(ptr, 0, sizeof(type));</programlisting>
|
||||
</refsect2>
|
||||
<refsect2><title>void *talloc_zero_size(const void *<emphasis role="italic">ctx</emphasis>, size_t <emphasis role="italic">size</emphasis>)</title>
|
||||
<para>
|
||||
The talloc_zero_size() function is useful when you don't have a
|
||||
known type.
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2><title>void *talloc_memdup(const void *<emphasis role="italic">ctx</emphasis>, const void *<emphasis role="italic">p</emphasis>, size_t size);</title>
|
||||
<para>
|
||||
The talloc_memdup() function is equivalent to:
|
||||
</para>
|
||||
<programlisting>ptr = talloc_size(ctx, size);
|
||||
if (ptr) memcpy(ptr, p, size);</programlisting>
|
||||
</refsect2>
|
||||
<refsect2><title>char *talloc_strdup(const void *<emphasis role="italic">ctx</emphasis>, const char *<emphasis role="italic">p</emphasis>);</title>
|
||||
<para>
|
||||
The talloc_strdup() function is equivalent to:
|
||||
</para>
|
||||
<programlisting>ptr = talloc_size(ctx, strlen(p)+1);
|
||||
if (ptr) memcpy(ptr, p, strlen(p)+1);</programlisting>
|
||||
<para>
|
||||
This function sets the name of the new pointer to the passed
|
||||
string. This is equivalent to:
|
||||
</para>
|
||||
<programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
|
||||
</refsect2>
|
||||
<refsect2><title>char *talloc_strndup(const void *<emphasis role="italic">t</emphasis>, const char *<emphasis role="italic">p</emphasis>, size_t <emphasis role="italic">n</emphasis>);</title>
|
||||
<para>
|
||||
The talloc_strndup() function is the talloc equivalent of the C
|
||||
library function strndup(3).
|
||||
</para>
|
||||
<para>
|
||||
This function sets the name of the new pointer to the passed
|
||||
string. This is equivalent to:
|
||||
</para>
|
||||
<programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
|
||||
</refsect2>
|
||||
<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>
|
||||
<para>
|
||||
The talloc_vasprintf() function is the talloc equivalent of the C
|
||||
library function vasprintf(3).
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2><title>char *talloc_asprintf(const void *<emphasis role="italic">t</emphasis>, const char *<emphasis role="italic">fmt</emphasis>, ...);</title>
|
||||
<para>
|
||||
The talloc_asprintf() function is the talloc equivalent of the C
|
||||
library function asprintf(3).
|
||||
</para>
|
||||
<para>
|
||||
This function sets the name of the new pointer to the passed
|
||||
string. This is equivalent to:
|
||||
</para>
|
||||
<programlisting>talloc_set_name_const(ptr, ptr)</programlisting>
|
||||
</refsect2>
|
||||
<refsect2><title>char *talloc_asprintf_append(char *s, const char *fmt, ...);</title>
|
||||
<para>
|
||||
The talloc_asprintf_append() function appends the given formatted
|
||||
string to the given string.
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2><title>(type *)talloc_array(const void *ctx, type, uint_t count);</title>
|
||||
<para>
|
||||
The talloc_array() macro is equivalent to:
|
||||
</para>
|
||||
<programlisting>(type *)talloc_size(ctx, sizeof(type) * count);</programlisting>
|
||||
<para>
|
||||
except that it provides integer overflow protection for the
|
||||
multiply, returning NULL if the multiply overflows.
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2><title>void *talloc_array_size(const void *ctx, size_t size, uint_t count);</title>
|
||||
<para>
|
||||
The talloc_array_size() function is useful when the type is not
|
||||
known. It operates in the same way as talloc_array(), but takes a
|
||||
size instead of a type.
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2><title>(typeof(ptr)) talloc_array_ptrtype(const void *ctx, ptr, uint_t count);</title>
|
||||
<para>
|
||||
The talloc_ptrtype() macro should be used when you have a pointer to an array
|
||||
and want to allocate memory of an array to point at with this pointer. When compiling
|
||||
with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_array_size()
|
||||
and talloc_get_name() will return the current location in the source file.
|
||||
and not the type.
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2><title>void *talloc_realloc_fn(const void *ctx, void *ptr, size_t size)</title>
|
||||
<para>
|
||||
This is a non-macro version of talloc_realloc(), which is useful
|
||||
as libraries sometimes want a realloc function pointer. A
|
||||
realloc(3) implementation encapsulates the functionality of
|
||||
malloc(3), free(3) and realloc(3) in one call, which is why it is
|
||||
useful to be able to pass around a single function pointer.
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2><title>void *talloc_autofree_context(void);</title>
|
||||
<para>
|
||||
This is a handy utility function that returns a talloc context
|
||||
which will be automatically freed on program exit. This can be
|
||||
used to reduce the noise in memory leak reports.
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2><title>void *talloc_check_name(const void *ptr, const char *name);</title>
|
||||
<para>
|
||||
This function checks if a pointer has the specified <emphasis
|
||||
role="italic">name</emphasis>. If it does then the pointer is
|
||||
returned. It it doesn't then NULL is returned.
|
||||
</para>
|
||||
</refsect2>
|
||||
<refsect2><title>(type *)talloc_get_type(const void *ptr, type);</title>
|
||||
<para>
|
||||
This macro allows you to do type checking on talloc pointers. It
|
||||
is particularly useful for void* private pointers. It is
|
||||
equivalent to this:
|
||||
</para>
|
||||
<programlisting>(type *)talloc_check_name(ptr, #type)</programlisting>
|
||||
</refsect2>
|
||||
<refsect2><title>talloc_set_type(const void *ptr, type);</title>
|
||||
<para>
|
||||
This macro allows you to force the name of a pointer to be a
|
||||
particular <emphasis>type</emphasis>. This can be
|
||||
used in conjunction with talloc_get_type() to do type checking on
|
||||
void* pointers.
|
||||
</para>
|
||||
<para>
|
||||
It is equivalent to this:
|
||||
</para>
|
||||
<programlisting>talloc_set_name_const(ptr, #type)</programlisting>
|
||||
</refsect2>
|
||||
</refsect1>
|
||||
<refsect1><title>PERFORMANCE</title>
|
||||
<para>
|
||||
All the additional features of talloc(3) over malloc(3) do come at a
|
||||
price. We have a simple performance test in Samba4 that measures
|
||||
talloc() versus malloc() performance, and it seems that talloc() is
|
||||
about 10% slower than malloc() on my x86 Debian Linux box. For
|
||||
Samba, the great reduction in code complexity that we get by using
|
||||
talloc makes this worthwhile, especially as the total overhead of
|
||||
talloc/malloc in Samba is already quite small.
|
||||
</para>
|
||||
</refsect1>
|
||||
<refsect1><title>SEE ALSO</title>
|
||||
<para>
|
||||
malloc(3), strndup(3), vasprintf(3), asprintf(3),
|
||||
<ulink url="http://talloc.samba.org/"/>
|
||||
</para>
|
||||
</refsect1>
|
||||
<refsect1><title>COPYRIGHT/LICENSE</title>
|
||||
<para>
|
||||
Copyright (C) Andrew Tridgell 2004
|
||||
</para>
|
||||
<para>
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or (at
|
||||
your option) any later version.
|
||||
</para>
|
||||
<para>
|
||||
This program is distributed in the hope that it will be useful, but
|
||||
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
General Public License for more details.
|
||||
</para>
|
||||
<para>
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
</para>
|
||||
</refsect1>
|
||||
</refentry>
|
@ -48,24 +48,7 @@
|
||||
#endif /* _SAMBA_BUILD_ */
|
||||
|
||||
#ifndef _TALLOC_SAMBA3
|
||||
#include "config.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#ifdef HAVE_STDINT_H
|
||||
#include <stdint.h>
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_STDARG_H)
|
||||
#include <stdarg.h>
|
||||
#elif defined (HAVE_VARARGS_H)
|
||||
#include <varargs.h>
|
||||
#else
|
||||
#error "no var arg header"
|
||||
#endif
|
||||
|
||||
#include "replace.h"
|
||||
#include "talloc.h"
|
||||
#endif /* not _TALLOC_SAMBA3 */
|
||||
|
||||
@ -754,6 +737,18 @@ void *_talloc_steal(const void *new_ctx, const void *ptr)
|
||||
return discard_const_p(void, ptr);
|
||||
}
|
||||
|
||||
/*
|
||||
a wrapper around talloc_steal() for situations where you are moving a pointer
|
||||
between two structures, and want the old pointer to be set to NULL
|
||||
*/
|
||||
void *_talloc_move(const void *new_ctx, const void *_pptr)
|
||||
{
|
||||
const void **pptr = (const void **)_pptr;
|
||||
void *ret = _talloc_steal(new_ctx, *pptr);
|
||||
(*pptr) = NULL;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
return the total size of a talloc pool (subtree)
|
||||
*/
|
@ -65,15 +65,16 @@ typedef void TALLOC_CTX;
|
||||
/* this extremely strange macro is to avoid some braindamaged warning
|
||||
stupidity in gcc 4.1.x */
|
||||
#define talloc_steal(ctx, ptr) ({ _TALLOC_TYPEOF(ptr) __talloc_steal_ret = (_TALLOC_TYPEOF(ptr))_talloc_steal((ctx),(ptr)); __talloc_steal_ret; })
|
||||
#define talloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_reference((ctx),(ptr))
|
||||
#else
|
||||
#define talloc_set_destructor(ptr, function) \
|
||||
_talloc_set_destructor((ptr), (int (*)(void *))(function))
|
||||
#define _TALLOC_TYPEOF(ptr) void *
|
||||
#define talloc_steal(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_steal((ctx),(ptr))
|
||||
#define talloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_reference((ctx),(ptr))
|
||||
#endif
|
||||
|
||||
#define talloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_reference((ctx),(ptr))
|
||||
#define talloc_move(ctx, ptr) (_TALLOC_TYPEOF(*(ptr)))_talloc_move((ctx),(void *)(ptr))
|
||||
|
||||
/* useful macros for creating type checked pointers */
|
||||
#define talloc(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type)
|
||||
#define talloc_size(ctx, size) talloc_named_const(ctx, size, __location__)
|
||||
@ -127,6 +128,7 @@ int talloc_free(void *ptr);
|
||||
void talloc_free_children(void *ptr);
|
||||
void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name);
|
||||
void *_talloc_steal(const void *new_ctx, const void *ptr);
|
||||
void *_talloc_move(const void *new_ctx, const void *pptr);
|
||||
size_t talloc_total_size(const void *ptr);
|
||||
size_t talloc_total_blocks(const void *ptr);
|
||||
void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
|
10
source3/lib/talloc/talloc.pc.in
Normal file
10
source3/lib/talloc/talloc.pc.in
Normal file
@ -0,0 +1,10 @@
|
||||
prefix=@prefix@
|
||||
exec_prefix=@exec_prefix@
|
||||
libdir=@libdir@
|
||||
includedir=@includedir@
|
||||
|
||||
Name: talloc
|
||||
Description: A hierarchical pool based memory system with destructors
|
||||
Version: 4.0
|
||||
Libs: @LIBS@ -L${libdir} -ltalloc
|
||||
Cflags: -I${includedir} @CFLAGS@
|
637
source3/lib/talloc/talloc_guide.txt
Normal file
637
source3/lib/talloc/talloc_guide.txt
Normal file
@ -0,0 +1,637 @@
|
||||
Using talloc in Samba4
|
||||
----------------------
|
||||
|
||||
Andrew Tridgell
|
||||
September 2004
|
||||
|
||||
The most current version of this document is available at
|
||||
http://samba.org/ftp/unpacked/samba4/source/lib/talloc/talloc_guide.txt
|
||||
|
||||
If you are used to the "old" talloc from Samba3 before 3.0.20 then please read
|
||||
this carefully, as talloc has changed a lot. With 3.0.20 (or 3.0.14?) the
|
||||
Samba4 talloc has been ported back to Samba3, so this guide applies to both.
|
||||
|
||||
The new talloc is a hierarchical, reference counted memory pool system
|
||||
with destructors. Quite a mounthful really, but not too bad once you
|
||||
get used to it.
|
||||
|
||||
Perhaps the biggest change from Samba3 is that there is no distinction
|
||||
between a "talloc context" and a "talloc pointer". Any pointer
|
||||
returned from talloc() is itself a valid talloc context. This means
|
||||
you can do this:
|
||||
|
||||
struct foo *X = talloc(mem_ctx, struct foo);
|
||||
X->name = talloc_strdup(X, "foo");
|
||||
|
||||
and the pointer X->name would be a "child" of the talloc context "X"
|
||||
which is itself a child of mem_ctx. So if you do talloc_free(mem_ctx)
|
||||
then it is all destroyed, whereas if you do talloc_free(X) then just X
|
||||
and X->name are destroyed, and if you do talloc_free(X->name) then
|
||||
just the name element of X is destroyed.
|
||||
|
||||
If you think about this, then what this effectively gives you is an
|
||||
n-ary tree, where you can free any part of the tree with
|
||||
talloc_free().
|
||||
|
||||
If you find this confusing, then I suggest you run the testsuite to
|
||||
watch talloc in action. You may also like to add your own tests to
|
||||
testsuite.c to clarify how some particular situation is handled.
|
||||
|
||||
|
||||
Performance
|
||||
-----------
|
||||
|
||||
All the additional features of talloc() over malloc() do come at a
|
||||
price. We have a simple performance test in Samba4 that measures
|
||||
talloc() versus malloc() performance, and it seems that talloc() is
|
||||
about 4% slower than malloc() on my x86 Debian Linux box. For Samba,
|
||||
the great reduction in code complexity that we get by using talloc
|
||||
makes this worthwhile, especially as the total overhead of
|
||||
talloc/malloc in Samba is already quite small.
|
||||
|
||||
|
||||
talloc API
|
||||
----------
|
||||
|
||||
The following is a complete guide to the talloc API. Read it all at
|
||||
least twice.
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
(type *)talloc(const void *context, type);
|
||||
|
||||
The talloc() macro is the core of the talloc library. It takes a
|
||||
memory context and a type, and returns a pointer to a new area of
|
||||
memory of the given type.
|
||||
|
||||
The returned pointer is itself a talloc context, so you can use it as
|
||||
the context argument to more calls to talloc if you wish.
|
||||
|
||||
The returned pointer is a "child" of the supplied context. This means
|
||||
that if you talloc_free() the context then the new child disappears as
|
||||
well. Alternatively you can free just the child.
|
||||
|
||||
The context argument to talloc() can be NULL, in which case a new top
|
||||
level context is created.
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
void *talloc_size(const void *context, size_t size);
|
||||
|
||||
The function talloc_size() should be used when you don't have a
|
||||
convenient type to pass to talloc(). Unlike talloc(), it is not type
|
||||
safe (as it returns a void *), so you are on your own for type checking.
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
(typeof(ptr)) talloc_ptrtype(const void *ctx, ptr);
|
||||
|
||||
The talloc_ptrtype() macro should be used when you have a pointer and
|
||||
want to allocate memory to point at with this pointer. When compiling
|
||||
with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_size()
|
||||
and talloc_get_name() will return the current location in the source file.
|
||||
and not the type.
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
int talloc_free(void *ptr);
|
||||
|
||||
The talloc_free() function frees a piece of talloc memory, and all its
|
||||
children. You can call talloc_free() on any pointer returned by
|
||||
talloc().
|
||||
|
||||
The return value of talloc_free() indicates success or failure, with 0
|
||||
returned for success and -1 for failure. The only possible failure
|
||||
condition is if the pointer had a destructor attached to it and the
|
||||
destructor returned -1. See talloc_set_destructor() for details on
|
||||
destructors.
|
||||
|
||||
If this pointer has an additional parent when talloc_free() is called
|
||||
then the memory is not actually released, but instead the most
|
||||
recently established parent is destroyed. See talloc_reference() for
|
||||
details on establishing additional parents.
|
||||
|
||||
For more control on which parent is removed, see talloc_unlink()
|
||||
|
||||
talloc_free() operates recursively on its children.
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
int talloc_free_children(void *ptr);
|
||||
|
||||
The talloc_free_children() walks along the list of all children of a
|
||||
talloc context and talloc_free()s only the children, not the context
|
||||
itself.
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
void *talloc_reference(const void *context, const void *ptr);
|
||||
|
||||
The talloc_reference() function makes "context" an additional parent
|
||||
of "ptr".
|
||||
|
||||
The return value of talloc_reference() is always the original pointer
|
||||
"ptr", unless talloc ran out of memory in creating the reference in
|
||||
which case it will return NULL (each additional reference consumes
|
||||
around 48 bytes of memory on intel x86 platforms).
|
||||
|
||||
If "ptr" is NULL, then the function is a no-op, and simply returns NULL.
|
||||
|
||||
After creating a reference you can free it in one of the following
|
||||
ways:
|
||||
|
||||
- you can talloc_free() any parent of the original pointer. That
|
||||
will reduce the number of parents of this pointer by 1, and will
|
||||
cause this pointer to be freed if it runs out of parents.
|
||||
|
||||
- you can talloc_free() the pointer itself. That will destroy the
|
||||
most recently established parent to the pointer and leave the
|
||||
pointer as a child of its current parent.
|
||||
|
||||
For more control on which parent to remove, see talloc_unlink()
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
int talloc_unlink(const void *context, const void *ptr);
|
||||
|
||||
The talloc_unlink() function removes a specific parent from ptr. The
|
||||
context passed must either be a context used in talloc_reference()
|
||||
with this pointer, or must be a direct parent of ptr.
|
||||
|
||||
Note that if the parent has already been removed using talloc_free()
|
||||
then this function will fail and will return -1. Likewise, if "ptr"
|
||||
is NULL, then the function will make no modifications and return -1.
|
||||
|
||||
Usually you can just use talloc_free() instead of talloc_unlink(), but
|
||||
sometimes it is useful to have the additional control on which parent
|
||||
is removed.
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
void talloc_set_destructor(const void *ptr, int (*destructor)(void *));
|
||||
|
||||
The function talloc_set_destructor() sets the "destructor" for the
|
||||
pointer "ptr". A destructor is a function that is called when the
|
||||
memory used by a pointer is about to be released. The destructor
|
||||
receives the pointer as an argument, and should return 0 for success
|
||||
and -1 for failure.
|
||||
|
||||
The destructor can do anything it wants to, including freeing other
|
||||
pieces of memory. A common use for destructors is to clean up
|
||||
operating system resources (such as open file descriptors) contained
|
||||
in the structure the destructor is placed on.
|
||||
|
||||
You can only place one destructor on a pointer. If you need more than
|
||||
one destructor then you can create a zero-length child of the pointer
|
||||
and place an additional destructor on that.
|
||||
|
||||
To remove a destructor call talloc_set_destructor() with NULL for the
|
||||
destructor.
|
||||
|
||||
If your destructor attempts to talloc_free() the pointer that it is
|
||||
the destructor for then talloc_free() will return -1 and the free will
|
||||
be ignored. This would be a pointless operation anyway, as the
|
||||
destructor is only called when the memory is just about to go away.
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
int talloc_increase_ref_count(const void *ptr);
|
||||
|
||||
The talloc_increase_ref_count(ptr) function is exactly equivalent to:
|
||||
|
||||
talloc_reference(NULL, ptr);
|
||||
|
||||
You can use either syntax, depending on which you think is clearer in
|
||||
your code.
|
||||
|
||||
It returns 0 on success and -1 on failure.
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
size_t talloc_reference_count(const void *ptr);
|
||||
|
||||
Return the number of references to the pointer.
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
void talloc_set_name(const void *ptr, const char *fmt, ...);
|
||||
|
||||
Each talloc pointer has a "name". The name is used principally for
|
||||
debugging purposes, although it is also possible to set and get the
|
||||
name on a pointer in as a way of "marking" pointers in your code.
|
||||
|
||||
The main use for names on pointer is for "talloc reports". See
|
||||
talloc_report() and talloc_report_full() for details. Also see
|
||||
talloc_enable_leak_report() and talloc_enable_leak_report_full().
|
||||
|
||||
The talloc_set_name() function allocates memory as a child of the
|
||||
pointer. It is logically equivalent to:
|
||||
talloc_set_name_const(ptr, talloc_asprintf(ptr, fmt, ...));
|
||||
|
||||
Note that multiple calls to talloc_set_name() will allocate more
|
||||
memory without releasing the name. All of the memory is released when
|
||||
the ptr is freed using talloc_free().
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
void talloc_set_name_const(const void *ptr, const char *name);
|
||||
|
||||
The function talloc_set_name_const() is just like talloc_set_name(),
|
||||
but it takes a string constant, and is much faster. It is extensively
|
||||
used by the "auto naming" macros, such as talloc_p().
|
||||
|
||||
This function does not allocate any memory. It just copies the
|
||||
supplied pointer into the internal representation of the talloc
|
||||
ptr. This means you must not pass a name pointer to memory that will
|
||||
disappear before the ptr is freed with talloc_free().
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
void *talloc_named(const void *context, size_t size, const char *fmt, ...);
|
||||
|
||||
The talloc_named() function creates a named talloc pointer. It is
|
||||
equivalent to:
|
||||
|
||||
ptr = talloc_size(context, size);
|
||||
talloc_set_name(ptr, fmt, ....);
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
void *talloc_named_const(const void *context, size_t size, const char *name);
|
||||
|
||||
This is equivalent to:
|
||||
|
||||
ptr = talloc_size(context, size);
|
||||
talloc_set_name_const(ptr, name);
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
const char *talloc_get_name(const void *ptr);
|
||||
|
||||
This returns the current name for the given talloc pointer. See
|
||||
talloc_set_name() for details.
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
void *talloc_init(const char *fmt, ...);
|
||||
|
||||
This function creates a zero length named talloc context as a top
|
||||
level context. It is equivalent to:
|
||||
|
||||
talloc_named(NULL, 0, fmt, ...);
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
void *talloc_new(void *ctx);
|
||||
|
||||
This is a utility macro that creates a new memory context hanging
|
||||
off an exiting context, automatically naming it "talloc_new: __location__"
|
||||
where __location__ is the source line it is called from. It is
|
||||
particularly useful for creating a new temporary working context.
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
(type *)talloc_realloc(const void *context, void *ptr, type, count);
|
||||
|
||||
The talloc_realloc() macro changes the size of a talloc
|
||||
pointer. The "count" argument is the number of elements of type "type"
|
||||
that you want the resulting pointer to hold.
|
||||
|
||||
talloc_realloc() has the following equivalences:
|
||||
|
||||
talloc_realloc(context, NULL, type, 1) ==> talloc(context, type);
|
||||
talloc_realloc(context, NULL, type, N) ==> talloc_array(context, type, N);
|
||||
talloc_realloc(context, ptr, type, 0) ==> talloc_free(ptr);
|
||||
|
||||
The "context" argument is only used if "ptr" is NULL, otherwise it is
|
||||
ignored.
|
||||
|
||||
talloc_realloc() returns the new pointer, or NULL on failure. The call
|
||||
will fail either due to a lack of memory, or because the pointer has
|
||||
more than one parent (see talloc_reference()).
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
void *talloc_realloc_size(const void *context, void *ptr, size_t size);
|
||||
|
||||
the talloc_realloc_size() function is useful when the type is not
|
||||
known so the typesafe talloc_realloc() cannot be used.
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
void *talloc_steal(const void *new_ctx, const void *ptr);
|
||||
|
||||
The talloc_steal() function changes the parent context of a talloc
|
||||
pointer. It is typically used when the context that the pointer is
|
||||
currently a child of is going to be freed and you wish to keep the
|
||||
memory for a longer time.
|
||||
|
||||
The talloc_steal() function returns the pointer that you pass it. It
|
||||
does not have any failure modes.
|
||||
|
||||
NOTE: It is possible to produce loops in the parent/child relationship
|
||||
if you are not careful with talloc_steal(). No guarantees are provided
|
||||
as to your sanity or the safety of your data if you do this.
|
||||
|
||||
talloc_steal (new_ctx, NULL) will return NULL with no sideeffects.
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
size_t talloc_total_size(const void *ptr);
|
||||
|
||||
The talloc_total_size() function returns the total size in bytes used
|
||||
by this pointer and all child pointers. Mostly useful for debugging.
|
||||
|
||||
Passing NULL is allowed, but it will only give a meaningful result if
|
||||
talloc_enable_leak_report() or talloc_enable_leak_report_full() has
|
||||
been called.
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
size_t talloc_total_blocks(const void *ptr);
|
||||
|
||||
The talloc_total_blocks() function returns the total memory block
|
||||
count used by this pointer and all child pointers. Mostly useful for
|
||||
debugging.
|
||||
|
||||
Passing NULL is allowed, but it will only give a meaningful result if
|
||||
talloc_enable_leak_report() or talloc_enable_leak_report_full() has
|
||||
been called.
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
|
||||
void (*callback)(const void *ptr,
|
||||
int depth, int max_depth,
|
||||
int is_ref,
|
||||
void *priv),
|
||||
void *priv);
|
||||
|
||||
This provides a more flexible reports than talloc_report(). It
|
||||
will recursively call the callback for the entire tree of memory
|
||||
referenced by the pointer. References in the tree are passed with
|
||||
is_ref = 1 and the pointer that is referenced.
|
||||
|
||||
You can pass NULL for the pointer, in which case a report is
|
||||
printed for the top level memory context, but only if
|
||||
talloc_enable_leak_report() or talloc_enable_leak_report_full()
|
||||
has been called.
|
||||
|
||||
The recursion is stopped when depth >= max_depth.
|
||||
max_depth = -1 means only stop at leaf nodes.
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f);
|
||||
|
||||
This provides a more flexible reports than talloc_report(). It
|
||||
will let you specify the depth and max_depth.
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
void talloc_report(const void *ptr, FILE *f);
|
||||
|
||||
The talloc_report() function prints a summary report of all memory
|
||||
used by ptr. One line of report is printed for each immediate child of
|
||||
ptr, showing the total memory and number of blocks used by that child.
|
||||
|
||||
You can pass NULL for the pointer, in which case a report is printed
|
||||
for the top level memory context, but only if
|
||||
talloc_enable_leak_report() or talloc_enable_leak_report_full() has
|
||||
been called.
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
void talloc_report_full(const void *ptr, FILE *f);
|
||||
|
||||
This provides a more detailed report than talloc_report(). It will
|
||||
recursively print the ensire tree of memory referenced by the
|
||||
pointer. References in the tree are shown by giving the name of the
|
||||
pointer that is referenced.
|
||||
|
||||
You can pass NULL for the pointer, in which case a report is printed
|
||||
for the top level memory context, but only if
|
||||
talloc_enable_leak_report() or talloc_enable_leak_report_full() has
|
||||
been called.
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
void talloc_enable_leak_report(void);
|
||||
|
||||
This enables calling of talloc_report(NULL, stderr) when the program
|
||||
exits. In Samba4 this is enabled by using the --leak-report command
|
||||
line option.
|
||||
|
||||
For it to be useful, this function must be called before any other
|
||||
talloc function as it establishes a "null context" that acts as the
|
||||
top of the tree. If you don't call this function first then passing
|
||||
NULL to talloc_report() or talloc_report_full() won't give you the
|
||||
full tree printout.
|
||||
|
||||
Here is a typical talloc report:
|
||||
|
||||
talloc report on 'null_context' (total 267 bytes in 15 blocks)
|
||||
libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
|
||||
libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
|
||||
iconv(UTF8,CP850) contains 42 bytes in 2 blocks
|
||||
libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
|
||||
iconv(CP850,UTF8) contains 42 bytes in 2 blocks
|
||||
iconv(UTF8,UTF-16LE) contains 45 bytes in 2 blocks
|
||||
iconv(UTF-16LE,UTF8) contains 45 bytes in 2 blocks
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
void talloc_enable_leak_report_full(void);
|
||||
|
||||
This enables calling of talloc_report_full(NULL, stderr) when the
|
||||
program exits. In Samba4 this is enabled by using the
|
||||
--leak-report-full command line option.
|
||||
|
||||
For it to be useful, this function must be called before any other
|
||||
talloc function as it establishes a "null context" that acts as the
|
||||
top of the tree. If you don't call this function first then passing
|
||||
NULL to talloc_report() or talloc_report_full() won't give you the
|
||||
full tree printout.
|
||||
|
||||
Here is a typical full report:
|
||||
|
||||
full talloc report on 'root' (total 18 bytes in 8 blocks)
|
||||
p1 contains 18 bytes in 7 blocks (ref 0)
|
||||
r1 contains 13 bytes in 2 blocks (ref 0)
|
||||
reference to: p2
|
||||
p2 contains 1 bytes in 1 blocks (ref 1)
|
||||
x3 contains 1 bytes in 1 blocks (ref 0)
|
||||
x2 contains 1 bytes in 1 blocks (ref 0)
|
||||
x1 contains 1 bytes in 1 blocks (ref 0)
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
void talloc_enable_null_tracking(void);
|
||||
|
||||
This enables tracking of the NULL memory context without enabling leak
|
||||
reporting on exit. Useful for when you want to do your own leak
|
||||
reporting call via talloc_report_null_full();
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
void talloc_disable_null_tracking(void);
|
||||
|
||||
This disables tracking of the NULL memory context.
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
(type *)talloc_zero(const void *ctx, type);
|
||||
|
||||
The talloc_zero() macro is equivalent to:
|
||||
|
||||
ptr = talloc(ctx, type);
|
||||
if (ptr) memset(ptr, 0, sizeof(type));
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
void *talloc_zero_size(const void *ctx, size_t size)
|
||||
|
||||
The talloc_zero_size() function is useful when you don't have a known type
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
void *talloc_memdup(const void *ctx, const void *p, size_t size);
|
||||
|
||||
The talloc_memdup() function is equivalent to:
|
||||
|
||||
ptr = talloc_size(ctx, size);
|
||||
if (ptr) memcpy(ptr, p, size);
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
char *talloc_strdup(const void *ctx, const char *p);
|
||||
|
||||
The talloc_strdup() function is equivalent to:
|
||||
|
||||
ptr = talloc_size(ctx, strlen(p)+1);
|
||||
if (ptr) memcpy(ptr, p, strlen(p)+1);
|
||||
|
||||
This functions sets the name of the new pointer to the passed
|
||||
string. This is equivalent to:
|
||||
talloc_set_name_const(ptr, ptr)
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
char *talloc_strndup(const void *t, const char *p, size_t n);
|
||||
|
||||
The talloc_strndup() function is the talloc equivalent of the C
|
||||
library function strndup()
|
||||
|
||||
This functions sets the name of the new pointer to the passed
|
||||
string. This is equivalent to:
|
||||
talloc_set_name_const(ptr, ptr)
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
char *talloc_vasprintf(const void *t, const char *fmt, va_list ap);
|
||||
|
||||
The talloc_vasprintf() function is the talloc equivalent of the C
|
||||
library function vasprintf()
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
char *talloc_asprintf(const void *t, const char *fmt, ...);
|
||||
|
||||
The talloc_asprintf() function is the talloc equivalent of the C
|
||||
library function asprintf()
|
||||
|
||||
This functions sets the name of the new pointer to the passed
|
||||
string. This is equivalent to:
|
||||
talloc_set_name_const(ptr, ptr)
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
char *talloc_asprintf_append(char *s, const char *fmt, ...);
|
||||
|
||||
The talloc_asprintf_append() function appends the given formatted
|
||||
string to the given string.
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
(type *)talloc_array(const void *ctx, type, uint_t count);
|
||||
|
||||
The talloc_array() macro is equivalent to:
|
||||
|
||||
(type *)talloc_size(ctx, sizeof(type) * count);
|
||||
|
||||
except that it provides integer overflow protection for the multiply,
|
||||
returning NULL if the multiply overflows.
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
void *talloc_array_size(const void *ctx, size_t size, uint_t count);
|
||||
|
||||
The talloc_array_size() function is useful when the type is not
|
||||
known. It operates in the same way as talloc_array(), but takes a size
|
||||
instead of a type.
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
(typeof(ptr)) talloc_array_ptrtype(const void *ctx, ptr, uint_t count);
|
||||
|
||||
The talloc_ptrtype() macro should be used when you have a pointer to an array
|
||||
and want to allocate memory of an array to point at with this pointer. When compiling
|
||||
with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_array_size()
|
||||
and talloc_get_name() will return the current location in the source file.
|
||||
and not the type.
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
void *talloc_realloc_fn(const void *ctx, void *ptr, size_t size);
|
||||
|
||||
This is a non-macro version of talloc_realloc(), which is useful
|
||||
as libraries sometimes want a ralloc function pointer. A realloc()
|
||||
implementation encapsulates the functionality of malloc(), free() and
|
||||
realloc() in one call, which is why it is useful to be able to pass
|
||||
around a single function pointer.
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
void *talloc_autofree_context(void);
|
||||
|
||||
This is a handy utility function that returns a talloc context
|
||||
which will be automatically freed on program exit. This can be used
|
||||
to reduce the noise in memory leak reports.
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
void *talloc_check_name(const void *ptr, const char *name);
|
||||
|
||||
This function checks if a pointer has the specified name. If it does
|
||||
then the pointer is returned. It it doesn't then NULL is returned.
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
(type *)talloc_get_type(const void *ptr, type);
|
||||
|
||||
This macro allows you to do type checking on talloc pointers. It is
|
||||
particularly useful for void* private pointers. It is equivalent to
|
||||
this:
|
||||
|
||||
(type *)talloc_check_name(ptr, #type)
|
||||
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
talloc_set_type(const void *ptr, type);
|
||||
|
||||
This macro allows you to force the name of a pointer to be a
|
||||
particular type. This can be used in conjunction with
|
||||
talloc_get_type() to do type checking on void* pointers.
|
||||
|
||||
It is equivalent to this:
|
||||
talloc_set_name_const(ptr, #type)
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
talloc_get_size(const void *ctx);
|
||||
|
||||
This function lets you know the amount of memory alloced so far by
|
||||
this context. It does NOT account for subcontext memory.
|
||||
This can be used to calculate the size of an array.
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
void *talloc_find_parent_byname(const void *ctx, const char *name);
|
||||
|
||||
Find a parent memory context of the current context that has the given
|
||||
name. This can be very useful in complex programs where it may be
|
||||
difficult to pass all information down to the level you need, but you
|
||||
know the structure you want is a parent of another context.
|
||||
|
||||
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
(type *)talloc_find_parent_bytype(ctx, type);
|
||||
|
||||
Like talloc_find_parent_byname() but takes a type, making it typesafe.
|
||||
|
@ -28,30 +28,10 @@
|
||||
#include "version.h"
|
||||
#endif /* _SAMBA_BUILD_ */
|
||||
|
||||
#include "config.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef HAVE_STDARG_H
|
||||
#include <stdarg.h>
|
||||
#endif
|
||||
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "replace.h"
|
||||
#include "system/time.h"
|
||||
#include "talloc.h"
|
||||
|
||||
#ifndef False
|
||||
#define False 0
|
||||
#endif
|
||||
#ifndef True
|
||||
#define True 1
|
||||
#endif
|
||||
#ifndef BOOL
|
||||
#define BOOL int
|
||||
#endif
|
||||
|
||||
struct torture_context;
|
||||
|
||||
static struct timeval timeval_current(void)
|
||||
@ -84,7 +64,7 @@ static double timeval_elapsed(struct timeval *tv)
|
||||
(unsigned)talloc_total_size(ptr), \
|
||||
(unsigned)tsize); \
|
||||
talloc_report_full(ptr, stdout); \
|
||||
return False; \
|
||||
return false; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
@ -95,7 +75,7 @@ static double timeval_elapsed(struct timeval *tv)
|
||||
(unsigned)talloc_total_blocks(ptr), \
|
||||
(unsigned)tblocks); \
|
||||
talloc_report_full(ptr, stdout); \
|
||||
return False; \
|
||||
return false; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
@ -108,7 +88,7 @@ static double timeval_elapsed(struct timeval *tv)
|
||||
talloc_report_full(ptr, stdout); \
|
||||
talloc_report_full(parent, stdout); \
|
||||
talloc_report_full(NULL, stdout); \
|
||||
return False; \
|
||||
return false; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
@ -116,7 +96,7 @@ static double timeval_elapsed(struct timeval *tv)
|
||||
/*
|
||||
test references
|
||||
*/
|
||||
static BOOL test_ref1(void)
|
||||
static bool test_ref1(void)
|
||||
{
|
||||
void *root, *p1, *p2, *ref, *r1;
|
||||
|
||||
@ -157,7 +137,7 @@ static BOOL test_ref1(void)
|
||||
|
||||
printf("Testing NULL\n");
|
||||
if (talloc_reference(root, NULL)) {
|
||||
return False;
|
||||
return false;
|
||||
}
|
||||
|
||||
CHECK_BLOCKS(root, 1);
|
||||
@ -166,13 +146,13 @@ static BOOL test_ref1(void)
|
||||
|
||||
talloc_free(root);
|
||||
|
||||
return True;
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
test references
|
||||
*/
|
||||
static BOOL test_ref2(void)
|
||||
static bool test_ref2(void)
|
||||
{
|
||||
void *root, *p1, *p2, *ref, *r1;
|
||||
|
||||
@ -222,13 +202,13 @@ static BOOL test_ref2(void)
|
||||
|
||||
talloc_free(root);
|
||||
|
||||
return True;
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
test references
|
||||
*/
|
||||
static BOOL test_ref3(void)
|
||||
static bool test_ref3(void)
|
||||
{
|
||||
void *root, *p1, *p2, *ref, *r1;
|
||||
|
||||
@ -260,13 +240,13 @@ static BOOL test_ref3(void)
|
||||
|
||||
talloc_free(root);
|
||||
|
||||
return True;
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
test references
|
||||
*/
|
||||
static BOOL test_ref4(void)
|
||||
static bool test_ref4(void)
|
||||
{
|
||||
void *root, *p1, *p2, *ref, *r1;
|
||||
|
||||
@ -308,14 +288,14 @@ static BOOL test_ref4(void)
|
||||
|
||||
talloc_free(root);
|
||||
|
||||
return True;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
test references
|
||||
*/
|
||||
static BOOL test_unlink1(void)
|
||||
static bool test_unlink1(void)
|
||||
{
|
||||
void *root, *p1, *p2, *ref, *r1;
|
||||
|
||||
@ -352,7 +332,7 @@ static BOOL test_unlink1(void)
|
||||
|
||||
talloc_free(root);
|
||||
|
||||
return True;
|
||||
return true;
|
||||
}
|
||||
|
||||
static int fail_destructor(void *ptr)
|
||||
@ -363,7 +343,7 @@ static int fail_destructor(void *ptr)
|
||||
/*
|
||||
miscellaneous tests to try to get a higher test coverage percentage
|
||||
*/
|
||||
static BOOL test_misc(void)
|
||||
static bool test_misc(void)
|
||||
{
|
||||
void *root, *p1;
|
||||
char *p2;
|
||||
@ -377,7 +357,7 @@ static BOOL test_misc(void)
|
||||
p1 = talloc_size(root, 0x7fffffff);
|
||||
if (p1) {
|
||||
printf("failed: large talloc allowed\n");
|
||||
return False;
|
||||
return false;
|
||||
}
|
||||
|
||||
p1 = talloc_strdup(root, "foo");
|
||||
@ -395,11 +375,11 @@ static BOOL test_misc(void)
|
||||
p2 = talloc_strdup(p1, "foo");
|
||||
if (talloc_unlink(root, p2) != -1) {
|
||||
printf("failed: talloc_unlink() of non-reference context should return -1\n");
|
||||
return False;
|
||||
return false;
|
||||
}
|
||||
if (talloc_unlink(p1, p2) != 0) {
|
||||
printf("failed: talloc_unlink() of parent should succeed\n");
|
||||
return False;
|
||||
return false;
|
||||
}
|
||||
talloc_free(p1);
|
||||
CHECK_BLOCKS(p1, 1);
|
||||
@ -409,7 +389,7 @@ static BOOL test_misc(void)
|
||||
if (strcmp(talloc_get_name(p1), "my name is foo") != 0) {
|
||||
printf("failed: wrong name after talloc_set_name(my name is foo) - '%s'=>'%s'\n",
|
||||
(name?name:"NULL"), talloc_get_name(p1));
|
||||
return False;
|
||||
return false;
|
||||
}
|
||||
CHECK_BLOCKS(p1, 2);
|
||||
CHECK_BLOCKS(root, 3);
|
||||
@ -418,7 +398,7 @@ static BOOL test_misc(void)
|
||||
if (strcmp(talloc_get_name(p1), "UNNAMED") != 0) {
|
||||
printf("failed: wrong name after talloc_set_name(NULL) - '%s'\n",
|
||||
talloc_get_name(p1));
|
||||
return False;
|
||||
return false;
|
||||
}
|
||||
CHECK_BLOCKS(p1, 2);
|
||||
CHECK_BLOCKS(root, 3);
|
||||
@ -426,13 +406,13 @@ static BOOL test_misc(void)
|
||||
|
||||
if (talloc_free(NULL) != -1) {
|
||||
printf("talloc_free(NULL) should give -1\n");
|
||||
return False;
|
||||
return false;
|
||||
}
|
||||
|
||||
talloc_set_destructor(p1, fail_destructor);
|
||||
if (talloc_free(p1) != -1) {
|
||||
printf("Failed destructor should cause talloc_free to fail\n");
|
||||
return False;
|
||||
return false;
|
||||
}
|
||||
talloc_set_destructor(p1, NULL);
|
||||
|
||||
@ -442,24 +422,24 @@ static BOOL test_misc(void)
|
||||
p2 = (char *)talloc_zero_size(p1, 20);
|
||||
if (p2[19] != 0) {
|
||||
printf("Failed to give zero memory\n");
|
||||
return False;
|
||||
return false;
|
||||
}
|
||||
talloc_free(p2);
|
||||
|
||||
if (talloc_strdup(root, NULL) != NULL) {
|
||||
printf("failed: strdup on NULL should give NULL\n");
|
||||
return False;
|
||||
return false;
|
||||
}
|
||||
|
||||
p2 = talloc_strndup(p1, "foo", 2);
|
||||
if (strcmp("fo", p2) != 0) {
|
||||
printf("failed: strndup doesn't work\n");
|
||||
return False;
|
||||
return false;
|
||||
}
|
||||
p2 = talloc_asprintf_append(p2, "o%c", 'd');
|
||||
if (strcmp("food", p2) != 0) {
|
||||
printf("failed: talloc_asprintf_append doesn't work\n");
|
||||
return False;
|
||||
return false;
|
||||
}
|
||||
CHECK_BLOCKS(p2, 1);
|
||||
CHECK_BLOCKS(p1, 3);
|
||||
@ -467,7 +447,7 @@ static BOOL test_misc(void)
|
||||
p2 = talloc_asprintf_append(NULL, "hello %s", "world");
|
||||
if (strcmp("hello world", p2) != 0) {
|
||||
printf("failed: talloc_asprintf_append doesn't work\n");
|
||||
return False;
|
||||
return false;
|
||||
}
|
||||
CHECK_BLOCKS(p2, 1);
|
||||
CHECK_BLOCKS(p1, 3);
|
||||
@ -476,13 +456,13 @@ static BOOL test_misc(void)
|
||||
d = talloc_array(p1, double, 0x20000000);
|
||||
if (d) {
|
||||
printf("failed: integer overflow not detected\n");
|
||||
return False;
|
||||
return false;
|
||||
}
|
||||
|
||||
d = talloc_realloc(p1, d, double, 0x20000000);
|
||||
if (d) {
|
||||
printf("failed: integer overflow not detected\n");
|
||||
return False;
|
||||
return false;
|
||||
}
|
||||
|
||||
talloc_free(p1);
|
||||
@ -497,7 +477,7 @@ static BOOL test_misc(void)
|
||||
p2 = talloc_asprintf(p1, "my test '%s'", "string");
|
||||
if (strcmp(p2, "my test 'string'") != 0) {
|
||||
printf("failed: talloc_asprintf(\"my test '%%s'\", \"string\") gave: \"%s\"\n", p2);
|
||||
return False;
|
||||
return false;
|
||||
}
|
||||
CHECK_BLOCKS(p1, 3);
|
||||
CHECK_SIZE(p2, 17);
|
||||
@ -532,7 +512,7 @@ static BOOL test_misc(void)
|
||||
|
||||
if (talloc_unlink(root, NULL) != -1) {
|
||||
printf("failed: talloc_unlink(root, NULL) == -1\n");
|
||||
return False;
|
||||
return false;
|
||||
}
|
||||
|
||||
talloc_report(root, stdout);
|
||||
@ -547,14 +527,14 @@ static BOOL test_misc(void)
|
||||
talloc_enable_leak_report();
|
||||
talloc_enable_leak_report_full();
|
||||
|
||||
return True;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
test realloc
|
||||
*/
|
||||
static BOOL test_realloc(void)
|
||||
static bool test_realloc(void)
|
||||
{
|
||||
void *root, *p1, *p2;
|
||||
|
||||
@ -586,7 +566,7 @@ static BOOL test_realloc(void)
|
||||
talloc_increase_ref_count(p2);
|
||||
if (talloc_realloc_size(NULL, p2, 5) != NULL) {
|
||||
printf("failed: talloc_realloc() on a referenced pointer should fail\n");
|
||||
return False;
|
||||
return false;
|
||||
}
|
||||
CHECK_BLOCKS(p1, 4);
|
||||
|
||||
@ -596,7 +576,7 @@ static BOOL test_realloc(void)
|
||||
|
||||
if (talloc_realloc_size(NULL, p1, 0x7fffffff) != NULL) {
|
||||
printf("failed: oversize talloc should fail\n");
|
||||
return False;
|
||||
return false;
|
||||
}
|
||||
|
||||
talloc_realloc_size(NULL, p1, 0);
|
||||
@ -606,13 +586,13 @@ static BOOL test_realloc(void)
|
||||
|
||||
talloc_free(root);
|
||||
|
||||
return True;
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
test realloc with a child
|
||||
*/
|
||||
static BOOL test_realloc_child(void)
|
||||
static bool test_realloc_child(void)
|
||||
{
|
||||
void *root;
|
||||
struct el2 {
|
||||
@ -650,14 +630,14 @@ static BOOL test_realloc_child(void)
|
||||
|
||||
talloc_free(root);
|
||||
|
||||
return True;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
test type checking
|
||||
*/
|
||||
static BOOL test_type(void)
|
||||
static bool test_type(void)
|
||||
{
|
||||
void *root;
|
||||
struct el1 {
|
||||
@ -678,27 +658,27 @@ static BOOL test_type(void)
|
||||
|
||||
if (talloc_get_type(el1, struct el1) != el1) {
|
||||
printf("type check failed on el1\n");
|
||||
return False;
|
||||
return false;
|
||||
}
|
||||
if (talloc_get_type(el1, struct el2) != NULL) {
|
||||
printf("type check failed on el1 with el2\n");
|
||||
return False;
|
||||
return false;
|
||||
}
|
||||
talloc_set_type(el1, struct el2);
|
||||
if (talloc_get_type(el1, struct el2) != (struct el2 *)el1) {
|
||||
printf("type set failed on el1 with el2\n");
|
||||
return False;
|
||||
return false;
|
||||
}
|
||||
|
||||
talloc_free(root);
|
||||
|
||||
return True;
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
test steal
|
||||
*/
|
||||
static BOOL test_steal(void)
|
||||
static bool test_steal(void)
|
||||
{
|
||||
void *root, *p1, *p2;
|
||||
|
||||
@ -715,12 +695,12 @@ static BOOL test_steal(void)
|
||||
|
||||
if (talloc_steal(p1, NULL) != NULL) {
|
||||
printf("failed: stealing NULL should give NULL\n");
|
||||
return False;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (talloc_steal(p1, p1) != p1) {
|
||||
printf("failed: stealing to ourselves is a nop\n");
|
||||
return False;
|
||||
return false;
|
||||
}
|
||||
CHECK_BLOCKS(root, 3);
|
||||
CHECK_SIZE(root, 30);
|
||||
@ -747,13 +727,47 @@ static BOOL test_steal(void)
|
||||
CHECK_SIZE(NULL, 3);
|
||||
talloc_free(p1);
|
||||
|
||||
return True;
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
test move
|
||||
*/
|
||||
static bool test_move(void)
|
||||
{
|
||||
void *root;
|
||||
struct t_move {
|
||||
char *p;
|
||||
int *x;
|
||||
} *t1, *t2;
|
||||
printf("TESTING MOVE\n");
|
||||
|
||||
root = talloc_new(NULL);
|
||||
|
||||
t1 = talloc(root, struct t_move);
|
||||
t2 = talloc(root, struct t_move);
|
||||
t1->p = talloc_strdup(t1, "foo");
|
||||
t1->x = talloc(t1, int);
|
||||
*t1->x = 42;
|
||||
|
||||
t2->p = talloc_move(t2, &t1->p);
|
||||
t2->x = talloc_move(t2, &t1->x);
|
||||
if (t1->p != NULL || t1->x != NULL ||
|
||||
strcmp(t2->p, "foo") ||
|
||||
*t2->x != 42) {
|
||||
printf("talloc move failed\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
talloc_free(root);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
test talloc_realloc_fn
|
||||
*/
|
||||
static BOOL test_realloc_fn(void)
|
||||
static bool test_realloc_fn(void)
|
||||
{
|
||||
void *root, *p1;
|
||||
|
||||
@ -774,11 +788,11 @@ static BOOL test_realloc_fn(void)
|
||||
talloc_free(root);
|
||||
|
||||
|
||||
return True;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
static BOOL test_unref_reparent(void)
|
||||
static bool test_unref_reparent(void)
|
||||
{
|
||||
void *root, *p1, *p2, *c1;
|
||||
|
||||
@ -804,13 +818,13 @@ static BOOL test_unref_reparent(void)
|
||||
talloc_free(p2);
|
||||
talloc_free(root);
|
||||
|
||||
return True;
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
measure the speed of talloc versus malloc
|
||||
*/
|
||||
static BOOL test_speed(void)
|
||||
static bool test_speed(void)
|
||||
{
|
||||
void *ctx = talloc_new(NULL);
|
||||
unsigned count;
|
||||
@ -848,11 +862,11 @@ static BOOL test_speed(void)
|
||||
|
||||
printf("malloc: %.0f ops/sec\n", count/timeval_elapsed(&tv));
|
||||
|
||||
return True;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
static BOOL test_lifeless(void)
|
||||
static bool test_lifeless(void)
|
||||
{
|
||||
void *top = talloc_new(NULL);
|
||||
char *parent, *child;
|
||||
@ -872,7 +886,7 @@ static BOOL test_lifeless(void)
|
||||
talloc_free(child_owner);
|
||||
talloc_free(child);
|
||||
|
||||
return True;
|
||||
return true;
|
||||
}
|
||||
|
||||
static int loop_destructor_count;
|
||||
@ -884,7 +898,7 @@ static int test_loop_destructor(char *ptr)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static BOOL test_loop(void)
|
||||
static bool test_loop(void)
|
||||
{
|
||||
void *top = talloc_new(NULL);
|
||||
char *parent;
|
||||
@ -907,11 +921,11 @@ static BOOL test_loop(void)
|
||||
|
||||
if (loop_destructor_count != 1) {
|
||||
printf("FAILED TO FIRE LOOP DESTRUCTOR\n");
|
||||
return False;
|
||||
return false;
|
||||
}
|
||||
loop_destructor_count = 0;
|
||||
|
||||
return True;
|
||||
return true;
|
||||
}
|
||||
|
||||
static int fail_destructor_str(char *ptr)
|
||||
@ -919,7 +933,7 @@ static int fail_destructor_str(char *ptr)
|
||||
return -1;
|
||||
}
|
||||
|
||||
static BOOL test_free_parent_deny_child(void)
|
||||
static bool test_free_parent_deny_child(void)
|
||||
{
|
||||
void *top = talloc_new(NULL);
|
||||
char *level1;
|
||||
@ -939,12 +953,12 @@ static BOOL test_free_parent_deny_child(void)
|
||||
|
||||
talloc_free(top);
|
||||
|
||||
return True;
|
||||
return true;
|
||||
}
|
||||
|
||||
static BOOL test_talloc_ptrtype(void)
|
||||
static bool test_talloc_ptrtype(void)
|
||||
{
|
||||
BOOL ret = True;
|
||||
bool ret = true;
|
||||
void *top = talloc_new(NULL);
|
||||
struct struct1 {
|
||||
int foo;
|
||||
@ -963,13 +977,13 @@ static BOOL test_talloc_ptrtype(void)
|
||||
"(should be %lu)\n",
|
||||
__location__, (unsigned long)talloc_get_size(s1),
|
||||
(unsigned long)sizeof(struct struct1));
|
||||
ret = False;
|
||||
ret = false;
|
||||
}
|
||||
|
||||
if (strcmp(location1, talloc_get_name(s1)) != 0) {
|
||||
printf("%s: talloc_ptrtype() sets the wrong name '%s' (should be '%s')\n",
|
||||
__location__, talloc_get_name(s1), location1);
|
||||
ret = False;
|
||||
ret = false;
|
||||
}
|
||||
|
||||
s2 = talloc_array_ptrtype(top, s2, 10);location2 = __location__;
|
||||
@ -979,14 +993,14 @@ static BOOL test_talloc_ptrtype(void)
|
||||
"%lu (should be %lu)\n",
|
||||
__location__, (unsigned long)talloc_get_size(s2),
|
||||
(unsigned long)(sizeof(struct struct1)*10));
|
||||
ret = False;
|
||||
ret = false;
|
||||
}
|
||||
|
||||
if (strcmp(location2, talloc_get_name(s2)) != 0) {
|
||||
printf("%s: talloc_array_ptrtype() sets the wrong name '%s' (should be '%s')\n",
|
||||
__location__, talloc_get_name(s2),
|
||||
location2);
|
||||
ret = False;
|
||||
ret = false;
|
||||
}
|
||||
|
||||
s3 = talloc_array_ptrtype(top, s3, 10);location3 = __location__;
|
||||
@ -996,13 +1010,13 @@ static BOOL test_talloc_ptrtype(void)
|
||||
"%lu (should be %lu)\n",
|
||||
__location__, (unsigned long)talloc_get_size(s3),
|
||||
(unsigned long)(sizeof(struct struct1 *)*10));
|
||||
ret = False;
|
||||
ret = false;
|
||||
}
|
||||
|
||||
if (strcmp(location3, talloc_get_name(s3)) != 0) {
|
||||
printf("%s: talloc_array_ptrtype() sets the wrong name '%s' (should be '%s')\n",
|
||||
__location__, talloc_get_name(s3), location3);
|
||||
ret = False;
|
||||
ret = false;
|
||||
}
|
||||
|
||||
s4 = talloc_array_ptrtype(top, s4, 10);location4 = __location__;
|
||||
@ -1012,13 +1026,13 @@ static BOOL test_talloc_ptrtype(void)
|
||||
"%lu (should be %lu)\n",
|
||||
__location__, (unsigned long)talloc_get_size(s4),
|
||||
(unsigned long)(sizeof(struct struct1 **)*10));
|
||||
ret = False;
|
||||
ret = false;
|
||||
}
|
||||
|
||||
if (strcmp(location4, talloc_get_name(s4)) != 0) {
|
||||
printf("%s: talloc_array_ptrtype() sets the wrong name '%s' (should be '%s')\n",
|
||||
__location__, talloc_get_name(s4), location4);
|
||||
ret = False;
|
||||
ret = false;
|
||||
}
|
||||
|
||||
talloc_free(top);
|
||||
@ -1026,9 +1040,9 @@ static BOOL test_talloc_ptrtype(void)
|
||||
return ret;
|
||||
}
|
||||
|
||||
BOOL torture_local_talloc(struct torture_context *torture)
|
||||
bool torture_local_talloc(struct torture_context *torture)
|
||||
{
|
||||
BOOL ret = True;
|
||||
bool ret = true;
|
||||
|
||||
talloc_disable_null_tracking();
|
||||
talloc_enable_null_tracking();
|
||||
@ -1042,6 +1056,7 @@ BOOL torture_local_talloc(struct torture_context *torture)
|
||||
ret &= test_realloc();
|
||||
ret &= test_realloc_child();
|
||||
ret &= test_steal();
|
||||
ret &= test_move();
|
||||
ret &= test_unref_reparent();
|
||||
ret &= test_realloc_fn();
|
||||
ret &= test_type();
|
49
source3/lib/talloc/web/index.html
Normal file
49
source3/lib/talloc/web/index.html
Normal file
@ -0,0 +1,49 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
|
||||
<HTML>
|
||||
<HEAD>
|
||||
<TITLE>talloc</TITLE>
|
||||
</HEAD>
|
||||
<BODY BGCOLOR="#ffffff" TEXT="#000000" VLINK="#292555" LINK="#292555" ALINK="#cc0033">
|
||||
|
||||
<h1>talloc</h1>
|
||||
|
||||
talloc is a hierarchical pool based memory allocator with
|
||||
destructors. It is the core memory allocator used in Samba4, and has
|
||||
made a huge difference in many aspects of Samba4 development.<p>
|
||||
|
||||
To get started with talloc, I would recommend you read the <a
|
||||
href="http://samba.org/ftp/unpacked/samba4/source/lib/talloc/talloc_guide.txt">talloc guide</a>.
|
||||
|
||||
<h2>Discussion and bug reports</h2>
|
||||
|
||||
talloc does not currently have its own mailing list or bug tracking
|
||||
system. For now, please use the <a
|
||||
href="https://lists.samba.org/mailman/listinfo/samba-technical">samba-technical</a>
|
||||
mailing list, and the <a href="http://bugzilla.samba.org/">Samba
|
||||
bugzilla</a> bug tracking system.
|
||||
|
||||
<h2>Download</h2>
|
||||
|
||||
You can download the latest release either via rsync or anonymous
|
||||
svn. To fetch via svn use the following command:
|
||||
|
||||
<pre>
|
||||
svn co svn://svnanon.samba.org/samba/branches/SAMBA_4_0/source/lib/talloc talloc
|
||||
svn co svn://svnanon.samba.org/samba/branches/SAMBA_4_0/source/lib/replace libreplace
|
||||
</pre>
|
||||
|
||||
To fetch via rsync use this command:
|
||||
|
||||
<pre>
|
||||
rsync -Pavz samba.org::ftp/unpacked/samba4/source/lib/talloc .
|
||||
rsync -Pavz samba.org::ftp/unpacked/samba4/source/lib/libreplace .
|
||||
</pre>
|
||||
|
||||
<hr>
|
||||
<tiny>
|
||||
<a href="http://samba.org/~tridge/">Andrew Tridgell</a><br>
|
||||
talloc AT tridgell.net
|
||||
</tiny>
|
||||
|
||||
</BODY>
|
||||
</HTML>
|
Loading…
Reference in New Issue
Block a user