Blame xmlmemory.c

Packit 423ecb
/*
Packit 423ecb
 * xmlmemory.c:  libxml memory allocator wrapper.
Packit 423ecb
 *
Packit 423ecb
 * daniel@veillard.com
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
#define IN_LIBXML
Packit 423ecb
#include "libxml.h"
Packit 423ecb
Packit 423ecb
#include <string.h>
Packit 423ecb
Packit 423ecb
#ifdef HAVE_SYS_TYPES_H
Packit 423ecb
#include <sys/types.h>
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
#ifdef HAVE_TIME_H
Packit 423ecb
#include <time.h>
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
#ifdef HAVE_STDLIB_H
Packit 423ecb
#include <stdlib.h>
Packit 423ecb
#else
Packit 423ecb
#ifdef HAVE_MALLOC_H
Packit 423ecb
#include <malloc.h>
Packit 423ecb
#endif
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
#ifdef HAVE_CTYPE_H
Packit 423ecb
#include <ctype.h>
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
/* #define DEBUG_MEMORY */
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * MEM_LIST:
Packit 423ecb
 *
Packit 423ecb
 * keep track of all allocated blocks for error reporting
Packit 423ecb
 * Always build the memory list !
Packit 423ecb
 */
Packit 423ecb
#ifdef DEBUG_MEMORY_LOCATION
Packit 423ecb
#ifndef MEM_LIST
Packit 423ecb
#define MEM_LIST /* keep a list of all the allocated memory blocks */
Packit 423ecb
#endif
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
#include <libxml/globals.h>	/* must come before xmlmemory.h */
Packit 423ecb
#include <libxml/xmlmemory.h>
Packit 423ecb
#include <libxml/xmlerror.h>
Packit 423ecb
#include <libxml/threads.h>
Packit 423ecb
Packit 423ecb
static int xmlMemInitialized = 0;
Packit 423ecb
static unsigned long  debugMemSize = 0;
Packit 423ecb
static unsigned long  debugMemBlocks = 0;
Packit 423ecb
static unsigned long  debugMaxMemSize = 0;
Packit 423ecb
static xmlMutexPtr xmlMemMutex = NULL;
Packit 423ecb
Packit 423ecb
void xmlMallocBreakpoint(void);
Packit 423ecb
Packit 423ecb
/************************************************************************
Packit 423ecb
 *									*
Packit 423ecb
 *		Macros, variables and associated types			*
Packit 423ecb
 *									*
Packit 423ecb
 ************************************************************************/
