Blame talloc.h

Packit Service fa3ceb
#ifndef _TALLOC_H_
Packit Service fa3ceb
#define _TALLOC_H_
Packit Service fa3ceb
/*
Packit Service fa3ceb
   Unix SMB/CIFS implementation.
Packit Service fa3ceb
   Samba temporary memory allocation functions
Packit Service fa3ceb
Packit Service fa3ceb
   Copyright (C) Andrew Tridgell 2004-2005
Packit Service fa3ceb
   Copyright (C) Stefan Metzmacher 2006
Packit Service fa3ceb
Packit Service fa3ceb
     ** NOTE! The following LGPL license applies to the talloc
Packit Service fa3ceb
     ** library. This does NOT imply that all of Samba is released
Packit Service fa3ceb
     ** under the LGPL
Packit Service fa3ceb
Packit Service fa3ceb
   This library is free software; you can redistribute it and/or
Packit Service fa3ceb
   modify it under the terms of the GNU Lesser General Public
Packit Service fa3ceb
   License as published by the Free Software Foundation; either
Packit Service fa3ceb
   version 3 of the License, or (at your option) any later version.
Packit Service fa3ceb
Packit Service fa3ceb
   This library is distributed in the hope that it will be useful,
Packit Service fa3ceb
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service fa3ceb
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service fa3ceb
   Lesser General Public License for more details.
Packit Service fa3ceb
Packit Service fa3ceb
   You should have received a copy of the GNU Lesser General Public
Packit Service fa3ceb
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit Service fa3ceb
*/
Packit Service fa3ceb
Packit Service fa3ceb
#include <stdlib.h>
Packit Service fa3ceb
#include <stdio.h>
Packit Service fa3ceb
#include <stdarg.h>
Packit Service fa3ceb
Packit Service fa3ceb
#ifdef __cplusplus
Packit Service fa3ceb
extern "C" {
Packit Service fa3ceb
#endif
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @defgroup talloc The talloc API
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * talloc is a hierarchical, reference counted memory pool system with
Packit Service fa3ceb
 * destructors. It is the core memory allocator used in Samba.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @{
Packit Service fa3ceb
 */
Packit Service fa3ceb
Packit Service fa3ceb
#define TALLOC_VERSION_MAJOR 2
Packit Service fa3ceb
#define TALLOC_VERSION_MINOR 3
Packit Service fa3ceb
Packit Service fa3ceb
int talloc_version_major(void);
Packit Service fa3ceb
int talloc_version_minor(void);
Packit Service fa3ceb
/* This is mostly useful only for testing */
Packit Service fa3ceb
int talloc_test_get_magic(void);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Define a talloc parent type
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * As talloc is a hierarchial memory allocator, every talloc chunk is a
Packit Service fa3ceb
 * potential parent to other talloc chunks. So defining a separate type for a
Packit Service fa3ceb
 * talloc chunk is not strictly necessary. TALLOC_CTX is defined nevertheless,
Packit Service fa3ceb
 * as it provides an indicator for function arguments. You will frequently
Packit Service fa3ceb
 * write code like
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      struct foo *foo_create(TALLOC_CTX *mem_ctx)
Packit Service fa3ceb
 *      {
Packit Service fa3ceb
 *              struct foo *result;
Packit Service fa3ceb
 *              result = talloc(mem_ctx, struct foo);
Packit Service fa3ceb
 *              if (result == NULL) return NULL;
Packit Service fa3ceb
 *                      ... initialize foo ...
Packit Service fa3ceb
 *              return result;
Packit Service fa3ceb
 *      }
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * In this type of allocating functions it is handy to have a general
Packit Service fa3ceb
 * TALLOC_CTX type to indicate which parent to put allocated structures on.
Packit Service fa3ceb
 */
Packit Service fa3ceb
typedef void TALLOC_CTX;
Packit Service fa3ceb
Packit Service fa3ceb
/*
Packit Service fa3ceb
  this uses a little trick to allow __LINE__ to be stringified
Packit Service fa3ceb
*/
Packit Service fa3ceb
#ifndef __location__
Packit Service fa3ceb
#define __TALLOC_STRING_LINE1__(s)    #s
Packit Service fa3ceb
#define __TALLOC_STRING_LINE2__(s)   __TALLOC_STRING_LINE1__(s)
Packit Service fa3ceb
#define __TALLOC_STRING_LINE3__  __TALLOC_STRING_LINE2__(__LINE__)
Packit Service fa3ceb
#define __location__ __FILE__ ":" __TALLOC_STRING_LINE3__
Packit Service fa3ceb
#endif
Packit Service fa3ceb
Packit Service fa3ceb
#ifndef TALLOC_DEPRECATED
Packit Service fa3ceb
#define TALLOC_DEPRECATED 0
Packit Service fa3ceb
#endif
Packit Service fa3ceb
Packit Service fa3ceb
#ifndef PRINTF_ATTRIBUTE
Packit Service fa3ceb
#if (__GNUC__ >= 3)
Packit Service fa3ceb
/** Use gcc attribute to check printf fns.  a1 is the 1-based index of
Packit Service fa3ceb
 * the parameter containing the format, and a2 the index of the first
Packit Service fa3ceb
 * argument. Note that some gcc 2.x versions don't handle this
Packit Service fa3ceb
 * properly **/
Packit Service fa3ceb
#define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
Packit Service fa3ceb
#else
Packit Service fa3ceb
#define PRINTF_ATTRIBUTE(a1, a2)
Packit Service fa3ceb
#endif
Packit Service fa3ceb
#endif
Packit Service fa3ceb
Packit Service fa3ceb
#ifndef _DEPRECATED_
Packit Service fa3ceb
#ifdef HAVE___ATTRIBUTE__
Packit Service fa3ceb
#define _DEPRECATED_ __attribute__ ((deprecated))
Packit Service fa3ceb
#else
Packit Service fa3ceb
#define _DEPRECATED_
Packit Service fa3ceb
#endif
Packit Service fa3ceb
#endif
Packit Service fa3ceb
#ifdef DOXYGEN
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Create a new talloc context.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The talloc() macro is the core of the talloc library. It takes a memory
Packit Service fa3ceb
 * context and a type, and returns a pointer to a new area of memory of the
Packit Service fa3ceb
 * given type.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The returned pointer is itself a talloc context, so you can use it as the
Packit Service fa3ceb
 * context argument to more calls to talloc if you wish.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The returned pointer is a "child" of the supplied context. This means that if
Packit Service fa3ceb
 * you talloc_free() the context then the new child disappears as well.
Packit Service fa3ceb
 * Alternatively you can free just the child.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ctx      A talloc context to create a new reference on or NULL to
Packit Service fa3ceb
 *                      create a new top level context.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  type     The type of memory to allocate.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              A type casted talloc context or NULL on error.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      unsigned int *a, *b;
Packit Service fa3ceb
 *
Packit Service fa3ceb
 *      a = talloc(NULL, unsigned int);
Packit Service fa3ceb
 *      b = talloc(a, unsigned int);
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @see talloc_zero
Packit Service fa3ceb
 * @see talloc_array
Packit Service fa3ceb
 * @see talloc_steal
Packit Service fa3ceb
 * @see talloc_free
Packit Service fa3ceb
 */
Packit Service fa3ceb
void *talloc(const void *ctx, #type);
Packit Service fa3ceb
#else
Packit Service fa3ceb
#define talloc(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type)
Packit Service fa3ceb
void *_talloc(const void *context, size_t size);
Packit Service fa3ceb
#endif
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Create a new top level talloc context.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This function creates a zero length named talloc context as a top level
Packit Service fa3ceb
 * context. It is equivalent to:
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      talloc_named(NULL, 0, fmt, ...);
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 * @param[in]  fmt      Format string for the name.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ...      Additional printf-style arguments.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The allocated memory chunk, NULL on error.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @see talloc_named()
Packit Service fa3ceb
 */
Packit Service fa3ceb
void *talloc_init(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
Packit Service fa3ceb
Packit Service fa3ceb
#ifdef DOXYGEN
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Free a chunk of talloc memory.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The talloc_free() function frees a piece of talloc memory, and all its
Packit Service fa3ceb
 * children. You can call talloc_free() on any pointer returned by
Packit Service fa3ceb
 * talloc().
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The return value of talloc_free() indicates success or failure, with 0
Packit Service fa3ceb
 * returned for success and -1 for failure. A possible failure condition
Packit Service fa3ceb
 * is if the pointer had a destructor attached to it and the destructor
Packit Service fa3ceb
 * returned -1. See talloc_set_destructor() for details on
Packit Service fa3ceb
 * destructors. Likewise, if "ptr" is NULL, then the function will make
Packit Service fa3ceb
 * no modifications and return -1.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * From version 2.0 and onwards, as a special case, talloc_free() is
Packit Service fa3ceb
 * refused on pointers that have more than one parent associated, as talloc
Packit Service fa3ceb
 * would have no way of knowing which parent should be removed. This is
Packit Service fa3ceb
 * different from older versions in the sense that always the reference to
Packit Service fa3ceb
 * the most recently established parent has been destroyed. Hence to free a
Packit Service fa3ceb
 * pointer that has more than one parent please use talloc_unlink().
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * To help you find problems in your code caused by this behaviour, if
Packit Service fa3ceb
 * you do try and free a pointer with more than one parent then the
Packit Service fa3ceb
 * talloc logging function will be called to give output like this:
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *   ERROR: talloc_free with references at some_dir/source/foo.c:123
Packit Service fa3ceb
 *     reference at some_dir/source/other.c:325
Packit Service fa3ceb
 *     reference at some_dir/source/third.c:121
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * Please see the documentation for talloc_set_log_fn() and
Packit Service fa3ceb
 * talloc_set_log_stderr() for more information on talloc logging
Packit Service fa3ceb
 * functions.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * If TALLOC_FREE_FILL environment variable is set,
Packit Service fa3ceb
 * the memory occupied by the context is filled with the value of this variable.
Packit Service fa3ceb
 * The value should be a numeric representation of the character you want to
Packit Service fa3ceb
 * use.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * talloc_free() operates recursively on its children.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ptr      The chunk to be freed.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              Returns 0 on success and -1 on error. A possible
Packit Service fa3ceb
 *                      failure condition is if the pointer had a destructor
Packit Service fa3ceb
 *                      attached to it and the destructor returned -1. Likewise,
Packit Service fa3ceb
 *                      if "ptr" is NULL, then the function will make no
Packit Service fa3ceb
 *                      modifications and returns -1.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * Example:
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      unsigned int *a, *b;
Packit Service fa3ceb
 *      a = talloc(NULL, unsigned int);
Packit Service fa3ceb
 *      b = talloc(a, unsigned int);
Packit Service fa3ceb
 *
Packit Service fa3ceb
 *      talloc_free(a); // Frees a and b
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @see talloc_set_destructor()
Packit Service fa3ceb
 * @see talloc_unlink()
Packit Service fa3ceb
 */
Packit Service fa3ceb
int talloc_free(void *ptr);
Packit Service fa3ceb
#else
Packit Service fa3ceb
#define talloc_free(ctx) _talloc_free(ctx, __location__)
Packit Service fa3ceb
int _talloc_free(void *ptr, const char *location);
Packit Service fa3ceb
#endif
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Free a talloc chunk's children.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The function walks along the list of all children of a talloc context and
Packit Service fa3ceb
 * talloc_free()s only the children, not the context itself.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * A NULL argument is handled as no-op.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ptr      The chunk that you want to free the children of
Packit Service fa3ceb
 *                      (NULL is allowed too)
Packit Service fa3ceb
 */
Packit Service fa3ceb
void talloc_free_children(void *ptr);
Packit Service fa3ceb
Packit Service fa3ceb
#ifdef DOXYGEN
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Assign a destructor function to be called when a chunk is freed.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The function talloc_set_destructor() sets the "destructor" for the pointer
Packit Service fa3ceb
 * "ptr". A destructor is a function that is called when the memory used by a
Packit Service fa3ceb
 * pointer is about to be released. The destructor receives the pointer as an
Packit Service fa3ceb
 * argument, and should return 0 for success and -1 for failure.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The destructor can do anything it wants to, including freeing other pieces
Packit Service fa3ceb
 * of memory. A common use for destructors is to clean up operating system
Packit Service fa3ceb
 * resources (such as open file descriptors) contained in the structure the
Packit Service fa3ceb
 * destructor is placed on.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * You can only place one destructor on a pointer. If you need more than one
Packit Service fa3ceb
 * destructor then you can create a zero-length child of the pointer and place
Packit Service fa3ceb
 * an additional destructor on that.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * To remove a destructor call talloc_set_destructor() with NULL for the
Packit Service fa3ceb
 * destructor.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * If your destructor attempts to talloc_free() the pointer that it is the
Packit Service fa3ceb
 * destructor for then talloc_free() will return -1 and the free will be
Packit Service fa3ceb
 * ignored. This would be a pointless operation anyway, as the destructor is
Packit Service fa3ceb
 * only called when the memory is just about to go away.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ptr      The talloc chunk to add a destructor to.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  destructor  The destructor function to be called. NULL to remove
Packit Service fa3ceb
 *                         it.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * Example:
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      static int destroy_fd(int *fd) {
Packit Service fa3ceb
 *              close(*fd);
Packit Service fa3ceb
 *              return 0;
Packit Service fa3ceb
 *      }
Packit Service fa3ceb
 *
Packit Service fa3ceb
 *      int *open_file(const char *filename) {
Packit Service fa3ceb
 *              int *fd = talloc(NULL, int);
Packit Service fa3ceb
 *              *fd = open(filename, O_RDONLY);
Packit Service fa3ceb
 *              if (*fd < 0) {
Packit Service fa3ceb
 *                      talloc_free(fd);
Packit Service fa3ceb
 *                      return NULL;
Packit Service fa3ceb
 *              }
Packit Service fa3ceb
 *              // Whenever they free this, we close the file.
Packit Service fa3ceb
 *              talloc_set_destructor(fd, destroy_fd);
Packit Service fa3ceb
 *              return fd;
Packit Service fa3ceb
 *      }
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @see talloc()
Packit Service fa3ceb
 * @see talloc_free()
Packit Service fa3ceb
 */
Packit Service fa3ceb
void talloc_set_destructor(const void *ptr, int (*destructor)(void *));
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Change a talloc chunk's parent.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The talloc_steal() function changes the parent context of a talloc
Packit Service fa3ceb
 * pointer. It is typically used when the context that the pointer is
Packit Service fa3ceb
 * currently a child of is going to be freed and you wish to keep the
Packit Service fa3ceb
 * memory for a longer time.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * To make the changed hierarchy less error-prone, you might consider to use
Packit Service fa3ceb
 * talloc_move().
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * If you try and call talloc_steal() on a pointer that has more than one
Packit Service fa3ceb
 * parent then the result is ambiguous. Talloc will choose to remove the
Packit Service fa3ceb
 * parent that is currently indicated by talloc_parent() and replace it with
Packit Service fa3ceb
 * the chosen parent. You will also get a message like this via the talloc
Packit Service fa3ceb
 * logging functions:
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *   WARNING: talloc_steal with references at some_dir/source/foo.c:123
Packit Service fa3ceb
 *     reference at some_dir/source/other.c:325
Packit Service fa3ceb
 *     reference at some_dir/source/third.c:121
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * To unambiguously change the parent of a pointer please see the function
Packit Service fa3ceb
 * talloc_reparent(). See the talloc_set_log_fn() documentation for more
Packit Service fa3ceb
 * information on talloc logging.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  new_ctx  The new parent context.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ptr      The talloc chunk to move.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              Returns the pointer that you pass it. It does not have
Packit Service fa3ceb
 *                      any failure modes.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @note It is possible to produce loops in the parent/child relationship
Packit Service fa3ceb
 * if you are not careful with talloc_steal(). No guarantees are provided
Packit Service fa3ceb
 * as to your sanity or the safety of your data if you do this.
Packit Service fa3ceb
 */
Packit Service fa3ceb
void *talloc_steal(const void *new_ctx, const void *ptr);
Packit Service fa3ceb
#else /* DOXYGEN */
Packit Service fa3ceb
/* try to make talloc_set_destructor() and talloc_steal() type safe,
Packit Service fa3ceb
   if we have a recent gcc */
Packit Service fa3ceb
#if (__GNUC__ >= 3)
Packit Service fa3ceb
#define _TALLOC_TYPEOF(ptr) __typeof__(ptr)
Packit Service fa3ceb
#define talloc_set_destructor(ptr, function)				      \
Packit Service fa3ceb
	do {								      \
Packit Service fa3ceb
		int (*_talloc_destructor_fn)(_TALLOC_TYPEOF(ptr)) = (function);	      \
Packit Service fa3ceb
		_talloc_set_destructor((ptr), (int (*)(void *))_talloc_destructor_fn); \
Packit Service fa3ceb
	} while(0)
Packit Service fa3ceb
/* this extremely strange macro is to avoid some braindamaged warning
Packit Service fa3ceb
   stupidity in gcc 4.1.x */
Packit Service fa3ceb
#define talloc_steal(ctx, ptr) ({ _TALLOC_TYPEOF(ptr) __talloc_steal_ret = (_TALLOC_TYPEOF(ptr))_talloc_steal_loc((ctx),(ptr), __location__); __talloc_steal_ret; })
Packit Service fa3ceb
#else /* __GNUC__ >= 3 */
Packit Service fa3ceb
#define talloc_set_destructor(ptr, function) \
Packit Service fa3ceb
	_talloc_set_destructor((ptr), (int (*)(void *))(function))
Packit Service fa3ceb
#define _TALLOC_TYPEOF(ptr) void *
Packit Service fa3ceb
#define talloc_steal(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_steal_loc((ctx),(ptr), __location__)
Packit Service fa3ceb
#endif /* __GNUC__ >= 3 */
Packit Service fa3ceb
void _talloc_set_destructor(const void *ptr, int (*_destructor)(void *));
Packit Service fa3ceb
void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location);
Packit Service fa3ceb
#endif /* DOXYGEN */
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Assign a name to a talloc chunk.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * Each talloc pointer has a "name". The name is used principally for
Packit Service fa3ceb
 * debugging purposes, although it is also possible to set and get the name on
Packit Service fa3ceb
 * a pointer in as a way of "marking" pointers in your code.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The main use for names on pointer is for "talloc reports". See
Packit Service fa3ceb
 * talloc_report() and talloc_report_full() for details. Also see
Packit Service fa3ceb
 * talloc_enable_leak_report() and talloc_enable_leak_report_full().
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The talloc_set_name() function allocates memory as a child of the
Packit Service fa3ceb
 * pointer. It is logically equivalent to:
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      talloc_set_name_const(ptr, talloc_asprintf(ptr, fmt, ...));
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ptr      The talloc chunk to assign a name to.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  fmt      Format string for the name.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ...      Add printf-style additional arguments.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The assigned name, NULL on error.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @note Multiple calls to talloc_set_name() will allocate more memory without
Packit Service fa3ceb
 * releasing the name. All of the memory is released when the ptr is freed
Packit Service fa3ceb
 * using talloc_free().
Packit Service fa3ceb
 */
Packit Service fa3ceb
const char *talloc_set_name(const void *ptr, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
Packit Service fa3ceb
Packit Service fa3ceb
#ifdef DOXYGEN
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Change a talloc chunk's parent.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This function has the same effect as talloc_steal(), and additionally sets
Packit Service fa3ceb
 * the source pointer to NULL. You would use it like this:
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      struct foo *X = talloc(tmp_ctx, struct foo);
Packit Service fa3ceb
 *      struct foo *Y;
Packit Service fa3ceb
 *      Y = talloc_move(new_ctx, &X);
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  new_ctx  The new parent context.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  pptr     Pointer to a pointer to the talloc chunk to move.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The pointer to the talloc chunk that moved.
Packit Service fa3ceb
 *                      It does not have any failure modes.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 */
Packit Service fa3ceb
void *talloc_move(const void *new_ctx, void **pptr);
Packit Service fa3ceb
#else
Packit Service fa3ceb
#define talloc_move(ctx, pptr) (_TALLOC_TYPEOF(*(pptr)))_talloc_move((ctx),(void *)(pptr))
Packit Service fa3ceb
void *_talloc_move(const void *new_ctx, const void *pptr);
Packit Service fa3ceb
#endif
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Assign a name to a talloc chunk.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The function is just like talloc_set_name(), but it takes a string constant,
Packit Service fa3ceb
 * and is much faster. It is extensively used by the "auto naming" macros, such
Packit Service fa3ceb
 * as talloc_p().
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This function does not allocate any memory. It just copies the supplied
Packit Service fa3ceb
 * pointer into the internal representation of the talloc ptr. This means you
Packit Service fa3ceb
 * must not pass a name pointer to memory that will disappear before the ptr
Packit Service fa3ceb
 * is freed with talloc_free().
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ptr      The talloc chunk to assign a name to.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  name     Format string for the name.
Packit Service fa3ceb
 */
Packit Service fa3ceb
void talloc_set_name_const(const void *ptr, const char *name);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Create a named talloc chunk.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The talloc_named() function creates a named talloc pointer. It is
Packit Service fa3ceb
 * equivalent to:
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      ptr = talloc_size(context, size);
Packit Service fa3ceb
 *      talloc_set_name(ptr, fmt, ....);
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  context  The talloc context to hang the result off.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  size     Number of char's that you want to allocate.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  fmt      Format string for the name.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ...      Additional printf-style arguments.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The allocated memory chunk, NULL on error.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @see talloc_set_name()
Packit Service fa3ceb
 */
Packit Service fa3ceb
void *talloc_named(const void *context, size_t size,
Packit Service fa3ceb
		   const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Basic routine to allocate a chunk of memory.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This is equivalent to:
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      ptr = talloc_size(context, size);
Packit Service fa3ceb
 *      talloc_set_name_const(ptr, name);
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  context  The parent context.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  size     The number of char's that we want to allocate.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  name     The name the talloc block has.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return             The allocated memory chunk, NULL on error.
Packit Service fa3ceb
 */
Packit Service fa3ceb
void *talloc_named_const(const void *context, size_t size, const char *name);
Packit Service fa3ceb
Packit Service fa3ceb
#ifdef DOXYGEN
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Untyped allocation.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The function should be used when you don't have a convenient type to pass to
Packit Service fa3ceb
 * talloc(). Unlike talloc(), it is not type safe (as it returns a void *), so
Packit Service fa3ceb
 * you are on your own for type checking.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * Best to use talloc() or talloc_array() instead.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ctx     The talloc context to hang the result off.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  size    Number of char's that you want to allocate.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return             The allocated memory chunk, NULL on error.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * Example:
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      void *mem = talloc_size(NULL, 100);
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 */
Packit Service fa3ceb
void *talloc_size(const void *ctx, size_t size);
Packit Service fa3ceb
#else
Packit Service fa3ceb
#define talloc_size(ctx, size) talloc_named_const(ctx, size, __location__)
Packit Service fa3ceb
#endif
Packit Service fa3ceb
Packit Service fa3ceb
#ifdef DOXYGEN
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Allocate into a typed pointer.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The talloc_ptrtype() macro should be used when you have a pointer and want
Packit Service fa3ceb
 * to allocate memory to point at with this pointer. When compiling with
Packit Service fa3ceb
 * gcc >= 3 it is typesafe. Note this is a wrapper of talloc_size() and
Packit Service fa3ceb
 * talloc_get_name() will return the current location in the source file and
Packit Service fa3ceb
 * not the type.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ctx      The talloc context to hang the result off.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  type     The pointer you want to assign the result to.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The properly casted allocated memory chunk, NULL on
Packit Service fa3ceb
 *                      error.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * Example:
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *       unsigned int *a = talloc_ptrtype(NULL, a);
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 */
Packit Service fa3ceb
void *talloc_ptrtype(const void *ctx, #type);
Packit Service fa3ceb
#else
Packit Service fa3ceb
#define talloc_ptrtype(ctx, ptr) (_TALLOC_TYPEOF(ptr))talloc_size(ctx, sizeof(*(ptr)))
Packit Service fa3ceb
#endif
Packit Service fa3ceb
Packit Service fa3ceb
#ifdef DOXYGEN
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Allocate a new 0-sized talloc chunk.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This is a utility macro that creates a new memory context hanging off an
Packit Service fa3ceb
 * existing context, automatically naming it "talloc_new: __location__" where
Packit Service fa3ceb
 * __location__ is the source line it is called from. It is particularly
Packit Service fa3ceb
 * useful for creating a new temporary working context.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ctx      The talloc parent context.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              A new talloc chunk, NULL on error.
Packit Service fa3ceb
 */
Packit Service fa3ceb
void *talloc_new(const void *ctx);
Packit Service fa3ceb
#else
Packit Service fa3ceb
#define talloc_new(ctx) talloc_named_const(ctx, 0, "talloc_new: " __location__)
Packit Service fa3ceb
#endif
Packit Service fa3ceb
Packit Service fa3ceb
#ifdef DOXYGEN
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Allocate a 0-initizialized structure.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The macro is equivalent to:
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      ptr = talloc(ctx, type);
Packit Service fa3ceb
 *      if (ptr) memset(ptr, 0, sizeof(type));
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ctx      The talloc context to hang the result off.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  type     The type that we want to allocate.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              Pointer to a piece of memory, properly cast to 'type *',
Packit Service fa3ceb
 *                      NULL on error.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * Example:
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      unsigned int *a, *b;
Packit Service fa3ceb
 *      a = talloc_zero(NULL, unsigned int);
Packit Service fa3ceb
 *      b = talloc_zero(a, unsigned int);
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @see talloc()
Packit Service fa3ceb
 * @see talloc_zero_size()
Packit Service fa3ceb
 * @see talloc_zero_array()
Packit Service fa3ceb
 */
Packit Service fa3ceb
void *talloc_zero(const void *ctx, #type);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Allocate untyped, 0-initialized memory.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ctx      The talloc context to hang the result off.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  size     Number of char's that you want to allocate.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The allocated memory chunk.
Packit Service fa3ceb
 */
Packit Service fa3ceb
void *talloc_zero_size(const void *ctx, size_t size);
Packit Service fa3ceb
#else
Packit Service fa3ceb
#define talloc_zero(ctx, type) (type *)_talloc_zero(ctx, sizeof(type), #type)
Packit Service fa3ceb
#define talloc_zero_size(ctx, size) _talloc_zero(ctx, size, __location__)
Packit Service fa3ceb
void *_talloc_zero(const void *ctx, size_t size, const char *name);
Packit Service fa3ceb
#endif
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Return the name of a talloc chunk.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ptr      The talloc chunk.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The current name for the given talloc pointer.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @see talloc_set_name()
Packit Service fa3ceb
 */
Packit Service fa3ceb
const char *talloc_get_name(const void *ptr);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Verify that a talloc chunk carries a specified name.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This function checks if a pointer has the specified name. If it does
Packit Service fa3ceb
 * then the pointer is returned.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ptr       The talloc chunk to check.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  name      The name to check against.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return               The pointer if the name matches, NULL if it doesn't.
Packit Service fa3ceb
 */
Packit Service fa3ceb
void *talloc_check_name(const void *ptr, const char *name);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Get the parent chunk of a pointer.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ptr      The talloc pointer to inspect.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The talloc parent of ptr, NULL on error.
Packit Service fa3ceb
 */
Packit Service fa3ceb
void *talloc_parent(const void *ptr);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Get a talloc chunk's parent name.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ptr      The talloc pointer to inspect.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The name of ptr's parent chunk.
Packit Service fa3ceb
 */
Packit Service fa3ceb
const char *talloc_parent_name(const void *ptr);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Get the total size of a talloc chunk including its children.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The function returns the total size in bytes used by this pointer and all
Packit Service fa3ceb
 * child pointers. Mostly useful for debugging.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * Passing NULL is allowed, but it will only give a meaningful result if
Packit Service fa3ceb
 * talloc_enable_leak_report() or talloc_enable_leak_report_full() has
Packit Service fa3ceb
 * been called.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ptr      The talloc chunk.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The total size.
Packit Service fa3ceb
 */
Packit Service fa3ceb
size_t talloc_total_size(const void *ptr);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Get the number of talloc chunks hanging off a chunk.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The talloc_total_blocks() function returns the total memory block
Packit Service fa3ceb
 * count used by this pointer and all child pointers. Mostly useful for
Packit Service fa3ceb
 * debugging.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * Passing NULL is allowed, but it will only give a meaningful result if
Packit Service fa3ceb
 * talloc_enable_leak_report() or talloc_enable_leak_report_full() has
Packit Service fa3ceb
 * been called.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ptr      The talloc chunk.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The total size.
Packit Service fa3ceb
 */
Packit Service fa3ceb
size_t talloc_total_blocks(const void *ptr);
Packit Service fa3ceb
Packit Service fa3ceb
#ifdef DOXYGEN
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Duplicate a memory area into a talloc chunk.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The function is equivalent to:
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      ptr = talloc_size(ctx, size);
Packit Service fa3ceb
 *      if (ptr) memcpy(ptr, p, size);
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  t        The talloc context to hang the result off.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  p        The memory chunk you want to duplicate.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  size     Number of char's that you want copy.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The allocated memory chunk.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @see talloc_size()
Packit Service fa3ceb
 */
Packit Service fa3ceb
void *talloc_memdup(const void *t, const void *p, size_t size);
Packit Service fa3ceb
#else
Packit Service fa3ceb
#define talloc_memdup(t, p, size) _talloc_memdup(t, p, size, __location__)
Packit Service fa3ceb
void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name);
Packit Service fa3ceb
#endif
Packit Service fa3ceb
Packit Service fa3ceb
#ifdef DOXYGEN
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Assign a type to a talloc chunk.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This macro allows you to force the name of a pointer to be of a particular
Packit Service fa3ceb
 * type. This can be used in conjunction with talloc_get_type() to do type
Packit Service fa3ceb
 * checking on void* pointers.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * It is equivalent to this:
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      talloc_set_name_const(ptr, #type)
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ptr      The talloc chunk to assign the type to.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  type     The type to assign.
Packit Service fa3ceb
 */
Packit Service fa3ceb
void talloc_set_type(const char *ptr, #type);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Get a typed pointer out of a talloc pointer.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This macro allows you to do type checking on talloc pointers. It is
Packit Service fa3ceb
 * particularly useful for void* private pointers. It is equivalent to
Packit Service fa3ceb
 * this:
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      (type *)talloc_check_name(ptr, #type)
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ptr      The talloc pointer to check.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  type     The type to check against.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The properly casted pointer given by ptr, NULL on error.
Packit Service fa3ceb
 */
Packit Service fa3ceb
type *talloc_get_type(const void *ptr, #type);
Packit Service fa3ceb
#else
Packit Service fa3ceb
#define talloc_set_type(ptr, type) talloc_set_name_const(ptr, #type)
Packit Service fa3ceb
#define talloc_get_type(ptr, type) (type *)talloc_check_name(ptr, #type)
Packit Service fa3ceb
#endif
Packit Service fa3ceb
Packit Service fa3ceb
#ifdef DOXYGEN
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Safely turn a void pointer into a typed pointer.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This macro is used together with talloc(mem_ctx, struct foo). If you had to
Packit Service fa3ceb
 * assign the talloc chunk pointer to some void pointer variable,
Packit Service fa3ceb
 * talloc_get_type_abort() is the recommended way to get the convert the void
Packit Service fa3ceb
 * pointer back to a typed pointer.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ptr      The void pointer to convert.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  type     The type that this chunk contains
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The same value as ptr, type-checked and properly cast.
Packit Service fa3ceb
 */
Packit Service fa3ceb
void *talloc_get_type_abort(const void *ptr, #type);
Packit Service fa3ceb
#else
Packit Service fa3ceb
#ifdef TALLOC_GET_TYPE_ABORT_NOOP
Packit Service fa3ceb
#define talloc_get_type_abort(ptr, type) (type *)(ptr)
Packit Service fa3ceb
#else
Packit Service fa3ceb
#define talloc_get_type_abort(ptr, type) (type *)_talloc_get_type_abort(ptr, #type, __location__)
Packit Service fa3ceb
#endif
Packit Service fa3ceb
void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location);
Packit Service fa3ceb
#endif
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Find a parent context by name.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * Find a parent memory context of the current context that has the given
Packit Service fa3ceb
 * name. This can be very useful in complex programs where it may be
Packit Service fa3ceb
 * difficult to pass all information down to the level you need, but you
Packit Service fa3ceb
 * know the structure you want is a parent of another context.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ctx      The talloc chunk to start from.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  name     The name of the parent we look for.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The memory context we are looking for, NULL if not
Packit Service fa3ceb
 *                      found.
Packit Service fa3ceb
 */
Packit Service fa3ceb
void *talloc_find_parent_byname(const void *ctx, const char *name);
Packit Service fa3ceb
Packit Service fa3ceb
#ifdef DOXYGEN
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Find a parent context by type.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * Find a parent memory context of the current context that has the given
Packit Service fa3ceb
 * name. This can be very useful in complex programs where it may be
Packit Service fa3ceb
 * difficult to pass all information down to the level you need, but you
Packit Service fa3ceb
 * know the structure you want is a parent of another context.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * Like talloc_find_parent_byname() but takes a type, making it typesafe.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ptr      The talloc chunk to start from.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  type     The type of the parent to look for.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The memory context we are looking for, NULL if not
Packit Service fa3ceb
 *                      found.
Packit Service fa3ceb
 */
Packit Service fa3ceb
void *talloc_find_parent_bytype(const void *ptr, #type);
Packit Service fa3ceb
#else
Packit Service fa3ceb
#define talloc_find_parent_bytype(ptr, type) (type *)talloc_find_parent_byname(ptr, #type)
Packit Service fa3ceb
#endif
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Allocate a talloc pool.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * A talloc pool is a pure optimization for specific situations. In the
Packit Service fa3ceb
 * release process for Samba 3.2 we found out that we had become considerably
Packit Service fa3ceb
 * slower than Samba 3.0 was. Profiling showed that malloc(3) was a large CPU
Packit Service fa3ceb
 * consumer in benchmarks. For Samba 3.2 we have internally converted many
Packit Service fa3ceb
 * static buffers to dynamically allocated ones, so malloc(3) being beaten
Packit Service fa3ceb
 * more was no surprise. But it made us slower.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * talloc_pool() is an optimization to call malloc(3) a lot less for the use
Packit Service fa3ceb
 * pattern Samba has: The SMB protocol is mainly a request/response protocol
Packit Service fa3ceb
 * where we have to allocate a certain amount of memory per request and free
Packit Service fa3ceb
 * that after the SMB reply is sent to the client.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * talloc_pool() creates a talloc chunk that you can use as a talloc parent
Packit Service fa3ceb
 * exactly as you would use any other ::TALLOC_CTX. The difference is that
Packit Service fa3ceb
 * when you talloc a child of this pool, no malloc(3) is done. Instead, talloc
Packit Service fa3ceb
 * just increments a pointer inside the talloc_pool. This also works
Packit Service fa3ceb
 * recursively. If you use the child of the talloc pool as a parent for
Packit Service fa3ceb
 * grand-children, their memory is also taken from the talloc pool.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * If there is not enough memory in the pool to allocate the new child,
Packit Service fa3ceb
 * it will create a new talloc chunk as if the parent was a normal talloc
Packit Service fa3ceb
 * context.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * If you talloc_free() children of a talloc pool, the memory is not given
Packit Service fa3ceb
 * back to the system. Instead, free(3) is only called if the talloc_pool()
Packit Service fa3ceb
 * itself is released with talloc_free().
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The downside of a talloc pool is that if you talloc_move() a child of a
Packit Service fa3ceb
 * talloc pool to a talloc parent outside the pool, the whole pool memory is
Packit Service fa3ceb
 * not free(3)'ed until that moved chunk is also talloc_free()ed.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  context  The talloc context to hang the result off.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  size     Size of the talloc pool.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The allocated talloc pool, NULL on error.
Packit Service fa3ceb
 */
Packit Service fa3ceb
void *talloc_pool(const void *context, size_t size);
Packit Service fa3ceb
Packit Service fa3ceb
#ifdef DOXYGEN
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Allocate a talloc object as/with an additional pool.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This is like talloc_pool(), but's it's more flexible
Packit Service fa3ceb
 * and allows an object to be a pool for its children.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in] ctx                   The talloc context to hang the result off.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in] type                  The type that we want to allocate.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in] num_subobjects        The expected number of subobjects, which will
Packit Service fa3ceb
 *                                  be allocated within the pool. This allocates
Packit Service fa3ceb
 *                                  space for talloc_chunk headers.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in] total_subobjects_size The size that all subobjects can use in total.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The allocated talloc object, NULL on error.
Packit Service fa3ceb
 */
Packit Service fa3ceb
void *talloc_pooled_object(const void *ctx, #type,
Packit Service fa3ceb
			   unsigned num_subobjects,
Packit Service fa3ceb
			   size_t total_subobjects_size);
Packit Service fa3ceb
#else
Packit Service fa3ceb
#define talloc_pooled_object(_ctx, _type, \
Packit Service fa3ceb
			     _num_subobjects, \
Packit Service fa3ceb
			     _total_subobjects_size) \
Packit Service fa3ceb
	(_type *)_talloc_pooled_object((_ctx), sizeof(_type), #_type, \
Packit Service fa3ceb
					(_num_subobjects), \
Packit Service fa3ceb
					(_total_subobjects_size))
Packit Service fa3ceb
void *_talloc_pooled_object(const void *ctx,
Packit Service fa3ceb
			    size_t type_size,
Packit Service fa3ceb
			    const char *type_name,
Packit Service fa3ceb
			    unsigned num_subobjects,
Packit Service fa3ceb
			    size_t total_subobjects_size);
Packit Service fa3ceb
#endif
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Free a talloc chunk and NULL out the pointer.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * TALLOC_FREE() frees a pointer and sets it to NULL. Use this if you want
Packit Service fa3ceb
 * immediate feedback (i.e. crash) if you use a pointer after having free'ed
Packit Service fa3ceb
 * it.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ctx      The chunk to be freed.
Packit Service fa3ceb
 */
Packit Service fa3ceb
#define TALLOC_FREE(ctx) do { if (ctx != NULL) { talloc_free(ctx); ctx=NULL; } } while(0)
Packit Service fa3ceb
Packit Service fa3ceb
/* @} ******************************************************************/
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * \defgroup talloc_ref The talloc reference function.
Packit Service fa3ceb
 * @ingroup talloc
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This module contains the definitions around talloc references
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @{
Packit Service fa3ceb
 */
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Increase the reference count of a talloc chunk.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The talloc_increase_ref_count(ptr) function is exactly equivalent to:
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      talloc_reference(NULL, ptr);
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * You can use either syntax, depending on which you think is clearer in
Packit Service fa3ceb
 * your code.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ptr      The pointer to increase the reference count.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              0 on success, -1 on error.
Packit Service fa3ceb
 */
Packit Service fa3ceb
int talloc_increase_ref_count(const void *ptr);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Get the number of references to a talloc chunk.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ptr      The pointer to retrieve the reference count from.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The number of references.
Packit Service fa3ceb
 */
Packit Service fa3ceb
size_t talloc_reference_count(const void *ptr);
Packit Service fa3ceb
Packit Service fa3ceb
#ifdef DOXYGEN
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Create an additional talloc parent to a pointer.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The talloc_reference() function makes "context" an additional parent of
Packit Service fa3ceb
 * ptr. Each additional reference consumes around 48 bytes of memory on intel
Packit Service fa3ceb
 * x86 platforms.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * If ptr is NULL, then the function is a no-op, and simply returns NULL.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * After creating a reference you can free it in one of the following ways:
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * - you can talloc_free() any parent of the original pointer. That
Packit Service fa3ceb
 *   will reduce the number of parents of this pointer by 1, and will
Packit Service fa3ceb
 *   cause this pointer to be freed if it runs out of parents.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * - you can talloc_free() the pointer itself if it has at maximum one
Packit Service fa3ceb
 *   parent. This behaviour has been changed since the release of version
Packit Service fa3ceb
 *   2.0. Further information in the description of "talloc_free".
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * For more control on which parent to remove, see talloc_unlink()
Packit Service fa3ceb
 * @param[in]  ctx      The additional parent.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ptr      The pointer you want to create an additional parent for.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The original pointer 'ptr', NULL if talloc ran out of
Packit Service fa3ceb
 *                      memory in creating the reference.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @warning You should try to avoid using this interface. It turns a beautiful
Packit Service fa3ceb
 *          talloc-tree into a graph. It is often really hard to debug if you
Packit Service fa3ceb
 *          screw something up by accident.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * Example:
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      unsigned int *a, *b, *c;
Packit Service fa3ceb
 *      a = talloc(NULL, unsigned int);
Packit Service fa3ceb
 *      b = talloc(NULL, unsigned int);
Packit Service fa3ceb
 *      c = talloc(a, unsigned int);
Packit Service fa3ceb
 *      // b also serves as a parent of c.
Packit Service fa3ceb
 *      talloc_reference(b, c);
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @see talloc_unlink()
Packit Service fa3ceb
 */
Packit Service fa3ceb
void *talloc_reference(const void *ctx, const void *ptr);
Packit Service fa3ceb
#else
Packit Service fa3ceb
#define talloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_reference_loc((ctx),(ptr), __location__)
Packit Service fa3ceb
void *_talloc_reference_loc(const void *context, const void *ptr, const char *location);
Packit Service fa3ceb
#endif
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Remove a specific parent from a talloc chunk.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The function removes a specific parent from ptr. The context passed must
Packit Service fa3ceb
 * either be a context used in talloc_reference() with this pointer, or must be
Packit Service fa3ceb
 * a direct parent of ptr.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * You can just use talloc_free() instead of talloc_unlink() if there
Packit Service fa3ceb
 * is at maximum one parent. This behaviour has been changed since the
Packit Service fa3ceb
 * release of version 2.0. Further information in the description of
Packit Service fa3ceb
 * "talloc_free".
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  context  The talloc parent to remove.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ptr      The talloc ptr you want to remove the parent from.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              0 on success, -1 on error.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @note If the parent has already been removed using talloc_free() then
Packit Service fa3ceb
 * this function will fail and will return -1.  Likewise, if ptr is NULL,
Packit Service fa3ceb
 * then the function will make no modifications and return -1.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @warning You should try to avoid using this interface. It turns a beautiful
Packit Service fa3ceb
 *          talloc-tree into a graph. It is often really hard to debug if you
Packit Service fa3ceb
 *          screw something up by accident.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * Example:
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      unsigned int *a, *b, *c;
Packit Service fa3ceb
 *      a = talloc(NULL, unsigned int);
Packit Service fa3ceb
 *      b = talloc(NULL, unsigned int);
Packit Service fa3ceb
 *      c = talloc(a, unsigned int);
Packit Service fa3ceb
 *      // b also serves as a parent of c.
Packit Service fa3ceb
 *      talloc_reference(b, c);
Packit Service fa3ceb
 *      talloc_unlink(b, c);
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 */
Packit Service fa3ceb
int talloc_unlink(const void *context, void *ptr);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Provide a talloc context that is freed at program exit.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This is a handy utility function that returns a talloc context
Packit Service fa3ceb
 * which will be automatically freed on program exit. This can be used
Packit Service fa3ceb
 * to reduce the noise in memory leak reports.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * Never use this in code that might be used in objects loaded with
Packit Service fa3ceb
 * dlopen and unloaded with dlclose. talloc_autofree_context()
Packit Service fa3ceb
 * internally uses atexit(3). Some platforms like modern Linux handles
Packit Service fa3ceb
 * this fine, but for example FreeBSD does not deal well with dlopen()
Packit Service fa3ceb
 * and atexit() used simultaneously: dlclose() does not clean up the
Packit Service fa3ceb
 * list of atexit-handlers, so when the program exits the code that
Packit Service fa3ceb
 * was registered from within talloc_autofree_context() is gone, the
Packit Service fa3ceb
 * program crashes at exit.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              A talloc context, NULL on error.
Packit Service fa3ceb
 */
Packit Service fa3ceb
void *talloc_autofree_context(void) _DEPRECATED_;
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Get the size of a talloc chunk.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This function lets you know the amount of memory allocated so far by
Packit Service fa3ceb
 * this context. It does NOT account for subcontext memory.
Packit Service fa3ceb
 * This can be used to calculate the size of an array.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ctx      The talloc chunk.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The size of the talloc chunk.
Packit Service fa3ceb
 */
Packit Service fa3ceb
size_t talloc_get_size(const void *ctx);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Show the parentage of a context.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  context            The talloc context to look at.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  file               The output to use, a file, stdout or stderr.
Packit Service fa3ceb
 */
Packit Service fa3ceb
void talloc_show_parents(const void *context, FILE *file);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Check if a context is parent of a talloc chunk.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This checks if context is referenced in the talloc hierarchy above ptr.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  context  The assumed talloc context.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ptr      The talloc chunk to check.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              Return 1 if this is the case, 0 if not.
Packit Service fa3ceb
 */
Packit Service fa3ceb
int talloc_is_parent(const void *context, const void *ptr);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Change the parent context of a talloc pointer.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The function changes the parent context of a talloc pointer. It is typically
Packit Service fa3ceb
 * used when the context that the pointer is currently a child of is going to be
Packit Service fa3ceb
 * freed and you wish to keep the memory for a longer time.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The difference between talloc_reparent() and talloc_steal() is that
Packit Service fa3ceb
 * talloc_reparent() can specify which parent you wish to change. This is
Packit Service fa3ceb
 * useful when a pointer has multiple parents via references.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  old_parent
Packit Service fa3ceb
 * @param[in]  new_parent
Packit Service fa3ceb
 * @param[in]  ptr
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              Return the pointer you passed. It does not have any
Packit Service fa3ceb
 *                      failure modes.
Packit Service fa3ceb
 */
Packit Service fa3ceb
void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr);
Packit Service fa3ceb
Packit Service fa3ceb
/* @} ******************************************************************/
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @defgroup talloc_array The talloc array functions
Packit Service fa3ceb
 * @ingroup talloc
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * Talloc contains some handy helpers for handling Arrays conveniently
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @{
Packit Service fa3ceb
 */
Packit Service fa3ceb
Packit Service fa3ceb
#ifdef DOXYGEN
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Allocate an array.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The macro is equivalent to:
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      (type *)talloc_size(ctx, sizeof(type) * count);
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * except that it provides integer overflow protection for the multiply,
Packit Service fa3ceb
 * returning NULL if the multiply overflows.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ctx      The talloc context to hang the result off.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  type     The type that we want to allocate.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  count    The number of 'type' elements you want to allocate.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The allocated result, properly cast to 'type *', NULL on
Packit Service fa3ceb
 *                      error.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * Example:
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      unsigned int *a, *b;
Packit Service fa3ceb
 *      a = talloc_zero(NULL, unsigned int);
Packit Service fa3ceb
 *      b = talloc_array(a, unsigned int, 100);
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @see talloc()
Packit Service fa3ceb
 * @see talloc_zero_array()
Packit Service fa3ceb
 */
Packit Service fa3ceb
void *talloc_array(const void *ctx, #type, unsigned count);
Packit Service fa3ceb
#else
Packit Service fa3ceb
#define talloc_array(ctx, type, count) (type *)_talloc_array(ctx, sizeof(type), count, #type)
Packit Service fa3ceb
void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name);
Packit Service fa3ceb
#endif
Packit Service fa3ceb
Packit Service fa3ceb
#ifdef DOXYGEN
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Allocate an array.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ctx      The talloc context to hang the result off.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  size     The size of an array element.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  count    The number of elements you want to allocate.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The allocated result, NULL on error.
Packit Service fa3ceb
 */
Packit Service fa3ceb
void *talloc_array_size(const void *ctx, size_t size, unsigned count);
Packit Service fa3ceb
#else
Packit Service fa3ceb
#define talloc_array_size(ctx, size, count) _talloc_array(ctx, size, count, __location__)
Packit Service fa3ceb
#endif
Packit Service fa3ceb
Packit Service fa3ceb
#ifdef DOXYGEN
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Allocate an array into a typed pointer.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The macro should be used when you have a pointer to an array and want to
Packit Service fa3ceb
 * allocate memory of an array to point at with this pointer. When compiling
Packit Service fa3ceb
 * with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_array_size()
Packit Service fa3ceb
 * and talloc_get_name() will return the current location in the source file
Packit Service fa3ceb
 * and not the type.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ctx      The talloc context to hang the result off.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ptr      The pointer you want to assign the result to.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  count    The number of elements you want to allocate.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The allocated memory chunk, properly casted. NULL on
Packit Service fa3ceb
 *                      error.
Packit Service fa3ceb
 */
Packit Service fa3ceb
void *talloc_array_ptrtype(const void *ctx, const void *ptr, unsigned count);
Packit Service fa3ceb
#else
Packit Service fa3ceb
#define talloc_array_ptrtype(ctx, ptr, count) (_TALLOC_TYPEOF(ptr))talloc_array_size(ctx, sizeof(*(ptr)), count)
Packit Service fa3ceb
#endif
Packit Service fa3ceb
Packit Service fa3ceb
#ifdef DOXYGEN
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Get the number of elements in a talloc'ed array.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * A talloc chunk carries its own size, so for talloc'ed arrays it is not
Packit Service fa3ceb
 * necessary to store the number of elements explicitly.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ctx      The allocated array.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The number of elements in ctx.
Packit Service fa3ceb
 */
Packit Service fa3ceb
size_t talloc_array_length(const void *ctx);
Packit Service fa3ceb
#else
Packit Service fa3ceb
#define talloc_array_length(ctx) (talloc_get_size(ctx)/sizeof(*ctx))
Packit Service fa3ceb
#endif
Packit Service fa3ceb
Packit Service fa3ceb
#ifdef DOXYGEN
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Allocate a zero-initialized array
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ctx      The talloc context to hang the result off.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  type     The type that we want to allocate.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  count    The number of "type" elements you want to allocate.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The allocated result casted to "type *", NULL on error.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The talloc_zero_array() macro is equivalent to:
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *     ptr = talloc_array(ctx, type, count);
Packit Service fa3ceb
 *     if (ptr) memset(ptr, 0, sizeof(type) * count);
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 */
Packit Service fa3ceb
void *talloc_zero_array(const void *ctx, #type, unsigned count);
Packit Service fa3ceb
#else
Packit Service fa3ceb
#define talloc_zero_array(ctx, type, count) (type *)_talloc_zero_array(ctx, sizeof(type), count, #type)
Packit Service fa3ceb
void *_talloc_zero_array(const void *ctx,
Packit Service fa3ceb
			 size_t el_size,
Packit Service fa3ceb
			 unsigned count,
Packit Service fa3ceb
			 const char *name);
Packit Service fa3ceb
#endif
Packit Service fa3ceb
Packit Service fa3ceb
#ifdef DOXYGEN
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Change the size of a talloc array.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The macro changes the size of a talloc pointer. The 'count' argument is the
Packit Service fa3ceb
 * number of elements of type 'type' that you want the resulting pointer to
Packit Service fa3ceb
 * hold.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * talloc_realloc() has the following equivalences:
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      talloc_realloc(ctx, NULL, type, 1) ==> talloc(ctx, type);
Packit Service fa3ceb
 *      talloc_realloc(ctx, NULL, type, N) ==> talloc_array(ctx, type, N);
Packit Service fa3ceb
 *      talloc_realloc(ctx, ptr, type, 0)  ==> talloc_free(ptr);
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The "context" argument is only used if "ptr" is NULL, otherwise it is
Packit Service fa3ceb
 * ignored.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ctx      The parent context used if ptr is NULL.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ptr      The chunk to be resized.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  type     The type of the array element inside ptr.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  count    The intended number of array elements.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The new array, NULL on error. The call will fail either
Packit Service fa3ceb
 *                      due to a lack of memory, or because the pointer has more
Packit Service fa3ceb
 *                      than one parent (see talloc_reference()).
Packit Service fa3ceb
 */
Packit Service fa3ceb
void *talloc_realloc(const void *ctx, void *ptr, #type, size_t count);
Packit Service fa3ceb
#else
Packit Service fa3ceb
#define talloc_realloc(ctx, p, type, count) (type *)_talloc_realloc_array(ctx, p, sizeof(type), count, #type)
Packit Service fa3ceb
void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name);
Packit Service fa3ceb
#endif
Packit Service fa3ceb
Packit Service fa3ceb
#ifdef DOXYGEN
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Untyped realloc to change the size of a talloc array.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The macro is useful when the type is not known so the typesafe
Packit Service fa3ceb
 * talloc_realloc() cannot be used.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ctx      The parent context used if 'ptr' is NULL.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ptr      The chunk to be resized.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  size     The new chunk size.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The new array, NULL on error.
Packit Service fa3ceb
 */
Packit Service fa3ceb
void *talloc_realloc_size(const void *ctx, void *ptr, size_t size);
Packit Service fa3ceb
#else
Packit Service fa3ceb
#define talloc_realloc_size(ctx, ptr, size) _talloc_realloc(ctx, ptr, size, __location__)
Packit Service fa3ceb
void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name);
Packit Service fa3ceb
#endif
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Provide a function version of talloc_realloc_size.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This is a non-macro version of talloc_realloc(), which is useful as
Packit Service fa3ceb
 * libraries sometimes want a ralloc function pointer. A realloc()
Packit Service fa3ceb
 * implementation encapsulates the functionality of malloc(), free() and
Packit Service fa3ceb
 * realloc() in one call, which is why it is useful to be able to pass around
Packit Service fa3ceb
 * a single function pointer.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  context  The parent context used if ptr is NULL.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ptr      The chunk to be resized.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  size     The new chunk size.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The new chunk, NULL on error.
Packit Service fa3ceb
 */
Packit Service fa3ceb
void *talloc_realloc_fn(const void *context, void *ptr, size_t size);
Packit Service fa3ceb
Packit Service fa3ceb
/* @} ******************************************************************/
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @defgroup talloc_string The talloc string functions.
Packit Service fa3ceb
 * @ingroup talloc
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * talloc string allocation and manipulation functions.
Packit Service fa3ceb
 * @{
Packit Service fa3ceb
 */
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Duplicate a string into a talloc chunk.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This function is equivalent to:
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      ptr = talloc_size(ctx, strlen(p)+1);
Packit Service fa3ceb
 *      if (ptr) memcpy(ptr, p, strlen(p)+1);
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This functions sets the name of the new pointer to the passed
Packit Service fa3ceb
 * string. This is equivalent to:
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      talloc_set_name_const(ptr, ptr)
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  t        The talloc context to hang the result off.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  p        The string you want to duplicate.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The duplicated string, NULL on error.
Packit Service fa3ceb
 */
Packit Service fa3ceb
char *talloc_strdup(const void *t, const char *p);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Append a string to given string.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The destination string is reallocated to take
Packit Service fa3ceb
 * strlen(s) + strlen(a) + 1 characters.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This functions sets the name of the new pointer to the new
Packit Service fa3ceb
 * string. This is equivalent to:
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      talloc_set_name_const(ptr, ptr)
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * If s == NULL then new context is created.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  s        The destination to append to.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  a        The string you want to append.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The concatenated strings, NULL on error.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @see talloc_strdup()
Packit Service fa3ceb
 * @see talloc_strdup_append_buffer()
Packit Service fa3ceb
 */
Packit Service fa3ceb
char *talloc_strdup_append(char *s, const char *a);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Append a string to a given buffer.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This is a more efficient version of talloc_strdup_append(). It determines the
Packit Service fa3ceb
 * length of the destination string by the size of the talloc context.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * Use this very carefully as it produces a different result than
Packit Service fa3ceb
 * talloc_strdup_append() when a zero character is in the middle of the
Packit Service fa3ceb
 * destination string.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      char *str_a = talloc_strdup(NULL, "hello world");
Packit Service fa3ceb
 *      char *str_b = talloc_strdup(NULL, "hello world");
Packit Service fa3ceb
 *      str_a[5] = str_b[5] = '\0'
Packit Service fa3ceb
 *
Packit Service fa3ceb
 *      char *app = talloc_strdup_append(str_a, ", hello");
Packit Service fa3ceb
 *      char *buf = talloc_strdup_append_buffer(str_b, ", hello");
Packit Service fa3ceb
 *
Packit Service fa3ceb
 *      printf("%s\n", app); // hello, hello (app = "hello, hello")
Packit Service fa3ceb
 *      printf("%s\n", buf); // hello (buf = "hello\0world, hello")
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * If s == NULL then new context is created.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  s        The destination buffer to append to.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  a        The string you want to append.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The concatenated strings, NULL on error.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @see talloc_strdup()
Packit Service fa3ceb
 * @see talloc_strdup_append()
Packit Service fa3ceb
 * @see talloc_array_length()
Packit Service fa3ceb
 */
Packit Service fa3ceb
char *talloc_strdup_append_buffer(char *s, const char *a);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Duplicate a length-limited string into a talloc chunk.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This function is the talloc equivalent of the C library function strndup(3).
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This functions sets the name of the new pointer to the passed string. This is
Packit Service fa3ceb
 * equivalent to:
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      talloc_set_name_const(ptr, ptr)
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  t        The talloc context to hang the result off.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  p        The string you want to duplicate.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  n        The maximum string length to duplicate.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The duplicated string, NULL on error.
Packit Service fa3ceb
 */
Packit Service fa3ceb
char *talloc_strndup(const void *t, const char *p, size_t n);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Append at most n characters of a string to given string.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The destination string is reallocated to take
Packit Service fa3ceb
 * strlen(s) + strnlen(a, n) + 1 characters.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This functions sets the name of the new pointer to the new
Packit Service fa3ceb
 * string. This is equivalent to:
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      talloc_set_name_const(ptr, ptr)
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * If s == NULL then new context is created.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  s        The destination string to append to.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  a        The source string you want to append.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  n        The number of characters you want to append from the
Packit Service fa3ceb
 *                      string.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The concatenated strings, NULL on error.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @see talloc_strndup()
Packit Service fa3ceb
 * @see talloc_strndup_append_buffer()
Packit Service fa3ceb
 */
Packit Service fa3ceb
char *talloc_strndup_append(char *s, const char *a, size_t n);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Append at most n characters of a string to given buffer
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This is a more efficient version of talloc_strndup_append(). It determines
Packit Service fa3ceb
 * the length of the destination string by the size of the talloc context.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * Use this very carefully as it produces a different result than
Packit Service fa3ceb
 * talloc_strndup_append() when a zero character is in the middle of the
Packit Service fa3ceb
 * destination string.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      char *str_a = talloc_strdup(NULL, "hello world");
Packit Service fa3ceb
 *      char *str_b = talloc_strdup(NULL, "hello world");
Packit Service fa3ceb
 *      str_a[5] = str_b[5] = '\0'
Packit Service fa3ceb
 *
Packit Service fa3ceb
 *      char *app = talloc_strndup_append(str_a, ", hello", 7);
Packit Service fa3ceb
 *      char *buf = talloc_strndup_append_buffer(str_b, ", hello", 7);
Packit Service fa3ceb
 *
Packit Service fa3ceb
 *      printf("%s\n", app); // hello, hello (app = "hello, hello")
Packit Service fa3ceb
 *      printf("%s\n", buf); // hello (buf = "hello\0world, hello")
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * If s == NULL then new context is created.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  s        The destination buffer to append to.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  a        The source string you want to append.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  n        The number of characters you want to append from the
Packit Service fa3ceb
 *                      string.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The concatenated strings, NULL on error.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @see talloc_strndup()
Packit Service fa3ceb
 * @see talloc_strndup_append()
Packit Service fa3ceb
 * @see talloc_array_length()
Packit Service fa3ceb
 */
Packit Service fa3ceb
char *talloc_strndup_append_buffer(char *s, const char *a, size_t n);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Format a string given a va_list.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This function is the talloc equivalent of the C library function
Packit Service fa3ceb
 * vasprintf(3).
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This functions sets the name of the new pointer to the new string. This is
Packit Service fa3ceb
 * equivalent to:
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      talloc_set_name_const(ptr, ptr)
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  t        The talloc context to hang the result off.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  fmt      The format string.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ap       The parameters used to fill fmt.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The formatted string, NULL on error.
Packit Service fa3ceb
 */
Packit Service fa3ceb
char *talloc_vasprintf(const void *t, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Format a string given a va_list and append it to the given destination
Packit Service fa3ceb
 *        string.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  s        The destination string to append to.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  fmt      The format string.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ap       The parameters used to fill fmt.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The formatted string, NULL on error.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @see talloc_vasprintf()
Packit Service fa3ceb
 */
Packit Service fa3ceb
char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Format a string given a va_list and append it to the given destination
Packit Service fa3ceb
 *        buffer.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  s        The destination buffer to append to.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  fmt      The format string.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ap       The parameters used to fill fmt.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The formatted string, NULL on error.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @see talloc_vasprintf()
Packit Service fa3ceb
 */
Packit Service fa3ceb
char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Format a string.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This function is the talloc equivalent of the C library function asprintf(3).
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This functions sets the name of the new pointer to the new string. This is
Packit Service fa3ceb
 * equivalent to:
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      talloc_set_name_const(ptr, ptr)
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  t        The talloc context to hang the result off.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  fmt      The format string.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ...      The parameters used to fill fmt.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The formatted string, NULL on error.
Packit Service fa3ceb
 */
Packit Service fa3ceb
char *talloc_asprintf(const void *t, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Append a formatted string to another string.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This function appends the given formatted string to the given string. Use
Packit Service fa3ceb
 * this variant when the string in the current talloc buffer may have been
Packit Service fa3ceb
 * truncated in length.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This functions sets the name of the new pointer to the new
Packit Service fa3ceb
 * string. This is equivalent to:
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      talloc_set_name_const(ptr, ptr)
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * If s == NULL then new context is created.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  s        The string to append to.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  fmt      The format string.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ...      The parameters used to fill fmt.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The formatted string, NULL on error.
Packit Service fa3ceb
 */
Packit Service fa3ceb
char *talloc_asprintf_append(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Append a formatted string to another string.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This is a more efficient version of talloc_asprintf_append(). It determines
Packit Service fa3ceb
 * the length of the destination string by the size of the talloc context.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * Use this very carefully as it produces a different result than
Packit Service fa3ceb
 * talloc_asprintf_append() when a zero character is in the middle of the
Packit Service fa3ceb
 * destination string.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      char *str_a = talloc_strdup(NULL, "hello world");
Packit Service fa3ceb
 *      char *str_b = talloc_strdup(NULL, "hello world");
Packit Service fa3ceb
 *      str_a[5] = str_b[5] = '\0'
Packit Service fa3ceb
 *
Packit Service fa3ceb
 *      char *app = talloc_asprintf_append(str_a, "%s", ", hello");
Packit Service fa3ceb
 *      char *buf = talloc_strdup_append_buffer(str_b, "%s", ", hello");
Packit Service fa3ceb
 *
Packit Service fa3ceb
 *      printf("%s\n", app); // hello, hello (app = "hello, hello")
Packit Service fa3ceb
 *      printf("%s\n", buf); // hello (buf = "hello\0world, hello")
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * If s == NULL then new context is created.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  s        The string to append to
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  fmt      The format string.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ...      The parameters used to fill fmt.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @return              The formatted string, NULL on error.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @see talloc_asprintf()
Packit Service fa3ceb
 * @see talloc_asprintf_append()
Packit Service fa3ceb
 */
Packit Service fa3ceb
char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
Packit Service fa3ceb
Packit Service fa3ceb
/* @} ******************************************************************/
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @defgroup talloc_debug The talloc debugging support functions
Packit Service fa3ceb
 * @ingroup talloc
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * To aid memory debugging, talloc contains routines to inspect the currently
Packit Service fa3ceb
 * allocated memory hierarchy.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @{
Packit Service fa3ceb
 */
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Walk a complete talloc hierarchy.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This provides a more flexible reports than talloc_report(). It
Packit Service fa3ceb
 * will recursively call the callback for the entire tree of memory
Packit Service fa3ceb
 * referenced by the pointer. References in the tree are passed with
Packit Service fa3ceb
 * is_ref = 1 and the pointer that is referenced.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * You can pass NULL for the pointer, in which case a report is
Packit Service fa3ceb
 * printed for the top level memory context, but only if
Packit Service fa3ceb
 * talloc_enable_leak_report() or talloc_enable_leak_report_full()
Packit Service fa3ceb
 * has been called.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The recursion is stopped when depth >= max_depth.
Packit Service fa3ceb
 * max_depth = -1 means only stop at leaf nodes.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ptr      The talloc chunk.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  depth    Internal parameter to control recursion. Call with 0.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  max_depth  Maximum recursion level.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  callback  Function to be called on every chunk.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  private_data  Private pointer passed to callback.
Packit Service fa3ceb
 */
Packit Service fa3ceb
void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
Packit Service fa3ceb
			    void (*callback)(const void *ptr,
Packit Service fa3ceb
					     int depth, int max_depth,
Packit Service fa3ceb
					     int is_ref,
Packit Service fa3ceb
					     void *private_data),
Packit Service fa3ceb
			    void *private_data);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Print a talloc hierarchy.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This provides a more flexible reports than talloc_report(). It
Packit Service fa3ceb
 * will let you specify the depth and max_depth.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ptr      The talloc chunk.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  depth    Internal parameter to control recursion. Call with 0.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  max_depth  Maximum recursion level.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  f        The file handle to print to.
Packit Service fa3ceb
 */
Packit Service fa3ceb
void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Print a summary report of all memory used by ptr.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This provides a more detailed report than talloc_report(). It will
Packit Service fa3ceb
 * recursively print the entire tree of memory referenced by the
Packit Service fa3ceb
 * pointer. References in the tree are shown by giving the name of the
Packit Service fa3ceb
 * pointer that is referenced.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * You can pass NULL for the pointer, in which case a report is printed
Packit Service fa3ceb
 * for the top level memory context, but only if
Packit Service fa3ceb
 * talloc_enable_leak_report() or talloc_enable_leak_report_full() has
Packit Service fa3ceb
 * been called.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ptr      The talloc chunk.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  f        The file handle to print to.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * Example:
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      unsigned int *a, *b;
Packit Service fa3ceb
 *      a = talloc(NULL, unsigned int);
Packit Service fa3ceb
 *      b = talloc(a, unsigned int);
Packit Service fa3ceb
 *      fprintf(stderr, "Dumping memory tree for a:\n");
Packit Service fa3ceb
 *      talloc_report_full(a, stderr);
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @see talloc_report()
Packit Service fa3ceb
 */
Packit Service fa3ceb
void talloc_report_full(const void *ptr, FILE *f);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Print a summary report of all memory used by ptr.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This function prints a summary report of all memory used by ptr. One line of
Packit Service fa3ceb
 * report is printed for each immediate child of ptr, showing the total memory
Packit Service fa3ceb
 * and number of blocks used by that child.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * You can pass NULL for the pointer, in which case a report is printed
Packit Service fa3ceb
 * for the top level memory context, but only if talloc_enable_leak_report()
Packit Service fa3ceb
 * or talloc_enable_leak_report_full() has been called.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  ptr      The talloc chunk.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @param[in]  f        The file handle to print to.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * Example:
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 *      unsigned int *a, *b;
Packit Service fa3ceb
 *      a = talloc(NULL, unsigned int);
Packit Service fa3ceb
 *      b = talloc(a, unsigned int);
Packit Service fa3ceb
 *      fprintf(stderr, "Summary of memory tree for a:\n");
Packit Service fa3ceb
 *      talloc_report(a, stderr);
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @see talloc_report_full()
Packit Service fa3ceb
 */
Packit Service fa3ceb
void talloc_report(const void *ptr, FILE *f);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Enable tracking the use of NULL memory contexts.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This enables tracking of the NULL memory context without enabling leak
Packit Service fa3ceb
 * reporting on exit. Useful for when you want to do your own leak
Packit Service fa3ceb
 * reporting call via talloc_report_null_full();
Packit Service fa3ceb
 */
Packit Service fa3ceb
void talloc_enable_null_tracking(void);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Enable tracking the use of NULL memory contexts.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This enables tracking of the NULL memory context without enabling leak
Packit Service fa3ceb
 * reporting on exit. Useful for when you want to do your own leak
Packit Service fa3ceb
 * reporting call via talloc_report_null_full();
Packit Service fa3ceb
 */
Packit Service fa3ceb
void talloc_enable_null_tracking_no_autofree(void);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Disable tracking of the NULL memory context.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This disables tracking of the NULL memory context.
Packit Service fa3ceb
 */
Packit Service fa3ceb
void talloc_disable_null_tracking(void);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Enable leak report when a program exits.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This enables calling of talloc_report(NULL, stderr) when the program
Packit Service fa3ceb
 * exits. In Samba4 this is enabled by using the --leak-report command
Packit Service fa3ceb
 * line option.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * For it to be useful, this function must be called before any other
Packit Service fa3ceb
 * talloc function as it establishes a "null context" that acts as the
Packit Service fa3ceb
 * top of the tree. If you don't call this function first then passing
Packit Service fa3ceb
 * NULL to talloc_report() or talloc_report_full() won't give you the
Packit Service fa3ceb
 * full tree printout.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * Here is a typical talloc report:
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 * talloc report on 'null_context' (total 267 bytes in 15 blocks)
Packit Service fa3ceb
 *      libcli/auth/spnego_parse.c:55  contains     31 bytes in   2 blocks
Packit Service fa3ceb
 *      libcli/auth/spnego_parse.c:55  contains     31 bytes in   2 blocks
Packit Service fa3ceb
 *      iconv(UTF8,CP850)              contains     42 bytes in   2 blocks
Packit Service fa3ceb
 *      libcli/auth/spnego_parse.c:55  contains     31 bytes in   2 blocks
Packit Service fa3ceb
 *      iconv(CP850,UTF8)              contains     42 bytes in   2 blocks
Packit Service fa3ceb
 *      iconv(UTF8,UTF-16LE)           contains     45 bytes in   2 blocks
Packit Service fa3ceb
 *      iconv(UTF-16LE,UTF8)           contains     45 bytes in   2 blocks
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 */
Packit Service fa3ceb
void talloc_enable_leak_report(void);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Enable full leak report when a program exits.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * This enables calling of talloc_report_full(NULL, stderr) when the
Packit Service fa3ceb
 * program exits. In Samba4 this is enabled by using the
Packit Service fa3ceb
 * --leak-report-full command line option.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * For it to be useful, this function must be called before any other
Packit Service fa3ceb
 * talloc function as it establishes a "null context" that acts as the
Packit Service fa3ceb
 * top of the tree. If you don't call this function first then passing
Packit Service fa3ceb
 * NULL to talloc_report() or talloc_report_full() won't give you the
Packit Service fa3ceb
 * full tree printout.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * Here is a typical full report:
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * @code
Packit Service fa3ceb
 * full talloc report on 'root' (total 18 bytes in 8 blocks)
Packit Service fa3ceb
 *      p1                             contains     18 bytes in   7 blocks (ref 0)
Packit Service fa3ceb
 *      r1                             contains     13 bytes in   2 blocks (ref 0)
Packit Service fa3ceb
 *      reference to: p2
Packit Service fa3ceb
 *      p2                             contains      1 bytes in   1 blocks (ref 1)
Packit Service fa3ceb
 *      x3                             contains      1 bytes in   1 blocks (ref 0)
Packit Service fa3ceb
 *      x2                             contains      1 bytes in   1 blocks (ref 0)
Packit Service fa3ceb
 *      x1                             contains      1 bytes in   1 blocks (ref 0)
Packit Service fa3ceb
 * @endcode
Packit Service fa3ceb
 */
Packit Service fa3ceb
void talloc_enable_leak_report_full(void);
Packit Service fa3ceb
Packit Service fa3ceb
/**
Packit Service fa3ceb
 * @brief Set a custom "abort" function that is called on serious error.
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The default "abort" function is abort().
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * The "abort" function is called when:
Packit Service fa3ceb
 *
Packit Service fa3ceb
 * 
    Packit Service fa3ceb
     *  
  • talloc_get_type_abort() fails
  • Packit Service fa3ceb
     *  
  • the provided pointer is not a valid talloc context
  • Packit Service fa3ceb
     *  
  • when the context meta data are invalid
  • Packit Service fa3ceb
     *  
  • when access after free is detected
  • Packit Service fa3ceb
     * 
    Packit Service fa3ceb
     *
    Packit Service fa3ceb
     * Example:
    Packit Service fa3ceb
     *
    Packit Service fa3ceb
     * @code
    Packit Service fa3ceb
     * void my_abort(const char *reason)
    Packit Service fa3ceb
     * {
    Packit Service fa3ceb
     *      fprintf(stderr, "talloc abort: %s\n", reason);
    Packit Service fa3ceb
     *      abort();
    Packit Service fa3ceb
     * }
    Packit Service fa3ceb
     *
    Packit Service fa3ceb
     *      talloc_set_abort_fn(my_abort);
    Packit Service fa3ceb
     * @endcode
    Packit Service fa3ceb
     *
    Packit Service fa3ceb
     * @param[in]  abort_fn      The new "abort" function.
    Packit Service fa3ceb
     *
    Packit Service fa3ceb
     * @see talloc_set_log_fn()
    Packit Service fa3ceb
     * @see talloc_get_type()
    Packit Service fa3ceb
     */
    Packit Service fa3ceb
    void talloc_set_abort_fn(void (*abort_fn)(const char *reason));
    Packit Service fa3ceb
    Packit Service fa3ceb
    /**
    Packit Service fa3ceb
     * @brief Set a logging function.
    Packit Service fa3ceb
     *
    Packit Service fa3ceb
     * @param[in]  log_fn      The logging function.
    Packit Service fa3ceb
     *
    Packit Service fa3ceb
     * @see talloc_set_log_stderr()
    Packit Service fa3ceb
     * @see talloc_set_abort_fn()
    Packit Service fa3ceb
     */
    Packit Service fa3ceb
    void talloc_set_log_fn(void (*log_fn)(const char *message));
    Packit Service fa3ceb
    Packit Service fa3ceb
    /**
    Packit Service fa3ceb
     * @brief Set stderr as the output for logs.
    Packit Service fa3ceb
     *
    Packit Service fa3ceb
     * @see talloc_set_log_fn()
    Packit Service fa3ceb
     * @see talloc_set_abort_fn()
    Packit Service fa3ceb
     */
    Packit Service fa3ceb
    void talloc_set_log_stderr(void);
    Packit Service fa3ceb
    Packit Service fa3ceb
    /**
    Packit Service fa3ceb
     * @brief Set a max memory limit for the current context hierarchy
    Packit Service fa3ceb
     *	  This affects all children of this context and constrain any
    Packit Service fa3ceb
     *	  allocation in the hierarchy to never exceed the limit set.
    Packit Service fa3ceb
     *	  The limit can be removed by setting 0 (unlimited) as the
    Packit Service fa3ceb
     *	  max_size by calling the function again on the same context.
    Packit Service fa3ceb
     *	  Memory limits can also be nested, meaning a child can have
    Packit Service fa3ceb
     *	  a stricter memory limit than a parent.
    Packit Service fa3ceb
     *	  Memory limits are enforced only at memory allocation time.
    Packit Service fa3ceb
     *	  Stealing a context into a 'limited' hierarchy properly
    Packit Service fa3ceb
     *	  updates memory usage but does *not* cause failure if the
    Packit Service fa3ceb
     *	  move causes the new parent to exceed its limits. However
    Packit Service fa3ceb
     *	  any further allocation on that hierarchy will then fail.
    Packit Service fa3ceb
     *
    Packit Service fa3ceb
     * @warning talloc memlimit functionality is deprecated. Please
    Packit Service fa3ceb
     *	    consider using cgroup memory limits instead.
    Packit Service fa3ceb
     *
    Packit Service fa3ceb
     * @param[in]	ctx		The talloc context to set the limit on
    Packit Service fa3ceb
     * @param[in]	max_size	The (new) max_size
    Packit Service fa3ceb
     */
    Packit Service fa3ceb
    int talloc_set_memlimit(const void *ctx, size_t max_size) _DEPRECATED_;
    Packit Service fa3ceb
    Packit Service fa3ceb
    /* @} ******************************************************************/
    Packit Service fa3ceb
    Packit Service fa3ceb
    #if TALLOC_DEPRECATED
    Packit Service fa3ceb
    #define talloc_zero_p(ctx, type) talloc_zero(ctx, type)
    Packit Service fa3ceb
    #define talloc_p(ctx, type) talloc(ctx, type)
    Packit Service fa3ceb
    #define talloc_array_p(ctx, type, count) talloc_array(ctx, type, count)
    Packit Service fa3ceb
    #define talloc_realloc_p(ctx, p, type, count) talloc_realloc(ctx, p, type, count)
    Packit Service fa3ceb
    #define talloc_destroy(ctx) talloc_free(ctx)
    Packit Service fa3ceb
    #define talloc_append_string(c, s, a) (s?talloc_strdup_append(s,a):talloc_strdup(c, a))
    Packit Service fa3ceb
    #endif
    Packit Service fa3ceb
    Packit Service fa3ceb
    #ifndef TALLOC_MAX_DEPTH
    Packit Service fa3ceb
    #define TALLOC_MAX_DEPTH 10000
    Packit Service fa3ceb
    #endif
    Packit Service fa3ceb
    Packit Service fa3ceb
    #ifdef __cplusplus
    Packit Service fa3ceb
    } /* end of extern "C" */
    Packit Service fa3ceb
    #endif
    Packit Service fa3ceb
    Packit Service fa3ceb
    #endif