Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 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 |
| 7 | * are met: |
| 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 copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | /* |
| 30 | * Contains declarations of types and constants used by malloc leak |
| 31 | * detection code in both, libc and libc_malloc_debug libraries. |
| 32 | */ |
| 33 | #ifndef MALLOC_DEBUG_COMMON_H |
| 34 | #define MALLOC_DEBUG_COMMON_H |
| 35 | |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 36 | #include <pthread.h> |
| 37 | #include <stdint.h> |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 38 | #include <stdlib.h> |
| 39 | |
Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 40 | #include "private/libc_logging.h" |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 41 | |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 42 | #define HASHTABLE_SIZE 1543 |
| 43 | #define BACKTRACE_SIZE 32 |
| 44 | /* flag definitions, currently sharing storage with "size" */ |
| 45 | #define SIZE_FLAG_ZYGOTE_CHILD (1<<31) |
| 46 | #define SIZE_FLAG_MASK (SIZE_FLAG_ZYGOTE_CHILD) |
| 47 | |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 48 | // This must match the alignment used by the malloc implementation. |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 49 | #ifndef MALLOC_ALIGNMENT |
| 50 | #define MALLOC_ALIGNMENT ((size_t)(2 * sizeof(void *))) |
| 51 | #endif |
| 52 | |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 53 | #ifdef USE_JEMALLOC |
| 54 | #include "jemalloc.h" |
| 55 | #define Malloc(function) je_ ## function |
| 56 | #else |
| 57 | #ifndef USE_DLMALLOC |
| 58 | #error "Either one of USE_DLMALLOC or USE_JEMALLOC must be defined." |
| 59 | #endif |
| 60 | #include "dlmalloc.h" |
| 61 | #define Malloc(function) dl ## function |
| 62 | #endif |
| 63 | |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 64 | // ============================================================================= |
| 65 | // Structures |
| 66 | // ============================================================================= |
| 67 | |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 68 | struct HashEntry { |
| 69 | size_t slot; |
| 70 | HashEntry* prev; |
| 71 | HashEntry* next; |
| 72 | size_t numEntries; |
| 73 | // fields above "size" are NOT sent to the host |
| 74 | size_t size; |
| 75 | size_t allocations; |
Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 76 | uintptr_t backtrace[0]; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 77 | }; |
| 78 | |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 79 | struct HashTable { |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 80 | pthread_mutex_t lock; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 81 | size_t count; |
| 82 | HashEntry* slots[HASHTABLE_SIZE]; |
| 83 | }; |
| 84 | |
| 85 | /* Entry in malloc dispatch table. */ |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 86 | typedef void* (*MallocDebugCalloc)(size_t, size_t); |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 87 | typedef void (*MallocDebugFree)(void*); |
| 88 | typedef struct mallinfo (*MallocDebugMallinfo)(); |
| 89 | typedef void* (*MallocDebugMalloc)(size_t); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 90 | typedef size_t (*MallocDebugMallocUsableSize)(const void*); |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 91 | typedef void* (*MallocDebugMemalign)(size_t, size_t); |
| 92 | typedef int (*MallocDebugPosixMemalign)(void**, size_t, size_t); |
| 93 | typedef void* (*MallocDebugPvalloc)(size_t); |
| 94 | typedef void* (*MallocDebugRealloc)(void*, size_t); |
| 95 | typedef void* (*MallocDebugValloc)(size_t); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 96 | struct MallocDebug { |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 97 | MallocDebugCalloc calloc; |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 98 | MallocDebugFree free; |
| 99 | MallocDebugMallinfo mallinfo; |
| 100 | MallocDebugMalloc malloc; |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 101 | MallocDebugMallocUsableSize malloc_usable_size; |
Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 102 | MallocDebugMemalign memalign; |
| 103 | MallocDebugPosixMemalign posix_memalign; |
| 104 | MallocDebugPvalloc pvalloc; |
| 105 | MallocDebugRealloc realloc; |
| 106 | MallocDebugValloc valloc; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 107 | }; |
| 108 | |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 109 | typedef bool (*MallocDebugInit)(HashTable*); |
| 110 | typedef void (*MallocDebugFini)(int); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 111 | |
| 112 | // ============================================================================= |
| 113 | // log functions |
| 114 | // ============================================================================= |
| 115 | |
| 116 | #define debug_log(format, ...) \ |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 117 | __libc_format_log(ANDROID_LOG_DEBUG, "malloc_leak_check", (format), ##__VA_ARGS__ ) |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 118 | #define error_log(format, ...) \ |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 119 | __libc_format_log(ANDROID_LOG_ERROR, "malloc_leak_check", (format), ##__VA_ARGS__ ) |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 120 | #define info_log(format, ...) \ |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 121 | __libc_format_log(ANDROID_LOG_INFO, "malloc_leak_check", (format), ##__VA_ARGS__ ) |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 122 | |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 123 | #endif // MALLOC_DEBUG_COMMON_H |