Packit 423ecb
Packit 423ecb
#if !defined(LIBXML_THREAD_ENABLED) && !defined(LIBXML_THREAD_ALLOC_ENABLED)
Packit 423ecb
#ifdef xmlMalloc
Packit 423ecb
#undef xmlMalloc
Packit 423ecb
#endif
Packit 423ecb
#ifdef xmlRealloc
Packit 423ecb
#undef xmlRealloc
Packit 423ecb
#endif
Packit 423ecb
#ifdef xmlMemStrdup
Packit 423ecb
#undef xmlMemStrdup
Packit 423ecb
#endif
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
/*
Packit 423ecb
 * Each of the blocks allocated begin with a header containing informations
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
#define MEMTAG 0x5aa5
Packit 423ecb
Packit 423ecb
#define MALLOC_TYPE 1
Packit 423ecb
#define REALLOC_TYPE 2
Packit 423ecb
#define STRDUP_TYPE 3
Packit 423ecb
#define MALLOC_ATOMIC_TYPE 4
Packit 423ecb
#define REALLOC_ATOMIC_TYPE 5
Packit 423ecb
Packit 423ecb
typedef struct memnod {
Packit 423ecb
    unsigned int   mh_tag;
Packit 423ecb
    unsigned int   mh_type;
Packit 423ecb
    unsigned long  mh_number;
Packit 423ecb
    size_t         mh_size;
Packit 423ecb
#ifdef MEM_LIST
Packit 423ecb
   struct memnod *mh_next;
Packit 423ecb
   struct memnod *mh_prev;
Packit 423ecb
#endif
Packit 423ecb
   const char    *mh_file;
Packit 423ecb
   unsigned int   mh_line;
Packit 423ecb
}  MEMHDR;
Packit 423ecb
Packit 423ecb
Packit 423ecb
#ifdef SUN4
Packit 423ecb
#define ALIGN_SIZE  16
Packit 423ecb
#else
Packit 423ecb
#define ALIGN_SIZE  sizeof(double)
Packit 423ecb
#endif
Packit 423ecb
#define HDR_SIZE    sizeof(MEMHDR)
Packit 423ecb
#define RESERVE_SIZE (((HDR_SIZE + (ALIGN_SIZE-1)) \
Packit 423ecb
		      / ALIGN_SIZE ) * ALIGN_SIZE)
Packit 423ecb
Packit 423ecb
#define MAX_SIZE_T ((size_t)-1)
Packit 423ecb
Packit 423ecb
#define CLIENT_2_HDR(a) ((void *) (((char *) (a)) - RESERVE_SIZE))
Packit 423ecb
#define HDR_2_CLIENT(a)    ((void *) (((char *) (a)) + RESERVE_SIZE))
Packit 423ecb
Packit 423ecb
Packit 423ecb
static unsigned int block=0;
Packit 423ecb
static unsigned int xmlMemStopAtBlock = 0;
Packit 423ecb
static void *xmlMemTraceBlockAt = NULL;
Packit 423ecb
#ifdef MEM_LIST
Packit 423ecb
static MEMHDR *memlist = NULL;
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
static void debugmem_tag_error(void *addr);
Packit 423ecb
#ifdef MEM_LIST
Packit 423ecb
static void  debugmem_list_add(MEMHDR *);
Packit 423ecb
static void debugmem_list_delete(MEMHDR *);
Packit 423ecb
#endif
Packit 423ecb
#define Mem_Tag_Err(a) debugmem_tag_error(a);
Packit 423ecb
Packit 423ecb
#ifndef TEST_POINT
Packit 423ecb
#define TEST_POINT
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlMallocBreakpoint:
Packit 423ecb
 *
Packit 423ecb
 * Breakpoint to use in conjunction with xmlMemStopAtBlock. When the block
Packit 423ecb
 * number reaches the specified value this function is called. One need to add a breakpoint
Packit 423ecb
 * to it to get the context in which the given block is allocated.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
void
Packit 423ecb
xmlMallocBreakpoint(void) {
Packit 423ecb
    xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
	    "xmlMallocBreakpoint reached on block %d\n", xmlMemStopAtBlock);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlMallocLoc:
Packit 423ecb
 * @size:  an int specifying the size in byte to allocate.
Packit 423ecb
 * @file:  the file name or NULL
Packit 423ecb
 * @line:  the line number
Packit 423ecb
 *
Packit 423ecb
 * a malloc() equivalent, with logging of the allocation info.
Packit 423ecb
 *
Packit 423ecb
 * Returns a pointer to the allocated area or NULL in case of lack of memory.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
void *
Packit 423ecb
xmlMallocLoc(size_t size, const char * file, int line)
Packit 423ecb
{
Packit 423ecb
    MEMHDR *p;
Packit 423ecb
    void *ret;
Packit 423ecb
Packit 423ecb
    if (!xmlMemInitialized) xmlInitMemory();
Packit 423ecb
#ifdef DEBUG_MEMORY
Packit 423ecb
    xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
	    "Malloc(%d)\n",size);
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
    TEST_POINT
Packit 423ecb
Packit 423ecb
    if (size > (MAX_SIZE_T - RESERVE_SIZE)) {
Packit 423ecb
	xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
		"xmlMallocLoc : Unsigned overflow\n");
Packit 423ecb
	xmlMemoryDump();
Packit 423ecb
	return(NULL);
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    p = (MEMHDR *) malloc(RESERVE_SIZE+size);
Packit 423ecb
Packit 423ecb
    if (!p) {
Packit 423ecb
	xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
		"xmlMallocLoc : Out of free space\n");
Packit 423ecb
	xmlMemoryDump();
Packit 423ecb
	return(NULL);
Packit 423ecb
    }
Packit 423ecb
    p->mh_tag = MEMTAG;
Packit 423ecb
    p->mh_size = size;
Packit 423ecb
    p->mh_type = MALLOC_TYPE;
Packit 423ecb
    p->mh_file = file;
Packit 423ecb
    p->mh_line = line;
Packit 423ecb
    xmlMutexLock(xmlMemMutex);
Packit 423ecb
    p->mh_number = ++block;
Packit 423ecb
    debugMemSize += size;
Packit 423ecb
    debugMemBlocks++;
Packit 423ecb
    if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize;
Packit 423ecb
#ifdef MEM_LIST
Packit 423ecb
    debugmem_list_add(p);
Packit 423ecb
#endif
Packit 423ecb
    xmlMutexUnlock(xmlMemMutex);
Packit 423ecb
Packit 423ecb
#ifdef DEBUG_MEMORY
Packit 423ecb
    xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
	    "Malloc(%d) Ok\n",size);
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
    if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint();
Packit 423ecb
Packit 423ecb
    ret = HDR_2_CLIENT(p);
Packit 423ecb
Packit 423ecb
    if (xmlMemTraceBlockAt == ret) {
Packit 423ecb
	xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
			"%p : Malloc(%lu) Ok\n", xmlMemTraceBlockAt,
Packit 423ecb
			(long unsigned)size);
Packit 423ecb
	xmlMallocBreakpoint();
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    TEST_POINT
Packit 423ecb
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlMallocAtomicLoc:
Packit 423ecb
 * @size:  an unsigned int specifying the size in byte to allocate.
Packit 423ecb
 * @file:  the file name or NULL
Packit 423ecb
 * @line:  the line number
Packit 423ecb
 *
Packit 423ecb
 * a malloc() equivalent, with logging of the allocation info.
Packit 423ecb
 *
Packit 423ecb
 * Returns a pointer to the allocated area or NULL in case of lack of memory.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
void *
Packit 423ecb
xmlMallocAtomicLoc(size_t size, const char * file, int line)
Packit 423ecb
{
Packit 423ecb
    MEMHDR *p;
Packit 423ecb
    void *ret;
Packit 423ecb
Packit 423ecb
    if (!xmlMemInitialized) xmlInitMemory();
Packit 423ecb
#ifdef DEBUG_MEMORY
Packit 423ecb
    xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
	    "Malloc(%d)\n",size);
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
    TEST_POINT
Packit 423ecb
Packit 423ecb
    if (size > (MAX_SIZE_T - RESERVE_SIZE)) {
Packit 423ecb
	xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
		"xmlMallocAtomicLoc : Unsigned overflow\n");
Packit 423ecb
	xmlMemoryDump();
Packit 423ecb
	return(NULL);
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    p = (MEMHDR *) malloc(RESERVE_SIZE+size);
Packit 423ecb
Packit 423ecb
    if (!p) {
Packit 423ecb
	xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
		"xmlMallocAtomicLoc : Out of free space\n");
Packit 423ecb
	xmlMemoryDump();
Packit 423ecb
	return(NULL);
Packit 423ecb
    }
Packit 423ecb
    p->mh_tag = MEMTAG;
Packit 423ecb
    p->mh_size = size;
Packit 423ecb
    p->mh_type = MALLOC_ATOMIC_TYPE;
Packit 423ecb
    p->mh_file = file;
Packit 423ecb
    p->mh_line = line;
Packit 423ecb
    xmlMutexLock(xmlMemMutex);
Packit 423ecb
    p->mh_number = ++block;
Packit 423ecb
    debugMemSize += size;
Packit 423ecb
    debugMemBlocks++;
Packit 423ecb
    if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize;
Packit 423ecb
#ifdef MEM_LIST
Packit 423ecb
    debugmem_list_add(p);
Packit 423ecb
#endif
Packit 423ecb
    xmlMutexUnlock(xmlMemMutex);
Packit 423ecb
Packit 423ecb
#ifdef DEBUG_MEMORY
Packit 423ecb
    xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
	    "Malloc(%d) Ok\n",size);
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
    if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint();
Packit 423ecb
Packit 423ecb
    ret = HDR_2_CLIENT(p);
Packit 423ecb
Packit 423ecb
    if (xmlMemTraceBlockAt == ret) {
Packit 423ecb
	xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
			"%p : Malloc(%lu) Ok\n", xmlMemTraceBlockAt,
Packit 423ecb
			(long unsigned)size);
Packit 423ecb
	xmlMallocBreakpoint();
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    TEST_POINT
Packit 423ecb
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
/**
Packit 423ecb
 * xmlMemMalloc:
Packit 423ecb
 * @size:  an int specifying the size in byte to allocate.
Packit 423ecb
 *
Packit 423ecb
 * a malloc() equivalent, with logging of the allocation info.
Packit 423ecb
 *
Packit 423ecb
 * Returns a pointer to the allocated area or NULL in case of lack of memory.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
void *
Packit 423ecb
xmlMemMalloc(size_t size)
Packit 423ecb
{
Packit 423ecb
    return(xmlMallocLoc(size, "none", 0));
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlReallocLoc:
Packit 423ecb
 * @ptr:  the initial memory block pointer
Packit 423ecb
 * @size:  an int specifying the size in byte to allocate.
Packit 423ecb
 * @file:  the file name or NULL
Packit 423ecb
 * @line:  the line number
Packit 423ecb
 *
Packit 423ecb
 * a realloc() equivalent, with logging of the allocation info.
Packit 423ecb
 *
Packit 423ecb
 * Returns a pointer to the allocated area or NULL in case of lack of memory.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
void *
Packit 423ecb
xmlReallocLoc(void *ptr,size_t size, const char * file, int line)
Packit 423ecb
{
Packit 423ecb
    MEMHDR *p, *tmp;
Packit 423ecb
    unsigned long number;
Packit 423ecb
#ifdef DEBUG_MEMORY
Packit 423ecb
    size_t oldsize;
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
    if (ptr == NULL)
Packit 423ecb
        return(xmlMallocLoc(size, file, line));
Packit 423ecb
Packit 423ecb
    if (!xmlMemInitialized) xmlInitMemory();
Packit 423ecb
    TEST_POINT
Packit 423ecb
Packit 423ecb
    p = CLIENT_2_HDR(ptr);
Packit 423ecb
    number = p->mh_number;
Packit 423ecb
    if (xmlMemStopAtBlock == number) xmlMallocBreakpoint();
Packit 423ecb
    if (p->mh_tag != MEMTAG) {
Packit 423ecb
       Mem_Tag_Err(p);
Packit 423ecb
	 goto error;
Packit 423ecb
    }
Packit 423ecb
    p->mh_tag = ~MEMTAG;
Packit 423ecb
    xmlMutexLock(xmlMemMutex);
Packit 423ecb
    debugMemSize -= p->mh_size;
Packit 423ecb
    debugMemBlocks--;
Packit 423ecb
#ifdef DEBUG_MEMORY
Packit 423ecb
    oldsize = p->mh_size;
Packit 423ecb
#endif
Packit 423ecb
#ifdef MEM_LIST
Packit 423ecb
    debugmem_list_delete(p);
Packit 423ecb
#endif
Packit 423ecb
    xmlMutexUnlock(xmlMemMutex);
Packit 423ecb
Packit 423ecb
    if (size > (MAX_SIZE_T - RESERVE_SIZE)) {
Packit 423ecb
	xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
		"xmlReallocLoc : Unsigned overflow\n");
Packit 423ecb
	xmlMemoryDump();
Packit 423ecb
	return(NULL);
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    tmp = (MEMHDR *) realloc(p,RESERVE_SIZE+size);
Packit 423ecb
    if (!tmp) {
Packit 423ecb
	 free(p);
Packit 423ecb
	 goto error;
Packit 423ecb
    }
Packit 423ecb
    p = tmp;
Packit 423ecb
    if (xmlMemTraceBlockAt == ptr) {
Packit 423ecb
	xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
			"%p : Realloced(%lu -> %lu) Ok\n",
Packit 423ecb
			xmlMemTraceBlockAt, (long unsigned)p->mh_size,
Packit 423ecb
			(long unsigned)size);
Packit 423ecb
	xmlMallocBreakpoint();
Packit 423ecb
    }
Packit 423ecb
    p->mh_tag = MEMTAG;
Packit 423ecb
    p->mh_number = number;
Packit 423ecb
    p->mh_type = REALLOC_TYPE;
Packit 423ecb
    p->mh_size = size;
Packit 423ecb
    p->mh_file = file;
Packit 423ecb
    p->mh_line = line;
Packit 423ecb
    xmlMutexLock(xmlMemMutex);
Packit 423ecb
    debugMemSize += size;
Packit 423ecb
    debugMemBlocks++;
Packit 423ecb
    if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize;
Packit 423ecb
#ifdef MEM_LIST
Packit 423ecb
    debugmem_list_add(p);
Packit 423ecb
#endif
Packit 423ecb
    xmlMutexUnlock(xmlMemMutex);
Packit 423ecb
Packit 423ecb
    TEST_POINT
Packit 423ecb
Packit 423ecb
#ifdef DEBUG_MEMORY
Packit 423ecb
    xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
	    "Realloced(%d to %d) Ok\n", oldsize, size);
Packit 423ecb
#endif
Packit 423ecb
    return(HDR_2_CLIENT(p));
Packit 423ecb
Packit 423ecb
error:
Packit 423ecb
    return(NULL);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlMemRealloc:
Packit 423ecb
 * @ptr:  the initial memory block pointer
Packit 423ecb
 * @size:  an int specifying the size in byte to allocate.
Packit 423ecb
 *
Packit 423ecb
 * a realloc() equivalent, with logging of the allocation info.
Packit 423ecb
 *
Packit 423ecb
 * Returns a pointer to the allocated area or NULL in case of lack of memory.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
void *
Packit 423ecb
xmlMemRealloc(void *ptr,size_t size) {
Packit 423ecb
    return(xmlReallocLoc(ptr, size, "none", 0));
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlMemFree:
Packit 423ecb
 * @ptr:  the memory block pointer
Packit 423ecb
 *
Packit 423ecb
 * a free() equivalent, with error checking.
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlMemFree(void *ptr)
Packit 423ecb
{
Packit 423ecb
    MEMHDR *p;
Packit 423ecb
    char *target;
Packit 423ecb
#ifdef DEBUG_MEMORY
Packit 423ecb
    size_t size;
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
    if (ptr == NULL)
Packit 423ecb
	return;
Packit 423ecb
Packit 423ecb
    if (ptr == (void *) -1) {
Packit 423ecb
	xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
	    "trying to free pointer from freed area\n");
Packit 423ecb
        goto error;
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    if (xmlMemTraceBlockAt == ptr) {
Packit 423ecb
	xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
			"%p : Freed()\n", xmlMemTraceBlockAt);
Packit 423ecb
	xmlMallocBreakpoint();
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    TEST_POINT
Packit 423ecb
Packit 423ecb
    target = (char *) ptr;
Packit 423ecb
Packit 423ecb
    p = CLIENT_2_HDR(ptr);
Packit 423ecb
    if (p->mh_tag != MEMTAG) {
Packit 423ecb
        Mem_Tag_Err(p);
Packit 423ecb
        goto error;
Packit 423ecb
    }
Packit 423ecb
    if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint();
Packit 423ecb
    p->mh_tag = ~MEMTAG;
Packit 423ecb
    memset(target, -1, p->mh_size);
Packit 423ecb
    xmlMutexLock(xmlMemMutex);
Packit 423ecb
    debugMemSize -= p->mh_size;
Packit 423ecb
    debugMemBlocks--;
Packit 423ecb
#ifdef DEBUG_MEMORY
Packit 423ecb
    size = p->mh_size;
Packit 423ecb
#endif
Packit 423ecb
#ifdef MEM_LIST
Packit 423ecb
    debugmem_list_delete(p);
Packit 423ecb
#endif
Packit 423ecb
    xmlMutexUnlock(xmlMemMutex);
Packit 423ecb
Packit 423ecb
    free(p);
Packit 423ecb
Packit 423ecb
    TEST_POINT
Packit 423ecb
Packit 423ecb
#ifdef DEBUG_MEMORY
Packit 423ecb
    xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
	    "Freed(%d) Ok\n", size);
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
    return;
Packit 423ecb
Packit 423ecb
error:
Packit 423ecb
    xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
	    "xmlMemFree(%p) error\n", ptr);
Packit 423ecb
    xmlMallocBreakpoint();
Packit 423ecb
    return;
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlMemStrdupLoc:
Packit 423ecb
 * @str:  the initial string pointer
Packit 423ecb
 * @file:  the file name or NULL
Packit 423ecb
 * @line:  the line number
Packit 423ecb
 *
Packit 423ecb
 * a strdup() equivalent, with logging of the allocation info.
Packit 423ecb
 *
Packit 423ecb
 * Returns a pointer to the new string or NULL if allocation error occurred.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
char *
Packit 423ecb
xmlMemStrdupLoc(const char *str, const char *file, int line)
Packit 423ecb
{
Packit 423ecb
    char *s;
Packit 423ecb
    size_t size = strlen(str) + 1;
Packit 423ecb
    MEMHDR *p;
Packit 423ecb
Packit 423ecb
    if (!xmlMemInitialized) xmlInitMemory();
Packit 423ecb
    TEST_POINT
Packit 423ecb
Packit 423ecb
    if (size > (MAX_SIZE_T - RESERVE_SIZE)) {
Packit 423ecb
	xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
		"xmlMemStrdupLoc : Unsigned overflow\n");
Packit 423ecb
	xmlMemoryDump();
Packit 423ecb
	return(NULL);
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    p = (MEMHDR *) malloc(RESERVE_SIZE+size);
Packit 423ecb
    if (!p) {
Packit 423ecb
      goto error;
Packit 423ecb
    }
Packit 423ecb
    p->mh_tag = MEMTAG;
Packit 423ecb
    p->mh_size = size;
Packit 423ecb
    p->mh_type = STRDUP_TYPE;
Packit 423ecb
    p->mh_file = file;
Packit 423ecb
    p->mh_line = line;
Packit 423ecb
    xmlMutexLock(xmlMemMutex);
Packit 423ecb
    p->mh_number = ++block;
Packit 423ecb
    debugMemSize += size;
Packit 423ecb
    debugMemBlocks++;
Packit 423ecb
    if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize;
Packit 423ecb
#ifdef MEM_LIST
Packit 423ecb
    debugmem_list_add(p);
Packit 423ecb
#endif
Packit 423ecb
    xmlMutexUnlock(xmlMemMutex);
Packit 423ecb
Packit 423ecb
    s = (char *) HDR_2_CLIENT(p);
Packit 423ecb
Packit 423ecb
    if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint();
Packit 423ecb
Packit 423ecb
    strcpy(s,str);
Packit 423ecb
Packit 423ecb
    TEST_POINT
Packit 423ecb
Packit 423ecb
    if (xmlMemTraceBlockAt == s) {
Packit 423ecb
	xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
			"%p : Strdup() Ok\n", xmlMemTraceBlockAt);
Packit 423ecb
	xmlMallocBreakpoint();
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    return(s);
Packit 423ecb
Packit 423ecb
error:
Packit 423ecb
    return(NULL);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlMemoryStrdup:
Packit 423ecb
 * @str:  the initial string pointer
Packit 423ecb
 *
Packit 423ecb
 * a strdup() equivalent, with logging of the allocation info.
Packit 423ecb
 *
Packit 423ecb
 * Returns a pointer to the new string or NULL if allocation error occurred.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
char *
Packit 423ecb
xmlMemoryStrdup(const char *str) {
Packit 423ecb
    return(xmlMemStrdupLoc(str, "none", 0));
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlMemUsed:
Packit 423ecb
 *
Packit 423ecb
 * Provides the amount of memory currently allocated
Packit 423ecb
 *
Packit 423ecb
 * Returns an int representing the amount of memory allocated.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
int
Packit 423ecb
xmlMemUsed(void) {
Packit 423ecb
    int res;
Packit 423ecb
Packit 423ecb
    xmlMutexLock(xmlMemMutex);
Packit 423ecb
    res = debugMemSize;
Packit 423ecb
    xmlMutexUnlock(xmlMemMutex);
Packit 423ecb
    return(res);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlMemBlocks:
Packit 423ecb
 *
Packit 423ecb
 * Provides the number of memory areas currently allocated
Packit 423ecb
 *
Packit 423ecb
 * Returns an int representing the number of blocks
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
int
Packit 423ecb
xmlMemBlocks(void) {
Packit 423ecb
    int res;
Packit 423ecb
Packit 423ecb
    xmlMutexLock(xmlMemMutex);
Packit 423ecb
    res = debugMemBlocks;
Packit 423ecb
    xmlMutexUnlock(xmlMemMutex);
Packit 423ecb
    return(res);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#ifdef MEM_LIST
Packit 423ecb
/**
Packit 423ecb
 * xmlMemContentShow:
Packit 423ecb
 * @fp:  a FILE descriptor used as the output file
Packit 423ecb
 * @p:  a memory block header
Packit 423ecb
 *
Packit 423ecb
 * tries to show some content from the memory block
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
static void
Packit 423ecb
xmlMemContentShow(FILE *fp, MEMHDR *p)
Packit 423ecb
{
Packit 423ecb
    int i,j,k,len;
Packit 423ecb
    const char *buf;
Packit 423ecb
Packit 423ecb
    if (p == NULL) {
Packit 423ecb
	fprintf(fp, " NULL");
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
    len = p->mh_size;
Packit 423ecb
    buf = (const char *) HDR_2_CLIENT(p);
Packit 423ecb
Packit 423ecb
    for (i = 0;i < len;i++) {
Packit 423ecb
        if (buf[i] == 0) break;
Packit 423ecb
	if (!isprint((unsigned char) buf[i])) break;
Packit 423ecb
    }
Packit 423ecb
    if ((i < 4) && ((buf[i] != 0) || (i == 0))) {
Packit 423ecb
        if (len >= 4) {
Packit 423ecb
	    MEMHDR *q;
Packit 423ecb
	    void *cur;
Packit 423ecb
Packit 423ecb
            for (j = 0;(j < len -3) && (j < 40);j += 4) {
Packit 423ecb
		cur = *((void **) &buf[j]);
Packit 423ecb
		q = CLIENT_2_HDR(cur);
Packit 423ecb
		p = memlist;
Packit 423ecb
		k = 0;
Packit 423ecb
		while (p != NULL) {
Packit 423ecb
		    if (p == q) break;
Packit 423ecb
		    p = p->mh_next;
Packit 423ecb
		    if (k++ > 100) break;
Packit 423ecb
		}
Packit 423ecb
		if ((p != NULL) && (p == q)) {
Packit 423ecb
		    fprintf(fp, " pointer to #%lu at index %d",
Packit 423ecb
		            p->mh_number, j);
Packit 423ecb
		    return;
Packit 423ecb
		}
Packit 423ecb
	    }
Packit 423ecb
	}
Packit 423ecb
    } else if ((i == 0) && (buf[i] == 0)) {
Packit 423ecb
        fprintf(fp," null");
Packit 423ecb
    } else {
Packit 423ecb
        if (buf[i] == 0) fprintf(fp," \"%.25s\"", buf);
Packit 423ecb
	else {
Packit 423ecb
            fprintf(fp," [");
Packit 423ecb
	    for (j = 0;j < i;j++)
Packit 423ecb
                fprintf(fp,"%c", buf[j]);
Packit 423ecb
            fprintf(fp,"]");
Packit 423ecb
	}
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlMemDisplayLast:
Packit 423ecb
 * @fp:  a FILE descriptor used as the output file, if NULL, the result is
Packit 423ecb
 *       written to the file .memorylist
Packit 423ecb
 * @nbBytes: the amount of memory to dump
Packit 423ecb
 *
Packit 423ecb
 * the last nbBytes of memory allocated and not freed, useful for dumping
Packit 423ecb
 * the memory left allocated between two places at runtime.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
void
Packit 423ecb
xmlMemDisplayLast(FILE *fp, long nbBytes)
Packit 423ecb
{
Packit 423ecb
#ifdef MEM_LIST
Packit 423ecb
    MEMHDR *p;
Packit 423ecb
    unsigned idx;
Packit 423ecb
    int     nb = 0;
Packit 423ecb
#endif
Packit 423ecb
    FILE *old_fp = fp;
Packit 423ecb
Packit 423ecb
    if (nbBytes <= 0)
Packit 423ecb
        return;
Packit 423ecb
Packit 423ecb
    if (fp == NULL) {
Packit 423ecb
	fp = fopen(".memorylist", "w");
Packit 423ecb
	if (fp == NULL)
Packit 423ecb
	    return;
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
#ifdef MEM_LIST
Packit 423ecb
    fprintf(fp,"   Last %li MEMORY ALLOCATED : %lu, MAX was %lu\n",
Packit 423ecb
            nbBytes, debugMemSize, debugMaxMemSize);
Packit 423ecb
    fprintf(fp,"BLOCK  NUMBER   SIZE  TYPE\n");
Packit 423ecb
    idx = 0;
Packit 423ecb
    xmlMutexLock(xmlMemMutex);
Packit 423ecb
    p = memlist;
Packit 423ecb
    while ((p) && (nbBytes > 0)) {
Packit 423ecb
	  fprintf(fp,"%-5u  %6lu %6lu ",idx++,p->mh_number,
Packit 423ecb
		  (unsigned long)p->mh_size);
Packit 423ecb
        switch (p->mh_type) {
Packit 423ecb
           case STRDUP_TYPE:fprintf(fp,"strdup()  in ");break;
Packit 423ecb
           case MALLOC_TYPE:fprintf(fp,"malloc()  in ");break;
Packit 423ecb
           case REALLOC_TYPE:fprintf(fp,"realloc() in ");break;
Packit 423ecb
           case MALLOC_ATOMIC_TYPE:fprintf(fp,"atomicmalloc()  in ");break;
Packit 423ecb
           case REALLOC_ATOMIC_TYPE:fprintf(fp,"atomicrealloc() in ");break;
Packit 423ecb
           default:
Packit 423ecb
	        fprintf(fp,"Unknown memory block, may be corrupted");
Packit 423ecb
		xmlMutexUnlock(xmlMemMutex);
Packit 423ecb
		if (old_fp == NULL)
Packit 423ecb
		    fclose(fp);
Packit 423ecb
		return;
Packit 423ecb
        }
Packit 423ecb
	if (p->mh_file != NULL) fprintf(fp,"%s(%u)", p->mh_file, p->mh_line);
Packit 423ecb
        if (p->mh_tag != MEMTAG)
Packit 423ecb
	      fprintf(fp,"  INVALID");
Packit 423ecb
        nb++;
Packit 423ecb
	if (nb < 100)
Packit 423ecb
	    xmlMemContentShow(fp, p);
Packit 423ecb
	else
Packit 423ecb
	    fprintf(fp," skip");
Packit 423ecb
Packit 423ecb
        fprintf(fp,"\n");
Packit 423ecb
	nbBytes -= (unsigned long)p->mh_size;
Packit 423ecb
        p = p->mh_next;
Packit 423ecb
    }
Packit 423ecb
    xmlMutexUnlock(xmlMemMutex);
Packit 423ecb
#else
Packit 423ecb
    fprintf(fp,"Memory list not compiled (MEM_LIST not defined !)\n");
Packit 423ecb
#endif
Packit 423ecb
    if (old_fp == NULL)
Packit 423ecb
	fclose(fp);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlMemDisplay:
Packit 423ecb
 * @fp:  a FILE descriptor used as the output file, if NULL, the result is
Packit 423ecb
 *       written to the file .memorylist
Packit 423ecb
 *
Packit 423ecb
 * show in-extenso the memory blocks allocated
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
void
Packit 423ecb
xmlMemDisplay(FILE *fp)
Packit 423ecb
{
Packit 423ecb
#ifdef MEM_LIST
Packit 423ecb
    MEMHDR *p;
Packit 423ecb
    unsigned idx;
Packit 423ecb
    int     nb = 0;
Packit 423ecb
#if defined(HAVE_LOCALTIME) && defined(HAVE_STRFTIME)
Packit 423ecb
    time_t currentTime;
Packit 423ecb
    char buf[500];
Packit 423ecb
    struct tm * tstruct;
Packit 423ecb
#endif
Packit 423ecb
#endif
Packit 423ecb
    FILE *old_fp = fp;
Packit 423ecb
Packit 423ecb
    if (fp == NULL) {
Packit 423ecb
	fp = fopen(".memorylist", "w");
Packit 423ecb
	if (fp == NULL)
Packit 423ecb
	    return;
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
#ifdef MEM_LIST
Packit 423ecb
#if defined(HAVE_LOCALTIME) && defined(HAVE_STRFTIME)
Packit 423ecb
    currentTime = time(NULL);
Packit 423ecb
    tstruct = localtime(&currentTime);
Packit 423ecb
    strftime(buf, sizeof(buf) - 1, "%I:%M:%S %p", tstruct);
Packit 423ecb
    fprintf(fp,"      %s\n\n", buf);
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
Packit 423ecb
    fprintf(fp,"      MEMORY ALLOCATED : %lu, MAX was %lu\n",
Packit 423ecb
            debugMemSize, debugMaxMemSize);
Packit 423ecb
    fprintf(fp,"BLOCK  NUMBER   SIZE  TYPE\n");
Packit 423ecb
    idx = 0;
Packit 423ecb
    xmlMutexLock(xmlMemMutex);
Packit 423ecb
    p = memlist;
Packit 423ecb
    while (p) {
Packit 423ecb
	  fprintf(fp,"%-5u  %6lu %6lu ",idx++,p->mh_number,
Packit 423ecb
		  (unsigned long)p->mh_size);
Packit 423ecb
        switch (p->mh_type) {
Packit 423ecb
           case STRDUP_TYPE:fprintf(fp,"strdup()  in ");break;
Packit 423ecb
           case MALLOC_TYPE:fprintf(fp,"malloc()  in ");break;
Packit 423ecb
           case REALLOC_TYPE:fprintf(fp,"realloc() in ");break;
Packit 423ecb
           case MALLOC_ATOMIC_TYPE:fprintf(fp,"atomicmalloc()  in ");break;
Packit 423ecb
           case REALLOC_ATOMIC_TYPE:fprintf(fp,"atomicrealloc() in ");break;
Packit 423ecb
           default:
Packit 423ecb
	        fprintf(fp,"Unknown memory block, may be corrupted");
Packit 423ecb
		xmlMutexUnlock(xmlMemMutex);
Packit 423ecb
		if (old_fp == NULL)
Packit 423ecb
		    fclose(fp);
Packit 423ecb
		return;
Packit 423ecb
        }
Packit 423ecb
	if (p->mh_file != NULL) fprintf(fp,"%s(%u)", p->mh_file, p->mh_line);
Packit 423ecb
        if (p->mh_tag != MEMTAG)
Packit 423ecb
	      fprintf(fp,"  INVALID");
Packit 423ecb
        nb++;
Packit 423ecb
	if (nb < 100)
Packit 423ecb
	    xmlMemContentShow(fp, p);
Packit 423ecb
	else
Packit 423ecb
	    fprintf(fp," skip");
Packit 423ecb
Packit 423ecb
        fprintf(fp,"\n");
Packit 423ecb
        p = p->mh_next;
Packit 423ecb
    }
Packit 423ecb
    xmlMutexUnlock(xmlMemMutex);
Packit 423ecb
#else
Packit 423ecb
    fprintf(fp,"Memory list not compiled (MEM_LIST not defined !)\n");
Packit 423ecb
#endif
Packit 423ecb
    if (old_fp == NULL)
Packit 423ecb
	fclose(fp);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#ifdef MEM_LIST
Packit 423ecb
Packit 423ecb
static void debugmem_list_add(MEMHDR *p)
Packit 423ecb
{
Packit 423ecb
     p->mh_next = memlist;
Packit 423ecb
     p->mh_prev = NULL;
Packit 423ecb
     if (memlist) memlist->mh_prev = p;
Packit 423ecb
     memlist = p;
Packit 423ecb
#ifdef MEM_LIST_DEBUG
Packit 423ecb
     if (stderr)
Packit 423ecb
     Mem_Display(stderr);
Packit 423ecb
#endif
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
static void debugmem_list_delete(MEMHDR *p)
Packit 423ecb
{
Packit 423ecb
     if (p->mh_next)
Packit 423ecb
     p->mh_next->mh_prev = p->mh_prev;
Packit 423ecb
     if (p->mh_prev)
Packit 423ecb
     p->mh_prev->mh_next = p->mh_next;
Packit 423ecb
     else memlist = p->mh_next;
Packit 423ecb
#ifdef MEM_LIST_DEBUG
Packit 423ecb
     if (stderr)
Packit 423ecb
     Mem_Display(stderr);
Packit 423ecb
#endif
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
/*
Packit 423ecb
 * debugmem_tag_error:
Packit 423ecb
 *
Packit 423ecb
 * internal error function.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
static void debugmem_tag_error(void *p)
Packit 423ecb
{
Packit 423ecb
     xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
	     "Memory tag error occurs :%p \n\t bye\n", p);
Packit 423ecb
#ifdef MEM_LIST
Packit 423ecb
     if (stderr)
Packit 423ecb
     xmlMemDisplay(stderr);
Packit 423ecb
#endif
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#ifdef MEM_LIST
Packit 423ecb
static FILE *xmlMemoryDumpFile = NULL;
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlMemShow:
Packit 423ecb
 * @fp:  a FILE descriptor used as the output file
Packit 423ecb
 * @nr:  number of entries to dump
Packit 423ecb
 *
Packit 423ecb
 * show a show display of the memory allocated, and dump
Packit 423ecb
 * the @nr last allocated areas which were not freed
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
void
Packit 423ecb
xmlMemShow(FILE *fp, int nr ATTRIBUTE_UNUSED)
Packit 423ecb
{
Packit 423ecb
#ifdef MEM_LIST
Packit 423ecb
    MEMHDR *p;
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
    if (fp != NULL)
Packit 423ecb
	fprintf(fp,"      MEMORY ALLOCATED : %lu, MAX was %lu\n",
Packit 423ecb
		debugMemSize, debugMaxMemSize);
Packit 423ecb
#ifdef MEM_LIST
Packit 423ecb
    xmlMutexLock(xmlMemMutex);
Packit 423ecb
    if (nr > 0) {
Packit 423ecb
	fprintf(fp,"NUMBER   SIZE  TYPE   WHERE\n");
Packit 423ecb
	p = memlist;
Packit 423ecb
	while ((p) && nr > 0) {
Packit 423ecb
	      fprintf(fp,"%6lu %6lu ",p->mh_number,(unsigned long)p->mh_size);
Packit 423ecb
	    switch (p->mh_type) {
Packit 423ecb
	       case STRDUP_TYPE:fprintf(fp,"strdup()  in ");break;
Packit 423ecb
	       case MALLOC_TYPE:fprintf(fp,"malloc()  in ");break;
Packit 423ecb
	       case MALLOC_ATOMIC_TYPE:fprintf(fp,"atomicmalloc()  in ");break;
Packit 423ecb
	      case REALLOC_TYPE:fprintf(fp,"realloc() in ");break;
Packit 423ecb
	      case REALLOC_ATOMIC_TYPE:fprintf(fp,"atomicrealloc() in ");break;
Packit 423ecb
		default:fprintf(fp,"   ???    in ");break;
Packit 423ecb
	    }
Packit 423ecb
	    if (p->mh_file != NULL)
Packit 423ecb
	        fprintf(fp,"%s(%u)", p->mh_file, p->mh_line);
Packit 423ecb
	    if (p->mh_tag != MEMTAG)
Packit 423ecb
		fprintf(fp,"  INVALID");
Packit 423ecb
	    xmlMemContentShow(fp, p);
Packit 423ecb
	    fprintf(fp,"\n");
Packit 423ecb
	    nr--;
Packit 423ecb
	    p = p->mh_next;
Packit 423ecb
	}
Packit 423ecb
    }
Packit 423ecb
    xmlMutexUnlock(xmlMemMutex);
Packit 423ecb
#endif /* MEM_LIST */
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlMemoryDump:
Packit 423ecb
 *
