blob: c981c3d593f1c7fe393304706795c469d36fd8cc [file] [log] [blame]
Austin Schuh745610d2015-09-06 18:19:50 -07001// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
2// Copyright (c) 2011, 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: Craig Silverstein <opensource@google.com>
33//
34// This .h file imports the code that causes tcmalloc to override libc
35// versions of malloc/free/new/delete/etc. That is, it provides the
36// logic that makes it so calls to malloc(10) go through tcmalloc,
37// rather than the default (libc) malloc.
38//
39// This file also provides a method: ReplaceSystemAlloc(), that every
40// libc_override_*.h file it #includes is required to provide. This
41// is called when first setting up tcmalloc -- that is, when a global
42// constructor in tcmalloc.cc is executed -- to do any initialization
43// work that may be required for this OS. (Note we cannot entirely
44// control when tcmalloc is initialized, and the system may do some
45// mallocs and frees before this routine is called.) It may be a
46// noop.
47//
48// Every libc has its own way of doing this, and sometimes the compiler
49// matters too, so we have a different file for each libc, and often
50// for different compilers and OS's.
51
52#ifndef TCMALLOC_LIBC_OVERRIDE_INL_H_
53#define TCMALLOC_LIBC_OVERRIDE_INL_H_
54
55#include <config.h>
56#ifdef HAVE_FEATURES_H
57#include <features.h> // for __GLIBC__
58#endif
59#include <gperftools/tcmalloc.h>
60
Brian Silverman20350ac2021-11-17 18:19:55 -080061#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
62#define CPP_NOTHROW noexcept
63#define CPP_BADALLOC
64#else
65#define CPP_NOTHROW throw()
66#define CPP_BADALLOC throw(std::bad_alloc)
67#endif
68
Austin Schuh745610d2015-09-06 18:19:50 -070069static void ReplaceSystemAlloc(); // defined in the .h files below
70
71// For windows, there are two ways to get tcmalloc. If we're
72// patching, then src/windows/patch_function.cc will do the necessary
73// overriding here. Otherwise, we doing the 'redefine' trick, where
74// we remove malloc/new/etc from mscvcrt.dll, and just need to define
75// them now.
76#if defined(_WIN32) && defined(WIN32_DO_PATCHING)
77void PatchWindowsFunctions(); // in src/windows/patch_function.cc
78static void ReplaceSystemAlloc() { PatchWindowsFunctions(); }
79
80#elif defined(_WIN32) && !defined(WIN32_DO_PATCHING)
81#include "libc_override_redefine.h"
82
83#elif defined(__APPLE__)
84#include "libc_override_osx.h"
85
86#elif defined(__GLIBC__)
87#include "libc_override_glibc.h"
88
89// Not all gcc systems necessarily support weak symbols, but all the
90// ones I know of do, so for now just assume they all do.
91#elif defined(__GNUC__)
92#include "libc_override_gcc_and_weak.h"
93
94#else
95#error Need to add support for your libc/OS here
96
97#endif
98
99#endif // TCMALLOC_LIBC_OVERRIDE_INL_H_