| OLD | NEW |
| 1 // Copyright (c) 2008, Google Inc. | 1 // Copyright (c) 2008, 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 16 matching lines...) Expand all Loading... |
| 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | 29 |
| 30 // --- | 30 // --- |
| 31 // Author: Sanjay Ghemawat <opensource@google.com> | 31 // Author: Sanjay Ghemawat <opensource@google.com> |
| 32 | 32 |
| 33 #include "config.h" | 33 #include "config.h" |
| 34 #include "common.h" | 34 #include "common.h" |
| 35 #include "system-alloc.h" | 35 #include "system-alloc.h" |
| 36 | 36 |
| 37 #if defined(HAVE_UNISTD_H) && defined(HAVE_GETPAGESIZE) |
| 38 #include <unistd.h> // for getpagesize |
| 39 #endif |
| 40 |
| 37 namespace tcmalloc { | 41 namespace tcmalloc { |
| 38 | 42 |
| 39 // Note: the following only works for "n"s that fit in 32-bits, but | 43 // Note: the following only works for "n"s that fit in 32-bits, but |
| 40 // that is fine since we only use it for small sizes. | 44 // that is fine since we only use it for small sizes. |
| 41 static inline int LgFloor(size_t n) { | 45 static inline int LgFloor(size_t n) { |
| 42 int log = 0; | 46 int log = 0; |
| 43 for (int i = 4; i >= 0; --i) { | 47 for (int i = 4; i >= 0; --i) { |
| 44 int shift = (1 << i); | 48 int shift = (1 << i); |
| 45 size_t x = n >> shift; | 49 size_t x = n >> shift; |
| 46 if (x != 0) { | 50 if (x != 0) { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 } | 103 } |
| 100 if (ClassIndex(kMaxSize) >= sizeof(class_array_)) { | 104 if (ClassIndex(kMaxSize) >= sizeof(class_array_)) { |
| 101 CRASH("Invalid class index %d for kMaxSize\n", ClassIndex(kMaxSize)); | 105 CRASH("Invalid class index %d for kMaxSize\n", ClassIndex(kMaxSize)); |
| 102 } | 106 } |
| 103 | 107 |
| 104 // Compute the size classes we want to use | 108 // Compute the size classes we want to use |
| 105 int sc = 1; // Next size class to assign | 109 int sc = 1; // Next size class to assign |
| 106 int alignment = kAlignment; | 110 int alignment = kAlignment; |
| 107 CHECK_CONDITION(kAlignment <= 16); | 111 CHECK_CONDITION(kAlignment <= 16); |
| 108 int last_lg = -1; | 112 int last_lg = -1; |
| 109 for (size_t size = kAlignment; size <= kMaxSize; size += alignment) { | 113 for (size_t size = kMinClassSize; size <= kMaxSize; size += alignment) { |
| 110 int lg = LgFloor(size); | 114 int lg = LgFloor(size); |
| 111 if (lg > last_lg) { | 115 if (lg > last_lg) { |
| 112 // Increase alignment every so often to reduce number of size classes. | 116 // Increase alignment every so often to reduce number of size classes. |
| 113 alignment = AlignmentForSize(size); | 117 alignment = AlignmentForSize(size); |
| 114 last_lg = lg; | 118 last_lg = lg; |
| 115 } | 119 } |
| 116 CHECK_CONDITION((size % alignment) == 0); | 120 CHECK_CONDITION((size % alignment) == 0); |
| 117 | 121 |
| 118 // Allocate enough pages so leftover is less than 1/8 of total. | 122 // Allocate enough pages so leftover is less than 1/8 of total. |
| 119 // This bounds wasted space to at most 12.5%. | 123 // This bounds wasted space to at most 12.5%. |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 int(class_to_size_[cl]), | 198 int(class_to_size_[cl]), |
| 195 int(class_to_pages_[cl] << kPageShift), | 199 int(class_to_pages_[cl] << kPageShift), |
| 196 max_waste * 100.0 / alloc_size | 200 max_waste * 100.0 / alloc_size |
| 197 ); | 201 ); |
| 198 } | 202 } |
| 199 } | 203 } |
| 200 | 204 |
| 201 // Metadata allocator -- keeps stats about how many bytes allocated. | 205 // Metadata allocator -- keeps stats about how many bytes allocated. |
| 202 static uint64_t metadata_system_bytes_ = 0; | 206 static uint64_t metadata_system_bytes_ = 0; |
| 203 void* MetaDataAlloc(size_t bytes) { | 207 void* MetaDataAlloc(size_t bytes) { |
| 204 void* result = TCMalloc_SystemAlloc(bytes, NULL); | 208 static size_t pagesize; |
| 209 #ifdef HAVE_GETPAGESIZE |
| 210 if (pagesize == 0) |
| 211 pagesize = getpagesize(); |
| 212 #endif |
| 213 |
| 214 void* result = TCMalloc_SystemAlloc(bytes, NULL, pagesize); |
| 205 if (result != NULL) { | 215 if (result != NULL) { |
| 206 metadata_system_bytes_ += bytes; | 216 metadata_system_bytes_ += bytes; |
| 207 } | 217 } |
| 208 return result; | 218 return result; |
| 209 } | 219 } |
| 210 | 220 |
| 211 uint64_t metadata_system_bytes() { return metadata_system_bytes_; } | 221 uint64_t metadata_system_bytes() { return metadata_system_bytes_; } |
| 212 | 222 |
| 223 void increment_metadata_system_bytes(size_t bytes) { |
| 224 metadata_system_bytes_ += bytes; |
| 225 } |
| 226 |
| 213 } // namespace tcmalloc | 227 } // namespace tcmalloc |
| OLD | NEW |