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