ruby: Make ruby's Map use hashmap.hh to simplify things.
[gem5.git] / src / mem / gems_common / Allocator.hh
1 /*
2 * Copyright (c) 1999 by Mark Hill and David Wood for the Wisconsin
3 * Multifacet Project. ALL RIGHTS RESERVED.
4 *
5 * ##HEADER##
6 *
7 * This software is furnished under a license and may be used and
8 * copied only in accordance with the terms of such license and the
9 * inclusion of the above copyright notice. This software or any
10 * other copies thereof or any derivative works may not be provided or
11 * otherwise made available to any other persons. Title to and
12 * ownership of the software is retained by Mark Hill and David Wood.
13 * Any use of this software must include the above copyright notice.
14 *
15 * THIS SOFTWARE IS PROVIDED "AS IS". THE LICENSOR MAKES NO
16 * WARRANTIES ABOUT ITS CORRECTNESS OR PERFORMANCE.
17 * */
18
19 /*
20 * $Id$
21 */
22
23 #ifndef ALLOCATOR_H
24 #define ALLOCATOR_H
25
26 #include "mem/gems_common/Vector.hh"
27
28 template <class TYPE>
29 class Allocator {
30 public:
31 // Constructors
32 Allocator() { m_counter = 0; }
33
34 // Destructor
35 ~Allocator() { for(int i=0; i<m_pool_vec.size(); i++) { delete m_pool_vec[i]; }}
36
37 // Public Methods
38 TYPE* allocate(const TYPE& obj);
39 void deallocate(TYPE* obj_ptr);
40 private:
41 // Private copy constructor and assignment operator
42 Allocator(const Allocator& obj);
43 Allocator& operator=(const Allocator& obj);
44
45 // Private Methods
46
47 // Data Members (m_ prefix)
48 Vector<TYPE*> m_pool_vec;
49 int m_counter;
50 };
51
52 template <class TYPE>
53 inline
54 TYPE* Allocator<TYPE>::allocate(const TYPE& obj)
55 {
56 m_counter++;
57 DEBUG_EXPR(ALLOCATOR_COMP, LowPrio, m_counter);
58 TYPE* new_obj_ptr;
59
60 // See if we need to allocate any new objects
61 if (m_pool_vec.size() == 0) {
62 // Allocate a new item
63 m_pool_vec.insertAtBottom(new TYPE);
64 }
65
66 // Pop the pointer from the stack/pool
67 int size = m_pool_vec.size();
68 new_obj_ptr = m_pool_vec[size-1];
69 m_pool_vec.setSize(size-1);
70
71 // Copy the object
72 *new_obj_ptr = obj;
73 return new_obj_ptr;
74 }
75
76 template <class TYPE>
77 inline
78 void Allocator<TYPE>::deallocate(TYPE* obj)
79 {
80 m_pool_vec.insertAtBottom(obj);
81 }
82
83 #endif //ALLOCATOR_H