updated libxml2 to 2.5.10
[TestXSLT.git] / libxml2 / globals.c
index c4eb3f5..aec4873 100644 (file)
@@ -21,6 +21,7 @@
 
 #include <libxml/globals.h>
 #include <libxml/xmlmemory.h>
+#include <libxml/threads.h>
 
 /* #define DEBUG_GLOBALS */
 
 #define IS_MAIN_THREAD 1
 #endif
 
+/*
+ * Mutex to protect "ForNewThreads" variables
+ */
+static xmlMutexPtr xmlThrDefMutex = NULL;
+
+void xmlInitGlobals()
+{
+    xmlThrDefMutex = xmlNewMutex();
+}
+
+void xmlCleanupGlobals()
+{
+    if (xmlThrDefMutex != NULL) {
+       xmlFreeMutex(xmlThrDefMutex);
+       xmlThrDefMutex = NULL;
+    }
+}
+
 /************************************************************************
  *                                                                     *
  *     All the user accessible global variables of the library         *
  * Memory allocation routines
  */
 #if defined(DEBUG_MEMORY_LOCATION) || defined(DEBUG_MEMORY)
+#ifndef _DEBUG_MEMORY_ALLOC_
 extern void xmlMemFree(void *ptr);
 extern void * xmlMemMalloc(size_t size);
 extern void * xmlMemRealloc(void *ptr,size_t size);
 extern char * xmlMemoryStrdup(const char *str);
+#endif
 
 xmlFreeFunc xmlFree = (xmlFreeFunc) xmlMemFree;
 xmlMallocFunc xmlMalloc = (xmlMallocFunc) xmlMemMalloc;
+xmlMallocFunc xmlMallocAtomic = (xmlMallocFunc) xmlMemMalloc;
 xmlReallocFunc xmlRealloc = (xmlReallocFunc) xmlMemRealloc;
 xmlStrdupFunc xmlMemStrdup = (xmlStrdupFunc) xmlMemoryStrdup;
 #else
@@ -70,6 +92,17 @@ xmlFreeFunc xmlFree = (xmlFreeFunc) free;
  */
 xmlMallocFunc xmlMalloc = (xmlMallocFunc) malloc;
 /**
+ * xmlMallocAtomic:
+ * @size:  the size requested in bytes
+ *
+ * The variable holding the libxml malloc() implementation for atomic
+ * data (i.e. blocks not containings pointers), useful when using a
+ * garbage collecting allocator.
+ *
+ * Returns a pointer to the newly allocated block or NULL in case of error
+ */
+xmlMallocFunc xmlMallocAtomic = (xmlMallocFunc) malloc;
+/**
  * xmlRealloc:
  * @mem: an already allocated block of memory
  * @size:  the new size requested in bytes
@@ -120,6 +153,7 @@ xmlStrdupFunc xmlMemStrdup = (xmlStrdupFunc) xmlStrdup;
 
 #undef xmlFree
 #undef xmlMalloc
+#undef xmlMallocAtomic
 #undef xmlMemStrdup
 #undef xmlRealloc
 
@@ -137,12 +171,14 @@ const char *xmlParserVersion = LIBXML_VERSION_STRING;
  * XML_BUFFER_ALLOC_EXACT
  */
 xmlBufferAllocationScheme xmlBufferAllocScheme = XML_BUFFER_ALLOC_EXACT;
+static xmlBufferAllocationScheme xmlBufferAllocSchemeThrDef = XML_BUFFER_ALLOC_EXACT;
 /**
  * xmlDefaultBufferSize:
  *
  * Global setting, default buffer size. Default value is BASE_BUFFER_SIZE
  */
 int xmlDefaultBufferSize = BASE_BUFFER_SIZE;
+static int xmlDefaultBufferSizeThrDef = BASE_BUFFER_SIZE;
 
 /*
  * Parser defaults
@@ -162,6 +198,7 @@ int oldXMLWDcompatibility = 0; /* DEPRECATED */
  * Disabled by default
  */
 int xmlParserDebugEntities = 0;
