diff options
author | Dwayne Boone <dwayne@zeroc.com> | 2005-02-02 15:45:55 +0000 |
---|---|---|
committer | Dwayne Boone <dwayne@zeroc.com> | 2005-02-02 15:45:55 +0000 |
commit | c840d965365ebf0e8aef6ac8f91cac54a230643d (patch) | |
tree | 058849928a43ca4d498e3530d86614d1d3b3e3f3 /cpp/src | |
parent | backed out change (diff) | |
download | ice-c840d965365ebf0e8aef6ac8f91cac54a230643d.tar.bz2 ice-c840d965365ebf0e8aef6ac8f91cac54a230643d.tar.xz ice-c840d965365ebf0e8aef6ac8f91cac54a230643d.zip |
Added MemoryPool to IceUtil
Diffstat (limited to 'cpp/src')
-rw-r--r-- | cpp/src/IceUtil/Makefile | 1 | ||||
-rwxr-xr-x | cpp/src/IceUtil/MemoryPool.cpp | 119 | ||||
-rw-r--r-- | cpp/src/IceUtil/iceutil.dsp | 8 |
3 files changed, 128 insertions, 0 deletions
diff --git a/cpp/src/IceUtil/Makefile b/cpp/src/IceUtil/Makefile index f4be2503cef..90a3b8f8706 100644 --- a/cpp/src/IceUtil/Makefile +++ b/cpp/src/IceUtil/Makefile @@ -26,6 +26,7 @@ OBJS = Base64.o \ InputUtil.o \ MD5.o \ MD5I.o \ + MemoryPool.o \ Options.o \ OutputUtil.o \ RWRecMutex.o \ diff --git a/cpp/src/IceUtil/MemoryPool.cpp b/cpp/src/IceUtil/MemoryPool.cpp new file mode 100755 index 00000000000..aab2fcfba54 --- /dev/null +++ b/cpp/src/IceUtil/MemoryPool.cpp @@ -0,0 +1,119 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2005 ZeroC, Inc. All rights reserved. +// +// This copy of Ice is licensed to you under the terms described in the +// ICE_LICENSE file included in this distribution. +// +// ********************************************************************** + +#include <IceUtil/MemoryPool.h> + +using namespace std; + +bool IceUtil::SimpleMemoryPool::_usePool = true; +int IceUtil::SimpleMemoryPool::_traceLevel = 0; + +IceUtil::SimpleMemoryPool::SimpleMemoryPool(const char* name, int num) : + _name(name), + _num(num), + _pool(0) +{ +} + +void* +IceUtil::SimpleMemoryPool::operatorNew(size_t sz) +{ + if(!_usePool) + { + return ::operator new(sz); + } + + sz = (sz + 7) & 0xffffff8U; + + if(_pool == 0 || *reinterpret_cast<void**>(_pool) == 0) + { + int total = 8 + sz; + char* p = reinterpret_cast<char*>(malloc(_num * total)); + + int i; + for(i = 0; i < _num - 1; ++i) + { + *reinterpret_cast<void**>(p + i * total) = p + (i + 1) * total; + } + *reinterpret_cast<void**>(p + i * total) = 0; + + if(_pool == 0) + { + _pool = p; + } + else + { + *reinterpret_cast<void**>(_pool) = p; + } + } + + void* result = reinterpret_cast<char*>(_pool) + 8; + _pool = *reinterpret_cast<void**>(_pool); + return result; +} + +void +IceUtil::SimpleMemoryPool::operatorDelete(void* p) +{ + if(!_usePool) + { + ::operator delete(p); + return; + } + + if(p == 0) + { + return; + } + + *reinterpret_cast<void**>(reinterpret_cast<char*>(p) - 8) = _pool; + _pool = reinterpret_cast<char*>(p) - 8; +} + +void +IceUtil::SimpleMemoryPool::usePool(bool b) +{ + _usePool = b; +} + +void +IceUtil::SimpleMemoryPool::traceLevel(int lvl) +{ + _traceLevel = lvl; +} + +IceUtil::MemoryPool::MemoryPool(const char* name, int num) : + SimpleMemoryPool(name, num) +{ +} + +void* +IceUtil::MemoryPool::operatorNew(size_t sz) +{ + if(!_usePool) + { + return ::operator new(sz); + } + + IceUtil::Mutex::Lock lock(*this); + return SimpleMemoryPool::operatorNew(sz); +} + +void +IceUtil::MemoryPool::operatorDelete(void* p) +{ + if(!_usePool) + { + ::operator delete(p); + return; + } + + IceUtil::Mutex::Lock lock(*this); + SimpleMemoryPool::operatorDelete(p); +} diff --git a/cpp/src/IceUtil/iceutil.dsp b/cpp/src/IceUtil/iceutil.dsp index 46012592e12..25f84bf03e2 100644 --- a/cpp/src/IceUtil/iceutil.dsp +++ b/cpp/src/IceUtil/iceutil.dsp @@ -150,6 +150,10 @@ SOURCE=.\MD5I.cpp # End Source File
# Begin Source File
+SOURCE=.\MemoryPool.cpp
+# End Source File
+# Begin Source File
+
SOURCE=.\Options.cpp
# End Source File
# Begin Source File
@@ -266,6 +270,10 @@ SOURCE=MD5I.h # End Source File
# Begin Source File
+SOURCE=..\..\include\IceUtil\MemoryPool.h
+# End Source File
+# Begin Source File
+
SOURCE=..\..\include\iceutil\Monitor.h
# End Source File
# Begin Source File
|