| 1 |
// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- |
| 2 |
// Copyright (c) 2007, Google Inc. |
| 3 |
// All rights reserved. |
| 4 |
// |
| 5 |
// Redistribution and use in source and binary forms, with or without |
| 6 |
// modification, are permitted provided that the following conditions are |
| 7 |
// met: |
| 8 |
// |
| 9 |
// * 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. |
| 18 |
// |
| 19 |
// 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 |
| 33 |
// |
| 34 |
// 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 |
|
| 55 |
extern "C" { |
| 56 |
|
| 57 |
void* _malloc_base(size_t size) { |
| 58 |
return malloc(size); |
| 59 |
} |
| 60 |
|
| 61 |
void _free_base(void* p) { |
| 62 |
free(p); |
| 63 |
} |
| 64 |
|
| 65 |
void* _calloc_base(size_t n, size_t size) { |
| 66 |
return calloc(n, size); |
| 67 |
} |
| 68 |
|
| 69 |
void* _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 |
|
| 90 |
void* _calloc_impl(size_t n, size_t size) { |
| 91 |
return calloc(n, size); |
| 92 |
} |
| 93 |
|
| 94 |
size_t _msize(void* p) { |
| 95 |
return MallocExtension::instance()->GetAllocatedSize(p); |
| 96 |
} |
| 97 |
|
| 98 |
HANDLE __acrt_heap = nullptr; |
| 99 |
|
| 100 |
bool __acrt_initialize_heap() { |
| 101 |
new TCMallocGuard(); |
| 102 |
return true; |
| 103 |
} |
| 104 |
|
| 105 |
bool __acrt_uninitialize_heap(bool) { |
| 106 |
return true; |
| 107 |
} |
| 108 |
|
| 109 |
intptr_t _get_heap_handle() { |
| 110 |
return 0; |
| 111 |
} |
| 112 |
|
| 113 |
HANDLE __acrt_getheap() { |
| 114 |
return __acrt_heap; |
| 115 |
} |
| 116 |
|
| 117 |
// The CRT heap initialization stub. |
| 118 |
int _heap_init() { |
| 119 |
// 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. |
| 127 |
void _heap_term() { |
| 128 |
} |
| 129 |
|
| 130 |
// 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. |
| 134 |
void* _crtheap = reinterpret_cast<void*>(1); |
| 135 |
|
| 136 |
int _set_new_mode(int flag) { |
| 137 |
return tc_set_new_mode(flag); |
| 138 |
} |
| 139 |
|
| 140 |
int _query_new_mode() { |
| 141 |
return tc_query_new_mode(); |
| 142 |
} |
| 143 |
|
| 144 |
} // extern "C" |
| 145 |
|
| 146 |
#ifndef NDEBUG |
| 147 |
#undef malloc |
| 148 |
#undef free |
| 149 |
#undef calloc |
| 150 |
int _CrtDbgReport(int, const char*, int, const char*, const char*, ...) { |
| 151 |
return 0; |
| 152 |
} |
| 153 |
|
| 154 |
int _CrtDbgReportW(int, const wchar_t*, int, const wchar_t*, const wchar_t*, ...) { |
| 155 |
return 0; |
| 156 |
} |
| 157 |
|
| 158 |
int _CrtSetReportMode(int, int) { |
| 159 |
return 0; |
| 160 |
} |
| 161 |
|
| 162 |
extern "C" void* _malloc_dbg(size_t size, int , const char*, int) { |
| 163 |
return malloc(size); |
| 164 |
} |
| 165 |
|
| 166 |
extern "C" void _free_dbg(void* ptr, int) { |
| 167 |
free(ptr); |
| 168 |
} |
| 169 |
|
| 170 |
extern "C" void* _calloc_dbg(size_t n, size_t size, int, const char*, int) { |
| 171 |
return calloc(n, size); |
| 172 |
} |
| 173 |
#endif // NDEBUG |