--- parser3/src/include/pa_pool.h 2003/03/06 12:02:04 1.86.2.32 +++ parser3/src/include/pa_pool.h 2024/11/04 03:53:25 1.97 @@ -1,56 +1,83 @@ /** @file - Parser: Pool class and helper operator decls + Parser: pool class decl. - Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2000-2024 Art. Lebedev Studio (http://www.artlebedev.com) - Author: Alexandr Petrosian (http://paf.design.ru) + Authors: Konstantin Morshnev , Alexandr Petrosian */ #ifndef PA_POOL_H #define PA_POOL_H -static const char* IDENT_POOL_H="$Date: 2003/03/06 12:02:04 $"; - -// include +#define IDENT_PA_POOL_H "$Id: pa_pool.h,v 1.97 2024/11/04 03:53:25 moko Exp $" #include "pa_config_includes.h" #include "pa_array.h" /** - Pool mechanizm allows users not to free up allocated memory, + Pool mechanizm allows users not to free up allocated objects, leaving that problem to 'pools'. + + @see Pooled */ -class Pool: public Array { + +class Pool { public: - char *malloc(size_t size) { - CharPtr result=CharPtr((char *)Array::malloc(size)); - *this += result; - return result.get(); - } - - char *copy(const char* buf, size_t size=0) { - if(!size) - size=strlen(buf)+1; - - char *result=malloc(size); - memcpy(result, buf, size); - return result; - } - char *format_integer(int value); + struct Cleanup : public PA_Allocated { + void (*cleanup) (void *); + void *data; + + Cleanup(void (*acleanup) (void *), void *adata): cleanup(acleanup), data(adata) {} + }; + + 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: + + 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(const Pool&); + Pool& operator= (const Pool&); }; /** - Allocate something on pool, usage: - Something* something=new(pool) Something[count]; - - with NO corresponding - operator delete[] (Pool& pool) - it's Pool's responsibility to free that up + Base for all classes that are allocated in 'pools'. + Holds Pool object. */ -inline void *operator new[] (size_t size, Pool& pool) { - return pool.malloc(size); -} +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