Packit 423ecb
 * Dump in-extenso the memory blocks allocated to the file .memorylist
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
void
Packit 423ecb
xmlMemoryDump(void)
Packit 423ecb
{
Packit 423ecb
#ifdef MEM_LIST
Packit 423ecb
    FILE *dump;
Packit 423ecb
Packit 423ecb
    if (debugMaxMemSize == 0)
Packit 423ecb
	return;
Packit 423ecb
    dump = fopen(".memdump", "w");
Packit 423ecb
    if (dump == NULL)
Packit 423ecb
	xmlMemoryDumpFile = stderr;
Packit 423ecb
    else xmlMemoryDumpFile = dump;
Packit 423ecb
Packit 423ecb
    xmlMemDisplay(xmlMemoryDumpFile);
Packit 423ecb
Packit 423ecb
    if (dump != NULL) fclose(dump);
Packit 423ecb
#endif /* MEM_LIST */
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
Packit 423ecb
/****************************************************************
Packit 423ecb
 *								*
Packit 423ecb
 *		Initialization Routines				*
Packit 423ecb
 *								*
Packit 423ecb
 ****************************************************************/
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlInitMemory:
Packit 423ecb
 *
Packit 423ecb
 * Initialize the memory layer.
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 on success
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlInitMemory(void)
Packit 423ecb
{
Packit 423ecb
#ifdef HAVE_STDLIB_H
Packit 423ecb
     char *breakpoint;
Packit 423ecb
#endif
Packit 423ecb
#ifdef DEBUG_MEMORY
Packit 423ecb
     xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
	     "xmlInitMemory()\n");
Packit 423ecb
#endif
Packit 423ecb
    /*
Packit 423ecb
     This is really not good code (see Bug 130419).  Suggestions for
Packit 423ecb
     improvement will be welcome!
Packit 423ecb
    */
Packit 423ecb
     if (xmlMemInitialized) return(-1);
Packit 423ecb
     xmlMemInitialized = 1;
Packit 423ecb
     xmlMemMutex = xmlNewMutex();
Packit 423ecb
Packit 423ecb
#ifdef HAVE_STDLIB_H
Packit 423ecb
     breakpoint = getenv("XML_MEM_BREAKPOINT");
Packit 423ecb
     if (breakpoint != NULL) {
Packit 423ecb
         sscanf(breakpoint, "%ud", &xmlMemStopAtBlock);
Packit 423ecb
     }
Packit 423ecb
#endif
Packit 423ecb
#ifdef HAVE_STDLIB_H
Packit 423ecb
     breakpoint = getenv("XML_MEM_TRACE");
Packit 423ecb
     if (breakpoint != NULL) {
Packit 423ecb
         sscanf(breakpoint, "%p", &xmlMemTraceBlockAt);
Packit 423ecb
     }
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
#ifdef DEBUG_MEMORY
Packit 423ecb
     xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
	     "xmlInitMemory() Ok\n");
