Annotation of parser3/src/main/pa_globals.C, revision 1.192

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

E-mail: