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