Packit 423ecb
#endif
Packit 423ecb
     return(0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlCleanupMemory:
Packit 423ecb
 *
Packit 423ecb
 * Free up all the memory allocated by the library for its own
Packit 423ecb
 * use. This should not be called by user level code.
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlCleanupMemory(void) {
Packit 423ecb
#ifdef DEBUG_MEMORY
Packit 423ecb
     xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
	     "xmlCleanupMemory()\n");
Packit 423ecb
#endif
Packit 423ecb
    if (xmlMemInitialized == 0)
Packit 423ecb
        return;
Packit 423ecb
Packit 423ecb
    xmlFreeMutex(xmlMemMutex);
Packit 423ecb
    xmlMemMutex = NULL;
Packit 423ecb
    xmlMemInitialized = 0;
Packit 423ecb
#ifdef DEBUG_MEMORY
Packit 423ecb
     xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
	     "xmlCleanupMemory() Ok\n");
Packit 423ecb
#endif
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlMemSetup:
Packit 423ecb
 * @freeFunc: the free() function to use
Packit 423ecb
 * @mallocFunc: the malloc() function to use
Packit 423ecb
 * @reallocFunc: the realloc() function to use
Packit 423ecb
 * @strdupFunc: the strdup() function to use
Packit 423ecb
 *
