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