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 |
(...skipping 10 matching lines...) Expand all Loading... |
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 } |
30 | 30 |
31 void operator delete(void* p) { | 31 void operator delete(void* p) throw() { |
32 free(p); | 32 free(p); |
33 } | 33 } |
34 | 34 |
35 void* operator new[](size_t size) { | 35 void* operator new[](size_t size) { |
36 return generic_cpp_alloc(size, false); | 36 return generic_cpp_alloc(size, false); |
37 } | 37 } |
38 | 38 |
39 void operator delete[](void* p) { | 39 void operator delete[](void* p) throw() { |
40 free(p); | 40 free(p); |
41 } | 41 } |
42 | 42 |
43 void* operator new(size_t size, const std::nothrow_t& nt) { | 43 void* operator new(size_t size, const std::nothrow_t& nt) { |
44 return generic_cpp_alloc(size, true); | 44 return generic_cpp_alloc(size, true); |
45 } | 45 } |
46 | 46 |
47 void operator delete(void* p, const std::nothrow_t& nt) { | 47 void operator delete(void* p, const std::nothrow_t& nt) throw() { |
48 free(p); | 48 free(p); |
49 } | 49 } |
50 | 50 |
51 void* operator new[](size_t size, const std::nothrow_t& nt) { | 51 void* operator new[](size_t size, const std::nothrow_t& nt) { |
52 return generic_cpp_alloc(size, true); | 52 return generic_cpp_alloc(size, true); |
53 } | 53 } |
54 | 54 |
55 void operator delete[](void* p, const std::nothrow_t& nt) { | 55 void operator delete[](void* p, const std::nothrow_t& nt) throw() { |
56 free(p); | 56 free(p); |
57 } | 57 } |
58 | 58 |
59 // This function behaves similarly to MSVC's _set_new_mode. | 59 // This function behaves similarly to MSVC's _set_new_mode. |
60 // If flag is 0 (default), calls to malloc will behave normally. | 60 // If flag is 0 (default), calls to malloc will behave normally. |
61 // If flag is 1, calls to malloc will behave like calls to new, | 61 // If flag is 1, calls to malloc will behave like calls to new, |
62 // and the std_new_handler will be invoked on failure. | 62 // and the std_new_handler will be invoked on failure. |
63 // Returns the previous mode. | 63 // Returns the previous mode. |
64 int _set_new_mode(int flag) throw() { | 64 int _set_new_mode(int flag) throw() { |
65 int old_mode = new_mode; | 65 int old_mode = new_mode; |
(...skipping 10 matching lines...) Expand all 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) { | |
87 free(p); | |
88 } | |
89 | |
90 #ifdef WIN32 | 86 #ifdef WIN32 |
91 | 87 |
92 void* _recalloc(void* p, size_t n, size_t elem_size) { | 88 void* _recalloc(void* p, size_t n, size_t elem_size) { |
93 if (!p) | 89 if (!p) |
94 return calloc(n, elem_size); | 90 return calloc(n, elem_size); |
95 | 91 |
96 // This API is a bit odd. | 92 // This API is a bit odd. |
97 // Note: recalloc only guarantees zeroed memory when p is NULL. | 93 // Note: recalloc only guarantees zeroed memory when p is NULL. |
98 // Generally, calls to malloc() have padding. So a request | 94 // Generally, calls to malloc() have padding. So a request |
99 // to malloc N bytes actually malloc's N+x bytes. Later, if | 95 // to malloc N bytes actually malloc's N+x bytes. Later, if |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 | 163 |
168 void* _calloc_dbg(size_t n, size_t size, int, const char*, int) { | 164 void* _calloc_dbg(size_t n, size_t size, int, const char*, int) { |
169 return calloc(n, size); | 165 return calloc(n, size); |
170 } | 166 } |
171 #endif // NDEBUG | 167 #endif // NDEBUG |
172 | 168 |
173 #endif // WIN32 | 169 #endif // WIN32 |
174 | 170 |
175 } // extern C | 171 } // extern C |
176 | 172 |
OLD | NEW |