--- parser3/src/include/pa_pool.h 2001/01/26 15:28:39 1.2 +++ parser3/src/include/pa_pool.h 2003/02/21 10:55:43 1.86.2.31 @@ -1,98 +1,55 @@ -/* +/** @file + Parser: Pool class and helper operator decls - String Chunk0 - ====== ======== - head--------->[ptr, size] - append_here-------->[ptr, size] - link_row ........ - . . - . [ptr, size] - ...........>[link to the next chunk] + 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/02/21 10:55:43 $"; -class Pool; +// include -class String { +#include "pa_config_includes.h" +#include "pa_array.h" + +/** + Pool mechanizm allows users not to free up allocated memory, + leaving that problem to 'pools'. +*/ +class Pool: public Array { public: - enum { - CR_PREALLOCATED_COUNT=5, - CR_GROW_PERCENT=60 - }; - -private: - friend Pool; - - // the pool I'm allocated on - Pool *pool; - - // last chank allocated count cache - int curr_chunk_rows; - struct Chunk { - // the number of rows per chunk - int count; - union Row { - // chunk item - struct { - char *ptr; // pointer to the start of string fragment - size_t size; // length of the fragment - } item; - Chunk *link; // link to the next chunk in chain - } first[CR_PREALLOCATED_COUNT]; - // next rows are here - Chunk *preallocated_link; + char *malloc(size_t size) { + CharPtr result=CharPtr((char *)Array::malloc(size)); + *this += result; + return result.get(); } - head; // the head chunk of the chunk chain - // next append would write to this record - Chunk::Row *append_here; - - // the address of place where lies address - // of the link to the next chunk to allocate - Chunk::Row *link_row; - - // new&constructors made private to enforce factory manufacturing at pool - void *operator new(size_t size, Pool *apool); - - void construct(Pool *apool); - String(Pool *apool) { - construct(apool); - } - String(Pool *apool, char *src) { - construct(apool); - *this+=src; - } + char *copy(const char* buf, size_t size=0) { + if(!size) + size=strlen(buf)+1; - bool chunk_is_full() { - return append_here == link_row; + char *result=malloc(size); + memcpy(result, buf, size); + return result; } - void expand(); - -public: - size_t size(); - char *c_str(); - String& operator += (char *src); + char *format_integer(int value); }; -class Pool { -public: - Pool(); - ~Pool(); - void *alloc(size_t size); - void *calloc(size_t size); - - String *makeString() { - return new(this) String(this); - } - String *makeString(char *src) { - return new(this) String(this, src); - } -}; +/** + 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