1// Copyright (c) 2007, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8//     * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10//     * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14//     * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30// ---
31// Author: Mike Belshe
32//
33// To link tcmalloc into a EXE or DLL statically without using the patching
34// facility, we can take a stock libcmt and remove all the allocator functions.
35// When we relink the EXE/DLL with the modified libcmt and tcmalloc, a few
36// functions are missing.  This file contains the additional overrides which
37// are required in the VS2005 libcmt in order to link the modified libcmt.
38//
39// See also
40// http://groups.google.com/group/google-perftools/browse_thread/thread/41cd3710af85e57b
41
42#include <config.h>
43
44#ifndef _WIN32
45# error You should only be including this file in a windows environment!
46#endif
47
48#ifndef WIN32_OVERRIDE_ALLOCATORS
49# error This file is intended for use when overriding allocators
50#endif
51
52#include "tcmalloc.cc"
53
54extern "C" void* _recalloc(void* p, size_t n, size_t size) {
55  void* result = realloc(p, n * size);
56  memset(result, 0, n * size);
57  return result;
58}
59
60extern "C" void* _calloc_impl(size_t n, size_t size) {
61  return calloc(n, size);
62}
63
64extern "C" size_t _msize(void* p) {
65  return MallocExtension::instance()->GetAllocatedSize(p);
66}
67
68extern "C" intptr_t _get_heap_handle() {
69  return 0;
70}
71
72// The CRT heap initialization stub.
73extern "C" int _heap_init() {
74  // We intentionally leak this object.  It lasts for the process
75  // lifetime.  Trying to teardown at _heap_term() is so late that
76  // you can't do anything useful anyway.
77  new TCMallocGuard();
78  return 1;
79}
80
81// The CRT heap cleanup stub.
82extern "C" void _heap_term() {
83}
84
85extern "C" int _set_new_mode(int flag) {
86  return tc_set_new_mode(flag);
87}
88
89#ifndef NDEBUG
90#undef malloc
91#undef free
92#undef calloc
93int _CrtDbgReport(int, const char*, int, const char*, const char*, ...) {
94  return 0;
95}
96
97int _CrtDbgReportW(int, const wchar_t*, int, const wchar_t*, const wchar_t*, ...) {
98  return 0;
99}
100
101int _CrtSetReportMode(int, int) {
102  return 0;
103}
104
105extern "C" void* _malloc_dbg(size_t size, int , const char*, int) {
106  return malloc(size);
107}
108
109extern "C" void _free_dbg(void* ptr, int) {
110  free(ptr);
111}
112
113extern "C" void* _calloc_dbg(size_t n, size_t size, int, const char*, int) {
114  return calloc(n, size);
115}
116#endif  // NDEBUG
117
118// We set this to 1 because part of the CRT uses a check of _crtheap != 0
119// to test whether the CRT has been initialized.  Once we've ripped out
120// the allocators from libcmt, we need to provide this definition so that
121// the rest of the CRT is still usable.
122extern "C" void* _crtheap = reinterpret_cast<void*>(1);
123