Annotation of parser3/src/include/pa_pool.h, revision 1.55

1.34      paf         1: /** @file
1.36      paf         2:        Parser: pool class decl.
                      3: 
1.27      paf         4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.36      paf         5: 
1.28      paf         6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.27      paf         7: 
1.55    ! parser      8:        $Id: pa_pool.h,v 1.54 2001/09/15 13:20:22 parser Exp $
1.1       paf         9: */
                     10: 
                     11: #ifndef PA_POOL_H
                     12: #define PA_POOL_H
                     13: 
1.52      parser     14: #include "pa_config_includes.h"
1.1       paf        15: 
1.55    ! parser     16: #include <XalanDOM/XalanDOMString.hpp>
        !            17: #include <util/TransService.hpp>
        !            18: #include <PlatformSupport/XSLException.hpp>
        !            19: 
        !            20: 
        !            21: // forwards
        !            22: 
1.21      paf        23: class Exception;
1.25      paf        24: class Temp_exception;
1.55    ! parser     25: class String;
        !            26: 
        !            27: void Pool_cleanup(void *);
1.1       paf        28: 
1.35      paf        29: /** 
1.34      paf        30:        Pool mechanizm allows users not to free up allocated memory,
                     31:        leaving that problem to 'pools'.
                     32: 
                     33:        Also holds Exception object, which can be temporary set using 
                     34:        Temp_exception auto-object.
1.38      paf        35: 
                     36:        @see Pooled
1.34      paf        37: */
1.47      paf        38: 
1.1       paf        39: class Pool {
1.25      paf        40:        friend Temp_exception;
1.55    ! parser     41:        friend void Pool_cleanup(void *);
1.1       paf        42: public:
1.18      paf        43: 
1.55    ! parser     44:        Pool(void *astorage);
1.18      paf        45: 
1.55    ! parser     46: private:
        !            47:        void cleanup() {
        !            48:                delete transcoder;
        !            49:        }
        !            50: public:
1.40      paf        51:        void set_context(void *acontext) { fcontext=acontext; }
                     52:        void *context() { return fcontext; }
1.39      paf        53: 
1.47      paf        54:        void set_tag(void *atag) { ftag=atag; }
                     55:        void *tag() { return ftag; }
1.44      paf        56: 
1.34      paf        57:        /// allocates some bytes on pool
1.51      parser     58:        void *malloc(size_t size/*, int place=0*/) {
                     59:                return check(real_malloc(size/*, place*/), size);
1.18      paf        60:        }
1.34      paf        61:        /// allocates some bytes clearing them with zeros
1.18      paf        62:        void *calloc(size_t size) {
                     63:                return check(real_calloc(size), size);
                     64:        }
1.39      paf        65: 
1.53      parser     66:        /// registers a routine to clean up non-pooled allocations
1.54      parser     67:        void register_cleanup(void (*cleanup) (void *), void *data) {
                     68:                if(!real_register_cleanup(cleanup, data))
                     69:                        fail_register_cleanup();
                     70:        }
1.53      parser     71: 
                     72:        /// current exception object of the pool
                     73:        Exception& exception() const { return *fexception; }
                     74: 
1.55    ! parser     75:        /// resets transcoder if they change charset 
        !            76:        void set_charset(const String &charset);
        !            77:        /// converts Xalan string to char *
        !            78:        const char *transcode(const XalanDOMString& s);
        !            79:        /// converts XSL exception to parser exception
        !            80:        void _throw(const String *source, const XSLException& e);
        !            81: 
        !            82: private:
        !            83: 
        !            84:        void set_charset(const char *charset);
        !            85:        void update_transcoder();
        !            86: 
1.39      paf        87: private:
                     88: 
                     89:        void *fstorage;
1.40      paf        90:        void *fcontext;
1.47      paf        91:        void *ftag;
1.55    ! parser     92:        const char *charset;
        !            93:        XMLTranscoder *transcoder;
1.8       paf        94: 
1.50      parser     95: private: 
                     96:        
                     97:        //{
                     98:        /// @name implementation defined
1.51      parser     99:     void *real_malloc(size_t size/*, int place*/);
1.20      paf       100:     void *real_calloc(size_t size);
1.54      parser    101:        bool real_register_cleanup(void (*cleanup) (void *), void *data);
1.50      parser    102:        //}
1.17      paf       103: 
1.23      paf       104: private: 
1.17      paf       105: 
1.34      paf       106:        /// checks whether mem allocated OK. throws exception otherwise
1.23      paf       107:        void *check(void *ptr, size_t size) {
                    108:                if(ptr)
                    109:                        return ptr;
                    110: 
1.54      parser    111:                fail_alloc(size);
1.23      paf       112: 
                    113:                // never reached
                    114:                return 0;
                    115:        }
1.54      parser    116:        /// throws allocation exception
                    117:        void fail_alloc(size_t size) const;
                    118: 
                    119:        /// throws register cleanup exception
                    120:        void fail_register_cleanup() const;
1.23      paf       121: 
1.34      paf       122: private: // exception handling
1.17      paf       123: 
1.45      paf       124:        // exception replacement mechanism is 'private'zed from direct usage
1.34      paf       125:        // Temp_exception object enforces paired set/restore
1.23      paf       126:        Exception *set_exception(Exception *e){
1.31      paf       127:                Exception *r=fexception;
                    128:                fexception=e;
1.23      paf       129:                return r;
                    130:        }
                    131:        void restore_exception(Exception *e) {
1.31      paf       132:                fexception=e;
1.23      paf       133:        }
                    134: 
                    135: private:
                    136: 
                    137:        // current request's exception object
1.31      paf       138:        Exception *fexception;
1.17      paf       139: 
1.8       paf       140: private: //disabled
                    141: 
1.18      paf       142:        // Pool(const Pool&) {}
1.16      paf       143:        Pool& operator = (const Pool&) { return *this; }
1.21      paf       144: };
                    145: 
