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