override_functions.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include <config.h>
  43. #ifndef _WIN32
  44. # error You should only be including this file in a windows environment!
  45. #endif
  46. #ifndef WIN32_OVERRIDE_ALLOCATORS
  47. # error This file is intended for use when overriding allocators
  48. #endif
  49. #include "tcmalloc.cc"
  50. extern "C" void* _recalloc(void* p, size_t n, size_t size) {
  51. void* result = realloc(p, n * size);
  52. memset(result, 0, n * size);
  53. return result;
  54. }
  55. extern "C" void* _calloc_impl(size_t n, size_t size) {
  56. return calloc(n, size);
  57. }
  58. extern "C" size_t _msize(void* p) {
  59. return MallocExtension::instance()->GetAllocatedSize(p);
  60. }
  61. extern "C" intptr_t _get_heap_handle() {
  62. return 0;
  63. }
  64. // The CRT heap initialization stub.
  65. extern "C" int _heap_init() {
  66. // We intentionally leak this object. It lasts for the process
  67. // lifetime. Trying to teardown at _heap_term() is so late that
  68. // you can't do anything useful anyway.
  69. new TCMallocGuard();
  70. return 1;
  71. }
  72. // The CRT heap cleanup stub.
  73. extern "C" void _heap_term() {
  74. }
  75. extern "C" int _set_new_mode(int flag) {
  76. return tc_set_new_mode(flag);
  77. }
  78. #ifndef NDEBUG
  79. #undef malloc
  80. #undef free
  81. #undef calloc
  82. int _CrtDbgReport(int, const char*, int, const char*, const char*, ...) {
  83. return 0;
  84. }
  85. int _CrtDbgReportW(int, const wchar_t*, int, const wchar_t*, const wchar_t*, ...) {
  86. return 0;
  87. }
  88. int _CrtSetReportMode(int, int) {
  89. return 0;
  90. }
  91. extern "C" void* _malloc_dbg(size_t size, int , const char*, int) {
  92. return malloc(size);
  93. }
  94. extern "C" void _free_dbg(void* ptr, int) {
  95. free(ptr);
  96. }
  97. extern "C" void* _calloc_dbg(size_t n, size_t size, int, const char*, int) {
  98. return calloc(n, size);
  99. }
  100. #endif // NDEBUG
  101. // We set this to 1 because part of the CRT uses a check of _crtheap != 0
  102. // to test whether the CRT has been initialized. Once we've ripped out
  103. // the allocators from libcmt, we need to provide this definition so that
  104. // the rest of the CRT is still usable.
  105. extern "C" void* _crtheap = reinterpret_cast<void*>(1);