Chromium Code Reviews| 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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 61 | 61 |
| 62 // Returns the number of free objects in cache. | 62 // Returns the number of free objects in cache. |
| 63 int length() { | 63 int length() { |
| 64 SpinLockHolder h(&lock_); | 64 SpinLockHolder h(&lock_); |
| 65 return counter_; | 65 return counter_; |
| 66 } | 66 } |
| 67 | 67 |
| 68 // Returns the number of free objects in the transfer cache. | 68 // Returns the number of free objects in the transfer cache. |
| 69 int tc_length(); | 69 int tc_length(); |
| 70 | 70 |
| 71 // Returns the memory overhead (internal fragmentation) attributable | |
| 72 // to the freelist. This is memory lost when the size of elements | |
| 73 // in a freelist doesn't exactly divide the page-size (an 8192-byte | |
| 74 // page full of 5-byte objects would have 2 bytes memory overhead). | |
|
jar (doing other things)
2011/07/31 08:19:39
This commment is confusing... but perhaps you're j
| |
| 75 size_t OverheadBytes(); | |
| 76 | |
| 71 private: | 77 private: |
| 72 // TransferCache is used to cache transfers of | 78 // TransferCache is used to cache transfers of |
| 73 // sizemap.num_objects_to_move(size_class) back and forth between | 79 // sizemap.num_objects_to_move(size_class) back and forth between |
| 74 // thread caches and the central cache for a given size class. | 80 // thread caches and the central cache for a given size class. |
| 75 struct TCEntry { | 81 struct TCEntry { |
| 76 void *head; // Head of chain of objects. | 82 void *head; // Head of chain of objects. |
| 77 void *tail; // Tail of chain of objects. | 83 void *tail; // Tail of chain of objects. |
| 78 }; | 84 }; |
| 79 | 85 |
| 80 // A central cache freelist can have anywhere from 0 to kNumTransferEntries | 86 // A central cache freelist can have anywhere from 0 to kMaxNumTransferEntries |
| 81 // slots to put link list chains into. To keep memory usage bounded the total | 87 // slots to put link list chains into. |
| 82 // number of TCEntries across size classes is fixed. Currently each size | |
| 83 // class is initially given one TCEntry which also means that the maximum any | |
| 84 // one class can have is kNumClasses. | |
| 85 #ifdef TCMALLOC_SMALL_BUT_SLOW | 88 #ifdef TCMALLOC_SMALL_BUT_SLOW |
| 86 // For the small memory model, the transfer cache is not used. | 89 // For the small memory model, the transfer cache is not used. |
| 87 static const int kNumTransferEntries = 0; | 90 static const int kMaxNumTransferEntries = 0; |
| 88 #else | 91 #else |
| 89 static const int kNumTransferEntries = kNumClasses; | 92 // Starting point for the the maximum number of entries in the transfer cache. |
| 93 // This actual maximum for a given size class may be lower than this | |
| 94 // maximum value. | |
| 95 static const int kMaxNumTransferEntries = 64; | |
| 90 #endif | 96 #endif |
| 91 | 97 |
| 92 // REQUIRES: lock_ is held | 98 // REQUIRES: lock_ is held |
| 93 // Remove object from cache and return. | 99 // Remove object from cache and return. |
| 94 // Return NULL if no free entries in cache. | 100 // Return NULL if no free entries in cache. |
| 95 void* FetchFromSpans() EXCLUSIVE_LOCKS_REQUIRED(lock_); | 101 void* FetchFromSpans() EXCLUSIVE_LOCKS_REQUIRED(lock_); |
| 96 | 102 |
| 97 // REQUIRES: lock_ is held | 103 // REQUIRES: lock_ is held |
| 98 // Remove object from cache and return. Fetches | 104 // Remove object from cache and return. Fetches |
| 99 // from pageheap if cache is empty. Only returns | 105 // from pageheap if cache is empty. Only returns |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 138 bool ShrinkCache(int locked_size_class, bool force) LOCKS_EXCLUDED(lock_); | 144 bool ShrinkCache(int locked_size_class, bool force) LOCKS_EXCLUDED(lock_); |
| 139 | 145 |
| 140 // This lock protects all the data members. cached_entries and cache_size_ | 146 // This lock protects all the data members. cached_entries and cache_size_ |
| 141 // may be looked at without holding the lock. | 147 // may be looked at without holding the lock. |
| 142 SpinLock lock_; | 148 SpinLock lock_; |
| 143 | 149 |
| 144 // We keep linked lists of empty and non-empty spans. | 150 // We keep linked lists of empty and non-empty spans. |
| 145 size_t size_class_; // My size class | 151 size_t size_class_; // My size class |
| 146 Span empty_; // Dummy header for list of empty spans | 152 Span empty_; // Dummy header for list of empty spans |
| 147 Span nonempty_; // Dummy header for list of non-empty spans | 153 Span nonempty_; // Dummy header for list of non-empty spans |
| 154 size_t num_spans_; // Number of spans in empty_ plus nonempty_ | |
| 148 size_t counter_; // Number of free objects in cache entry | 155 size_t counter_; // Number of free objects in cache entry |
| 149 | 156 |
| 150 // Here we reserve space for TCEntry cache slots. Since one size class can | 157 // Here we reserve space for TCEntry cache slots. Space is preallocated |
| 151 // end up getting all the TCEntries quota in the system we just preallocate | 158 // for the largest possible number of entries than any one size class may |
| 152 // sufficient number of entries here. | 159 // accumulate. Not all size classes are allowed to accumulate |
| 153 TCEntry tc_slots_[kNumTransferEntries]; | 160 // kMaxNumTransferEntries, so there is some wasted space for those size |
| 161 // classes. | |
| 162 TCEntry tc_slots_[kMaxNumTransferEntries]; | |
| 154 | 163 |
| 155 // Number of currently used cached entries in tc_slots_. This variable is | 164 // Number of currently used cached entries in tc_slots_. This variable is |
| 156 // updated under a lock but can be read without one. | 165 // updated under a lock but can be read without one. |
| 157 int32_t used_slots_; | 166 int32_t used_slots_; |
| 158 // The current number of slots for this size class. This is an | 167 // The current number of slots for this size class. This is an |
| 159 // adaptive value that is increased if there is lots of traffic | 168 // adaptive value that is increased if there is lots of traffic |
| 160 // on a given size class. | 169 // on a given size class. |
| 161 int32_t cache_size_; | 170 int32_t cache_size_; |
| 171 // Maximum size of the cache for a given size class. | |
| 172 int32_t max_cache_size_; | |
| 162 }; | 173 }; |
| 163 | 174 |
| 164 // Pads each CentralCache object to multiple of 64 bytes. Since some | 175 // Pads each CentralCache object to multiple of 64 bytes. Since some |
| 165 // compilers (such as MSVC) don't like it when the padding is 0, I use | 176 // compilers (such as MSVC) don't like it when the padding is 0, I use |
| 166 // template specialization to remove the padding entirely when | 177 // template specialization to remove the padding entirely when |
| 167 // sizeof(CentralFreeList) is a multiple of 64. | 178 // sizeof(CentralFreeList) is a multiple of 64. |
| 168 template<int kFreeListSizeMod64> | 179 template<int kFreeListSizeMod64> |
| 169 class CentralFreeListPaddedTo : public CentralFreeList { | 180 class CentralFreeListPaddedTo : public CentralFreeList { |
| 170 private: | 181 private: |
| 171 char pad_[64 - kFreeListSizeMod64]; | 182 char pad_[64 - kFreeListSizeMod64]; |
| 172 }; | 183 }; |
| 173 | 184 |
| 174 template<> | 185 template<> |
| 175 class CentralFreeListPaddedTo<0> : public CentralFreeList { | 186 class CentralFreeListPaddedTo<0> : public CentralFreeList { |
| 176 }; | 187 }; |
| 177 | 188 |
| 178 class CentralFreeListPadded : public CentralFreeListPaddedTo< | 189 class CentralFreeListPadded : public CentralFreeListPaddedTo< |
| 179 sizeof(CentralFreeList) % 64> { | 190 sizeof(CentralFreeList) % 64> { |
| 180 }; | 191 }; |
| 181 | 192 |
| 182 } // namespace tcmalloc | 193 } // namespace tcmalloc |
| 183 | 194 |
| 184 #endif // TCMALLOC_CENTRAL_FREELIST_H_ | 195 #endif // TCMALLOC_CENTRAL_FREELIST_H_ |
| OLD | NEW |