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

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.190   ! moko       31: volatile const char * IDENT_PA_GLOBALS_C="$Id: pa_globals.C,v 1.189 2013/07/21 14:22:54 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.175     paf       147: void *pa_look_for[]={(void*)0x84ba980,(void*)0x8969460,(void*)0x0,(void*)0x0,
1.157     paf       148:                        (void*)0x0,(void*)0x0,(void*)0x0,(void*)0x0};
                    149: bool pa_looked(void*p) {
                    150:        for(int i=0; i<8; i++)
1.175     paf       151:                if(pa_look_for[i]==p) {
                    152:                        __asm__("int $3");
1.157     paf       153:                        return true;
1.175     paf       154:                }
                    155:        if((((int)p)&~0xFF)==0x89a7700) {
                    156:                __asm__("int $3");
                    157:                return true;
                    158:        }
1.157     paf       159:        return false;
                    160: }
                    161: static void* pa_gc_malloc_log(size_t size){
                    162:        void *p=pa_gc_malloc(size);
                    163:         fprintf(stderr, "pa_gc_malloc_log(%d)=0x%p\n", size, p);
1.175     paf       164:        if(pa_looked(p))
                    165:                fprintf(stderr,"catched debug malloc(%d)=0x%p\n", size, p);
1.157     paf       166:        return p;
                    167:         
                    168: }
                    169: static void* pa_gc_malloc_atomic_log(size_t size){
1.175     paf       170: #ifdef PA_WORKAROUND_BUGGY_MALLOCATOMIC_IN_LIBXML_GC_MEMORY
                    171:        void *p=pa_gc_malloc(size);
                    172:         fprintf(stderr, "pa_gc_malloc_atomicFAKE_log(%d)=0x%p\n", size, p);
                    173: #else
1.157     paf       174:        void *p=pa_gc_malloc_atomic(size);
                    175:         fprintf(stderr, "pa_gc_malloc_atomic_log(%d)=0x%p\n", size, p);
1.175     paf       176: #endif
                    177:        if(pa_looked(p))
                    178:                fprintf(stderr,"catched debug malloc atomic(%d)=0x%p\n", size, p);
1.157     paf       179:        return p;
                    180: }
                    181: static void* pa_gc_realloc_log(void *ptr, size_t size){
                    182:        void *p=pa_gc_realloc(ptr, size);
                    183:         fprintf(stderr, "pa_gc_realloc_log(0x%p, %d)=0x%p\n", ptr, size, p);
1.175     paf       184:        if(pa_looked(p))
                    185:                fprintf(stderr,"catched debug realloc(%d)=0x%p\n", size, p);
1.157     paf       186:        return p;
                    187: }
                    188: static void pa_gc_free_log(void *p){
1.175     paf       189: #ifdef PA_WORKAROUND_BUGGY_FREE_IN_LIBXML_GC_MEMORY
                    190:         fprintf(stderr, "pa_gc_freeIGNORE_log(0x%p)\n", p);
                    191: #else
1.157     paf       192:         fprintf(stderr, "pa_gc_free_log(0x%p)\n", p);
1.175     paf       193: #endif
                    194:        if(pa_looked(p))
                    195:                fprintf(stderr,"catched debug free(0x%p)\n", p);
                    196: #ifndef PA_WORKAROUND_BUGGY_FREE_IN_LIBXML_GC_MEMORY
1.157     paf       197:         pa_gc_free(p);
1.175     paf       198: #endif
1.157     paf       199: }
1.159     paf       200: #else
                    201: 
                    202: inline void *check(void *result, const char *where, size_t size) {
                    203:        if(!result)
1.184     misha     204:                pa_fail_alloc(where, size);
1.159     paf       205: 
                    206:        return result;
                    207: }
                    208: static void* pa_gc_malloc_nonull(size_t size) { 
1.184     misha     209:        return check(pa_gc_malloc(size), "allocate XML compsite memory", size);
1.159     paf       210: }
                    211: static void* pa_gc_malloc_atomic_nonull(size_t size) { 
1.175     paf       212: #ifdef PA_WORKAROUND_BUGGY_MALLOCATOMIC_IN_LIBXML_GC_MEMORY
1.184     misha     213:        return check(pa_gc_malloc(size), "allocate XML composite memory (asked atomic)", size);
1.175     paf       214: #else
1.184     misha     215:        return check(pa_gc_malloc_atomic(size), "allocate XML atomic memory", size);
1.175     paf       216: #endif
1.159     paf       217: }
                    218: static void* pa_gc_realloc_nonull(void* ptr, size_t size) { 
1.184     misha     219:        return check(pa_gc_realloc(ptr, size), "reallocate XML memory", size);
1.159     paf       220: }
                    221: 
1.175     paf       222: static void pa_gc_free_maybeignore(
                    223:        void* 
                    224: #ifndef PA_WORKAROUND_BUGGY_FREE_IN_LIBXML_GC_MEMORY
                    225:                ptr
                    226: #endif
                    227:        ) {
                    228: #ifndef PA_WORKAROUND_BUGGY_FREE_IN_LIBXML_GC_MEMORY
                    229:        pa_gc_free(ptr);
                    230: #endif
                    231: }
                    232: 
1.157     paf       233: #endif
                    234: #endif
                    235: 
                    236: void pa_CORD_oom_fn(void) {
1.184     misha     237:        pa_fail_alloc("expand string", 0);
1.157     paf       238: }
                    239: 
                    240: /**
                    241:        @todo gc: libltdl: substitute lt_dlmalloc & co
                    242: */
                    243: static void gc_substitute_memory_management_functions() {
                    244:        // in libxml & libxslt
                    245: #ifdef XML
                    246:        // asking to use GC memory
                    247: #if LIBXML_VERSION >= 20507
                    248: #ifdef PA_DEBUG_XML_GC_MEMORY
                    249:        xmlGcMemSetup(
                    250:                /*xmlFreeFunc */pa_gc_free_log,
                    251:                /*xmlMallocFunc */pa_gc_malloc_log,
                    252:                /*xmlMallocFunc */pa_gc_malloc_atomic_log,
                    253:                /*xmlReallocFunc */pa_gc_realloc_log,
                    254:                /*xmlStrdupFunc */pa_GC_strdup);
                    255: #else
                    256:        xmlGcMemSetup(
1.175     paf       257:                /*xmlFreeFunc */pa_gc_free_maybeignore,
1.159     paf       258:                /*xmlMallocFunc */pa_gc_malloc_nonull,
                    259:                /*xmlMallocFunc */pa_gc_malloc_atomic_nonull,
                    260:                /*xmlReallocFunc */pa_gc_realloc_nonull,
1.157     paf       261:                /*xmlStrdupFunc */pa_GC_strdup);
                    262: #endif
1.32      paf       263: 
1.157     paf       264: #else
                    265:        xmlMemSetup(
1.175     paf       266:                /*xmlFreeFunc */pa_gc_free_maybeignore,
1.157     paf       267:                /*xmlMallocFunc */pa_gc_malloc,
                    268:                /*xmlReallocFunc */pa_gc_realloc,
                    269:                /*xmlStrdupFunc */pa_GC_strdup);
                    270: #endif
1.5       paf       271: 
1.157     paf       272: #endif
1.141     paf       273: 
1.157     paf       274:        // pcre
1.183     misha     275:        pcre_malloc=pa_gc_malloc;
                    276:        pcre_free=pa_gc_free;
1.135     paf       277: 
1.1       paf       278: 
1.157     paf       279:        // cord
                    280:        CORD_oom_fn=pa_CORD_oom_fn;
                    281: }
1.88      paf       282: 
1.157     paf       283: /**
                    284:        @test hint on one should call this for each thread xmlSubstituteEntitiesDefault(1);
                    285: */
                    286: void pa_globals_init() {
1.164     paf       287:        // global variables 
                    288:        cache_managers=new Cache_managers;
                    289: 
                    290: 
1.157     paf       291:        // in various libraries
                    292:        gc_substitute_memory_management_functions();
1.101     paf       293: 
1.157     paf       294:        // hex value
                    295:        setup_hex_value();
1.74      parser    296: 
1.76      parser    297: #ifdef XML
1.96      paf       298:        // initializing xml libs
                    299: 
1.157     paf       300:        /*
                    301:        * Register the EXSLT extensions and the test module
                    302:        */
                    303:        exsltRegisterAll();
                    304:        xsltRegisterTestModule();
                    305:        xmlDefaultSAXHandlerInit();
                    306:        /*
                    307:        * disable CDATA from being built in the document tree
                    308:        */
                    309:        // never added yet  xmlDefaultSAXHandler.cdataBlock = NULL;
                    310:        
1.99      paf       311:        /*
                    312:         * Initialization function for the XML parser.
                    313:         * This is not reentrant. Call once before processing in case of
                    314:         * use in multithreaded programs.
                    315:        */
                    316:        xmlInitParser();
1.107     paf       317: 
                    318:        // 1. this is needed for proper parsing of stylesheets
                    319:        // there were a situation where honest entity ruined innocent xpath compilation
                    320:        // doc says "you sould turn it on on stylesheet load" without deepening into details
                    321:        // 2. when dom tree with entites goes under transform text nodes 
                    322:        // got [erroreosly] cut on first entity occurance
1.109     paf       323:        // --
1.107     paf       324:        // that is why this is:
                    325:        xmlSubstituteEntitiesDefault(1);
1.100     paf       326:        
                    327:        // Bit in the loadsubset context field to tell to do ID/REFs lookups 
                    328:        xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
                    329:        // Bit in the loadsubset context field to tell to do complete the elements attributes lists 
                    330:        // with the ones defaulted from the DTDs 
1.157     paf       331:        xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
1.138     paf       332: 
                    333:        // validate each document after load/create (?)
                    334:        //xmlDoValidityCheckingDefaultValue = 1;
1.99      paf       335: 
1.104     paf       336: //regretfully this not only replaces entities on parse, but also on generate   xmlSubstituteEntitiesDefault(1);
1.105     paf       337:        // never switched this on xmlIndentTreeOutput=1;
1.104     paf       338: 
1.101     paf       339:        xmlSetGenericErrorFunc(0, xmlParserGenericErrorFunc);
1.102     paf       340:        xsltSetGenericErrorFunc(0, xmlParserGenericErrorFunc);
1.105     paf       341: //     FILE *f=fopen("y:\\xslt.log", "wt");
                    342: //     xsltSetGenericDebugFunc(f/*stderr*/, 0);
1.110     paf       343: 
1.162     paf       344:        pa_xml_io_init();
1.157     paf       345: #endif
1.174     paf       346: }
                    347: 
