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 <malloc.h> | 5 #include <malloc.h> |
6 #include <new.h> | 6 #include <new.h> |
7 #include <windows.h> | 7 #include <windows.h> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 | 10 |
11 // This shim make it possible to perform additional checks on allocations | 11 // This shim make it possible to perform additional checks on allocations |
12 // before passing them to the Heap functions. | 12 // before passing them to the Heap functions. |
13 | 13 |
14 // new_mode behaves similarly to MSVC's _set_new_mode. | 14 // Heap functions are stripped from libcmt.lib using the prep_libc.py |
15 // If flag is 0 (default), calls to malloc will behave normally. | 15 // for each object file stripped, we re-implement them here to allow us to |
16 // If flag is 1, calls to malloc will behave like calls to new, | 16 // perform additional checks: |
17 // and the std_new_handler will be invoked on failure. | 17 // 1. Enforcing the maximum size that can be allocated to 2Gb. |
18 // Can be set by calling _set_new_mode(). | 18 // 2. Calling new_handler if malloc fails. |
19 static int new_mode = 0; | 19 |
20 extern "C" { | |
21 // We set this to 1 because part of the CRT uses a check of _crtheap != 0 | |
22 // to test whether the CRT has been initialized. Once we've ripped out | |
23 // the allocators from libcmt, we need to provide this definition so that | |
24 // the rest of the CRT is still usable. | |
25 // heapinit.c | |
26 void* _crtheap = reinterpret_cast<void*>(1); | |
27 } | |
20 | 28 |
21 namespace { | 29 namespace { |
22 | 30 |
23 // This is a simple allocator based on the windows heap. | |
24 const size_t kWindowsPageSize = 4096; | 31 const size_t kWindowsPageSize = 4096; |
25 const size_t kMaxWindowsAllocation = INT_MAX - kWindowsPageSize; | 32 const size_t kMaxWindowsAllocation = INT_MAX - kWindowsPageSize; |
26 static HANDLE win_heap; | 33 HANDLE win_heap; |
cpu_(ooo_6.6-7.5)
2015/01/22 18:25:15
kill win_heap
| |
34 int new_mode = 0; | |
27 | 35 |
28 // VS2013 crt uses the process heap as its heap, so we do the same here. | 36 // VS2013 crt uses the process heap as its heap, so we do the same here. |
29 // See heapinit.c in VS CRT sources. | 37 // See heapinit.c in VS CRT sources. |
30 bool win_heap_init() { | 38 bool win_heap_init() { |
31 win_heap = GetProcessHeap(); | 39 win_heap = GetProcessHeap(); |
32 if (win_heap == NULL) | 40 if (win_heap == NULL) |
33 return false; | 41 return false; |
34 | 42 |
35 ULONG enable_lfh = 2; | 43 ULONG enable_lfh = 2; |
36 // NOTE: Setting LFH may fail. Vista already has it enabled. | 44 // NOTE: Setting LFH may fail. Vista already has it enabled. |
37 // And under the debugger, it won't use LFH. So we | 45 // And under the debugger, it won't use LFH. So we |
38 // ignore any errors. | 46 // ignore any errors. |
39 HeapSetInformation(win_heap, HeapCompatibilityInformation, &enable_lfh, | 47 HeapSetInformation(win_heap, HeapCompatibilityInformation, &enable_lfh, |
40 sizeof(enable_lfh)); | 48 sizeof(enable_lfh)); |
41 | 49 |
50 // This allows us to offload most of the Heap functionality to WinHeap. | |
51 _crtheap = win_heap; | |
42 return true; | 52 return true; |
43 } | 53 } |
44 | 54 |
45 void* win_heap_malloc(size_t size) { | 55 void* win_heap_malloc(size_t size) { |
46 if (size < kMaxWindowsAllocation) | 56 if (size < kMaxWindowsAllocation) |
47 return HeapAlloc(win_heap, 0, size); | 57 return HeapAlloc(win_heap, 0, size); |
48 return NULL; | 58 return NULL; |
49 } | 59 } |
50 | 60 |
51 void win_heap_free(void* size) { | 61 void win_heap_free(void* size) { |
52 HeapFree(win_heap, 0, size); | 62 HeapFree(win_heap, 0, size); |
53 } | 63 } |
54 | 64 |
55 void* win_heap_realloc(void* ptr, size_t size) { | 65 void* win_heap_realloc(void* ptr, size_t size) { |
56 if (!ptr) | 66 if (!ptr) |
57 return win_heap_malloc(size); | 67 return win_heap_malloc(size); |
58 if (!size) { | 68 if (!size) { |
59 win_heap_free(ptr); | 69 win_heap_free(ptr); |
60 return NULL; | 70 return NULL; |
61 } | 71 } |
62 if (size < kMaxWindowsAllocation) | 72 if (size < kMaxWindowsAllocation) |
63 return HeapReAlloc(win_heap, 0, ptr, size); | 73 return HeapReAlloc(win_heap, 0, ptr, size); |
64 return NULL; | 74 return NULL; |
65 } | 75 } |
66 | 76 |
67 size_t win_heap_msize(void* ptr) { | |
68 return HeapSize(win_heap, 0, ptr); | |
69 } | |
70 | |
71 void* win_heap_memalign(size_t alignment, size_t size) { | |
72 // Reserve enough space to ensure we can align and set aligned_ptr[-1] to the | |
73 // original allocation for use with win_heap_memalign_free() later. | |
74 size_t allocation_size = size + (alignment - 1) + sizeof(void*); | |
75 | |
76 // Check for overflow. Alignment and size are checked in allocator_shim. | |
77 if (size >= allocation_size || alignment >= allocation_size) { | |
78 return NULL; | |
79 } | |
80 | |
81 // Since we're directly calling the allocator function, before OOM handling, | |
82 // we need to NULL check to ensure the allocation succeeded. | |
83 void* ptr = win_heap_malloc(allocation_size); | |
84 if (!ptr) | |
85 return ptr; | |
86 | |
87 char* aligned_ptr = static_cast<char*>(ptr) + sizeof(void*); | |
88 aligned_ptr += | |
89 alignment - reinterpret_cast<uintptr_t>(aligned_ptr) & (alignment - 1); | |
90 | |
91 reinterpret_cast<void**>(aligned_ptr)[-1] = ptr; | |
92 return aligned_ptr; | |
93 } | |
94 | |
95 void win_heap_memalign_free(void* ptr) { | |
96 if (ptr) | |
97 win_heap_free(static_cast<void**>(ptr)[-1]); | |
98 } | |
99 | |
100 void win_heap_term() { | 77 void win_heap_term() { |
101 win_heap = NULL; | 78 win_heap = NULL; |
102 } | 79 } |
103 | 80 |
104 } // namespace | |
105 | |
106 // Call the new handler, if one has been set. | 81 // Call the new handler, if one has been set. |
107 // Returns true on successfully calling the handler, false otherwise. | 82 // Returns true on successfully calling the handler, false otherwise. |
108 inline bool call_new_handler(bool nothrow, size_t size) { | 83 inline bool call_new_handler(bool nothrow, size_t size) { |
109 // Get the current new handler. | 84 // Get the current new handler. |
110 _PNH nh = _query_new_handler(); | 85 _PNH nh = _query_new_handler(); |
111 #if defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS | 86 #if defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS |
112 if (!nh) | 87 if (!nh) |
113 return false; | 88 return false; |
114 // Since exceptions are disabled, we don't really know if new_handler | 89 // Since exceptions are disabled, we don't really know if new_handler |
115 // failed. Assume it will abort if it fails. | 90 // failed. Assume it will abort if it fails. |
116 return nh(size); | 91 return nh(size); |
117 #else | 92 #else |
118 #error "Exceptions in allocator shim are not supported!" | 93 #error "Exceptions in allocator shim are not supported!" |
119 #endif // defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS | 94 #endif // defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS |
120 return false; | 95 return false; |
121 } | 96 } |
122 | 97 |
98 // Implement a C++ style allocation, which always calls the new_handler | |
99 // on failure. | |
100 inline void* generic_cpp_alloc(size_t size, bool nothrow) { | |
101 void* ptr; | |
102 for (;;) { | |
103 ptr = malloc(size); | |
104 if (ptr) | |
105 return ptr; | |
106 if (!call_new_handler(nothrow, size)) | |
107 break; | |
108 } | |
109 return ptr; | |
110 } | |
111 | |
112 } // namespace | |
113 | |
114 // new.cpp | |
115 void* operator new(size_t size) { | |
116 return generic_cpp_alloc(size, false); | |
117 } | |
118 | |
119 // delete.cpp | |
120 void operator delete(void* p) throw() { | |
121 free(p); | |
122 } | |
123 | |
124 // new2.cpp | |
125 void* operator new[](size_t size) { | |
126 return generic_cpp_alloc(size, false); | |
127 } | |
128 | |
129 // delete2.cpp | |
130 void operator delete[](void* p) throw() { | |
131 free(p); | |
132 } | |
133 | |
134 // newopnt.cpp | |
135 void* operator new(size_t size, const std::nothrow_t& nt) { | |
136 return generic_cpp_alloc(size, true); | |
137 } | |
138 | |
139 // newaopnt.cpp | |
140 void* operator new[](size_t size, const std::nothrow_t& nt) { | |
141 return generic_cpp_alloc(size, true); | |
142 } | |
143 | |
144 // This function behaves similarly to MSVC's _set_new_mode. | |
145 // If flag is 0 (default), calls to malloc will behave normally. | |
146 // If flag is 1, calls to malloc will behave like calls to new, | |
147 // and the std_new_handler will be invoked on failure. | |
148 // Returns the previous mode. | |
149 // new_mode.cpp | |
150 int _set_new_mode(int flag) throw() { | |
151 int old_mode = new_mode; | |
152 new_mode = flag; | |
153 return old_mode; | |
154 } | |
155 | |
156 // new_mode.cpp | |
157 int _query_new_mode() { | |
158 return new_mode; | |
159 } | |
160 | |
123 extern "C" { | 161 extern "C" { |
124 | 162 // malloc.c |
125 void* malloc(size_t size) { | 163 void* malloc(size_t size) { |
126 void* ptr; | 164 void* ptr; |
127 for (;;) { | 165 for (;;) { |
128 ptr = win_heap_malloc(size); | 166 ptr = win_heap_malloc(size); |
129 if (ptr) | 167 if (ptr) |
130 return ptr; | 168 return ptr; |
131 | 169 |
132 if (!new_mode || !call_new_handler(true, size)) | 170 if (!new_mode || !call_new_handler(true, size)) |
133 break; | 171 break; |
134 } | 172 } |
135 return ptr; | 173 return ptr; |
136 } | 174 } |
137 | 175 |
176 // free.c | |
138 void free(void* p) { | 177 void free(void* p) { |
139 win_heap_free(p); | 178 win_heap_free(p); |
140 return; | 179 return; |
141 } | 180 } |
142 | 181 |
182 // realloc.c | |
143 void* realloc(void* ptr, size_t size) { | 183 void* realloc(void* ptr, size_t size) { |
144 // Webkit is brittle for allocators that return NULL for malloc(0). The | 184 // Webkit is brittle for allocators that return NULL for malloc(0). The |
145 // realloc(0, 0) code path does not guarantee a non-NULL return, so be sure | 185 // realloc(0, 0) code path does not guarantee a non-NULL return, so be sure |
146 // to call malloc for this case. | 186 // to call malloc for this case. |
147 if (!ptr) | 187 if (!ptr) |
148 return malloc(size); | 188 return malloc(size); |
149 | 189 |
150 void* new_ptr; | 190 void* new_ptr; |
151 for (;;) { | 191 for (;;) { |
152 new_ptr = win_heap_realloc(ptr, size); | 192 new_ptr = win_heap_realloc(ptr, size); |
153 | 193 |
154 // Subtle warning: NULL return does not alwas indicate out-of-memory. If | 194 // Subtle warning: NULL return does not alwas indicate out-of-memory. If |
155 // the requested new size is zero, realloc should free the ptr and return | 195 // the requested new size is zero, realloc should free the ptr and return |
156 // NULL. | 196 // NULL. |
157 if (new_ptr || !size) | 197 if (new_ptr || !size) |
158 return new_ptr; | 198 return new_ptr; |
159 if (!new_mode || !call_new_handler(true, size)) | 199 if (!new_mode || !call_new_handler(true, size)) |
160 break; | 200 break; |
161 } | 201 } |
162 return new_ptr; | 202 return new_ptr; |
163 } | 203 } |
164 | 204 |
165 | 205 // heapinit.c |
166 size_t _msize(void* p) { | |
167 return win_heap_msize(p); | |
168 } | |
169 | |
170 intptr_t _get_heap_handle() { | 206 intptr_t _get_heap_handle() { |
171 return reinterpret_cast<intptr_t>(win_heap); | 207 return reinterpret_cast<intptr_t>(win_heap); |
172 } | 208 } |
173 | 209 |
174 // The CRT heap initialization stub. | 210 // heapinit.c |
175 int _heap_init() { | 211 int _heap_init() { |
176 return win_heap_init() ? 1 : 0; | 212 return win_heap_init() ? 1 : 0; |
177 } | 213 } |
178 | 214 |
179 // The CRT heap cleanup stub. | 215 // heapinit.c |
180 void _heap_term() { | 216 void _heap_term() { |
181 win_heap_term(); | 217 win_heap_term(); |
182 } | 218 } |
183 | 219 |
184 // We set this to 1 because part of the CRT uses a check of _crtheap != 0 | 220 // calloc.c |
185 // to test whether the CRT has been initialized. Once we've ripped out | 221 void* calloc(size_t n, size_t elem_size) { |
186 // the allocators from libcmt, we need to provide this definition so that | 222 // Overflow check |
cpu_(ooo_6.6-7.5)
2015/01/22 18:25:15
period
| |
187 // the rest of the CRT is still usable. | 223 const size_t size = n * elem_size; |
188 void* _crtheap = reinterpret_cast<void*>(1); | 224 if (elem_size != 0 && size / elem_size != n) |
189 | |
190 // Provide support for aligned memory through Windows only _aligned_malloc(). | |
191 void* _aligned_malloc(size_t size, size_t alignment) { | |
192 // _aligned_malloc guarantees parameter validation, so do so here. These | |
193 // checks are somewhat stricter than _aligned_malloc() since we're effectively | |
194 // using memalign() under the hood. | |
195 if (size == 0U || (alignment & (alignment - 1)) != 0U || | |
196 (alignment % sizeof(void*)) != 0U) | |
197 return NULL; | 225 return NULL; |
198 | 226 |
199 void* ptr; | 227 void* result = malloc(size); |
200 for (;;) { | 228 if (result != NULL) { |
201 ptr = win_heap_memalign(alignment, size); | 229 memset(result, 0, size); |
202 | |
203 if (ptr) { | |
204 return ptr; | |
205 } | |
206 | |
207 if (!new_mode || !call_new_handler(true, size)) | |
208 break; | |
209 } | 230 } |
210 return ptr; | 231 return result; |
211 } | 232 } |
212 | 233 |
213 void _aligned_free(void* p) { | 234 // recalloc.c |
214 // Pointers allocated with win_heap_memalign() MUST be freed via | 235 void* _recalloc(void* p, size_t n, size_t elem_size) { |
215 // win_heap_memalign_free() since the aligned pointer is not the real one. | 236 if (!p) |
216 win_heap_memalign_free(p); | 237 return calloc(n, elem_size); |
238 | |
239 // This API is a bit odd. | |
240 // Note: recalloc only guarantees zeroed memory when p is NULL. | |
241 // Generally, calls to malloc() have padding. So a request | |
242 // to malloc N bytes actually malloc's N+x bytes. Later, if | |
243 // that buffer is passed to recalloc, we don't know what N | |
244 // was anymore. We only know what N+x is. As such, there is | |
245 // no way to know what to zero out. | |
246 const size_t size = n * elem_size; | |
247 if (elem_size != 0 && size / elem_size != n) | |
248 return NULL; | |
249 return realloc(p, size); | |
217 } | 250 } |
218 | 251 |
219 #include "generic_allocators.cc" | 252 // calloc_impl.c |
253 void* _calloc_impl(size_t n, size_t size) { | |
254 return calloc(n, size); | |
255 } | |
220 | 256 |
221 } // extern C | 257 } // extern C |
OLD | NEW |