+static int xmlParserDebugEntitiesThrDef = 0;
 /**
  * xmlDoValidityCheckingDefaultValue:
  *
@@ -169,6 +206,7 @@ int xmlParserDebugEntities = 0;
  * Disabled by default.
  */
 int xmlDoValidityCheckingDefaultValue = 0;
+static int xmlDoValidityCheckingDefaultValueThrDef = 0;
 /**
  * xmlGetWarningsDefaultValue:
  *
@@ -176,6 +214,7 @@ int xmlDoValidityCheckingDefaultValue = 0;
  * Activated by default.
  */
 int xmlGetWarningsDefaultValue = 1;
+static int xmlGetWarningsDefaultValueThrDef = 1;
 /**
  * xmlLoadExtDtdDefaultValue:
  *
@@ -184,6 +223,7 @@ int xmlGetWarningsDefaultValue = 1;
  * Disabled by default.
  */
 int xmlLoadExtDtdDefaultValue = 0;
+static int xmlLoadExtDtdDefaultValueThrDef = 0;
 /**
  * xmlPedanticParserDefaultValue:
  *
@@ -191,6 +231,7 @@ int xmlLoadExtDtdDefaultValue = 0;
  * Disabled by default.
  */
 int xmlPedanticParserDefaultValue = 0;
+static int xmlPedanticParserDefaultValueThrDef = 0;
 /**
  * xmlLineNumbersDefaultValue:
  *
@@ -200,6 +241,7 @@ int xmlPedanticParserDefaultValue = 0;
  * applicaton.
  */
 int xmlLineNumbersDefaultValue = 0;
+static int xmlLineNumbersDefaultValueThrDef = 0;
 /**
  * xmlKeepBlanksDefaultValue:
  *
@@ -210,6 +252,7 @@ int xmlLineNumbersDefaultValue = 0;
  * for some applications since this was libxml1 default behaviour.
  */
 int xmlKeepBlanksDefaultValue = 1;
+static int xmlKeepBlanksDefaultValueThrDef = 1;
 /**
  * xmlSubstituteEntitiesDefaultValue:
  *
@@ -220,9 +263,12 @@ int xmlKeepBlanksDefaultValue = 1;
  * engine does not handle entities references transparently.
  */
 int xmlSubstituteEntitiesDefaultValue = 0;
+static int xmlSubstituteEntitiesDefaultValueThrDef = 0;
 
 xmlRegisterNodeFunc xmlRegisterNodeDefaultValue = NULL;
+static xmlRegisterNodeFunc xmlRegisterNodeDefaultValueThrDef = NULL;
 xmlDeregisterNodeFunc xmlDeregisterNodeDefaultValue = NULL;
+static xmlDeregisterNodeFunc xmlDeregisterNodeDefaultValueThrDef = NULL;
 
 /*
  * Error handling
@@ -239,12 +285,14 @@ void xmlGenericErrorDefaultFunc   (void *ctx ATTRIBUTE_UNUSED,
  * Global setting: function used for generic error callbacks
  */
 xmlGenericErrorFunc xmlGenericError = xmlGenericErrorDefaultFunc;
+static xmlGenericErrorFunc xmlGenericErrorThrDef = xmlGenericErrorDefaultFunc;
 /**
  * xmlGenericErrorContext:
  *
  * Global setting passed to generic error callbacks
  */
 void *xmlGenericErrorContext = NULL;
+static void *xmlGenericErrorContextThrDef = NULL;
 
 /*
  * output defaults
@@ -256,6 +304,7 @@ void *xmlGenericErrorContext = NULL;
  * Enabled by default
  */
 int xmlIndentTreeOutput = 1;
+static int xmlIndentTreeOutputThrDef = 1;
 
 /**
  * xmlTreeIndentString:
@@ -263,6 +312,7 @@ int xmlIndentTreeOutput = 1;
  * The string used to do one-level indent. By default is equal to "  " (two spaces)
  */
 const char *xmlTreeIndentString = "  ";
