mirror of
https://gitlab.gnome.org/GNOME/libxml2.git
synced 2025-01-27 14:03:36 +03:00
threads: Allocate mutexes statically
This commit is contained in:
parent
9ef80ff1a9
commit
65d381f32c
27
dict.c
27
dict.c
@ -24,6 +24,7 @@
|
||||
#include <time.h>
|
||||
|
||||
#include "private/dict.h"
|
||||
#include "private/threads.h"
|
||||
|
||||
/*
|
||||
* Following http://www.ocert.org/advisories/ocert-2011-003.html
|
||||
@ -129,7 +130,7 @@ struct _xmlDict {
|
||||
* A mutex for modifying the reference counter for shared
|
||||
* dictionaries.
|
||||
*/
|
||||
static xmlMutexPtr xmlDictMutex = NULL;
|
||||
static xmlMutex xmlDictMutex;
|
||||
|
||||
#ifdef DICT_RANDOMIZATION
|
||||
#ifdef HAVE_RAND_R
|
||||
@ -155,14 +156,9 @@ int xmlInitializeDict(void) {
|
||||
*
|
||||
* This function is not public
|
||||
* Do the dictionary mutex initialization.
|
||||
*
|
||||
* Returns 0 if initialization was already done, and 1 if that
|
||||
* call led to the initialization
|
||||
*/
|
||||
int __xmlInitializeDict(void) {
|
||||
if ((xmlDictMutex = xmlNewMutex()) == NULL)
|
||||
return(0);
|
||||
xmlMutexLock(xmlDictMutex);
|
||||
xmlInitMutex(&xmlDictMutex);
|
||||
|
||||
#ifdef DICT_RANDOMIZATION
|
||||
#ifdef HAVE_RAND_R
|
||||
@ -172,7 +168,6 @@ int __xmlInitializeDict(void) {
|
||||
srand(time(NULL));
|
||||
#endif
|
||||
#endif
|
||||
xmlMutexUnlock(xmlDictMutex);
|
||||
return(1);
|
||||
}
|
||||
|
||||
@ -180,13 +175,13 @@ int __xmlInitializeDict(void) {
|
||||
int __xmlRandom(void) {
|
||||
int ret;
|
||||
|
||||
xmlMutexLock(xmlDictMutex);
|
||||
xmlMutexLock(&xmlDictMutex);
|
||||
#ifdef HAVE_RAND_R
|
||||
ret = rand_r(& rand_seed);
|
||||
#else
|
||||
ret = rand();
|
||||
#endif
|
||||
xmlMutexUnlock(xmlDictMutex);
|
||||
xmlMutexUnlock(&xmlDictMutex);
|
||||
return(ret);
|
||||
}
|
||||
#endif
|
||||
@ -210,7 +205,7 @@ xmlDictCleanup(void) {
|
||||
*/
|
||||
void
|
||||
xmlCleanupDictInternal(void) {
|
||||
xmlFreeMutex(xmlDictMutex);
|
||||
xmlCleanupMutex(&xmlDictMutex);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -627,9 +622,9 @@ xmlDictCreateSub(xmlDictPtr sub) {
|
||||
int
|
||||
xmlDictReference(xmlDictPtr dict) {
|
||||
if (dict == NULL) return -1;
|
||||
xmlMutexLock(xmlDictMutex);
|
||||
xmlMutexLock(&xmlDictMutex);
|
||||
dict->ref_counter++;
|
||||
xmlMutexUnlock(xmlDictMutex);
|
||||
xmlMutexUnlock(&xmlDictMutex);
|
||||
return(0);
|
||||
}
|
||||
|
||||
@ -787,14 +782,14 @@ xmlDictFree(xmlDictPtr dict) {
|
||||
return;
|
||||
|
||||
/* decrement the counter, it may be shared by a parser and docs */
|
||||
xmlMutexLock(xmlDictMutex);
|
||||
xmlMutexLock(&xmlDictMutex);
|
||||
dict->ref_counter--;
|
||||
if (dict->ref_counter > 0) {
|
||||
xmlMutexUnlock(xmlDictMutex);
|
||||
xmlMutexUnlock(&xmlDictMutex);
|
||||
return;
|
||||
}
|
||||
|
||||
xmlMutexUnlock(xmlDictMutex);
|
||||
xmlMutexUnlock(&xmlDictMutex);
|
||||
|
||||
if (dict->subdict != NULL) {
|
||||
xmlDictFree(dict->subdict);
|
||||
|
96
globals.c
96
globals.c
@ -40,7 +40,7 @@
|
||||
/*
|
||||
* Mutex to protect "ForNewThreads" variables
|
||||
*/
|
||||
static xmlMutexPtr xmlThrDefMutex = NULL;
|
||||
static xmlMutex xmlThrDefMutex;
|
||||
|
||||
/**
|
||||
* xmlInitGlobals:
|
||||
@ -57,8 +57,7 @@ void xmlInitGlobals(void) {
|
||||
* Additional initialisation for multi-threading
|
||||
*/
|
||||
void xmlInitGlobalsInternal(void) {
|
||||
if (xmlThrDefMutex == NULL)
|
||||
xmlThrDefMutex = xmlNewMutex();
|
||||
xmlInitMutex(&xmlThrDefMutex);
|
||||
}
|
||||
|
||||
/************************************************************************
|
||||
@ -508,13 +507,7 @@ xmlInitializeGlobalState(xmlGlobalStatePtr gs)
|
||||
(void *) gs, xmlGetThreadId());
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Perform initialization as required by libxml
|
||||
*/
|
||||
if (xmlThrDefMutex == NULL)
|
||||
xmlInitGlobalsInternal();
|
||||
|
||||
xmlMutexLock(xmlThrDefMutex);
|
||||
xmlMutexLock(&xmlThrDefMutex);
|
||||
|
||||
#if defined(LIBXML_HTML_ENABLED) && defined(LIBXML_LEGACY_ENABLED) && defined(LIBXML_SAX1_ENABLED)
|
||||
inithtmlDefaultSAXHandler(&gs->htmlDefaultSAXHandler);
|
||||
@ -569,7 +562,7 @@ xmlInitializeGlobalState(xmlGlobalStatePtr gs)
|
||||
gs->xmlOutputBufferCreateFilenameValue = xmlOutputBufferCreateFilenameValueThrDef;
|
||||
memset(&gs->xmlLastError, 0, sizeof(xmlError));
|
||||
|
||||
xmlMutexUnlock(xmlThrDefMutex);
|
||||
xmlMutexUnlock(&xmlThrDefMutex);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -591,30 +584,27 @@ void xmlCleanupGlobals(void) {
|
||||
void xmlCleanupGlobalsInternal(void) {
|
||||
xmlResetError(&xmlLastError);
|
||||
|
||||
if (xmlThrDefMutex != NULL) {
|
||||
xmlFreeMutex(xmlThrDefMutex);
|
||||
xmlThrDefMutex = NULL;
|
||||
}
|
||||
xmlCleanupMutex(&xmlThrDefMutex);
|
||||
__xmlGlobalInitMutexDestroy();
|
||||
}
|
||||
|
||||
void
|
||||
xmlThrDefSetGenericErrorFunc(void *ctx, xmlGenericErrorFunc handler) {
|
||||
xmlMutexLock(xmlThrDefMutex);
|
||||
xmlMutexLock(&xmlThrDefMutex);
|
||||
xmlGenericErrorContextThrDef = ctx;
|
||||
if (handler != NULL)
|
||||
xmlGenericErrorThrDef = handler;
|
||||
else
|
||||
xmlGenericErrorThrDef = xmlGenericErrorDefaultFunc;
|
||||
xmlMutexUnlock(xmlThrDefMutex);
|
||||
xmlMutexUnlock(&xmlThrDefMutex);
|
||||
}
|
||||
|
||||
void
|
||||
xmlThrDefSetStructuredErrorFunc(void *ctx, xmlStructuredErrorFunc handler) {
|
||||
xmlMutexLock(xmlThrDefMutex);
|
||||
xmlMutexLock(&xmlThrDefMutex);
|
||||
xmlStructuredErrorContextThrDef = ctx;
|
||||
xmlStructuredErrorThrDef = handler;
|
||||
xmlMutexUnlock(xmlThrDefMutex);
|
||||
xmlMutexUnlock(&xmlThrDefMutex);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -640,12 +630,12 @@ xmlThrDefRegisterNodeDefault(xmlRegisterNodeFunc func)
|
||||
{
|
||||
xmlRegisterNodeFunc old;
|
||||
|
||||
xmlMutexLock(xmlThrDefMutex);
|
||||
xmlMutexLock(&xmlThrDefMutex);
|
||||
old = xmlRegisterNodeDefaultValueThrDef;
|
||||
|
||||
__xmlRegisterCallbacks = 1;
|
||||
xmlRegisterNodeDefaultValueThrDef = func;
|
||||
xmlMutexUnlock(xmlThrDefMutex);
|
||||
xmlMutexUnlock(&xmlThrDefMutex);
|
||||
|
||||
return(old);
|
||||
}
|
||||
@ -673,12 +663,12 @@ xmlThrDefDeregisterNodeDefault(xmlDeregisterNodeFunc func)
|
||||
{
|
||||
xmlDeregisterNodeFunc old;
|
||||
|
||||
xmlMutexLock(xmlThrDefMutex);
|
||||
xmlMutexLock(&xmlThrDefMutex);
|
||||
old = xmlDeregisterNodeDefaultValueThrDef;
|
||||
|
||||
__xmlRegisterCallbacks = 1;
|
||||
xmlDeregisterNodeDefaultValueThrDef = func;
|
||||
xmlMutexUnlock(xmlThrDefMutex);
|
||||
xmlMutexUnlock(&xmlThrDefMutex);
|
||||
|
||||
return(old);
|
||||
}
|
||||
@ -688,14 +678,14 @@ xmlThrDefParserInputBufferCreateFilenameDefault(xmlParserInputBufferCreateFilena
|
||||
{
|
||||
xmlParserInputBufferCreateFilenameFunc old;
|
||||
|
||||
xmlMutexLock(xmlThrDefMutex);
|
||||
xmlMutexLock(&xmlThrDefMutex);
|
||||
old = xmlParserInputBufferCreateFilenameValueThrDef;
|
||||
if (old == NULL) {
|
||||
old = __xmlParserInputBufferCreateFilename;
|
||||
}
|
||||
|
||||
xmlParserInputBufferCreateFilenameValueThrDef = func;
|
||||
xmlMutexUnlock(xmlThrDefMutex);
|
||||
xmlMutexUnlock(&xmlThrDefMutex);
|
||||
|
||||
return(old);
|
||||
}
|
||||
@ -705,7 +695,7 @@ xmlThrDefOutputBufferCreateFilenameDefault(xmlOutputBufferCreateFilenameFunc fun
|
||||
{
|
||||
xmlOutputBufferCreateFilenameFunc old;
|
||||
|
||||
xmlMutexLock(xmlThrDefMutex);
|
||||
xmlMutexLock(&xmlThrDefMutex);
|
||||
old = xmlOutputBufferCreateFilenameValueThrDef;
|
||||
#ifdef LIBXML_OUTPUT_ENABLED
|
||||
if (old == NULL) {
|
||||
@ -713,7 +703,7 @@ xmlThrDefOutputBufferCreateFilenameDefault(xmlOutputBufferCreateFilenameFunc fun
|
||||
}
|
||||
#endif
|
||||
xmlOutputBufferCreateFilenameValueThrDef = func;
|
||||
xmlMutexUnlock(xmlThrDefMutex);
|
||||
xmlMutexUnlock(&xmlThrDefMutex);
|
||||
|
||||
return(old);
|
||||
}
|
||||
@ -816,10 +806,10 @@ __xmlBufferAllocScheme(void) {
|
||||
}
|
||||
xmlBufferAllocationScheme xmlThrDefBufferAllocScheme(xmlBufferAllocationScheme v) {
|
||||
xmlBufferAllocationScheme ret;
|
||||
xmlMutexLock(xmlThrDefMutex);
|
||||
xmlMutexLock(&xmlThrDefMutex);
|
||||
ret = xmlBufferAllocSchemeThrDef;
|
||||
xmlBufferAllocSchemeThrDef = v;
|
||||
xmlMutexUnlock(xmlThrDefMutex);
|
||||
xmlMutexUnlock(&xmlThrDefMutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -833,10 +823,10 @@ __xmlDefaultBufferSize(void) {
|
||||
}
|
||||
int xmlThrDefDefaultBufferSize(int v) {
|
||||
int ret;
|
||||
xmlMutexLock(xmlThrDefMutex);
|
||||
xmlMutexLock(&xmlThrDefMutex);
|
||||
ret = xmlDefaultBufferSizeThrDef;
|
||||
xmlDefaultBufferSizeThrDef = v;
|
||||
xmlMutexUnlock(xmlThrDefMutex);
|
||||
xmlMutexUnlock(&xmlThrDefMutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -870,10 +860,10 @@ __xmlDoValidityCheckingDefaultValue(void) {
|
||||
}
|
||||
int xmlThrDefDoValidityCheckingDefaultValue(int v) {
|
||||
int ret;
|
||||
xmlMutexLock(xmlThrDefMutex);
|
||||
xmlMutexLock(&xmlThrDefMutex);
|
||||
ret = xmlDoValidityCheckingDefaultValueThrDef;
|
||||
xmlDoValidityCheckingDefaultValueThrDef = v;
|
||||
xmlMutexUnlock(xmlThrDefMutex);
|
||||
xmlMutexUnlock(&xmlThrDefMutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -923,10 +913,10 @@ __xmlGetWarningsDefaultValue(void) {
|
||||
}
|
||||
int xmlThrDefGetWarningsDefaultValue(int v) {
|
||||
int ret;
|
||||
xmlMutexLock(xmlThrDefMutex);
|
||||
xmlMutexLock(&xmlThrDefMutex);
|
||||
ret = xmlGetWarningsDefaultValueThrDef;
|
||||
xmlGetWarningsDefaultValueThrDef = v;
|
||||
xmlMutexUnlock(xmlThrDefMutex);
|
||||
xmlMutexUnlock(&xmlThrDefMutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -940,10 +930,10 @@ __xmlIndentTreeOutput(void) {
|
||||
}
|
||||
int xmlThrDefIndentTreeOutput(int v) {
|
||||
int ret;
|
||||
xmlMutexLock(xmlThrDefMutex);
|
||||
xmlMutexLock(&xmlThrDefMutex);
|
||||
ret = xmlIndentTreeOutputThrDef;
|
||||
xmlIndentTreeOutputThrDef = v;
|
||||
xmlMutexUnlock(xmlThrDefMutex);
|
||||
xmlMutexUnlock(&xmlThrDefMutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -957,10 +947,10 @@ __xmlTreeIndentString(void) {
|
||||
}
|
||||
const char * xmlThrDefTreeIndentString(const char * v) {
|
||||
const char * ret;
|
||||
xmlMutexLock(xmlThrDefMutex);
|
||||
xmlMutexLock(&xmlThrDefMutex);
|
||||
ret = xmlTreeIndentStringThrDef;
|
||||
xmlTreeIndentStringThrDef = v;
|
||||
xmlMutexUnlock(xmlThrDefMutex);
|
||||
xmlMutexUnlock(&xmlThrDefMutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -974,10 +964,10 @@ __xmlKeepBlanksDefaultValue(void) {
|
||||
}
|
||||
int xmlThrDefKeepBlanksDefaultValue(int v) {
|
||||
int ret;
|
||||
xmlMutexLock(xmlThrDefMutex);
|
||||
xmlMutexLock(&xmlThrDefMutex);
|
||||
ret = xmlKeepBlanksDefaultValueThrDef;
|
||||
xmlKeepBlanksDefaultValueThrDef = v;
|
||||
xmlMutexUnlock(xmlThrDefMutex);
|
||||
xmlMutexUnlock(&xmlThrDefMutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -991,10 +981,10 @@ __xmlLineNumbersDefaultValue(void) {
|
||||
}
|
||||
int xmlThrDefLineNumbersDefaultValue(int v) {
|
||||
int ret;
|
||||
xmlMutexLock(xmlThrDefMutex);
|
||||
xmlMutexLock(&xmlThrDefMutex);
|
||||
ret = xmlLineNumbersDefaultValueThrDef;
|
||||
xmlLineNumbersDefaultValueThrDef = v;
|
||||
xmlMutexUnlock(xmlThrDefMutex);
|
||||
xmlMutexUnlock(&xmlThrDefMutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -1008,10 +998,10 @@ __xmlLoadExtDtdDefaultValue(void) {
|
||||
}
|
||||
int xmlThrDefLoadExtDtdDefaultValue(int v) {
|
||||
int ret;
|
||||
xmlMutexLock(xmlThrDefMutex);
|
||||
xmlMutexLock(&xmlThrDefMutex);
|
||||
ret = xmlLoadExtDtdDefaultValueThrDef;
|
||||
xmlLoadExtDtdDefaultValueThrDef = v;
|
||||
xmlMutexUnlock(xmlThrDefMutex);
|
||||
xmlMutexUnlock(&xmlThrDefMutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -1025,10 +1015,10 @@ __xmlParserDebugEntities(void) {
|
||||
}
|
||||
int xmlThrDefParserDebugEntities(int v) {
|
||||
int ret;
|
||||
xmlMutexLock(xmlThrDefMutex);
|
||||
xmlMutexLock(&xmlThrDefMutex);
|
||||
ret = xmlParserDebugEntitiesThrDef;
|
||||
xmlParserDebugEntitiesThrDef = v;
|
||||
xmlMutexUnlock(xmlThrDefMutex);
|
||||
xmlMutexUnlock(&xmlThrDefMutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -1051,10 +1041,10 @@ __xmlPedanticParserDefaultValue(void) {
|
||||
}
|
||||
int xmlThrDefPedanticParserDefaultValue(int v) {
|
||||
int ret;
|
||||
xmlMutexLock(xmlThrDefMutex);
|
||||
xmlMutexLock(&xmlThrDefMutex);
|
||||
ret = xmlPedanticParserDefaultValueThrDef;
|
||||
xmlPedanticParserDefaultValueThrDef = v;
|
||||
xmlMutexUnlock(xmlThrDefMutex);
|
||||
xmlMutexUnlock(&xmlThrDefMutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -1068,10 +1058,10 @@ __xmlSaveNoEmptyTags(void) {
|
||||
}
|
||||
int xmlThrDefSaveNoEmptyTags(int v) {
|
||||
int ret;
|
||||
xmlMutexLock(xmlThrDefMutex);
|
||||
xmlMutexLock(&xmlThrDefMutex);
|
||||
ret = xmlSaveNoEmptyTagsThrDef;
|
||||
xmlSaveNoEmptyTagsThrDef = v;
|
||||
xmlMutexUnlock(xmlThrDefMutex);
|
||||
xmlMutexUnlock(&xmlThrDefMutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -1085,10 +1075,10 @@ __xmlSubstituteEntitiesDefaultValue(void) {
|
||||
}
|
||||
int xmlThrDefSubstituteEntitiesDefaultValue(int v) {
|
||||
int ret;
|
||||
xmlMutexLock(xmlThrDefMutex);
|
||||
xmlMutexLock(&xmlThrDefMutex);
|
||||
ret = xmlSubstituteEntitiesDefaultValueThrDef;
|
||||
xmlSubstituteEntitiesDefaultValueThrDef = v;
|
||||
xmlMutexUnlock(xmlThrDefMutex);
|
||||
xmlMutexUnlock(&xmlThrDefMutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,35 @@
|
||||
#ifndef XML_THREADS_H_PRIVATE__
|
||||
#define XML_THREADS_H_PRIVATE__
|
||||
|
||||
#include <libxml/threads.h>
|
||||
|
||||
#ifdef LIBXML_THREAD_ENABLED
|
||||
#ifdef HAVE_PTHREAD_H
|
||||
#include <pthread.h>
|
||||
#define HAVE_POSIX_THREADS
|
||||
#elif defined(_WIN32)
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#ifndef HAVE_COMPILER_TLS
|
||||
#include <process.h>
|
||||
#endif
|
||||
#define HAVE_WIN32_THREADS
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/*
|
||||
* xmlMutex are a simple mutual exception locks
|
||||
*/
|
||||
struct _xmlMutex {
|
||||
#ifdef HAVE_POSIX_THREADS
|
||||
pthread_mutex_t lock;
|
||||
#elif defined HAVE_WIN32_THREADS
|
||||
CRITICAL_SECTION cs;
|
||||
#else
|
||||
int empty;
|
||||
#endif
|
||||
};
|
||||
|
||||
void __xmlGlobalInitMutexLock(void);
|
||||
void __xmlGlobalInitMutexUnlock(void);
|
||||
void __xmlGlobalInitMutexDestroy(void);
|
||||
@ -8,4 +37,7 @@ void __xmlGlobalInitMutexDestroy(void);
|
||||
void xmlInitThreadsInternal(void);
|
||||
void xmlCleanupThreadsInternal(void);
|
||||
|
||||
void xmlInitMutex(xmlMutexPtr mutex);
|
||||
void xmlCleanupMutex(xmlMutexPtr mutex);
|
||||
|
||||
#endif /* XML_THREADS_H_PRIVATE__ */
|
||||
|
80
threads.c
80
threads.c
@ -16,20 +16,6 @@
|
||||
#include <libxml/threads.h>
|
||||
#include <libxml/globals.h>
|
||||
|
||||
#ifdef LIBXML_THREAD_ENABLED
|
||||
#ifdef HAVE_PTHREAD_H
|
||||
#include <pthread.h>
|
||||
#define HAVE_POSIX_THREADS
|
||||
#elif defined(_WIN32)
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#ifndef HAVE_COMPILER_TLS
|
||||
#include <process.h>
|
||||
#endif
|
||||
#define HAVE_WIN32_THREADS
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(SOLARIS)
|
||||
#include <note.h>
|
||||
#endif
|
||||
@ -78,19 +64,6 @@ static int libxml_is_threaded = 1;
|
||||
* be hosted on allocated blocks needing them for the allocation ...
|
||||
*/
|
||||
|
||||
/*
|
||||
* xmlMutex are a simple mutual exception locks
|
||||
*/
|
||||
struct _xmlMutex {
|
||||
#ifdef HAVE_POSIX_THREADS
|
||||
pthread_mutex_t lock;
|
||||
#elif defined HAVE_WIN32_THREADS
|
||||
CRITICAL_SECTION cs;
|
||||
#else
|
||||
int empty;
|
||||
#endif
|
||||
};
|
||||
|
||||
/*
|
||||
* xmlRMutex are reentrant mutual exception locks
|
||||
*/
|
||||
@ -131,6 +104,24 @@ static volatile LPCRITICAL_SECTION global_init_lock = NULL;
|
||||
|
||||
static xmlRMutexPtr xmlLibraryLock = NULL;
|
||||
|
||||
/**
|
||||
* xmlInitMutex:
|
||||
* @mutex: the mutex
|
||||
*
|
||||
* Initialize a mutex.
|
||||
*/
|
||||
void
|
||||
xmlInitMutex(xmlMutexPtr mutex)
|
||||
{
|
||||
#ifdef HAVE_POSIX_THREADS
|
||||
pthread_mutex_init(&mutex->lock, NULL);
|
||||
#elif defined HAVE_WIN32_THREADS
|
||||
InitializeCriticalSection(&mutex->cs);
|
||||
#else
|
||||
(void) mutex;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* xmlNewMutex:
|
||||
*
|
||||
@ -146,21 +137,33 @@ xmlNewMutex(void)
|
||||
|
||||
if ((tok = malloc(sizeof(xmlMutex))) == NULL)
|
||||
return (NULL);
|
||||
#ifdef HAVE_POSIX_THREADS
|
||||
if (libxml_is_threaded != 0)
|
||||
pthread_mutex_init(&tok->lock, NULL);
|
||||
#elif defined HAVE_WIN32_THREADS
|
||||
InitializeCriticalSection(&tok->cs);
|
||||
#endif
|
||||
xmlInitMutex(tok);
|
||||
return (tok);
|
||||
}
|
||||
|
||||
/**
|
||||
* xmlCleanupMutex:
|
||||
* @mutex: the simple mutex
|
||||
*
|
||||
* Reclaim resources associated with a mutex.
|
||||
*/
|
||||
void
|
||||
xmlCleanupMutex(xmlMutexPtr mutex)
|
||||
{
|
||||
#ifdef HAVE_POSIX_THREADS
|
||||
pthread_mutex_destroy(&mutex->lock);
|
||||
#elif defined HAVE_WIN32_THREADS
|
||||
DeleteCriticalSection(&mutex->cs);
|
||||
#else
|
||||
(void) mutex;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* xmlFreeMutex:
|
||||
* @tok: the simple mutex
|
||||
*
|
||||
* xmlFreeMutex() is used to reclaim resources associated with a libxml2 token
|
||||
* struct.
|
||||
* Free a mutex.
|
||||
*/
|
||||
void
|
||||
xmlFreeMutex(xmlMutexPtr tok)
|
||||
@ -168,12 +171,7 @@ xmlFreeMutex(xmlMutexPtr tok)
|
||||
if (tok == NULL)
|
||||
return;
|
||||
|
||||
#ifdef HAVE_POSIX_THREADS
|
||||
if (libxml_is_threaded != 0)
|
||||
pthread_mutex_destroy(&tok->lock);
|
||||
#elif defined HAVE_WIN32_THREADS
|
||||
DeleteCriticalSection(&tok->cs);
|
||||
#endif
|
||||
xmlCleanupMutex(tok);
|
||||
free(tok);
|
||||
}
|
||||
|
||||
|
60
xmlmemory.c
60
xmlmemory.c
@ -32,11 +32,12 @@
|
||||
#include <libxml/threads.h>
|
||||
|
||||
#include "private/memory.h"
|
||||
#include "private/threads.h"
|
||||
|
||||
static unsigned long debugMemSize = 0;
|
||||
static unsigned long debugMemBlocks = 0;
|
||||
static unsigned long debugMaxMemSize = 0;
|
||||
static xmlMutexPtr xmlMemMutex = NULL;
|
||||
static xmlMutex xmlMemMutex;
|
||||
|
||||
void xmlMallocBreakpoint(void);
|
||||
|
||||
@ -176,7 +177,7 @@ xmlMallocLoc(size_t size, const char * file, int line)
|
||||
p->mh_type = MALLOC_TYPE;
|
||||
p->mh_file = file;
|
||||
p->mh_line = line;
|
||||
xmlMutexLock(xmlMemMutex);
|
||||
xmlMutexLock(&xmlMemMutex);
|
||||
p->mh_number = ++block;
|
||||
debugMemSize += size;
|
||||
debugMemBlocks++;
|
||||
@ -184,7 +185,7 @@ xmlMallocLoc(size_t size, const char * file, int line)
|
||||
#ifdef MEM_LIST
|
||||
debugmem_list_add(p);
|
||||
#endif
|
||||
xmlMutexUnlock(xmlMemMutex);
|
||||
xmlMutexUnlock(&xmlMemMutex);
|
||||
|
||||
#ifdef DEBUG_MEMORY
|
||||
xmlGenericError(xmlGenericErrorContext,
|
||||
@ -252,7 +253,7 @@ xmlMallocAtomicLoc(size_t size, const char * file, int line)
|
||||
p->mh_type = MALLOC_ATOMIC_TYPE;
|
||||
p->mh_file = file;
|
||||
p->mh_line = line;
|
||||
xmlMutexLock(xmlMemMutex);
|
||||
xmlMutexLock(&xmlMemMutex);
|
||||
p->mh_number = ++block;
|
||||
debugMemSize += size;
|
||||
debugMemBlocks++;
|
||||
@ -260,7 +261,7 @@ xmlMallocAtomicLoc(size_t size, const char * file, int line)
|
||||
#ifdef MEM_LIST
|
||||
debugmem_list_add(p);
|
||||
#endif
|
||||
xmlMutexUnlock(xmlMemMutex);
|
||||
xmlMutexUnlock(&xmlMemMutex);
|
||||
|
||||
#ifdef DEBUG_MEMORY
|
||||
xmlGenericError(xmlGenericErrorContext,
|
||||
@ -332,7 +333,7 @@ xmlReallocLoc(void *ptr,size_t size, const char * file, int line)
|
||||
goto error;
|
||||
}
|
||||
p->mh_tag = ~MEMTAG;
|
||||
xmlMutexLock(xmlMemMutex);
|
||||
xmlMutexLock(&xmlMemMutex);
|
||||
debugMemSize -= p->mh_size;
|
||||
debugMemBlocks--;
|
||||
#ifdef DEBUG_MEMORY
|
||||
@ -341,7 +342,7 @@ xmlReallocLoc(void *ptr,size_t size, const char * file, int line)
|
||||
#ifdef MEM_LIST
|
||||
debugmem_list_delete(p);
|
||||
#endif
|
||||
xmlMutexUnlock(xmlMemMutex);
|
||||
xmlMutexUnlock(&xmlMemMutex);
|
||||
|
||||
if (size > (MAX_SIZE_T - RESERVE_SIZE)) {
|
||||
xmlGenericError(xmlGenericErrorContext,
|
||||
@ -369,14 +370,14 @@ xmlReallocLoc(void *ptr,size_t size, const char * file, int line)
|
||||
p->mh_size = size;
|
||||
p->mh_file = file;
|
||||
p->mh_line = line;
|
||||
xmlMutexLock(xmlMemMutex);
|
||||
xmlMutexLock(&xmlMemMutex);
|
||||
debugMemSize += size;
|
||||
debugMemBlocks++;
|
||||
if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize;
|
||||
#ifdef MEM_LIST
|
||||
debugmem_list_add(p);
|
||||
#endif
|
||||
xmlMutexUnlock(xmlMemMutex);
|
||||
xmlMutexUnlock(&xmlMemMutex);
|
||||
|
||||
TEST_POINT
|
||||
|
||||
@ -447,7 +448,7 @@ xmlMemFree(void *ptr)
|
||||
if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint();
|
||||
p->mh_tag = ~MEMTAG;
|
||||
memset(target, -1, p->mh_size);
|
||||
xmlMutexLock(xmlMemMutex);
|
||||
xmlMutexLock(&xmlMemMutex);
|
||||
debugMemSize -= p->mh_size;
|
||||
debugMemBlocks--;
|
||||
#ifdef DEBUG_MEMORY
|
||||
@ -456,7 +457,7 @@ xmlMemFree(void *ptr)
|
||||
#ifdef MEM_LIST
|
||||
debugmem_list_delete(p);
|
||||
#endif
|
||||
xmlMutexUnlock(xmlMemMutex);
|
||||
xmlMutexUnlock(&xmlMemMutex);
|
||||
|
||||
free(p);
|
||||
|
||||
@ -513,7 +514,7 @@ xmlMemStrdupLoc(const char *str, const char *file, int line)
|
||||
p->mh_type = STRDUP_TYPE;
|
||||
p->mh_file = file;
|
||||
p->mh_line = line;
|
||||
xmlMutexLock(xmlMemMutex);
|
||||
xmlMutexLock(&xmlMemMutex);
|
||||
p->mh_number = ++block;
|
||||
debugMemSize += size;
|
||||
debugMemBlocks++;
|
||||
@ -521,7 +522,7 @@ xmlMemStrdupLoc(const char *str, const char *file, int line)
|
||||
#ifdef MEM_LIST
|
||||
debugmem_list_add(p);
|
||||
#endif
|
||||
xmlMutexUnlock(xmlMemMutex);
|
||||
xmlMutexUnlock(&xmlMemMutex);
|
||||
|
||||
s = (char *) HDR_2_CLIENT(p);
|
||||
|
||||
@ -569,9 +570,9 @@ int
|
||||
xmlMemUsed(void) {
|
||||
int res;
|
||||
|
||||
xmlMutexLock(xmlMemMutex);
|
||||
xmlMutexLock(&xmlMemMutex);
|
||||
res = debugMemSize;
|
||||
xmlMutexUnlock(xmlMemMutex);
|
||||
xmlMutexUnlock(&xmlMemMutex);
|
||||
return(res);
|
||||
}
|
||||
|
||||
@ -587,9 +588,9 @@ int
|
||||
xmlMemBlocks(void) {
|
||||
int res;
|
||||
|
||||
xmlMutexLock(xmlMemMutex);
|
||||
xmlMutexLock(&xmlMemMutex);
|
||||
res = debugMemBlocks;
|
||||
xmlMutexUnlock(xmlMemMutex);
|
||||
xmlMutexUnlock(&xmlMemMutex);
|
||||
return(res);
|
||||
}
|
||||
|
||||
@ -689,7 +690,7 @@ xmlMemDisplayLast(FILE *fp, long nbBytes)
|
||||
nbBytes, debugMemSize, debugMaxMemSize);
|
||||
fprintf(fp,"BLOCK NUMBER SIZE TYPE\n");
|
||||
idx = 0;
|
||||
xmlMutexLock(xmlMemMutex);
|
||||
xmlMutexLock(&xmlMemMutex);
|
||||
p = memlist;
|
||||
while ((p) && (nbBytes > 0)) {
|
||||
fprintf(fp,"%-5u %6lu %6lu ",idx++,p->mh_number,
|
||||
@ -702,7 +703,7 @@ xmlMemDisplayLast(FILE *fp, long nbBytes)
|
||||
case REALLOC_ATOMIC_TYPE:fprintf(fp,"atomicrealloc() in ");break;
|
||||
default:
|
||||
fprintf(fp,"Unknown memory block, may be corrupted");
|
||||
xmlMutexUnlock(xmlMemMutex);
|
||||
xmlMutexUnlock(&xmlMemMutex);
|
||||
if (old_fp == NULL)
|
||||
fclose(fp);
|
||||
return;
|
||||
@ -720,7 +721,7 @@ xmlMemDisplayLast(FILE *fp, long nbBytes)
|
||||
nbBytes -= (unsigned long)p->mh_size;
|
||||
p = p->mh_next;
|
||||
}
|
||||
xmlMutexUnlock(xmlMemMutex);
|
||||
xmlMutexUnlock(&xmlMemMutex);
|
||||
#else
|
||||
fprintf(fp,"Memory list not compiled (MEM_LIST not defined !)\n");
|
||||
#endif
|
||||
@ -766,7 +767,7 @@ xmlMemDisplay(FILE *fp)
|
||||
debugMemSize, debugMaxMemSize);
|
||||
fprintf(fp,"BLOCK NUMBER SIZE TYPE\n");
|
||||
idx = 0;
|
||||
xmlMutexLock(xmlMemMutex);
|
||||
xmlMutexLock(&xmlMemMutex);
|
||||
p = memlist;
|
||||
while (p) {
|
||||
fprintf(fp,"%-5u %6lu %6lu ",idx++,p->mh_number,
|
||||
@ -779,7 +780,7 @@ xmlMemDisplay(FILE *fp)
|
||||
case REALLOC_ATOMIC_TYPE:fprintf(fp,"atomicrealloc() in ");break;
|
||||
default:
|
||||
fprintf(fp,"Unknown memory block, may be corrupted");
|
||||
xmlMutexUnlock(xmlMemMutex);
|
||||
xmlMutexUnlock(&xmlMemMutex);
|
||||
if (old_fp == NULL)
|
||||
fclose(fp);
|
||||
return;
|
||||
@ -796,7 +797,7 @@ xmlMemDisplay(FILE *fp)
|
||||
fprintf(fp,"\n");
|
||||
p = p->mh_next;
|
||||
}
|
||||
xmlMutexUnlock(xmlMemMutex);
|
||||
xmlMutexUnlock(&xmlMemMutex);
|
||||
#else
|
||||
fprintf(fp,"Memory list not compiled (MEM_LIST not defined !)\n");
|
||||
#endif
|
||||
@ -873,7 +874,7 @@ xmlMemShow(FILE *fp, int nr ATTRIBUTE_UNUSED)
|
||||
fprintf(fp," MEMORY ALLOCATED : %lu, MAX was %lu\n",
|
||||
debugMemSize, debugMaxMemSize);
|
||||
#ifdef MEM_LIST
|
||||
xmlMutexLock(xmlMemMutex);
|
||||
xmlMutexLock(&xmlMemMutex);
|
||||
if (nr > 0) {
|
||||
fprintf(fp,"NUMBER SIZE TYPE WHERE\n");
|
||||
p = memlist;
|
||||
@ -897,7 +898,7 @@ xmlMemShow(FILE *fp, int nr ATTRIBUTE_UNUSED)
|
||||
p = p->mh_next;
|
||||
}
|
||||
}
|
||||
xmlMutexUnlock(xmlMemMutex);
|
||||
xmlMutexUnlock(&xmlMemMutex);
|
||||
#endif /* MEM_LIST */
|
||||
}
|
||||
|
||||
@ -958,11 +959,7 @@ xmlInitMemoryInternal(void) {
|
||||
xmlGenericError(xmlGenericErrorContext,
|
||||
"xmlInitMemory()\n");
|
||||
#endif
|
||||
/*
|
||||
This is really not good code (see Bug 130419). Suggestions for
|
||||
improvement will be welcome!
|
||||
*/
|
||||
xmlMemMutex = xmlNewMutex();
|
||||
xmlInitMutex(&xmlMemMutex);
|
||||
|
||||
breakpoint = getenv("XML_MEM_BREAKPOINT");
|
||||
if (breakpoint != NULL) {
|
||||
@ -1004,8 +1001,7 @@ xmlCleanupMemoryInternal(void) {
|
||||
"xmlCleanupMemory()\n");
|
||||
#endif
|
||||
|
||||
xmlFreeMutex(xmlMemMutex);
|
||||
xmlMemMutex = NULL;
|
||||
xmlCleanupMutex(&xmlMemMutex);
|
||||
#ifdef DEBUG_MEMORY
|
||||
xmlGenericError(xmlGenericErrorContext,
|
||||
"xmlCleanupMemory() Ok\n");
|
||||
|
Loading…
x
Reference in New Issue
Block a user