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

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.42    ! paf         8:        $Id: pa_pool.h,v 1.41 2001/03/22 16:38:19 paf Exp $
1.1       paf         9: */
                     10: 
                     11: #ifndef PA_POOL_H
                     12: #define PA_POOL_H
                     13: 
                     14: #include <stddef.h>
                     15: 
1.21      paf        16: class Exception;
1.25      paf        17: class Temp_exception;
1.1       paf        18: 
1.35      paf        19: /** 
1.34      paf        20:        Pool mechanizm allows users not to free up allocated memory,
                     21:        leaving that problem to 'pools'.
                     22: 
                     23:        Also holds Exception object, which can be temporary set using 
                     24:        Temp_exception auto-object.
1.38      paf        25: 
                     26:        @see Pooled
1.34      paf        27: */
                     28: 
1.1       paf        29: class Pool {
1.25      paf        30:        friend Temp_exception;
1.1       paf        31: public:
1.18      paf        32: 
1.42    ! paf        33:        Pool() : fstorage(0), fcontext(0), fexception(0) {}
1.40      paf        34:        ~Pool();
1.18      paf        35: 
1.39      paf        36:        void set_storage(void *astorage) { fstorage=astorage; }
1.40      paf        37:        void set_context(void *acontext) { fcontext=acontext; }
1.39      paf        38: 
                     39:        void *storage() { return fstorage; }
1.40      paf        40:        void *context() { return fcontext; }
1.39      paf        41: 
1.34      paf        42:        /// current exception object of the pool
1.31      paf        43:        Exception& exception() const { return *fexception; }
1.18      paf        44: 
1.34      paf        45:        /// allocates some bytes on pool
1.18      paf        46:        void *malloc(size_t size) {
                     47:                return check(real_malloc(size), size);
                     48:        }
1.34      paf        49:        /// allocates some bytes clearing them with zeros
1.18      paf        50:        void *calloc(size_t size) {
                     51:                return check(real_calloc(size), size);
                     52:        }
1.39      paf        53: 
                     54: private:
                     55: 
                     56:        void *fstorage;
1.40      paf        57:        void *fcontext;
1.8       paf        58: 
1.23      paf        59: private: // implementation defined
1.18      paf        60: 
1.20      paf        61:     void *real_malloc(size_t size);
                     62:     void *real_calloc(size_t size);
1.17      paf        63: 
1.23      paf        64: private: 
1.17      paf        65: 
1.34      paf        66:        /// checks whether mem allocated OK. throws exception otherwise
1.23      paf        67:        void *check(void *ptr, size_t size) {
                     68:                if(ptr)
                     69:                        return ptr;
                     70: 
                     71:                fail(size);
                     72: 
                     73:                // never reached
                     74:                return 0;
                     75:        }
1.34      paf        76:        /// throws proper exception
1.23      paf        77:        void fail(size_t size) const;
                     78: 
1.34      paf        79: private: // exception handling
1.17      paf        80: 
1.23      paf        81:        // exception replacement mechanism is 'protected' from direct usage
1.34      paf        82:        // Temp_exception object enforces paired set/restore
1.23      paf        83:        Exception *set_exception(Exception *e){
1.31      paf        84:                Exception *r=fexception;
                     85:                fexception=e;
1.23      paf        86:                return r;
                     87:        }
                     88:        void restore_exception(Exception *e) {
1.31      paf        89:                fexception=e;
1.23      paf        90:        }
                     91: 
                     92: private:
                     93: 
                     94:        // current request's exception object
1.31      paf        95:        Exception *fexception;
1.17      paf        96: 
1.8       paf        97: private: //disabled
                     98: 
1.18      paf        99:        // Pool(const Pool&) {}
1.16      paf       100:        Pool& operator = (const Pool&) { return *this; }
1.21      paf       101: };
                    102: 
