OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 // When possible, we implement allocator functions on top of the basic | 5 // When possible, we implement allocator functions on top of the basic |
6 // low-level functions malloc() and free(). This way, including a new | 6 // low-level functions malloc() and free(). This way, including a new |
7 // allocator is as simple as providing just a small interface. | 7 // allocator is as simple as providing just a small interface. |
8 // | 8 // |
9 // As such, this file should not contain any allocator-specific code. | 9 // As such, this file should not contain any allocator-specific code. |
10 | 10 |
11 // Implement a C++ style allocation, which always calls the new_handler | 11 // Implement a C++ style allocation, which always calls the new_handler |
12 // on failure. | 12 // on failure. |
13 inline void* generic_cpp_alloc(size_t size, bool nothrow) { | 13 inline void* generic_cpp_alloc(size_t size, bool nothrow) { |
14 void* ptr; | 14 void* ptr; |
15 for (;;) { | 15 for (;;) { |
16 ptr = malloc(size); | 16 ptr = malloc(size); |
17 if (ptr) | 17 if (ptr) |
18 return ptr; | 18 return ptr; |
19 if (!call_new_handler(nothrow)) | 19 if (!call_new_handler(nothrow, size)) |
20 break; | 20 break; |
21 } | 21 } |
22 return ptr; | 22 return ptr; |
23 } | 23 } |
24 | 24 |
25 extern "C++" { | 25 extern "C++" { |
26 | 26 |
27 void* operator new(size_t size) { | 27 void* operator new(size_t size) { |
28 return generic_cpp_alloc(size, false); | 28 return generic_cpp_alloc(size, false); |
29 } | 29 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 const size_t size = n * elem_size; | 76 const size_t size = n * elem_size; |
77 if (elem_size != 0 && size / elem_size != n) return NULL; | 77 if (elem_size != 0 && size / elem_size != n) return NULL; |
78 | 78 |
79 void* result = malloc(size); | 79 void* result = malloc(size); |
80 if (result != NULL) { | 80 if (result != NULL) { |
81 memset(result, 0, size); | 81 memset(result, 0, size); |
82 } | 82 } |
83 return result; | 83 return result; |
84 } | 84 } |
85 | 85 |
86 void cfree(void* p) __THROW { | 86 void cfree(void* p) { |
87 free(p); | 87 free(p); |
88 } | 88 } |
89 | 89 |
90 #ifdef WIN32 | 90 #ifdef WIN32 |
91 | 91 |
92 void* _recalloc(void* p, size_t n, size_t elem_size) { | 92 void* _recalloc(void* p, size_t n, size_t elem_size) { |
93 if (!p) | 93 if (!p) |
94 return calloc(n, elem_size); | 94 return calloc(n, elem_size); |
95 | 95 |
96 // This API is a bit odd. | 96 // This API is a bit odd. |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 | 167 |
168 void* _calloc_dbg(size_t n, size_t size, int, const char*, int) { | 168 void* _calloc_dbg(size_t n, size_t size, int, const char*, int) { |
169 return calloc(n, size); | 169 return calloc(n, size); |
170 } | 170 } |
171 #endif // NDEBUG | 171 #endif // NDEBUG |
172 | 172 |
173 #endif // WIN32 | 173 #endif // WIN32 |
174 | 174 |
175 } // extern C | 175 } // extern C |
176 | 176 |
OLD | NEW |