+static const char *xmlTreeIndentStringThrDef = "  ";
 
 /**
  * xmlSaveNoEmptyTags:
@@ -273,6 +323,7 @@ const char *xmlTreeIndentString = "  ";
  * Disabled by default
  */
 int xmlSaveNoEmptyTags = 0;
+int xmlSaveNoEmptyTagsThrDef = 0;
 
 /**
  * xmlDefaultSAXHandler:
@@ -414,6 +465,7 @@ xmlInitializeGlobalState(xmlGlobalStatePtr gs)
     /*
      * Perform initialization as required by libxml
      */
+    xmlMutexLock(xmlThrDefMutex);
 
 #ifdef LIBXML_DOCB_ENABLED
     initdocbDefaultSAXHandler(&gs->docbDefaultSAXHandler);
@@ -421,43 +473,60 @@ xmlInitializeGlobalState(xmlGlobalStatePtr gs)
 #ifdef LIBXML_HTML_ENABLED
     inithtmlDefaultSAXHandler(&gs->htmlDefaultSAXHandler);
 #endif
-    initGenericErrorDefaultFunc(&gs->xmlGenericError);
 
     gs->oldXMLWDcompatibility = 0;
-    gs->xmlBufferAllocScheme = XML_BUFFER_ALLOC_EXACT;
-    gs->xmlDefaultBufferSize = BASE_BUFFER_SIZE;
+    gs->xmlBufferAllocScheme = xmlBufferAllocSchemeThrDef;
+    gs->xmlDefaultBufferSize = xmlDefaultBufferSizeThrDef;
     initxmlDefaultSAXHandler(&gs->xmlDefaultSAXHandler, 1);
     gs->xmlDefaultSAXLocator.getPublicId = getPublicId;
     gs->xmlDefaultSAXLocator.getSystemId = getSystemId;
     gs->xmlDefaultSAXLocator.getLineNumber = getLineNumber;
     gs->xmlDefaultSAXLocator.getColumnNumber = getColumnNumber;
-    gs->xmlDoValidityCheckingDefaultValue = 0;
+    gs->xmlDoValidityCheckingDefaultValue = 
+         xmlDoValidityCheckingDefaultValueThrDef;
 #if defined(DEBUG_MEMORY_LOCATION) | defined(DEBUG_MEMORY)
     gs->xmlFree = (xmlFreeFunc) xmlMemFree;
     gs->xmlMalloc = (xmlMallocFunc) xmlMemMalloc;
+    gs->xmlMallocAtomic = (xmlMallocFunc) xmlMemMalloc;
     gs->xmlRealloc = (xmlReallocFunc) xmlMemRealloc;
     gs->xmlMemStrdup = (xmlStrdupFunc) xmlMemoryStrdup;
 #else
     gs->xmlFree = (xmlFreeFunc) free;
     gs->xmlMalloc = (xmlMallocFunc) malloc;
+    gs->xmlMallocAtomic = (xmlMallocFunc) malloc;
     gs->xmlRealloc = (xmlReallocFunc) realloc;
     gs->xmlMemStrdup = (xmlStrdupFunc) xmlStrdup;
 #endif
-    gs->xmlGenericErrorContext = NULL;
-    gs->xmlGetWarningsDefaultValue = 1;
-    gs->xmlIndentTreeOutput = 1;
-    gs->xmlTreeIndentString = "  ";
-    gs->xmlKeepBlanksDefaultValue = 1;
-    gs->xmlLineNumbersDefaultValue = 0;
-    gs->xmlLoadExtDtdDefaultValue = 0;
-    gs->xmlParserDebugEntities = 0;
+    gs->xmlGetWarningsDefaultValue = xmlGetWarningsDefaultValueThrDef;
+    gs->xmlIndentTreeOutput = xmlIndentTreeOutputThrDef;
+    gs->xmlTreeIndentString = xmlTreeIndentStringThrDef;
+    gs->xmlKeepBlanksDefaultValue = xmlKeepBlanksDefaultValueThrDef;
+    gs->xmlLineNumbersDefaultValue = xmlLineNumbersDefaultValueThrDef;
+    gs->xmlLoadExtDtdDefaultValue = xmlLoadExtDtdDefaultValueThrDef;
+    gs->xmlParserDebugEntities = xmlParserDebugEntitiesThrDef;
     gs->xmlParserVersion = LIBXML_VERSION_STRING;