1.35      paf       146: /** 
1.34      paf       147:        Base for all classes that are allocated in 'pools'.
                    148: 
                    149:        Holds Pool object. Contains useful wrappers to it's methods.
1.38      paf       150: 
                    151:        @see NEW, Temp_exception
1.34      paf       152: */
1.21      paf       153: class Pooled {
1.23      paf       154:        // the pool i'm allocated on
1.49      paf       155:        Pool *fpool;
1.21      paf       156: public:
1.37      paf       157: 
                    158:        /// the Pooled-sole: Pooled instances can be allocated in Pool rather then on heap
1.21      paf       159:        static void *operator new(size_t size, Pool& apool) { 
1.51      parser    160:                return apool.malloc(size/*, 1*/);
1.21      paf       161:        }
                    162: 
1.49      paf       163:        Pooled(Pool& apool) : fpool(&apool) {}
1.23      paf       164: 
1.34      paf       165:        /// my pool
1.49      paf       166:        Pool& pool() const { return *fpool; }
                    167: 
                    168:        /** used for moving objects from one pool to another. 
                    169:                in between object can have no pool and can not be used
                    170:                @see SQL_Driver_manager
                    171:        */
                    172:        void set_pool(Pool *apool) { fpool=apool; }
1.21      paf       173: 
1.34      paf       174:        /// useful wrapper around pool
1.49      paf       175:        void *malloc(size_t size) const { return fpool->malloc(size); }
1.34      paf       176:        /// useful wrapper around pool
1.49      paf       177:        void *calloc(size_t size) const { return fpool->calloc(size); }
1.53      parser    178:        /// useful wrapper around pool
                    179:        void register_cleanup(void (*cleanup) (void *), void *data) { 
                    180:                fpool->register_cleanup(cleanup, data);
                    181:        }
1.34      paf       182:        /// useful wrapper around pool
1.49      paf       183:        Exception& exception() const { return fpool->exception(); }
1.55    ! parser    184:        /// useful wrapper around pool
        !           185:        const char *transcode(const XalanDOMString& s) { return fpool->transcode(s); }
        !           186:        /// useful wrapper around pool
        !           187:        void _throw(const String *source, const XSLException& e) { fpool->_throw(source, e); }
1.23      paf       188: };
1.34      paf       189: /// useful macro for creating objects on current Pooled object Pooled::pool()
1.23      paf       190: #define NEW new(pool())
                    191: 
1.35      paf       192: /** 
1.34      paf       193:        Auto-object used for temporary changing Pool's exception().
                    194: 
                    195:        Use by with these macros:
1.38      paf       196:        @code
1.34      paf       197:                TRY { 
1.38      paf       198:                        // ... 
1.34      paf       199:                        if(?) 
                    200:                                THROW(?); 
1.38      paf       201:                        // ...
1.34      paf       202:                } CATCH(e) { 
1.38      paf       203:                        // code, using e fields
                    204:                        // e.comment() 
1.34      paf       205:                }
                    206:                END_CATCH
1.38      paf       207:        @endcode
                    208: 
                    209:        @see TRY, THROW
1.34      paf       210: */
1.25      paf       211: class Temp_exception {
1.29      paf       212:        Pool& fpool;
1.23      paf       213:        Exception *saved_exception;
                    214: public:
1.25      paf       215:        Temp_exception(Pool& apool, Exception& exception) : 
1.29      paf       216:                fpool(apool),
1.23      paf       217:                saved_exception(apool.set_exception(&exception)) {
                    218:        }
1.25      paf       219:        ~Temp_exception() { 
1.29      paf       220:                fpool.restore_exception(saved_exception); 
1.23      paf       221:        }
1.1       paf       222: };
1.23      paf       223: 
1.33      paf       224: #define XTRY(pool) \
1.24      paf       225:        { \
                    226:                Exception temp_exception; \
1.33      paf       227:                Temp_exception le(pool, temp_exception); \
1.24      paf       228:                if(setjmp(temp_exception.mark)==0)
1.31      paf       229: 
1.33      paf       230: #define XTHROW(exception) exception._throw
                    231: #define XCATCH(e) \
1.24      paf       232:                else{ \
                    233:                        Exception& e=temp_exception;
1.31      paf       234: 
1.33      paf       235: #define XEND_CATCH \
1.24      paf       236:                } \
                    237:        }
1.33      paf       238: 
1.34      paf       239: //@{
                    240: /// @see Temp_exception 
1.33      paf       241: #define TRY XTRY(pool())
                    242: #define THROW XTHROW(exception())
                    243: #define CATCH(e) XCATCH(e)
                    244: #define END_CATCH XEND_CATCH 
                    245: 
                    246: #define PTRY XTRY(pool)
                    247: #define PTHROW XTHROW(pool.exception())
                    248: #define PCATCH(e) XCATCH(e)
                    249: #define PEND_CATCH XEND_CATCH 
1.34      paf       250: //@}
1.1       paf       251: 
                    252: #endif

E-mail: