-
Notifications
You must be signed in to change notification settings - Fork 0
/
allocator.cu
executable file
·105 lines (93 loc) · 2.31 KB
/
allocator.cu
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
/*
# Copyright (c) 2011-2012 NVIDIA CORPORATION. All Rights Reserved.
#
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto. Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.
*/
#include <allocator.h>
#include <stack>
#include <map>
#include <vector.h>
template< typename T >
inline T* allocate( int size )
{
return new T( size );
}
template<typename T>
typename Allocator<T>::FreeMap& Allocator<T>::getFreeVars( )
{
static FreeMap s_free_vars;
return s_free_vars;
}
template<typename T>
T*
Allocator<T>::allocate(int size)
{
T *v;
bool is_empty;
{
//locate free var list for the right size
FreeList &f_vars=getFreeVars( )[size];
is_empty = f_vars.empty();
if (!is_empty) {
//set the return value to the previously freed vector
v=f_vars.top();
//remove the vector from the free vector list
f_vars.pop();
}
}
if(is_empty) //if there are no free vectors
{
//create a new vector
//v=allocate<T>(size);
v = new T(size);
}
return v;
}
template<typename T>
T* Allocator<T>::free(T* v,int size) {
//add the vector to the free vector list
{
if( v != NULL )
getFreeVars( )[size].push(v);
}
return NULL;
}
template<typename T>
T*
Allocator<T>::resize( T *ptr, int old_size, int new_size )
{
if( old_size == new_size )
return ptr;
free( ptr, old_size );
return allocate( new_size );
}
template<typename T>
void Allocator<T>::clear() {
{
FreeMap &free_vars = getFreeVars( );
for(typename FreeMap::iterator m_iter=free_vars.begin();m_iter!=free_vars.end();m_iter++)
{
FreeList &stack=m_iter->second;
while(!stack.empty()) {
T *ptr = stack.top();
stack.pop();
delete ptr;
}
}
free_vars.clear();
}
}
/****************************************
* Explict instantiations
***************************************/
// here we imply Index_Type is int
template class Allocator<Vector_d<float> >;
template class Allocator<Vector_d<double> >;
template class Allocator<Vector_d<int> >;
template class Allocator<Vector_h<float> >;
template class Allocator<Vector_h<double> >;
template class Allocator<Vector_h<int> >;