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