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

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

E-mail: