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