Pull from FB rev 63ce89e2f2301e6bba44a111cc7d4218022156f6
[folly.git] / folly / Malloc.h
1 /*
2  * Copyright 2012 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 // If using fbstring from libstdc++, then just define stub code
24 // here to typedef the fbstring type into the folly namespace.
25 // This provides backwards compatibility for code that explicitly
26 // includes and uses fbstring.
27 #if defined(_GLIBCXX_USE_FB) && !defined(_LIBSTDCXX_FBSTRING)
28
29 #include <string>
30 namespace folly {
31   using std::goodMallocSize;
32   using std::jemallocMinInPlaceExpandable;
33   using std::usingJEMalloc;
34   using std::smartRealloc;
35 }
36
37 #else // !defined(_GLIBCXX_USE_FB) || defined(_LIBSTDCXX_FBSTRING)
38
39 #ifdef _LIBSTDCXX_FBSTRING
40 #pragma GCC system_header
41 #define FOLLY_HAVE_MALLOC_H 1
42 #else
43 #include "folly-config.h"
44 #endif
45
46 // for malloc_usable_size
47 // NOTE: FreeBSD 9 doesn't have malloc.h.  It's defitions
48 // are found in stdlib.h.
49 #ifdef FOLLY_HAVE_MALLOC_H
50 #include <malloc.h>
51 #else
52 #include <stdlib.h>
53 #endif
54
55 #include <cassert>
56 #include <cstddef>
57 #include <cstdlib>
58 #include <cstring>
59
60 /**
61  * Declare rallocm() and malloc_usable_size() as weak symbols.  It
62  * will be provided by jemalloc if we are using jemalloc, or it will
63  * be NULL if we are using another malloc implementation.
64  */
65 extern "C" int rallocm(void**, size_t*, size_t, size_t, int)
66 __attribute__((weak));
67
68 /**
69  * Define the ALLOCM_SUCCESS, ALLOCM_ZERO, and ALLOCM_NO_MOVE constants
70  * normally provided by jemalloc.  We define them so that we don't have to
71  * include jemalloc.h, in case the program is built without jemalloc support.
72  */
73 #ifndef ALLOCM_SUCCESS
74 #define ALLOCM_SUCCESS 0
75 #define ALLOCM_ERR_OOM 1
76 #define ALLOCM_ERR_NOT_MOVED 2
77
78 #define ALLOCM_ZERO    64
79 #define ALLOCM_NO_MOVE 128
80 #endif
81
82 #ifdef _LIBSTDCXX_FBSTRING
83 namespace std _GLIBCXX_VISIBILITY(default) {
84 _GLIBCXX_BEGIN_NAMESPACE_VERSION
85 #else
86 namespace folly {
87 #endif
88
89
90 /**
91  * Determine if we are using jemalloc or not.
92  */
93 inline bool usingJEMalloc() {
94   return rallocm != NULL;
95 }
96
97 /**
98  * For jemalloc's size classes, see
99  * http://www.canonware.com/download/jemalloc/jemalloc-latest/doc/jemalloc.html
100  */
101 inline size_t goodMallocSize(size_t minSize) {
102   if (!usingJEMalloc()) {
103     // Not using jemalloc - no smarts
104     return minSize;
105   }
106   if (minSize <= 64) {
107     // Choose smallest allocation to be 64 bytes - no tripping over
108     // cache line boundaries, and small string optimization takes care
109     // of short strings anyway.
110     return 64;
111   }
112   if (minSize <= 512) {
113     // Round up to the next multiple of 64; we don't want to trip over
114     // cache line boundaries.
115     return (minSize + 63) & ~size_t(63);
116   }
117   if (minSize <= 3840) {
118     // Round up to the next multiple of 256
119     return (minSize + 255) & ~size_t(255);
120   }
121   if (minSize <= 4072 * 1024) {
122     // Round up to the next multiple of 4KB
123     return (minSize + 4095) & ~size_t(4095);
124   }
125   // Holy Moly
126   // Round up to the next multiple of 4MB
127   return (minSize + 4194303) & ~size_t(4194303);
128 }
129
130 // We always request "good" sizes for allocation, so jemalloc can
131 // never grow in place small blocks; they're already occupied to the
132 // brim.  Blocks larger than or equal to 4096 bytes can in fact be
133 // expanded in place, and this constant reflects that.
134 static const size_t jemallocMinInPlaceExpandable = 4096;
135
136 /**
137  * This function tries to reallocate a buffer of which only the first
138  * currentSize bytes are used. The problem with using realloc is that
139  * if currentSize is relatively small _and_ if realloc decides it
140  * needs to move the memory chunk to a new buffer, then realloc ends
141  * up copying data that is not used. It's impossible to hook into
142  * GNU's malloc to figure whether expansion will occur in-place or as
143  * a malloc-copy-free troika. (If an expand_in_place primitive would
144  * be available, smartRealloc would use it.) As things stand, this
145  * routine just tries to call realloc() (thus benefitting of potential
146  * copy-free coalescing) unless there's too much slack memory.
147  */
148 inline void* smartRealloc(void* p,
149                           const size_t currentSize,
150                           const size_t currentCapacity,
151                           const size_t newCapacity) {
152   assert(p);
153   assert(currentSize <= currentCapacity &&
154          currentCapacity < newCapacity);
155
156   if (usingJEMalloc()) {
157     // using jemalloc's API. Don't forget that jemalloc can never grow
158     // in place blocks smaller than 4096 bytes.
159     if (currentCapacity >= jemallocMinInPlaceExpandable &&
160         rallocm(&p, NULL, newCapacity, 0, ALLOCM_NO_MOVE) == ALLOCM_SUCCESS) {
161       // Managed to expand in place
162       return p;
163     }
164     // Cannot expand; must move
165     auto const result = malloc(newCapacity);
166     std::memcpy(result, p, currentSize);
167     free(p);
168     return result;
169   }
170
171   // No jemalloc no honey
172   auto const slack = currentCapacity - currentSize;
173   if (slack * 2 > currentSize) {
174     // Too much slack, malloc-copy-free cycle:
175     auto const result = malloc(newCapacity);
176     std::memcpy(result, p, currentSize);
177     free(p);
178     return result;
179   }
180   // If there's not too much slack, we realloc in hope of coalescing
181   return realloc(p, newCapacity);
182 }
183
184 #ifdef _LIBSTDCXX_FBSTRING
185 _GLIBCXX_END_NAMESPACE_VERSION
186 #endif
187
188 } // folly
189
190 #endif // !defined(_GLIBCXX_USE_FB) || defined(_LIBSTDCXX_FBSTRING)
191
192 #endif // FOLLY_MALLOC_H_