Packit 423ecb
 * Override the default memory access functions with a new set
Packit 423ecb
 * This has to be called before any other libxml routines !
Packit 423ecb
 *
Packit 423ecb
 * Should this be blocked if there was already some allocations
Packit 423ecb
 * done ?
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 on success
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlMemSetup(xmlFreeFunc freeFunc, xmlMallocFunc mallocFunc,
Packit 423ecb
            xmlReallocFunc reallocFunc, xmlStrdupFunc strdupFunc) {
Packit 423ecb
#ifdef DEBUG_MEMORY
Packit 423ecb
     xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
	     "xmlMemSetup()\n");
Packit 423ecb
#endif
Packit 423ecb
    if (freeFunc == NULL)
Packit 423ecb
	return(-1);
Packit 423ecb
    if (mallocFunc == NULL)
Packit 423ecb
	return(-1);
Packit 423ecb
    if (reallocFunc == NULL)
Packit 423ecb
	return(-1);
Packit 423ecb
    if (strdupFunc == NULL)
Packit 423ecb
	return(-1);
Packit 423ecb
    xmlFree = freeFunc;
Packit 423ecb
    xmlMalloc = mallocFunc;
Packit 423ecb
    xmlMallocAtomic = mallocFunc;
Packit 423ecb
    xmlRealloc = reallocFunc;
