OLD | NEW |
1 // Copyright (c) 2005, Google Inc. | 1 // Copyright (c) 2005, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
(...skipping 30 matching lines...) Expand all Loading... |
41 #else | 41 #else |
42 #include <sys/types.h> | 42 #include <sys/types.h> |
43 #endif | 43 #endif |
44 #ifdef HAVE_MMAP | 44 #ifdef HAVE_MMAP |
45 #include <sys/mman.h> // for munmap, mmap, MADV_DONTNEED, etc | 45 #include <sys/mman.h> // for munmap, mmap, MADV_DONTNEED, etc |
46 #endif | 46 #endif |
47 #ifdef HAVE_UNISTD_H | 47 #ifdef HAVE_UNISTD_H |
48 #include <unistd.h> // for sbrk, getpagesize, off_t | 48 #include <unistd.h> // for sbrk, getpagesize, off_t |
49 #endif | 49 #endif |
50 #include <new> // for operator new | 50 #include <new> // for operator new |
51 #include <gperftools/malloc_extension.h> | 51 #include <google/malloc_extension.h> |
52 #include "base/basictypes.h" | 52 #include "base/basictypes.h" |
53 #include "base/commandlineflags.h" | 53 #include "base/commandlineflags.h" |
54 #include "base/spinlock.h" // for SpinLockHolder, SpinLock, etc | 54 #include "base/spinlock.h" // for SpinLockHolder, SpinLock, etc |
55 #include "common.h" | 55 #include "common.h" |
56 #include "internal_logging.h" | 56 #include "internal_logging.h" |
57 | 57 |
58 // On systems (like freebsd) that don't define MAP_ANONYMOUS, use the old | 58 // On systems (like freebsd) that don't define MAP_ANONYMOUS, use the old |
59 // form of the name instead. | 59 // form of the name instead. |
60 #ifndef MAP_ANONYMOUS | 60 #ifndef MAP_ANONYMOUS |
61 # define MAP_ANONYMOUS MAP_ANON | 61 # define MAP_ANONYMOUS MAP_ANON |
62 #endif | 62 #endif |
63 | 63 |
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 | |
66 // MADV_DONTNEED. | |
67 #if !defined(MADV_FREE) && defined(MADV_DONTNEED) | |
68 # define MADV_FREE MADV_DONTNEED | |
69 #endif | |
70 | |
71 // Solaris has a bug where it doesn't declare madvise() for C++. | 64 // Solaris has a bug where it doesn't declare madvise() for C++. |
72 // http://www.opensolaris.org/jive/thread.jspa?threadID=21035&tstart=0 | 65 // http://www.opensolaris.org/jive/thread.jspa?threadID=21035&tstart=0 |
73 #if defined(__sun) && defined(__SVR4) | 66 #if defined(__sun) && defined(__SVR4) |
74 # include <sys/types.h> // for caddr_t | 67 # include <sys/types.h> // for caddr_t |
75 extern "C" { extern int madvise(caddr_t, size_t, int); } | 68 extern "C" { extern int madvise(caddr_t, size_t, int); } |
76 #endif | 69 #endif |
77 | 70 |
78 // Set kDebugMode mode so that we can have use C++ conditionals | 71 // Set kDebugMode mode so that we can have use C++ conditionals |
79 // instead of preprocessor conditionals. | 72 // instead of preprocessor conditionals. |
80 #ifdef NDEBUG | 73 #ifdef NDEBUG |
81 static const bool kDebugMode = false; | 74 static const bool kDebugMode = false; |
82 #else | 75 #else |
83 static const bool kDebugMode = true; | 76 static const bool kDebugMode = true; |
84 #endif | 77 #endif |
85 | 78 |
86 // TODO(sanjay): Move the code below into the tcmalloc namespace | |
87 using tcmalloc::kLog; | |
88 using tcmalloc::Log; | |
89 | |
90 // Anonymous namespace to avoid name conflicts on "CheckAddressBits". | 79 // Anonymous namespace to avoid name conflicts on "CheckAddressBits". |
91 namespace { | 80 namespace { |
92 | 81 |
93 // Check that no bit is set at position ADDRESS_BITS or higher. | 82 // Check that no bit is set at position ADDRESS_BITS or higher. |
94 template <int ADDRESS_BITS> bool CheckAddressBits(uintptr_t ptr) { | 83 template <int ADDRESS_BITS> bool CheckAddressBits(uintptr_t ptr) { |
95 return (ptr >> ADDRESS_BITS) == 0; | 84 return (ptr >> ADDRESS_BITS) == 0; |
96 } | 85 } |
97 | 86 |
98 // Specialize for the bit width of a pointer to avoid undefined shift. | 87 // Specialize for the bit width of a pointer to avoid undefined shift. |
99 template <> bool CheckAddressBits<8 * sizeof(void*)>(uintptr_t ptr) { | 88 template <> bool CheckAddressBits<8 * sizeof(void*)>(uintptr_t ptr) { |
100 return true; | 89 return true; |
101 } | 90 } |
102 | 91 |
103 } // Anonymous namespace to avoid name conflicts on "CheckAddressBits". | 92 } // Anonymous namespace to avoid name conflicts on "CheckAddressBits". |
104 | 93 |
105 COMPILE_ASSERT(kAddressBits <= 8 * sizeof(void*), | 94 COMPILE_ASSERT(kAddressBits <= 8 * sizeof(void*), |
106 address_bits_larger_than_pointer_size); | 95 address_bits_larger_than_pointer_size); |
107 | 96 |
108 // Structure for discovering alignment | 97 // Structure for discovering alignment |
109 union MemoryAligner { | 98 union MemoryAligner { |
110 void* p; | 99 void* p; |
111 double d; | 100 double d; |
112 size_t s; | 101 size_t s; |
113 } CACHELINE_ALIGNED; | 102 } CACHELINE_ALIGNED; |
114 | 103 |
115 static SpinLock spinlock(SpinLock::LINKER_INITIALIZED); | 104 static SpinLock spinlock(SpinLock::LINKER_INITIALIZED); |
116 | 105 |
117 #if defined(HAVE_MMAP) || defined(MADV_FREE) | 106 #if defined(HAVE_MMAP) || defined(MADV_DONTNEED) |
118 // Page size is initialized on demand (only needed for mmap-based allocators) | 107 // Page size is initialized on demand (only needed for mmap-based allocators) |
119 static size_t pagesize = 0; | 108 static size_t pagesize = 0; |
120 #endif | 109 #endif |
121 | 110 |
122 // The current system allocator | 111 // The current system allocator |
123 SysAllocator* sys_alloc = NULL; | 112 SysAllocator* sys_alloc = NULL; |
124 | 113 |
125 // Configuration parameters. | 114 // Configuration parameters. |
126 DEFINE_int32(malloc_devmem_start, | 115 DEFINE_int32(malloc_devmem_start, |
127 EnvToInt("TCMALLOC_DEVMEM_START", 0), | 116 EnvToInt("TCMALLOC_DEVMEM_START", 0), |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 } | 413 } |
425 | 414 |
426 void* DefaultSysAllocator::Alloc(size_t size, size_t *actual_size, | 415 void* DefaultSysAllocator::Alloc(size_t size, size_t *actual_size, |
427 size_t alignment) { | 416 size_t alignment) { |
428 for (int i = 0; i < kMaxAllocators; i++) { | 417 for (int i = 0; i < kMaxAllocators; i++) { |
429 if (!failed_[i] && allocs_[i] != NULL) { | 418 if (!failed_[i] && allocs_[i] != NULL) { |
430 void* result = allocs_[i]->Alloc(size, actual_size, alignment); | 419 void* result = allocs_[i]->Alloc(size, actual_size, alignment); |
431 if (result != NULL) { | 420 if (result != NULL) { |
432 return result; | 421 return result; |
433 } | 422 } |
| 423 TCMalloc_MESSAGE(__FILE__, __LINE__, "%s failed.\n", names_[i]); |
434 failed_[i] = true; | 424 failed_[i] = true; |
435 } | 425 } |
436 } | 426 } |
437 // After both failed, reset "failed_" to false so that a single failed | 427 // After both failed, reset "failed_" to false so that a single failed |
438 // allocation won't make the allocator never work again. | 428 // allocation won't make the allocator never work again. |
439 for (int i = 0; i < kMaxAllocators; i++) { | 429 for (int i = 0; i < kMaxAllocators; i++) { |
440 failed_[i] = false; | 430 failed_[i] = false; |
441 } | 431 } |
442 return NULL; | 432 return NULL; |
443 } | 433 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
487 reinterpret_cast<uintptr_t>(result) + *actual_size - 1); | 477 reinterpret_cast<uintptr_t>(result) + *actual_size - 1); |
488 } else { | 478 } else { |
489 CheckAddressBits<kAddressBits>( | 479 CheckAddressBits<kAddressBits>( |
490 reinterpret_cast<uintptr_t>(result) + size - 1); | 480 reinterpret_cast<uintptr_t>(result) + size - 1); |
491 } | 481 } |
492 } | 482 } |
493 return result; | 483 return result; |
494 } | 484 } |
495 | 485 |
496 void TCMalloc_SystemRelease(void* start, size_t length) { | 486 void TCMalloc_SystemRelease(void* start, size_t length) { |
497 #ifdef MADV_FREE | 487 #ifdef MADV_DONTNEED |
498 if (FLAGS_malloc_devmem_start) { | 488 if (FLAGS_malloc_devmem_start) { |
499 // It's not safe to use MADV_FREE/MADV_DONTNEED if we've been | 489 // It's not safe to use MADV_DONTNEED if we've been mapping |
500 // mapping /dev/mem for heap memory. | 490 // /dev/mem for heap memory |
501 return; | 491 return; |
502 } | 492 } |
503 if (pagesize == 0) pagesize = getpagesize(); | 493 if (pagesize == 0) pagesize = getpagesize(); |
504 const size_t pagemask = pagesize - 1; | 494 const size_t pagemask = pagesize - 1; |
505 | 495 |
506 size_t new_start = reinterpret_cast<size_t>(start); | 496 size_t new_start = reinterpret_cast<size_t>(start); |
507 size_t end = new_start + length; | 497 size_t end = new_start + length; |
508 size_t new_end = end; | 498 size_t new_end = end; |
509 | 499 |
510 // Round up the starting address and round down the ending address | 500 // Round up the starting address and round down the ending address |
511 // to be page aligned: | 501 // to be page aligned: |
512 new_start = (new_start + pagesize - 1) & ~pagemask; | 502 new_start = (new_start + pagesize - 1) & ~pagemask; |
513 new_end = new_end & ~pagemask; | 503 new_end = new_end & ~pagemask; |
514 | 504 |
515 ASSERT((new_start & pagemask) == 0); | 505 ASSERT((new_start & pagemask) == 0); |
516 ASSERT((new_end & pagemask) == 0); | 506 ASSERT((new_end & pagemask) == 0); |
517 ASSERT(new_start >= reinterpret_cast<size_t>(start)); | 507 ASSERT(new_start >= reinterpret_cast<size_t>(start)); |
518 ASSERT(new_end <= end); | 508 ASSERT(new_end <= end); |
519 | 509 |
520 if (new_end > new_start) { | 510 if (new_end > new_start) { |
521 // Note -- ignoring most return codes, because if this fails it | 511 // Note -- ignoring most return codes, because if this fails it |
522 // doesn't matter... | 512 // doesn't matter... |
523 while (madvise(reinterpret_cast<char*>(new_start), new_end - new_start, | 513 while (madvise(reinterpret_cast<char*>(new_start), new_end - new_start, |
524 MADV_FREE) == -1 && | 514 MADV_DONTNEED) == -1 && |
525 errno == EAGAIN) { | 515 errno == EAGAIN) { |
526 // NOP | 516 // NOP |
527 } | 517 } |
528 } | 518 } |
529 #endif | 519 #endif |
530 } | 520 } |
OLD | NEW |