1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-01-26 10:03:34 +03:00

enhanced by adding mutex to protect global structures in a multi-threading

* xmlmemory.c: enhanced by adding mutex to protect global
  structures in a multi-threading environment.  This fixed
  some random errors on the Threads regression tests.

* encoding.c, include/libxml/encoding.h: Enhanced the handling of
  UTF-16, UTF-16LE and UTF-16BE encodings.  Now UTF-16 output is
  handled internally by default, with proper BOM and UTF-16LE
  encoding.  Native UTF-16LE and UTF-16BE encoding will not generate
  BOM on output, and will be automatically recognized on input.
* test/utf16lebom.xml, test/utf16bebom.xml, result/utf16?ebom*:
  added regression tests for above.
This commit is contained in:
William M. Brack 2003-11-29 10:47:56 +00:00
parent 2dffb760bf
commit 0622fe8b03
2 changed files with 47 additions and 21 deletions

View File

@ -1,3 +1,9 @@
Sat Nov 29 18:38:22 HKT 2003 William Brack <wbrack@mmm.com.hk>
* xmlmemory.c: enhanced by adding mutex to protect global
structures in a multi-threading environment. This fixed
some random errors on the Threads regression tests.
Fri Nov 28 21:39:49 MST 2003 John Fleck <jfleck@inkstain.net> Fri Nov 28 21:39:49 MST 2003 John Fleck <jfleck@inkstain.net>
* doc/xml.html * doc/xml.html
@ -6,13 +12,13 @@ Fri Nov 28 21:39:49 MST 2003 John Fleck <jfleck@inkstain.net>
Fri Nov 28 17:28:47 HKT 2003 William Brack <wbrack@mmm.com.hk> Fri Nov 28 17:28:47 HKT 2003 William Brack <wbrack@mmm.com.hk>
* encoding.c, include/libxml/encoding.h: Enhanced the handling of UTF-16, * encoding.c, include/libxml/encoding.h: Enhanced the handling of
UTF-16LE and UTF-16BE encodings. Now UTF-16 output is handled internally UTF-16, UTF-16LE and UTF-16BE encodings. Now UTF-16 output is
by default, with proper BOM and UTF-16LE encoding. Native UTF-16LE and handled internally by default, with proper BOM and UTF-16LE
UTF-16BE encoding will not generate a BOM on output, and will be encoding. Native UTF-16LE and UTF-16BE encoding will not generate
automatically recognized on input. BOM on output, and will be automatically recognized on input.
* test/utf16lebom.xml, test/utf16bebom.xml, result/utf16?ebom*: added * test/utf16lebom.xml, test/utf16bebom.xml, result/utf16?ebom*:
regression tests for above. added regression tests for above.
Thu Nov 27 19:25:10 CET 2003 Igor Zlatkovic <igor@zlatkovic.com> Thu Nov 27 19:25:10 CET 2003 Igor Zlatkovic <igor@zlatkovic.com>

View File

@ -45,10 +45,12 @@
#include <libxml/xmlmemory.h> #include <libxml/xmlmemory.h>
#include <libxml/globals.h> #include <libxml/globals.h>
#include <libxml/xmlerror.h> #include <libxml/xmlerror.h>
#include <libxml/threads.h>
static int xmlMemInitialized = 0; static int xmlMemInitialized = 0;
static unsigned long debugMemSize = 0; static unsigned long debugMemSize = 0;
static unsigned long debugMaxMemSize = 0; static unsigned long debugMaxMemSize = 0;
static xmlMutexPtr xmlMemMutex = NULL;
void xmlMallocBreakpoint(void); void xmlMallocBreakpoint(void);
@ -110,8 +112,8 @@ typedef struct memnod {
#define HDR_2_CLIENT(a) ((void *) (((char *) (a)) + RESERVE_SIZE)) #define HDR_2_CLIENT(a) ((void *) (((char *) (a)) + RESERVE_SIZE))
static int block=0; static unsigned int block=0;
static int xmlMemStopAtBlock = 0; static unsigned int xmlMemStopAtBlock = 0;
static void *xmlMemTraceBlockAt = NULL; static void *xmlMemTraceBlockAt = NULL;
#ifdef MEM_LIST #ifdef MEM_LIST
static MEMHDR *memlist = NULL; static MEMHDR *memlist = NULL;
@ -176,23 +178,25 @@ xmlMallocLoc(size_t size, const char * file, int line)
return(NULL); return(NULL);
} }
p->mh_tag = MEMTAG; p->mh_tag = MEMTAG;
p->mh_number = ++block;
p->mh_size = size; p->mh_size = size;
p->mh_type = MALLOC_TYPE; p->mh_type = MALLOC_TYPE;
p->mh_file = file; p->mh_file = file;
p->mh_line = line; p->mh_line = line;
xmlMutexLock(xmlMemMutex);
p->mh_number = ++block;
debugMemSize += size; debugMemSize += size;
if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize; if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize;
#ifdef MEM_LIST #ifdef MEM_LIST
debugmem_list_add(p); debugmem_list_add(p);
#endif #endif
xmlMutexUnlock(xmlMemMutex);
#ifdef DEBUG_MEMORY #ifdef DEBUG_MEMORY
xmlGenericError(xmlGenericErrorContext, xmlGenericError(xmlGenericErrorContext,
"Malloc(%d) Ok\n",size); "Malloc(%d) Ok\n",size);
#endif #endif
if (xmlMemStopAtBlock == block) xmlMallocBreakpoint(); if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint();
ret = HDR_2_CLIENT(p); ret = HDR_2_CLIENT(p);
@ -241,23 +245,25 @@ xmlMallocAtomicLoc(size_t size, const char * file, int line)
return(NULL); return(NULL);
} }
p->mh_tag = MEMTAG; p->mh_tag = MEMTAG;
p->mh_number = ++block;
p->mh_size = size; p->mh_size = size;
p->mh_type = MALLOC_ATOMIC_TYPE; p->mh_type = MALLOC_ATOMIC_TYPE;
p->mh_file = file; p->mh_file = file;
p->mh_line = line; p->mh_line = line;
xmlMutexLock(xmlMemMutex);
p->mh_number = ++block;
debugMemSize += size; debugMemSize += size;
if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize; if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize;
#ifdef MEM_LIST #ifdef MEM_LIST
debugmem_list_add(p); debugmem_list_add(p);
#endif #endif
xmlMutexUnlock(xmlMemMutex);
#ifdef DEBUG_MEMORY #ifdef DEBUG_MEMORY
xmlGenericError(xmlGenericErrorContext, xmlGenericError(xmlGenericErrorContext,
"Malloc(%d) Ok\n",size); "Malloc(%d) Ok\n",size);
#endif #endif
if (xmlMemStopAtBlock == block) xmlMallocBreakpoint(); if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint();
ret = HDR_2_CLIENT(p); ret = HDR_2_CLIENT(p);
@ -316,11 +322,13 @@ xmlReallocLoc(void *ptr,size_t size, const char * file, int line)
goto error; goto error;
} }
p->mh_tag = ~MEMTAG; p->mh_tag = ~MEMTAG;
xmlMutexLock(xmlMemMutex);
debugMemSize -= p->mh_size; debugMemSize -= p->mh_size;
#ifdef MEM_LIST #ifdef MEM_LIST
debugmem_list_delete(p); debugmem_list_delete(p);
#endif #endif
xmlMutexUnlock(xmlMemMutex);
p = (MEMHDR *) realloc(p,RESERVE_SIZE+size); p = (MEMHDR *) realloc(p,RESERVE_SIZE+size);
if (!p) { if (!p) {
goto error; goto error;
@ -337,11 +345,13 @@ xmlReallocLoc(void *ptr,size_t size, const char * file, int line)
p->mh_size = size; p->mh_size = size;
p->mh_file = file; p->mh_file = file;
p->mh_line = line; p->mh_line = line;
xmlMutexLock(xmlMemMutex);
debugMemSize += size; debugMemSize += size;
if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize; if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize;
#ifdef MEM_LIST #ifdef MEM_LIST
debugmem_list_add(p); debugmem_list_add(p);
#endif #endif
xmlMutexUnlock(xmlMemMutex);
TEST_POINT TEST_POINT
@ -400,12 +410,14 @@ xmlMemFree(void *ptr)
goto error; goto error;
} }
p->mh_tag = ~MEMTAG; p->mh_tag = ~MEMTAG;
debugMemSize -= p->mh_size;
memset(target, -1, p->mh_size); memset(target, -1, p->mh_size);
xmlMutexLock(xmlMemMutex);
debugMemSize -= p->mh_size;
#ifdef MEM_LIST #ifdef MEM_LIST
debugmem_list_delete(p); debugmem_list_delete(p);
#endif #endif
xmlMutexUnlock(xmlMemMutex);
free(p); free(p);
TEST_POINT TEST_POINT
@ -445,19 +457,22 @@ xmlMemStrdupLoc(const char *str, const char *file, int line)
goto error; goto error;
} }
p->mh_tag = MEMTAG; p->mh_tag = MEMTAG;
p->mh_number = ++block;
p->mh_size = size; p->mh_size = size;
p->mh_type = STRDUP_TYPE; p->mh_type = STRDUP_TYPE;
p->mh_file = file; p->mh_file = file;
p->mh_line = line; p->mh_line = line;
xmlMutexLock(xmlMemMutex);
p->mh_number = ++block;
debugMemSize += size; debugMemSize += size;
if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize; if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize;
#ifdef MEM_LIST #ifdef MEM_LIST
debugmem_list_add(p); debugmem_list_add(p);
#endif #endif
xmlMutexUnlock(xmlMemMutex);
s = (char *) HDR_2_CLIENT(p); s = (char *) HDR_2_CLIENT(p);
if (xmlMemStopAtBlock == block) xmlMallocBreakpoint(); if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint();
if (s != NULL) if (s != NULL)
strcpy(s,str); strcpy(s,str);
@ -594,6 +609,7 @@ xmlMemDisplay(FILE *fp)
debugMemSize, debugMaxMemSize); debugMemSize, debugMaxMemSize);
fprintf(fp,"BLOCK NUMBER SIZE TYPE\n"); fprintf(fp,"BLOCK NUMBER SIZE TYPE\n");
idx = 0; idx = 0;
xmlMutexLock(xmlMemMutex);
p = memlist; p = memlist;
while (p) { while (p) {
fprintf(fp,"%-5u %6lu %6lu ",idx++,p->mh_number, fprintf(fp,"%-5u %6lu %6lu ",idx++,p->mh_number,
@ -618,6 +634,7 @@ xmlMemDisplay(FILE *fp)
fprintf(fp,"\n"); fprintf(fp,"\n");
p = p->mh_next; p = p->mh_next;
} }
xmlMutexUnlock(xmlMemMutex);
#else #else
fprintf(fp,"Memory list not compiled (MEM_LIST not defined !)\n"); fprintf(fp,"Memory list not compiled (MEM_LIST not defined !)\n");
#endif #endif
@ -692,6 +709,7 @@ xmlMemShow(FILE *fp, int nr ATTRIBUTE_UNUSED)
fprintf(fp," MEMORY ALLOCATED : %lu, MAX was %lu\n", fprintf(fp," MEMORY ALLOCATED : %lu, MAX was %lu\n",
debugMemSize, debugMaxMemSize); debugMemSize, debugMaxMemSize);
#ifdef MEM_LIST #ifdef MEM_LIST
xmlMutexLock(xmlMemMutex);
if (nr > 0) { if (nr > 0) {
fprintf(fp,"NUMBER SIZE TYPE WHERE\n"); fprintf(fp,"NUMBER SIZE TYPE WHERE\n");
p = memlist; p = memlist;
@ -715,6 +733,7 @@ xmlMemShow(FILE *fp, int nr ATTRIBUTE_UNUSED)
p = p->mh_next; p = p->mh_next;
} }
} }
xmlMutexUnlock(xmlMemMutex);
#endif /* MEM_LIST */ #endif /* MEM_LIST */
} }
@ -766,12 +785,14 @@ xmlInitMemory(void)
char *breakpoint; char *breakpoint;
#endif #endif
xmlMemInitialized = 1;
if (xmlInitMemoryDone) return(-1); if (xmlInitMemoryDone) return(-1);
xmlMemMutex = xmlNewMutex();
#ifdef HAVE_STDLIB_H #ifdef HAVE_STDLIB_H
breakpoint = getenv("XML_MEM_BREAKPOINT"); breakpoint = getenv("XML_MEM_BREAKPOINT");
if (breakpoint != NULL) { if (breakpoint != NULL) {
sscanf(breakpoint, "%d", &xmlMemStopAtBlock); sscanf(breakpoint, "%ud", &xmlMemStopAtBlock);
} }
#endif #endif
#ifdef HAVE_STDLIB_H #ifdef HAVE_STDLIB_H
@ -785,7 +806,6 @@ xmlInitMemory(void)
xmlGenericError(xmlGenericErrorContext, xmlGenericError(xmlGenericErrorContext,
"xmlInitMemory() Ok\n"); "xmlInitMemory() Ok\n");
#endif #endif
xmlMemInitialized = 1;
xmlInitMemoryDone = 1; xmlInitMemoryDone = 1;
return(0); return(0);