diff options
Diffstat (limited to 'cpp')
-rwxr-xr-x | cpp/include/IceUtil/MemoryPool.h | 56 | ||||
-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 |
4 files changed, 184 insertions, 0 deletions
diff --git a/cpp/include/IceUtil/MemoryPool.h b/cpp/include/IceUtil/MemoryPool.h new file mode 100755 index 00000000000..8b04bf6fe9b --- /dev/null +++ b/cpp/include/IceUtil/MemoryPool.h @@ -0,0 +1,56 @@ +// **********************************************************************
+//
+// 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.
+//
+// **********************************************************************
+
+#ifndef UTIL_MEMORY_POOL_H
+#define UTIL_MEMORY_POOL_H
+
+#include <IceUtil/Mutex.h>
+
+namespace IceUtil
+{
+
+class ICE_UTIL_API SimpleMemoryPool
+{
+public:
+
+ SimpleMemoryPool(const char*, int);
+
+ void* operatorNew(size_t);
+ void operatorDelete(void*);
+
+ static void usePool(bool);
+ static void traceLevel(int);
+
+protected:
+
+ static bool _usePool;
+
+private:
+
+ const char* _name;
+ const int _num;
+
+ void* _pool;
+
+ static int _traceLevel;
+};
+
+class ICE_UTIL_API MemoryPool : public SimpleMemoryPool, IceUtil::Mutex
+{
+public:
+
+ MemoryPool(const char*, int);
+
+ void* operatorNew(size_t);
+ void operatorDelete(void*);
+};
+
+}
+
+#endif
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
|