CACAO
Allocator.hpp
Go to the documentation of this file.
1 /* src/vm/jit/compiler2/alloc/Allocator.hpp - Custom allocator for container
2 
3  Copyright (C) 2013
4  CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 
6  This file is part of CACAO.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2, or (at
11  your option) any later version.
12 
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  02110-1301, USA.
22 
23 */
24 
25 #ifndef _JIT_COMPILER2_ALLOC_ALLOCATOR
26 #define _JIT_COMPILER2_ALLOC_ALLOCATOR
27 
28 #include <memory>
29 #include "vm/statistics.hpp"
30 
31 namespace cacao {
32 namespace jit {
33 namespace compiler2 {
34 namespace alloc {
35 
36 STAT_DECLARE_VAR(std::size_t,comp2_allocated,0)
37 STAT_DECLARE_VAR(std::size_t,comp2_deallocated,0)
38 STAT_DECLARE_VAR(std::size_t,comp2_max,0)
39 
40 std::size_t& current_heap_size();
41 
42 template<class T, class BaseAllocator = std::allocator<T> >
43 class Allocator : public BaseAllocator {
44 public:
45  typedef typename BaseAllocator::pointer pointer;
46  typedef typename BaseAllocator::size_type size_type;
47 
48  Allocator() throw() : BaseAllocator() {}
49  Allocator(const Allocator& other) throw() : BaseAllocator(other) {}
50  template <class U>
51  Allocator(const Allocator<U, typename BaseAllocator::template rebind<U>::other> &other) throw() : BaseAllocator(other) {}
53 
54  /**
55  * @note See @cite MeyersEffectiveSTL2001, Item why this is needed.
56  */
57  template<class U>
58  struct rebind {
60  };
61 
62  pointer allocate(size_type n) {
63  pointer r = BaseAllocator::allocate(n);
64  current_heap_size() += n;
65  STATISTICS(comp2_allocated+=n);
66  STATISTICS(comp2_max.max(current_heap_size()));
67  return r;
68  }
69 
70  pointer allocate(size_type n, pointer h) {
71  pointer r = BaseAllocator::allocate(n, h);
72  current_heap_size() += n;
73  STATISTICS(comp2_allocated+=n);
74  STATISTICS(comp2_max.max(current_heap_size()));
75  return r;
76  }
77 
78  void deallocate(pointer p, size_type n) throw() {
79  BaseAllocator::deallocate(p, n);
80  current_heap_size() -= n;
81  STATISTICS(comp2_deallocated+=n);
82  }
83 };
84 
85 } // end namespace alloc
86 } // end namespace compiler2
87 } // end namespace jit
88 } // end namespace cacao
89 
90 #endif /* _JIT_COMPILER2_ALLOC_ALLOCATOR */
91 
92 
93 /*
94  * These are local overrides for various environment variables in Emacs.
95  * Please do not remove this and leave it at the end of the file, where
96  * Emacs will automagically detect them.
97  * ---------------------------------------------------------------------
98  * Local variables:
99  * mode: c++
100  * indent-tabs-mode: t
101  * c-basic-offset: 4
102  * tab-width: 4
103  * End:
104  * vim:noexpandtab:sw=4:ts=4:
105  */
#define STATISTICS(x)
Wrapper for statistics only code.
Definition: statistics.hpp:975
void deallocate(pointer p, size_type n)
Definition: Allocator.hpp:78
BaseAllocator::size_type size_type
Definition: Allocator.hpp:46
Allocator(const Allocator< U, typename BaseAllocator::template rebind< U >::other > &other)
Definition: Allocator.hpp:51
Allocator(const Allocator &other)
Definition: Allocator.hpp:49
This file contains the statistics framework.
Allocator< U, typename BaseAllocator::template rebind< U >::other > other
Definition: Allocator.hpp:59
pointer allocate(size_type n, pointer h)
Definition: Allocator.hpp:70
#define STAT_DECLARE_VAR(type, var, init)
Declare an external statistics variable.
Definition: statistics.hpp:963
std::size_t & current_heap_size()
Definition: Allocator.cpp:37