Annotation of parser3/src/main/pa_globals.C, revision 1.196
1.15 paf 1: /** @file
1.16 paf 2: Parser: globals.
3:
1.194 moko 4: Copyright (c) 2001-2015 Art. Lebedev Studio (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.102 paf 8: #include "pa_config_includes.h"
9:
10: #ifdef XML
1.157 paf 11: #include "libxml/xmlversion.h"
1.102 paf 12: #include "libxslt/extensions.h"
13: #include "libxslt/xsltutils.h"
1.116 paf 14: extern "C" {
1.102 paf 15: #include "libexslt/exslt.h"
1.193 moko 16: }
1.102 paf 17: #endif
18:
1.1 paf 19: #include "pa_globals.h"
1.83 parser 20: #include "pa_sapi.h"
1.101 paf 21: #include "pa_threads.h"
1.162 paf 22: #include "pa_xml_io.h"
1.163 paf 23: #include "pa_common.h"
1.196 ! moko 24: #include "pa_symbols.h"
1.70 parser 25:
1.164 paf 26: #include "pa_cache_managers.h"
27:
1.188 moko 28: #include "ltdl.h"
1.182 misha 29: #include "pcre.h"
30:
1.196 ! moko 31: volatile const char * IDENT_PA_GLOBALS_C="$Id: pa_globals.C,v 1.195 2016/05/24 11:01:19 moko Exp $" IDENT_PA_GLOBALS_H IDENT_PA_SAPI_H;
1.187 moko 32:
1.157 paf 33: // defines
1.155 paf 34:
1.157 paf 35: //#define PA_DEBUG_XML_GC_MEMORY
1.95 paf 36:
1.157 paf 37: // globals
1.32 paf 38:
1.195 moko 39: short hex_value[0x100]={0};
1.111 paf 40:
1.5 paf 41: static void setup_hex_value() {
1.195 moko 42: hex_value['0'] = 0;
43: hex_value['1'] = 1;
44: hex_value['2'] = 2;
45: hex_value['3'] = 3;
46: hex_value['4'] = 4;
47: hex_value['5'] = 5;
48: hex_value['6'] = 6;
49: hex_value['7'] = 7;
50: hex_value['8'] = 8;
1.5 paf 51: hex_value['9'] = 9;
52: hex_value['A'] = 10;
53: hex_value['B'] = 11;
54: hex_value['C'] = 12;
55: hex_value['D'] = 13;
56: hex_value['E'] = 14;
57: hex_value['F'] = 15;
58: hex_value['a'] = 10;
59: hex_value['b'] = 11;
60: hex_value['c'] = 12;
61: hex_value['d'] = 13;
62: hex_value['e'] = 14;
63: hex_value['f'] = 15;
64: }
1.1 paf 65:
1.185 moko 66: THREAD_LOCAL Request* thread_request=NULL;
1.162 paf 67:
68: void pa_register_thread_request(Request& r) {
1.185 moko 69: thread_request=&r;
1.162 paf 70: }
71: /// retrives request set by pa_set_request function, useful in contextless places [slow]
72: Request& pa_thread_request() {
1.185 moko 73: return *thread_request;
1.162 paf 74: }
1.176 paf 75:
1.162 paf 76:
1.99 paf 77: #ifdef XML
1.101 paf 78:
1.157 paf 79: class XML_Generic_error_info {
1.173 paf 80: public:/*internal, actually*/
1.166 paf 81: char buf[MAX_STRING*5];
1.157 paf 82: size_t used;
83: public:
84: XML_Generic_error_info() {
85: buf[used=0]=0;
86: }
1.173 paf 87: const char* get() {
88: return used? buf: 0;
1.157 paf 89: }
1.162 paf 90: };
1.101 paf 91:
1.186 moko 92: THREAD_LOCAL XML_Generic_error_info* xml_generic_error_info = NULL;
1.101 paf 93:
1.162 paf 94: static void xmlParserGenericErrorFunc(void * /*ctx*/, const char* msg, ...) {
1.172 paf 95: XML_Generic_error_info* p;
1.186 moko 96:
97: if(!(p=xml_generic_error_info)) // occupy empty one
98: p=xml_generic_error_info=new(PointerFreeGC) XML_Generic_error_info;
1.101 paf 99:
1.172 paf 100: va_list args;
101: va_start(args, msg);
102: p->used+=vsnprintf(p->buf+p->used, sizeof(p->buf)-p->used, msg, args);
103: va_end(args);
1.101 paf 104: }
105:
1.102 paf 106: bool xmlHaveGenericErrors() {
1.186 moko 107: return xml_generic_error_info!=0;
1.102 paf 108: }
109:
1.157 paf 110: const char* xmlGenericErrors() {
1.186 moko 111: if(XML_Generic_error_info *p=xml_generic_error_info) {
112: xml_generic_error_info=0;
1.173 paf 113: return p->get();
114: }
1.110 paf 115:
1.162 paf 116: return 0; // no errors for our thread_id registered
1.150 paf 117: }
118:
1.195 moko 119: #endif // XML
1.99 paf 120:
1.157 paf 121: #ifdef XML
122:
123: static char *pa_GC_strdup(const char *s) {
124: if(!s)
125: return 0;
126:
127: size_t size=strlen(s)+1;
1.190 moko 128: char *result=(char *)pa_gc_malloc_atomic(size);
1.195 moko 129:
1.159 paf 130: if(!result)
1.184 misha 131: pa_fail_alloc("duplicate XML string",size);
1.159 paf 132:
1.157 paf 133: memcpy(result, s, size);
1.170 paf 134: #ifdef PA_DEBUG_XML_GC_MEMORY
135: fprintf(stderr, "pa_GC_strdup(%p=%s, length=%d)=0x%p\n", s, s, size, result);
136: #endif
1.157 paf 137: return result;
138: }
139:
140: #ifdef PA_DEBUG_XML_GC_MEMORY
1.191 moko 141:
1.157 paf 142: static void* pa_gc_malloc_log(size_t size){
143: void *p=pa_gc_malloc(size);
144: fprintf(stderr, "pa_gc_malloc_log(%d)=0x%p\n", size, p);
145: return p;
146:
147: }
1.191 moko 148:
1.157 paf 149: static void* pa_gc_malloc_atomic_log(size_t size){
150: void *p=pa_gc_malloc_atomic(size);
151: fprintf(stderr, "pa_gc_malloc_atomic_log(%d)=0x%p\n", size, p);
152: return p;
153: }
1.191 moko 154:
1.157 paf 155: static void* pa_gc_realloc_log(void *ptr, size_t size){
156: void *p=pa_gc_realloc(ptr, size);
157: fprintf(stderr, "pa_gc_realloc_log(0x%p, %d)=0x%p\n", ptr, size, p);
158: return p;
159: }
1.191 moko 160:
1.157 paf 161: static void pa_gc_free_log(void *p){
162: fprintf(stderr, "pa_gc_free_log(0x%p)\n", p);
163: pa_gc_free(p);
164: }
1.191 moko 165:
1.159 paf 166: #else
167:
168: inline void *check(void *result, const char *where, size_t size) {
169: if(!result)
1.184 misha 170: pa_fail_alloc(where, size);
1.159 paf 171: return result;
172: }
1.191 moko 173:
1.159 paf 174: static void* pa_gc_malloc_nonull(size_t size) {
1.184 misha 175: return check(pa_gc_malloc(size), "allocate XML compsite memory", size);
1.159 paf 176: }
1.191 moko 177:
1.159 paf 178: static void* pa_gc_malloc_atomic_nonull(size_t size) {
1.184 misha 179: return check(pa_gc_malloc_atomic(size), "allocate XML atomic memory", size);
1.159 paf 180: }
1.191 moko 181:
1.159 paf 182: static void* pa_gc_realloc_nonull(void* ptr, size_t size) {
1.184 misha 183: return check(pa_gc_realloc(ptr, size), "reallocate XML memory", size);
1.159 paf 184: }
185:
1.191 moko 186: static void pa_gc_free_maybeignore(void* ptr) {
1.175 paf 187: pa_gc_free(ptr);
188: }
189:
1.157 paf 190: #endif
1.195 moko 191:
192: #endif // XML
1.157 paf 193:
194: void pa_CORD_oom_fn(void) {
1.184 misha 195: pa_fail_alloc("expand string", 0);
1.157 paf 196: }
197:
198: /**
199: @todo gc: libltdl: substitute lt_dlmalloc & co
200: */
201: static void gc_substitute_memory_management_functions() {
202: // in libxml & libxslt
203: #ifdef XML
204: // asking to use GC memory
205: #ifdef PA_DEBUG_XML_GC_MEMORY
206: xmlGcMemSetup(
207: /*xmlFreeFunc */pa_gc_free_log,
208: /*xmlMallocFunc */pa_gc_malloc_log,
209: /*xmlMallocFunc */pa_gc_malloc_atomic_log,
210: /*xmlReallocFunc */pa_gc_realloc_log,
211: /*xmlStrdupFunc */pa_GC_strdup);
212: #else
213: xmlGcMemSetup(
1.175 paf 214: /*xmlFreeFunc */pa_gc_free_maybeignore,
1.159 paf 215: /*xmlMallocFunc */pa_gc_malloc_nonull,
216: /*xmlMallocFunc */pa_gc_malloc_atomic_nonull,
217: /*xmlReallocFunc */pa_gc_realloc_nonull,
1.157 paf 218: /*xmlStrdupFunc */pa_GC_strdup);
219: #endif
1.32 paf 220:
1.157 paf 221: #endif
1.141 paf 222:
1.157 paf 223: // pcre
1.183 misha 224: pcre_malloc=pa_gc_malloc;
225: pcre_free=pa_gc_free;
1.135 paf 226:
1.157 paf 227: // cord
228: CORD_oom_fn=pa_CORD_oom_fn;
229: }
1.88 paf 230:
1.157 paf 231: /**
232: @test hint on one should call this for each thread xmlSubstituteEntitiesDefault(1);
233: */
234: void pa_globals_init() {
1.164 paf 235: // global variables
236: cache_managers=new Cache_managers;
237:
1.157 paf 238: // in various libraries
239: gc_substitute_memory_management_functions();
1.101 paf 240:
1.157 paf 241: // hex value
242: setup_hex_value();
1.74 parser 243:
1.196 ! moko 244: #ifdef SYMBOLS_CACHING
! 245: // symbols cache
! 246: Symbols::init();
! 247: #endif
! 248:
1.76 parser 249: #ifdef XML
1.96 paf 250: // initializing xml libs
251:
1.191 moko 252: // Register the EXSLT extensions and the test module
1.157 paf 253: exsltRegisterAll();
254: xsltRegisterTestModule();
255: xmlDefaultSAXHandlerInit();
1.191 moko 256:
257: // disable CDATA from being built in the document tree
1.157 paf 258: // never added yet xmlDefaultSAXHandler.cdataBlock = NULL;
259:
1.191 moko 260: // Initialization function for the XML parser. This is not reentrant.
261: // Call once before processing in case of use in multithreaded programs.
1.99 paf 262: xmlInitParser();
1.107 paf 263:
264: // 1. this is needed for proper parsing of stylesheets
265: // there were a situation where honest entity ruined innocent xpath compilation
266: // doc says "you sould turn it on on stylesheet load" without deepening into details
267: // 2. when dom tree with entites goes under transform text nodes
268: // got [erroreosly] cut on first entity occurance
1.109 paf 269: // --
1.107 paf 270: // that is why this is:
271: xmlSubstituteEntitiesDefault(1);
1.100 paf 272:
273: // Bit in the loadsubset context field to tell to do ID/REFs lookups
274: xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
275: // Bit in the loadsubset context field to tell to do complete the elements attributes lists
276: // with the ones defaulted from the DTDs
1.157 paf 277: xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
1.138 paf 278:
279: // validate each document after load/create (?)
1.191 moko 280: // xmlDoValidityCheckingDefaultValue = 1;
1.99 paf 281:
1.191 moko 282: // regretfully this not only replaces entities on parse, but also on generate xmlSubstituteEntitiesDefault(1);
1.105 paf 283: // never switched this on xmlIndentTreeOutput=1;
1.104 paf 284:
1.101 paf 285: xmlSetGenericErrorFunc(0, xmlParserGenericErrorFunc);
1.102 paf 286: xsltSetGenericErrorFunc(0, xmlParserGenericErrorFunc);
1.191 moko 287:
1.195 moko 288: // FILE *f=fopen("xslt.log", "wt");
1.105 paf 289: // xsltSetGenericDebugFunc(f/*stderr*/, 0);
1.110 paf 290:
1.162 paf 291: pa_xml_io_init();
1.157 paf 292: #endif
1.174 paf 293: }
294:
1.188 moko 295: static bool is_dlinited=false;
296:
1.174 paf 297: void pa_globals_done() {
1.191 moko 298: delete cache_managers;
299: cache_managers=0;
300:
1.188 moko 301: if(is_dlinited)
302: lt_dlexit();
303: }
304:
305: void pa_dlinit() {
306: if(!is_dlinited){
307: if(lt_dlinit())
1.192 moko 308: throw Exception(0,0,"preparation for dynamic library loading failed, %s", lt_dlerror());
1.188 moko 309: is_dlinited=true;
310: }
1.157 paf 311: }
312:
313: #ifdef _MSC_VER
314:
315: #ifndef PA_DEBUG_DISABLE_GC
1.191 moko 316:
317: #define GC_LIB "../../../../win32/gc"
318: #ifdef _DEBUG
319: #pragma comment(lib, GC_LIB "/Debug/gc.lib")
320: #else
321: #pragma comment(lib, GC_LIB "/Release/gc.lib")
322: #endif
1.96 paf 323:
1.195 moko 324: #endif // PA_DEBUG_DISABLE_GC
1.76 parser 325:
1.191 moko 326:
327: #ifdef XML
328:
1.181 paf 329: #define GNOME_LIBS "../../../../win32/gnome"
330:
1.191 moko 331: #ifdef _DEBUG
332: #define LIB_XML GNOME_LIBS "/libxml2-x.x.x/win32/debug/lib/"
333: #define LIB_XSLT GNOME_LIBS "/libxslt-x.x.x/win32/debug/lib/"
334: #else
335: #define LIB_XML GNOME_LIBS "/libxml2-x.x.x/win32/release/lib/"
336: #define LIB_XSLT GNOME_LIBS "/libxslt-x.x.x/win32/release/lib/"
1.181 paf 337: #endif
338:
1.191 moko 339: #ifdef LIBXML_STATIC
340: #pragma comment(lib, LIB_XML "libxml2_a.lib")
341: #else
342: #pragma comment(lib, LIB_XML "libxml2.lib")
343: #endif
1.157 paf 344:
1.191 moko 345: #ifdef LIBXSLT_STATIC
346: #pragma comment(lib, LIB_XSLT "libxslt_a.lib")
347: #else
348: #pragma comment(lib, LIB_XSLT "libxslt.lib")
349: #endif
1.157 paf 350:
1.191 moko 351: #ifdef LIBEXSLT_STATIC
352: #pragma comment(lib, LIB_XSLT "libexslt_a.lib")
1.157 paf 353: #else
1.191 moko 354: #pragma comment(lib, LIB_XSLT "libexslt.lib")
355: #endif
1.157 paf 356:
1.195 moko 357: #endif // XML
1.157 paf 358:
1.195 moko 359: #endif // _MSC_VER
E-mail: