Ensure that the memory pool can be reused after a rewind and get rid of the segmented array.

This commit is contained in:
Roberto Raggi
2010-03-18 15:21:07 +01:00
parent 1e2af0a77d
commit 61a504c427
17 changed files with 132 additions and 262 deletions

View File

@@ -54,6 +54,9 @@
namespace CPlusPlus {
class MemoryPool;
class RecursiveMemoryPool;
class CPLUSPLUS_EXPORT MemoryPool
{
MemoryPool(const MemoryPool &other);
@@ -66,33 +69,19 @@ public:
bool initializeAllocatedMemory() const;
void setInitializeAllocatedMemory(bool initializeAllocatedMemory);
void reset();
inline void *allocate(size_t size)
{
size = (size + 7) & ~7;
if (ptr && (ptr + size < end)) {
void *addr = ptr;
ptr += size;
if (_ptr && (_ptr + size < _end)) {
void *addr = _ptr;
_ptr += size;
return addr;
}
return allocate_helper(size);
}
struct State
{
char *ptr;
char *end;
int blockCount;
inline bool isValid() const
{ return ptr != 0; }
inline State(char *ptr = 0, int blockCount = 0)
: ptr(ptr), blockCount(blockCount) {}
};
State state() const;
void rewind(const State &state);
private:
void *allocate_helper(size_t size);
@@ -101,13 +90,29 @@ private:
char **_blocks;
int _allocatedBlocks;
int _blockCount;
char *ptr, *end;
char *_ptr;
char *_end;
int _ccc;
enum
{
BLOCK_SIZE = 8 * 1024,
DEFAULT_BLOCK_COUNT = 8
};
friend class RecursiveMemoryPool;
};
class CPLUSPLUS_EXPORT RecursiveMemoryPool
{
MemoryPool *_pool;
int _blockCount;
char *_ptr;
char *_end;
public:
RecursiveMemoryPool(MemoryPool *pool);
~RecursiveMemoryPool();
};
class CPLUSPLUS_EXPORT Managed