-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmcmalloc.cpp
156 lines (132 loc) · 4.4 KB
/
mcmalloc.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
/*
* Copyright 2017 Yamana Laboratory, Waseda University
* Supported by JST CREST Grant Number JPMJCR1503, Japan.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mcmalloc.hpp"
const bool mcmallocDebugFlag = false;
// NOTE: 1:main thread + 1:HElib thread pool control thread + 8:HElib thread pool threds + 72:parallel processing threads
const int nThread = 1 + 1 + 8 + 72;
mc::MCMalloc<nThread> mcmalloc;
bool initFlag = false;
thread_local struct ThreadLocalData {
inline int &Index() { return index_; }
inline bool &MainFlag() { return mainFlag_; }
inline bool &InitFlag() { return initFlag_; }
int index_;
bool mainFlag_;
bool initFlag_;
} threadLocalData = {0, false, false};
#define _threadInit() \
if (UNLIKELY(!threadLocalData.InitFlag())) { \
threadLocalData.InitFlag() = true; \
_init(); \
if (threadLocalData.MainFlag() && !initFlag) \
initFlag = true, mcmalloc.Init(); \
}
void mainInit() {
threadLocalData.MainFlag() = true;
mcmalloc._Init();
}
void mainTerm() {}
void threadInit() {
if (!threadLocalData.MainFlag())
threadLocalData.Index() = threadutil::NewCurrentThreadIndex();
eassert(threadLocalData.Index() != -1 && threadLocalData.Index() < nThread,
"required: threadIndex != -1 && threadIndex(%d) < nThread(%d)",
threadLocalData.Index(), nThread);
}
void threadTerm() {
if (!threadLocalData.MainFlag()) {
bool ret = threadutil::DeleteCurrentThreadIndex();
eassert(ret, "[mcmalloc threadutil::DeleteCurrentThreadIndex failed]");
// TODO: error processing
ret = true;
// NOTE: unmap mmap buffer
batchMmapTerm();
}
}
#ifdef __APPLE__
extern "C" {
#endif
void *malloc(size_t size) {
if (UNLIKELY(size == 0)) return nullptr;
const int threadIndex = threadLocalData.Index();
if (UNLIKELY(!threadLocalData.InitFlag())) {
threadLocalData.InitFlag() = true;
_init();
if (threadLocalData.MainFlag() && !initFlag)
initFlag = true, mcmalloc.Init();
return malloc(size);
}
void *ptr = mcmalloc.Malloc(size, threadIndex);
if (mcmallocDebugFlag)
myprintf("#====malloc: size=%8d, ptr=%p\n", (int)size, ptr);
if (UNLIKELY(ptr == nullptr)) errno = ENOMEM;
return ptr;
}
void free(void *ptr) {
if (mcmallocDebugFlag) myprintf("#====free: ptr=%p\n", ptr);
if (UNLIKELY(ptr == nullptr)) return;
_threadInit();
bool ret = mcmalloc.Free(ptr, threadLocalData.Index());
eassert(ret, "[mcmallocmaloc free failed]");
return;
}
void *calloc(size_t nmemb, size_t size) {
if (UNLIKELY(nmemb == 0 || size == 0)) return nullptr;
_threadInit();
const size_t total_size = nmemb * size;
void *ptr = malloc(total_size);
if (ptr != nullptr) memset(ptr, 0, total_size);
if (mcmallocDebugFlag)
myprintf("#====calloc: nmemb=%d, size=%8d, ptr=%p\n", (int)nmemb, (int)size,
ptr);
return ptr;
}
#ifdef __APPLE__
void *realloc(void *ptr, size_t size) {
#else
void *realloc(void *ptr, size_t size) throw() {
#endif
if (UNLIKELY(ptr == nullptr)) return malloc(size);
_threadInit();
void *newPtr = mcmalloc.Realloc(ptr, size, threadLocalData.Index());
if (mcmallocDebugFlag)
myprintf("#====realloc: ptr=%p, size=%8d, newPtr=%p\n", ptr, (int)size,
newPtr);
return newPtr;
}
int posix_memalign(void **memptr, size_t alignment, size_t size) throw() {
if (UNLIKELY(size == 0)) {
*memptr = nullptr;
return 0;
}
_threadInit();
if (mcmallocDebugFlag)
myprintf("#====posix_memalign: memptr=%p, alignment=%8d, size=%d\n",
(void *)memptr, (int)alignment, (int)size);
int ret =
mcmalloc.PosixMemalign(memptr, alignment, size, threadLocalData.Index());
// FYI:
// // 0:success
// // 12:ENOMEM 12 /* Out of memory */
// // 22:EINVAL 22 /* Invalid argument */
return ret;
}
// TODO: memalign
// TODO: valloc
#ifdef __APPLE__
}
#endif