Annotation of parser3/src/include/pa_pool.h, revision 1.62
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.62 ! parser 8: $Id: pa_pool.h,v 1.61 2001/10/05 17:33:50 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.59 parser 16: #ifdef XML
1.55 parser 17: #include <XalanDOM/XalanDOMString.hpp>
18: #include <util/TransService.hpp>
1.59 parser 19: #endif
1.55 parser 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.60 parser 68: /// resets transcoder if they change charset
69: void set_charset(const String &charset);
70: /// returns current charset
71: const String& get_charset() { return *charset; }
72:
1.39 paf 73: private:
74:
75: void *fstorage;
1.40 paf 76: void *fcontext;
1.47 paf 77: void *ftag;
1.60 parser 78: const String *charset;
1.8 paf 79:
1.50 parser 80: private:
81:
82: //{
83: /// @name implementation defined
1.51 parser 84: void *real_malloc(size_t size/*, int place*/);
1.20 paf 85: void *real_calloc(size_t size);
1.54 parser 86: bool real_register_cleanup(void (*cleanup) (void *), void *data);
1.50 parser 87: //}
1.17 paf 88:
1.23 paf 89: private:
1.17 paf 90:
1.34 paf 91: /// checks whether mem allocated OK. throws exception otherwise
1.23 paf 92: void *check(void *ptr, size_t size) {
93: if(ptr)
94: return ptr;
95:
1.54 parser 96: fail_alloc(size);
1.23 paf 97:
98: // never reached
99: return 0;
100: }
1.54 parser 101: /// throws allocation exception
102: void fail_alloc(size_t size) const;
103:
104: /// throws register cleanup exception
105: void fail_register_cleanup() const;
1.23 paf 106:
1.34 paf 107: private: // exception handling
1.17 paf 108:
1.45 paf 109: // exception replacement mechanism is 'private'zed from direct usage
1.34 paf 110: // Temp_exception object enforces paired set/restore
1.23 paf 111: Exception *set_exception(Exception *e){
1.31 paf 112: Exception *r=fexception;
113: fexception=e;
1.23 paf 114: return r;
115: }
116: void restore_exception(Exception *e) {
1.31 paf 117: fexception=e;
1.23 paf 118: }
119:
120: private:
121:
122: // current request's exception object
1.31 paf 123: Exception *fexception;
1.17 paf 124:
1.59 parser 125: #ifdef XML
126:
127: public:
128: /// converts Xalan string to char *
129: const char *transcode_cstr(const XalanDOMString& s);
130: /// converts Xalan string to parser String
131: String& transcode(const XalanDOMString& s);
132:
133: private:
134:
135: void set_charset(const char *new_scharset);
136: void update_transcoder();
137:
138: private:
139:
140: XMLTranscoder *transcoder;
141:
142: #endif
143:
1.8 paf 144: private: //disabled
145:
1.18 paf 146: // Pool(const Pool&) {}
1.16 paf 147: Pool& operator = (const Pool&) { return *this; }
1.21 paf 148: };
149:
1.35 paf 150: /**
1.34 paf 151: Base for all classes that are allocated in 'pools'.
152:
153: Holds Pool object. Contains useful wrappers to it's methods.
1.38 paf 154:
155: @see NEW, Temp_exception
1.34 paf 156: */
1.21 paf 157: class Pooled {
1.23 paf 158: // the pool i'm allocated on
1.49 paf 159: Pool *fpool;
1.21 paf 160: public:
1.37 paf 161:
162: /// the Pooled-sole: Pooled instances can be allocated in Pool rather then on heap
1.21 paf 163: static void *operator new(size_t size, Pool& apool) {
1.51 parser 164: return apool.malloc(size/*, 1*/);
1.21 paf 165: }
166:
1.49 paf 167: Pooled(Pool& apool) : fpool(&apool) {}
1.23 paf 168:
1.34 paf 169: /// my pool
1.49 paf 170: Pool& pool() const { return *fpool; }
171:
172: /** used for moving objects from one pool to another.
173: in between object can have no pool and can not be used
174: @see SQL_Driver_manager
175: */
176: void set_pool(Pool *apool) { fpool=apool; }
1.21 paf 177:
1.57 parser 178: //{
179: /// @name useful wrapper around pool
1.49 paf 180: void *malloc(size_t size) const { return fpool->malloc(size); }
181: void *calloc(size_t size) const { return fpool->calloc(size); }
1.57 parser 182: void register_cleanup(void (*cleanup) (void *), void *data) { fpool->register_cleanup(cleanup, data); }
1.49 paf 183: Exception& exception() const { return fpool->exception(); }
1.59 parser 184: #ifdef XML
1.57 parser 185: const char *transcode_cstr(const XalanDOMString& s) { return fpool->transcode_cstr(s); }
186: String& transcode(const XalanDOMString& s) { return fpool->transcode(s); }
1.59 parser 187: #endif
1.57 parser 188: //}
1.23 paf 189: };
1.34 paf 190: /// useful macro for creating objects on current Pooled object Pooled::pool()
1.23 paf 191: #define NEW new(pool())
192:
1.35 paf 193: /**
1.34 paf 194: Auto-object used for temporary changing Pool's exception().
195:
196: Use by with these macros:
1.38 paf 197: @code
1.34 paf 198: TRY {
1.38 paf 199: // ...
1.34 paf 200: if(?)
201: THROW(?);
1.38 paf 202: // ...
1.34 paf 203: } CATCH(e) {
1.38 paf 204: // code, using e fields
205: // e.comment()
1.34 paf 206: }
207: END_CATCH
1.38 paf 208: @endcode
209:
210: @see TRY, THROW
1.34 paf 211: */
1.25 paf 212: class Temp_exception {
1.29 paf 213: Pool& fpool;
1.23 paf 214: Exception *saved_exception;
215: public:
1.25 paf 216: Temp_exception(Pool& apool, Exception& exception) :
1.29 paf 217: fpool(apool),
1.23 paf 218: saved_exception(apool.set_exception(&exception)) {
219: }
1.25 paf 220: ~Temp_exception() {
1.29 paf 221: fpool.restore_exception(saved_exception);
1.23 paf 222: }
1.1 paf 223: };
1.23 paf 224:
1.33 paf 225: #define XTRY(pool) \
1.24 paf 226: { \
227: Exception temp_exception; \
1.33 paf 228: Temp_exception le(pool, temp_exception); \
1.24 paf 229: if(setjmp(temp_exception.mark)==0)
1.31 paf 230:
1.33 paf 231: #define XTHROW(exception) exception._throw
232: #define XCATCH(e) \
1.24 paf 233: else{ \
234: Exception& e=temp_exception;
1.31 paf 235:
1.33 paf 236: #define XEND_CATCH \
1.24 paf 237: } \
238: }
1.33 paf 239:
1.34 paf 240: //@{
241: /// @see Temp_exception
1.33 paf 242: #define TRY XTRY(pool())
243: #define THROW XTHROW(exception())
244: #define CATCH(e) XCATCH(e)
245: #define END_CATCH XEND_CATCH
246:
247: #define PTRY XTRY(pool)
248: #define PTHROW XTHROW(pool.exception())
249: #define PCATCH(e) XCATCH(e)
250: #define PEND_CATCH XEND_CATCH
1.34 paf 251: //@}
1.1 paf 252:
253: #endif
E-mail: