Annotation of parser3/src/main/pa_globals.C, revision 1.163
1.15 paf 1: /** @file
1.16 paf 2: Parser: globals.
3:
1.157 paf 4: Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)
1.113 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.133 paf 6: */
1.16 paf 7:
1.163 ! paf 8: static const char * const IDENT_GLOBALS_C="$Date: 2003/11/26 12:49:27 $";
1.1 paf 9:
1.102 paf 10: #include "pa_config_includes.h"
11:
12: #ifdef XML
1.157 paf 13: #include "libxml/xmlversion.h"
1.102 paf 14: #include "libxslt/extensions.h"
15: #include "libxslt/xsltutils.h"
1.116 paf 16: extern "C" {
1.102 paf 17: #include "libexslt/exslt.h"
1.116 paf 18: };
1.102 paf 19: #endif
20:
1.157 paf 21: #include "pcre.h"
22:
1.1 paf 23: #include "pa_globals.h"
1.32 paf 24: #include "pa_string.h"
1.83 parser 25: #include "pa_sapi.h"
1.101 paf 26: #include "pa_threads.h"
1.162 paf 27: #include "pa_xml_io.h"
1.163 ! paf 28: #include "pa_common.h"
1.70 parser 29:
1.157 paf 30: // defines
1.155 paf 31:
1.157 paf 32: //#define PA_DEBUG_XML_GC_MEMORY
1.95 paf 33:
1.157 paf 34: // globals
1.32 paf 35:
1.5 paf 36: short hex_value[0x100];
1.111 paf 37:
38: #ifdef XML
39: GdomeDOMImplementation *domimpl;
40: #endif
1.5 paf 41:
42: static void setup_hex_value() {
1.68 parser 43: memset(hex_value, 0, sizeof(hex_value));
1.5 paf 44: hex_value['0'] = 0;
45: hex_value['1'] = 1;
46: hex_value['2'] = 2;
47: hex_value['3'] = 3;
48: hex_value['4'] = 4;
49: hex_value['5'] = 5;
50: hex_value['6'] = 6;
51: hex_value['7'] = 7;
52: hex_value['8'] = 8;
53: hex_value['9'] = 9;
54: hex_value['A'] = 10;
55: hex_value['B'] = 11;
56: hex_value['C'] = 12;
57: hex_value['D'] = 13;
58: hex_value['E'] = 14;
59: hex_value['F'] = 15;
60: hex_value['a'] = 10;
61: hex_value['b'] = 11;
62: hex_value['c'] = 12;
63: hex_value['d'] = 13;
64: hex_value['e'] = 14;
65: hex_value['f'] = 15;
66: }
1.1 paf 67:
1.162 paf 68:
69: Hash<pa_thread_t, Request*> thread_request;
70: void pa_register_thread_request(Request& r) {
71: thread_request.put(pa_get_thread_id(), &r);
72: }
73: /// retrives request set by pa_set_request function, useful in contextless places [slow]
74: Request& pa_thread_request() {
75: return *thread_request.get(pa_get_thread_id());
76: }
77:
1.99 paf 78: #ifdef XML
1.101 paf 79:
1.157 paf 80: class XML_Generic_error_info {
81: public:
82: char buf[MAX_STRING];
83: size_t used;
84: public:
85: XML_Generic_error_info() {
86: reset();
87: }
88: void reset() {
89: buf[used=0]=0;
90: }
91: const char* get_and_reset() {
92: char* result=new(PointerFreeGC) char[used+1];
93: memcpy(result, buf, used+1);
94: reset();
95: return result;
96: }
1.162 paf 97: };
1.101 paf 98:
1.162 paf 99: Hash<pa_thread_t, XML_Generic_error_info*> xml_generic_error_infos;
1.101 paf 100:
1.162 paf 101: static void xmlParserGenericErrorFunc(void * /*ctx*/, const char* msg, ...) {
102: //_asm int 3;
1.157 paf 103: pa_thread_t thread_id=pa_get_thread_id();
1.101 paf 104:
105: // infinitely looking for free slot to fill it
106: while(true) {
107: SYNCHRONIZED; // find+fill blocked
108:
109: // first try to get existing for this thread_id
1.162 paf 110: XML_Generic_error_info *p=xml_generic_error_infos.get(thread_id);
111: if(!p) // occupy empty one
112: xml_generic_error_infos.put(thread_id, (p=new(PointerFreeGC) XML_Generic_error_info));
1.101 paf 113:
114: va_list args;
115: va_start(args, msg);
1.157 paf 116: p->used+=vsnprintf(p->buf+p->used, sizeof(p->buf)-p->used, msg, args);
1.101 paf 117: va_end(args);
118:
119: break;
120: }
121: }
122:
1.102 paf 123: bool xmlHaveGenericErrors() {
1.157 paf 124: pa_thread_t thread_id=pa_get_thread_id();
1.102 paf 125:
126: SYNCHRONIZED; // find blocked
127:
1.162 paf 128: return xml_generic_error_infos.get(thread_id)!=0;
1.102 paf 129: }
130:
1.157 paf 131: const char* xmlGenericErrors() {
132: pa_thread_t thread_id=pa_get_thread_id();
1.101 paf 133:
134: SYNCHRONIZED; // find+free blocked
135:
1.162 paf 136: if(XML_Generic_error_info *p=xml_generic_error_infos.get(thread_id))
137: return p->get_and_reset();
1.110 paf 138:
1.162 paf 139: return 0; // no errors for our thread_id registered
1.150 paf 140: }
141:
1.99 paf 142: #endif
143:
1.83 parser 144: void pa_globals_destroy(void *) {
145: try {
1.96 paf 146: #ifdef XML
147: GdomeException exc;
148: gdome_di_unref (domimpl, &exc);
1.160 paf 149: // uncomment SAPI::abort below if adding potential-throw code here
1.96 paf 150: #endif
1.83 parser 151: } catch(const Exception& e) {
1.160 paf 152: // SAPI::abort("pa_globals_destroy failed: %s", e.comment());
1.83 parser 153: }
154: }
155:
156:
1.157 paf 157: #ifdef XML
158:
159: static char *pa_GC_strdup(const char *s) {
160: if(!s)
161: return 0;
162:
163: size_t size=strlen(s)+1;
164: char *result=(char *)GC_malloc_atomic(size);
1.159 paf 165: if(!result)
166: SAPI::abort("out of memory (while duplicating XML string [size=%d])", size);
167:
1.157 paf 168: memcpy(result, s, size);
169: return result;
170: }
171:
172: #ifdef PA_DEBUG_XML_GC_MEMORY
173: void *pa_look_for[]={(void*)0x8abe000,(void*)0x0,(void*)0x0,(void*)0x0,
174: (void*)0x0,(void*)0x0,(void*)0x0,(void*)0x0};
175: bool pa_looked(void*p) {
176: for(int i=0; i<8; i++)
177: if(pa_look_for[i]==p)
178: return true;
179: return false;
180: }
181: static void* pa_gc_malloc_log(size_t size){
182: void *p=pa_gc_malloc(size);
183: fprintf(stderr, "pa_gc_malloc_log(%d)=0x%p\n", size, p);
184: // if(pa_looked(p))
185: // fprintf(stderr,"catched debug malloc(%d)=0x%p\n", size, p);
186: return p;
187:
188: }
189: static void* pa_gc_malloc_atomic_log(size_t size){
190: void *p=pa_gc_malloc_atomic(size);
191: fprintf(stderr, "pa_gc_malloc_atomic_log(%d)=0x%p\n", size, p);
192: // if(pa_looked(p))
193: // fprintf(stderr,"catched debug malloc atomic(%d)=0x%p\n", size, p);
194: return p;
195: }
196: static void* pa_gc_realloc_log(void *ptr, size_t size){
197: void *p=pa_gc_realloc(ptr, size);
198: fprintf(stderr, "pa_gc_realloc_log(0x%p, %d)=0x%p\n", ptr, size, p);
199: // if(pa_looked(p))
200: // fprintf(stderr,"catched debug realloc(%d)=0x%p\n", size, p);
201: return p;
202: }
203: //static void pa_gc_free_ignore(void *){}
204: static void pa_gc_free_log(void *p){
205: fprintf(stderr, "pa_gc_free_log(0x%p)\n", p);
206: // if(pa_looked(p))
207: // fprintf(stderr,"catched debug free(0x%p)\n", p);
208: pa_gc_free(p);
209: }
1.159 paf 210: #else
211:
212: inline void *check(void *result, const char *where, size_t size) {
213: if(!result)
214: SAPI::abort("out of memory (while %s [size=%d])", where, size);
215:
216: return result;
217: }
218: static void* pa_gc_malloc_nonull(size_t size) {
219: return check(pa_gc_malloc(size), "allocating XML compsite memory", size);
220: }
221: static void* pa_gc_malloc_atomic_nonull(size_t size) {
222: return check(pa_gc_malloc_atomic(size), "allocating XML atomic memory", size);
223: }
224: static void* pa_gc_realloc_nonull(void* ptr, size_t size) {
225: return check(pa_gc_realloc(ptr, size), "reallocating XML memory", size);
226: }
227:
1.157 paf 228: #endif
229: #endif
230:
231: void pa_CORD_oom_fn(void) {
232: SAPI::abort("out of memory (while expanding string)");
233: }
234:
235: /**
236: @todo gc: libltdl: substitute lt_dlmalloc & co
237: */
238: static void gc_substitute_memory_management_functions() {
239: // in libxml & libxslt
240: #ifdef XML
241: // asking to use GC memory
242: #if LIBXML_VERSION >= 20507
243: #ifdef PA_DEBUG_XML_GC_MEMORY
244: xmlGcMemSetup(
245: /*xmlFreeFunc */pa_gc_free_log,
246: /*xmlMallocFunc */pa_gc_malloc_log,
247: /*xmlMallocFunc */pa_gc_malloc_atomic_log,
248: /*xmlReallocFunc */pa_gc_realloc_log,
249: /*xmlStrdupFunc */pa_GC_strdup);
250: #else
251: xmlGcMemSetup(
252: /*xmlFreeFunc */pa_gc_free,
1.159 paf 253: /*xmlMallocFunc */pa_gc_malloc_nonull,
254: /*xmlMallocFunc */pa_gc_malloc_atomic_nonull,
255: /*xmlReallocFunc */pa_gc_realloc_nonull,
1.157 paf 256: /*xmlStrdupFunc */pa_GC_strdup);
257: #endif
1.32 paf 258:
1.157 paf 259: #else
260: xmlMemSetup(
261: /*xmlFreeFunc */pa_gc_free,
262: /*xmlMallocFunc */pa_gc_malloc,
263: /*xmlReallocFunc */pa_gc_realloc,
264: /*xmlStrdupFunc */pa_GC_strdup);
265: #endif
1.5 paf 266:
1.157 paf 267: #endif
1.141 paf 268:
1.157 paf 269: // pcre
270: pcre_malloc=pa_gc_malloc;
271: pcre_free=pa_gc_free;
1.135 paf 272:
1.1 paf 273:
1.157 paf 274: // cord
275: CORD_oom_fn=pa_CORD_oom_fn;
276: }
1.88 paf 277:
1.157 paf 278: /**
279: @test hint on one should call this for each thread xmlSubstituteEntitiesDefault(1);
280: */
281: void pa_globals_init() {
282: // in various libraries
283: gc_substitute_memory_management_functions();
1.101 paf 284:
1.157 paf 285: // hex value
286: setup_hex_value();
1.74 parser 287:
1.76 parser 288: #ifdef XML
1.96 paf 289: // initializing xml libs
290:
1.157 paf 291: /* First get a DOMImplementation reference */
1.96 paf 292: domimpl = gdome_di_mkref ();
1.157 paf 293: /*
294: * Register the EXSLT extensions and the test module
295: */
296: exsltRegisterAll();
297: xsltRegisterTestModule();
298: xmlDefaultSAXHandlerInit();
299: /*
300: * disable CDATA from being built in the document tree
301: */
302: // never added yet xmlDefaultSAXHandler.cdataBlock = NULL;
303:
1.99 paf 304: /*
305: * Initialization function for the XML parser.
306: * This is not reentrant. Call once before processing in case of
307: * use in multithreaded programs.
308: */
309: xmlInitParser();
1.107 paf 310:
311: // 1. this is needed for proper parsing of stylesheets
312: // there were a situation where honest entity ruined innocent xpath compilation
313: // doc says "you sould turn it on on stylesheet load" without deepening into details
314: // 2. when dom tree with entites goes under transform text nodes
315: // got [erroreosly] cut on first entity occurance
1.109 paf 316: // --
1.107 paf 317: // that is why this is:
318: xmlSubstituteEntitiesDefault(1);
1.100 paf 319:
320: // Bit in the loadsubset context field to tell to do ID/REFs lookups
321: xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
322: // Bit in the loadsubset context field to tell to do complete the elements attributes lists
323: // with the ones defaulted from the DTDs
1.157 paf 324: xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
1.138 paf 325:
326: // validate each document after load/create (?)
327: //xmlDoValidityCheckingDefaultValue = 1;
1.99 paf 328:
1.104 paf 329: //regretfully this not only replaces entities on parse, but also on generate xmlSubstituteEntitiesDefault(1);
1.105 paf 330: // never switched this on xmlIndentTreeOutput=1;
1.104 paf 331:
1.101 paf 332: xmlSetGenericErrorFunc(0, xmlParserGenericErrorFunc);
1.102 paf 333: xsltSetGenericErrorFunc(0, xmlParserGenericErrorFunc);
1.105 paf 334: // FILE *f=fopen("y:\\xslt.log", "wt");
335: // xsltSetGenericDebugFunc(f/*stderr*/, 0);
1.110 paf 336:
1.162 paf 337: pa_xml_io_init();
1.157 paf 338: #endif
339: }
340:
341: #ifdef _MSC_VER
342:
343: #ifndef PA_DEBUG_DISABLE_GC
344: # define GC_LIB "../../../../win32/gc"
345: # ifdef _DEBUG
346: # pragma comment(lib, GC_LIB "/Debug/gc.lib")
347: # else
348: # pragma comment(lib, GC_LIB "/Release/gc.lib")
349: # endif
1.96 paf 350:
1.76 parser 351: #endif
352:
1.157 paf 353: #ifdef XML
1.158 paf 354: # define GNOME_LIBS "../../../../win32/gnome"
1.131 paf 355: # pragma comment(lib, GNOME_LIBS "/glib/lib/libglib-1.3-11.lib")
1.76 parser 356: # ifdef _DEBUG
1.157 paf 357:
358: # ifdef LIBXML_STATIC
359: # pragma comment(lib, GNOME_LIBS "/gnome-xml/win32/binaries-debug/libxml2_a.lib")
360: # else
361: # pragma comment(lib, GNOME_LIBS "/gnome-xml/win32/binaries-debug/libxml2.lib")
362: # endif
363:
364: # ifdef LIBXSLT_STATIC
365: # pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libxslt_DebugStatic/libxslt.lib")
366: # else
367: # pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libxslt_DebugDynamic/libxslt.lib")
368: # endif
369: # ifdef LIBEXSLT_STATIC
370: # pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libexslt_DebugStatic/libexslt.lib")
371: # else
372: # pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libexslt_DebugDynamic/libexslt.lib")
373: # endif
374:
375: # ifdef LIBGDOME_STATIC
376: # pragma comment(lib, GNOME_LIBS "/gdome2-x.x.x/win32/dsp/libgdome_DebugStatic/libgdome.lib")
377: # else
378: # pragma comment(lib, GNOME_LIBS "/gdome2-x.x.x/win32/dsp/libgdome_DebugDynamic/libgdome.lib")
379: # endif
380:
381: #else
382:
383: # ifdef LIBXML_STATIC
384: # pragma comment(lib, GNOME_LIBS "/gnome-xml/win32/binaries-release/libxml2_a.lib")
385: # else
386: # pragma comment(lib, GNOME_LIBS "/gnome-xml/win32/binaries-release/libxml2.lib")
387: # endif
388:
389: # ifdef LIBXSLT_STATIC
390: # pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libxslt_ReleaseStatic/libxslt.lib")
391: # else
392: # pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libxslt_ReleaseDynamic/libxslt.lib")
393: # endif
394: # ifdef LIBEXSLT_STATIC
395: # pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libexslt_ReleaseStatic/libexslt.lib")
396: # else
397: # pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/dsp/libexslt_ReleaseDynamic/libexslt.lib")
398: # endif
399:
400: # ifdef LIBGDOME_STATIC
401: # pragma comment(lib, GNOME_LIBS "/gdome2-x.x.x/win32/dsp/libgdome_ReleaseStatic/libgdome.lib")
402: # else
403: # pragma comment(lib, GNOME_LIBS "/gdome2-x.x.x/win32/dsp/libgdome_ReleaseDynamic/libgdome.lib")
404: # endif
405:
1.85 paf 406: # endif
1.157 paf 407: #endif
408:
1.85 paf 409: #endif
E-mail: