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

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

E-mail: