|
|
| version 1.14, 2001/01/29 17:01:51 | version 1.97, 2024/11/04 03:53:25 |
|---|---|
| Line 1 | Line 1 |
| /* | /** @file |
| $Id$ | Parser: pool class decl. |
| Copyright (c) 2000-2024 Art. Lebedev Studio (http://www.artlebedev.com) | |
| Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru> | |
| */ | */ |
| #ifndef PA_POOL_H | #ifndef PA_POOL_H |
| #define PA_POOL_H | #define PA_POOL_H |
| #include <stddef.h> | #define IDENT_PA_POOL_H "$Id$" |
| #include "pa_string.h" | #include "pa_config_includes.h" |
| #include "pa_hash.h" | |
| #include "pa_array.h" | #include "pa_array.h" |
| //#include "pa_table.h" | |
| /** | |
| Pool mechanizm allows users not to free up allocated objects, | |
| leaving that problem to 'pools'. | |
| @see Pooled | |
| */ | |
| class Pool { | class Pool { |
| public: | public: |
| struct Cleanup : public PA_Allocated { | |
| void (*cleanup) (void *); | |
| void *data; | |
| Cleanup(void (*acleanup) (void *), void *adata): cleanup(acleanup), data(adata) {} | |
| }; | |
| Pool(); | Pool(); |
| ~Pool(); | ~Pool(); |
| void *malloc(size_t size); | |
| void *calloc(size_t size); | |
| String& make_string() { | /// registers a routine to clean up non-pooled allocations |
| return *new(this) String(this); | void register_cleanup(void (*cleanup) (void *), void *data); |
| } | /// unregister it, looking it up by it's data |
| Hash& make_hash() { | void unregister_cleanup(void *cleanup_data); |
| return *new(this) Hash(this, false); | |
| } | private: |
| Hash& make_thread_safe_hash() { | |
| return *new(this) Hash(this, true); | Array<Cleanup> cleanups; |
| } | |
| Array& make_array() { | private: |
| return *new(this) Array(this); | |
| } | //{ |
| Array& make_array(int initial_rows) { | /// @name implementation defined |
| return *new(this) Array(this, initial_rows); | bool real_register_cleanup(void (*cleanup) (void *), void *data); |
| } | //} |
| /*Table& make_table(char *afile, uint aline, Array *acolumns, int initial_rows) { | |
| return *new(this) Table(this, afile, aline, acolumns, initial_rows); | private: |
| }*/ | |
| /// throws register cleanup exception | |
| void fail_register_cleanup() const; | |
| private: //disabled | private: //disabled |
| Pool(Pool&) {} | Pool(const Pool&); |
| Pool& operator = (Pool&) { return *this; } | Pool& operator= (const Pool&); |
| }; | |
| /** | |
| Base for all classes that are allocated in 'pools'. | |
| Holds Pool object. | |
| */ | |
| class Pooled { | |
| // the pool i'm allocated on | |
| Pool& fpool; | |
| public: | |
| Pooled(Pool& apool); | |
| /// my pool | |
| //Pool& pool() const { return *fpool; } | |
| /// Sole: this got called automatically from Pool::~Pool() | |
| virtual ~Pooled(); | |
| }; | }; |
| #endif | #endif |