--- parser3/src/include/pa_pool.h 2001/02/20 18:45:51 1.22 +++ parser3/src/include/pa_pool.h 2026/04/25 13:38:46 1.98 @@ -1,61 +1,83 @@ -/* - $Id: pa_pool.h,v 1.22 2001/02/20 18:45:51 paf Exp $ +/** @file + Parser: pool class decl. + + Copyright (c) 2000-2026 Art. Lebedev Studio (https://www.artlebedev.com) + + Authors: Konstantin Morshnev , Alexandr Petrosian */ #ifndef PA_POOL_H #define PA_POOL_H -#include +#define IDENT_PA_POOL_H "$Id: pa_pool.h,v 1.98 2026/04/25 13:38:46 moko Exp $" + +#include "pa_config_includes.h" +#include "pa_array.h" -class String; -class Exception; +/** + Pool mechanizm allows users not to free up allocated objects, + leaving that problem to 'pools'. + + @see Pooled +*/ class Pool { public: - // Exception to report pool errors - Pool(Exception& aexception) : fexception(aexception) {} - ~Pool() {} + struct Cleanup : public PA_Allocated { + void (*cleanup) (void *); + void *data; + + Cleanup(void (*acleanup) (void *), void *adata): cleanup(acleanup), data(adata) {} + }; - Exception& exception() { return fexception; } + Pool(); + ~Pool(); - void *malloc(size_t size) { - return check(real_malloc(size), size); - } - void *calloc(size_t size) { - return check(real_calloc(size), size); - } + /// 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); -protected: // implementation defined +private: - void *real_malloc(size_t size); - void *real_calloc(size_t size); + Array cleanups; -protected: +private: + + //{ + /// @name implementation defined + bool real_register_cleanup(void (*cleanup) (void *), void *data); + //} - Exception& fexception; +private: - // checks whether mem allocated OK. throws exception otherwise - void *check(void *ptr, size_t size); + /// 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) : fpool(apool) {} - Pool& pool() const { return fpool; } + Pooled(Pool& apool); + + /// my pool + //Pool& pool() const { return *fpool; } + + /// Sole: this got called automatically from Pool::~Pool() + virtual ~Pooled(); -protected: - // the pool I'm allocated on - Pool& fpool; }; #endif