1// Copyright (c) 2011, 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: Craig Silverstein <opensource@google.com>
32//
33// Used to override malloc routines on systems that define the
34// memory allocation routines to be weak symbols in their libc
35// (almost all unix-based systems are like this), on gcc, which
36// suppports the 'alias' attribute.
37
38#ifndef TCMALLOC_LIBC_OVERRIDE_GCC_AND_WEAK_INL_H_
39#define TCMALLOC_LIBC_OVERRIDE_GCC_AND_WEAK_INL_H_
40
41#ifdef HAVE_SYS_CDEFS_H
42#include <sys/cdefs.h>    // for __THROW
43#endif
44#include <gperftools/tcmalloc.h>
45
46#ifndef __THROW    // I guess we're not on a glibc-like system
47# define __THROW   // __THROW is just an optimization, so ok to make it ""
48#endif
49
50#ifndef __GNUC__
51# error libc_override_gcc_and_weak.h is for gcc distributions only.
52#endif
53
54#define ALIAS(tc_fn)   __attribute__ ((alias (#tc_fn)))
55
56#if defined(__ANDROID__)
57// Android's bionic doesn't have std::bad_alloc.
58#define STD_BAD_ALLOC
59#else
60#define STD_BAD_ALLOC std::bad_alloc
61#endif
62
63void* operator new(size_t size) throw (STD_BAD_ALLOC)
64    ALIAS(tc_new);
65void operator delete(void* p) __THROW
66    ALIAS(tc_delete);
67void* operator new[](size_t size) throw (STD_BAD_ALLOC)
68    ALIAS(tc_newarray);
69void operator delete[](void* p) __THROW
70    ALIAS(tc_deletearray);
71void* operator new(size_t size, const std::nothrow_t& nt) __THROW
72    ALIAS(tc_new_nothrow);
73void* operator new[](size_t size, const std::nothrow_t& nt) __THROW
74    ALIAS(tc_newarray_nothrow);
75void operator delete(void* p, const std::nothrow_t& nt) __THROW
76    ALIAS(tc_delete_nothrow);
77void operator delete[](void* p, const std::nothrow_t& nt) __THROW
78    ALIAS(tc_deletearray_nothrow);
79
80extern "C" {
81  void* malloc(size_t size) __THROW               ALIAS(tc_malloc);
82  void free(void* ptr) __THROW                    ALIAS(tc_free);
83  void* realloc(void* ptr, size_t size) __THROW   ALIAS(tc_realloc);
84  void* calloc(size_t n, size_t size) __THROW     ALIAS(tc_calloc);
85  void cfree(void* ptr) __THROW                   ALIAS(tc_cfree);
86  void* memalign(size_t align, size_t s) __THROW  ALIAS(tc_memalign);
87  void* valloc(size_t size) __THROW               ALIAS(tc_valloc);
88  void* pvalloc(size_t size) __THROW              ALIAS(tc_pvalloc);
89  int posix_memalign(void** r, size_t a, size_t s) __THROW
90      ALIAS(tc_posix_memalign);
91  void malloc_stats(void) __THROW                 ALIAS(tc_malloc_stats);
92  int mallopt(int cmd, int value) __THROW         ALIAS(tc_mallopt);
93#ifdef HAVE_STRUCT_MALLINFO
94  struct mallinfo mallinfo(void) __THROW          ALIAS(tc_mallinfo);
95#endif
96  size_t malloc_size(void* p) __THROW             ALIAS(tc_malloc_size);
97#if defined(__ANDROID__)
98  size_t malloc_usable_size(const void* p) __THROW
99         ALIAS(tc_malloc_size);
100#else
101  size_t malloc_usable_size(void* p) __THROW      ALIAS(tc_malloc_size);
102#endif
103}   // extern "C"
104
105#undef ALIAS
106
107// No need to do anything at tcmalloc-registration time: we do it all
108// via overriding weak symbols (at link time).
109static void ReplaceSystemAlloc() { }
110
111#endif  // TCMALLOC_LIBC_OVERRIDE_GCC_AND_WEAK_INL_H_
112