blob: e7917d3a7b6c779745141454a266bdee9b30536a [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.
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
55extern "C" void* _recalloc(void* p, size_t n, size_t size) {
56 void* result = realloc(p, n * size);
57 memset(result, 0, n * size);
58 return result;
59}
60
61extern "C" void* _calloc_impl(size_t n, size_t size) {
62 return calloc(n, size);
63}
64
65extern "C" size_t _msize(void* p) {
66 return MallocExtension::instance()->GetAllocatedSize(p);
67}
68
69extern "C" intptr_t _get_heap_handle() {
70 return 0;
71}
72
73// The CRT heap initialization stub.
74extern "C" int _heap_init() {
75 // We intentionally leak this object. It lasts for the process
76 // lifetime. Trying to teardown at _heap_term() is so late that
77 // you can't do anything useful anyway.
78 new TCMallocGuard();
79 return 1;
80}
81
82// The CRT heap cleanup stub.
83extern "C" void _heap_term() {
84}
85
86extern "C" int _set_new_mode(int flag) {
87 return tc_set_new_mode(flag);
88}
89
90#ifndef NDEBUG
91#undef malloc
92#undef free
93#undef calloc
94int _CrtDbgReport(int, const char*, int, const char*, const char*, ...) {
95 return 0;
96}
97
98int _CrtDbgReportW(int, const wchar_t*, int, const wchar_t*, const wchar_t*, ...) {
99 return 0;
100}
101
102int _CrtSetReportMode(int, int) {
103 return 0;
104}
105
106extern "C" void* _malloc_dbg(size_t size, int , const char*, int) {
107 return malloc(size);
108}
109
110extern "C" void _free_dbg(void* ptr, int) {
111 free(ptr);
112}
113
114extern "C" void* _calloc_dbg(size_t n, size_t size, int, const char*, int) {
115 return calloc(n, size);
116}
117#endif // NDEBUG
118
119// We set this to 1 because part of the CRT uses a check of _crtheap != 0
120// to test whether the CRT has been initialized. Once we've ripped out
121// the allocators from libcmt, we need to provide this definition so that
122// the rest of the CRT is still usable.
123extern "C" void* _crtheap = reinterpret_cast<void*>(1);