- add sources.
[platform/framework/web/crosswalk.git] / src / third_party / tcmalloc / vendor / src / libc_override_glibc.h
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 are using glibc.
34
35 #ifndef TCMALLOC_LIBC_OVERRIDE_GLIBC_INL_H_
36 #define TCMALLOC_LIBC_OVERRIDE_GLIBC_INL_H_
37
38 #include <config.h>
39 #include <features.h>     // for __GLIBC__
40 #ifdef HAVE_SYS_CDEFS_H
41 #include <sys/cdefs.h>    // for __THROW
42 #endif
43 #include <gperftools/tcmalloc.h>
44
45 #ifndef __GLIBC__
46 # error libc_override_glibc.h is for glibc distributions only.
47 #endif
48
49 // In glibc, the memory-allocation methods are weak symbols, so we can
50 // just override them with our own.  If we're using gcc, we can use
51 // __attribute__((alias)) to do the overriding easily (exception:
52 // Mach-O, which doesn't support aliases).  Otherwise we have to use a
53 // function call.
54 #if !defined(__GNUC__) || defined(__MACH__)
55
56 // This also defines ReplaceSystemAlloc().
57 # include "libc_override_redefine.h"  // defines functions malloc()/etc
58
59 #else  // #if !defined(__GNUC__) || defined(__MACH__)
60
61 // If we get here, we're a gcc system, so do all the overriding we do
62 // with gcc.  This does the overriding of all the 'normal' memory
63 // allocation.  This also defines ReplaceSystemAlloc().
64 # include "libc_override_gcc_and_weak.h"
65
66 // We also have to do some glibc-specific overriding.  Some library
67 // routines on RedHat 9 allocate memory using malloc() and free it
68 // using __libc_free() (or vice-versa).  Since we provide our own
69 // implementations of malloc/free, we need to make sure that the
70 // __libc_XXX variants (defined as part of glibc) also point to the
71 // same implementations.  Since it only matters for redhat, we
72 // do it inside the gcc #ifdef, since redhat uses gcc.
73 // TODO(csilvers): only do this if we detect we're an old enough glibc?
74
75 #define ALIAS(tc_fn)   __attribute__ ((alias (#tc_fn)))
76 extern "C" {
77   void* __libc_malloc(size_t size)                ALIAS(tc_malloc);
78   void __libc_free(void* ptr)                     ALIAS(tc_free);
79   void* __libc_realloc(void* ptr, size_t size)    ALIAS(tc_realloc);
80   void* __libc_calloc(size_t n, size_t size)      ALIAS(tc_calloc);
81   void __libc_cfree(void* ptr)                    ALIAS(tc_cfree);
82   void* __libc_memalign(size_t align, size_t s)   ALIAS(tc_memalign);
83   void* __libc_valloc(size_t size)                ALIAS(tc_valloc);
84   void* __libc_pvalloc(size_t size)               ALIAS(tc_pvalloc);
85   int __posix_memalign(void** r, size_t a, size_t s)  ALIAS(tc_posix_memalign);
86 }   // extern "C"
87 #undef ALIAS
88
89 #endif  // #if defined(__GNUC__) && !defined(__MACH__)
90
91
92 // We also have to hook libc malloc.  While our work with weak symbols
93 // should make sure libc malloc is never called in most situations, it
94 // can be worked around by shared libraries with the DEEPBIND
95 // environment variable set.  The below hooks libc to call our malloc
96 // routines even in that situation.  In other situations, this hook
97 // should never be called.
98 extern "C" {
99 static void* glibc_override_malloc(size_t size, const void *caller) {
100   return tc_malloc(size);
101 }
102 static void* glibc_override_realloc(void *ptr, size_t size,
103                                     const void *caller) {
104   return tc_realloc(ptr, size);
105 }
106 static void glibc_override_free(void *ptr, const void *caller) {
107   tc_free(ptr);
108 }
109 static void* glibc_override_memalign(size_t align, size_t size,
110                                      const void *caller) {
111   return tc_memalign(align, size);
112 }
113
114 // We should be using __malloc_initialize_hook here, like the #if 0
115 // code below.  (See http://swoolley.org/man.cgi/3/malloc_hook.)
116 // However, this causes weird linker errors with programs that link
117 // with -static, so instead we just assign the vars directly at
118 // static-constructor time.  That should serve the same effect of
119 // making sure the hooks are set before the first malloc call the
120 // program makes.
121 #if 0
122 #include <malloc.h>  // for __malloc_hook, etc.
123 void glibc_override_malloc_init_hook(void) {
124   __malloc_hook = glibc_override_malloc;
125   __realloc_hook = glibc_override_realloc;
126   __free_hook = glibc_override_free;
127   __memalign_hook = glibc_override_memalign;
128 }
129
130 void (* MALLOC_HOOK_MAYBE_VOLATILE __malloc_initialize_hook)(void)
131     = &glibc_override_malloc_init_hook;
132 #endif
133
134 void* (* MALLOC_HOOK_MAYBE_VOLATILE __malloc_hook)(size_t, const void*)
135     = &glibc_override_malloc;
136 void* (* MALLOC_HOOK_MAYBE_VOLATILE __realloc_hook)(void*, size_t, const void*)
137     = &glibc_override_realloc;
138 void (* MALLOC_HOOK_MAYBE_VOLATILE __free_hook)(void*, const void*)
139     = &glibc_override_free;
140 void* (* MALLOC_HOOK_MAYBE_VOLATILE __memalign_hook)(size_t,size_t, const void*)
141     = &glibc_override_memalign;
142
143 }   // extern "C"
144
145 // No need to write ReplaceSystemAlloc(); one of the #includes above
146 // did it for us.
147
148 #endif  // TCMALLOC_LIBC_OVERRIDE_GLIBC_INL_H_