--- parser3/src/main/pa_pool.C 2003/03/06 12:02:05 1.59.2.6 +++ parser3/src/main/pa_pool.C 2003/11/07 13:59:22 1.61 @@ -1,18 +1,57 @@ /** @file - Parser: pool class implementation. + Parser: pool class. - Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char* IDENT_POOL_C="$Date: 2003/03/06 12:02:05 $"; +static const char* IDENT_POOL_C="$Date: 2003/11/07 13:59:22 $"; #include "pa_pool.h" -#include "pa_types.h" +#include "pa_exception.h" #include "pa_common.h" +#include "pa_sapi.h" +#include "pa_charset.h" -char* Pool::format_integer(int value) { - char local[MAX_NUMBER]; - return copy(local, snprintf(local, MAX_NUMBER, "%d", value)+1/*for terminating 0*/); -}; +// Pool +Pool::Pool(){} + +static void cleanup(Pool::Cleanup item, int) { + if(item.cleanup) + item.cleanup(item.data); +} +Pool::~Pool() { + //__asm__("int3"); + //_asm int 3; + //fprintf(stderr, "cleanups: %d\n", cleanups.size()); + // cleanups first, because they use some object's memory pointers + cleanups.for_each(cleanup, 0); +} + +void Pool::register_cleanup(void (*cleanup) (void *), void *data) { + cleanups+=Cleanup(cleanup, data); +} + +static void unregister_cleanup(Pool::Cleanup& item, void* cleanup_data) { + if(item.data==cleanup_data) + item.cleanup=0; +} +void Pool::unregister_cleanup(void *cleanup_data) { + cleanups.for_each_ref(::unregister_cleanup, cleanup_data); +} + +// Pooled + +static void cleanup(void *data) { + static_cast(data)->~Pooled(); +} + +Pooled::Pooled(Pool& apool): fpool(apool) { + fpool.register_cleanup(cleanup, this); +} + +/// Sole: this got called automatically from Pool::~Pool() +Pooled::~Pooled() { + fpool.unregister_cleanup(this); +}