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 | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 36 | #include <stdlib.h> |
| 37 | |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 38 | #define HASHTABLE_SIZE 1543 |
| 39 | #define BACKTRACE_SIZE 32 |
| 40 | /* flag definitions, currently sharing storage with "size" */ |
| 41 | #define SIZE_FLAG_ZYGOTE_CHILD (1<<31) |
| 42 | #define SIZE_FLAG_MASK (SIZE_FLAG_ZYGOTE_CHILD) |
| 43 | |
| 44 | #define MAX_SIZE_T (~(size_t)0) |
| 45 | |
| 46 | // ============================================================================= |
| 47 | // Structures |
| 48 | // ============================================================================= |
| 49 | |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 50 | struct HashEntry { |
| 51 | size_t slot; |
| 52 | HashEntry* prev; |
| 53 | HashEntry* next; |
| 54 | size_t numEntries; |
| 55 | // fields above "size" are NOT sent to the host |
| 56 | size_t size; |
| 57 | size_t allocations; |
| 58 | intptr_t backtrace[0]; |
| 59 | }; |
| 60 | |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 61 | struct HashTable { |
| 62 | size_t count; |
| 63 | HashEntry* slots[HASHTABLE_SIZE]; |
| 64 | }; |
| 65 | |
| 66 | /* Entry in malloc dispatch table. */ |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 67 | typedef void* (*MallocDebugMalloc)(size_t); |
| 68 | typedef void (*MallocDebugFree)(void*); |
| 69 | typedef void* (*MallocDebugCalloc)(size_t, size_t); |
| 70 | typedef void* (*MallocDebugRealloc)(void*, size_t); |
| 71 | typedef void* (*MallocDebugMemalign)(size_t, size_t); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 72 | struct MallocDebug { |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 73 | MallocDebugMalloc malloc; |
| 74 | MallocDebugFree free; |
| 75 | MallocDebugCalloc calloc; |
| 76 | MallocDebugRealloc realloc; |
| 77 | MallocDebugMemalign memalign; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 78 | }; |
| 79 | |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 80 | /* Malloc debugging initialization and finalization routines. |
| 81 | * |
| 82 | * These routines must be implemented in .so modules that implement malloc |
| 83 | * debugging. The are is called once per process from malloc_init_impl and |
| 84 | * malloc_fini_impl respectively. |
| 85 | * |
| 86 | * They are implemented in bionic/libc/bionic/malloc_debug_common.c when malloc |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 87 | * debugging gets initialized for the process. |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 88 | * |
| 89 | * MallocDebugInit returns: |
| 90 | * 0 on success, -1 on failure. |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 91 | */ |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 92 | typedef int (*MallocDebugInit)(); |
| 93 | typedef void (*MallocDebugFini)(); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 94 | |
| 95 | // ============================================================================= |
| 96 | // log functions |
| 97 | // ============================================================================= |
| 98 | |
| 99 | #define debug_log(format, ...) \ |
| 100 | __libc_android_log_print(ANDROID_LOG_DEBUG, "malloc_leak_check", (format), ##__VA_ARGS__ ) |
| 101 | #define error_log(format, ...) \ |
| 102 | __libc_android_log_print(ANDROID_LOG_ERROR, "malloc_leak_check", (format), ##__VA_ARGS__ ) |
| 103 | #define info_log(format, ...) \ |
| 104 | __libc_android_log_print(ANDROID_LOG_INFO, "malloc_leak_check", (format), ##__VA_ARGS__ ) |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 105 | |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 106 | #endif // MALLOC_DEBUG_COMMON_H |