add LockTraits
[folly.git] / folly / Malloc.h
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 // Functions to provide smarter use of jemalloc, if jemalloc is being used.
18 // http://www.canonware.com/download/jemalloc/jemalloc-latest/doc/jemalloc.html
19
20 #pragma once
21 #define FOLLY_MALLOC_H_
22
23 /**
24  * Define various MALLOCX_* macros normally provided by jemalloc.  We define
25  * them so that we don't have to include jemalloc.h, in case the program is
26  * built without jemalloc support.
27  */
28 #ifndef MALLOCX_LG_ALIGN
29 #define MALLOCX_LG_ALIGN(la) (la)
30 #endif
31 #ifndef MALLOCX_ZERO
32 #define MALLOCX_ZERO (static_cast<int>(0x40))
33 #endif
34
35 // If using fbstring from libstdc++ (see comment in FBString.h), then
36 // just define stub code here to typedef the fbstring type into the
37 // folly namespace.
38 // This provides backwards compatibility for code that explicitly
39 // includes and uses fbstring.
40 #if defined(_GLIBCXX_USE_FB) && !defined(_LIBSTDCXX_FBSTRING)
41
42 #include <folly/detail/Malloc.h>
43
44 #include <string>
45
46 namespace folly {
47   using std::goodMallocSize;
48   using std::jemallocMinInPlaceExpandable;
49   using std::usingJEMalloc;
50   using std::smartRealloc;
51   using std::checkedMalloc;
52   using std::checkedCalloc;
53   using std::checkedRealloc;
54 }
55
56 #else // !defined(_GLIBCXX_USE_FB) || defined(_LIBSTDCXX_FBSTRING)
57
58 #ifdef _LIBSTDCXX_FBSTRING
59 #pragma GCC system_header
60
61 /**
62  * Declare *allocx() and mallctl*() as weak symbols. These will be provided by
63  * jemalloc if we are using jemalloc, or will be NULL if we are using another
64  * malloc implementation.
65  */
66 extern "C" void* mallocx(size_t, int)
67 __attribute__((__weak__));
68 extern "C" void* rallocx(void*, size_t, int)
69 __attribute__((__weak__));
70 extern "C" size_t xallocx(void*, size_t, size_t, int)
71 __attribute__((__weak__));
72 extern "C" size_t sallocx(const void*, int)
73 __attribute__((__weak__));
74 extern "C" void dallocx(void*, int)
75 __attribute__((__weak__));
76 extern "C" void sdallocx(void*, size_t, int)
77 __attribute__((__weak__));
78 extern "C" size_t nallocx(size_t, int)
79 __attribute__((__weak__));
80 extern "C" int mallctl(const char*, void*, size_t*, void*, size_t)
81 __attribute__((__weak__));
82 extern "C" int mallctlnametomib(const char*, size_t*, size_t*)
83 __attribute__((__weak__));
84 extern "C" int mallctlbymib(const size_t*, size_t, void*, size_t*, void*,
85                             size_t)
86 __attribute__((__weak__));
87
88 #include <bits/functexcept.h>
89 #define FOLLY_HAVE_MALLOC_H 1
90 #else
91 #include <folly/detail/Malloc.h> /* nolint */
92 #endif
93
94 // for malloc_usable_size
95 // NOTE: FreeBSD 9 doesn't have malloc.h.  It's defitions
96 // are found in stdlib.h.
97 #if FOLLY_HAVE_MALLOC_H
98 #include <malloc.h>
99 #else
100 #include <stdlib.h>
101 #endif
102
103 #include <cassert>
104 #include <cstddef>
105 #include <cstdint>
106 #include <cstdlib>
107 #include <cstring>
108
109 #include <new>
110
111 #ifdef _LIBSTDCXX_FBSTRING
112 namespace std _GLIBCXX_VISIBILITY(default) {
113 _GLIBCXX_BEGIN_NAMESPACE_VERSION
114 #else
115 namespace folly {
116 #endif
117
118 // Cannot depend on Portability.h when _LIBSTDCXX_FBSTRING.
119 // Disabled for nvcc because it fails on attributes on lambdas.
120 #if defined(__GNUC__) && !defined(__NVCC__)
121 #define FOLLY_MALLOC_NOINLINE __attribute__((__noinline__))
122 #else
123 #define FOLLY_MALLOC_NOINLINE
124 #endif
125
126 /**
127  * Determine if we are using jemalloc or not.
128  */
129 inline bool usingJEMalloc() noexcept {
130   // Checking for rallocx != NULL is not sufficient; we may be in a dlopen()ed
131   // module that depends on libjemalloc, so rallocx is resolved, but the main
132   // program might be using a different memory allocator.
133   // How do we determine that we're using jemalloc? In the hackiest
134   // way possible. We allocate memory using malloc() and see if the
135   // per-thread counter of allocated memory increases. This makes me
136   // feel dirty inside. Also note that this requires jemalloc to have
137   // been compiled with --enable-stats.
138   static const bool result = [] () FOLLY_MALLOC_NOINLINE noexcept {
139     // Some platforms (*cough* OSX *cough*) require weak symbol checks to be
140     // in the form if (mallctl != nullptr). Not if (mallctl) or if (!mallctl)
141     // (!!). http://goo.gl/xpmctm
142     if (mallocx == nullptr || rallocx == nullptr || xallocx == nullptr
143         || sallocx == nullptr || dallocx == nullptr || sdallocx == nullptr
144         || nallocx == nullptr || mallctl == nullptr
145         || mallctlnametomib == nullptr || mallctlbymib == nullptr) {
146       return false;
147     }
148
149     // "volatile" because gcc optimizes out the reads from *counter, because
150     // it "knows" malloc doesn't modify global state...
151     /* nolint */ volatile uint64_t* counter;
152     size_t counterLen = sizeof(uint64_t*);
153
154     if (mallctl("thread.allocatedp", static_cast<void*>(&counter), &counterLen,
155                 nullptr, 0) != 0) {
156       return false;
157     }
158
159     if (counterLen != sizeof(uint64_t*)) {
160       return false;
161     }
162
163     uint64_t origAllocated = *counter;
164
165     // Static because otherwise clever compilers will find out that
166     // the ptr is not used and does not escape the scope, so they will
167     // just optimize away the malloc.
168     static void* ptr = malloc(1);
169     if (!ptr) {
170       // wtf, failing to allocate 1 byte
171       return false;
172     }
173
174     return (origAllocated != *counter);
175   }();
176
177   return result;
178 }
179
180 inline size_t goodMallocSize(size_t minSize) noexcept {
181   if (minSize == 0) {
182     return 0;
183   }
184
185   if (!usingJEMalloc()) {
186     // Not using jemalloc - no smarts
187     return minSize;
188   }
189
190   return nallocx(minSize, 0);
191 }
192
193 // We always request "good" sizes for allocation, so jemalloc can
194 // never grow in place small blocks; they're already occupied to the
195 // brim.  Blocks larger than or equal to 4096 bytes can in fact be
196 // expanded in place, and this constant reflects that.
197 static const size_t jemallocMinInPlaceExpandable = 4096;
198
199 /**
200  * Trivial wrappers around malloc, calloc, realloc that check for allocation
201  * failure and throw std::bad_alloc in that case.
202  */
203 inline void* checkedMalloc(size_t size) {
204   void* p = malloc(size);
205   if (!p) std::__throw_bad_alloc();
206   return p;
207 }
208
209 inline void* checkedCalloc(size_t n, size_t size) {
210   void* p = calloc(n, size);
211   if (!p) std::__throw_bad_alloc();
212   return p;
213 }
214
215 inline void* checkedRealloc(void* ptr, size_t size) {
216   void* p = realloc(ptr, size);
217   if (!p) std::__throw_bad_alloc();
218   return p;
219 }
220
221 /**
222  * This function tries to reallocate a buffer of which only the first
223  * currentSize bytes are used. The problem with using realloc is that
224  * if currentSize is relatively small _and_ if realloc decides it
225  * needs to move the memory chunk to a new buffer, then realloc ends
226  * up copying data that is not used. It's impossible to hook into
227  * GNU's malloc to figure whether expansion will occur in-place or as
228  * a malloc-copy-free troika. (If an expand_in_place primitive would
229  * be available, smartRealloc would use it.) As things stand, this
230  * routine just tries to call realloc() (thus benefitting of potential
231  * copy-free coalescing) unless there's too much slack memory.
232  */
233 inline void* smartRealloc(void* p,
234                           const size_t currentSize,
235                           const size_t currentCapacity,
236                           const size_t newCapacity) {
237   assert(p);
238   assert(currentSize <= currentCapacity &&
239          currentCapacity < newCapacity);
240
241   if (usingJEMalloc()) {
242     // using jemalloc's API. Don't forget that jemalloc can never grow
243     // in place blocks smaller than 4096 bytes.
244     //
245     // NB: newCapacity may not be precisely equal to a jemalloc size class,
246     // i.e. newCapacity is not guaranteed to be the result of a
247     // goodMallocSize() call, therefore xallocx() may return more than
248     // newCapacity bytes of space.  Use >= rather than == to check whether
249     // xallocx() successfully expanded in place.
250     if (currentCapacity >= jemallocMinInPlaceExpandable &&
251         xallocx(p, newCapacity, 0, 0) >= newCapacity) {
252       // Managed to expand in place
253       return p;
254     }
255     // Cannot expand; must move
256     auto const result = checkedMalloc(newCapacity);
257     std::memcpy(result, p, currentSize);
258     free(p);
259     return result;
260   }
261
262   // No jemalloc no honey
263   auto const slack = currentCapacity - currentSize;
264   if (slack * 2 > currentSize) {
265     // Too much slack, malloc-copy-free cycle:
266     auto const result = checkedMalloc(newCapacity);
267     std::memcpy(result, p, currentSize);
268     free(p);
269     return result;
270   }
271   // If there's not too much slack, we realloc in hope of coalescing
272   return checkedRealloc(p, newCapacity);
273 }
274
275 #ifdef _LIBSTDCXX_FBSTRING
276 _GLIBCXX_END_NAMESPACE_VERSION
277 #endif
278
279 } // folly
280
281 #endif // !defined(_GLIBCXX_USE_FB) || defined(_LIBSTDCXX_FBSTRING)