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

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

E-mail: