| 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 1060 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  1071   return result; |  1071   return result; | 
|  1072 } |  1072 } | 
|  1073  |  1073  | 
|  1074 inline void* do_malloc(size_t size) { |  1074 inline void* do_malloc(size_t size) { | 
|  1075   AddRoomForMark(&size); |  1075   AddRoomForMark(&size); | 
|  1076  |  1076  | 
|  1077   void* ret = NULL; |  1077   void* ret = NULL; | 
|  1078  |  1078  | 
|  1079   // The following call forces module initialization |  1079   // The following call forces module initialization | 
|  1080   ThreadCache* heap = ThreadCache::GetCache(); |  1080   ThreadCache* heap = ThreadCache::GetCache(); | 
|  1081   if (size <= kMaxSize) { |  1081   // First, check if our security policy allows this size. | 
|  1082     size_t cl = Static::sizemap()->SizeClass(size); |  1082   if (tcmalloc::IsAllocSizePermitted(size)) { | 
|  1083     size = Static::sizemap()->class_to_size(cl); |  1083     if (size <= kMaxSize) { | 
 |  1084       size_t cl = Static::sizemap()->SizeClass(size); | 
 |  1085       size = Static::sizemap()->class_to_size(cl); | 
|  1084  |  1086  | 
|  1085     // TODO(jar): If this has any detectable performance impact, it can be |  1087       // TODO(jar): If this has any detectable performance impact, it can be | 
|  1086     // optimized by only tallying sizes if the profiler was activated to recall |  1088       // optimized by only tallying sizes if the profiler was activated to | 
|  1087     // these tallies.  I don't think this is performance critical, but we really |  1089       // recall these tallies.  I don't think this is performance critical, but | 
|  1088     // should measure it. |  1090       // we really should measure it. | 
|  1089     heap->AddToByteAllocatedTotal(size);  // Chromium profiling. |  1091       heap->AddToByteAllocatedTotal(size);  // Chromium profiling. | 
|  1090  |  1092  | 
|  1091     if ((FLAGS_tcmalloc_sample_parameter > 0) && heap->SampleAllocation(size)) { |  1093       if ((FLAGS_tcmalloc_sample_parameter > 0) && | 
|  1092       ret = DoSampledAllocation(size); |  1094           heap->SampleAllocation(size)) { | 
 |  1095         ret = DoSampledAllocation(size); | 
 |  1096         MarkAllocatedRegion(ret); | 
 |  1097       } else { | 
 |  1098         // The common case, and also the simplest.  This just pops the | 
 |  1099         // size-appropriate freelist, after replenishing it if it's empty. | 
 |  1100         ret = CheckMallocResult(heap->Allocate(size, cl)); | 
 |  1101       } | 
 |  1102     } else { | 
 |  1103       ret = do_malloc_pages(heap, size); | 
|  1093       MarkAllocatedRegion(ret); |  1104       MarkAllocatedRegion(ret); | 
|  1094     } else { |  | 
|  1095       // The common case, and also the simplest.  This just pops the |  | 
|  1096       // size-appropriate freelist, after replenishing it if it's empty. |  | 
|  1097       ret = CheckMallocResult(heap->Allocate(size, cl)); |  | 
|  1098     } |  1105     } | 
|  1099   } else { |  | 
|  1100     ret = do_malloc_pages(heap, size); |  | 
|  1101     MarkAllocatedRegion(ret); |  | 
|  1102   } |  1106   } | 
|  1103   if (ret == NULL) errno = ENOMEM; |  1107   if (ret == NULL) errno = ENOMEM; | 
|  1104   return ret; |  1108   return ret; | 
|  1105 } |  1109 } | 
|  1106  |  1110  | 
|  1107 inline void* do_calloc(size_t n, size_t elem_size) { |  1111 inline void* do_calloc(size_t n, size_t elem_size) { | 
|  1108   // Overflow check |  1112   // Overflow check | 
|  1109   const size_t size = n * elem_size; |  1113   const size_t size = n * elem_size; | 
|  1110   if (elem_size != 0 && size / elem_size != n) return NULL; |  1114   if (elem_size != 0 && size / elem_size != n) return NULL; | 
|  1111  |  1115  | 
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  1226   AddRoomForMark(&new_size); |  1230   AddRoomForMark(&new_size); | 
|  1227   // Get the size of the old entry |  1231   // Get the size of the old entry | 
|  1228   const size_t old_size = GetSizeWithCallback(old_ptr, invalid_get_size_fn); |  1232   const size_t old_size = GetSizeWithCallback(old_ptr, invalid_get_size_fn); | 
|  1229  |  1233  | 
|  1230   // Reallocate if the new size is larger than the old size, |  1234   // Reallocate if the new size is larger than the old size, | 
|  1231   // or if the new size is significantly smaller than the old size. |  1235   // or if the new size is significantly smaller than the old size. | 
|  1232   // We do hysteresis to avoid resizing ping-pongs: |  1236   // We do hysteresis to avoid resizing ping-pongs: | 
|  1233   //    . If we need to grow, grow to max(new_size, old_size * 1.X) |  1237   //    . If we need to grow, grow to max(new_size, old_size * 1.X) | 
|  1234   //    . Don't shrink unless new_size < old_size * 0.Y |  1238   //    . Don't shrink unless new_size < old_size * 0.Y | 
|  1235   // X and Y trade-off time for wasted space.  For now we do 1.25 and 0.5. |  1239   // X and Y trade-off time for wasted space.  For now we do 1.25 and 0.5. | 
|  1236   const int lower_bound_to_grow = old_size + old_size / 4; |  1240   const size_t lower_bound_to_grow = old_size + old_size / 4; | 
|  1237   const int upper_bound_to_shrink = old_size / 2; |  1241   const size_t upper_bound_to_shrink = old_size / 2; | 
|  1238   if ((new_size > old_size) || (new_size < upper_bound_to_shrink)) { |  1242   if ((new_size > old_size) || (new_size < upper_bound_to_shrink)) { | 
|  1239     // Need to reallocate. |  1243     // Need to reallocate. | 
|  1240     void* new_ptr = NULL; |  1244     void* new_ptr = NULL; | 
|  1241  |  1245  | 
|  1242     if (new_size > old_size && new_size < lower_bound_to_grow) { |  1246     if (new_size > old_size && new_size < lower_bound_to_grow) { | 
|  1243       new_ptr = do_malloc_or_cpp_alloc(lower_bound_to_grow); |  1247       new_ptr = do_malloc_or_cpp_alloc(lower_bound_to_grow); | 
|  1244     } |  1248     } | 
|  1245     ExcludeMarkFromSize(&new_size);  // do_malloc will add space if needed. |  1249     ExcludeMarkFromSize(&new_size);  // do_malloc will add space if needed. | 
|  1246     if (new_ptr == NULL) { |  1250     if (new_ptr == NULL) { | 
|  1247       // Either new_size is not a tiny increment, or last do_malloc failed. |  1251       // Either new_size is not a tiny increment, or last do_malloc failed. | 
| (...skipping 606 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  1854   *mark = ~allocated_mark;  //  Distinctively not allocated. |  1858   *mark = ~allocated_mark;  //  Distinctively not allocated. | 
|  1855 } |  1859 } | 
|  1856  |  1860  | 
|  1857 static void MarkAllocatedRegion(void* ptr) { |  1861 static void MarkAllocatedRegion(void* ptr) { | 
|  1858   if (ptr == NULL) return; |  1862   if (ptr == NULL) return; | 
|  1859   MarkType* mark = GetMarkLocation(ptr); |  1863   MarkType* mark = GetMarkLocation(ptr); | 
|  1860   *mark = GetMarkValue(ptr, mark); |  1864   *mark = GetMarkValue(ptr, mark); | 
|  1861 } |  1865 } | 
|  1862  |  1866  | 
|  1863 #endif  // TCMALLOC_VALIDATION |  1867 #endif  // TCMALLOC_VALIDATION | 
| OLD | NEW |