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 30 matching lines...) Expand all Loading... |
41 } | 41 } |
42 | 42 |
43 void* operator new(size_t size, const std::nothrow_t& nt) __THROW { | 43 void* operator new(size_t size, const std::nothrow_t& nt) __THROW { |
44 return generic_cpp_alloc(size, true); | 44 return generic_cpp_alloc(size, true); |
45 } | 45 } |
46 | 46 |
47 void* operator new[](size_t size, const std::nothrow_t& nt) __THROW { | 47 void* operator new[](size_t size, const std::nothrow_t& nt) __THROW { |
48 return generic_cpp_alloc(size, true); | 48 return generic_cpp_alloc(size, true); |
49 } | 49 } |
50 | 50 |
51 } // extern "C++" | |
52 | |
53 extern "C" { | |
54 | |
55 // This function behaves similarly to MSVC's _set_new_mode. | 51 // This function behaves similarly to MSVC's _set_new_mode. |
56 // If flag is 0 (default), calls to malloc will behave normally. | 52 // If flag is 0 (default), calls to malloc will behave normally. |
57 // If flag is 1, calls to malloc will behave like calls to new, | 53 // If flag is 1, calls to malloc will behave like calls to new, |
58 // and the std_new_handler will be invoked on failure. | 54 // and the std_new_handler will be invoked on failure. |
59 // Returns the previous mode. | 55 // Returns the previous mode. |
60 int _set_new_mode(int flag) __THROW { | 56 int _set_new_mode(int flag) __THROW { |
61 int old_mode = new_mode; | 57 int old_mode = new_mode; |
62 new_mode = flag; | 58 new_mode = flag; |
63 return old_mode; | 59 return old_mode; |
64 } | 60 } |
65 | 61 |
| 62 } // extern "C++" |
| 63 |
| 64 extern "C" { |
| 65 |
66 void* calloc(size_t n, size_t elem_size) __THROW { | 66 void* calloc(size_t n, size_t elem_size) __THROW { |
67 // Overflow check | 67 // Overflow check |
68 const size_t size = n * elem_size; | 68 const size_t size = n * elem_size; |
69 if (elem_size != 0 && size / elem_size != n) return NULL; | 69 if (elem_size != 0 && size / elem_size != n) return NULL; |
70 | 70 |
71 void* result = malloc(size); | 71 void* result = malloc(size); |
72 if (result != NULL) { | 72 if (result != NULL) { |
73 memset(result, 0, size); | 73 memset(result, 0, size); |
74 } | 74 } |
75 return result; | 75 return result; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 | 128 |
129 void* _calloc_dbg(size_t n, size_t size, int, const char*, int) { | 129 void* _calloc_dbg(size_t n, size_t size, int, const char*, int) { |
130 return calloc(n, size); | 130 return calloc(n, size); |
131 } | 131 } |
132 #endif // NDEBUG | 132 #endif // NDEBUG |
133 | 133 |
134 #endif // WIN32 | 134 #endif // WIN32 |
135 | 135 |
136 } // extern C | 136 } // extern C |
137 | 137 |
OLD | NEW |