--- parser3/src/include/pa_pool.h 2001/01/26 15:28:39 1.2 +++ parser3/src/include/pa_pool.h 2001/02/20 18:45:51 1.22 @@ -1,14 +1,5 @@ /* - - String Chunk0 - ====== ======== - head--------->[ptr, size] - append_here-------->[ptr, size] - link_row ........ - . . - . [ptr, size] - ...........>[link to the next chunk] - + $Id: pa_pool.h,v 1.22 2001/02/20 18:45:51 paf Exp $ */ #ifndef PA_POOL_H @@ -16,83 +7,55 @@ #include -class Pool; +class String; +class Exception; -class String { +class Pool { public: - enum { - CR_PREALLOCATED_COUNT=5, - CR_GROW_PERCENT=60 - }; -private: - friend Pool; + // Exception to report pool errors + Pool(Exception& aexception) : fexception(aexception) {} + ~Pool() {} - // the pool I'm allocated on - Pool *pool; + Exception& exception() { return fexception; } - // last chank allocated count cache - int curr_chunk_rows; - struct Chunk { - // the number of rows per chunk - int count; - union Row { - // chunk item - struct { - char *ptr; // pointer to the start of string fragment - size_t size; // length of the fragment - } item; - Chunk *link; // link to the next chunk in chain - } first[CR_PREALLOCATED_COUNT]; - // next rows are here - Chunk *preallocated_link; + void *malloc(size_t size) { + return check(real_malloc(size), size); } - head; // the head chunk of the chunk chain - - // next append would write to this record - Chunk::Row *append_here; - - // the address of place where lies address - // of the link to the next chunk to allocate - Chunk::Row *link_row; - - // new&constructors made private to enforce factory manufacturing at pool - void *operator new(size_t size, Pool *apool); - - void construct(Pool *apool); - String(Pool *apool) { - construct(apool); - } - String(Pool *apool, char *src) { - construct(apool); - *this+=src; + void *calloc(size_t size) { + return check(real_calloc(size), size); } - bool chunk_is_full() { - return append_here == link_row; - } - void expand(); +protected: // implementation defined -public: + void *real_malloc(size_t size); + void *real_calloc(size_t size); + +protected: - size_t size(); - char *c_str(); - String& operator += (char *src); + Exception& fexception; + + // checks whether mem allocated OK. throws exception otherwise + void *check(void *ptr, size_t size); + +private: //disabled + + // Pool(const Pool&) {} + Pool& operator = (const Pool&) { return *this; } }; -class Pool { +class Pooled { public: - Pool(); - ~Pool(); - void *alloc(size_t size); - void *calloc(size_t size); - - String *makeString() { - return new(this) String(this); - } - String *makeString(char *src) { - return new(this) String(this, src); + static void *operator new(size_t size, Pool& apool) { + return apool.malloc(size); } + + Pooled(Pool& apool) : fpool(apool) {} + Pool& pool() const { return fpool; } + +protected: + // the pool I'm allocated on + Pool& fpool; }; #endif