--- parser3/src/include/pa_pool.h 2001/02/20 18:45:51 1.22 +++ parser3/src/include/pa_pool.h 2003/03/06 12:02:04 1.86.2.32 @@ -1,61 +1,56 @@ -/* - $Id: pa_pool.h,v 1.22 2001/02/20 18:45:51 paf Exp $ +/** @file + Parser: Pool class and helper operator decls + + 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 - -class String; -class Exception; - -class Pool { -public: +static const char* IDENT_POOL_H="$Date: 2003/03/06 12:02:04 $"; - // Exception to report pool errors - Pool(Exception& aexception) : fexception(aexception) {} - ~Pool() {} +// include - Exception& exception() { return fexception; } +#include "pa_config_includes.h" +#include "pa_array.h" - void *malloc(size_t size) { - return check(real_malloc(size), size); - } - void *calloc(size_t size) { - return check(real_calloc(size), size); +/** + Pool mechanizm allows users not to free up allocated memory, + leaving that problem to 'pools'. +*/ +class Pool: public Array { +public: + char *malloc(size_t size) { + CharPtr result=CharPtr((char *)Array::malloc(size)); + *this += result; + return result.get(); } -protected: // implementation defined - - void *real_malloc(size_t size); - void *real_calloc(size_t size); - -protected: - - Exception& fexception; - - // checks whether mem allocated OK. throws exception otherwise - void *check(void *ptr, size_t size); - -private: //disabled - - // Pool(const Pool&) {} - Pool& operator = (const Pool&) { return *this; } -}; - -class Pooled { -public: - static void *operator new(size_t size, Pool& apool) { - return apool.malloc(size); + 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; } - Pooled(Pool& apool) : fpool(apool) {} - Pool& pool() const { return fpool; } + char *format_integer(int value); -protected: - // the pool I'm allocated on - Pool& fpool; }; +/** + 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 +*/ +inline void *operator new[] (size_t size, Pool& pool) { + return pool.malloc(size); +} + #endif