blob: 8afb851023b26b9ce64376a93ad8119cc6745f43 [file] [log] [blame]
Austin Schuh745610d2015-09-06 18:19:50 -07001// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
2// Copyright (c) 2007, Google Inc.
3// All rights reserved.
Brian Silverman20350ac2021-11-17 18:19:55 -08004//
Austin Schuh745610d2015-09-06 18:19:50 -07005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
Brian Silverman20350ac2021-11-17 18:19:55 -08008//
Austin Schuh745610d2015-09-06 18:19:50 -07009// * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15// * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
Brian Silverman20350ac2021-11-17 18:19:55 -080018//
Austin Schuh745610d2015-09-06 18:19:50 -070019// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30//
31// ---
32// Author: Mike Belshe
Brian Silverman20350ac2021-11-17 18:19:55 -080033//
Austin Schuh745610d2015-09-06 18:19:50 -070034// To link tcmalloc into a EXE or DLL statically without using the patching
35// facility, we can take a stock libcmt and remove all the allocator functions.
36// When we relink the EXE/DLL with the modified libcmt and tcmalloc, a few
37// functions are missing. This file contains the additional overrides which
38// are required in the VS2005 libcmt in order to link the modified libcmt.
39//
40// See also
41// http://groups.google.com/group/google-perftools/browse_thread/thread/41cd3710af85e57b
42
43#include <config.h>
44
45#ifndef _WIN32
46# error You should only be including this file in a windows environment!
47#endif
48
49#ifndef WIN32_OVERRIDE_ALLOCATORS
50# error This file is intended for use when overriding allocators
51#endif
52
53#include "tcmalloc.cc"
54
Brian Silverman20350ac2021-11-17 18:19:55 -080055extern "C" {
56
57void* _malloc_base(size_t size) {
58 return malloc(size);
Austin Schuh745610d2015-09-06 18:19:50 -070059}
60
Brian Silverman20350ac2021-11-17 18:19:55 -080061void _free_base(void* p) {
62 free(p);
63}
64
65void* _calloc_base(size_t n, size_t size) {
Austin Schuh745610d2015-09-06 18:19:50 -070066 return calloc(n, size);
67}
68
Brian Silverman20350ac2021-11-17 18:19:55 -080069void* _recalloc(void* old_ptr, size_t n, size_t size) {
70 // Ensure that (n * size) does not overflow
71 if (!(n == 0 || (std::numeric_limits<size_t>::max)() / n >= size)) {
72 errno = ENOMEM;
73 return NULL;
74 }
75
76 const size_t old_size = tc_malloc_size(old_ptr);
77 const size_t new_size = n * size;
78
79 void* new_ptr = realloc(old_ptr, new_size);
80
81 // If the reallocation succeeded and the new block is larger, zero-fill the
82 // new bytes:
83 if (new_ptr != NULL && new_size > old_size) {
84 memset(static_cast<char*>(new_ptr) + old_size, 0, tc_nallocx(new_size, 0) - old_size);
85 }
86
87 return new_ptr;
88}
89
90void* _calloc_impl(size_t n, size_t size) {
91 return calloc(n, size);
92}
93
94size_t _msize(void* p) {
Austin Schuh745610d2015-09-06 18:19:50 -070095 return MallocExtension::instance()->GetAllocatedSize(p);
96}
97
Brian Silverman20350ac2021-11-17 18:19:55 -080098HANDLE __acrt_heap = nullptr;
99
100bool __acrt_initialize_heap() {
101 new TCMallocGuard();
102 return true;
103}
104
105bool __acrt_uninitialize_heap(bool) {
106 return true;
107}
108
109intptr_t _get_heap_handle() {
Austin Schuh745610d2015-09-06 18:19:50 -0700110 return 0;
111}
112
Brian Silverman20350ac2021-11-17 18:19:55 -0800113HANDLE __acrt_getheap() {
114 return __acrt_heap;
115}
116
Austin Schuh745610d2015-09-06 18:19:50 -0700117// The CRT heap initialization stub.
Brian Silverman20350ac2021-11-17 18:19:55 -0800118int _heap_init() {
Austin Schuh745610d2015-09-06 18:19:50 -0700119 // We intentionally leak this object. It lasts for the process
120 // lifetime. Trying to teardown at _heap_term() is so late that
121 // you can't do anything useful anyway.
122 new TCMallocGuard();
123 return 1;
124}
125
126// The CRT heap cleanup stub.
Brian Silverman20350ac2021-11-17 18:19:55 -0800127void _heap_term() {
Austin Schuh745610d2015-09-06 18:19:50 -0700128}
129
Brian Silverman20350ac2021-11-17 18:19:55 -0800130// We set this to 1 because part of the CRT uses a check of _crtheap != 0
131// to test whether the CRT has been initialized. Once we've ripped out
132// the allocators from libcmt, we need to provide this definition so that
133// the rest of the CRT is still usable.
134void* _crtheap = reinterpret_cast<void*>(1);
135
136int _set_new_mode(int flag) {
Austin Schuh745610d2015-09-06 18:19:50 -0700137 return tc_set_new_mode(flag);
138}
139
Brian Silverman20350ac2021-11-17 18:19:55 -0800140int _query_new_mode() {
141 return tc_query_new_mode();
142}
143
144} // extern "C"
145
Austin Schuh745610d2015-09-06 18:19:50 -0700146#ifndef NDEBUG
147#undef malloc
148#undef free
149#undef calloc
150int _CrtDbgReport(int, const char*, int, const char*, const char*, ...) {
151 return 0;
152}
153
154int _CrtDbgReportW(int, const wchar_t*, int, const wchar_t*, const wchar_t*, ...) {
155 return 0;
156}
157
158int _CrtSetReportMode(int, int) {
159 return 0;
160}
161
162extern "C" void* _malloc_dbg(size_t size, int , const char*, int) {
163 return malloc(size);
164}
165
166extern "C" void _free_dbg(void* ptr, int) {
167 free(ptr);
168}
169
170extern "C" void* _calloc_dbg(size_t n, size_t size, int, const char*, int) {
171 return calloc(n, size);
172}
173#endif // NDEBUG