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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 } | 108 } |
105 if (ClassIndex(kMaxSize) >= sizeof(class_array_)) { | 109 if (ClassIndex(kMaxSize) >= sizeof(class_array_)) { |
106 Log(kCrash, __FILE__, __LINE__, | 110 Log(kCrash, __FILE__, __LINE__, |
107 "Invalid class index for kMaxSize", ClassIndex(kMaxSize)); | 111 "Invalid class index for kMaxSize", ClassIndex(kMaxSize)); |
108 } | 112 } |
109 | 113 |
110 // Compute the size classes we want to use | 114 // Compute the size classes we want to use |
111 int sc = 1; // Next size class to assign | 115 int sc = 1; // Next size class to assign |
112 int alignment = kAlignment; | 116 int alignment = kAlignment; |
113 CHECK_CONDITION(kAlignment <= 16); | 117 CHECK_CONDITION(kAlignment <= 16); |
114 for (size_t size = kAlignment; size <= kMaxSize; size += alignment) { | 118 for (size_t size = kMinClassSize; size <= kMaxSize; size += alignment) { |
115 alignment = AlignmentForSize(size); | 119 alignment = AlignmentForSize(size); |
116 CHECK_CONDITION((size % alignment) == 0); | 120 CHECK_CONDITION((size % alignment) == 0); |
117 | 121 |
118 int blocks_to_move = NumMoveSize(size) / 4; | 122 int blocks_to_move = NumMoveSize(size) / 4; |
119 size_t psize = 0; | 123 size_t psize = 0; |
120 do { | 124 do { |
121 psize += kPageSize; | 125 psize += kPageSize; |
122 // Allocate enough pages so leftover is less than 1/8 of total. | 126 // Allocate enough pages so leftover is less than 1/8 of total. |
123 // This bounds wasted space to at most 12.5%. | 127 // This bounds wasted space to at most 12.5%. |
124 while ((psize % size) > (psize >> 3)) { | 128 while ((psize % size) > (psize >> 3)) { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 | 187 |
184 // Initialize the num_objects_to_move array. | 188 // Initialize the num_objects_to_move array. |
185 for (size_t cl = 1; cl < kNumClasses; ++cl) { | 189 for (size_t cl = 1; cl < kNumClasses; ++cl) { |
186 num_objects_to_move_[cl] = NumMoveSize(ByteSizeForClass(cl)); | 190 num_objects_to_move_[cl] = NumMoveSize(ByteSizeForClass(cl)); |
187 } | 191 } |
188 } | 192 } |
189 | 193 |
190 // Metadata allocator -- keeps stats about how many bytes allocated. | 194 // Metadata allocator -- keeps stats about how many bytes allocated. |
191 static uint64_t metadata_system_bytes_ = 0; | 195 static uint64_t metadata_system_bytes_ = 0; |
192 void* MetaDataAlloc(size_t bytes) { | 196 void* MetaDataAlloc(size_t bytes) { |
193 void* result = TCMalloc_SystemAlloc(bytes, NULL); | 197 static size_t pagesize; |
| 198 #ifdef HAVE_GETPAGESIZE |
| 199 if (pagesize == 0) |
| 200 pagesize = getpagesize(); |
| 201 #endif |
| 202 |
| 203 void* result = TCMalloc_SystemAlloc(bytes, NULL, pagesize); |
194 if (result != NULL) { | 204 if (result != NULL) { |
195 metadata_system_bytes_ += bytes; | 205 metadata_system_bytes_ += bytes; |
196 } | 206 } |
197 return result; | 207 return result; |
198 } | 208 } |
199 | 209 |
200 uint64_t metadata_system_bytes() { return metadata_system_bytes_; } | 210 uint64_t metadata_system_bytes() { return metadata_system_bytes_; } |
201 | 211 |
| 212 void increment_metadata_system_bytes(size_t bytes) { |
| 213 metadata_system_bytes_ += bytes; |
| 214 } |
| 215 |
202 } // namespace tcmalloc | 216 } // namespace tcmalloc |
OLD | NEW |