--- parser3/src/include/pa_pool.h 2001/02/11 11:27:24 1.21 +++ parser3/src/include/pa_pool.h 2012/03/16 09:24:09 1.90 @@ -1,87 +1,83 @@ -/* - $Id: pa_pool.h,v 1.21 2001/02/11 11:27:24 paf Exp $ +/** @file + Parser: pool class decl. + + Copyright (c) 2000-2012 Art. Lebedev Studio (http://www.artlebedev.com) + + Author: Alexandr Petrosian (http://paf.design.ru) */ #ifndef PA_POOL_H #define PA_POOL_H -#include +#define IDENT_PA_POOL_H "$Id: pa_pool.h,v 1.90 2012/03/16 09:24:09 moko Exp $" + +#include "pa_config_includes.h" +#include "pa_array.h" -///#include "pa_string.h" -///#include "pa_hash.h" -///#include "pa_array.h" -//#include "pa_table.h" -//#include "pa_exception.h" +/** + Pool mechanizm allows users not to free up allocated objects, + leaving that problem to 'pools'. -class String; -class Exception; + @see Pooled +*/ class Pool { public: - // Exception to report pool errors - Pool(Exception& aexception) : fexception(aexception) {} - ~Pool() {} - - Exception& exception() { return fexception; } - - void *malloc(size_t size) { - return check(real_malloc(size), size); - } - void *calloc(size_t size) { - return check(real_calloc(size), size); - } -/* - String& make_string() { - return *new(*this) String(*this); - } - Hash& make_hash() { - return *new(*this) Hash(*this, false); - } - Hash& make_thread_safe_hash() { - return *new(*this) Hash(*this, true); - } - Array& make_array() { - return *new(*this) Array(*this); - } - Array& make_array(int initial_rows) { - return *new(*this) Array(*this, initial_rows); - } -*/ + struct Cleanup { + void (*cleanup) (void *); + void *data; + + Cleanup(void (*acleanup) (void *), void *adata): cleanup(acleanup), data(adata) {} + }; - /*Table& make_table(char *afile, uint aline, Array *acolumns, int initial_rows) { - return *new(this) Table(this, afile, aline, acolumns, initial_rows); - }*/ + Pool(); + ~Pool(); -protected: // implementation defined + /// 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); - void *real_malloc(size_t size); - void *real_calloc(size_t size); +private: -protected: + Array cleanups; - Exception& fexception; +private: + + //{ + /// @name implementation defined + bool real_register_cleanup(void (*cleanup) (void *), void *data); + //} - // checks whether mem allocated OK. throws exception otherwise - void *check(void *ptr, size_t size); +private: + + /// throws register cleanup exception + void fail_register_cleanup() const; private: //disabled - // Pool(const Pool&) {} - Pool& operator = (const Pool&) { return *this; } + Pool(const Pool&); + Pool& operator= (const Pool&); }; +/** + Base for all classes that are allocated in 'pools'. + Holds Pool object. +*/ class Pooled { + // the pool i'm allocated on + Pool& fpool; public: - static void *operator new(size_t size, Pool& apool) { - return apool.malloc(size); - } - - Pooled(Pool& apool) : pool(apool) {} - -protected: - // the pool I'm allocated on - Pool& pool; + + Pooled(Pool& apool); + + /// my pool + //Pool& pool() const { return *fpool; } + + /// Sole: this got called automatically from Pool::~Pool() + virtual ~Pooled(); + }; #endif