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

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

E-mail: