Blame talloc.c

rpm-build 95f51c
/*
rpm-build 95f51c
   Samba Unix SMB/CIFS implementation.
rpm-build 95f51c
rpm-build 95f51c
   Samba trivial allocation library - new interface
rpm-build 95f51c
rpm-build 95f51c
   NOTE: Please read talloc_guide.txt for full documentation
rpm-build 95f51c
rpm-build 95f51c
   Copyright (C) Andrew Tridgell 2004
rpm-build 95f51c
   Copyright (C) Stefan Metzmacher 2006
rpm-build 95f51c
rpm-build 95f51c
     ** NOTE! The following LGPL license applies to the talloc
rpm-build 95f51c
     ** library. This does NOT imply that all of Samba is released
rpm-build 95f51c
     ** under the LGPL
rpm-build 95f51c
rpm-build 95f51c
   This library is free software; you can redistribute it and/or
rpm-build 95f51c
   modify it under the terms of the GNU Lesser General Public
rpm-build 95f51c
   License as published by the Free Software Foundation; either
rpm-build 95f51c
   version 3 of the License, or (at your option) any later version.
rpm-build 95f51c
rpm-build 95f51c
   This library is distributed in the hope that it will be useful,
rpm-build 95f51c
   but WITHOUT ANY WARRANTY; without even the implied warranty of
rpm-build 95f51c
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
rpm-build 95f51c
   Lesser General Public License for more details.
rpm-build 95f51c
rpm-build 95f51c
   You should have received a copy of the GNU Lesser General Public
rpm-build 95f51c
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
rpm-build 95f51c
*/
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  inspired by http://swapped.cc/halloc/
rpm-build 95f51c
*/
rpm-build 95f51c
rpm-build 95f51c
#include "replace.h"
rpm-build 95f51c
#include "talloc.h"
rpm-build 95f51c
rpm-build 95f51c
#ifdef HAVE_SYS_AUXV_H
rpm-build 95f51c
#include <sys/auxv.h>
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#if (TALLOC_VERSION_MAJOR != TALLOC_BUILD_VERSION_MAJOR)
rpm-build 95f51c
#error "TALLOC_VERSION_MAJOR != TALLOC_BUILD_VERSION_MAJOR"
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#if (TALLOC_VERSION_MINOR != TALLOC_BUILD_VERSION_MINOR)
rpm-build 95f51c
#error "TALLOC_VERSION_MINOR != TALLOC_BUILD_VERSION_MINOR"
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
/* Special macros that are no-ops except when run under Valgrind on
rpm-build 95f51c
 * x86.  They've moved a little bit from valgrind 1.0.4 to 1.9.4 */
rpm-build 95f51c
#ifdef HAVE_VALGRIND_MEMCHECK_H
rpm-build 95f51c
        /* memcheck.h includes valgrind.h */
rpm-build 95f51c
#include <valgrind/memcheck.h>
rpm-build 95f51c
#elif defined(HAVE_VALGRIND_H)
rpm-build 95f51c
#include <valgrind.h>
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
/* use this to force every realloc to change the pointer, to stress test
rpm-build 95f51c
   code that might not cope */
rpm-build 95f51c
#define ALWAYS_REALLOC 0
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
#define MAX_TALLOC_SIZE 0x10000000
rpm-build 95f51c
rpm-build 95f51c
#define TALLOC_FLAG_FREE 0x01
rpm-build 95f51c
#define TALLOC_FLAG_LOOP 0x02
rpm-build 95f51c
#define TALLOC_FLAG_POOL 0x04		/* This is a talloc pool */
rpm-build 95f51c
#define TALLOC_FLAG_POOLMEM 0x08	/* This is allocated in a pool */
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
 * Bits above this are random, used to make it harder to fake talloc
rpm-build 95f51c
 * headers during an attack.  Try not to change this without good reason.
rpm-build 95f51c
 */
rpm-build 95f51c
#define TALLOC_FLAG_MASK 0x0F
rpm-build 95f51c
rpm-build 95f51c
#define TALLOC_MAGIC_REFERENCE ((const char *)1)
rpm-build 95f51c
rpm-build 95f51c
#define TALLOC_MAGIC_BASE 0xe814ec70
rpm-build 95f51c
#define TALLOC_MAGIC_NON_RANDOM ( \
rpm-build 95f51c
	~TALLOC_FLAG_MASK & ( \
rpm-build 95f51c
		TALLOC_MAGIC_BASE + \
rpm-build 95f51c
		(TALLOC_BUILD_VERSION_MAJOR << 24) + \
rpm-build 95f51c
		(TALLOC_BUILD_VERSION_MINOR << 16) + \
rpm-build 95f51c
		(TALLOC_BUILD_VERSION_RELEASE << 8)))
rpm-build 95f51c
static unsigned int talloc_magic = TALLOC_MAGIC_NON_RANDOM;
rpm-build 95f51c
rpm-build 95f51c
/* by default we abort when given a bad pointer (such as when talloc_free() is called
rpm-build 95f51c
   on a pointer that came from malloc() */
rpm-build 95f51c
#ifndef TALLOC_ABORT
rpm-build 95f51c
#define TALLOC_ABORT(reason) abort()
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#ifndef discard_const_p
rpm-build 95f51c
#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
rpm-build 95f51c
# define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
rpm-build 95f51c
#else
rpm-build 95f51c
# define discard_const_p(type, ptr) ((type *)(ptr))
rpm-build 95f51c
#endif
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
/* these macros gain us a few percent of speed on gcc */
rpm-build 95f51c
#if (__GNUC__ >= 3)
rpm-build 95f51c
/* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
rpm-build 95f51c
   as its first argument */
rpm-build 95f51c
#ifndef likely
rpm-build 95f51c
#define likely(x)   __builtin_expect(!!(x), 1)
rpm-build 95f51c
#endif
rpm-build 95f51c
#ifndef unlikely
rpm-build 95f51c
#define unlikely(x) __builtin_expect(!!(x), 0)
rpm-build 95f51c
#endif
rpm-build 95f51c
#else
rpm-build 95f51c
#ifndef likely
rpm-build 95f51c
#define likely(x) (x)
rpm-build 95f51c
#endif
rpm-build 95f51c
#ifndef unlikely
rpm-build 95f51c
#define unlikely(x) (x)
rpm-build 95f51c
#endif
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
/* this null_context is only used if talloc_enable_leak_report() or
rpm-build 95f51c
   talloc_enable_leak_report_full() is called, otherwise it remains
rpm-build 95f51c
   NULL
rpm-build 95f51c
*/
rpm-build 95f51c
static void *null_context;
rpm-build 95f51c
static bool talloc_report_null;
rpm-build 95f51c
static bool talloc_report_null_full;
rpm-build 95f51c
static void *autofree_context;
rpm-build 95f51c
rpm-build 95f51c
static void talloc_setup_atexit(void);
rpm-build 95f51c
rpm-build 95f51c
/* used to enable fill of memory on free, which can be useful for
rpm-build 95f51c
 * catching use after free errors when valgrind is too slow
rpm-build 95f51c
 */
rpm-build 95f51c
static struct {
rpm-build 95f51c
	bool initialised;
rpm-build 95f51c
	bool enabled;
rpm-build 95f51c
	uint8_t fill_value;
rpm-build 95f51c
} talloc_fill;
rpm-build 95f51c
rpm-build 95f51c
#define TALLOC_FILL_ENV "TALLOC_FREE_FILL"
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
 * do not wipe the header, to allow the
rpm-build 95f51c
 * double-free logic to still work
rpm-build 95f51c
 */
rpm-build 95f51c
#define TC_INVALIDATE_FULL_FILL_CHUNK(_tc) do { \
rpm-build 95f51c
	if (unlikely(talloc_fill.enabled)) { \
rpm-build 95f51c
		size_t _flen = (_tc)->size; \
rpm-build 95f51c
		char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
rpm-build 95f51c
		memset(_fptr, talloc_fill.fill_value, _flen); \
rpm-build 95f51c
	} \
rpm-build 95f51c
} while (0)
rpm-build 95f51c
rpm-build 95f51c
#if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
rpm-build 95f51c
/* Mark the whole chunk as not accessable */
rpm-build 95f51c
#define TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc) do { \
rpm-build 95f51c
	size_t _flen = TC_HDR_SIZE + (_tc)->size; \
rpm-build 95f51c
	char *_fptr = (char *)(_tc); \
rpm-build 95f51c
	VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
rpm-build 95f51c
} while(0)
rpm-build 95f51c
#else
rpm-build 95f51c
#define TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc) do { } while (0)
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#define TC_INVALIDATE_FULL_CHUNK(_tc) do { \
rpm-build 95f51c
	TC_INVALIDATE_FULL_FILL_CHUNK(_tc); \
rpm-build 95f51c
	TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc); \
rpm-build 95f51c
} while (0)
rpm-build 95f51c
rpm-build 95f51c
#define TC_INVALIDATE_SHRINK_FILL_CHUNK(_tc, _new_size) do { \
rpm-build 95f51c
	if (unlikely(talloc_fill.enabled)) { \
rpm-build 95f51c
		size_t _flen = (_tc)->size - (_new_size); \
rpm-build 95f51c
		char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
rpm-build 95f51c
		_fptr += (_new_size); \
rpm-build 95f51c
		memset(_fptr, talloc_fill.fill_value, _flen); \
rpm-build 95f51c
	} \
rpm-build 95f51c
} while (0)
rpm-build 95f51c
rpm-build 95f51c
#if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
rpm-build 95f51c
/* Mark the unused bytes not accessable */
rpm-build 95f51c
#define TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { \
rpm-build 95f51c
	size_t _flen = (_tc)->size - (_new_size); \
rpm-build 95f51c
	char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
rpm-build 95f51c
	_fptr += (_new_size); \
rpm-build 95f51c
	VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
rpm-build 95f51c
} while (0)
rpm-build 95f51c
#else
rpm-build 95f51c
#define TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#define TC_INVALIDATE_SHRINK_CHUNK(_tc, _new_size) do { \
rpm-build 95f51c
	TC_INVALIDATE_SHRINK_FILL_CHUNK(_tc, _new_size); \
rpm-build 95f51c
	TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size); \
rpm-build 95f51c
} while (0)
rpm-build 95f51c
rpm-build 95f51c
#define TC_UNDEFINE_SHRINK_FILL_CHUNK(_tc, _new_size) do { \
rpm-build 95f51c
	if (unlikely(talloc_fill.enabled)) { \
rpm-build 95f51c
		size_t _flen = (_tc)->size - (_new_size); \
rpm-build 95f51c
		char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
rpm-build 95f51c
		_fptr += (_new_size); \
rpm-build 95f51c
		memset(_fptr, talloc_fill.fill_value, _flen); \
rpm-build 95f51c
	} \
rpm-build 95f51c
} while (0)
rpm-build 95f51c
rpm-build 95f51c
#if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
rpm-build 95f51c
/* Mark the unused bytes as undefined */
rpm-build 95f51c
#define TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { \
rpm-build 95f51c
	size_t _flen = (_tc)->size - (_new_size); \
rpm-build 95f51c
	char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
rpm-build 95f51c
	_fptr += (_new_size); \
rpm-build 95f51c
	VALGRIND_MAKE_MEM_UNDEFINED(_fptr, _flen); \
rpm-build 95f51c
} while (0)
rpm-build 95f51c
#else
rpm-build 95f51c
#define TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#define TC_UNDEFINE_SHRINK_CHUNK(_tc, _new_size) do { \
rpm-build 95f51c
	TC_UNDEFINE_SHRINK_FILL_CHUNK(_tc, _new_size); \
rpm-build 95f51c
	TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size); \
rpm-build 95f51c
} while (0)
rpm-build 95f51c
rpm-build 95f51c
#if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
rpm-build 95f51c
/* Mark the new bytes as undefined */
rpm-build 95f51c
#define TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size) do { \
rpm-build 95f51c
	size_t _old_used = TC_HDR_SIZE + (_tc)->size; \
rpm-build 95f51c
	size_t _new_used = TC_HDR_SIZE + (_new_size); \
rpm-build 95f51c
	size_t _flen = _new_used - _old_used; \
rpm-build 95f51c
	char *_fptr = _old_used + (char *)(_tc); \
rpm-build 95f51c
	VALGRIND_MAKE_MEM_UNDEFINED(_fptr, _flen); \
rpm-build 95f51c
} while (0)
rpm-build 95f51c
#else
rpm-build 95f51c
#define TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
#define TC_UNDEFINE_GROW_CHUNK(_tc, _new_size) do { \
rpm-build 95f51c
	TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size); \
rpm-build 95f51c
} while (0)
rpm-build 95f51c
rpm-build 95f51c
struct talloc_reference_handle {
rpm-build 95f51c
	struct talloc_reference_handle *next, *prev;
rpm-build 95f51c
	void *ptr;
rpm-build 95f51c
	const char *location;
rpm-build 95f51c
};
rpm-build 95f51c
rpm-build 95f51c
struct talloc_memlimit {
rpm-build 95f51c
	struct talloc_chunk *parent;
rpm-build 95f51c
	struct talloc_memlimit *upper;
rpm-build 95f51c
	size_t max_size;
rpm-build 95f51c
	size_t cur_size;
rpm-build 95f51c
};
rpm-build 95f51c
rpm-build 95f51c
static inline bool talloc_memlimit_check(struct talloc_memlimit *limit, size_t size);
rpm-build 95f51c
static inline void talloc_memlimit_grow(struct talloc_memlimit *limit,
rpm-build 95f51c
				size_t size);
rpm-build 95f51c
static inline void talloc_memlimit_shrink(struct talloc_memlimit *limit,
rpm-build 95f51c
				size_t size);
rpm-build 95f51c
static inline void tc_memlimit_update_on_free(struct talloc_chunk *tc);
rpm-build 95f51c
rpm-build 95f51c
static inline void _tc_set_name_const(struct talloc_chunk *tc,
rpm-build 95f51c
				const char *name);
rpm-build 95f51c
static struct talloc_chunk *_vasprintf_tc(const void *t,
rpm-build 95f51c
				const char *fmt,
rpm-build 95f51c
				va_list ap);
rpm-build 95f51c
rpm-build 95f51c
typedef int (*talloc_destructor_t)(void *);
rpm-build 95f51c
rpm-build 95f51c
struct talloc_pool_hdr;
rpm-build 95f51c
rpm-build 95f51c
struct talloc_chunk {
rpm-build 95f51c
	/*
rpm-build 95f51c
	 * flags includes the talloc magic, which is randomised to
rpm-build 95f51c
	 * make overwrite attacks harder
rpm-build 95f51c
	 */
rpm-build 95f51c
	unsigned flags;
rpm-build 95f51c
rpm-build 95f51c
	/*
rpm-build 95f51c
	 * If you have a logical tree like:
rpm-build 95f51c
	 *
rpm-build 95f51c
	 *           <parent>
rpm-build 95f51c
	 *           /   |   \
rpm-build 95f51c
	 *          /    |    \
rpm-build 95f51c
	 *         /     |     \
rpm-build 95f51c
	 * <child 1> <child 2> <child 3>
rpm-build 95f51c
	 *
rpm-build 95f51c
	 * The actual talloc tree is:
rpm-build 95f51c
	 *
rpm-build 95f51c
	 *  <parent>
rpm-build 95f51c
	 *     |
rpm-build 95f51c
	 *  <child 1> - <child 2> - <child 3>
rpm-build 95f51c
	 *
rpm-build 95f51c
	 * The children are linked with next/prev pointers, and
rpm-build 95f51c
	 * child 1 is linked to the parent with parent/child
rpm-build 95f51c
	 * pointers.
rpm-build 95f51c
	 */
rpm-build 95f51c
rpm-build 95f51c
	struct talloc_chunk *next, *prev;
rpm-build 95f51c
	struct talloc_chunk *parent, *child;
rpm-build 95f51c
	struct talloc_reference_handle *refs;
rpm-build 95f51c
	talloc_destructor_t destructor;
rpm-build 95f51c
	const char *name;
rpm-build 95f51c
	size_t size;
rpm-build 95f51c
rpm-build 95f51c
	/*
rpm-build 95f51c
	 * limit semantics:
rpm-build 95f51c
	 * if 'limit' is set it means all *new* children of the context will
rpm-build 95f51c
	 * be limited to a total aggregate size ox max_size for memory
rpm-build 95f51c
	 * allocations.
rpm-build 95f51c
	 * cur_size is used to keep track of the current use
rpm-build 95f51c
	 */
rpm-build 95f51c
	struct talloc_memlimit *limit;
rpm-build 95f51c
rpm-build 95f51c
	/*
rpm-build 95f51c
	 * For members of a pool (i.e. TALLOC_FLAG_POOLMEM is set), "pool"
rpm-build 95f51c
	 * is a pointer to the struct talloc_chunk of the pool that it was
rpm-build 95f51c
	 * allocated from. This way children can quickly find the pool to chew
rpm-build 95f51c
	 * from.
rpm-build 95f51c
	 */
rpm-build 95f51c
	struct talloc_pool_hdr *pool;
rpm-build 95f51c
};
rpm-build 95f51c
rpm-build 95f51c
union talloc_chunk_cast_u {
rpm-build 95f51c
	uint8_t *ptr;
rpm-build 95f51c
	struct talloc_chunk *chunk;
rpm-build 95f51c
};
rpm-build 95f51c
rpm-build 95f51c
/* 16 byte alignment seems to keep everyone happy */
rpm-build 95f51c
#define TC_ALIGN16(s) (((s)+15)&~15)
rpm-build 95f51c
#define TC_HDR_SIZE TC_ALIGN16(sizeof(struct talloc_chunk))
rpm-build 95f51c
#define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
rpm-build 95f51c
rpm-build 95f51c
_PUBLIC_ int talloc_version_major(void)
rpm-build 95f51c
{
rpm-build 95f51c
	return TALLOC_VERSION_MAJOR;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
_PUBLIC_ int talloc_version_minor(void)
rpm-build 95f51c
{
rpm-build 95f51c
	return TALLOC_VERSION_MINOR;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
_PUBLIC_ int talloc_test_get_magic(void)
rpm-build 95f51c
{
rpm-build 95f51c
	return talloc_magic;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static inline void _talloc_chunk_set_free(struct talloc_chunk *tc,
rpm-build 95f51c
			      const char *location)
rpm-build 95f51c
{
rpm-build 95f51c
	/*
rpm-build 95f51c
	 * Mark this memory as free, and also over-stamp the talloc
rpm-build 95f51c
	 * magic with the old-style magic.
rpm-build 95f51c
	 *
rpm-build 95f51c
	 * Why?  This tries to avoid a memory read use-after-free from
rpm-build 95f51c
	 * disclosing our talloc magic, which would then allow an
rpm-build 95f51c
	 * attacker to prepare a valid header and so run a destructor.
rpm-build 95f51c
	 *
rpm-build 95f51c
	 */
rpm-build 95f51c
	tc->flags = TALLOC_MAGIC_NON_RANDOM | TALLOC_FLAG_FREE
rpm-build 95f51c
		| (tc->flags & TALLOC_FLAG_MASK);
rpm-build 95f51c
rpm-build 95f51c
	/* we mark the freed memory with where we called the free
rpm-build 95f51c
	 * from. This means on a double free error we can report where
rpm-build 95f51c
	 * the first free came from
rpm-build 95f51c
	 */
rpm-build 95f51c
	if (location) {
rpm-build 95f51c
		tc->name = location;
rpm-build 95f51c
	}
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static inline void _talloc_chunk_set_not_free(struct talloc_chunk *tc)
rpm-build 95f51c
{
rpm-build 95f51c
	/*
rpm-build 95f51c
	 * Mark this memory as not free.
rpm-build 95f51c
	 *
rpm-build 95f51c
	 * Why? This is memory either in a pool (and so available for
rpm-build 95f51c
	 * talloc's re-use or after the realloc().  We need to mark
rpm-build 95f51c
	 * the memory as free() before any realloc() call as we can't
rpm-build 95f51c
	 * write to the memory after that.
rpm-build 95f51c
	 *
rpm-build 95f51c
	 * We put back the normal magic instead of the 'not random'
rpm-build 95f51c
	 * magic.
rpm-build 95f51c
	 */
rpm-build 95f51c
rpm-build 95f51c
	tc->flags = talloc_magic |
rpm-build 95f51c
		((tc->flags & TALLOC_FLAG_MASK) & ~TALLOC_FLAG_FREE);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static void (*talloc_log_fn)(const char *message);
rpm-build 95f51c
rpm-build 95f51c
_PUBLIC_ void talloc_set_log_fn(void (*log_fn)(const char *message))
rpm-build 95f51c
{
rpm-build 95f51c
	talloc_log_fn = log_fn;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
#ifdef HAVE_CONSTRUCTOR_ATTRIBUTE
rpm-build 95f51c
void talloc_lib_init(void) __attribute__((constructor));
rpm-build 95f51c
void talloc_lib_init(void)
rpm-build 95f51c
{
rpm-build 95f51c
	uint32_t random_value;
rpm-build 95f51c
#if defined(HAVE_GETAUXVAL) && defined(AT_RANDOM)
rpm-build 95f51c
	uint8_t *p;
rpm-build 95f51c
	/*
rpm-build 95f51c
	 * Use the kernel-provided random values used for
rpm-build 95f51c
	 * ASLR.  This won't change per-exec, which is ideal for us
rpm-build 95f51c
	 */
rpm-build 95f51c
	p = (uint8_t *) getauxval(AT_RANDOM);
rpm-build 95f51c
	if (p) {
rpm-build 95f51c
		/*
rpm-build 95f51c
		 * We get 16 bytes from getauxval.  By calling rand(),
rpm-build 95f51c
		 * a totally insecure PRNG, but one that will
rpm-build 95f51c
		 * deterministically have a different value when called
rpm-build 95f51c
		 * twice, we ensure that if two talloc-like libraries
rpm-build 95f51c
		 * are somehow loaded in the same address space, that
rpm-build 95f51c
		 * because we choose different bytes, we will keep the
rpm-build 95f51c
		 * protection against collision of multiple talloc
rpm-build 95f51c
		 * libs.
rpm-build 95f51c
		 *
rpm-build 95f51c
		 * This protection is important because the effects of
rpm-build 95f51c
		 * passing a talloc pointer from one to the other may
rpm-build 95f51c
		 * be very hard to determine.
rpm-build 95f51c
		 */
rpm-build 95f51c
		int offset = rand() % (16 - sizeof(random_value));
rpm-build 95f51c
		memcpy(&random_value, p + offset, sizeof(random_value));
rpm-build 95f51c
	} else
rpm-build 95f51c
#endif
rpm-build 95f51c
	{
rpm-build 95f51c
		/*
rpm-build 95f51c
		 * Otherwise, hope the location we are loaded in
rpm-build 95f51c
		 * memory is randomised by someone else
rpm-build 95f51c
		 */
rpm-build 95f51c
		random_value = ((uintptr_t)talloc_lib_init & 0xFFFFFFFF);
rpm-build 95f51c
	}
rpm-build 95f51c
	talloc_magic = random_value & ~TALLOC_FLAG_MASK;
rpm-build 95f51c
}
rpm-build 95f51c
#else
rpm-build 95f51c
#warning "No __attribute__((constructor)) support found on this platform, additional talloc security measures not available"
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
static void talloc_lib_atexit(void)
rpm-build 95f51c
{
rpm-build 95f51c
	TALLOC_FREE(autofree_context);
rpm-build 95f51c
rpm-build 95f51c
	if (talloc_total_size(null_context) == 0) {
rpm-build 95f51c
		return;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (talloc_report_null_full) {
rpm-build 95f51c
		talloc_report_full(null_context, stderr);
rpm-build 95f51c
	} else if (talloc_report_null) {
rpm-build 95f51c
		talloc_report(null_context, stderr);
rpm-build 95f51c
	}
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static void talloc_setup_atexit(void)
rpm-build 95f51c
{
rpm-build 95f51c
	static bool done;
rpm-build 95f51c
rpm-build 95f51c
	if (done) {
rpm-build 95f51c
		return;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	atexit(talloc_lib_atexit);
rpm-build 95f51c
	done = true;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static void talloc_log(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
rpm-build 95f51c
static void talloc_log(const char *fmt, ...)
rpm-build 95f51c
{
rpm-build 95f51c
	va_list ap;
rpm-build 95f51c
	char *message;
rpm-build 95f51c
rpm-build 95f51c
	if (!talloc_log_fn) {
rpm-build 95f51c
		return;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	va_start(ap, fmt);
rpm-build 95f51c
	message = talloc_vasprintf(NULL, fmt, ap);
rpm-build 95f51c
	va_end(ap);
rpm-build 95f51c
rpm-build 95f51c
	talloc_log_fn(message);
rpm-build 95f51c
	talloc_free(message);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static void talloc_log_stderr(const char *message)
rpm-build 95f51c
{
rpm-build 95f51c
	fprintf(stderr, "%s", message);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
_PUBLIC_ void talloc_set_log_stderr(void)
rpm-build 95f51c
{
rpm-build 95f51c
	talloc_set_log_fn(talloc_log_stderr);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static void (*talloc_abort_fn)(const char *reason);
rpm-build 95f51c
rpm-build 95f51c
_PUBLIC_ void talloc_set_abort_fn(void (*abort_fn)(const char *reason))
rpm-build 95f51c
{
rpm-build 95f51c
	talloc_abort_fn = abort_fn;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static void talloc_abort(const char *reason)
rpm-build 95f51c
{
rpm-build 95f51c
	talloc_log("%s\n", reason);
rpm-build 95f51c
rpm-build 95f51c
	if (!talloc_abort_fn) {
rpm-build 95f51c
		TALLOC_ABORT(reason);
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	talloc_abort_fn(reason);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static void talloc_abort_access_after_free(void)
rpm-build 95f51c
{
rpm-build 95f51c
	talloc_abort("Bad talloc magic value - access after free");
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static void talloc_abort_unknown_value(void)
rpm-build 95f51c
{
rpm-build 95f51c
	talloc_abort("Bad talloc magic value - unknown value");
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/* panic if we get a bad magic value */
rpm-build 95f51c
static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
rpm-build 95f51c
{
rpm-build 95f51c
	const char *pp = (const char *)ptr;
rpm-build 95f51c
	struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
rpm-build 95f51c
	if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~TALLOC_FLAG_MASK)) != talloc_magic)) {
rpm-build 95f51c
		if ((tc->flags & (TALLOC_FLAG_FREE | ~TALLOC_FLAG_MASK))
rpm-build 95f51c
		    == (TALLOC_MAGIC_NON_RANDOM | TALLOC_FLAG_FREE)) {
rpm-build 95f51c
			talloc_log("talloc: access after free error - first free may be at %s\n", tc->name);
rpm-build 95f51c
			talloc_abort_access_after_free();
rpm-build 95f51c
			return NULL;
rpm-build 95f51c
		}
rpm-build 95f51c
rpm-build 95f51c
		talloc_abort_unknown_value();
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
	return tc;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/* hook into the front of the list */
rpm-build 95f51c
#define _TLIST_ADD(list, p) \
rpm-build 95f51c
do { \
rpm-build 95f51c
        if (!(list)) { \
rpm-build 95f51c
		(list) = (p); \
rpm-build 95f51c
		(p)->next = (p)->prev = NULL; \
rpm-build 95f51c
	} else { \
rpm-build 95f51c
		(list)->prev = (p); \
rpm-build 95f51c
		(p)->next = (list); \
rpm-build 95f51c
		(p)->prev = NULL; \
rpm-build 95f51c
		(list) = (p); \
rpm-build 95f51c
	}\
rpm-build 95f51c
} while (0)
rpm-build 95f51c
rpm-build 95f51c
/* remove an element from a list - element doesn't have to be in list. */
rpm-build 95f51c
#define _TLIST_REMOVE(list, p) \
rpm-build 95f51c
do { \
rpm-build 95f51c
	if ((p) == (list)) { \
rpm-build 95f51c
		(list) = (p)->next; \
rpm-build 95f51c
		if (list) (list)->prev = NULL; \
rpm-build 95f51c
	} else { \
rpm-build 95f51c
		if ((p)->prev) (p)->prev->next = (p)->next; \
rpm-build 95f51c
		if ((p)->next) (p)->next->prev = (p)->prev; \
rpm-build 95f51c
	} \
rpm-build 95f51c
	if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
rpm-build 95f51c
} while (0)
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  return the parent chunk of a pointer
rpm-build 95f51c
*/
rpm-build 95f51c
static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *tc;
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(ptr == NULL)) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	tc = talloc_chunk_from_ptr(ptr);
rpm-build 95f51c
	while (tc->prev) tc=tc->prev;
rpm-build 95f51c
rpm-build 95f51c
	return tc->parent;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
_PUBLIC_ void *talloc_parent(const void *ptr)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *tc = talloc_parent_chunk(ptr);
rpm-build 95f51c
	return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  find parents name
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ const char *talloc_parent_name(const void *ptr)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *tc = talloc_parent_chunk(ptr);
rpm-build 95f51c
	return tc? tc->name : NULL;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  A pool carries an in-pool object count count in the first 16 bytes.
rpm-build 95f51c
  bytes. This is done to support talloc_steal() to a parent outside of the
rpm-build 95f51c
  pool. The count includes the pool itself, so a talloc_free() on a pool will
rpm-build 95f51c
  only destroy the pool if the count has dropped to zero. A talloc_free() of a
rpm-build 95f51c
  pool member will reduce the count, and eventually also call free(3) on the
rpm-build 95f51c
  pool memory.
rpm-build 95f51c
rpm-build 95f51c
  The object count is not put into "struct talloc_chunk" because it is only
rpm-build 95f51c
  relevant for talloc pools and the alignment to 16 bytes would increase the
rpm-build 95f51c
  memory footprint of each talloc chunk by those 16 bytes.
rpm-build 95f51c
*/
rpm-build 95f51c
rpm-build 95f51c
struct talloc_pool_hdr {
rpm-build 95f51c
	void *end;
rpm-build 95f51c
	unsigned int object_count;
rpm-build 95f51c
	size_t poolsize;
rpm-build 95f51c
};
rpm-build 95f51c
rpm-build 95f51c
union talloc_pool_hdr_cast_u {
rpm-build 95f51c
	uint8_t *ptr;
rpm-build 95f51c
	struct talloc_pool_hdr *hdr;
rpm-build 95f51c
};
rpm-build 95f51c
rpm-build 95f51c
#define TP_HDR_SIZE TC_ALIGN16(sizeof(struct talloc_pool_hdr))
rpm-build 95f51c
rpm-build 95f51c
static inline struct talloc_pool_hdr *talloc_pool_from_chunk(struct talloc_chunk *c)
rpm-build 95f51c
{
rpm-build 95f51c
	union talloc_chunk_cast_u tcc = { .chunk = c };
rpm-build 95f51c
	union talloc_pool_hdr_cast_u tphc = { tcc.ptr - TP_HDR_SIZE };
rpm-build 95f51c
	return tphc.hdr;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static inline struct talloc_chunk *talloc_chunk_from_pool(struct talloc_pool_hdr *h)
rpm-build 95f51c
{
rpm-build 95f51c
	union talloc_pool_hdr_cast_u tphc = { .hdr = h };
rpm-build 95f51c
	union talloc_chunk_cast_u tcc = { .ptr = tphc.ptr + TP_HDR_SIZE };
rpm-build 95f51c
	return tcc.chunk;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static inline void *tc_pool_end(struct talloc_pool_hdr *pool_hdr)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *tc = talloc_chunk_from_pool(pool_hdr);
rpm-build 95f51c
	return (char *)tc + TC_HDR_SIZE + pool_hdr->poolsize;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static inline size_t tc_pool_space_left(struct talloc_pool_hdr *pool_hdr)
rpm-build 95f51c
{
rpm-build 95f51c
	return (char *)tc_pool_end(pool_hdr) - (char *)pool_hdr->end;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/* If tc is inside a pool, this gives the next neighbour. */
rpm-build 95f51c
static inline void *tc_next_chunk(struct talloc_chunk *tc)
rpm-build 95f51c
{
rpm-build 95f51c
	return (char *)tc + TC_ALIGN16(TC_HDR_SIZE + tc->size);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static inline void *tc_pool_first_chunk(struct talloc_pool_hdr *pool_hdr)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *tc = talloc_chunk_from_pool(pool_hdr);
rpm-build 95f51c
	return tc_next_chunk(tc);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/* Mark the whole remaining pool as not accessable */
rpm-build 95f51c
static inline void tc_invalidate_pool(struct talloc_pool_hdr *pool_hdr)
rpm-build 95f51c
{
rpm-build 95f51c
	size_t flen = tc_pool_space_left(pool_hdr);
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(talloc_fill.enabled)) {
rpm-build 95f51c
		memset(pool_hdr->end, talloc_fill.fill_value, flen);
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
#if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
rpm-build 95f51c
	VALGRIND_MAKE_MEM_NOACCESS(pool_hdr->end, flen);
rpm-build 95f51c
#endif
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  Allocate from a pool
rpm-build 95f51c
*/
rpm-build 95f51c
rpm-build 95f51c
static inline struct talloc_chunk *tc_alloc_pool(struct talloc_chunk *parent,
rpm-build 95f51c
						     size_t size, size_t prefix_len)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_pool_hdr *pool_hdr = NULL;
rpm-build 95f51c
	union talloc_chunk_cast_u tcc;
rpm-build 95f51c
	size_t space_left;
rpm-build 95f51c
	struct talloc_chunk *result;
rpm-build 95f51c
	size_t chunk_size;
rpm-build 95f51c
rpm-build 95f51c
	if (parent == NULL) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (parent->flags & TALLOC_FLAG_POOL) {
rpm-build 95f51c
		pool_hdr = talloc_pool_from_chunk(parent);
rpm-build 95f51c
	}
rpm-build 95f51c
	else if (parent->flags & TALLOC_FLAG_POOLMEM) {
rpm-build 95f51c
		pool_hdr = parent->pool;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (pool_hdr == NULL) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	space_left = tc_pool_space_left(pool_hdr);
rpm-build 95f51c
rpm-build 95f51c
	/*
rpm-build 95f51c
	 * Align size to 16 bytes
rpm-build 95f51c
	 */
rpm-build 95f51c
	chunk_size = TC_ALIGN16(size + prefix_len);
rpm-build 95f51c
rpm-build 95f51c
	if (space_left < chunk_size) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	tcc = (union talloc_chunk_cast_u) {
rpm-build 95f51c
		.ptr = ((uint8_t *)pool_hdr->end) + prefix_len
rpm-build 95f51c
	};
rpm-build 95f51c
	result = tcc.chunk;
rpm-build 95f51c
rpm-build 95f51c
#if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
rpm-build 95f51c
	VALGRIND_MAKE_MEM_UNDEFINED(pool_hdr->end, chunk_size);
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
	pool_hdr->end = (void *)((char *)pool_hdr->end + chunk_size);
rpm-build 95f51c
rpm-build 95f51c
	result->flags = talloc_magic | TALLOC_FLAG_POOLMEM;
rpm-build 95f51c
	result->pool = pool_hdr;
rpm-build 95f51c
rpm-build 95f51c
	pool_hdr->object_count++;
rpm-build 95f51c
rpm-build 95f51c
	return result;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
   Allocate a bit of memory as a child of an existing pointer
rpm-build 95f51c
*/
rpm-build 95f51c
static inline void *__talloc_with_prefix(const void *context,
rpm-build 95f51c
					size_t size,
rpm-build 95f51c
					size_t prefix_len,
rpm-build 95f51c
					struct talloc_chunk **tc_ret)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *tc = NULL;
rpm-build 95f51c
	struct talloc_memlimit *limit = NULL;
rpm-build 95f51c
	size_t total_len = TC_HDR_SIZE + size + prefix_len;
rpm-build 95f51c
	struct talloc_chunk *parent = NULL;
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(context == NULL)) {
rpm-build 95f51c
		context = null_context;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(size >= MAX_TALLOC_SIZE)) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(total_len < TC_HDR_SIZE)) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (likely(context != NULL)) {
rpm-build 95f51c
		parent = talloc_chunk_from_ptr(context);
rpm-build 95f51c
rpm-build 95f51c
		if (parent->limit != NULL) {
rpm-build 95f51c
			limit = parent->limit;
rpm-build 95f51c
		}
rpm-build 95f51c
rpm-build 95f51c
		tc = tc_alloc_pool(parent, TC_HDR_SIZE+size, prefix_len);
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (tc == NULL) {
rpm-build 95f51c
		uint8_t *ptr = NULL;
rpm-build 95f51c
		union talloc_chunk_cast_u tcc;
rpm-build 95f51c
rpm-build 95f51c
		/*
rpm-build 95f51c
		 * Only do the memlimit check/update on actual allocation.
rpm-build 95f51c
		 */
rpm-build 95f51c
		if (!talloc_memlimit_check(limit, total_len)) {
rpm-build 95f51c
			errno = ENOMEM;
rpm-build 95f51c
			return NULL;
rpm-build 95f51c
		}
rpm-build 95f51c
rpm-build 95f51c
		ptr = malloc(total_len);
rpm-build 95f51c
		if (unlikely(ptr == NULL)) {
rpm-build 95f51c
			return NULL;
rpm-build 95f51c
		}
rpm-build 95f51c
		tcc = (union talloc_chunk_cast_u) { .ptr = ptr + prefix_len };
rpm-build 95f51c
		tc = tcc.chunk;
rpm-build 95f51c
		tc->flags = talloc_magic;
rpm-build 95f51c
		tc->pool  = NULL;
rpm-build 95f51c
rpm-build 95f51c
		talloc_memlimit_grow(limit, total_len);
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	tc->limit = limit;
rpm-build 95f51c
	tc->size = size;
rpm-build 95f51c
	tc->destructor = NULL;
rpm-build 95f51c
	tc->child = NULL;
rpm-build 95f51c
	tc->name = NULL;
rpm-build 95f51c
	tc->refs = NULL;
rpm-build 95f51c
rpm-build 95f51c
	if (likely(context != NULL)) {
rpm-build 95f51c
		if (parent->child) {
rpm-build 95f51c
			parent->child->parent = NULL;
rpm-build 95f51c
			tc->next = parent->child;
rpm-build 95f51c
			tc->next->prev = tc;
rpm-build 95f51c
		} else {
rpm-build 95f51c
			tc->next = NULL;
rpm-build 95f51c
		}
rpm-build 95f51c
		tc->parent = parent;
rpm-build 95f51c
		tc->prev = NULL;
rpm-build 95f51c
		parent->child = tc;
rpm-build 95f51c
	} else {
rpm-build 95f51c
		tc->next = tc->prev = tc->parent = NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	*tc_ret = tc;
rpm-build 95f51c
	return TC_PTR_FROM_CHUNK(tc);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static inline void *__talloc(const void *context,
rpm-build 95f51c
			size_t size,
rpm-build 95f51c
			struct talloc_chunk **tc)
rpm-build 95f51c
{
rpm-build 95f51c
	return __talloc_with_prefix(context, size, 0, tc);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
 * Create a talloc pool
rpm-build 95f51c
 */
rpm-build 95f51c
rpm-build 95f51c
static inline void *_talloc_pool(const void *context, size_t size)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *tc;
rpm-build 95f51c
	struct talloc_pool_hdr *pool_hdr;
rpm-build 95f51c
	void *result;
rpm-build 95f51c
rpm-build 95f51c
	result = __talloc_with_prefix(context, size, TP_HDR_SIZE, &tc);
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(result == NULL)) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	pool_hdr = talloc_pool_from_chunk(tc);
rpm-build 95f51c
rpm-build 95f51c
	tc->flags |= TALLOC_FLAG_POOL;
rpm-build 95f51c
	tc->size = 0;
rpm-build 95f51c
rpm-build 95f51c
	pool_hdr->object_count = 1;
rpm-build 95f51c
	pool_hdr->end = result;
rpm-build 95f51c
	pool_hdr->poolsize = size;
rpm-build 95f51c
rpm-build 95f51c
	tc_invalidate_pool(pool_hdr);
rpm-build 95f51c
rpm-build 95f51c
	return result;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
_PUBLIC_ void *talloc_pool(const void *context, size_t size)
rpm-build 95f51c
{
rpm-build 95f51c
	return _talloc_pool(context, size);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
 * Create a talloc pool correctly sized for a basic size plus
rpm-build 95f51c
 * a number of subobjects whose total size is given. Essentially
rpm-build 95f51c
 * a custom allocator for talloc to reduce fragmentation.
rpm-build 95f51c
 */
rpm-build 95f51c
rpm-build 95f51c
_PUBLIC_ void *_talloc_pooled_object(const void *ctx,
rpm-build 95f51c
				     size_t type_size,
rpm-build 95f51c
				     const char *type_name,
rpm-build 95f51c
				     unsigned num_subobjects,
rpm-build 95f51c
				     size_t total_subobjects_size)
rpm-build 95f51c
{
rpm-build 95f51c
	size_t poolsize, subobjects_slack, tmp;
rpm-build 95f51c
	struct talloc_chunk *tc;
rpm-build 95f51c
	struct talloc_pool_hdr *pool_hdr;
rpm-build 95f51c
	void *ret;
rpm-build 95f51c
rpm-build 95f51c
	poolsize = type_size + total_subobjects_size;
rpm-build 95f51c
rpm-build 95f51c
	if ((poolsize < type_size) || (poolsize < total_subobjects_size)) {
rpm-build 95f51c
		goto overflow;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (num_subobjects == UINT_MAX) {
rpm-build 95f51c
		goto overflow;
rpm-build 95f51c
	}
rpm-build 95f51c
	num_subobjects += 1;       /* the object body itself */
rpm-build 95f51c
rpm-build 95f51c
	/*
rpm-build 95f51c
	 * Alignment can increase the pool size by at most 15 bytes per object
rpm-build 95f51c
	 * plus alignment for the object itself
rpm-build 95f51c
	 */
rpm-build 95f51c
	subobjects_slack = (TC_HDR_SIZE + TP_HDR_SIZE + 15) * num_subobjects;
rpm-build 95f51c
	if (subobjects_slack < num_subobjects) {
rpm-build 95f51c
		goto overflow;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	tmp = poolsize + subobjects_slack;
rpm-build 95f51c
	if ((tmp < poolsize) || (tmp < subobjects_slack)) {
rpm-build 95f51c
		goto overflow;
rpm-build 95f51c
	}
rpm-build 95f51c
	poolsize = tmp;
rpm-build 95f51c
rpm-build 95f51c
	ret = _talloc_pool(ctx, poolsize);
rpm-build 95f51c
	if (ret == NULL) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	tc = talloc_chunk_from_ptr(ret);
rpm-build 95f51c
	tc->size = type_size;
rpm-build 95f51c
rpm-build 95f51c
	pool_hdr = talloc_pool_from_chunk(tc);
rpm-build 95f51c
rpm-build 95f51c
#if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
rpm-build 95f51c
	VALGRIND_MAKE_MEM_UNDEFINED(pool_hdr->end, type_size);
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
	pool_hdr->end = ((char *)pool_hdr->end + TC_ALIGN16(type_size));
rpm-build 95f51c
rpm-build 95f51c
	_tc_set_name_const(tc, type_name);
rpm-build 95f51c
	return ret;
rpm-build 95f51c
rpm-build 95f51c
overflow:
rpm-build 95f51c
	return NULL;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  setup a destructor to be called on free of a pointer
rpm-build 95f51c
  the destructor should return 0 on success, or -1 on failure.
rpm-build 95f51c
  if the destructor fails then the free is failed, and the memory can
rpm-build 95f51c
  be continued to be used
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
rpm-build 95f51c
	tc->destructor = destructor;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  increase the reference count on a piece of memory.
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ int talloc_increase_ref_count(const void *ptr)
rpm-build 95f51c
{
rpm-build 95f51c
	if (unlikely(!talloc_reference(null_context, ptr))) {
rpm-build 95f51c
		return -1;
rpm-build 95f51c
	}
rpm-build 95f51c
	return 0;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  helper for talloc_reference()
rpm-build 95f51c
rpm-build 95f51c
  this is referenced by a function pointer and should not be inline
rpm-build 95f51c
*/
rpm-build 95f51c
static int talloc_reference_destructor(struct talloc_reference_handle *handle)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
rpm-build 95f51c
	_TLIST_REMOVE(ptr_tc->refs, handle);
rpm-build 95f51c
	return 0;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
   more efficient way to add a name to a pointer - the name must point to a
rpm-build 95f51c
   true string constant
rpm-build 95f51c
*/
rpm-build 95f51c
static inline void _tc_set_name_const(struct talloc_chunk *tc,
rpm-build 95f51c
					const char *name)
rpm-build 95f51c
{
rpm-build 95f51c
	tc->name = name;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  internal talloc_named_const()
rpm-build 95f51c
*/
rpm-build 95f51c
static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
rpm-build 95f51c
{
rpm-build 95f51c
	void *ptr;
rpm-build 95f51c
	struct talloc_chunk *tc;
rpm-build 95f51c
rpm-build 95f51c
	ptr = __talloc(context, size, &tc);
rpm-build 95f51c
	if (unlikely(ptr == NULL)) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	_tc_set_name_const(tc, name);
rpm-build 95f51c
rpm-build 95f51c
	return ptr;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  make a secondary reference to a pointer, hanging off the given context.
rpm-build 95f51c
  the pointer remains valid until both the original caller and this given
rpm-build 95f51c
  context are freed.
rpm-build 95f51c
rpm-build 95f51c
  the major use for this is when two different structures need to reference the
rpm-build 95f51c
  same underlying data, and you want to be able to free the two instances separately,
rpm-build 95f51c
  and in either order
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void *_talloc_reference_loc(const void *context, const void *ptr, const char *location)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *tc;
rpm-build 95f51c
	struct talloc_reference_handle *handle;
rpm-build 95f51c
	if (unlikely(ptr == NULL)) return NULL;
rpm-build 95f51c
rpm-build 95f51c
	tc = talloc_chunk_from_ptr(ptr);
rpm-build 95f51c
	handle = (struct talloc_reference_handle *)_talloc_named_const(context,
rpm-build 95f51c
						   sizeof(struct talloc_reference_handle),
rpm-build 95f51c
						   TALLOC_MAGIC_REFERENCE);
rpm-build 95f51c
	if (unlikely(handle == NULL)) return NULL;
rpm-build 95f51c
rpm-build 95f51c
	/* note that we hang the destructor off the handle, not the
rpm-build 95f51c
	   main context as that allows the caller to still setup their
rpm-build 95f51c
	   own destructor on the context if they want to */
rpm-build 95f51c
	talloc_set_destructor(handle, talloc_reference_destructor);
rpm-build 95f51c
	handle->ptr = discard_const_p(void, ptr);
rpm-build 95f51c
	handle->location = location;
rpm-build 95f51c
	_TLIST_ADD(tc->refs, handle);
rpm-build 95f51c
	return handle->ptr;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static void *_talloc_steal_internal(const void *new_ctx, const void *ptr);
rpm-build 95f51c
rpm-build 95f51c
static inline void _tc_free_poolmem(struct talloc_chunk *tc,
rpm-build 95f51c
					const char *location)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_pool_hdr *pool;
rpm-build 95f51c
	struct talloc_chunk *pool_tc;
rpm-build 95f51c
	void *next_tc;
rpm-build 95f51c
rpm-build 95f51c
	pool = tc->pool;
rpm-build 95f51c
	pool_tc = talloc_chunk_from_pool(pool);
rpm-build 95f51c
	next_tc = tc_next_chunk(tc);
rpm-build 95f51c
rpm-build 95f51c
	_talloc_chunk_set_free(tc, location);
rpm-build 95f51c
rpm-build 95f51c
	TC_INVALIDATE_FULL_CHUNK(tc);
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(pool->object_count == 0)) {
rpm-build 95f51c
		talloc_abort("Pool object count zero!");
rpm-build 95f51c
		return;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	pool->object_count--;
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(pool->object_count == 1
rpm-build 95f51c
		     && !(pool_tc->flags & TALLOC_FLAG_FREE))) {
rpm-build 95f51c
		/*
rpm-build 95f51c
		 * if there is just one object left in the pool
rpm-build 95f51c
		 * and pool->flags does not have TALLOC_FLAG_FREE,
rpm-build 95f51c
		 * it means this is the pool itself and
rpm-build 95f51c
		 * the rest is available for new objects
rpm-build 95f51c
		 * again.
rpm-build 95f51c
		 */
rpm-build 95f51c
		pool->end = tc_pool_first_chunk(pool);
rpm-build 95f51c
		tc_invalidate_pool(pool);
rpm-build 95f51c
		return;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(pool->object_count == 0)) {
rpm-build 95f51c
		/*
rpm-build 95f51c
		 * we mark the freed memory with where we called the free
rpm-build 95f51c
		 * from. This means on a double free error we can report where
rpm-build 95f51c
		 * the first free came from
rpm-build 95f51c
		 */
rpm-build 95f51c
		pool_tc->name = location;
rpm-build 95f51c
rpm-build 95f51c
		if (pool_tc->flags & TALLOC_FLAG_POOLMEM) {
rpm-build 95f51c
			_tc_free_poolmem(pool_tc, location);
rpm-build 95f51c
		} else {
rpm-build 95f51c
			/*
rpm-build 95f51c
			 * The tc_memlimit_update_on_free()
rpm-build 95f51c
			 * call takes into account the
rpm-build 95f51c
			 * prefix TP_HDR_SIZE allocated before
rpm-build 95f51c
			 * the pool talloc_chunk.
rpm-build 95f51c
			 */
rpm-build 95f51c
			tc_memlimit_update_on_free(pool_tc);
rpm-build 95f51c
			TC_INVALIDATE_FULL_CHUNK(pool_tc);
rpm-build 95f51c
			free(pool);
rpm-build 95f51c
		}
rpm-build 95f51c
		return;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (pool->end == next_tc) {
rpm-build 95f51c
		/*
rpm-build 95f51c
		 * if pool->pool still points to end of
rpm-build 95f51c
		 * 'tc' (which is stored in the 'next_tc' variable),
rpm-build 95f51c
		 * we can reclaim the memory of 'tc'.
rpm-build 95f51c
		 */
rpm-build 95f51c
		pool->end = tc;
rpm-build 95f51c
		return;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	/*
rpm-build 95f51c
	 * Do nothing. The memory is just "wasted", waiting for the pool
rpm-build 95f51c
	 * itself to be freed.
rpm-build 95f51c
	 */
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static inline void _tc_free_children_internal(struct talloc_chunk *tc,
rpm-build 95f51c
						  void *ptr,
rpm-build 95f51c
						  const char *location);
rpm-build 95f51c
rpm-build 95f51c
static inline int _talloc_free_internal(void *ptr, const char *location);
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
   internal free call that takes a struct talloc_chunk *.
rpm-build 95f51c
*/
rpm-build 95f51c
static inline int _tc_free_internal(struct talloc_chunk *tc,
rpm-build 95f51c
				const char *location)
rpm-build 95f51c
{
rpm-build 95f51c
	void *ptr_to_free;
rpm-build 95f51c
	void *ptr = TC_PTR_FROM_CHUNK(tc);
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(tc->refs)) {
rpm-build 95f51c
		int is_child;
rpm-build 95f51c
		/* check if this is a reference from a child or
rpm-build 95f51c
		 * grandchild back to it's parent or grandparent
rpm-build 95f51c
		 *
rpm-build 95f51c
		 * in that case we need to remove the reference and
rpm-build 95f51c
		 * call another instance of talloc_free() on the current
rpm-build 95f51c
		 * pointer.
rpm-build 95f51c
		 */
rpm-build 95f51c
		is_child = talloc_is_parent(tc->refs, ptr);
rpm-build 95f51c
		_talloc_free_internal(tc->refs, location);
rpm-build 95f51c
		if (is_child) {
rpm-build 95f51c
			return _talloc_free_internal(ptr, location);
rpm-build 95f51c
		}
rpm-build 95f51c
		return -1;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
rpm-build 95f51c
		/* we have a free loop - stop looping */
rpm-build 95f51c
		return 0;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(tc->destructor)) {
rpm-build 95f51c
		talloc_destructor_t d = tc->destructor;
rpm-build 95f51c
rpm-build 95f51c
		/*
rpm-build 95f51c
		 * Protect the destructor against some overwrite
rpm-build 95f51c
		 * attacks, by explicitly checking it has the right
rpm-build 95f51c
		 * magic here.
rpm-build 95f51c
		 */
rpm-build 95f51c
		if (talloc_chunk_from_ptr(ptr) != tc) {
rpm-build 95f51c
			/*
rpm-build 95f51c
			 * This can't actually happen, the
rpm-build 95f51c
			 * call itself will panic.
rpm-build 95f51c
			 */
rpm-build 95f51c
			TALLOC_ABORT("talloc_chunk_from_ptr failed!");
rpm-build 95f51c
		}
rpm-build 95f51c
rpm-build 95f51c
		if (d == (talloc_destructor_t)-1) {
rpm-build 95f51c
			return -1;
rpm-build 95f51c
		}
rpm-build 95f51c
		tc->destructor = (talloc_destructor_t)-1;
rpm-build 95f51c
		if (d(ptr) == -1) {
rpm-build 95f51c
			/*
rpm-build 95f51c
			 * Only replace the destructor pointer if
rpm-build 95f51c
			 * calling the destructor didn't modify it.
rpm-build 95f51c
			 */
rpm-build 95f51c
			if (tc->destructor == (talloc_destructor_t)-1) {
rpm-build 95f51c
				tc->destructor = d;
rpm-build 95f51c
			}
rpm-build 95f51c
			return -1;
rpm-build 95f51c
		}
rpm-build 95f51c
		tc->destructor = NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (tc->parent) {
rpm-build 95f51c
		_TLIST_REMOVE(tc->parent->child, tc);
rpm-build 95f51c
		if (tc->parent->child) {
rpm-build 95f51c
			tc->parent->child->parent = tc->parent;
rpm-build 95f51c
		}
rpm-build 95f51c
	} else {
rpm-build 95f51c
		if (tc->prev) tc->prev->next = tc->next;
rpm-build 95f51c
		if (tc->next) tc->next->prev = tc->prev;
rpm-build 95f51c
		tc->prev = tc->next = NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	tc->flags |= TALLOC_FLAG_LOOP;
rpm-build 95f51c
rpm-build 95f51c
	_tc_free_children_internal(tc, ptr, location);
rpm-build 95f51c
rpm-build 95f51c
	_talloc_chunk_set_free(tc, location);
rpm-build 95f51c
rpm-build 95f51c
	if (tc->flags & TALLOC_FLAG_POOL) {
rpm-build 95f51c
		struct talloc_pool_hdr *pool;
rpm-build 95f51c
rpm-build 95f51c
		pool = talloc_pool_from_chunk(tc);
rpm-build 95f51c
rpm-build 95f51c
		if (unlikely(pool->object_count == 0)) {
rpm-build 95f51c
			talloc_abort("Pool object count zero!");
rpm-build 95f51c
			return 0;
rpm-build 95f51c
		}
rpm-build 95f51c
rpm-build 95f51c
		pool->object_count--;
rpm-build 95f51c
rpm-build 95f51c
		if (likely(pool->object_count != 0)) {
rpm-build 95f51c
			return 0;
rpm-build 95f51c
		}
rpm-build 95f51c
rpm-build 95f51c
		/*
rpm-build 95f51c
		 * With object_count==0, a pool becomes a normal piece of
rpm-build 95f51c
		 * memory to free. If it's allocated inside a pool, it needs
rpm-build 95f51c
		 * to be freed as poolmem, else it needs to be just freed.
rpm-build 95f51c
		*/
rpm-build 95f51c
		ptr_to_free = pool;
rpm-build 95f51c
	} else {
rpm-build 95f51c
		ptr_to_free = tc;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (tc->flags & TALLOC_FLAG_POOLMEM) {
rpm-build 95f51c
		_tc_free_poolmem(tc, location);
rpm-build 95f51c
		return 0;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	tc_memlimit_update_on_free(tc);
rpm-build 95f51c
rpm-build 95f51c
	TC_INVALIDATE_FULL_CHUNK(tc);
rpm-build 95f51c
	free(ptr_to_free);
rpm-build 95f51c
	return 0;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
   internal talloc_free call
rpm-build 95f51c
*/
rpm-build 95f51c
static inline int _talloc_free_internal(void *ptr, const char *location)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *tc;
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(ptr == NULL)) {
rpm-build 95f51c
		return -1;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	/* possibly initialised the talloc fill value */
rpm-build 95f51c
	if (unlikely(!talloc_fill.initialised)) {
rpm-build 95f51c
		const char *fill = getenv(TALLOC_FILL_ENV);
rpm-build 95f51c
		if (fill != NULL) {
rpm-build 95f51c
			talloc_fill.enabled = true;
rpm-build 95f51c
			talloc_fill.fill_value = strtoul(fill, NULL, 0);
rpm-build 95f51c
		}
rpm-build 95f51c
		talloc_fill.initialised = true;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	tc = talloc_chunk_from_ptr(ptr);
rpm-build 95f51c
	return _tc_free_internal(tc, location);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static inline size_t _talloc_total_limit_size(const void *ptr,
rpm-build 95f51c
					struct talloc_memlimit *old_limit,
rpm-build 95f51c
					struct talloc_memlimit *new_limit);
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
   move a lump of memory from one talloc context to another return the
rpm-build 95f51c
   ptr on success, or NULL if it could not be transferred.
rpm-build 95f51c
   passing NULL as ptr will always return NULL with no side effects.
rpm-build 95f51c
*/
rpm-build 95f51c
static void *_talloc_steal_internal(const void *new_ctx, const void *ptr)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *tc, *new_tc;
rpm-build 95f51c
	size_t ctx_size = 0;
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(!ptr)) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(new_ctx == NULL)) {
rpm-build 95f51c
		new_ctx = null_context;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	tc = talloc_chunk_from_ptr(ptr);
rpm-build 95f51c
rpm-build 95f51c
	if (tc->limit != NULL) {
rpm-build 95f51c
rpm-build 95f51c
		ctx_size = _talloc_total_limit_size(ptr, NULL, NULL);
rpm-build 95f51c
rpm-build 95f51c
		/* Decrement the memory limit from the source .. */
rpm-build 95f51c
		talloc_memlimit_shrink(tc->limit->upper, ctx_size);
rpm-build 95f51c
rpm-build 95f51c
		if (tc->limit->parent == tc) {
rpm-build 95f51c
			tc->limit->upper = NULL;
rpm-build 95f51c
		} else {
rpm-build 95f51c
			tc->limit = NULL;
rpm-build 95f51c
		}
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(new_ctx == NULL)) {
rpm-build 95f51c
		if (tc->parent) {
rpm-build 95f51c
			_TLIST_REMOVE(tc->parent->child, tc);
rpm-build 95f51c
			if (tc->parent->child) {
rpm-build 95f51c
				tc->parent->child->parent = tc->parent;
rpm-build 95f51c
			}
rpm-build 95f51c
		} else {
rpm-build 95f51c
			if (tc->prev) tc->prev->next = tc->next;
rpm-build 95f51c
			if (tc->next) tc->next->prev = tc->prev;
rpm-build 95f51c
		}
rpm-build 95f51c
rpm-build 95f51c
		tc->parent = tc->next = tc->prev = NULL;
rpm-build 95f51c
		return discard_const_p(void, ptr);
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	new_tc = talloc_chunk_from_ptr(new_ctx);
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(tc == new_tc || tc->parent == new_tc)) {
rpm-build 95f51c
		return discard_const_p(void, ptr);
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (tc->parent) {
rpm-build 95f51c
		_TLIST_REMOVE(tc->parent->child, tc);
rpm-build 95f51c
		if (tc->parent->child) {
rpm-build 95f51c
			tc->parent->child->parent = tc->parent;
rpm-build 95f51c
		}
rpm-build 95f51c
	} else {
rpm-build 95f51c
		if (tc->prev) tc->prev->next = tc->next;
rpm-build 95f51c
		if (tc->next) tc->next->prev = tc->prev;
rpm-build 95f51c
		tc->prev = tc->next = NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	tc->parent = new_tc;
rpm-build 95f51c
	if (new_tc->child) new_tc->child->parent = NULL;
rpm-build 95f51c
	_TLIST_ADD(new_tc->child, tc);
rpm-build 95f51c
rpm-build 95f51c
	if (tc->limit || new_tc->limit) {
rpm-build 95f51c
		ctx_size = _talloc_total_limit_size(ptr, tc->limit,
rpm-build 95f51c
						    new_tc->limit);
rpm-build 95f51c
		/* .. and increment it in the destination. */
rpm-build 95f51c
		if (new_tc->limit) {
rpm-build 95f51c
			talloc_memlimit_grow(new_tc->limit, ctx_size);
rpm-build 95f51c
		}
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	return discard_const_p(void, ptr);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
   move a lump of memory from one talloc context to another return the
rpm-build 95f51c
   ptr on success, or NULL if it could not be transferred.
rpm-build 95f51c
   passing NULL as ptr will always return NULL with no side effects.
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *tc;
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(ptr == NULL)) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	tc = talloc_chunk_from_ptr(ptr);
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(tc->refs != NULL) && talloc_parent(ptr) != new_ctx) {
rpm-build 95f51c
		struct talloc_reference_handle *h;
rpm-build 95f51c
rpm-build 95f51c
		talloc_log("WARNING: talloc_steal with references at %s\n",
rpm-build 95f51c
			   location);
rpm-build 95f51c
rpm-build 95f51c
		for (h=tc->refs; h; h=h->next) {
rpm-build 95f51c
			talloc_log("\treference at %s\n",
rpm-build 95f51c
				   h->location);
rpm-build 95f51c
		}
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
#if 0
rpm-build 95f51c
	/* this test is probably too expensive to have on in the
rpm-build 95f51c
	   normal build, but it useful for debugging */
rpm-build 95f51c
	if (talloc_is_parent(new_ctx, ptr)) {
rpm-build 95f51c
		talloc_log("WARNING: stealing into talloc child at %s\n", location);
rpm-build 95f51c
	}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
	return _talloc_steal_internal(new_ctx, ptr);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
   this is like a talloc_steal(), but you must supply the old
rpm-build 95f51c
   parent. This resolves the ambiguity in a talloc_steal() which is
rpm-build 95f51c
   called on a context that has more than one parent (via references)
rpm-build 95f51c
rpm-build 95f51c
   The old parent can be either a reference or a parent
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *tc;
rpm-build 95f51c
	struct talloc_reference_handle *h;
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(ptr == NULL)) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (old_parent == talloc_parent(ptr)) {
rpm-build 95f51c
		return _talloc_steal_internal(new_parent, ptr);
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	tc = talloc_chunk_from_ptr(ptr);
rpm-build 95f51c
	for (h=tc->refs;h;h=h->next) {
rpm-build 95f51c
		if (talloc_parent(h) == old_parent) {
rpm-build 95f51c
			if (_talloc_steal_internal(new_parent, h) != h) {
rpm-build 95f51c
				return NULL;
rpm-build 95f51c
			}
rpm-build 95f51c
			return discard_const_p(void, ptr);
rpm-build 95f51c
		}
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	/* it wasn't a parent */
rpm-build 95f51c
	return NULL;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  remove a secondary reference to a pointer. This undo's what
rpm-build 95f51c
  talloc_reference() has done. The context and pointer arguments
rpm-build 95f51c
  must match those given to a talloc_reference()
rpm-build 95f51c
*/
rpm-build 95f51c
static inline int talloc_unreference(const void *context, const void *ptr)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
rpm-build 95f51c
	struct talloc_reference_handle *h;
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(context == NULL)) {
rpm-build 95f51c
		context = null_context;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	for (h=tc->refs;h;h=h->next) {
rpm-build 95f51c
		struct talloc_chunk *p = talloc_parent_chunk(h);
rpm-build 95f51c
		if (p == NULL) {
rpm-build 95f51c
			if (context == NULL) break;
rpm-build 95f51c
		} else if (TC_PTR_FROM_CHUNK(p) == context) {
rpm-build 95f51c
			break;
rpm-build 95f51c
		}
rpm-build 95f51c
	}
rpm-build 95f51c
	if (h == NULL) {
rpm-build 95f51c
		return -1;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	return _talloc_free_internal(h, __location__);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  remove a specific parent context from a pointer. This is a more
rpm-build 95f51c
  controlled variant of talloc_free()
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ int talloc_unlink(const void *context, void *ptr)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *tc_p, *new_p, *tc_c;
rpm-build 95f51c
	void *new_parent;
rpm-build 95f51c
rpm-build 95f51c
	if (ptr == NULL) {
rpm-build 95f51c
		return -1;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (context == NULL) {
rpm-build 95f51c
		context = null_context;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (talloc_unreference(context, ptr) == 0) {
rpm-build 95f51c
		return 0;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (context != NULL) {
rpm-build 95f51c
		tc_c = talloc_chunk_from_ptr(context);
rpm-build 95f51c
	} else {
rpm-build 95f51c
		tc_c = NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
	if (tc_c != talloc_parent_chunk(ptr)) {
rpm-build 95f51c
		return -1;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	tc_p = talloc_chunk_from_ptr(ptr);
rpm-build 95f51c
rpm-build 95f51c
	if (tc_p->refs == NULL) {
rpm-build 95f51c
		return _talloc_free_internal(ptr, __location__);
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	new_p = talloc_parent_chunk(tc_p->refs);
rpm-build 95f51c
	if (new_p) {
rpm-build 95f51c
		new_parent = TC_PTR_FROM_CHUNK(new_p);
rpm-build 95f51c
	} else {
rpm-build 95f51c
		new_parent = NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (talloc_unreference(new_parent, ptr) != 0) {
rpm-build 95f51c
		return -1;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	_talloc_steal_internal(new_parent, ptr);
rpm-build 95f51c
rpm-build 95f51c
	return 0;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  add a name to an existing pointer - va_list version
rpm-build 95f51c
*/
rpm-build 95f51c
static inline const char *tc_set_name_v(struct talloc_chunk *tc,
rpm-build 95f51c
				const char *fmt,
rpm-build 95f51c
				va_list ap) PRINTF_ATTRIBUTE(2,0);
rpm-build 95f51c
rpm-build 95f51c
static inline const char *tc_set_name_v(struct talloc_chunk *tc,
rpm-build 95f51c
				const char *fmt,
rpm-build 95f51c
				va_list ap)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *name_tc = _vasprintf_tc(TC_PTR_FROM_CHUNK(tc),
rpm-build 95f51c
							fmt,
rpm-build 95f51c
							ap);
rpm-build 95f51c
	if (likely(name_tc)) {
rpm-build 95f51c
		tc->name = TC_PTR_FROM_CHUNK(name_tc);
rpm-build 95f51c
		_tc_set_name_const(name_tc, ".name");
rpm-build 95f51c
	} else {
rpm-build 95f51c
		tc->name = NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
	return tc->name;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  add a name to an existing pointer
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ const char *talloc_set_name(const void *ptr, const char *fmt, ...)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
rpm-build 95f51c
	const char *name;
rpm-build 95f51c
	va_list ap;
rpm-build 95f51c
	va_start(ap, fmt);
rpm-build 95f51c
	name = tc_set_name_v(tc, fmt, ap);
rpm-build 95f51c
	va_end(ap);
rpm-build 95f51c
	return name;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  create a named talloc pointer. Any talloc pointer can be named, and
rpm-build 95f51c
  talloc_named() operates just like talloc() except that it allows you
rpm-build 95f51c
  to name the pointer.
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void *talloc_named(const void *context, size_t size, const char *fmt, ...)
rpm-build 95f51c
{
rpm-build 95f51c
	va_list ap;
rpm-build 95f51c
	void *ptr;
rpm-build 95f51c
	const char *name;
rpm-build 95f51c
	struct talloc_chunk *tc;
rpm-build 95f51c
rpm-build 95f51c
	ptr = __talloc(context, size, &tc);
rpm-build 95f51c
	if (unlikely(ptr == NULL)) return NULL;
rpm-build 95f51c
rpm-build 95f51c
	va_start(ap, fmt);
rpm-build 95f51c
	name = tc_set_name_v(tc, fmt, ap);
rpm-build 95f51c
	va_end(ap);
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(name == NULL)) {
rpm-build 95f51c
		_talloc_free_internal(ptr, __location__);
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	return ptr;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  return the name of a talloc ptr, or "UNNAMED"
rpm-build 95f51c
*/
rpm-build 95f51c
static inline const char *__talloc_get_name(const void *ptr)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
rpm-build 95f51c
	if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
rpm-build 95f51c
		return ".reference";
rpm-build 95f51c
	}
rpm-build 95f51c
	if (likely(tc->name)) {
rpm-build 95f51c
		return tc->name;
rpm-build 95f51c
	}
rpm-build 95f51c
	return "UNNAMED";
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
_PUBLIC_ const char *talloc_get_name(const void *ptr)
rpm-build 95f51c
{
rpm-build 95f51c
	return __talloc_get_name(ptr);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  check if a pointer has the given name. If it does, return the pointer,
rpm-build 95f51c
  otherwise return NULL
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void *talloc_check_name(const void *ptr, const char *name)
rpm-build 95f51c
{
rpm-build 95f51c
	const char *pname;
rpm-build 95f51c
	if (unlikely(ptr == NULL)) return NULL;
rpm-build 95f51c
	pname = __talloc_get_name(ptr);
rpm-build 95f51c
	if (likely(pname == name || strcmp(pname, name) == 0)) {
rpm-build 95f51c
		return discard_const_p(void, ptr);
rpm-build 95f51c
	}
rpm-build 95f51c
	return NULL;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static void talloc_abort_type_mismatch(const char *location,
rpm-build 95f51c
					const char *name,
rpm-build 95f51c
					const char *expected)
rpm-build 95f51c
{
rpm-build 95f51c
	const char *reason;
rpm-build 95f51c
rpm-build 95f51c
	reason = talloc_asprintf(NULL,
rpm-build 95f51c
				 "%s: Type mismatch: name[%s] expected[%s]",
rpm-build 95f51c
				 location,
rpm-build 95f51c
				 name?name:"NULL",
rpm-build 95f51c
				 expected);
rpm-build 95f51c
	if (!reason) {
rpm-build 95f51c
		reason = "Type mismatch";
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	talloc_abort(reason);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
_PUBLIC_ void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location)
rpm-build 95f51c
{
rpm-build 95f51c
	const char *pname;
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(ptr == NULL)) {
rpm-build 95f51c
		talloc_abort_type_mismatch(location, NULL, name);
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	pname = __talloc_get_name(ptr);
rpm-build 95f51c
	if (likely(pname == name || strcmp(pname, name) == 0)) {
rpm-build 95f51c
		return discard_const_p(void, ptr);
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	talloc_abort_type_mismatch(location, pname, name);
rpm-build 95f51c
	return NULL;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  this is for compatibility with older versions of talloc
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void *talloc_init(const char *fmt, ...)
rpm-build 95f51c
{
rpm-build 95f51c
	va_list ap;
rpm-build 95f51c
	void *ptr;
rpm-build 95f51c
	const char *name;
rpm-build 95f51c
	struct talloc_chunk *tc;
rpm-build 95f51c
rpm-build 95f51c
	ptr = __talloc(NULL, 0, &tc);
rpm-build 95f51c
	if (unlikely(ptr == NULL)) return NULL;
rpm-build 95f51c
rpm-build 95f51c
	va_start(ap, fmt);
rpm-build 95f51c
	name = tc_set_name_v(tc, fmt, ap);
rpm-build 95f51c
	va_end(ap);
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(name == NULL)) {
rpm-build 95f51c
		_talloc_free_internal(ptr, __location__);
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	return ptr;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static inline void _tc_free_children_internal(struct talloc_chunk *tc,
rpm-build 95f51c
						  void *ptr,
rpm-build 95f51c
						  const char *location)
rpm-build 95f51c
{
rpm-build 95f51c
	while (tc->child) {
rpm-build 95f51c
		/* we need to work out who will own an abandoned child
rpm-build 95f51c
		   if it cannot be freed. In priority order, the first
rpm-build 95f51c
		   choice is owner of any remaining reference to this
rpm-build 95f51c
		   pointer, the second choice is our parent, and the
rpm-build 95f51c
		   final choice is the null context. */
rpm-build 95f51c
		void *child = TC_PTR_FROM_CHUNK(tc->child);
rpm-build 95f51c
		const void *new_parent = null_context;
rpm-build 95f51c
		if (unlikely(tc->child->refs)) {
rpm-build 95f51c
			struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
rpm-build 95f51c
			if (p) new_parent = TC_PTR_FROM_CHUNK(p);
rpm-build 95f51c
		}
rpm-build 95f51c
		if (unlikely(_tc_free_internal(tc->child, location) == -1)) {
rpm-build 95f51c
			if (talloc_parent_chunk(child) != tc) {
rpm-build 95f51c
				/*
rpm-build 95f51c
				 * Destructor already reparented this child.
rpm-build 95f51c
				 * No further reparenting needed.
rpm-build 95f51c
				 */
rpm-build 95f51c
				continue;
rpm-build 95f51c
			}
rpm-build 95f51c
			if (new_parent == null_context) {
rpm-build 95f51c
				struct talloc_chunk *p = talloc_parent_chunk(ptr);
rpm-build 95f51c
				if (p) new_parent = TC_PTR_FROM_CHUNK(p);
rpm-build 95f51c
			}
rpm-build 95f51c
			_talloc_steal_internal(new_parent, child);
rpm-build 95f51c
		}
rpm-build 95f51c
	}
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  this is a replacement for the Samba3 talloc_destroy_pool functionality. It
rpm-build 95f51c
  should probably not be used in new code. It's in here to keep the talloc
rpm-build 95f51c
  code consistent across Samba 3 and 4.
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void talloc_free_children(void *ptr)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *tc_name = NULL;
rpm-build 95f51c
	struct talloc_chunk *tc;
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(ptr == NULL)) {
rpm-build 95f51c
		return;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	tc = talloc_chunk_from_ptr(ptr);
rpm-build 95f51c
rpm-build 95f51c
	/* we do not want to free the context name if it is a child .. */
rpm-build 95f51c
	if (likely(tc->child)) {
rpm-build 95f51c
		for (tc_name = tc->child; tc_name; tc_name = tc_name->next) {
rpm-build 95f51c
			if (tc->name == TC_PTR_FROM_CHUNK(tc_name)) break;
rpm-build 95f51c
		}
rpm-build 95f51c
		if (tc_name) {
rpm-build 95f51c
			_TLIST_REMOVE(tc->child, tc_name);
rpm-build 95f51c
			if (tc->child) {
rpm-build 95f51c
				tc->child->parent = tc;
rpm-build 95f51c
			}
rpm-build 95f51c
		}
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	_tc_free_children_internal(tc, ptr, __location__);
rpm-build 95f51c
rpm-build 95f51c
	/* .. so we put it back after all other children have been freed */
rpm-build 95f51c
	if (tc_name) {
rpm-build 95f51c
		if (tc->child) {
rpm-build 95f51c
			tc->child->parent = NULL;
rpm-build 95f51c
		}
rpm-build 95f51c
		tc_name->parent = tc;
rpm-build 95f51c
		_TLIST_ADD(tc->child, tc_name);
rpm-build 95f51c
	}
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
   Allocate a bit of memory as a child of an existing pointer
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void *_talloc(const void *context, size_t size)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *tc;
rpm-build 95f51c
	return __talloc(context, size, &tc);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  externally callable talloc_set_name_const()
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void talloc_set_name_const(const void *ptr, const char *name)
rpm-build 95f51c
{
rpm-build 95f51c
	_tc_set_name_const(talloc_chunk_from_ptr(ptr), name);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  create a named talloc pointer. Any talloc pointer can be named, and
rpm-build 95f51c
  talloc_named() operates just like talloc() except that it allows you
rpm-build 95f51c
  to name the pointer.
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void *talloc_named_const(const void *context, size_t size, const char *name)
rpm-build 95f51c
{
rpm-build 95f51c
	return _talloc_named_const(context, size, name);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
   free a talloc pointer. This also frees all child pointers of this
rpm-build 95f51c
   pointer recursively
rpm-build 95f51c
rpm-build 95f51c
   return 0 if the memory is actually freed, otherwise -1. The memory
rpm-build 95f51c
   will not be freed if the ref_count is > 1 or the destructor (if
rpm-build 95f51c
   any) returns non-zero
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ int _talloc_free(void *ptr, const char *location)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *tc;
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(ptr == NULL)) {
rpm-build 95f51c
		return -1;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	tc = talloc_chunk_from_ptr(ptr);
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(tc->refs != NULL)) {
rpm-build 95f51c
		struct talloc_reference_handle *h;
rpm-build 95f51c
rpm-build 95f51c
		if (talloc_parent(ptr) == null_context && tc->refs->next == NULL) {
rpm-build 95f51c
			/* in this case we do know which parent should
rpm-build 95f51c
			   get this pointer, as there is really only
rpm-build 95f51c
			   one parent */
rpm-build 95f51c
			return talloc_unlink(null_context, ptr);
rpm-build 95f51c
		}
rpm-build 95f51c
rpm-build 95f51c
		talloc_log("ERROR: talloc_free with references at %s\n",
rpm-build 95f51c
			   location);
rpm-build 95f51c
rpm-build 95f51c
		for (h=tc->refs; h; h=h->next) {
rpm-build 95f51c
			talloc_log("\treference at %s\n",
rpm-build 95f51c
				   h->location);
rpm-build 95f51c
		}
rpm-build 95f51c
		return -1;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	return _talloc_free_internal(ptr, location);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  A talloc version of realloc. The context argument is only used if
rpm-build 95f51c
  ptr is NULL
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *tc;
rpm-build 95f51c
	void *new_ptr;
rpm-build 95f51c
	bool malloced = false;
rpm-build 95f51c
	struct talloc_pool_hdr *pool_hdr = NULL;
rpm-build 95f51c
	size_t old_size = 0;
rpm-build 95f51c
	size_t new_size = 0;
rpm-build 95f51c
rpm-build 95f51c
	/* size zero is equivalent to free() */
rpm-build 95f51c
	if (unlikely(size == 0)) {
rpm-build 95f51c
		talloc_unlink(context, ptr);
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(size >= MAX_TALLOC_SIZE)) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	/* realloc(NULL) is equivalent to malloc() */
rpm-build 95f51c
	if (ptr == NULL) {
rpm-build 95f51c
		return _talloc_named_const(context, size, name);
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	tc = talloc_chunk_from_ptr(ptr);
rpm-build 95f51c
rpm-build 95f51c
	/* don't allow realloc on referenced pointers */
rpm-build 95f51c
	if (unlikely(tc->refs)) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	/* don't let anybody try to realloc a talloc_pool */
rpm-build 95f51c
	if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (tc->limit && (size > tc->size)) {
rpm-build 95f51c
		if (!talloc_memlimit_check(tc->limit, (size - tc->size))) {
rpm-build 95f51c
			errno = ENOMEM;
rpm-build 95f51c
			return NULL;
rpm-build 95f51c
		}
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	/* handle realloc inside a talloc_pool */
rpm-build 95f51c
	if (unlikely(tc->flags & TALLOC_FLAG_POOLMEM)) {
rpm-build 95f51c
		pool_hdr = tc->pool;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
#if (ALWAYS_REALLOC == 0)
rpm-build 95f51c
	/* don't shrink if we have less than 1k to gain */
rpm-build 95f51c
	if (size < tc->size && tc->limit == NULL) {
rpm-build 95f51c
		if (pool_hdr) {
rpm-build 95f51c
			void *next_tc = tc_next_chunk(tc);
rpm-build 95f51c
			TC_INVALIDATE_SHRINK_CHUNK(tc, size);
rpm-build 95f51c
			tc->size = size;
rpm-build 95f51c
			if (next_tc == pool_hdr->end) {
rpm-build 95f51c
				/* note: tc->size has changed, so this works */
rpm-build 95f51c
				pool_hdr->end = tc_next_chunk(tc);
rpm-build 95f51c
			}
rpm-build 95f51c
			return ptr;
rpm-build 95f51c
		} else if ((tc->size - size) < 1024) {
rpm-build 95f51c
			/*
rpm-build 95f51c
			 * if we call TC_INVALIDATE_SHRINK_CHUNK() here
rpm-build 95f51c
			 * we would need to call TC_UNDEFINE_GROW_CHUNK()
rpm-build 95f51c
			 * after each realloc call, which slows down
rpm-build 95f51c
			 * testing a lot :-(.
rpm-build 95f51c
			 *
rpm-build 95f51c
			 * That is why we only mark memory as undefined here.
rpm-build 95f51c
			 */
rpm-build 95f51c
			TC_UNDEFINE_SHRINK_CHUNK(tc, size);
rpm-build 95f51c
rpm-build 95f51c
			/* do not shrink if we have less than 1k to gain */
rpm-build 95f51c
			tc->size = size;
rpm-build 95f51c
			return ptr;
rpm-build 95f51c
		}
rpm-build 95f51c
	} else if (tc->size == size) {
rpm-build 95f51c
		/*
rpm-build 95f51c
		 * do not change the pointer if it is exactly
rpm-build 95f51c
		 * the same size.
rpm-build 95f51c
		 */
rpm-build 95f51c
		return ptr;
rpm-build 95f51c
	}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
	/*
rpm-build 95f51c
	 * by resetting magic we catch users of the old memory
rpm-build 95f51c
	 *
rpm-build 95f51c
	 * We mark this memory as free, and also over-stamp the talloc
rpm-build 95f51c
	 * magic with the old-style magic.
rpm-build 95f51c
	 *
rpm-build 95f51c
	 * Why?  This tries to avoid a memory read use-after-free from
rpm-build 95f51c
	 * disclosing our talloc magic, which would then allow an
rpm-build 95f51c
	 * attacker to prepare a valid header and so run a destructor.
rpm-build 95f51c
	 *
rpm-build 95f51c
	 * What else?  We have to re-stamp back a valid normal magic
rpm-build 95f51c
	 * on this memory once realloc() is done, as it will have done
rpm-build 95f51c
	 * a memcpy() into the new valid memory.  We can't do this in
rpm-build 95f51c
	 * reverse as that would be a real use-after-free.
rpm-build 95f51c
	 */
rpm-build 95f51c
	_talloc_chunk_set_free(tc, NULL);
rpm-build 95f51c
rpm-build 95f51c
#if ALWAYS_REALLOC
rpm-build 95f51c
	if (pool_hdr) {
rpm-build 95f51c
		new_ptr = tc_alloc_pool(tc, size + TC_HDR_SIZE, 0);
rpm-build 95f51c
		pool_hdr->object_count--;
rpm-build 95f51c
rpm-build 95f51c
		if (new_ptr == NULL) {
rpm-build 95f51c
			new_ptr = malloc(TC_HDR_SIZE+size);
rpm-build 95f51c
			malloced = true;
rpm-build 95f51c
			new_size = size;
rpm-build 95f51c
		}
rpm-build 95f51c
rpm-build 95f51c
		if (new_ptr) {
rpm-build 95f51c
			memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
rpm-build 95f51c
			TC_INVALIDATE_FULL_CHUNK(tc);
rpm-build 95f51c
		}
rpm-build 95f51c
	} else {
rpm-build 95f51c
		/* We're doing malloc then free here, so record the difference. */
rpm-build 95f51c
		old_size = tc->size;
rpm-build 95f51c
		new_size = size;
rpm-build 95f51c
		new_ptr = malloc(size + TC_HDR_SIZE);
rpm-build 95f51c
		if (new_ptr) {
rpm-build 95f51c
			memcpy(new_ptr, tc, MIN(tc->size, size) + TC_HDR_SIZE);
rpm-build 95f51c
			free(tc);
rpm-build 95f51c
		}
rpm-build 95f51c
	}
rpm-build 95f51c
#else
rpm-build 95f51c
	if (pool_hdr) {
rpm-build 95f51c
		struct talloc_chunk *pool_tc;
rpm-build 95f51c
		void *next_tc = tc_next_chunk(tc);
rpm-build 95f51c
		size_t old_chunk_size = TC_ALIGN16(TC_HDR_SIZE + tc->size);
rpm-build 95f51c
		size_t new_chunk_size = TC_ALIGN16(TC_HDR_SIZE + size);
rpm-build 95f51c
		size_t space_needed;
rpm-build 95f51c
		size_t space_left;
rpm-build 95f51c
		unsigned int chunk_count = pool_hdr->object_count;
rpm-build 95f51c
rpm-build 95f51c
		pool_tc = talloc_chunk_from_pool(pool_hdr);
rpm-build 95f51c
		if (!(pool_tc->flags & TALLOC_FLAG_FREE)) {
rpm-build 95f51c
			chunk_count -= 1;
rpm-build 95f51c
		}
rpm-build 95f51c
rpm-build 95f51c
		if (chunk_count == 1) {
rpm-build 95f51c
			/*
rpm-build 95f51c
			 * optimize for the case where 'tc' is the only
rpm-build 95f51c
			 * chunk in the pool.
rpm-build 95f51c
			 */
rpm-build 95f51c
			char *start = tc_pool_first_chunk(pool_hdr);
rpm-build 95f51c
			space_needed = new_chunk_size;
rpm-build 95f51c
			space_left = (char *)tc_pool_end(pool_hdr) - start;
rpm-build 95f51c
rpm-build 95f51c
			if (space_left >= space_needed) {
rpm-build 95f51c
				size_t old_used = TC_HDR_SIZE + tc->size;
rpm-build 95f51c
				size_t new_used = TC_HDR_SIZE + size;
rpm-build 95f51c
				new_ptr = start;
rpm-build 95f51c
rpm-build 95f51c
#if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
rpm-build 95f51c
				{
rpm-build 95f51c
					/*
rpm-build 95f51c
					 * The area from
rpm-build 95f51c
					 * start -> tc may have
rpm-build 95f51c
					 * been freed and thus been marked as
rpm-build 95f51c
					 * VALGRIND_MEM_NOACCESS. Set it to
rpm-build 95f51c
					 * VALGRIND_MEM_UNDEFINED so we can
rpm-build 95f51c
					 * copy into it without valgrind errors.
rpm-build 95f51c
					 * We can't just mark
rpm-build 95f51c
					 * new_ptr -> new_ptr + old_used
rpm-build 95f51c
					 * as this may overlap on top of tc,
rpm-build 95f51c
					 * (which is why we use memmove, not
rpm-build 95f51c
					 * memcpy below) hence the MIN.
rpm-build 95f51c
					 */
rpm-build 95f51c
					size_t undef_len = MIN((((char *)tc) - ((char *)new_ptr)),old_used);
rpm-build 95f51c
					VALGRIND_MAKE_MEM_UNDEFINED(new_ptr, undef_len);
rpm-build 95f51c
				}
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
				memmove(new_ptr, tc, old_used);
rpm-build 95f51c
rpm-build 95f51c
				tc = (struct talloc_chunk *)new_ptr;
rpm-build 95f51c
				TC_UNDEFINE_GROW_CHUNK(tc, size);
rpm-build 95f51c
rpm-build 95f51c
				/*
rpm-build 95f51c
				 * first we do not align the pool pointer
rpm-build 95f51c
				 * because we want to invalidate the padding
rpm-build 95f51c
				 * too.
rpm-build 95f51c
				 */
rpm-build 95f51c
				pool_hdr->end = new_used + (char *)new_ptr;
rpm-build 95f51c
				tc_invalidate_pool(pool_hdr);
rpm-build 95f51c
rpm-build 95f51c
				/* now the aligned pointer */
rpm-build 95f51c
				pool_hdr->end = new_chunk_size + (char *)new_ptr;
rpm-build 95f51c
				goto got_new_ptr;
rpm-build 95f51c
			}
rpm-build 95f51c
rpm-build 95f51c
			next_tc = NULL;
rpm-build 95f51c
		}
rpm-build 95f51c
rpm-build 95f51c
		if (new_chunk_size == old_chunk_size) {
rpm-build 95f51c
			TC_UNDEFINE_GROW_CHUNK(tc, size);
rpm-build 95f51c
			_talloc_chunk_set_not_free(tc);
rpm-build 95f51c
			tc->size = size;
rpm-build 95f51c
			return ptr;
rpm-build 95f51c
		}
rpm-build 95f51c
rpm-build 95f51c
		if (next_tc == pool_hdr->end) {
rpm-build 95f51c
			/*
rpm-build 95f51c
			 * optimize for the case where 'tc' is the last
rpm-build 95f51c
			 * chunk in the pool.
rpm-build 95f51c
			 */
rpm-build 95f51c
			space_needed = new_chunk_size - old_chunk_size;
rpm-build 95f51c
			space_left = tc_pool_space_left(pool_hdr);
rpm-build 95f51c
rpm-build 95f51c
			if (space_left >= space_needed) {
rpm-build 95f51c
				TC_UNDEFINE_GROW_CHUNK(tc, size);
rpm-build 95f51c
				_talloc_chunk_set_not_free(tc);
rpm-build 95f51c
				tc->size = size;
rpm-build 95f51c
				pool_hdr->end = tc_next_chunk(tc);
rpm-build 95f51c
				return ptr;
rpm-build 95f51c
			}
rpm-build 95f51c
		}
rpm-build 95f51c
rpm-build 95f51c
		new_ptr = tc_alloc_pool(tc, size + TC_HDR_SIZE, 0);
rpm-build 95f51c
rpm-build 95f51c
		if (new_ptr == NULL) {
rpm-build 95f51c
			new_ptr = malloc(TC_HDR_SIZE+size);
rpm-build 95f51c
			malloced = true;
rpm-build 95f51c
			new_size = size;
rpm-build 95f51c
		}
rpm-build 95f51c
rpm-build 95f51c
		if (new_ptr) {
rpm-build 95f51c
			memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
rpm-build 95f51c
rpm-build 95f51c
			_tc_free_poolmem(tc, __location__ "_talloc_realloc");
rpm-build 95f51c
		}
rpm-build 95f51c
	}
rpm-build 95f51c
	else {
rpm-build 95f51c
		/* We're doing realloc here, so record the difference. */
rpm-build 95f51c
		old_size = tc->size;
rpm-build 95f51c
		new_size = size;
rpm-build 95f51c
		new_ptr = realloc(tc, size + TC_HDR_SIZE);
rpm-build 95f51c
	}
rpm-build 95f51c
got_new_ptr:
rpm-build 95f51c
#endif
rpm-build 95f51c
	if (unlikely(!new_ptr)) {
rpm-build 95f51c
		/*
rpm-build 95f51c
		 * Ok, this is a strange spot.  We have to put back
rpm-build 95f51c
		 * the old talloc_magic and any flags, except the
rpm-build 95f51c
		 * TALLOC_FLAG_FREE as this was not free'ed by the
rpm-build 95f51c
		 * realloc() call after all
rpm-build 95f51c
		 */
rpm-build 95f51c
		_talloc_chunk_set_not_free(tc);
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	/*
rpm-build 95f51c
	 * tc is now the new value from realloc(), the old memory we
rpm-build 95f51c
	 * can't access any more and was preemptively marked as
rpm-build 95f51c
	 * TALLOC_FLAG_FREE before the call.  Now we mark it as not
rpm-build 95f51c
	 * free again
rpm-build 95f51c
	 */
rpm-build 95f51c
	tc = (struct talloc_chunk *)new_ptr;
rpm-build 95f51c
	_talloc_chunk_set_not_free(tc);
rpm-build 95f51c
	if (malloced) {
rpm-build 95f51c
		tc->flags &= ~TALLOC_FLAG_POOLMEM;
rpm-build 95f51c
	}
rpm-build 95f51c
	if (tc->parent) {
rpm-build 95f51c
		tc->parent->child = tc;
rpm-build 95f51c
	}
rpm-build 95f51c
	if (tc->child) {
rpm-build 95f51c
		tc->child->parent = tc;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (tc->prev) {
rpm-build 95f51c
		tc->prev->next = tc;
rpm-build 95f51c
	}
rpm-build 95f51c
	if (tc->next) {
rpm-build 95f51c
		tc->next->prev = tc;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (new_size > old_size) {
rpm-build 95f51c
		talloc_memlimit_grow(tc->limit, new_size - old_size);
rpm-build 95f51c
	} else if (new_size < old_size) {
rpm-build 95f51c
		talloc_memlimit_shrink(tc->limit, old_size - new_size);
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	tc->size = size;
rpm-build 95f51c
	_tc_set_name_const(tc, name);
rpm-build 95f51c
rpm-build 95f51c
	return TC_PTR_FROM_CHUNK(tc);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  a wrapper around talloc_steal() for situations where you are moving a pointer
rpm-build 95f51c
  between two structures, and want the old pointer to be set to NULL
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void *_talloc_move(const void *new_ctx, const void *_pptr)
rpm-build 95f51c
{
rpm-build 95f51c
	const void **pptr = discard_const_p(const void *,_pptr);
rpm-build 95f51c
	void *ret = talloc_steal(new_ctx, discard_const_p(void, *pptr));
rpm-build 95f51c
	(*pptr) = NULL;
rpm-build 95f51c
	return ret;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
enum talloc_mem_count_type {
rpm-build 95f51c
	TOTAL_MEM_SIZE,
rpm-build 95f51c
	TOTAL_MEM_BLOCKS,
rpm-build 95f51c
	TOTAL_MEM_LIMIT,
rpm-build 95f51c
};
rpm-build 95f51c
rpm-build 95f51c
static inline size_t _talloc_total_mem_internal(const void *ptr,
rpm-build 95f51c
					 enum talloc_mem_count_type type,
rpm-build 95f51c
					 struct talloc_memlimit *old_limit,
rpm-build 95f51c
					 struct talloc_memlimit *new_limit)
rpm-build 95f51c
{
rpm-build 95f51c
	size_t total = 0;
rpm-build 95f51c
	struct talloc_chunk *c, *tc;
rpm-build 95f51c
rpm-build 95f51c
	if (ptr == NULL) {
rpm-build 95f51c
		ptr = null_context;
rpm-build 95f51c
	}
rpm-build 95f51c
	if (ptr == NULL) {
rpm-build 95f51c
		return 0;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	tc = talloc_chunk_from_ptr(ptr);
rpm-build 95f51c
rpm-build 95f51c
	if (old_limit || new_limit) {
rpm-build 95f51c
		if (tc->limit && tc->limit->upper == old_limit) {
rpm-build 95f51c
			tc->limit->upper = new_limit;
rpm-build 95f51c
		}
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	/* optimize in the memlimits case */
rpm-build 95f51c
	if (type == TOTAL_MEM_LIMIT &&
rpm-build 95f51c
	    tc->limit != NULL &&
rpm-build 95f51c
	    tc->limit != old_limit &&
rpm-build 95f51c
	    tc->limit->parent == tc) {
rpm-build 95f51c
		return tc->limit->cur_size;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (tc->flags & TALLOC_FLAG_LOOP) {
rpm-build 95f51c
		return 0;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	tc->flags |= TALLOC_FLAG_LOOP;
rpm-build 95f51c
rpm-build 95f51c
	if (old_limit || new_limit) {
rpm-build 95f51c
		if (old_limit == tc->limit) {
rpm-build 95f51c
			tc->limit = new_limit;
rpm-build 95f51c
		}
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	switch (type) {
rpm-build 95f51c
	case TOTAL_MEM_SIZE:
rpm-build 95f51c
		if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
rpm-build 95f51c
			total = tc->size;
rpm-build 95f51c
		}
rpm-build 95f51c
		break;
rpm-build 95f51c
	case TOTAL_MEM_BLOCKS:
rpm-build 95f51c
		total++;
rpm-build 95f51c
		break;
rpm-build 95f51c
	case TOTAL_MEM_LIMIT:
rpm-build 95f51c
		if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
rpm-build 95f51c
			/*
rpm-build 95f51c
			 * Don't count memory allocated from a pool
rpm-build 95f51c
			 * when calculating limits. Only count the
rpm-build 95f51c
			 * pool itself.
rpm-build 95f51c
			 */
rpm-build 95f51c
			if (!(tc->flags & TALLOC_FLAG_POOLMEM)) {
rpm-build 95f51c
				if (tc->flags & TALLOC_FLAG_POOL) {
rpm-build 95f51c
					/*
rpm-build 95f51c
					 * If this is a pool, the allocated
rpm-build 95f51c
					 * size is in the pool header, and
rpm-build 95f51c
					 * remember to add in the prefix
rpm-build 95f51c
					 * length.
rpm-build 95f51c
					 */
rpm-build 95f51c
					struct talloc_pool_hdr *pool_hdr
rpm-build 95f51c
							= talloc_pool_from_chunk(tc);
rpm-build 95f51c
					total = pool_hdr->poolsize +
rpm-build 95f51c
							TC_HDR_SIZE +
rpm-build 95f51c
							TP_HDR_SIZE;
rpm-build 95f51c
				} else {
rpm-build 95f51c
					total = tc->size + TC_HDR_SIZE;
rpm-build 95f51c
				}
rpm-build 95f51c
			}
rpm-build 95f51c
		}
rpm-build 95f51c
		break;
rpm-build 95f51c
	}
rpm-build 95f51c
	for (c = tc->child; c; c = c->next) {
rpm-build 95f51c
		total += _talloc_total_mem_internal(TC_PTR_FROM_CHUNK(c), type,
rpm-build 95f51c
						    old_limit, new_limit);
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	tc->flags &= ~TALLOC_FLAG_LOOP;
rpm-build 95f51c
rpm-build 95f51c
	return total;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  return the total size of a talloc pool (subtree)
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ size_t talloc_total_size(const void *ptr)
rpm-build 95f51c
{
rpm-build 95f51c
	return _talloc_total_mem_internal(ptr, TOTAL_MEM_SIZE, NULL, NULL);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  return the total number of blocks in a talloc pool (subtree)
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ size_t talloc_total_blocks(const void *ptr)
rpm-build 95f51c
{
rpm-build 95f51c
	return _talloc_total_mem_internal(ptr, TOTAL_MEM_BLOCKS, NULL, NULL);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  return the number of external references to a pointer
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ size_t talloc_reference_count(const void *ptr)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
rpm-build 95f51c
	struct talloc_reference_handle *h;
rpm-build 95f51c
	size_t ret = 0;
rpm-build 95f51c
rpm-build 95f51c
	for (h=tc->refs;h;h=h->next) {
rpm-build 95f51c
		ret++;
rpm-build 95f51c
	}
rpm-build 95f51c
	return ret;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  report on memory usage by all children of a pointer, giving a full tree view
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
rpm-build 95f51c
			    void (*callback)(const void *ptr,
rpm-build 95f51c
			  		     int depth, int max_depth,
rpm-build 95f51c
					     int is_ref,
rpm-build 95f51c
					     void *private_data),
rpm-build 95f51c
			    void *private_data)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *c, *tc;
rpm-build 95f51c
rpm-build 95f51c
	if (ptr == NULL) {
rpm-build 95f51c
		ptr = null_context;
rpm-build 95f51c
	}
rpm-build 95f51c
	if (ptr == NULL) return;
rpm-build 95f51c
rpm-build 95f51c
	tc = talloc_chunk_from_ptr(ptr);
rpm-build 95f51c
rpm-build 95f51c
	if (tc->flags & TALLOC_FLAG_LOOP) {
rpm-build 95f51c
		return;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	callback(ptr, depth, max_depth, 0, private_data);
rpm-build 95f51c
rpm-build 95f51c
	if (max_depth >= 0 && depth >= max_depth) {
rpm-build 95f51c
		return;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	tc->flags |= TALLOC_FLAG_LOOP;
rpm-build 95f51c
	for (c=tc->child;c;c=c->next) {
rpm-build 95f51c
		if (c->name == TALLOC_MAGIC_REFERENCE) {
rpm-build 95f51c
			struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
rpm-build 95f51c
			callback(h->ptr, depth + 1, max_depth, 1, private_data);
rpm-build 95f51c
		} else {
rpm-build 95f51c
			talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
rpm-build 95f51c
		}
rpm-build 95f51c
	}
rpm-build 95f51c
	tc->flags &= ~TALLOC_FLAG_LOOP;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
rpm-build 95f51c
{
rpm-build 95f51c
	const char *name = __talloc_get_name(ptr);
rpm-build 95f51c
	struct talloc_chunk *tc;
rpm-build 95f51c
	FILE *f = (FILE *)_f;
rpm-build 95f51c
rpm-build 95f51c
	if (is_ref) {
rpm-build 95f51c
		fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
rpm-build 95f51c
		return;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	tc = talloc_chunk_from_ptr(ptr);
rpm-build 95f51c
	if (tc->limit && tc->limit->parent == tc) {
rpm-build 95f51c
		fprintf(f, "%*s%-30s is a memlimit context"
rpm-build 95f51c
			" (max_size = %lu bytes, cur_size = %lu bytes)\n",
rpm-build 95f51c
			depth*4, "",
rpm-build 95f51c
			name,
rpm-build 95f51c
			(unsigned long)tc->limit->max_size,
rpm-build 95f51c
			(unsigned long)tc->limit->cur_size);
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (depth == 0) {
rpm-build 95f51c
		fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
rpm-build 95f51c
			(max_depth < 0 ? "full " :""), name,
rpm-build 95f51c
			(unsigned long)talloc_total_size(ptr),
rpm-build 95f51c
			(unsigned long)talloc_total_blocks(ptr));
rpm-build 95f51c
		return;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
rpm-build 95f51c
		depth*4, "",
rpm-build 95f51c
		name,
rpm-build 95f51c
		(unsigned long)talloc_total_size(ptr),
rpm-build 95f51c
		(unsigned long)talloc_total_blocks(ptr),
rpm-build 95f51c
		(int)talloc_reference_count(ptr), ptr);
rpm-build 95f51c
rpm-build 95f51c
#if 0
rpm-build 95f51c
	fprintf(f, "content: ");
rpm-build 95f51c
	if (talloc_total_size(ptr)) {
rpm-build 95f51c
		int tot = talloc_total_size(ptr);
rpm-build 95f51c
		int i;
rpm-build 95f51c
rpm-build 95f51c
		for (i = 0; i < tot; i++) {
rpm-build 95f51c
			if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
rpm-build 95f51c
				fprintf(f, "%c", ((char *)ptr)[i]);
rpm-build 95f51c
			} else {
rpm-build 95f51c
				fprintf(f, "~%02x", ((char *)ptr)[i]);
rpm-build 95f51c
			}
rpm-build 95f51c
		}
rpm-build 95f51c
	}
rpm-build 95f51c
	fprintf(f, "\n");
rpm-build 95f51c
#endif
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  report on memory usage by all children of a pointer, giving a full tree view
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
rpm-build 95f51c
{
rpm-build 95f51c
	if (f) {
rpm-build 95f51c
		talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
rpm-build 95f51c
		fflush(f);
rpm-build 95f51c
	}
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  report on memory usage by all children of a pointer, giving a full tree view
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void talloc_report_full(const void *ptr, FILE *f)
rpm-build 95f51c
{
rpm-build 95f51c
	talloc_report_depth_file(ptr, 0, -1, f);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  report on memory usage by all children of a pointer
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void talloc_report(const void *ptr, FILE *f)
rpm-build 95f51c
{
rpm-build 95f51c
	talloc_report_depth_file(ptr, 0, 1, f);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  enable tracking of the NULL context
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void talloc_enable_null_tracking(void)
rpm-build 95f51c
{
rpm-build 95f51c
	if (null_context == NULL) {
rpm-build 95f51c
		null_context = _talloc_named_const(NULL, 0, "null_context");
rpm-build 95f51c
		if (autofree_context != NULL) {
rpm-build 95f51c
			talloc_reparent(NULL, null_context, autofree_context);
rpm-build 95f51c
		}
rpm-build 95f51c
	}
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  enable tracking of the NULL context, not moving the autofree context
rpm-build 95f51c
  into the NULL context. This is needed for the talloc testsuite
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void talloc_enable_null_tracking_no_autofree(void)
rpm-build 95f51c
{
rpm-build 95f51c
	if (null_context == NULL) {
rpm-build 95f51c
		null_context = _talloc_named_const(NULL, 0, "null_context");
rpm-build 95f51c
	}
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  disable tracking of the NULL context
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void talloc_disable_null_tracking(void)
rpm-build 95f51c
{
rpm-build 95f51c
	if (null_context != NULL) {
rpm-build 95f51c
		/* we have to move any children onto the real NULL
rpm-build 95f51c
		   context */
rpm-build 95f51c
		struct talloc_chunk *tc, *tc2;
rpm-build 95f51c
		tc = talloc_chunk_from_ptr(null_context);
rpm-build 95f51c
		for (tc2 = tc->child; tc2; tc2=tc2->next) {
rpm-build 95f51c
			if (tc2->parent == tc) tc2->parent = NULL;
rpm-build 95f51c
			if (tc2->prev == tc) tc2->prev = NULL;
rpm-build 95f51c
		}
rpm-build 95f51c
		for (tc2 = tc->next; tc2; tc2=tc2->next) {
rpm-build 95f51c
			if (tc2->parent == tc) tc2->parent = NULL;
rpm-build 95f51c
			if (tc2->prev == tc) tc2->prev = NULL;
rpm-build 95f51c
		}
rpm-build 95f51c
		tc->child = NULL;
rpm-build 95f51c
		tc->next = NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
	talloc_free(null_context);
rpm-build 95f51c
	null_context = NULL;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  enable leak reporting on exit
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void talloc_enable_leak_report(void)
rpm-build 95f51c
{
rpm-build 95f51c
	talloc_enable_null_tracking();
rpm-build 95f51c
	talloc_report_null = true;
rpm-build 95f51c
	talloc_setup_atexit();
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  enable full leak reporting on exit
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void talloc_enable_leak_report_full(void)
rpm-build 95f51c
{
rpm-build 95f51c
	talloc_enable_null_tracking();
rpm-build 95f51c
	talloc_report_null_full = true;
rpm-build 95f51c
	talloc_setup_atexit();
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
   talloc and zero memory.
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void *_talloc_zero(const void *ctx, size_t size, const char *name)
rpm-build 95f51c
{
rpm-build 95f51c
	void *p = _talloc_named_const(ctx, size, name);
rpm-build 95f51c
rpm-build 95f51c
	if (p) {
rpm-build 95f51c
		memset(p, '\0', size);
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	return p;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  memdup with a talloc.
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
rpm-build 95f51c
{
rpm-build 95f51c
	void *newp = NULL;
rpm-build 95f51c
rpm-build 95f51c
	if (likely(size > 0) && unlikely(p == NULL)) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	newp = _talloc_named_const(t, size, name);
rpm-build 95f51c
	if (likely(newp != NULL) && likely(size > 0)) {
rpm-build 95f51c
		memcpy(newp, p, size);
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	return newp;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
rpm-build 95f51c
{
rpm-build 95f51c
	char *ret;
rpm-build 95f51c
	struct talloc_chunk *tc;
rpm-build 95f51c
rpm-build 95f51c
	ret = (char *)__talloc(t, len + 1, &tc);
rpm-build 95f51c
	if (unlikely(!ret)) return NULL;
rpm-build 95f51c
rpm-build 95f51c
	memcpy(ret, p, len);
rpm-build 95f51c
	ret[len] = 0;
rpm-build 95f51c
rpm-build 95f51c
	_tc_set_name_const(tc, ret);
rpm-build 95f51c
	return ret;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  strdup with a talloc
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ char *talloc_strdup(const void *t, const char *p)
rpm-build 95f51c
{
rpm-build 95f51c
	if (unlikely(!p)) return NULL;
rpm-build 95f51c
	return __talloc_strlendup(t, p, strlen(p));
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  strndup with a talloc
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ char *talloc_strndup(const void *t, const char *p, size_t n)
rpm-build 95f51c
{
rpm-build 95f51c
	if (unlikely(!p)) return NULL;
rpm-build 95f51c
	return __talloc_strlendup(t, p, strnlen(p, n));
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static inline char *__talloc_strlendup_append(char *s, size_t slen,
rpm-build 95f51c
					      const char *a, size_t alen)
rpm-build 95f51c
{
rpm-build 95f51c
	char *ret;
rpm-build 95f51c
rpm-build 95f51c
	ret = talloc_realloc(NULL, s, char, slen + alen + 1);
rpm-build 95f51c
	if (unlikely(!ret)) return NULL;
rpm-build 95f51c
rpm-build 95f51c
	/* append the string and the trailing \0 */
rpm-build 95f51c
	memcpy(&ret[slen], a, alen);
rpm-build 95f51c
	ret[slen+alen] = 0;
rpm-build 95f51c
rpm-build 95f51c
	_tc_set_name_const(talloc_chunk_from_ptr(ret), ret);
rpm-build 95f51c
	return ret;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
 * Appends at the end of the string.
rpm-build 95f51c
 */
rpm-build 95f51c
_PUBLIC_ char *talloc_strdup_append(char *s, const char *a)
rpm-build 95f51c
{
rpm-build 95f51c
	if (unlikely(!s)) {
rpm-build 95f51c
		return talloc_strdup(NULL, a);
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(!a)) {
rpm-build 95f51c
		return s;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
 * Appends at the end of the talloc'ed buffer,
rpm-build 95f51c
 * not the end of the string.
rpm-build 95f51c
 */
rpm-build 95f51c
_PUBLIC_ char *talloc_strdup_append_buffer(char *s, const char *a)
rpm-build 95f51c
{
rpm-build 95f51c
	size_t slen;
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(!s)) {
rpm-build 95f51c
		return talloc_strdup(NULL, a);
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(!a)) {
rpm-build 95f51c
		return s;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	slen = talloc_get_size(s);
rpm-build 95f51c
	if (likely(slen > 0)) {
rpm-build 95f51c
		slen--;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	return __talloc_strlendup_append(s, slen, a, strlen(a));
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
 * Appends at the end of the string.
rpm-build 95f51c
 */
rpm-build 95f51c
_PUBLIC_ char *talloc_strndup_append(char *s, const char *a, size_t n)
rpm-build 95f51c
{
rpm-build 95f51c
	if (unlikely(!s)) {
rpm-build 95f51c
		return talloc_strndup(NULL, a, n);
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(!a)) {
rpm-build 95f51c
		return s;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
 * Appends at the end of the talloc'ed buffer,
rpm-build 95f51c
 * not the end of the string.
rpm-build 95f51c
 */
rpm-build 95f51c
_PUBLIC_ char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
rpm-build 95f51c
{
rpm-build 95f51c
	size_t slen;
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(!s)) {
rpm-build 95f51c
		return talloc_strndup(NULL, a, n);
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(!a)) {
rpm-build 95f51c
		return s;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	slen = talloc_get_size(s);
rpm-build 95f51c
	if (likely(slen > 0)) {
rpm-build 95f51c
		slen--;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
#ifndef HAVE_VA_COPY
rpm-build 95f51c
#ifdef HAVE___VA_COPY
rpm-build 95f51c
#define va_copy(dest, src) __va_copy(dest, src)
rpm-build 95f51c
#else
rpm-build 95f51c
#define va_copy(dest, src) (dest) = (src)
rpm-build 95f51c
#endif
rpm-build 95f51c
#endif
rpm-build 95f51c
rpm-build 95f51c
static struct talloc_chunk *_vasprintf_tc(const void *t,
rpm-build 95f51c
					  const char *fmt,
rpm-build 95f51c
					  va_list ap) PRINTF_ATTRIBUTE(2,0);
rpm-build 95f51c
rpm-build 95f51c
static struct talloc_chunk *_vasprintf_tc(const void *t,
rpm-build 95f51c
					  const char *fmt,
rpm-build 95f51c
					  va_list ap)
rpm-build 95f51c
{
rpm-build 95f51c
	int vlen;
rpm-build 95f51c
	size_t len;
rpm-build 95f51c
	char *ret;
rpm-build 95f51c
	va_list ap2;
rpm-build 95f51c
	struct talloc_chunk *tc;
rpm-build 95f51c
	char buf[1024];
rpm-build 95f51c
rpm-build 95f51c
	/* this call looks strange, but it makes it work on older solaris boxes */
rpm-build 95f51c
	va_copy(ap2, ap);
rpm-build 95f51c
	vlen = vsnprintf(buf, sizeof(buf), fmt, ap2);
rpm-build 95f51c
	va_end(ap2);
rpm-build 95f51c
	if (unlikely(vlen < 0)) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
	len = vlen;
rpm-build 95f51c
	if (unlikely(len + 1 < len)) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	ret = (char *)__talloc(t, len+1, &tc);
rpm-build 95f51c
	if (unlikely(!ret)) return NULL;
rpm-build 95f51c
rpm-build 95f51c
	if (len < sizeof(buf)) {
rpm-build 95f51c
		memcpy(ret, buf, len+1);
rpm-build 95f51c
	} else {
rpm-build 95f51c
		va_copy(ap2, ap);
rpm-build 95f51c
		vsnprintf(ret, len+1, fmt, ap2);
rpm-build 95f51c
		va_end(ap2);
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	_tc_set_name_const(tc, ret);
rpm-build 95f51c
	return tc;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
_PUBLIC_ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *tc = _vasprintf_tc(t, fmt, ap);
rpm-build 95f51c
	if (tc == NULL) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
	return TC_PTR_FROM_CHUNK(tc);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  Perform string formatting, and return a pointer to newly allocated
rpm-build 95f51c
  memory holding the result, inside a memory pool.
rpm-build 95f51c
 */
rpm-build 95f51c
_PUBLIC_ char *talloc_asprintf(const void *t, const char *fmt, ...)
rpm-build 95f51c
{
rpm-build 95f51c
	va_list ap;
rpm-build 95f51c
	char *ret;
rpm-build 95f51c
rpm-build 95f51c
	va_start(ap, fmt);
rpm-build 95f51c
	ret = talloc_vasprintf(t, fmt, ap);
rpm-build 95f51c
	va_end(ap);
rpm-build 95f51c
	return ret;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
rpm-build 95f51c
						 const char *fmt, va_list ap)
rpm-build 95f51c
						 PRINTF_ATTRIBUTE(3,0);
rpm-build 95f51c
rpm-build 95f51c
static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
rpm-build 95f51c
						 const char *fmt, va_list ap)
rpm-build 95f51c
{
rpm-build 95f51c
	ssize_t alen;
rpm-build 95f51c
	va_list ap2;
rpm-build 95f51c
	char c;
rpm-build 95f51c
rpm-build 95f51c
	va_copy(ap2, ap);
rpm-build 95f51c
	alen = vsnprintf(&c, 1, fmt, ap2);
rpm-build 95f51c
	va_end(ap2);
rpm-build 95f51c
rpm-build 95f51c
	if (alen <= 0) {
rpm-build 95f51c
		/* Either the vsnprintf failed or the format resulted in
rpm-build 95f51c
		 * no characters being formatted. In the former case, we
rpm-build 95f51c
		 * ought to return NULL, in the latter we ought to return
rpm-build 95f51c
		 * the original string. Most current callers of this
rpm-build 95f51c
		 * function expect it to never return NULL.
rpm-build 95f51c
		 */
rpm-build 95f51c
		return s;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	s = talloc_realloc(NULL, s, char, slen + alen + 1);
rpm-build 95f51c
	if (!s) return NULL;
rpm-build 95f51c
rpm-build 95f51c
	va_copy(ap2, ap);
rpm-build 95f51c
	vsnprintf(s + slen, alen + 1, fmt, ap2);
rpm-build 95f51c
	va_end(ap2);
rpm-build 95f51c
rpm-build 95f51c
	_tc_set_name_const(talloc_chunk_from_ptr(s), s);
rpm-build 95f51c
	return s;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/**
rpm-build 95f51c
 * Realloc @p s to append the formatted result of @p fmt and @p ap,
rpm-build 95f51c
 * and return @p s, which may have moved.  Good for gradually
rpm-build 95f51c
 * accumulating output into a string buffer. Appends at the end
rpm-build 95f51c
 * of the string.
rpm-build 95f51c
 **/
rpm-build 95f51c
_PUBLIC_ char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
rpm-build 95f51c
{
rpm-build 95f51c
	if (unlikely(!s)) {
rpm-build 95f51c
		return talloc_vasprintf(NULL, fmt, ap);
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/**
rpm-build 95f51c
 * Realloc @p s to append the formatted result of @p fmt and @p ap,
rpm-build 95f51c
 * and return @p s, which may have moved. Always appends at the
rpm-build 95f51c
 * end of the talloc'ed buffer, not the end of the string.
rpm-build 95f51c
 **/
rpm-build 95f51c
_PUBLIC_ char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
rpm-build 95f51c
{
rpm-build 95f51c
	size_t slen;
rpm-build 95f51c
rpm-build 95f51c
	if (unlikely(!s)) {
rpm-build 95f51c
		return talloc_vasprintf(NULL, fmt, ap);
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	slen = talloc_get_size(s);
rpm-build 95f51c
	if (likely(slen > 0)) {
rpm-build 95f51c
		slen--;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	return __talloc_vaslenprintf_append(s, slen, fmt, ap);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  Realloc @p s to append the formatted result of @p fmt and return @p
rpm-build 95f51c
  s, which may have moved.  Good for gradually accumulating output
rpm-build 95f51c
  into a string buffer.
rpm-build 95f51c
 */
rpm-build 95f51c
_PUBLIC_ char *talloc_asprintf_append(char *s, const char *fmt, ...)
rpm-build 95f51c
{
rpm-build 95f51c
	va_list ap;
rpm-build 95f51c
rpm-build 95f51c
	va_start(ap, fmt);
rpm-build 95f51c
	s = talloc_vasprintf_append(s, fmt, ap);
rpm-build 95f51c
	va_end(ap);
rpm-build 95f51c
	return s;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  Realloc @p s to append the formatted result of @p fmt and return @p
rpm-build 95f51c
  s, which may have moved.  Good for gradually accumulating output
rpm-build 95f51c
  into a buffer.
rpm-build 95f51c
 */
rpm-build 95f51c
_PUBLIC_ char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
rpm-build 95f51c
{
rpm-build 95f51c
	va_list ap;
rpm-build 95f51c
rpm-build 95f51c
	va_start(ap, fmt);
rpm-build 95f51c
	s = talloc_vasprintf_append_buffer(s, fmt, ap);
rpm-build 95f51c
	va_end(ap);
rpm-build 95f51c
	return s;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  alloc an array, checking for integer overflow in the array size
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
rpm-build 95f51c
{
rpm-build 95f51c
	if (count >= MAX_TALLOC_SIZE/el_size) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
	return _talloc_named_const(ctx, el_size * count, name);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  alloc an zero array, checking for integer overflow in the array size
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
rpm-build 95f51c
{
rpm-build 95f51c
	if (count >= MAX_TALLOC_SIZE/el_size) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
	return _talloc_zero(ctx, el_size * count, name);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  realloc an array, checking for integer overflow in the array size
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
rpm-build 95f51c
{
rpm-build 95f51c
	if (count >= MAX_TALLOC_SIZE/el_size) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
	return _talloc_realloc(ctx, ptr, el_size * count, name);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  a function version of talloc_realloc(), so it can be passed as a function pointer
rpm-build 95f51c
  to libraries that want a realloc function (a realloc function encapsulates
rpm-build 95f51c
  all the basic capabilities of an allocation library, which is why this is useful)
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
rpm-build 95f51c
{
rpm-build 95f51c
	return _talloc_realloc(context, ptr, size, NULL);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
static int talloc_autofree_destructor(void *ptr)
rpm-build 95f51c
{
rpm-build 95f51c
	autofree_context = NULL;
rpm-build 95f51c
	return 0;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  return a context which will be auto-freed on exit
rpm-build 95f51c
  this is useful for reducing the noise in leak reports
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void *talloc_autofree_context(void)
rpm-build 95f51c
{
rpm-build 95f51c
	if (autofree_context == NULL) {
rpm-build 95f51c
		autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
rpm-build 95f51c
		talloc_set_destructor(autofree_context, talloc_autofree_destructor);
rpm-build 95f51c
		talloc_setup_atexit();
rpm-build 95f51c
	}
rpm-build 95f51c
	return autofree_context;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
_PUBLIC_ size_t talloc_get_size(const void *context)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *tc;
rpm-build 95f51c
rpm-build 95f51c
	if (context == NULL) {
rpm-build 95f51c
		return 0;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	tc = talloc_chunk_from_ptr(context);
rpm-build 95f51c
rpm-build 95f51c
	return tc->size;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  find a parent of this context that has the given name, if any
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void *talloc_find_parent_byname(const void *context, const char *name)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *tc;
rpm-build 95f51c
rpm-build 95f51c
	if (context == NULL) {
rpm-build 95f51c
		return NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	tc = talloc_chunk_from_ptr(context);
rpm-build 95f51c
	while (tc) {
rpm-build 95f51c
		if (tc->name && strcmp(tc->name, name) == 0) {
rpm-build 95f51c
			return TC_PTR_FROM_CHUNK(tc);
rpm-build 95f51c
		}
rpm-build 95f51c
		while (tc && tc->prev) tc = tc->prev;
rpm-build 95f51c
		if (tc) {
rpm-build 95f51c
			tc = tc->parent;
rpm-build 95f51c
		}
rpm-build 95f51c
	}
rpm-build 95f51c
	return NULL;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  show the parentage of a context
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ void talloc_show_parents(const void *context, FILE *file)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *tc;
rpm-build 95f51c
rpm-build 95f51c
	if (context == NULL) {
rpm-build 95f51c
		fprintf(file, "talloc no parents for NULL\n");
rpm-build 95f51c
		return;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	tc = talloc_chunk_from_ptr(context);
rpm-build 95f51c
	fprintf(file, "talloc parents of '%s'\n", __talloc_get_name(context));
rpm-build 95f51c
	while (tc) {
rpm-build 95f51c
		fprintf(file, "\t'%s'\n", __talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
rpm-build 95f51c
		while (tc && tc->prev) tc = tc->prev;
rpm-build 95f51c
		if (tc) {
rpm-build 95f51c
			tc = tc->parent;
rpm-build 95f51c
		}
rpm-build 95f51c
	}
rpm-build 95f51c
	fflush(file);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  return 1 if ptr is a parent of context
rpm-build 95f51c
*/
rpm-build 95f51c
static int _talloc_is_parent(const void *context, const void *ptr, int depth)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *tc;
rpm-build 95f51c
rpm-build 95f51c
	if (context == NULL) {
rpm-build 95f51c
		return 0;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	tc = talloc_chunk_from_ptr(context);
rpm-build 95f51c
	while (tc) {
rpm-build 95f51c
		if (depth <= 0) {
rpm-build 95f51c
			return 0;
rpm-build 95f51c
		}
rpm-build 95f51c
		if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
rpm-build 95f51c
		while (tc && tc->prev) tc = tc->prev;
rpm-build 95f51c
		if (tc) {
rpm-build 95f51c
			tc = tc->parent;
rpm-build 95f51c
			depth--;
rpm-build 95f51c
		}
rpm-build 95f51c
	}
rpm-build 95f51c
	return 0;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  return 1 if ptr is a parent of context
rpm-build 95f51c
*/
rpm-build 95f51c
_PUBLIC_ int talloc_is_parent(const void *context, const void *ptr)
rpm-build 95f51c
{
rpm-build 95f51c
	return _talloc_is_parent(context, ptr, TALLOC_MAX_DEPTH);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  return the total size of memory used by this context and all children
rpm-build 95f51c
*/
rpm-build 95f51c
static inline size_t _talloc_total_limit_size(const void *ptr,
rpm-build 95f51c
					struct talloc_memlimit *old_limit,
rpm-build 95f51c
					struct talloc_memlimit *new_limit)
rpm-build 95f51c
{
rpm-build 95f51c
	return _talloc_total_mem_internal(ptr, TOTAL_MEM_LIMIT,
rpm-build 95f51c
					  old_limit, new_limit);
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
static inline bool talloc_memlimit_check(struct talloc_memlimit *limit, size_t size)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_memlimit *l;
rpm-build 95f51c
rpm-build 95f51c
	for (l = limit; l != NULL; l = l->upper) {
rpm-build 95f51c
		if (l->max_size != 0 &&
rpm-build 95f51c
		    ((l->max_size <= l->cur_size) ||
rpm-build 95f51c
		     (l->max_size - l->cur_size < size))) {
rpm-build 95f51c
			return false;
rpm-build 95f51c
		}
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	return true;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  Update memory limits when freeing a talloc_chunk.
rpm-build 95f51c
*/
rpm-build 95f51c
static void tc_memlimit_update_on_free(struct talloc_chunk *tc)
rpm-build 95f51c
{
rpm-build 95f51c
	size_t limit_shrink_size;
rpm-build 95f51c
rpm-build 95f51c
	if (!tc->limit) {
rpm-build 95f51c
		return;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	/*
rpm-build 95f51c
	 * Pool entries don't count. Only the pools
rpm-build 95f51c
	 * themselves are counted as part of the memory
rpm-build 95f51c
	 * limits. Note that this also takes care of
rpm-build 95f51c
	 * nested pools which have both flags
rpm-build 95f51c
	 * TALLOC_FLAG_POOLMEM|TALLOC_FLAG_POOL set.
rpm-build 95f51c
	 */
rpm-build 95f51c
	if (tc->flags & TALLOC_FLAG_POOLMEM) {
rpm-build 95f51c
		return;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	/*
rpm-build 95f51c
	 * If we are part of a memory limited context hierarchy
rpm-build 95f51c
	 * we need to subtract the memory used from the counters
rpm-build 95f51c
	 */
rpm-build 95f51c
rpm-build 95f51c
	limit_shrink_size = tc->size+TC_HDR_SIZE;
rpm-build 95f51c
rpm-build 95f51c
	/*
rpm-build 95f51c
	 * If we're deallocating a pool, take into
rpm-build 95f51c
	 * account the prefix size added for the pool.
rpm-build 95f51c
	 */
rpm-build 95f51c
rpm-build 95f51c
	if (tc->flags & TALLOC_FLAG_POOL) {
rpm-build 95f51c
		limit_shrink_size += TP_HDR_SIZE;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	talloc_memlimit_shrink(tc->limit, limit_shrink_size);
rpm-build 95f51c
rpm-build 95f51c
	if (tc->limit->parent == tc) {
rpm-build 95f51c
		free(tc->limit);
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	tc->limit = NULL;
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  Increase memory limit accounting after a malloc/realloc.
rpm-build 95f51c
*/
rpm-build 95f51c
static void talloc_memlimit_grow(struct talloc_memlimit *limit,
rpm-build 95f51c
				size_t size)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_memlimit *l;
rpm-build 95f51c
rpm-build 95f51c
	for (l = limit; l != NULL; l = l->upper) {
rpm-build 95f51c
		size_t new_cur_size = l->cur_size + size;
rpm-build 95f51c
		if (new_cur_size < l->cur_size) {
rpm-build 95f51c
			talloc_abort("logic error in talloc_memlimit_grow\n");
rpm-build 95f51c
			return;
rpm-build 95f51c
		}
rpm-build 95f51c
		l->cur_size = new_cur_size;
rpm-build 95f51c
	}
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
/*
rpm-build 95f51c
  Decrease memory limit accounting after a free/realloc.
rpm-build 95f51c
*/
rpm-build 95f51c
static void talloc_memlimit_shrink(struct talloc_memlimit *limit,
rpm-build 95f51c
				size_t size)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_memlimit *l;
rpm-build 95f51c
rpm-build 95f51c
	for (l = limit; l != NULL; l = l->upper) {
rpm-build 95f51c
		if (l->cur_size < size) {
rpm-build 95f51c
			talloc_abort("logic error in talloc_memlimit_shrink\n");
rpm-build 95f51c
			return;
rpm-build 95f51c
		}
rpm-build 95f51c
		l->cur_size = l->cur_size - size;
rpm-build 95f51c
	}
rpm-build 95f51c
}
rpm-build 95f51c
rpm-build 95f51c
_PUBLIC_ int talloc_set_memlimit(const void *ctx, size_t max_size)
rpm-build 95f51c
{
rpm-build 95f51c
	struct talloc_chunk *tc = talloc_chunk_from_ptr(ctx);
rpm-build 95f51c
	struct talloc_memlimit *orig_limit;
rpm-build 95f51c
	struct talloc_memlimit *limit = NULL;
rpm-build 95f51c
rpm-build 95f51c
	if (tc->limit && tc->limit->parent == tc) {
rpm-build 95f51c
		tc->limit->max_size = max_size;
rpm-build 95f51c
		return 0;
rpm-build 95f51c
	}
rpm-build 95f51c
	orig_limit = tc->limit;
rpm-build 95f51c
rpm-build 95f51c
	limit = malloc(sizeof(struct talloc_memlimit));
rpm-build 95f51c
	if (limit == NULL) {
rpm-build 95f51c
		return 1;
rpm-build 95f51c
	}
rpm-build 95f51c
	limit->parent = tc;
rpm-build 95f51c
	limit->max_size = max_size;
rpm-build 95f51c
	limit->cur_size = _talloc_total_limit_size(ctx, tc->limit, limit);
rpm-build 95f51c
rpm-build 95f51c
	if (orig_limit) {
rpm-build 95f51c
		limit->upper = orig_limit;
rpm-build 95f51c
	} else {
rpm-build 95f51c
		limit->upper = NULL;
rpm-build 95f51c
	}
rpm-build 95f51c
rpm-build 95f51c
	return 0;
rpm-build 95f51c
}