Chromium Code Reviews| 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 1071 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1082 return result; | 1082 return result; |
| 1083 } | 1083 } |
| 1084 | 1084 |
| 1085 inline void* do_malloc(size_t size) { | 1085 inline void* do_malloc(size_t size) { |
| 1086 AddRoomForMark(&size); | 1086 AddRoomForMark(&size); |
| 1087 | 1087 |
| 1088 void* ret = NULL; | 1088 void* ret = NULL; |
| 1089 | 1089 |
| 1090 // The following call forces module initialization | 1090 // The following call forces module initialization |
| 1091 ThreadCache* heap = ThreadCache::GetCache(); | 1091 ThreadCache* heap = ThreadCache::GetCache(); |
| 1092 // First, check if our security policy allows this size. | 1092 if (size <= kMaxSize && IsAllocSizePermitted(size)) { |
|
jar (doing other things)
2013/03/15 23:00:47
It would probably be clearer to not even risk call
jln (very slow on Chromium)
2013/03/17 10:21:51
For security, I would really be wary of making any
| |
| 1093 if (IsAllocSizePermitted(size)) { | 1093 size_t cl = Static::sizemap()->SizeClass(size); |
| 1094 if (size <= kMaxSize) { | 1094 size = Static::sizemap()->class_to_size(cl); |
| 1095 size_t cl = Static::sizemap()->SizeClass(size); | |
| 1096 size = Static::sizemap()->class_to_size(cl); | |
| 1097 | 1095 |
| 1098 // TODO(jar): If this has any detectable performance impact, it can be | 1096 // TODO(jar): If this has any detectable performance impact, it can be |
| 1099 // optimized by only tallying sizes if the profiler was activated to | 1097 // optimized by only tallying sizes if the profiler was activated to |
| 1100 // recall these tallies. I don't think this is performance critical, but | 1098 // recall these tallies. I don't think this is performance critical, but |
| 1101 // we really should measure it. | 1099 // we really should measure it. |
| 1102 heap->AddToByteAllocatedTotal(size); // Chromium profiling. | 1100 heap->AddToByteAllocatedTotal(size); // Chromium profiling. |
|
jar (doing other things)
2013/03/15 23:00:47
...but this line of optimization points a scary fi
| |
| 1103 | 1101 |
| 1104 if ((FLAGS_tcmalloc_sample_parameter > 0) && | 1102 if ((FLAGS_tcmalloc_sample_parameter > 0) && |
| 1105 heap->SampleAllocation(size)) { | 1103 heap->SampleAllocation(size)) { |
| 1106 ret = DoSampledAllocation(size); | 1104 ret = DoSampledAllocation(size); |
| 1107 MarkAllocatedRegion(ret); | 1105 MarkAllocatedRegion(ret); |
| 1108 } else { | |
| 1109 // The common case, and also the simplest. This just pops the | |
| 1110 // size-appropriate freelist, after replenishing it if it's empty. | |
| 1111 ret = CheckMallocResult(heap->Allocate(size, cl)); | |
| 1112 } | |
| 1113 } else { | 1106 } else { |
| 1114 ret = do_malloc_pages(heap, size); | 1107 // The common case, and also the simplest. This just pops the |
| 1115 MarkAllocatedRegion(ret); | 1108 // size-appropriate freelist, after replenishing it if it's empty. |
| 1109 ret = CheckMallocResult(heap->Allocate(size, cl)); | |
| 1116 } | 1110 } |
| 1111 } else if (IsAllocSizePermitted(size)) { | |
| 1112 ret = do_malloc_pages(heap, size); | |
| 1113 MarkAllocatedRegion(ret); | |
| 1117 } | 1114 } |
| 1118 if (ret == NULL) errno = ENOMEM; | 1115 if (ret == NULL) errno = ENOMEM; |
| 1119 return ret; | 1116 return ret; |
| 1120 } | 1117 } |
| 1121 | 1118 |
| 1122 inline void* do_calloc(size_t n, size_t elem_size) { | 1119 inline void* do_calloc(size_t n, size_t elem_size) { |
| 1123 // Overflow check | 1120 // Overflow check |
| 1124 const size_t size = n * elem_size; | 1121 const size_t size = n * elem_size; |
| 1125 if (elem_size != 0 && size / elem_size != n) return NULL; | 1122 if (elem_size != 0 && size / elem_size != n) return NULL; |
| 1126 | 1123 |
| (...skipping 744 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1871 *mark = ~allocated_mark; // Distinctively not allocated. | 1868 *mark = ~allocated_mark; // Distinctively not allocated. |
| 1872 } | 1869 } |
| 1873 | 1870 |
| 1874 static void MarkAllocatedRegion(void* ptr) { | 1871 static void MarkAllocatedRegion(void* ptr) { |
| 1875 if (ptr == NULL) return; | 1872 if (ptr == NULL) return; |
| 1876 MarkType* mark = GetMarkLocation(ptr); | 1873 MarkType* mark = GetMarkLocation(ptr); |
| 1877 *mark = GetMarkValue(ptr, mark); | 1874 *mark = GetMarkValue(ptr, mark); |
| 1878 } | 1875 } |
| 1879 | 1876 |
| 1880 #endif // TCMALLOC_VALIDATION | 1877 #endif // TCMALLOC_VALIDATION |
| OLD | NEW |