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

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

Issue 2907563002: allocator/Windows: delete the old non-unified allocator-shim (Closed)
Patch Set: remove comment Created 3 years, 7 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.h ('k') | base/process/memory_unittest.cc » ('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) 2012 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 // The allocator shim is only enabled in Release Static builds.
6 // This #if is needed as gyp can't have different compile
7 // targets between Debug and Release.
8 // TODO(wfh): Remove this once gyp is dead.
9 // TODO(siggi): Remove this file once the generic shim sticks.
10 #if defined(ALLOCATOR_SHIM)
11
12 #include <limits.h>
13 #include <malloc.h>
14 #include <new.h>
15 #include <windows.h>
16 #include <stddef.h>
17
18 #include "base/allocator/allocator_shim_win.h"
19 #include "base/allocator/winheap_stubs_win.h"
20
21 // This shim make it possible to perform additional checks on allocations
22 // before passing them to the Heap functions.
23
24 // Override heap functions to perform additional checks:
25 // 1. Enforcing the maximum size that can be allocated to 2Gb.
26 // 2. Calling new_handler if malloc fails
27
28 // See definitions of original functions in ucrt\corecrt_malloc.h in SDK
29 // include directory.
30
31 namespace {
32
33 int new_mode = 0;
34
35 } // namespace
36
37 extern "C" {
38
39 // Symbol to allow weak linkage to WinHeapMalloc from allocator_impl_win.cc.
40 void* (*malloc_unchecked)(size_t) = &base::allocator::WinHeapMalloc;
41
42 // This function behaves similarly to MSVC's _set_new_mode.
43 // If flag is 0 (default), calls to malloc will behave normally.
44 // If flag is 1, calls to malloc will behave like calls to new,
45 // and the std_new_handler will be invoked on failure.
46 // Returns the previous mode.
47 //
48 // Replaces _set_new_mode in ucrt\heap\new_mode.cpp
49 int _set_new_mode(int flag) {
50 // The MS CRT calls this function early on in startup, so this serves as a low
51 // overhead proof that the allocator shim is in place for this process.
52 base::allocator::g_is_win_shim_layer_initialized = true;
53 int old_mode = new_mode;
54 new_mode = flag;
55
56 return old_mode;
57 }
58
59 // Replaces _query_new_mode in ucrt\heap\new_mode.cpp
60 int _query_new_mode() {
61 return new_mode;
62 }
63
64 // Replaces malloc in ucrt\heap\malloc.cpp
65 __declspec(restrict) void* malloc(size_t size) {
66 void* ptr;
67 for (;;) {
68 ptr = base::allocator::WinHeapMalloc(size);
69 if (ptr)
70 return ptr;
71
72 if (!new_mode || base::allocator::WinCallNewHandler(size))
73 break;
74 }
75 return ptr;
76 }
77
78 // Replaces free in ucrt\heap\free.cpp
79 void free(void* p) {
80 base::allocator::WinHeapFree(p);
81 return;
82 }
83
84 // Replaces realloc in ucrt\heap\realloc.cpp
85 __declspec(restrict) void* realloc(void* ptr, size_t size) {
86 // Webkit is brittle for allocators that return NULL for malloc(0). The
87 // realloc(0, 0) code path does not guarantee a non-NULL return, so be sure
88 // to call malloc for this case.
89 if (!ptr)
90 return malloc(size);
91
92 void* new_ptr;
93 for (;;) {
94 new_ptr = base::allocator::WinHeapRealloc(ptr, size);
95
96 // Subtle warning: NULL return does not alwas indicate out-of-memory. If
97 // the requested new size is zero, realloc should free the ptr and return
98 // NULL.
99 if (new_ptr || !size)
100 return new_ptr;
101 if (!new_mode || !base::allocator::WinCallNewHandler(size))
102 break;
103 }
104 return new_ptr;
105 }
106
107 // Replaces calloc in ucrt\heap\calloc.cpp
108 __declspec(restrict) void* calloc(size_t n, size_t elem_size) {
109 // Overflow check.
110 const size_t size = n * elem_size;
111 if (elem_size != 0 && size / elem_size != n)
112 return nullptr;
113
114 void* result = malloc(size);
115 if (result) {
116 memset(result, 0, size);
117 }
118 return result;
119 }
120
121 } // extern C
122
123 #endif // defined(ALLOCATOR_SHIM)
OLDNEW
« no previous file with comments | « base/allocator/allocator_shim_win.h ('k') | base/process/memory_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698