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

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

E-mail: