-
Notifications
You must be signed in to change notification settings - Fork 718
/
Copy pathmemorymanager.cpp
167 lines (135 loc) · 4.31 KB
/
memorymanager.cpp
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
// STD
#include <utility>
// SEALNet
#include "seal/c/memorymanager.h"
#include "seal/c/utilities.h"
// SEAL
#include "seal/memorymanager.h"
using namespace std;
using namespace seal;
using namespace seal::c;
namespace
{
template <class T>
HRESULT GenericCreateProfileCopy(T *original, MMProf **copyptr)
{
T *copy = new T(*original);
*copyptr = copy;
return S_OK;
}
HRESULT CreateProfileCopy(MMProf *profile, MMProf **copyptr)
{
IfNullRet(profile, E_POINTER);
IfNullRet(copyptr, E_POINTER);
MMProfGlobal *global = dynamic_cast<MMProfGlobal *>(profile);
if (nullptr != global)
{
return GenericCreateProfileCopy(global, copyptr);
}
MMProfFixed *fixed = dynamic_cast<MMProfFixed *>(profile);
if (nullptr != fixed)
{
return GenericCreateProfileCopy(fixed, copyptr);
}
MMProfNew *newprof = dynamic_cast<MMProfNew *>(profile);
if (nullptr != newprof)
{
return GenericCreateProfileCopy(newprof, copyptr);
}
MMProfThreadLocal *threadlocal = dynamic_cast<MMProfThreadLocal *>(profile);
if (nullptr != threadlocal)
{
return GenericCreateProfileCopy(threadlocal, copyptr);
}
// No matching profile.
return E_UNEXPECTED;
}
} // namespace
SEAL_C_FUNC MemoryManager_GetPool1(int prof_opt, bool clear_on_destruction, void **pool_handle)
{
IfNullRet(pool_handle, E_POINTER);
mm_prof_opt profile_opt = static_cast<mm_prof_opt>(prof_opt);
MemoryPoolHandle handle;
// clear_on_destruction is only used when using mm_force_new
if (profile_opt == mm_prof_opt::mm_force_new)
{
handle = MemoryManager::GetPool(profile_opt, clear_on_destruction);
}
else
{
handle = MemoryManager::GetPool(profile_opt);
}
MemoryPoolHandle *handle_ptr = new MemoryPoolHandle(move(handle));
*pool_handle = handle_ptr;
return S_OK;
}
SEAL_C_FUNC MemoryManager_GetPool2(void **pool_handle)
{
IfNullRet(pool_handle, E_POINTER);
MemoryPoolHandle handle = MemoryManager::GetPool();
MemoryPoolHandle *handle_ptr = new MemoryPoolHandle(move(handle));
*pool_handle = handle_ptr;
return S_OK;
}
SEAL_C_FUNC MemoryManager_SwitchProfile(void *new_profile)
{
MMProf *profile = FromVoid<MMProf>(new_profile);
IfNullRet(profile, E_POINTER);
// SwitchProfile takes ownership of the profile pointer that is passed.
// The managed side will keep ownership of the new_profile parameter, so we
// need to make a copy that will be owned by the Memory Manager.
MMProf *new_mm_profile = nullptr;
IfFailRet(CreateProfileCopy(profile, &new_mm_profile));
MemoryManager::SwitchProfile(move(static_cast<MMProf *>(new_mm_profile)));
return S_OK;
}
SEAL_C_FUNC MMProf_CreateGlobal(void **profile)
{
IfNullRet(profile, E_POINTER);
MMProfGlobal *global = new MMProfGlobal();
*profile = global;
return S_OK;
}
SEAL_C_FUNC MMProf_CreateFixed(void *pool, void **profile)
{
MemoryPoolHandle *poolptr = FromVoid<MemoryPoolHandle>(pool);
IfNullRet(poolptr, E_POINTER);
IfNullRet(profile, E_POINTER);
MemoryPoolHandle myhandle(*poolptr);
MMProfFixed *fixed = new MMProfFixed(myhandle);
*profile = fixed;
return S_OK;
}
SEAL_C_FUNC MMProf_CreateNew(void **profile)
{
IfNullRet(profile, E_POINTER);
MMProfNew *newprof = new MMProfNew();
*profile = newprof;
return S_OK;
}
SEAL_C_FUNC MMProf_CreateThreadLocal(void **profile)
{
IfNullRet(profile, E_POINTER);
MMProfThreadLocal *threadlocal = new MMProfThreadLocal();
*profile = threadlocal;
return S_OK;
}
SEAL_C_FUNC MMProf_GetPool(void *thisptr, void **pool_handle)
{
MMProf *profile = FromVoid<MMProf>(thisptr);
IfNullRet(profile, E_POINTER);
IfNullRet(pool_handle, E_POINTER);
// The parameter to get_pool is always ignored, so just pass 0
MemoryPoolHandle *handle_ptr = new MemoryPoolHandle(profile->get_pool(0));
*pool_handle = handle_ptr;
return S_OK;
}
SEAL_C_FUNC MMProf_Destroy(void *thisptr)
{
MMProf *profile = FromVoid<MMProf>(thisptr);
IfNullRet(profile, E_POINTER);
delete profile;
return S_OK;
}