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

1.1       paf         1: /*
1.26    ! paf         2:   $Id: pa_pool.h,v 1.25 2001/02/25 16:36:11 paf Exp $
1.1       paf         3: */
                      4: 
                      5: #ifndef PA_POOL_H
                      6: #define PA_POOL_H
                      7: 
                      8: #include <stddef.h>
                      9: 
1.23      paf        10: //class String;
1.21      paf        11: class Exception;
1.25      paf        12: class Temp_exception;
1.1       paf        13: 
                     14: class Pool {
1.25      paf        15:        friend Temp_exception;
1.1       paf        16: public:
1.18      paf        17: 
1.24      paf        18:        Pool() : fexception(0) {}
1.18      paf        19:        ~Pool() {}
                     20: 
1.23      paf        21:        Exception& exception() const { return *fexception; }
1.18      paf        22: 
                     23:        void *malloc(size_t size) {
                     24:                return check(real_malloc(size), size);
                     25:        }
                     26:        void *calloc(size_t size) {
                     27:                return check(real_calloc(size), size);
                     28:        }
1.8       paf        29: 
1.23      paf        30: private: // implementation defined
1.18      paf        31: 
1.20      paf        32:     void *real_malloc(size_t size);
                     33:     void *real_calloc(size_t size);
1.17      paf        34: 
1.23      paf        35: private: 
1.17      paf        36: 
1.23      paf        37:        // checks whether mem allocated OK. throws exception otherwise
                     38:        void *check(void *ptr, size_t size) {
                     39:                if(ptr)
                     40:                        return ptr;
                     41: 
                     42:                fail(size);
                     43: 
                     44:                // never reached
                     45:                return 0;
                     46:        }
1.24      paf        47:        // throws proper exception
1.23      paf        48:        void fail(size_t size) const;
                     49: 
                     50: protected: // exception handling
1.17      paf        51: 
1.23      paf        52:        // exception replacement mechanism is 'protected' from direct usage
1.24      paf        53:        // Temp_exception_change object enforces paired set/restore
1.23      paf        54:        Exception *set_exception(Exception *e){
                     55:                Exception *r=fexception;
                     56:                fexception=e;
                     57:                return r;
                     58:        }
                     59:        void restore_exception(Exception *e) {
                     60:                fexception=e;
                     61:        }
                     62: 
                     63: private:
                     64: 
                     65:        // current request's exception object
                     66:        Exception *fexception;
1.17      paf        67: 
1.8       paf        68: private: //disabled
                     69: 
1.18      paf        70:        // Pool(const Pool&) {}
1.16      paf        71:        Pool& operator = (const Pool&) { return *this; }
1.21      paf        72: };
                     73: 
                     74: class Pooled {
1.23      paf        75:        // the pool i'm allocated on
                     76:        Pool& fpool;
1.21      paf        77: public:
1.23      paf        78:        
1.21      paf        79:        static void *operator new(size_t size, Pool& apool) { 
                     80:                return apool.malloc(size);
                     81:        }
                     82: 
1.23      paf        83:        Pooled(Pool& apool) : fpool(apool) {
                     84:        }
                     85: 
1.22      paf        86:        Pool& pool() const { return fpool; }
1.21      paf        87: 
1.23      paf        88:        void *malloc(size_t size) const { return fpool.malloc(size); }
                     89:        void *calloc(size_t size) const { return fpool.calloc(size); }
                     90:        Exception& exception() const { return fpool.exception(); }
                     91: };
                     92: #define NEW new(pool())
                     93: 
1.25      paf        94: class Temp_exception {
1.23      paf        95:        Pool pool;
                     96:        Exception *saved_exception;
                     97: public:
1.25      paf        98:        Temp_exception(Pool& apool, Exception& exception) : 
1.23      paf        99:                pool(apool),
                    100:                saved_exception(apool.set_exception(&exception)) {
                    101:        }
1.25      paf       102:        ~Temp_exception() { 
1.23      paf       103:                pool.restore_exception(saved_exception); 
                    104:        }
1.1       paf       105: };
1.23      paf       106: 
1.24      paf       107: #define TRY \
                    108:        { \
                    109:                Exception temp_exception; \
1.25      paf       110:                Temp_exception le(pool(), temp_exception); \
1.24      paf       111:                if(setjmp(temp_exception.mark)==0)
                    112: 
1.23      paf       113: #define THROW exception()._throw
1.26    ! paf       114: #define POOL_THROW pool.exception()._throw
1.24      paf       115: #define CATCH(e) \
                    116:                else{ \
                    117:                        Exception& e=temp_exception;
                    118: 
                    119: #define END_CATCH \
                    120:                } \
                    121:        }
1.23      paf       122: // usage:
1.24      paf       123: //   TRY { ...; if(?) RAISE(?); ...; } CATCH(e) { catch-code e.comment() } END_CATCH
1.1       paf       124: 
                    125: #endif

E-mail: