2 * Copyright 2012 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
20 #ifndef FOLLY_MALLOC_H_
21 #define FOLLY_MALLOC_H_
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)
31 using std::goodMallocSize;
32 using std::jemallocMinInPlaceExpandable;
33 using std::usingJEMalloc;
34 using std::smartRealloc;
35 using std::checkedMalloc;
36 using std::checkedCalloc;
37 using std::checkedRealloc;
40 #else // !defined(_GLIBCXX_USE_FB) || defined(_LIBSTDCXX_FBSTRING)
42 #ifdef _LIBSTDCXX_FBSTRING
43 #pragma GCC system_header
44 #define FOLLY_HAVE_MALLOC_H 1
46 #include "folly-config.h"
49 // for malloc_usable_size
50 // NOTE: FreeBSD 9 doesn't have malloc.h. It's defitions
51 // are found in stdlib.h.
52 #ifdef FOLLY_HAVE_MALLOC_H
66 * Declare rallocm() and malloc_usable_size() as weak symbols. It
67 * will be provided by jemalloc if we are using jemalloc, or it will
68 * be NULL if we are using another malloc implementation.
70 extern "C" int rallocm(void**, size_t*, size_t, size_t, int)
71 __attribute__((weak));
74 * Define the ALLOCM_SUCCESS, ALLOCM_ZERO, and ALLOCM_NO_MOVE constants
75 * normally provided by jemalloc. We define them so that we don't have to
76 * include jemalloc.h, in case the program is built without jemalloc support.
78 #ifndef ALLOCM_SUCCESS
79 #define ALLOCM_SUCCESS 0
80 #define ALLOCM_ERR_OOM 1
81 #define ALLOCM_ERR_NOT_MOVED 2
83 #define ALLOCM_ZERO 64
84 #define ALLOCM_NO_MOVE 128
87 #ifdef _LIBSTDCXX_FBSTRING
88 namespace std _GLIBCXX_VISIBILITY(default) {
89 _GLIBCXX_BEGIN_NAMESPACE_VERSION
96 * Determine if we are using jemalloc or not.
98 inline bool usingJEMalloc() {
99 return rallocm != NULL;
103 * For jemalloc's size classes, see
104 * http://www.canonware.com/download/jemalloc/jemalloc-latest/doc/jemalloc.html
106 inline size_t goodMallocSize(size_t minSize) {
107 if (!usingJEMalloc()) {
108 // Not using jemalloc - no smarts
112 // Choose smallest allocation to be 64 bytes - no tripping over
113 // cache line boundaries, and small string optimization takes care
114 // of short strings anyway.
117 if (minSize <= 512) {
118 // Round up to the next multiple of 64; we don't want to trip over
119 // cache line boundaries.
120 return (minSize + 63) & ~size_t(63);
122 if (minSize <= 3840) {
123 // Round up to the next multiple of 256
124 return (minSize + 255) & ~size_t(255);
126 if (minSize <= 4072 * 1024) {
127 // Round up to the next multiple of 4KB
128 return (minSize + 4095) & ~size_t(4095);
131 // Round up to the next multiple of 4MB
132 return (minSize + 4194303) & ~size_t(4194303);
135 // We always request "good" sizes for allocation, so jemalloc can
136 // never grow in place small blocks; they're already occupied to the
137 // brim. Blocks larger than or equal to 4096 bytes can in fact be
138 // expanded in place, and this constant reflects that.
139 static const size_t jemallocMinInPlaceExpandable = 4096;
142 * Trivial wrappers around malloc, calloc, realloc that check for allocation
143 * failure and throw std::bad_alloc in that case.
145 inline void* checkedMalloc(size_t size) {
146 void* p = malloc(size);
147 if (!p) throw std::bad_alloc();
151 inline void* checkedCalloc(size_t n, size_t size) {
152 void* p = calloc(n, size);
153 if (!p) throw std::bad_alloc();
157 inline void* checkedRealloc(void* ptr, size_t size) {
158 void* p = realloc(ptr, size);
159 if (!p) throw std::bad_alloc();
164 * This function tries to reallocate a buffer of which only the first
165 * currentSize bytes are used. The problem with using realloc is that
166 * if currentSize is relatively small _and_ if realloc decides it
167 * needs to move the memory chunk to a new buffer, then realloc ends
168 * up copying data that is not used. It's impossible to hook into
169 * GNU's malloc to figure whether expansion will occur in-place or as
170 * a malloc-copy-free troika. (If an expand_in_place primitive would
171 * be available, smartRealloc would use it.) As things stand, this
172 * routine just tries to call realloc() (thus benefitting of potential
173 * copy-free coalescing) unless there's too much slack memory.
175 inline void* smartRealloc(void* p,
176 const size_t currentSize,
177 const size_t currentCapacity,
178 const size_t newCapacity) {
180 assert(currentSize <= currentCapacity &&
181 currentCapacity < newCapacity);
183 if (usingJEMalloc()) {
184 // using jemalloc's API. Don't forget that jemalloc can never grow
185 // in place blocks smaller than 4096 bytes.
186 if (currentCapacity >= jemallocMinInPlaceExpandable &&
187 rallocm(&p, NULL, newCapacity, 0, ALLOCM_NO_MOVE) == ALLOCM_SUCCESS) {
188 // Managed to expand in place
191 // Cannot expand; must move
192 auto const result = checkedMalloc(newCapacity);
193 std::memcpy(result, p, currentSize);
198 // No jemalloc no honey
199 auto const slack = currentCapacity - currentSize;
200 if (slack * 2 > currentSize) {
201 // Too much slack, malloc-copy-free cycle:
202 auto const result = checkedMalloc(newCapacity);
203 std::memcpy(result, p, currentSize);
207 // If there's not too much slack, we realloc in hope of coalescing
208 return checkedRealloc(p, newCapacity);
211 #ifdef _LIBSTDCXX_FBSTRING
212 _GLIBCXX_END_NAMESPACE_VERSION
217 #endif // !defined(_GLIBCXX_USE_FB) || defined(_LIBSTDCXX_FBSTRING)
219 #endif // FOLLY_MALLOC_H_