| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/allocator/allocator_shim.h" | 5 #include "base/allocator/allocator_shim.h" |
| 6 | 6 |
| 7 #include <config.h> | 7 #include <config.h> |
| 8 #include "base/allocator/allocator_extension_thunks.h" | 8 #include "base/allocator/allocator_extension_thunks.h" |
| 9 #include "base/profiler/alternate_timer.h" | 9 #include "base/profiler/alternate_timer.h" |
| 10 #include "base/sysinfo.h" | 10 #include "base/sysinfo.h" |
| 11 | 11 |
| 12 // This shim make it possible to use different allocators via an environment | 12 // This shim make it possible to use different allocators via an environment |
| 13 // variable set before running the program. This may reduce the | 13 // variable set before running the program. This may reduce the |
| 14 // amount of inlining that we get with malloc/free/etc. | 14 // amount of inlining that we get with malloc/free/etc. |
| 15 | 15 |
| 16 // TODO(mbelshe): Ensure that all calls to tcmalloc have the proper call depth | 16 // TODO(mbelshe): Ensure that all calls to tcmalloc have the proper call depth |
| 17 // from the "user code" so that debugging tools (HeapChecker) can work. | 17 // from the "user code" so that debugging tools (HeapChecker) can work. |
| 18 | 18 |
| 19 // __THROW is defined in glibc systems. It means, counter-intuitively, | |
| 20 // "This function will never throw an exception." It's an optional | |
| 21 // optimization tool, but we may need to use it to match glibc prototypes. | |
| 22 #ifndef __THROW // I guess we're not on a glibc system | |
| 23 # define __THROW // __THROW is just an optimization, so ok to make it "" | |
| 24 #endif | |
| 25 | |
| 26 // new_mode behaves similarly to MSVC's _set_new_mode. | 19 // new_mode behaves similarly to MSVC's _set_new_mode. |
| 27 // If flag is 0 (default), calls to malloc will behave normally. | 20 // If flag is 0 (default), calls to malloc will behave normally. |
| 28 // If flag is 1, calls to malloc will behave like calls to new, | 21 // If flag is 1, calls to malloc will behave like calls to new, |
| 29 // and the std_new_handler will be invoked on failure. | 22 // and the std_new_handler will be invoked on failure. |
| 30 // Can be set by calling _set_new_mode(). | 23 // Can be set by calling _set_new_mode(). |
| 31 static int new_mode = 0; | 24 static int new_mode = 0; |
| 32 | 25 |
| 33 typedef enum { | 26 typedef enum { |
| 34 TCMALLOC, // TCMalloc is the default allocator. | 27 TCMALLOC, // TCMalloc is the default allocator. |
| 35 WINHEAP, // Windows Heap (standard Windows allocator). | 28 WINHEAP, // Windows Heap (standard Windows allocator). |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 } catch (const std::bad_alloc&) { | 88 } catch (const std::bad_alloc&) { |
| 96 if (!nothrow) | 89 if (!nothrow) |
| 97 throw; | 90 throw; |
| 98 return true; | 91 return true; |
| 99 } | 92 } |
| 100 #endif // (defined(__GNUC__) && !defined(__EXCEPTIONS)) || (defined(_HAS_EXCEPT
IONS) && !_HAS_EXCEPTIONS) | 93 #endif // (defined(__GNUC__) && !defined(__EXCEPTIONS)) || (defined(_HAS_EXCEPT
IONS) && !_HAS_EXCEPTIONS) |
| 101 return false; | 94 return false; |
| 102 } | 95 } |
| 103 | 96 |
| 104 extern "C" { | 97 extern "C" { |
| 105 void* malloc(size_t size) __THROW { | 98 void* malloc(size_t size) { |
| 106 void* ptr; | 99 void* ptr; |
| 107 for (;;) { | 100 for (;;) { |
| 108 switch (allocator) { | 101 switch (allocator) { |
| 109 case WINHEAP: | 102 case WINHEAP: |
| 110 case WINLFH: | 103 case WINLFH: |
| 111 ptr = win_heap_malloc(size); | 104 ptr = win_heap_malloc(size); |
| 112 break; | 105 break; |
| 113 case TCMALLOC: | 106 case TCMALLOC: |
| 114 default: | 107 default: |
| 115 ptr = do_malloc(size); | 108 ptr = do_malloc(size); |
| 116 break; | 109 break; |
| 117 } | 110 } |
| 118 if (ptr) | 111 if (ptr) |
| 119 return ptr; | 112 return ptr; |
| 120 | 113 |
| 121 if (!new_mode || !call_new_handler(true)) | 114 if (!new_mode || !call_new_handler(true)) |
| 122 break; | 115 break; |
| 123 } | 116 } |
| 124 return ptr; | 117 return ptr; |
| 125 } | 118 } |
| 126 | 119 |
| 127 void free(void* p) __THROW { | 120 void free(void* p) { |
| 128 switch (allocator) { | 121 switch (allocator) { |
| 129 case WINHEAP: | 122 case WINHEAP: |
| 130 case WINLFH: | 123 case WINLFH: |
| 131 win_heap_free(p); | 124 win_heap_free(p); |
| 132 return; | 125 return; |
| 133 case TCMALLOC: | 126 case TCMALLOC: |
| 134 do_free(p); | 127 do_free(p); |
| 135 return; | 128 return; |
| 136 } | 129 } |
| 137 } | 130 } |
| 138 | 131 |
| 139 void* realloc(void* ptr, size_t size) __THROW { | 132 void* realloc(void* ptr, size_t size) { |
| 140 // Webkit is brittle for allocators that return NULL for malloc(0). The | 133 // Webkit is brittle for allocators that return NULL for malloc(0). The |
| 141 // realloc(0, 0) code path does not guarantee a non-NULL return, so be sure | 134 // realloc(0, 0) code path does not guarantee a non-NULL return, so be sure |
| 142 // to call malloc for this case. | 135 // to call malloc for this case. |
| 143 if (!ptr) | 136 if (!ptr) |
| 144 return malloc(size); | 137 return malloc(size); |
| 145 | 138 |
| 146 void* new_ptr; | 139 void* new_ptr; |
| 147 for (;;) { | 140 for (;;) { |
| 148 switch (allocator) { | 141 switch (allocator) { |
| 149 case WINHEAP: | 142 case WINHEAP: |
| (...skipping 11 matching lines...) Expand all Loading... |
| 161 // NULL. | 154 // NULL. |
| 162 if (new_ptr || !size) | 155 if (new_ptr || !size) |
| 163 return new_ptr; | 156 return new_ptr; |
| 164 if (!new_mode || !call_new_handler(true)) | 157 if (!new_mode || !call_new_handler(true)) |
| 165 break; | 158 break; |
| 166 } | 159 } |
| 167 return new_ptr; | 160 return new_ptr; |
| 168 } | 161 } |
| 169 | 162 |
| 170 // TODO(mbelshe): Implement this for other allocators. | 163 // TODO(mbelshe): Implement this for other allocators. |
| 171 void malloc_stats(void) __THROW { | 164 void malloc_stats(void) { |
| 172 switch (allocator) { | 165 switch (allocator) { |
| 173 case WINHEAP: | 166 case WINHEAP: |
| 174 case WINLFH: | 167 case WINLFH: |
| 175 // No stats. | 168 // No stats. |
| 176 return; | 169 return; |
| 177 case TCMALLOC: | 170 case TCMALLOC: |
| 178 tc_malloc_stats(); | 171 tc_malloc_stats(); |
| 179 return; | 172 return; |
| 180 } | 173 } |
| 181 } | 174 } |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 void TCMallocDoFreeForTest(void* ptr) { | 369 void TCMallocDoFreeForTest(void* ptr) { |
| 377 do_free(ptr); | 370 do_free(ptr); |
| 378 } | 371 } |
| 379 | 372 |
| 380 size_t ExcludeSpaceForMarkForTest(size_t size) { | 373 size_t ExcludeSpaceForMarkForTest(size_t size) { |
| 381 return ExcludeSpaceForMark(size); | 374 return ExcludeSpaceForMark(size); |
| 382 } | 375 } |
| 383 | 376 |
| 384 } // namespace allocator. | 377 } // namespace allocator. |
| 385 } // namespace base. | 378 } // namespace base. |
| OLD | NEW |