| 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 13 matching lines...) Expand all Loading... |
| 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 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 "system-alloc.h" | 35 #include "system-alloc.h" |
| 35 #include "config.h" | |
| 36 #include "common.h" | |
| 37 | 36 |
| 38 namespace tcmalloc { | 37 namespace tcmalloc { |
| 39 | 38 |
| 40 // Note: the following only works for "n"s that fit in 32-bits, but | 39 // Note: the following only works for "n"s that fit in 32-bits, but |
| 41 // that is fine since we only use it for small sizes. | 40 // that is fine since we only use it for small sizes. |
| 42 static inline int LgFloor(size_t n) { | 41 static inline int LgFloor(size_t n) { |
| 43 int log = 0; | 42 int log = 0; |
| 44 for (int i = 4; i >= 0; --i) { | 43 for (int i = 4; i >= 0; --i) { |
| 45 int shift = (1 << i); | 44 int shift = (1 << i); |
| 46 size_t x = n >> shift; | 45 size_t x = n >> shift; |
| 47 if (x != 0) { | 46 if (x != 0) { |
| 48 n = x; | 47 n = x; |
| 49 log += shift; | 48 log += shift; |
| 50 } | 49 } |
| 51 } | 50 } |
| 52 ASSERT(n == 1); | 51 ASSERT(n == 1); |
| 53 return log; | 52 return log; |
| 54 } | 53 } |
| 55 | 54 |
| 55 int AlignmentForSize(size_t size) { |
| 56 int alignment = kAlignment; |
| 57 if (size >= 2048) { |
| 58 // Cap alignment at 256 for large sizes. |
| 59 alignment = 256; |
| 60 } else if (size >= 128) { |
| 61 // Space wasted due to alignment is at most 1/8, i.e., 12.5%. |
| 62 alignment = (1 << LgFloor(size)) / 8; |
| 63 } else if (size >= 16) { |
| 64 // We need an alignment of at least 16 bytes to satisfy |
| 65 // requirements for some SSE types. |
| 66 alignment = 16; |
| 67 } |
| 68 CHECK_CONDITION(size < 16 || alignment >= 16); |
| 69 CHECK_CONDITION((alignment & (alignment - 1)) == 0); |
| 70 return alignment; |
| 71 } |
| 72 |
| 56 int SizeMap::NumMoveSize(size_t size) { | 73 int SizeMap::NumMoveSize(size_t size) { |
| 57 if (size == 0) return 0; | 74 if (size == 0) return 0; |
| 58 // Use approx 64k transfers between thread and central caches. | 75 // Use approx 64k transfers between thread and central caches. |
| 59 int num = static_cast<int>(64.0 * 1024.0 / size); | 76 int num = static_cast<int>(64.0 * 1024.0 / size); |
| 60 if (num < 2) num = 2; | 77 if (num < 2) num = 2; |
| 61 | 78 |
| 62 // Avoid bringing too many objects into small object free lists. | 79 // Avoid bringing too many objects into small object free lists. |
| 63 // If this value is too large: | 80 // If this value is too large: |
| 64 // - We waste memory with extra objects sitting in the thread caches. | 81 // - We waste memory with extra objects sitting in the thread caches. |
| 65 // - The central freelist holds its lock for too long while | 82 // - The central freelist holds its lock for too long while |
| (...skipping 20 matching lines...) Expand all Loading... |
| 86 | 103 |
| 87 // Compute the size classes we want to use | 104 // Compute the size classes we want to use |
| 88 int sc = 1; // Next size class to assign | 105 int sc = 1; // Next size class to assign |
| 89 int alignment = kAlignment; | 106 int alignment = kAlignment; |
| 90 CHECK_CONDITION(kAlignment <= 16); | 107 CHECK_CONDITION(kAlignment <= 16); |
| 91 int last_lg = -1; | 108 int last_lg = -1; |
| 92 for (size_t size = kAlignment; size <= kMaxSize; size += alignment) { | 109 for (size_t size = kAlignment; size <= kMaxSize; size += alignment) { |
| 93 int lg = LgFloor(size); | 110 int lg = LgFloor(size); |
| 94 if (lg > last_lg) { | 111 if (lg > last_lg) { |
| 95 // Increase alignment every so often to reduce number of size classes. | 112 // Increase alignment every so often to reduce number of size classes. |
| 96 if (size >= 2048) { | 113 alignment = AlignmentForSize(size); |
| 97 // Cap alignment at 256 for large sizes | |
| 98 alignment = 256; | |
| 99 } else if (size >= 128) { | |
| 100 // Space wasted due to alignment is at most 1/8, i.e., 12.5%. | |
| 101 alignment = size / 8; | |
| 102 } else if (size >= 16) { | |
| 103 // We need an alignment of at least 16 bytes to satisfy | |
| 104 // requirements for some SSE types. | |
| 105 alignment = 16; | |
| 106 } | |
| 107 CHECK_CONDITION(size < 16 || alignment >= 16); | |
| 108 CHECK_CONDITION((alignment & (alignment - 1)) == 0); | |
| 109 last_lg = lg; | 114 last_lg = lg; |
| 110 } | 115 } |
| 111 CHECK_CONDITION((size % alignment) == 0); | 116 CHECK_CONDITION((size % alignment) == 0); |
| 112 | 117 |
| 113 // Allocate enough pages so leftover is less than 1/8 of total. | 118 // Allocate enough pages so leftover is less than 1/8 of total. |
| 114 // This bounds wasted space to at most 12.5%. | 119 // This bounds wasted space to at most 12.5%. |
| 115 size_t psize = kPageSize; | 120 size_t psize = kPageSize; |
| 116 while ((psize % size) > (psize >> 3)) { | 121 while ((psize % size) > (psize >> 3)) { |
| 117 psize += kPageSize; | 122 psize += kPageSize; |
| 118 } | 123 } |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 return result; | 208 return result; |
| 204 } | 209 } |
| 205 | 210 |
| 206 uint64_t metadata_system_bytes() { return metadata_system_bytes_; } | 211 uint64_t metadata_system_bytes() { return metadata_system_bytes_; } |
| 207 | 212 |
| 208 void increment_metadata_system_bytes(size_t bytes) { | 213 void increment_metadata_system_bytes(size_t bytes) { |
| 209 metadata_system_bytes_ += bytes; | 214 metadata_system_bytes_ += bytes; |
| 210 } | 215 } |
| 211 | 216 |
| 212 } // namespace tcmalloc | 217 } // namespace tcmalloc |
| OLD | NEW |