move shutdown socket set
[folly.git] / folly / Malloc.h
1 /*
2  * Copyright 2014 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 #ifndef FOLLY_MALLOC_H_
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++, then just define stub code
36 // here to typedef the fbstring type into the folly namespace.
37 // This provides backwards compatibility for code that explicitly
38 // includes and uses fbstring.
39 #if defined(_GLIBCXX_USE_FB) && !defined(_LIBSTDCXX_FBSTRING)
40
41 #include <folly/detail/Malloc.h>
42
43 #include <string>
44
45 namespace folly {
46   using std::goodMallocSize;
47   using std::jemallocMinInPlaceExpandable;
48   using std::usingJEMalloc;
49   using std::smartRealloc;
50   using std::checkedMalloc;
51   using std::checkedCalloc;
52   using std::checkedRealloc;
53 }
54
55 #else // !defined(_GLIBCXX_USE_FB) || defined(_LIBSTDCXX_FBSTRING)
56
57 #ifdef _LIBSTDCXX_FBSTRING
58 #pragma GCC system_header
59
60 /**
61  * Declare *allocx() and mallctl() as weak symbols. These will be provided by
62  * jemalloc if we are using jemalloc, or will be NULL if we are using another
63  * malloc implementation.
64  */
65 extern "C" void* mallocx(size_t, int)
66 __attribute__((__weak__));
67 extern "C" void* rallocx(void*, size_t, int)
68 __attribute__((__weak__));
69 extern "C" size_t xallocx(void*, size_t, size_t, int)
70 __attribute__((__weak__));
71 extern "C" size_t sallocx(const void*, int)
72 __attribute__((__weak__));
73 extern "C" void dallocx(void*, int)
74 __attribute__((__weak__));
75 extern "C" size_t nallocx(size_t, int)
76 __attribute__((__weak__));
77 extern "C" int mallctl(const char*, void*, size_t*, void*, size_t)
78 __attribute__((__weak__));
79
80 #include <bits/functexcept.h>
81 #define FOLLY_HAVE_MALLOC_H 1
82 #else
83 #include <folly/detail/Malloc.h> /* nolint */
84 #include <folly/Portability.h>
85 #endif
86
87 // for malloc_usable_size
88 // NOTE: FreeBSD 9 doesn't have malloc.h.  It's defitions
89 // are found in stdlib.h.
90 #if FOLLY_HAVE_MALLOC_H
91 #include <malloc.h>
92 #else
93 #include <stdlib.h>
94 #endif
95
96 #include <cassert>
97 #include <cstddef>
98 #include <cstdlib>
99 #include <cstring>
100
101 #include <new>
102
103 #ifdef _LIBSTDCXX_FBSTRING
104 namespace std _GLIBCXX_VISIBILITY(default) {
105 _GLIBCXX_BEGIN_NAMESPACE_VERSION
106 #else
107 namespace folly {
108 #endif
109
110 bool usingJEMallocSlow();
111
112 /**
113  * Determine if we are using jemalloc or not.
114  */
115 inline bool usingJEMalloc() {
116   // Checking for rallocx != NULL is not sufficient; we may be in a dlopen()ed
117   // module that depends on libjemalloc, so rallocx is resolved, but the main
118   // program might be using a different memory allocator. Look at the
119   // implementation of usingJEMallocSlow() for the (hacky) details.
120   static const bool result = usingJEMallocSlow();
121   return result;
122 }
123
124 /**
125  * For jemalloc's size classes, see
126  * http://www.canonware.com/download/jemalloc/jemalloc-latest/doc/jemalloc.html
127  */
128 inline size_t goodMallocSize(size_t minSize) noexcept {
129   if (!usingJEMalloc()) {
130     // Not using jemalloc - no smarts
131     return minSize;
132   }
133   size_t goodSize;
134   if (minSize <= 64) {
135     // Choose smallest allocation to be 64 bytes - no tripping over
136     // cache line boundaries, and small string optimization takes care
137     // of short strings anyway.
138     goodSize = 64;
139   } else if (minSize <= 512) {
140     // Round up to the next multiple of 64; we don't want to trip over
141     // cache line boundaries.
142     goodSize = (minSize + 63) & ~size_t(63);
143   } else if (minSize <= 3584) {
144     // Round up to the next multiple of 256.  For some size classes jemalloc
145     // will additionally round up to the nearest multiple of 512, hence the
146     // nallocx() call.
147     goodSize = nallocx((minSize + 255) & ~size_t(255), 0);
148   } else if (minSize <= 4072 * 1024) {
149     // Round up to the next multiple of 4KB
150     goodSize = (minSize + 4095) & ~size_t(4095);
151   } else {
152     // Holy Moly
153     // Round up to the next multiple of 4MB
154     goodSize = (minSize + 4194303) & ~size_t(4194303);
155   }
156   assert(nallocx(goodSize, 0) == goodSize);
157   return goodSize;
158 }
159
160 // We always request "good" sizes for allocation, so jemalloc can
161 // never grow in place small blocks; they're already occupied to the
162 // brim.  Blocks larger than or equal to 4096 bytes can in fact be
163 // expanded in place, and this constant reflects that.
164 static const size_t jemallocMinInPlaceExpandable = 4096;
165
166 /**
167  * Trivial wrappers around malloc, calloc, realloc that check for allocation
168  * failure and throw std::bad_alloc in that case.
169  */
170 inline void* checkedMalloc(size_t size) {
171   void* p = malloc(size);
172   if (!p) std::__throw_bad_alloc();
173   return p;
174 }
175
176 inline void* checkedCalloc(size_t n, size_t size) {
177   void* p = calloc(n, size);
178   if (!p) std::__throw_bad_alloc();
179   return p;
180 }
181
182 inline void* checkedRealloc(void* ptr, size_t size) {
183   void* p = realloc(ptr, size);
184   if (!p) std::__throw_bad_alloc();
185   return p;
186 }
187
188 /**
189  * This function tries to reallocate a buffer of which only the first
190  * currentSize bytes are used. The problem with using realloc is that
191  * if currentSize is relatively small _and_ if realloc decides it
192  * needs to move the memory chunk to a new buffer, then realloc ends
193  * up copying data that is not used. It's impossible to hook into
194  * GNU's malloc to figure whether expansion will occur in-place or as
195  * a malloc-copy-free troika. (If an expand_in_place primitive would
196  * be available, smartRealloc would use it.) As things stand, this
197  * routine just tries to call realloc() (thus benefitting of potential
198  * copy-free coalescing) unless there's too much slack memory.
199  */
200 inline void* smartRealloc(void* p,
201                           const size_t currentSize,
202                           const size_t currentCapacity,
203                           const size_t newCapacity) {
204   assert(p);
205   assert(currentSize <= currentCapacity &&
206          currentCapacity < newCapacity);
207
208   if (usingJEMalloc()) {
209     // using jemalloc's API. Don't forget that jemalloc can never grow
210     // in place blocks smaller than 4096 bytes.
211     //
212     // NB: newCapacity may not be precisely equal to a jemalloc size class,
213     // i.e. newCapacity is not guaranteed to be the result of a
214     // goodMallocSize() call, therefore xallocx() may return more than
215     // newCapacity bytes of space.  Use >= rather than == to check whether
216     // xallocx() successfully expanded in place.
217     if (currentCapacity >= jemallocMinInPlaceExpandable &&
218         xallocx(p, newCapacity, 0, 0) >= newCapacity) {
219       // Managed to expand in place
220       return p;
221     }
222     // Cannot expand; must move
223     auto const result = checkedMalloc(newCapacity);
224     std::memcpy(result, p, currentSize);
225     free(p);
226     return result;
227   }
228
229   // No jemalloc no honey
230   auto const slack = currentCapacity - currentSize;
231   if (slack * 2 > currentSize) {
232     // Too much slack, malloc-copy-free cycle:
233     auto const result = checkedMalloc(newCapacity);
234     std::memcpy(result, p, currentSize);
235     free(p);
236     return result;
237   }
238   // If there's not too much slack, we realloc in hope of coalescing
239   return checkedRealloc(p, newCapacity);
240 }
241
242 #ifdef _LIBSTDCXX_FBSTRING
243 _GLIBCXX_END_NAMESPACE_VERSION
244 #endif
245
246 } // folly
247
248 #endif // !defined(_GLIBCXX_USE_FB) || defined(_LIBSTDCXX_FBSTRING)
249
250 #endif // FOLLY_MALLOC_H_