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