--- parser3/src/include/pa_pool.h 2001/01/26 14:47:52 1.1 +++ parser3/src/include/pa_pool.h 2003/03/06 12:02:04 1.86.2.32 @@ -1,96 +1,56 @@ -/* +/** @file + Parser: Pool class and helper operator decls - String Chunk0 - ====== ======== - head_chunk--------->[ptr, size] - append_here-------->[ptr, size] - chunk_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/03/06 12:02:04 $"; -class Pool; +// include -class String { - friend Pool; +#include "pa_config_includes.h" +#include "pa_array.h" - // the pool I'm allocated on - Pool *pool; - - // the number of records per chunk - int chunk_items; - enum { - CI_CONST_STRING_GROW=5, - CI_DYNAMIC_STRING_GROW_DEFAULT=10 - }; - - struct Chunk { - 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; - // next rows are here +/** + 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(); } - *head_chunk; // 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 *chunk_link_row; - - // new&constructors made private to enforce factory manufacturing at pool - void *operator new(size_t size, Pool *apool); - - void construct(Pool *apool, int achunk_items); - String(Pool *apool, int achunk_items=CI_DYNAMIC_STRING_GROW_DEFAULT) { - construct(apool, achunk_items); - } - String(Pool *apool, char *src) { - construct(apool, CI_CONST_STRING_GROW); - *this+=src; - } + char *copy(const char* buf, size_t size=0) { + if(!size) + size=strlen(buf)+1; - bool chunk_is_full() { - return append_here == chunk_link_row; + char *result=malloc(size); + memcpy(result, buf, size); + return result; } - void expand(); -public: + char *format_integer(int value); - size_t size(); - char *c_str(); - String& operator += (char *src); }; -class Pool { -public: - Pool(); - ~Pool(); - void *alloc(size_t size); - void *calloc(size_t size); - - String *makeString() { - return new(this) String(this); - } - String *makeString(int chunk_items) { - return new(this) String(this, chunk_items); - } - 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