1.35      paf       103: /** 
1.34      paf       104:        Base for all classes that are allocated in 'pools'.
                    105: 
                    106:        Holds Pool object. Contains useful wrappers to it's methods.
1.38      paf       107: 
                    108:        @see NEW, Temp_exception
1.34      paf       109: */
1.21      paf       110: class Pooled {
1.23      paf       111:        // the pool i'm allocated on
                    112:        Pool& fpool;
1.21      paf       113: public:
1.37      paf       114: 
                    115:        /// the Pooled-sole: Pooled instances can be allocated in Pool rather then on heap
1.21      paf       116:        static void *operator new(size_t size, Pool& apool) { 
                    117:                return apool.malloc(size);
                    118:        }
                    119: 
1.23      paf       120:        Pooled(Pool& apool) : fpool(apool) {
                    121:        }
                    122: 
1.34      paf       123:        /// my pool
1.22      paf       124:        Pool& pool() const { return fpool; }
1.21      paf       125: 
1.34      paf       126:        /// useful wrapper around pool
1.23      paf       127:        void *malloc(size_t size) const { return fpool.malloc(size); }
1.34      paf       128:        /// useful wrapper around pool
1.23      paf       129:        void *calloc(size_t size) const { return fpool.calloc(size); }
1.34      paf       130:        /// useful wrapper around pool
1.31      paf       131:        Exception& exception() const { return fpool.exception(); }
1.23      paf       132: };
1.34      paf       133: /// useful macro for creating objects on current Pooled object Pooled::pool()
1.23      paf       134: #define NEW new(pool())
                    135: 
1.35      paf       136: /** 
1.34      paf       137:        Auto-object used for temporary changing Pool's exception().
                    138: 
                    139:        Use by with these macros:
1.38      paf       140:        @code
1.34      paf       141:                TRY { 
1.38      paf       142:                        // ... 
1.34      paf       143:                        if(?) 
                    144:                                THROW(?); 
1.38      paf       145:                        // ...
1.34      paf       146:                } CATCH(e) { 
1.38      paf       147:                        // code, using e fields
                    148:                        // e.comment() 
1.34      paf       149:                }
                    150:                END_CATCH
1.38      paf       151:        @endcode
                    152: 
                    153:        @see TRY, THROW
1.34      paf       154: */
1.25      paf       155: class Temp_exception {
1.29      paf       156:        Pool& fpool;
1.23      paf       157:        Exception *saved_exception;
                    158: public:
1.25      paf       159:        Temp_exception(Pool& apool, Exception& exception) : 
1.29      paf       160:                fpool(apool),
1.23      paf       161:                saved_exception(apool.set_exception(&exception)) {
                    162:        }
1.25      paf       163:        ~Temp_exception() { 
1.29      paf       164:                fpool.restore_exception(saved_exception); 
1.23      paf       165:        }
1.1       paf       166: };
1.23      paf       167: 
1.33      paf       168: #define XTRY(pool) \
1.24      paf       169:        { \
                    170:                Exception temp_exception; \
1.33      paf       171:                Temp_exception le(pool, temp_exception); \
1.24      paf       172:                if(setjmp(temp_exception.mark)==0)
1.31      paf       173: 
1.33      paf       174: #define XTHROW(exception) exception._throw
                    175: #define XCATCH(e) \
1.24      paf       176:                else{ \
                    177:                        Exception& e=temp_exception;
1.31      paf       178: 
1.33      paf       179: #define XEND_CATCH \
1.24      paf       180:                } \
                    181:        }
1.33      paf       182: 
1.34      paf       183: //@{
                    184: /// @see Temp_exception 
1.33      paf       185: #define TRY XTRY(pool())
                    186: #define THROW XTHROW(exception())
                    187: #define CATCH(e) XCATCH(e)
                    188: #define END_CATCH XEND_CATCH 
                    189: 
                    190: #define PTRY XTRY(pool)
                    191: #define PTHROW XTHROW(pool.exception())
                    192: #define PCATCH(e) XCATCH(e)
                    193: #define PEND_CATCH XEND_CATCH 
                    194: 
                    195: #define RTHROW XTHROW(r.pool().exception())
1.34      paf       196: //@}
1.1       paf       197: 
                    198: #endif

E-mail: