-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhamr_new_allocator_impl.h
121 lines (96 loc) · 2.75 KB
/
hamr_new_allocator_impl.h
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
120
121
#ifndef hamr_new_allocator_impl_h
#define hamr_new_allocator_impl_h
//#include "hamr_new_allocator.h"
#include <iostream>
#include <type_traits>
#include <memory>
#include <typeinfo>
#include <cassert>
#include <cstring>
namespace hamr
{
// --------------------------------------------------------------------------
template <typename T>
new_deleter<T>::new_deleter(T *ptr, size_t n) : m_ptr(ptr), m_elem(n)
{
#if defined(HAMR_VERBOSE)
if (hamr::get_verbose())
{
std::cerr << "created new_deleter for array of " << n
<< " objects of type " << typeid(T).name() << std::endl;
}
#endif
}
// --------------------------------------------------------------------------
template <typename T>
void new_deleter<T>::operator()(T *ptr)
{
assert(ptr == m_ptr);
#if defined(HAMR_VERBOSE)
if (hamr::get_verbose())
{
std::cerr << "new_deleter deleting array of " << m_elem
<< " objects of type " << typeid(T).name() << std::endl;
}
#endif
delete [] ptr;
}
// --------------------------------------------------------------------------
template <typename T>
std::shared_ptr<T> new_allocator<T>::allocate(size_t n)
{
#if defined(HAMR_VERBOSE)
if (hamr::get_verbose())
{
std::cerr << "new_allocator allocating array of " << n
<< " objects of type " << typeid(T).name() << std::endl;
}
#endif
// allocate
T *ptr = new T[n];
// package
return std::shared_ptr<T>(ptr, new_deleter<T>(ptr, n));
}
// --------------------------------------------------------------------------
template <typename T>
std::shared_ptr<T> new_allocator<T>::allocate(size_t n, const T &val)
{
#if defined(HAMR_VERBOSE)
if (hamr::get_verbose())
{
std::cerr << "new_allocator allocating array of " << n
<< " objects of type " << typeid(T).name() << " initialized"
<< std::endl;
}
#endif
// allocate
T *ptr = (T*)new unsigned char[n*sizeof(T)];
// construct
for (size_t i = 0; i < n; ++i)
new (&ptr[i]) T(val);
// package
return std::shared_ptr<T>(ptr, new_deleter<T>(ptr, n));
}
// --------------------------------------------------------------------------
template <typename T>
template <typename U>
std::shared_ptr<T> new_allocator<T>::allocate(size_t n, const U *vals)
{
#if defined(HAMR_VERBOSE)
if (hamr::get_verbose())
{
std::cerr << "new_allocator allocating array of " << n
<< " objects of type " << typeid(T).name() << " initialized"
<< std::endl;
}
#endif
// allocate
T *ptr = (T*)new unsigned char[n*sizeof(T)];
// construct
for (size_t i = 0; i < n; ++i)
new (&ptr[i]) T(vals[i]);
// package
return std::shared_ptr<T>(ptr, new_deleter<T>(ptr, n));
}
};
#endif