-    gs->xmlPedanticParserDefaultValue = 0;
-    gs->xmlSaveNoEmptyTags = 0;
-    gs->xmlSubstituteEntitiesDefaultValue = 0;
+    gs->xmlPedanticParserDefaultValue = xmlPedanticParserDefaultValueThrDef;
+    gs->xmlSaveNoEmptyTags = xmlSaveNoEmptyTagsThrDef;
+    gs->xmlSubstituteEntitiesDefaultValue = 
+        xmlSubstituteEntitiesDefaultValueThrDef;
 
-    gs->xmlRegisterNodeDefaultValue = NULL;
-    gs->xmlDeregisterNodeDefaultValue = NULL;
+    gs->xmlGenericError = xmlGenericErrorThrDef;
+    gs->xmlGenericErrorContext = xmlGenericErrorContextThrDef;
+    gs->xmlRegisterNodeDefaultValue = xmlRegisterNodeDefaultValueThrDef;
+    gs->xmlDeregisterNodeDefaultValue = xmlDeregisterNodeDefaultValueThrDef;
+
+    xmlMutexUnlock(xmlThrDefMutex);
+}
+
+void
+xmlThrDefSetGenericErrorFunc(void *ctx, xmlGenericErrorFunc handler) {
+    xmlMutexLock(xmlThrDefMutex);
+    xmlGenericErrorContextThrDef = ctx;
+    if (handler != NULL)
+       xmlGenericErrorThrDef = handler;
+    else
+       xmlGenericErrorThrDef = xmlGenericErrorDefaultFunc;
+    xmlMutexUnlock(xmlThrDefMutex);
 }
 
 /**
@@ -473,10 +542,26 @@ xmlRegisterNodeDefault(xmlRegisterNodeFunc func)
 {
     xmlRegisterNodeFunc old = xmlRegisterNodeDefaultValue;
     
+    __xmlRegisterCallbacks = 1;
     xmlRegisterNodeDefaultValue = func;
     return(old);
 }
 
+xmlRegisterNodeFunc
+xmlThrDefRegisterNodeDefault(xmlRegisterNodeFunc func)
+{
+    xmlRegisterNodeFunc old;
+    
+    xmlMutexLock(xmlThrDefMutex);
+    old = xmlRegisterNodeDefaultValueThrDef;
+    
+    __xmlRegisterCallbacks = 1;
+    xmlRegisterNodeDefaultValueThrDef = func;
+    xmlMutexUnlock(xmlThrDefMutex);
+
+    return(old);
+}
+
 /**
  * xmlDeregisterNodeDefault:
  * @func: function pointer to the new DeregisterNodeFunc
@@ -490,10 +575,26 @@ xmlDeregisterNodeDefault(xmlDeregisterNodeFunc func)
 {
     xmlDeregisterNodeFunc old = xmlDeregisterNodeDefaultValue;
     
+    __xmlRegisterCallbacks = 1;
     xmlDeregisterNodeDefaultValue = func;
     return(old);
 }
 
+xmlDeregisterNodeFunc
+xmlThrDefDeregisterNodeDefault(xmlDeregisterNodeFunc func)
+{
+    xmlDeregisterNodeFunc old;
+
+    xmlMutexLock(xmlThrDefMutex);
+    old = xmlDeregisterNodeDefaultValueThrDef;
+    
+    __xmlRegisterCallbacks = 1;
+    xmlDeregisterNodeDefaultValueThrDef = func;
+    xmlMutexUnlock(xmlThrDefMutex);
+
+    return(old);
+}
+
 
 #ifdef LIBXML_DOCB_ENABLED
 #undef docbDefaultSAXHandler
@@ -541,6 +642,14 @@ __xmlBufferAllocScheme(void) {
     else
        return (&xmlGetGlobalState()->xmlBufferAllocScheme);
 }
+xmlBufferAllocationScheme xmlThrDefBufferAllocScheme(xmlBufferAllocationScheme v) {
+    xmlBufferAllocationScheme ret;
+    xmlMutexLock(xmlThrDefMutex);
+    ret = xmlBufferAllocSchemeThrDef;
+    xmlBufferAllocSchemeThrDef = v;
+    xmlMutexUnlock(xmlThrDefMutex);
+    return ret;
+}
 
 #undef xmlDefaultBufferSize
 int *
@@ -550,6 +659,14 @@ __xmlDefaultBufferSize(void) {
     else
        return (&xmlGetGlobalState()->xmlDefaultBufferSize);
 }
+int xmlThrDefDefaultBufferSize(int v) {
+    int ret;
+    xmlMutexLock(xmlThrDefMutex);
+    ret = xmlDefaultBufferSizeThrDef;
+    xmlDefaultBufferSizeThrDef = v;
+    xmlMutexUnlock(xmlThrDefMutex);
+    return ret;
+}
 
 #undef xmlDefaultSAXHandler
 xmlSAXHandler *
@@ -577,6 +694,14 @@ __xmlDoValidityCheckingDefaultValue(void) {
     else
        return (&xmlGetGlobalState()->xmlDoValidityCheckingDefaultValue);
 }
+int xmlThrDefDoValidityCheckingDefaultValue(int v) {
+    int ret;
+    xmlMutexLock(xmlThrDefMutex);
+    ret = xmlDoValidityCheckingDefaultValueThrDef;
+    xmlDoValidityCheckingDefaultValueThrDef = v;
+    xmlMutexUnlock(xmlThrDefMutex);
+    return ret;
+}
 
 #undef xmlGenericError
 xmlGenericErrorFunc *
@@ -604,6 +729,14 @@ __xmlGetWarningsDefaultValue(void) {
     else
        return (&xmlGetGlobalState()->xmlGetWarningsDefaultValue);
 }
+int xmlThrDefGetWarningsDefaultValue(int v) {
+    int ret;
+    xmlMutexLock(xmlThrDefMutex);
+    ret = xmlGetWarningsDefaultValueThrDef;
+    xmlGetWarningsDefaultValueThrDef = v;
+    xmlMutexUnlock(xmlThrDefMutex);
+    return ret;
+}
 
 #undef xmlIndentTreeOutput
 int *
@@ -613,6 +746,14 @@ __xmlIndentTreeOutput(void) {
     else
        return (&xmlGetGlobalState()->xmlIndentTreeOutput);
 }
+int xmlThrDefIndentTreeOutput(int v) {
+    int ret;
+    xmlMutexLock(xmlThrDefMutex);
+    ret = xmlIndentTreeOutputThrDef;
+    xmlIndentTreeOutputThrDef = v;
+    xmlMutexUnlock(xmlThrDefMutex);
+    return ret;
+}
 
 #undef xmlTreeIndentString
 const char * *
@@ -622,6 +763,14 @@ __xmlTreeIndentString(void) {
     else
        return (&xmlGetGlobalState()->xmlTreeIndentString);
 }
+const char * xmlThrDefTreeIndentString(const char * v) {
+    const char * ret;
+    xmlMutexLock(xmlThrDefMutex);
+    ret = xmlTreeIndentStringThrDef;
+    xmlTreeIndentStringThrDef = v;
+    xmlMutexUnlock(xmlThrDefMutex);
+    return ret;
+}
 
 #undef xmlKeepBlanksDefaultValue
 int *
@@ -631,6 +780,14 @@ __xmlKeepBlanksDefaultValue(void) {
     else
        return (&xmlGetGlobalState()->xmlKeepBlanksDefaultValue);
 }
+int xmlThrDefKeepBlanksDefaultValue(int v) {
+    int ret;
+    xmlMutexLock(xmlThrDefMutex);
+    ret = xmlKeepBlanksDefaultValueThrDef;
+    xmlKeepBlanksDefaultValueThrDef = v;
+    xmlMutexUnlock(xmlThrDefMutex);
+    return ret;
+}
 
 #undef xmlLineNumbersDefaultValue
 int *
@@ -640,6 +797,14 @@ __xmlLineNumbersDefaultValue(void) {
     else
        return (&xmlGetGlobalState()->xmlLineNumbersDefaultValue);
 }
+int xmlThrDefLineNumbersDefaultValue(int v) {
+    int ret;
+    xmlMutexLock(xmlThrDefMutex);
+    ret = xmlLineNumbersDefaultValueThrDef;
+    xmlLineNumbersDefaultValueThrDef = v;
+    xmlMutexUnlock(xmlThrDefMutex);
+    return ret;
+}
 
 #undef xmlLoadExtDtdDefaultValue
 int *
@@ -649,6 +814,14 @@ __xmlLoadExtDtdDefaultValue(void) {
     else
        return (&xmlGetGlobalState()->xmlLoadExtDtdDefaultValue);
 }
+int xmlThrDefLoadExtDtdDefaultValue(int v) {
+    int ret;
+    xmlMutexLock(xmlThrDefMutex);
+    ret = xmlLoadExtDtdDefaultValueThrDef;
+    xmlLoadExtDtdDefaultValueThrDef = v;
+    xmlMutexUnlock(xmlThrDefMutex);
+    return ret;
+}
 
 #undef xmlParserDebugEntities
 int *
@@ -658,6 +831,14 @@ __xmlParserDebugEntities(void) {
     else
        return (&xmlGetGlobalState()->xmlParserDebugEntities);
 }
+int xmlThrDefParserDebugEntities(int v) {
+    int ret;
+    xmlMutexLock(xmlThrDefMutex);
+    ret = xmlParserDebugEntitiesThrDef;
+    xmlParserDebugEntitiesThrDef = v;
+    xmlMutexUnlock(xmlThrDefMutex);
+    return ret;
+}
 
 #undef xmlParserVersion
 const char * *
@@ -676,6 +857,14 @@ __xmlPedanticParserDefaultValue(void) {
     else
        return (&xmlGetGlobalState()->xmlPedanticParserDefaultValue);
 }
+int xmlThrDefPedanticParserDefaultValue(int v) {
+    int ret;
+    xmlMutexLock(xmlThrDefMutex);
+    ret = xmlPedanticParserDefaultValueThrDef;
+    xmlPedanticParserDefaultValueThrDef = v;
+    xmlMutexUnlock(xmlThrDefMutex);
+    return ret;
+}
 
 #undef xmlSaveNoEmptyTags
 int *
@@ -685,6 +874,14 @@ __xmlSaveNoEmptyTags(void) {
     else
        return (&xmlGetGlobalState()->xmlSaveNoEmptyTags);
 }
+int xmlThrDefSaveNoEmptyTags(int v) {
+    int ret;
+    xmlMutexLock(xmlThrDefMutex);
+    ret = xmlSaveNoEmptyTagsThrDef;
+    xmlSaveNoEmptyTagsThrDef = v;
+    xmlMutexUnlock(xmlThrDefMutex);
+    return ret;
+}
 
 #undef xmlSubstituteEntitiesDefaultValue
 int *
@@ -694,6 +891,14 @@ __xmlSubstituteEntitiesDefaultValue(void) {
     else
        return (&xmlGetGlobalState()->xmlSubstituteEntitiesDefaultValue);
 }
+int xmlThrDefSubstituteEntitiesDefaultValue(int v) {
+    int ret;
+    xmlMutexLock(xmlThrDefMutex);
+    ret = xmlSubstituteEntitiesDefaultValueThrDef;
+    xmlSubstituteEntitiesDefaultValueThrDef = v;
+    xmlMutexUnlock(xmlThrDefMutex);
+    return ret;
+}
 
 #undef xmlRegisterNodeDefaultValue
 xmlRegisterNodeFunc *