--- parser3/src/include/pa_pool.h 2001/01/30 11:51:07 1.17 +++ parser3/src/include/pa_pool.h 2003/03/06 12:02:04 1.86.2.32 @@ -1,64 +1,56 @@ -/* - $Id: pa_pool.h,v 1.17 2001/01/30 11:51:07 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 +static const char* IDENT_POOL_H="$Date: 2003/03/06 12:02:04 $"; -#include "pa_string.h" -#include "pa_hash.h" +// include + +#include "pa_config_includes.h" #include "pa_array.h" -//#include "pa_table.h" -#include "pa_exception.h" -class Pool { +/** + Pool mechanizm allows users not to free up allocated memory, + leaving that problem to 'pools'. +*/ +class Pool: public Array { public: - Pool(); - ~Pool(); - virtual void *malloc(size_t size)=0; - virtual void *calloc(size_t size)=0; - - 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); + char *malloc(size_t size) { + CharPtr result=CharPtr((char *)Array::malloc(size)); + *this += result; + return result.get(); } - Array& make_array(int initial_rows) { - return *new(*this) Array(*this, initial_rows); - } - /*Table& make_table(char *afile, uint aline, Array *acolumns, int initial_rows) { - return *new(this) Table(this, afile, aline, acolumns, initial_rows); - }*/ - - Exception *global_exception() { return fglobal_exception; } - Exception *set_global_exception(Exception *e); - void restore_global_exception(Exception *e); - - Exception *local_exception() { return flocal_exception; } - Exception *set_local_exception(Exception *e); - void restore_local_exception(Exception *e); - -protected: - Exception *fglobal_exception; - Exception *flocal_exception; - - // checks whether mem allocated OK. throws exceptions - void *check(void *ptr); + 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; + } -private: //disabled + char *format_integer(int value); - Pool(const Pool&) {} - Pool& operator = (const Pool&) { return *this; } }; +/** + 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