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
56void* operator new(size_t size) throw (std::bad_alloc)
57    ALIAS(tc_new);
58void operator delete(void* p) __THROW
59    ALIAS(tc_delete);
60void* operator new[](size_t size) throw (std::bad_alloc)
61    ALIAS(tc_newarray);
62void operator delete[](void* p) __THROW
63    ALIAS(tc_deletearray);
64void* operator new(size_t size, const std::nothrow_t& nt) __THROW
65    ALIAS(tc_new_nothrow);
66void* operator new[](size_t size, const std::nothrow_t& nt) __THROW
67    ALIAS(tc_newarray_nothrow);
68void operator delete(void* p, const std::nothrow_t& nt) __THROW
69    ALIAS(tc_delete_nothrow);
70void operator delete[](void* p, const std::nothrow_t& nt) __THROW
71    ALIAS(tc_deletearray_nothrow);
72
73extern "C" {
74  void* malloc(size_t size) __THROW               ALIAS(tc_malloc);
75  void free(void* ptr) __THROW                    ALIAS(tc_free);
76  void* realloc(void* ptr, size_t size) __THROW   ALIAS(tc_realloc);
77  void* calloc(size_t n, size_t size) __THROW     ALIAS(tc_calloc);
78  void cfree(void* ptr) __THROW                   ALIAS(tc_cfree);
79  void* memalign(size_t align, size_t s) __THROW  ALIAS(tc_memalign);
80  void* valloc(size_t size) __THROW               ALIAS(tc_valloc);
81  void* pvalloc(size_t size) __THROW              ALIAS(tc_pvalloc);
82  int posix_memalign(void** r, size_t a, size_t s) __THROW
83      ALIAS(tc_posix_memalign);
84  void malloc_stats(void) __THROW                 ALIAS(tc_malloc_stats);
85  int mallopt(int cmd, int value) __THROW         ALIAS(tc_mallopt);
86#ifdef HAVE_STRUCT_MALLINFO
87  struct mallinfo mallinfo(void) __THROW          ALIAS(tc_mallinfo);
88#endif
89  size_t malloc_size(void* p) __THROW             ALIAS(tc_malloc_size);
90  size_t malloc_usable_size(void* p) __THROW      ALIAS(tc_malloc_size);
91}   // extern "C"
92
93#undef ALIAS
94
95// No need to do anything at tcmalloc-registration time: we do it all
96// via overriding weak symbols (at link time).
97static void ReplaceSystemAlloc() { }
98
99#endif  // TCMALLOC_LIBC_OVERRIDE_GCC_AND_WEAK_INL_H_
100