Packit 423ecb
    xmlMemStrdup = strdupFunc;
Packit 423ecb
#ifdef DEBUG_MEMORY
Packit 423ecb
     xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
	     "xmlMemSetup() Ok\n");
Packit 423ecb
#endif
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlMemGet:
Packit 423ecb
 * @freeFunc: place to save the free() function in use
Packit 423ecb
 * @mallocFunc: place to save the malloc() function in use
Packit 423ecb
 * @reallocFunc: place to save the realloc() function in use
Packit 423ecb
 * @strdupFunc: place to save the strdup() function in use
Packit 423ecb
 *
Packit 423ecb
 * Provides the memory access functions set currently in use
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 on success
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlMemGet(xmlFreeFunc *freeFunc, xmlMallocFunc *mallocFunc,
Packit 423ecb
	  xmlReallocFunc *reallocFunc, xmlStrdupFunc *strdupFunc) {
Packit 423ecb
    if (freeFunc != NULL) *freeFunc = xmlFree;
Packit 423ecb
    if (mallocFunc != NULL) *mallocFunc = xmlMalloc;
Packit 423ecb
    if (reallocFunc != NULL) *reallocFunc = xmlRealloc;
Packit 423ecb
    if (strdupFunc != NULL) *strdupFunc = xmlMemStrdup;
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlGcMemSetup:
Packit 423ecb
 * @freeFunc: the free() function to use
Packit 423ecb
 * @mallocFunc: the malloc() function to use
Packit 423ecb
 * @mallocAtomicFunc: the malloc() function to use for atomic allocations
Packit 423ecb
 * @reallocFunc: the realloc() function to use
Packit 423ecb
 * @strdupFunc: the strdup() function to use
Packit 423ecb
 *
Packit 423ecb
 * Override the default memory access functions with a new set
Packit 423ecb
 * This has to be called before any other libxml routines !
Packit 423ecb
 * The mallocAtomicFunc is specialized for atomic block
Packit 423ecb
 * allocations (i.e. of areas  useful for garbage collected memory allocators
Packit 423ecb
 *
Packit 423ecb
 * Should this be blocked if there was already some allocations
Packit 423ecb
 * done ?
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 on success
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlGcMemSetup(xmlFreeFunc freeFunc, xmlMallocFunc mallocFunc,
Packit 423ecb
              xmlMallocFunc mallocAtomicFunc, xmlReallocFunc reallocFunc,
Packit 423ecb
	      xmlStrdupFunc strdupFunc) {
Packit 423ecb
#ifdef DEBUG_MEMORY
Packit 423ecb
     xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
	     "xmlGcMemSetup()\n");
Packit 423ecb
#endif
Packit 423ecb
    if (freeFunc == NULL)
Packit 423ecb
	return(-1);
Packit 423ecb
    if (mallocFunc == NULL)
Packit 423ecb
	return(-1);
Packit 423ecb
    if (mallocAtomicFunc == NULL)
Packit 423ecb
	return(-1);
Packit 423ecb
    if (reallocFunc == NULL)
Packit 423ecb
	return(-1);
Packit 423ecb
    if (strdupFunc == NULL)
Packit 423ecb
	return(-1);
Packit 423ecb
    xmlFree = freeFunc;
Packit 423ecb
    xmlMalloc = mallocFunc;
Packit 423ecb
    xmlMallocAtomic = mallocAtomicFunc;
Packit 423ecb
    xmlRealloc = reallocFunc;
Packit 423ecb
    xmlMemStrdup = strdupFunc;
Packit 423ecb
#ifdef DEBUG_MEMORY
Packit 423ecb
     xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
	     "xmlGcMemSetup() Ok\n");
Packit 423ecb
#endif
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlGcMemGet:
Packit 423ecb
 * @freeFunc: place to save the free() function in use
Packit 423ecb
 * @mallocFunc: place to save the malloc() function in use
Packit 423ecb
 * @mallocAtomicFunc: place to save the atomic malloc() function in use
Packit 423ecb
 * @reallocFunc: place to save the realloc() function in use
Packit 423ecb
 * @strdupFunc: place to save the strdup() function in use
Packit 423ecb
 *
Packit 423ecb
 * Provides the memory access functions set currently in use
Packit 423ecb
 * The mallocAtomicFunc is specialized for atomic block
Packit 423ecb
 * allocations (i.e. of areas  useful for garbage collected memory allocators
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 on success
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlGcMemGet(xmlFreeFunc *freeFunc, xmlMallocFunc *mallocFunc,
Packit 423ecb
            xmlMallocFunc *mallocAtomicFunc, xmlReallocFunc *reallocFunc,
Packit 423ecb
	    xmlStrdupFunc *strdupFunc) {
Packit 423ecb
    if (freeFunc != NULL) *freeFunc = xmlFree;
Packit 423ecb
    if (mallocFunc != NULL) *mallocFunc = xmlMalloc;
Packit 423ecb
    if (mallocAtomicFunc != NULL) *mallocAtomicFunc = xmlMallocAtomic;
Packit 423ecb
    if (reallocFunc != NULL) *reallocFunc = xmlRealloc;
Packit 423ecb
    if (strdupFunc != NULL) *strdupFunc = xmlMemStrdup;
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#define bottom_xmlmemory
Packit 423ecb
#include "elfgcchack.h"