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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 n = x; | 51 n = x; |
52 log += shift; | 52 log += shift; |
53 } | 53 } |
54 } | 54 } |
55 ASSERT(n == 1); | 55 ASSERT(n == 1); |
56 return log; | 56 return log; |
57 } | 57 } |
58 | 58 |
59 int AlignmentForSize(size_t size) { | 59 int AlignmentForSize(size_t size) { |
60 int alignment = kAlignment; | 60 int alignment = kAlignment; |
61 if (size > kMaxSize) { | 61 if (size >= 2048) { |
62 // Cap alignment at kPageSize for large sizes. | 62 // Cap alignment at 256 for large sizes. |
63 alignment = kPageSize; | 63 alignment = 256; |
64 } else if (size >= 128) { | 64 } else if (size >= 128) { |
65 // Space wasted due to alignment is at most 1/8, i.e., 12.5%. | 65 // Space wasted due to alignment is at most 1/8, i.e., 12.5%. |
66 alignment = (1 << LgFloor(size)) / 8; | 66 alignment = (1 << LgFloor(size)) / 8; |
67 } else if (size >= 16) { | 67 } else if (size >= 16) { |
68 // We need an alignment of at least 16 bytes to satisfy | 68 // We need an alignment of at least 16 bytes to satisfy |
69 // requirements for some SSE types. | 69 // requirements for some SSE types. |
70 alignment = 16; | 70 alignment = 16; |
71 } | 71 } |
72 // Maximum alignment allowed is page size alignment. | |
73 if (alignment > kPageSize) { | |
74 alignment = kPageSize; | |
75 } | |
76 CHECK_CONDITION(size < 16 || alignment >= 16); | 72 CHECK_CONDITION(size < 16 || alignment >= 16); |
77 CHECK_CONDITION((alignment & (alignment - 1)) == 0); | 73 CHECK_CONDITION((alignment & (alignment - 1)) == 0); |
78 return alignment; | 74 return alignment; |
79 } | 75 } |
80 | 76 |
81 int SizeMap::NumMoveSize(size_t size) { | 77 int SizeMap::NumMoveSize(size_t size) { |
82 if (size == 0) return 0; | 78 if (size == 0) return 0; |
83 // Use approx 64k transfers between thread and central caches. | 79 // Use approx 64k transfers between thread and central caches. |
84 int num = static_cast<int>(64.0 * 1024.0 / size); | 80 int num = static_cast<int>(64.0 * 1024.0 / size); |
85 if (num < 2) num = 2; | 81 if (num < 2) num = 2; |
(...skipping 22 matching lines...) Expand all Loading... |
108 } | 104 } |
109 if (ClassIndex(kMaxSize) >= sizeof(class_array_)) { | 105 if (ClassIndex(kMaxSize) >= sizeof(class_array_)) { |
110 Log(kCrash, __FILE__, __LINE__, | 106 Log(kCrash, __FILE__, __LINE__, |
111 "Invalid class index for kMaxSize", ClassIndex(kMaxSize)); | 107 "Invalid class index for kMaxSize", ClassIndex(kMaxSize)); |
112 } | 108 } |
113 | 109 |
114 // Compute the size classes we want to use | 110 // Compute the size classes we want to use |
115 int sc = 1; // Next size class to assign | 111 int sc = 1; // Next size class to assign |
116 int alignment = kAlignment; | 112 int alignment = kAlignment; |
117 CHECK_CONDITION(kAlignment <= 16); | 113 CHECK_CONDITION(kAlignment <= 16); |
| 114 int last_lg = -1; |
118 for (size_t size = kMinClassSize; size <= kMaxSize; size += alignment) { | 115 for (size_t size = kMinClassSize; size <= kMaxSize; size += alignment) { |
119 alignment = AlignmentForSize(size); | 116 int lg = LgFloor(size); |
| 117 if (lg > last_lg) { |
| 118 // Increase alignment every so often to reduce number of size classes. |
| 119 alignment = AlignmentForSize(size); |
| 120 last_lg = lg; |
| 121 } |
120 CHECK_CONDITION((size % alignment) == 0); | 122 CHECK_CONDITION((size % alignment) == 0); |
121 | 123 |
122 int blocks_to_move = NumMoveSize(size) / 4; | 124 // Allocate enough pages so leftover is less than 1/8 of total. |
123 size_t psize = 0; | 125 // This bounds wasted space to at most 12.5%. |
124 do { | 126 size_t psize = kPageSize; |
| 127 while ((psize % size) > (psize >> 3)) { |
125 psize += kPageSize; | 128 psize += kPageSize; |
126 // Allocate enough pages so leftover is less than 1/8 of total. | 129 } |
127 // This bounds wasted space to at most 12.5%. | |
128 while ((psize % size) > (psize >> 3)) { | |
129 psize += kPageSize; | |
130 } | |
131 // Continue to add pages until there are at least as many objects in | |
132 // the span as are needed when moving objects from the central | |
133 // freelists and spans to the thread caches. | |
134 } while ((psize / size) < (blocks_to_move)); | |
135 const size_t my_pages = psize >> kPageShift; | 130 const size_t my_pages = psize >> kPageShift; |
136 | 131 |
137 if (sc > 1 && my_pages == class_to_pages_[sc-1]) { | 132 if (sc > 1 && my_pages == class_to_pages_[sc-1]) { |
138 // See if we can merge this into the previous class without | 133 // See if we can merge this into the previous class without |
139 // increasing the fragmentation of the previous class. | 134 // increasing the fragmentation of the previous class. |
140 const size_t my_objects = (my_pages << kPageShift) / size; | 135 const size_t my_objects = (my_pages << kPageShift) / size; |
141 const size_t prev_objects = (class_to_pages_[sc-1] << kPageShift) | 136 const size_t prev_objects = (class_to_pages_[sc-1] << kPageShift) |
142 / class_to_size_[sc-1]; | 137 / class_to_size_[sc-1]; |
143 if (my_objects == prev_objects) { | 138 if (my_objects == prev_objects) { |
144 // Adjust last class to include this size | 139 // Adjust last class to include this size |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 return result; | 202 return result; |
208 } | 203 } |
209 | 204 |
210 uint64_t metadata_system_bytes() { return metadata_system_bytes_; } | 205 uint64_t metadata_system_bytes() { return metadata_system_bytes_; } |
211 | 206 |
212 void increment_metadata_system_bytes(size_t bytes) { | 207 void increment_metadata_system_bytes(size_t bytes) { |
213 metadata_system_bytes_ += bytes; | 208 metadata_system_bytes_ += bytes; |
214 } | 209 } |
215 | 210 |
216 } // namespace tcmalloc | 211 } // namespace tcmalloc |
OLD | NEW |