blob: 76333fd5f11ea815b593c55c7f2576da8f68415b (
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
|
/*
* Copyright (c) 1999 by Mark Hill and David Wood for the Wisconsin
* Multifacet Project. ALL RIGHTS RESERVED.
*
* ##HEADER##
*
* This software is furnished under a license and may be used and
* copied only in accordance with the terms of such license and the
* inclusion of the above copyright notice. This software or any
* other copies thereof or any derivative works may not be provided or
* otherwise made available to any other persons. Title to and
* ownership of the software is retained by Mark Hill and David Wood.
* Any use of this software must include the above copyright notice.
*
* THIS SOFTWARE IS PROVIDED "AS IS". THE LICENSOR MAKES NO
* WARRANTIES ABOUT ITS CORRECTNESS OR PERFORMANCE.
* */
/*
* $Id$
*/
#ifndef ALLOCATOR_H
#define ALLOCATOR_H
#include "mem/gems_common/Vector.hh"
template <class TYPE>
class Allocator {
public:
// Constructors
Allocator() { m_counter = 0; }
// Destructor
~Allocator() { for(int i=0; i<m_pool_vec.size(); i++) { delete m_pool_vec[i]; }}
// Public Methods
TYPE* allocate(const TYPE& obj);
void deallocate(TYPE* obj_ptr);
private:
// Private copy constructor and assignment operator
Allocator(const Allocator& obj);
Allocator& operator=(const Allocator& obj);
// Private Methods
// Data Members (m_ prefix)
Vector<TYPE*> m_pool_vec;
int m_counter;
};
template <class TYPE>
inline
TYPE* Allocator<TYPE>::allocate(const TYPE& obj)
{
m_counter++;
DEBUG_EXPR(ALLOCATOR_COMP, LowPrio, m_counter);
TYPE* new_obj_ptr;
// See if we need to allocate any new objects
if (m_pool_vec.size() == 0) {
// Allocate a new item
m_pool_vec.insertAtBottom(new TYPE);
}
// Pop the pointer from the stack/pool
int size = m_pool_vec.size();
new_obj_ptr = m_pool_vec[size-1];
m_pool_vec.setSize(size-1);
// Copy the object
*new_obj_ptr = obj;
return new_obj_ptr;
}
template <class TYPE>
inline
void Allocator<TYPE>::deallocate(TYPE* obj)
{
m_pool_vec.insertAtBottom(obj);
}
#endif //ALLOCATOR_H
|