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