-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathelab_alloc.h
110 lines (89 loc) · 2.43 KB
/
elab_alloc.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
//
// Created by ripopov on 12/18/17.
//
#ifndef ELAB_ALLOC_H
#define ELAB_ALLOC_H
#include <sc_elab/allocated_node.h>
#include <cstdint>
#include <typeinfo>
#ifndef _MSC_VER
template<typename T> std::string MANGLED_TYPENAME() {
return typeid(T).name();
}
template<typename T> std::string MANGLED_TYPENAME(T &&var) {
return typeid(var).name();
}
#else
inline std::string adjust_msvc_rawname (const char *rawname)
{
std::string name = (rawname + 1);
if (name[0] == '?')
return name;
else
return "?A" + name;
}
template<typename T> std::string MANGLED_TYPENAME() {
return adjust_msvc_rawname(typeid(T).raw_name());
}
template<typename T> std::string MANGLED_TYPENAME(T &&var) {
return adjust_msvc_rawname(typeid(var).raw_name());
}
#endif // !_MSC_VER
/**
* Dynamic memory allocation tracking for SystemC elaboration
*
* Currently elaborator supports following allocation methods:
*
* 1.
* template<class T, class... Args>
* T* sc_new(Args&&... args)
*
* usage example: my_module = sc_new<my_module> ("my_module");
*
* 2.
* template<class T>
* T* sc_new_array(size_t n)
*
* usage example: int * array_ptr = sc_new_array<int> (10); // create int[10]
*
* 3.
* All classes derived from sc_objects can use raw new, it is overloaded in
* sc_object
*
*/
namespace sc_elab {
///
/// Store information about dynamically allocated object
/// @param ptr - pointer to newly allocated object
/// @param is_array - true for new[]
/// @param type_name - mangled type name
/// @param sizeof_alloc - size of object
/// @param array_size - N, number of array elements for new[N]
///
void trace_alloc_impl(void *ptr,
bool is_array,
const char *type_name,
size_t sizeof_alloc,
size_t array_size = 0);
}
namespace sc_core
{
/// Allocate singular object dynamically and store information for SVC elaboration
template<class T, class... Args>
T* sc_new(Args&&... args)
{
T* rptr = ::new T(std::forward<Args>(args)...);
sc_elab::trace_alloc_impl(rptr, false, MANGLED_TYPENAME<T>().c_str() , sizeof(T));
return rptr;
}
/// Allocate array dynamically and store information for SVC elaboration
template<class T>
T* sc_new_array(size_t n)
{
T* rptr = ::new T[n];
if (n > 0)
sc_elab::trace_alloc_impl(rptr, true, MANGLED_TYPENAME<T>().c_str(), sizeof(T)*n, n);
return rptr;
}
}
#endif // ELAB_ALLOC_H