1.188     moko      348: static bool is_dlinited=false;
                    349: 
1.174     paf       350: void pa_globals_done() {
                    351:        delete cache_managers;  cache_managers=0;
1.188     moko      352:        if(is_dlinited)
                    353:                lt_dlexit();
                    354: }
                    355: 
                    356: void pa_dlinit() {
                    357:        if(!is_dlinited){
                    358:                if(lt_dlinit())
                    359:                        throw Exception(0,0,"prepare to dynamic libary loading failed, %s", lt_dlerror());
                    360:                is_dlinited=true;
                    361:        }
1.157     paf       362: }
                    363: 
                    364: #ifdef _MSC_VER
                    365: 
                    366: #ifndef PA_DEBUG_DISABLE_GC
                    367: #      define GC_LIB "../../../../win32/gc"
                    368: #      ifdef _DEBUG
                    369: #              pragma comment(lib, GC_LIB "/Debug/gc.lib")
                    370: #      else
                    371: #              pragma comment(lib, GC_LIB "/Release/gc.lib")
                    372: #      endif
1.96      paf       373: 
1.76      parser    374: #endif
                    375: 
1.181     paf       376: #define GNOME_LIBS "../../../../win32/gnome"
                    377: 
                    378: #ifdef WITH_MAILRECEIVE
                    379: #      pragma comment(lib, GNOME_LIBS "/glib/lib/libglib-1.3-11.lib")
                    380: #endif
                    381: 
1.157     paf       382: #ifdef XML
1.76      parser    383: #      ifdef _DEBUG
1.157     paf       384: 
                    385: #              ifdef LIBXML_STATIC
1.168     paf       386: #                      pragma comment(lib, GNOME_LIBS "/libxml2-x.x.x/win32/debug/lib/libxml2_a.lib")
1.157     paf       387: #              else
1.168     paf       388: #                      pragma comment(lib, GNOME_LIBS "/libxml2-x.x.x/win32/debug/lib/libxml2.lib")
1.157     paf       389: #              endif
                    390: 
                    391: #              ifdef LIBXSLT_STATIC
1.168     paf       392: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/debug/lib/libxslt_a.lib")
1.157     paf       393: #              else
1.168     paf       394: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/debug/lib/libxslt.lib")
1.157     paf       395: #              endif
                    396: #              ifdef LIBEXSLT_STATIC
1.168     paf       397: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/debug/lib/libexslt_a.lib")
1.157     paf       398: #              else
1.168     paf       399: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/debug/lib/libexslt.lib")
1.157     paf       400: #              endif
                    401: 
                    402: #else
                    403: 
                    404: #              ifdef LIBXML_STATIC
1.168     paf       405: #                      pragma comment(lib, GNOME_LIBS "/libxml2-x.x.x/win32/release/lib/libxml2_a.lib")
1.157     paf       406: #              else
1.168     paf       407: #                      pragma comment(lib, GNOME_LIBS "/libxml2-x.x.x/win32/release/lib/libxml2.lib")
1.157     paf       408: #              endif
                    409: 
                    410: #              ifdef LIBXSLT_STATIC
1.168     paf       411: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/release/lib/libxslt_a.lib")
1.157     paf       412: #              else
1.168     paf       413: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/release/lib/libxslt.lib")
1.157     paf       414: #              endif
                    415: #              ifdef LIBEXSLT_STATIC
1.168     paf       416: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/release/lib/libexslt_a.lib")
1.157     paf       417: #              else
1.168     paf       418: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/release/lib/libexslt.lib")
1.157     paf       419: #              endif
                    420: 
1.85      paf       421: #      endif
1.157     paf       422: #endif
                    423: 
1.85      paf       424: #endif

E-mail: