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