mirror of
https://gitlab.gnome.org/GNOME/libxml2.git
synced 2025-10-26 00:37:43 +03:00
check some malloc returns with Ashwin patch, add error messages and
* threads.c: check some malloc returns with Ashwin patch, add error messages and reindent the module. Daniel svn path=/trunk/; revision=3709
This commit is contained in:
@@ -1,3 +1,8 @@
|
|||||||
|
Tue Mar 18 09:23:05 CET 2008 Daniel Veillard <daniel@veillard.com>
|
||||||
|
|
||||||
|
* threads.c: check some malloc returns with Ashwin patch, add
|
||||||
|
error messages and reindent the module.
|
||||||
|
|
||||||
Fri Mar 14 15:28:43 CET 2008 Daniel Veillard <daniel@veillard.com>
|
Fri Mar 14 15:28:43 CET 2008 Daniel Veillard <daniel@veillard.com>
|
||||||
|
|
||||||
* xmlreader.c: patch from Ashwin removing duplicate tests
|
* xmlreader.c: patch from Ashwin removing duplicate tests
|
||||||
|
|||||||
93
threads.c
93
threads.c
@@ -129,6 +129,7 @@ struct _xmlRMutex {
|
|||||||
int empty;
|
int empty;
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This module still has some internal static data.
|
* This module still has some internal static data.
|
||||||
* - xmlLibraryLock a global lock
|
* - xmlLibraryLock a global lock
|
||||||
@@ -148,12 +149,12 @@ static __declspec(thread) int tlstate_inited = 0;
|
|||||||
static DWORD globalkey = TLS_OUT_OF_INDEXES;
|
static DWORD globalkey = TLS_OUT_OF_INDEXES;
|
||||||
#endif /* HAVE_COMPILER_TLS */
|
#endif /* HAVE_COMPILER_TLS */
|
||||||
static DWORD mainthread;
|
static DWORD mainthread;
|
||||||
static struct
|
static struct {
|
||||||
{
|
|
||||||
DWORD done;
|
DWORD done;
|
||||||
DWORD control;
|
DWORD control;
|
||||||
} run_once = { 0, 0};
|
} run_once = { 0, 0};
|
||||||
static volatile LPCRITICAL_SECTION global_init_lock = NULL;
|
static volatile LPCRITICAL_SECTION global_init_lock = NULL;
|
||||||
|
|
||||||
/* endif HAVE_WIN32_THREADS */
|
/* endif HAVE_WIN32_THREADS */
|
||||||
#elif defined HAVE_BEOS_THREADS
|
#elif defined HAVE_BEOS_THREADS
|
||||||
int32 globalkey = 0;
|
int32 globalkey = 0;
|
||||||
@@ -164,6 +165,7 @@ static vint32 global_init_count = 0;
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
static xmlRMutexPtr xmlLibraryLock = NULL;
|
static xmlRMutexPtr xmlLibraryLock = NULL;
|
||||||
|
|
||||||
#ifdef LIBXML_THREAD_ENABLED
|
#ifdef LIBXML_THREAD_ENABLED
|
||||||
static void xmlOnceInit(void);
|
static void xmlOnceInit(void);
|
||||||
#endif
|
#endif
|
||||||
@@ -208,7 +210,8 @@ xmlNewMutex(void)
|
|||||||
void
|
void
|
||||||
xmlFreeMutex(xmlMutexPtr tok)
|
xmlFreeMutex(xmlMutexPtr tok)
|
||||||
{
|
{
|
||||||
if (tok == NULL) return;
|
if (tok == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
#ifdef HAVE_PTHREAD_H
|
#ifdef HAVE_PTHREAD_H
|
||||||
if (libxml_is_threaded != 0)
|
if (libxml_is_threaded != 0)
|
||||||
@@ -240,7 +243,8 @@ xmlMutexLock(xmlMutexPtr tok)
|
|||||||
#elif defined HAVE_BEOS_THREADS
|
#elif defined HAVE_BEOS_THREADS
|
||||||
if (acquire_sem(tok->sem) != B_NO_ERROR) {
|
if (acquire_sem(tok->sem) != B_NO_ERROR) {
|
||||||
#ifdef DEBUG_THREADS
|
#ifdef DEBUG_THREADS
|
||||||
xmlGenericError(xmlGenericErrorContext, "xmlMutexLock():BeOS:Couldn't aquire semaphore\n");
|
xmlGenericError(xmlGenericErrorContext,
|
||||||
|
"xmlMutexLock():BeOS:Couldn't aquire semaphore\n");
|
||||||
exit();
|
exit();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -436,13 +440,19 @@ __xmlGlobalInitMutexLock(void)
|
|||||||
/* Create a new critical section */
|
/* Create a new critical section */
|
||||||
if (global_init_lock == NULL) {
|
if (global_init_lock == NULL) {
|
||||||
cs = malloc(sizeof(CRITICAL_SECTION));
|
cs = malloc(sizeof(CRITICAL_SECTION));
|
||||||
|
if (cs == NULL) {
|
||||||
|
xmlGenericError(xmlGenericErrorContext,
|
||||||
|
"xmlGlobalInitMutexLock: out of memory\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
InitializeCriticalSection(cs);
|
InitializeCriticalSection(cs);
|
||||||
|
|
||||||
/* Swap it into the global_init_lock */
|
/* Swap it into the global_init_lock */
|
||||||
#ifdef InterlockedCompareExchangePointer
|
#ifdef InterlockedCompareExchangePointer
|
||||||
InterlockedCompareExchangePointer(&global_init_lock, cs, NULL);
|
InterlockedCompareExchangePointer(&global_init_lock, cs, NULL);
|
||||||
#else /* Use older void* version */
|
#else /* Use older void* version */
|
||||||
InterlockedCompareExchange((void **)&global_init_lock, (void *)cs, NULL);
|
InterlockedCompareExchange((void **) &global_init_lock,
|
||||||
|
(void *) cs, NULL);
|
||||||
#endif /* InterlockedCompareExchangePointer */
|
#endif /* InterlockedCompareExchangePointer */
|
||||||
|
|
||||||
/* If another thread successfully recorded its critical
|
/* If another thread successfully recorded its critical
|
||||||
@@ -480,7 +490,8 @@ __xmlGlobalInitMutexLock(void)
|
|||||||
/* Acquire the chosen semaphore */
|
/* Acquire the chosen semaphore */
|
||||||
if (acquire_sem(global_init_lock) != B_NO_ERROR) {
|
if (acquire_sem(global_init_lock) != B_NO_ERROR) {
|
||||||
#ifdef DEBUG_THREADS
|
#ifdef DEBUG_THREADS
|
||||||
xmlGenericError(xmlGenericErrorContext, "xmlGlobalInitMutexLock():BeOS:Couldn't acquire semaphore\n");
|
xmlGenericError(xmlGenericErrorContext,
|
||||||
|
"xmlGlobalInitMutexLock():BeOS:Couldn't acquire semaphore\n");
|
||||||
exit();
|
exit();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -505,11 +516,11 @@ __xmlGlobalInitMutexUnlock(void)
|
|||||||
* Makes sure that the global initialization mutex is destroyed before
|
* Makes sure that the global initialization mutex is destroyed before
|
||||||
* application termination.
|
* application termination.
|
||||||
*/
|
*/
|
||||||
void __xmlGlobalInitMutexDestroy(void)
|
void
|
||||||
|
__xmlGlobalInitMutexDestroy(void)
|
||||||
{
|
{
|
||||||
#if defined HAVE_WIN32_THREADS
|
#if defined HAVE_WIN32_THREADS
|
||||||
if (global_init_lock != NULL)
|
if (global_init_lock != NULL) {
|
||||||
{
|
|
||||||
DeleteCriticalSection(global_init_lock);
|
DeleteCriticalSection(global_init_lock);
|
||||||
free(global_init_lock);
|
free(global_init_lock);
|
||||||
global_init_lock = NULL;
|
global_init_lock = NULL;
|
||||||
@@ -527,6 +538,7 @@ void __xmlGlobalInitMutexDestroy(void)
|
|||||||
#ifdef xmlLastError
|
#ifdef xmlLastError
|
||||||
#undef xmlLastError
|
#undef xmlLastError
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* xmlFreeGlobalState:
|
* xmlFreeGlobalState:
|
||||||
* @state: a thread global state
|
* @state: a thread global state
|
||||||
@@ -559,8 +571,11 @@ xmlNewGlobalState(void)
|
|||||||
xmlGlobalState *gs;
|
xmlGlobalState *gs;
|
||||||
|
|
||||||
gs = malloc(sizeof(xmlGlobalState));
|
gs = malloc(sizeof(xmlGlobalState));
|
||||||
if (gs == NULL)
|
if (gs == NULL) {
|
||||||
|
xmlGenericError(xmlGenericErrorContext,
|
||||||
|
"xmlGetGlobalState: out of memory\n");
|
||||||
return (NULL);
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
memset(gs, 0, sizeof(xmlGlobalState));
|
memset(gs, 0, sizeof(xmlGlobalState));
|
||||||
xmlInitializeGlobalState(gs);
|
xmlInitializeGlobalState(gs);
|
||||||
@@ -572,15 +587,16 @@ xmlNewGlobalState(void)
|
|||||||
#ifdef HAVE_WIN32_THREADS
|
#ifdef HAVE_WIN32_THREADS
|
||||||
#if !defined(HAVE_COMPILER_TLS)
|
#if !defined(HAVE_COMPILER_TLS)
|
||||||
#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
|
#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
|
||||||
typedef struct _xmlGlobalStateCleanupHelperParams
|
typedef struct _xmlGlobalStateCleanupHelperParams {
|
||||||
{
|
|
||||||
HANDLE thread;
|
HANDLE thread;
|
||||||
void *memory;
|
void *memory;
|
||||||
} xmlGlobalStateCleanupHelperParams;
|
} xmlGlobalStateCleanupHelperParams;
|
||||||
|
|
||||||
static void XMLCDECL xmlGlobalStateCleanupHelper (void *p)
|
static void XMLCDECL
|
||||||
|
xmlGlobalStateCleanupHelper(void *p)
|
||||||
{
|
{
|
||||||
xmlGlobalStateCleanupHelperParams *params = (xmlGlobalStateCleanupHelperParams *) p;
|
xmlGlobalStateCleanupHelperParams *params =
|
||||||
|
(xmlGlobalStateCleanupHelperParams *) p;
|
||||||
WaitForSingleObject(params->thread, INFINITE);
|
WaitForSingleObject(params->thread, INFINITE);
|
||||||
CloseHandle(params->thread);
|
CloseHandle(params->thread);
|
||||||
xmlFreeGlobalState(params->memory);
|
xmlFreeGlobalState(params->memory);
|
||||||
@@ -589,8 +605,7 @@ static void XMLCDECL xmlGlobalStateCleanupHelper (void *p)
|
|||||||
}
|
}
|
||||||
#else /* LIBXML_STATIC && !LIBXML_STATIC_FOR_DLL */
|
#else /* LIBXML_STATIC && !LIBXML_STATIC_FOR_DLL */
|
||||||
|
|
||||||
typedef struct _xmlGlobalStateCleanupHelperParams
|
typedef struct _xmlGlobalStateCleanupHelperParams {
|
||||||
{
|
|
||||||
void *memory;
|
void *memory;
|
||||||
struct _xmlGlobalStateCleanupHelperParams *prev;
|
struct _xmlGlobalStateCleanupHelperParams *prev;
|
||||||
struct _xmlGlobalStateCleanupHelperParams *next;
|
struct _xmlGlobalStateCleanupHelperParams *next;
|
||||||
@@ -604,15 +619,18 @@ static CRITICAL_SECTION cleanup_helpers_cs;
|
|||||||
#endif /* HAVE_WIN32_THREADS */
|
#endif /* HAVE_WIN32_THREADS */
|
||||||
|
|
||||||
#if defined HAVE_BEOS_THREADS
|
#if defined HAVE_BEOS_THREADS
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* xmlGlobalStateCleanup:
|
* xmlGlobalStateCleanup:
|
||||||
* @data: unused parameter
|
* @data: unused parameter
|
||||||
*
|
*
|
||||||
* Used for Beos only
|
* Used for Beos only
|
||||||
*/
|
*/
|
||||||
void xmlGlobalStateCleanup(void *data)
|
void
|
||||||
|
xmlGlobalStateCleanup(void *data)
|
||||||
{
|
{
|
||||||
void *globalval = tls_get(globalkey);
|
void *globalval = tls_get(globalkey);
|
||||||
|
|
||||||
if (globalval != NULL)
|
if (globalval != NULL)
|
||||||
xmlFreeGlobalState(globalval);
|
xmlFreeGlobalState(globalval);
|
||||||
}
|
}
|
||||||
@@ -664,11 +682,21 @@ xmlGetGlobalState(void)
|
|||||||
#endif
|
#endif
|
||||||
if (globalval == NULL) {
|
if (globalval == NULL) {
|
||||||
xmlGlobalState *tsd = xmlNewGlobalState();
|
xmlGlobalState *tsd = xmlNewGlobalState();
|
||||||
p = (xmlGlobalStateCleanupHelperParams *) malloc(sizeof(xmlGlobalStateCleanupHelperParams));
|
|
||||||
|
if (tsd == NULL)
|
||||||
|
return(NULL);
|
||||||
|
p = (xmlGlobalStateCleanupHelperParams *)
|
||||||
|
malloc(sizeof(xmlGlobalStateCleanupHelperParams));
|
||||||
|
if (p == NULL) {
|
||||||
|
xmlGenericError(xmlGenericErrorContext,
|
||||||
|
"xmlGetGlobalState: out of memory\n");
|
||||||
|
return(NULL);
|
||||||
|
}
|
||||||
p->memory = tsd;
|
p->memory = tsd;
|
||||||
#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
|
#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
|
||||||
DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
|
DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
|
||||||
GetCurrentProcess(), &p->thread, 0, TRUE, DUPLICATE_SAME_ACCESS);
|
GetCurrentProcess(), &p->thread, 0, TRUE,
|
||||||
|
DUPLICATE_SAME_ACCESS);
|
||||||
TlsSetValue(globalkey, tsd);
|
TlsSetValue(globalkey, tsd);
|
||||||
_beginthread(xmlGlobalStateCleanupHelper, 0, p);
|
_beginthread(xmlGlobalStateCleanupHelper, 0, p);
|
||||||
#else
|
#else
|
||||||
@@ -829,11 +857,12 @@ xmlInitThreads(void)
|
|||||||
(pthread_cond_init != NULL) &&
|
(pthread_cond_init != NULL) &&
|
||||||
(pthread_equal != NULL) &&
|
(pthread_equal != NULL) &&
|
||||||
(pthread_self != NULL) &&
|
(pthread_self != NULL) &&
|
||||||
(pthread_key_create != NULL) &&
|
|
||||||
(pthread_cond_signal != NULL)) {
|
(pthread_cond_signal != NULL)) {
|
||||||
libxml_is_threaded = 1;
|
libxml_is_threaded = 1;
|
||||||
|
|
||||||
/* fprintf(stderr, "Running multithreaded\n"); */
|
/* fprintf(stderr, "Running multithreaded\n"); */
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
/* fprintf(stderr, "Running without multithread\n"); */
|
/* fprintf(stderr, "Running without multithread\n"); */
|
||||||
libxml_is_threaded = 0;
|
libxml_is_threaded = 0;
|
||||||
}
|
}
|
||||||
@@ -856,10 +885,12 @@ xmlCleanupThreads(void)
|
|||||||
#if defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
|
#if defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
|
||||||
if (globalkey != TLS_OUT_OF_INDEXES) {
|
if (globalkey != TLS_OUT_OF_INDEXES) {
|
||||||
xmlGlobalStateCleanupHelperParams *p;
|
xmlGlobalStateCleanupHelperParams *p;
|
||||||
|
|
||||||
EnterCriticalSection(&cleanup_helpers_cs);
|
EnterCriticalSection(&cleanup_helpers_cs);
|
||||||
p = cleanup_helpers_head;
|
p = cleanup_helpers_head;
|
||||||
while (p != NULL) {
|
while (p != NULL) {
|
||||||
xmlGlobalStateCleanupHelperParams *temp = p;
|
xmlGlobalStateCleanupHelperParams *temp = p;
|
||||||
|
|
||||||
p = p->next;
|
p = p->next;
|
||||||
xmlFreeGlobalState(temp->memory);
|
xmlFreeGlobalState(temp->memory);
|
||||||
free(temp);
|
free(temp);
|
||||||
@@ -874,6 +905,7 @@ xmlCleanupThreads(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef LIBXML_THREAD_ENABLED
|
#ifdef LIBXML_THREAD_ENABLED
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* xmlOnceInit
|
* xmlOnceInit
|
||||||
*
|
*
|
||||||
@@ -884,7 +916,8 @@ xmlCleanupThreads(void)
|
|||||||
* details.
|
* details.
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
xmlOnceInit(void) {
|
xmlOnceInit(void)
|
||||||
|
{
|
||||||
#ifdef HAVE_PTHREAD_H
|
#ifdef HAVE_PTHREAD_H
|
||||||
(void) pthread_key_create(&globalkey, xmlFreeGlobalState);
|
(void) pthread_key_create(&globalkey, xmlFreeGlobalState);
|
||||||
mainthread = pthread_self();
|
mainthread = pthread_self();
|
||||||
@@ -892,15 +925,13 @@ xmlOnceInit(void) {
|
|||||||
|
|
||||||
#if defined(HAVE_WIN32_THREADS)
|
#if defined(HAVE_WIN32_THREADS)
|
||||||
if (!run_once.done) {
|
if (!run_once.done) {
|
||||||
if (InterlockedIncrement(&run_once.control) == 1)
|
if (InterlockedIncrement(&run_once.control) == 1) {
|
||||||
{
|
|
||||||
#if !defined(HAVE_COMPILER_TLS)
|
#if !defined(HAVE_COMPILER_TLS)
|
||||||
globalkey = TlsAlloc();
|
globalkey = TlsAlloc();
|
||||||
#endif
|
#endif
|
||||||
mainthread = GetCurrentThreadId();
|
mainthread = GetCurrentThreadId();
|
||||||
run_once.done = 1;
|
run_once.done = 1;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
/* Another thread is working; give up our slice and
|
/* Another thread is working; give up our slice and
|
||||||
* wait until they're done. */
|
* wait until they're done. */
|
||||||
while (!run_once.done)
|
while (!run_once.done)
|
||||||
@@ -933,9 +964,11 @@ xmlOnceInit(void) {
|
|||||||
*/
|
*/
|
||||||
#if defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
|
#if defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
|
||||||
#if defined(LIBXML_STATIC_FOR_DLL)
|
#if defined(LIBXML_STATIC_FOR_DLL)
|
||||||
BOOL XMLCALL xmlDllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
|
BOOL XMLCALL
|
||||||
|
xmlDllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
|
||||||
#else
|
#else
|
||||||
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
|
BOOL WINAPI
|
||||||
|
DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
switch (fdwReason) {
|
switch (fdwReason) {
|
||||||
@@ -943,14 +976,14 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
|
|||||||
if (globalkey != TLS_OUT_OF_INDEXES) {
|
if (globalkey != TLS_OUT_OF_INDEXES) {
|
||||||
xmlGlobalState *globalval = NULL;
|
xmlGlobalState *globalval = NULL;
|
||||||
xmlGlobalStateCleanupHelperParams *p =
|
xmlGlobalStateCleanupHelperParams *p =
|
||||||
(xmlGlobalStateCleanupHelperParams*)TlsGetValue(globalkey);
|
(xmlGlobalStateCleanupHelperParams *)
|
||||||
|
TlsGetValue(globalkey);
|
||||||
globalval = (xmlGlobalState *) (p ? p->memory : NULL);
|
globalval = (xmlGlobalState *) (p ? p->memory : NULL);
|
||||||
if (globalval) {
|
if (globalval) {
|
||||||
xmlFreeGlobalState(globalval);
|
xmlFreeGlobalState(globalval);
|
||||||
TlsSetValue(globalkey, NULL);
|
TlsSetValue(globalkey, NULL);
|
||||||
}
|
}
|
||||||
if (p)
|
if (p) {
|
||||||
{
|
|
||||||
EnterCriticalSection(&cleanup_helpers_cs);
|
EnterCriticalSection(&cleanup_helpers_cs);
|
||||||
if (p == cleanup_helpers_head)
|
if (p == cleanup_helpers_head)
|
||||||
cleanup_helpers_head = p->next;
|
cleanup_helpers_head = p->next;
|
||||||
|
|||||||
Reference in New Issue
Block a user