OLD | NEW |
1 // Copyright (c) 2005, Google Inc. | 1 // Copyright (c) 2005, 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 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 | 133 |
134 #if (defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)) && !defi
ned(WIN32_OVERRIDE_ALLOCATORS) | 134 #if (defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)) && !defi
ned(WIN32_OVERRIDE_ALLOCATORS) |
135 # define WIN32_DO_PATCHING 1 | 135 # define WIN32_DO_PATCHING 1 |
136 #endif | 136 #endif |
137 | 137 |
138 // Some windows file somewhere (at least on cygwin) #define's small (!) | 138 // Some windows file somewhere (at least on cygwin) #define's small (!) |
139 // For instance, <windows.h> appears to have "#define small char". | 139 // For instance, <windows.h> appears to have "#define small char". |
140 #undef small | 140 #undef small |
141 | 141 |
142 using STL_NAMESPACE::max; | 142 using STL_NAMESPACE::max; |
| 143 using STL_NAMESPACE::min; |
143 using STL_NAMESPACE::numeric_limits; | 144 using STL_NAMESPACE::numeric_limits; |
144 using STL_NAMESPACE::vector; | 145 using STL_NAMESPACE::vector; |
145 | 146 |
146 #include "libc_override.h" | 147 #include "libc_override.h" |
147 | 148 |
148 // __THROW is defined in glibc (via <sys/cdefs.h>). It means, | 149 // __THROW is defined in glibc (via <sys/cdefs.h>). It means, |
149 // counter-intuitively, "This function will never throw an exception." | 150 // counter-intuitively, "This function will never throw an exception." |
150 // It's an optional optimization tool, but we may need to use it to | 151 // It's an optional optimization tool, but we may need to use it to |
151 // match glibc prototypes. | 152 // match glibc prototypes. |
152 #ifndef __THROW // I guess we're not on a glibc system | 153 #ifndef __THROW // I guess we're not on a glibc system |
(...skipping 1087 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1240 AddRoomForMark(&new_size); | 1241 AddRoomForMark(&new_size); |
1241 // Get the size of the old entry | 1242 // Get the size of the old entry |
1242 const size_t old_size = GetSizeWithCallback(old_ptr, invalid_get_size_fn); | 1243 const size_t old_size = GetSizeWithCallback(old_ptr, invalid_get_size_fn); |
1243 | 1244 |
1244 // Reallocate if the new size is larger than the old size, | 1245 // Reallocate if the new size is larger than the old size, |
1245 // or if the new size is significantly smaller than the old size. | 1246 // or if the new size is significantly smaller than the old size. |
1246 // We do hysteresis to avoid resizing ping-pongs: | 1247 // We do hysteresis to avoid resizing ping-pongs: |
1247 // . If we need to grow, grow to max(new_size, old_size * 1.X) | 1248 // . If we need to grow, grow to max(new_size, old_size * 1.X) |
1248 // . Don't shrink unless new_size < old_size * 0.Y | 1249 // . Don't shrink unless new_size < old_size * 0.Y |
1249 // X and Y trade-off time for wasted space. For now we do 1.25 and 0.5. | 1250 // X and Y trade-off time for wasted space. For now we do 1.25 and 0.5. |
1250 const size_t lower_bound_to_grow = old_size + old_size / 4; | 1251 const size_t min_growth = min(old_size / 4, |
| 1252 (std::numeric_limits<size_t>::max)() - old_size); // Avoid overflow. |
| 1253 const size_t lower_bound_to_grow = old_size + min_growth; |
1251 const size_t upper_bound_to_shrink = old_size / 2; | 1254 const size_t upper_bound_to_shrink = old_size / 2; |
1252 if ((new_size > old_size) || (new_size < upper_bound_to_shrink)) { | 1255 if ((new_size > old_size) || (new_size < upper_bound_to_shrink)) { |
1253 // Need to reallocate. | 1256 // Need to reallocate. |
1254 void* new_ptr = NULL; | 1257 void* new_ptr = NULL; |
1255 | 1258 |
1256 if (new_size > old_size && new_size < lower_bound_to_grow) { | 1259 if (new_size > old_size && new_size < lower_bound_to_grow) { |
1257 new_ptr = do_malloc_or_cpp_alloc(lower_bound_to_grow); | 1260 new_ptr = do_malloc_or_cpp_alloc(lower_bound_to_grow); |
1258 } | 1261 } |
1259 ExcludeMarkFromSize(&new_size); // do_malloc will add space if needed. | 1262 ExcludeMarkFromSize(&new_size); // do_malloc will add space if needed. |
1260 if (new_ptr == NULL) { | 1263 if (new_ptr == NULL) { |
(...skipping 607 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1868 *mark = ~allocated_mark; // Distinctively not allocated. | 1871 *mark = ~allocated_mark; // Distinctively not allocated. |
1869 } | 1872 } |
1870 | 1873 |
1871 static void MarkAllocatedRegion(void* ptr) { | 1874 static void MarkAllocatedRegion(void* ptr) { |
1872 if (ptr == NULL) return; | 1875 if (ptr == NULL) return; |
1873 MarkType* mark = GetMarkLocation(ptr); | 1876 MarkType* mark = GetMarkLocation(ptr); |
1874 *mark = GetMarkValue(ptr, mark); | 1877 *mark = GetMarkValue(ptr, mark); |
1875 } | 1878 } |
1876 | 1879 |
1877 #endif // TCMALLOC_VALIDATION | 1880 #endif // TCMALLOC_VALIDATION |
OLD | NEW |