--- parser3/src/include/pa_pool.h 2001/01/27 15:21:05 1.8 +++ parser3/src/include/pa_pool.h 2020/12/15 17:10:31 1.95 @@ -1,43 +1,83 @@ -/* - $Id: pa_pool.h,v 1.8 2001/01/27 15:21:05 paf Exp $ +/** @file + Parser: pool class decl. + + Copyright (c) 2000-2020 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.95 2020/12/15 17:10:31 moko Exp $" -#include "pa_string.h" -#include "pa_hash.h" +#include "pa_config_includes.h" #include "pa_array.h" +/** + Pool mechanizm allows users not to free up allocated objects, + leaving that problem to 'pools'. + + @see Pooled +*/ + class Pool { public: + + struct Cleanup : public PA_Allocated { + void (*cleanup) (void *); + void *data; + + Cleanup(void (*acleanup) (void *), void *adata): cleanup(acleanup), data(adata) {} + }; + Pool(); ~Pool(); - void *malloc(size_t size); - void *calloc(size_t size); - String *make_string() { - return new(this) String(this); - } - String *make_string(char *src) { - return new(this) String(this, src); - } - Hash *make_hash() { - return new(this) Hash(this); - } - Array *make_array() { - return new(this) Array(this); - } - Array *make_array(int initial_rows) { - return new(this) Array(this, initial_rows); - } + /// 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: + + Array cleanups; + +private: + + //{ + /// @name implementation defined + bool real_register_cleanup(void (*cleanup) (void *), void *data); + //} + +private: + + /// throws register cleanup exception + void fail_register_cleanup() const; private: //disabled - Pool& operator = (Pool& src) { return *this; } - Pool(Pool& src) {} + 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: + + Pooled(Pool& apool); + + /// my pool + //Pool& pool() const { return *fpool; } + + /// Sole: this got called automatically from Pool::~Pool() + virtual ~Pooled(); + }; #endif