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