summaryrefslogtreecommitdiff
path: root/cpp/src/IceUtil/MemoryPool.cpp
blob: aab2fcfba542a5883cc4891af65de251d2de0f9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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);
}