Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(266)

Side by Side Diff: base/allocator/generic_allocators.cc

Issue 885443002: Roll Chrome into Mojo. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Rebase to ToT mojo Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/allocator/allocator_shim_win.cc ('k') | base/allocator/prep_libc.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 // When possible, we implement allocator functions on top of the basic
6 // low-level functions malloc() and free(). This way, including a new
7 // allocator is as simple as providing just a small interface.
8 //
9 // As such, this file should not contain any allocator-specific code.
10
11 // Implement a C++ style allocation, which always calls the new_handler
12 // on failure.
13 inline void* generic_cpp_alloc(size_t size, bool nothrow) {
14 void* ptr;
15 for (;;) {
16 ptr = malloc(size);
17 if (ptr)
18 return ptr;
19 if (!call_new_handler(nothrow, size))
20 break;
21 }
22 return ptr;
23 }
24
25 extern "C++" {
26
27 void* operator new(size_t size) {
28 return generic_cpp_alloc(size, false);
29 }
30
31 void operator delete(void* p) throw() {
32 free(p);
33 }
34
35 void* operator new[](size_t size) {
36 return generic_cpp_alloc(size, false);
37 }
38
39 void operator delete[](void* p) throw() {
40 free(p);
41 }
42
43 void* operator new(size_t size, const std::nothrow_t& nt) {
44 return generic_cpp_alloc(size, true);
45 }
46
47 void operator delete(void* p, const std::nothrow_t& nt) throw() {
48 free(p);
49 }
50
51 void* operator new[](size_t size, const std::nothrow_t& nt) {
52 return generic_cpp_alloc(size, true);
53 }
54
55 void operator delete[](void* p, const std::nothrow_t& nt) throw() {
56 free(p);
57 }
58
59 // This function behaves similarly to MSVC's _set_new_mode.
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,
62 // and the std_new_handler will be invoked on failure.
63 // Returns the previous mode.
64 int _set_new_mode(int flag) throw() {
65 int old_mode = new_mode;
66 new_mode = flag;
67 return old_mode;
68 }
69
70 } // extern "C++"
71
72 extern "C" {
73
74 void* calloc(size_t n, size_t elem_size) {
75 // Overflow check
76 const size_t size = n * elem_size;
77 if (elem_size != 0 && size / elem_size != n) return NULL;
78
79 void* result = malloc(size);
80 if (result != NULL) {
81 memset(result, 0, size);
82 }
83 return result;
84 }
85
86 #ifdef WIN32
87
88 void* _recalloc(void* p, size_t n, size_t elem_size) {
89 if (!p)
90 return calloc(n, elem_size);
91
92 // This API is a bit odd.
93 // Note: recalloc only guarantees zeroed memory when p is NULL.
94 // Generally, calls to malloc() have padding. So a request
95 // to malloc N bytes actually malloc's N+x bytes. Later, if
96 // that buffer is passed to recalloc, we don't know what N
97 // was anymore. We only know what N+x is. As such, there is
98 // no way to know what to zero out.
99 const size_t size = n * elem_size;
100 if (elem_size != 0 && size / elem_size != n) return NULL;
101 return realloc(p, size);
102 }
103
104 void* _calloc_impl(size_t n, size_t size) {
105 return calloc(n, size);
106 }
107
108 #ifndef NDEBUG
109 #undef malloc
110 #undef free
111 #undef calloc
112
113 static int error_handler(int reportType) {
114 switch (reportType) {
115 case 0: // _CRT_WARN
116 __debugbreak();
117 return 0;
118
119 case 1: // _CRT_ERROR
120 __debugbreak();
121 return 0;
122
123 case 2: // _CRT_ASSERT
124 __debugbreak();
125 return 0;
126 }
127 char* p = NULL;
128 *p = '\0';
129 return 0;
130 }
131
132 int _CrtDbgReport(int reportType,
133 const char*,
134 int, const char*,
135 const char*,
136 ...) {
137 return error_handler(reportType);
138 }
139
140 int _CrtDbgReportW(int reportType,
141 const wchar_t*,
142 int, const wchar_t*,
143 const wchar_t*,
144 ...) {
145 return error_handler(reportType);
146 }
147
148 int _CrtSetReportMode(int, int) {
149 return 0;
150 }
151
152 void* _malloc_dbg(size_t size, int , const char*, int) {
153 return malloc(size);
154 }
155
156 void* _realloc_dbg(void* ptr, size_t size, int, const char*, int) {
157 return realloc(ptr, size);
158 }
159
160 void _free_dbg(void* ptr, int) {
161 free(ptr);
162 }
163
164 void* _calloc_dbg(size_t n, size_t size, int, const char*, int) {
165 return calloc(n, size);
166 }
167 #endif // NDEBUG
168
169 #endif // WIN32
170
171 } // extern C
172
OLDNEW
« no previous file with comments | « base/allocator/allocator_shim_win.cc ('k') | base/allocator/prep_libc.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698