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/allocator_shim_win.cc

Issue 2143693003: Start refactoring Windows allocator shim in prep for hooking in generic allocator shim on Windows. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add back necessary includes. Created 4 years, 5 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
OLDNEW
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 // The allocator shim is only enabled in Release Static builds. 5 // The allocator shim is only enabled in Release Static builds.
6 // This #if is needed as gyp can't have different compile 6 // This #if is needed as gyp can't have different compile
7 // targets between Debug and Release. 7 // targets between Debug and Release.
8 // TODO(wfh): Remove this once gyp is dead. 8 // TODO(wfh): Remove this once gyp is dead.
9 #if defined(ALLOCATOR_SHIM) 9 #if defined(ALLOCATOR_SHIM)
10 10
11 #include <limits.h> 11 #include <limits.h>
12 #include <malloc.h> 12 #include <malloc.h>
13 #include <new.h> 13 #include <new.h>
14 #include <windows.h> 14 #include <windows.h>
15 #include <stddef.h> 15 #include <stddef.h>
16 16
17 #include "allocator_shim_win.h" 17 #include "base/allocator/allocator_shim_win.h"
Primiano Tucci (use gerrit) 2016/07/13 17:42:49 maybe add a TODO saying that this file will be del
Sigurður Ásgeirsson 2016/07/13 18:47:45 Done.
18 #include "base/allocator/winheap_stubs_win.h"
18 19
19 // This shim make it possible to perform additional checks on allocations 20 // This shim make it possible to perform additional checks on allocations
20 // before passing them to the Heap functions. 21 // before passing them to the Heap functions.
21 22
22 // Override heap functions to perform additional checks: 23 // Override heap functions to perform additional checks:
23 // 1. Enforcing the maximum size that can be allocated to 2Gb. 24 // 1. Enforcing the maximum size that can be allocated to 2Gb.
24 // 2. Calling new_handler if malloc fails 25 // 2. Calling new_handler if malloc fails
25 26
26 // See definitions of original functions in ucrt\corecrt_malloc.h in SDK 27 // See definitions of original functions in ucrt\corecrt_malloc.h in SDK
27 // include directory. 28 // include directory.
28 29
29 namespace base {
30 namespace allocator {
31 bool g_is_win_shim_layer_initialized = false;
32 } // namespace allocator
33 } // namespace base
34
35 namespace { 30 namespace {
36 31
37 const size_t kWindowsPageSize = 4096;
38 const size_t kMaxWindowsAllocation = INT_MAX - kWindowsPageSize;
39 int new_mode = 0; 32 int new_mode = 0;
40 33
41 inline HANDLE get_heap_handle() {
42 return reinterpret_cast<HANDLE>(_get_heap_handle());
43 }
44
45 void* win_heap_malloc(size_t size) {
46 if (size < kMaxWindowsAllocation)
47 return HeapAlloc(get_heap_handle(), 0, size);
48 return nullptr;
49 }
50
51 void win_heap_free(void* size) {
52 HeapFree(get_heap_handle(), 0, size);
53 }
54
55 void* win_heap_realloc(void* ptr, size_t size) {
56 if (!ptr)
57 return win_heap_malloc(size);
58 if (!size) {
59 win_heap_free(ptr);
60 return nullptr;
61 }
62 if (size < kMaxWindowsAllocation)
63 return HeapReAlloc(get_heap_handle(), 0, ptr, size);
64 return nullptr;
65 }
66
67 // Call the new handler, if one has been set.
68 // Returns true on successfully calling the handler, false otherwise.
69 inline bool call_new_handler(bool nothrow, size_t size) {
70 // Get the current new handler.
71 _PNH nh = _query_new_handler();
72 #if defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS
73 if (!nh)
74 return false;
75 // Since exceptions are disabled, we don't really know if new_handler
76 // failed. Assume it will abort if it fails.
77 return nh(size) ? true : false;
78 #else
79 #error "Exceptions in allocator shim are not supported!"
80 #endif // defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS
81 }
82
83 } // namespace 34 } // namespace
84 35
85 extern "C" { 36 extern "C" {
86 37
87 // Symbol to allow weak linkage to win_heap_malloc from memory_win.cc. 38 // Symbol to allow weak linkage to WinHeapMalloc from allocator_impl_win.cc.
88 void* (*malloc_unchecked)(size_t) = &win_heap_malloc; 39 void* (*malloc_unchecked)(size_t) = &base::allocator::WinHeapMalloc;
89 40
90 // This function behaves similarly to MSVC's _set_new_mode. 41 // This function behaves similarly to MSVC's _set_new_mode.
91 // If flag is 0 (default), calls to malloc will behave normally. 42 // If flag is 0 (default), calls to malloc will behave normally.
92 // If flag is 1, calls to malloc will behave like calls to new, 43 // If flag is 1, calls to malloc will behave like calls to new,
93 // and the std_new_handler will be invoked on failure. 44 // and the std_new_handler will be invoked on failure.
94 // Returns the previous mode. 45 // Returns the previous mode.
95 // 46 //
96 // Replaces _set_new_mode in ucrt\heap\new_mode.cpp 47 // Replaces _set_new_mode in ucrt\heap\new_mode.cpp
97 int _set_new_mode(int flag) { 48 int _set_new_mode(int flag) {
98 // The MS CRT calls this function early on in startup, so this serves as a low 49 // The MS CRT calls this function early on in startup, so this serves as a low
99 // overhead proof that the allocator shim is in place for this process. 50 // overhead proof that the allocator shim is in place for this process.
100 base::allocator::g_is_win_shim_layer_initialized = true; 51 base::allocator::g_is_win_shim_layer_initialized = true;
101 int old_mode = new_mode; 52 int old_mode = new_mode;
102 new_mode = flag; 53 new_mode = flag;
54
103 return old_mode; 55 return old_mode;
104 } 56 }
105 57
106 // Replaces _query_new_mode in ucrt\heap\new_mode.cpp 58 // Replaces _query_new_mode in ucrt\heap\new_mode.cpp
107 int _query_new_mode() { 59 int _query_new_mode() {
108 return new_mode; 60 return new_mode;
109 } 61 }
110 62
111 // Replaces malloc in ucrt\heap\malloc.cpp 63 // Replaces malloc in ucrt\heap\malloc.cpp
112 __declspec(restrict) void* malloc(size_t size) { 64 __declspec(restrict) void* malloc(size_t size) {
113 void* ptr; 65 void* ptr;
114 for (;;) { 66 for (;;) {
115 ptr = win_heap_malloc(size); 67 ptr = base::allocator::WinHeapMalloc(size);
116 if (ptr) 68 if (ptr)
117 return ptr; 69 return ptr;
118 70
119 if (!new_mode || !call_new_handler(true, size)) 71 if (!new_mode || base::allocator::WinCallNewHandler(size))
120 break; 72 break;
121 } 73 }
122 return ptr; 74 return ptr;
123 } 75 }
124 76
125 // Replaces free in ucrt\heap\free.cpp 77 // Replaces free in ucrt\heap\free.cpp
126 void free(void* p) { 78 void free(void* p) {
127 win_heap_free(p); 79 base::allocator::WinHeapFree(p);
128 return; 80 return;
129 } 81 }
130 82
131 // Replaces realloc in ucrt\heap\realloc.cpp 83 // Replaces realloc in ucrt\heap\realloc.cpp
132 __declspec(restrict) void* realloc(void* ptr, size_t size) { 84 __declspec(restrict) void* realloc(void* ptr, size_t size) {
133 // Webkit is brittle for allocators that return NULL for malloc(0). The 85 // Webkit is brittle for allocators that return NULL for malloc(0). The
134 // realloc(0, 0) code path does not guarantee a non-NULL return, so be sure 86 // realloc(0, 0) code path does not guarantee a non-NULL return, so be sure
135 // to call malloc for this case. 87 // to call malloc for this case.
136 if (!ptr) 88 if (!ptr)
137 return malloc(size); 89 return malloc(size);
138 90
139 void* new_ptr; 91 void* new_ptr;
140 for (;;) { 92 for (;;) {
141 new_ptr = win_heap_realloc(ptr, size); 93 new_ptr = base::allocator::WinHeapRealloc(ptr, size);
142 94
143 // Subtle warning: NULL return does not alwas indicate out-of-memory. If 95 // Subtle warning: NULL return does not alwas indicate out-of-memory. If
144 // the requested new size is zero, realloc should free the ptr and return 96 // the requested new size is zero, realloc should free the ptr and return
145 // NULL. 97 // NULL.
146 if (new_ptr || !size) 98 if (new_ptr || !size)
147 return new_ptr; 99 return new_ptr;
148 if (!new_mode || !call_new_handler(true, size)) 100 if (!new_mode || !base::allocator::WinCallNewHandler(size))
149 break; 101 break;
150 } 102 }
151 return new_ptr; 103 return new_ptr;
152 } 104 }
153 105
154 // Replaces calloc in ucrt\heap\calloc.cpp 106 // Replaces calloc in ucrt\heap\calloc.cpp
155 __declspec(restrict) void* calloc(size_t n, size_t elem_size) { 107 __declspec(restrict) void* calloc(size_t n, size_t elem_size) {
156 // Overflow check. 108 // Overflow check.
157 const size_t size = n * elem_size; 109 const size_t size = n * elem_size;
158 if (elem_size != 0 && size / elem_size != n) 110 if (elem_size != 0 && size / elem_size != n)
159 return nullptr; 111 return nullptr;
160 112
161 void* result = malloc(size); 113 void* result = malloc(size);
162 if (result) { 114 if (result) {
163 memset(result, 0, size); 115 memset(result, 0, size);
164 } 116 }
165 return result; 117 return result;
166 } 118 }
167 119
168 } // extern C 120 } // extern C
169 121
170 #endif // defined(ALLOCATOR_SHIM) 122 #endif // defined(ALLOCATOR_SHIM)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698