1 // Copyright (c) 2005, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 // Author: Sanjay Ghemawat
34 #include <errno.h> // for EAGAIN, errno
35 #include <fcntl.h> // for open, O_RDWR
36 #include <stddef.h> // for size_t, NULL, ptrdiff_t
37 #if defined HAVE_STDINT_H
38 #include <stdint.h> // for uintptr_t, intptr_t
39 #elif defined HAVE_INTTYPES_H
42 #include <sys/types.h>
45 #include <sys/mman.h> // for munmap, mmap, MADV_DONTNEED, etc
48 #include <unistd.h> // for sbrk, getpagesize, off_t
50 #include <new> // for operator new
51 #include <gperftools/malloc_extension.h>
52 #include "base/basictypes.h"
53 #include "base/commandlineflags.h"
54 #include "base/spinlock.h" // for SpinLockHolder, SpinLock, etc
56 #include "internal_logging.h"
58 // On systems (like freebsd) that don't define MAP_ANONYMOUS, use the old
59 // form of the name instead.
61 # define MAP_ANONYMOUS MAP_ANON
64 // MADV_FREE is specifically designed for use by malloc(), but only
65 // FreeBSD supports it; in linux we fall back to the somewhat inferior
67 #if !defined(MADV_FREE) && defined(MADV_DONTNEED)
68 # define MADV_FREE MADV_DONTNEED
71 // Solaris has a bug where it doesn't declare madvise() for C++.
72 // http://www.opensolaris.org/jive/thread.jspa?threadID=21035&tstart=0
73 #if defined(__sun) && defined(__SVR4)
74 # include <sys/types.h> // for caddr_t
75 extern "C" { extern int madvise(caddr_t
, size_t, int); }
78 // Set kDebugMode mode so that we can have use C++ conditionals
79 // instead of preprocessor conditionals.
81 static const bool kDebugMode
= false;
83 static const bool kDebugMode
= true;
86 // TODO(sanjay): Move the code below into the tcmalloc namespace
90 // Anonymous namespace to avoid name conflicts on "CheckAddressBits".
93 // Check that no bit is set at position ADDRESS_BITS or higher.
94 template <int ADDRESS_BITS
> bool CheckAddressBits(uintptr_t ptr
) {
95 return (ptr
>> ADDRESS_BITS
) == 0;
98 // Specialize for the bit width of a pointer to avoid undefined shift.
99 template <> bool CheckAddressBits
<8 * sizeof(void*)>(uintptr_t ptr
) {
103 } // Anonymous namespace to avoid name conflicts on "CheckAddressBits".
105 COMPILE_ASSERT(kAddressBits
<= 8 * sizeof(void*),
106 address_bits_larger_than_pointer_size
);
108 // Structure for discovering alignment
109 union MemoryAligner
{
115 static SpinLock
spinlock(SpinLock::LINKER_INITIALIZED
);
117 #if defined(HAVE_MMAP) || defined(MADV_FREE)
118 // Page size is initialized on demand (only needed for mmap-based allocators)
119 static size_t pagesize
= 0;
122 // The current system allocator
123 SysAllocator
* sys_alloc
= NULL
;
125 // Configuration parameters.
126 DEFINE_int32(malloc_devmem_start
,
127 EnvToInt("TCMALLOC_DEVMEM_START", 0),
128 "Physical memory starting location in MB for /dev/mem allocation."
129 " Setting this to 0 disables /dev/mem allocation");
130 DEFINE_int32(malloc_devmem_limit
,
131 EnvToInt("TCMALLOC_DEVMEM_LIMIT", 0),
132 "Physical memory limit location in MB for /dev/mem allocation."
133 " Setting this to 0 means no limit.");
134 DEFINE_bool(malloc_skip_sbrk
,
135 EnvToBool("TCMALLOC_SKIP_SBRK", false),
136 "Whether sbrk can be used to obtain memory.");
137 DEFINE_bool(malloc_skip_mmap
,
138 EnvToBool("TCMALLOC_SKIP_MMAP", false),
139 "Whether mmap can be used to obtain memory.");
142 class SbrkSysAllocator
: public SysAllocator
{
144 SbrkSysAllocator() : SysAllocator() {
146 void* Alloc(size_t size
, size_t *actual_size
, size_t alignment
);
148 static char sbrk_space
[sizeof(SbrkSysAllocator
)];
150 class MmapSysAllocator
: public SysAllocator
{
152 MmapSysAllocator() : SysAllocator() {
154 void* Alloc(size_t size
, size_t *actual_size
, size_t alignment
);
156 static char mmap_space
[sizeof(MmapSysAllocator
)];
158 class DevMemSysAllocator
: public SysAllocator
{
160 DevMemSysAllocator() : SysAllocator() {
162 void* Alloc(size_t size
, size_t *actual_size
, size_t alignment
);
165 class DefaultSysAllocator
: public SysAllocator
{
167 DefaultSysAllocator() : SysAllocator() {
168 for (int i
= 0; i
< kMaxAllocators
; i
++) {
174 void SetChildAllocator(SysAllocator
* alloc
, unsigned int index
,
176 if (index
< kMaxAllocators
&& alloc
!= NULL
) {
177 allocs_
[index
] = alloc
;
178 failed_
[index
] = false;
179 names_
[index
] = name
;
182 void* Alloc(size_t size
, size_t *actual_size
, size_t alignment
);
185 static const int kMaxAllocators
= 2;
186 bool failed_
[kMaxAllocators
];
187 SysAllocator
* allocs_
[kMaxAllocators
];
188 const char* names_
[kMaxAllocators
];
190 static char default_space
[sizeof(DefaultSysAllocator
)];
191 static const char sbrk_name
[] = "SbrkSysAllocator";
192 static const char mmap_name
[] = "MmapSysAllocator";
195 void* SbrkSysAllocator::Alloc(size_t size
, size_t *actual_size
,
201 // Check if we should use sbrk allocation.
202 // FLAGS_malloc_skip_sbrk starts out as false (its uninitialized
203 // state) and eventually gets initialized to the specified value. Note
204 // that this code runs for a while before the flags are initialized.
205 // That means that even if this flag is set to true, some (initial)
206 // memory will be allocated with sbrk before the flag takes effect.
207 if (FLAGS_malloc_skip_sbrk
) {
211 // sbrk will release memory if passed a negative number, so we do
212 // a strict check here
213 if (static_cast<ptrdiff_t>(size
+ alignment
) < 0) return NULL
;
215 // This doesn't overflow because TCMalloc_SystemAlloc has already
216 // tested for overflow at the alignment boundary.
217 size
= ((size
+ alignment
- 1) / alignment
) * alignment
;
219 // "actual_size" indicates that the bytes from the returned pointer
220 // p up to and including (p + actual_size - 1) have been allocated.
225 // Check that we we're not asking for so much more memory that we'd
226 // wrap around the end of the virtual address space. (This seems
227 // like something sbrk() should check for us, and indeed opensolaris
228 // does, but glibc does not:
229 // http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/lib/libc/port/sys/sbrk.c?a=true
230 // http://sourceware.org/cgi-bin/cvsweb.cgi/~checkout~/libc/misc/sbrk.c?rev=1.1.2.1&content-type=text/plain&cvsroot=glibc
231 // Without this check, sbrk may succeed when it ought to fail.)
232 if (reinterpret_cast<intptr_t>(sbrk(0)) + size
< size
) {
236 void* result
= sbrk(size
);
237 if (result
== reinterpret_cast<void*>(-1)) {
242 uintptr_t ptr
= reinterpret_cast<uintptr_t>(result
);
243 if ((ptr
& (alignment
-1)) == 0) return result
;
245 // Try to get more memory for alignment
246 size_t extra
= alignment
- (ptr
& (alignment
-1));
247 void* r2
= sbrk(extra
);
248 if (reinterpret_cast<uintptr_t>(r2
) == (ptr
+ size
)) {
249 // Contiguous with previous result
250 return reinterpret_cast<void*>(ptr
+ extra
);
253 // Give up and ask for "size + alignment - 1" bytes so
254 // that we can find an aligned region within it.
255 result
= sbrk(size
+ alignment
- 1);
256 if (result
== reinterpret_cast<void*>(-1)) {
259 ptr
= reinterpret_cast<uintptr_t>(result
);
260 if ((ptr
& (alignment
-1)) != 0) {
261 ptr
+= alignment
- (ptr
& (alignment
-1));
263 return reinterpret_cast<void*>(ptr
);
267 void* MmapSysAllocator::Alloc(size_t size
, size_t *actual_size
,
273 // Check if we should use mmap allocation.
274 // FLAGS_malloc_skip_mmap starts out as false (its uninitialized
275 // state) and eventually gets initialized to the specified value. Note
276 // that this code runs for a while before the flags are initialized.
277 // Chances are we never get here before the flags are initialized since
278 // sbrk is used until the heap is exhausted (before mmap is used).
279 if (FLAGS_malloc_skip_mmap
) {
283 // Enforce page alignment
284 if (pagesize
== 0) pagesize
= getpagesize();
285 if (alignment
< pagesize
) alignment
= pagesize
;
286 size_t aligned_size
= ((size
+ alignment
- 1) / alignment
) * alignment
;
287 if (aligned_size
< size
) {
292 // "actual_size" indicates that the bytes from the returned pointer
293 // p up to and including (p + actual_size - 1) have been allocated.
298 // Ask for extra memory if alignment > pagesize
300 if (alignment
> pagesize
) {
301 extra
= alignment
- pagesize
;
304 // Note: size + extra does not overflow since:
305 // size + alignment < (1<<NBITS).
306 // and extra <= alignment
307 // therefore size + extra < (1<<NBITS)
308 void* result
= mmap(NULL
, size
+ extra
,
309 PROT_READ
|PROT_WRITE
,
310 MAP_PRIVATE
|MAP_ANONYMOUS
,
312 if (result
== reinterpret_cast<void*>(MAP_FAILED
)) {
316 // Adjust the return memory so it is aligned
317 uintptr_t ptr
= reinterpret_cast<uintptr_t>(result
);
319 if ((ptr
& (alignment
- 1)) != 0) {
320 adjust
= alignment
- (ptr
& (alignment
- 1));
323 // Return the unused memory to the system
325 munmap(reinterpret_cast<void*>(ptr
), adjust
);
327 if (adjust
< extra
) {
328 munmap(reinterpret_cast<void*>(ptr
+ adjust
+ size
), extra
- adjust
);
332 return reinterpret_cast<void*>(ptr
);
336 void* DevMemSysAllocator::Alloc(size_t size
, size_t *actual_size
,
342 static bool initialized
= false;
343 static off_t physmem_base
; // next physical memory address to allocate
344 static off_t physmem_limit
; // maximum physical address allowed
345 static int physmem_fd
; // file descriptor for /dev/mem
347 // Check if we should use /dev/mem allocation. Note that it may take
348 // a while to get this flag initialized, so meanwhile we fall back to
349 // the next allocator. (It looks like 7MB gets allocated before
350 // this flag gets initialized -khr.)
351 if (FLAGS_malloc_devmem_start
== 0) {
352 // NOTE: not a devmem_failure - we'd like TCMalloc_SystemAlloc to
353 // try us again next time.
358 physmem_fd
= open("/dev/mem", O_RDWR
);
359 if (physmem_fd
< 0) {
362 physmem_base
= FLAGS_malloc_devmem_start
*1024LL*1024LL;
363 physmem_limit
= FLAGS_malloc_devmem_limit
*1024LL*1024LL;
367 // Enforce page alignment
368 if (pagesize
== 0) pagesize
= getpagesize();
369 if (alignment
< pagesize
) alignment
= pagesize
;
370 size_t aligned_size
= ((size
+ alignment
- 1) / alignment
) * alignment
;
371 if (aligned_size
< size
) {
376 // "actual_size" indicates that the bytes from the returned pointer
377 // p up to and including (p + actual_size - 1) have been allocated.
382 // Ask for extra memory if alignment > pagesize
384 if (alignment
> pagesize
) {
385 extra
= alignment
- pagesize
;
388 // check to see if we have any memory left
389 if (physmem_limit
!= 0 &&
390 ((size
+ extra
) > (physmem_limit
- physmem_base
))) {
394 // Note: size + extra does not overflow since:
395 // size + alignment < (1<<NBITS).
396 // and extra <= alignment
397 // therefore size + extra < (1<<NBITS)
398 void *result
= mmap(0, size
+ extra
, PROT_WRITE
|PROT_READ
,
399 MAP_SHARED
, physmem_fd
, physmem_base
);
400 if (result
== reinterpret_cast<void*>(MAP_FAILED
)) {
403 uintptr_t ptr
= reinterpret_cast<uintptr_t>(result
);
405 // Adjust the return memory so it is aligned
407 if ((ptr
& (alignment
- 1)) != 0) {
408 adjust
= alignment
- (ptr
& (alignment
- 1));
411 // Return the unused virtual memory to the system
413 munmap(reinterpret_cast<void*>(ptr
), adjust
);
415 if (adjust
< extra
) {
416 munmap(reinterpret_cast<void*>(ptr
+ adjust
+ size
), extra
- adjust
);
420 physmem_base
+= adjust
+ size
;
422 return reinterpret_cast<void*>(ptr
);
426 void* DefaultSysAllocator::Alloc(size_t size
, size_t *actual_size
,
428 for (int i
= 0; i
< kMaxAllocators
; i
++) {
429 if (!failed_
[i
] && allocs_
[i
] != NULL
) {
430 void* result
= allocs_
[i
]->Alloc(size
, actual_size
, alignment
);
431 if (result
!= NULL
) {
437 // After both failed, reset "failed_" to false so that a single failed
438 // allocation won't make the allocator never work again.
439 for (int i
= 0; i
< kMaxAllocators
; i
++) {
445 static bool system_alloc_inited
= false;
446 void InitSystemAllocators(void) {
447 MmapSysAllocator
*mmap
= new (mmap_space
) MmapSysAllocator();
448 SbrkSysAllocator
*sbrk
= new (sbrk_space
) SbrkSysAllocator();
450 // In 64-bit debug mode, place the mmap allocator first since it
451 // allocates pointers that do not fit in 32 bits and therefore gives
452 // us better testing of code's 64-bit correctness. It also leads to
453 // less false negatives in heap-checking code. (Numbers are less
454 // likely to look like pointers and therefore the conservative gc in
455 // the heap-checker is less likely to misinterpret a number as a
457 DefaultSysAllocator
*sdef
= new (default_space
) DefaultSysAllocator();
458 if (kDebugMode
&& sizeof(void*) > 4) {
459 sdef
->SetChildAllocator(mmap
, 0, mmap_name
);
460 sdef
->SetChildAllocator(sbrk
, 1, sbrk_name
);
462 sdef
->SetChildAllocator(sbrk
, 0, sbrk_name
);
463 sdef
->SetChildAllocator(mmap
, 1, mmap_name
);
468 void* TCMalloc_SystemAlloc(size_t size
, size_t *actual_size
,
470 // Discard requests that overflow
471 if (size
+ alignment
< size
) return NULL
;
473 SpinLockHolder
lock_holder(&spinlock
);
475 if (!system_alloc_inited
) {
476 InitSystemAllocators();
477 system_alloc_inited
= true;
480 // Enforce minimum alignment
481 if (alignment
< sizeof(MemoryAligner
)) alignment
= sizeof(MemoryAligner
);
483 void* result
= sys_alloc
->Alloc(size
, actual_size
, alignment
);
484 if (result
!= NULL
) {
486 CheckAddressBits
<kAddressBits
>(
487 reinterpret_cast<uintptr_t>(result
) + *actual_size
- 1);
489 CheckAddressBits
<kAddressBits
>(
490 reinterpret_cast<uintptr_t>(result
) + size
- 1);
496 void TCMalloc_SystemRelease(void* start
, size_t length
) {
498 if (FLAGS_malloc_devmem_start
) {
499 // It's not safe to use MADV_FREE/MADV_DONTNEED if we've been
500 // mapping /dev/mem for heap memory.
503 if (pagesize
== 0) pagesize
= getpagesize();
504 const size_t pagemask
= pagesize
- 1;
506 size_t new_start
= reinterpret_cast<size_t>(start
);
507 size_t end
= new_start
+ length
;
508 size_t new_end
= end
;
510 // Round up the starting address and round down the ending address
511 // to be page aligned:
512 new_start
= (new_start
+ pagesize
- 1) & ~pagemask
;
513 new_end
= new_end
& ~pagemask
;
515 ASSERT((new_start
& pagemask
) == 0);
516 ASSERT((new_end
& pagemask
) == 0);
517 ASSERT(new_start
>= reinterpret_cast<size_t>(start
));
518 ASSERT(new_end
<= end
);
520 if (new_end
> new_start
) {
521 // Note -- ignoring most return codes, because if this fails it
523 while (madvise(reinterpret_cast<char*>(new_start
), new_end
- new_start
,