--- parser3/src/include/pa_pool.h 2001/01/26 15:28:39 1.2 +++ parser3/src/include/pa_pool.h 2003/11/20 16:32:12 1.89 @@ -1,98 +1,83 @@ -/* +/** @file + Parser: pool class decl. - String Chunk0 - ====== ======== - head--------->[ptr, size] - append_here-------->[ptr, size] - link_row ........ - . . - . [ptr, size] - ...........>[link to the next chunk] + Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com) + Author: Alexandr Petrosian (http://paf.design.ru) */ #ifndef PA_POOL_H #define PA_POOL_H -#include +static const char * const IDENT_POOL_H="$Date: 2003/11/20 16:32:12 $"; -class Pool; +#include "pa_config_includes.h" +#include "pa_array.h" -class String { +/** + Pool mechanizm allows users not to free up allocated objects, + leaving that problem to 'pools'. + + @see Pooled +*/ + +class Pool { public: - enum { - CR_PREALLOCATED_COUNT=5, - CR_GROW_PERCENT=60 + + struct Cleanup { + void (*cleanup) (void *); + void *data; + + Cleanup(void (*acleanup) (void *), void *adata): cleanup(acleanup), data(adata) {} }; -private: - friend Pool; + Pool(); + ~Pool(); - // the pool I'm allocated on - Pool *pool; + /// registers a routine to clean up non-pooled allocations + void register_cleanup(void (*cleanup) (void *), void *data); + /// unregister it, looking it up by it's data + void unregister_cleanup(void *cleanup_data); + +private: - // 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; - } - head; // the head chunk of the chunk chain + Array cleanups; - // next append would write to this record - Chunk::Row *append_here; +private: - // 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; - } - - bool chunk_is_full() { - return append_here == link_row; - } - void expand(); + //{ + /// @name implementation defined + bool real_register_cleanup(void (*cleanup) (void *), void *data); + //} -public: +private: + + /// throws register cleanup exception + void fail_register_cleanup() const; + +private: //disabled - size_t size(); - char *c_str(); - String& operator += (char *src); + Pool(const Pool&); + Pool& operator= (const Pool&); }; -class Pool { +/** + Base for all classes that are allocated in 'pools'. + Holds Pool object. +*/ +class Pooled { + // the pool i'm allocated on + Pool& fpool; 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); - } + Pooled(Pool& apool); + + /// my pool + //Pool& pool() const { return *fpool; } + + /// Sole: this got called automatically from Pool::~Pool() + virtual ~Pooled(); + }; #endif