malloc_debug_common.h revision 5ee320dd35fafc11eaf90c62198e08c6670e35b4
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 36#include <pthread.h> 37#include <stdint.h> 38#include <stdlib.h> 39 40#include "private/libc_logging.h" 41 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 48// This must match the alignment used by the malloc implementation. 49#ifndef MALLOC_ALIGNMENT 50#define MALLOC_ALIGNMENT ((size_t)(2 * sizeof(void *))) 51#endif 52 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 64// valloc(3) and pvalloc(3) were removed from POSIX 2004. We do not include them 65// for LP64, but the symbols remain in LP32 for binary compatibility. 66#ifndef __LP64__ 67#define HAVE_DEPRECATED_MALLOC_FUNCS 1 68#endif 69 70// ============================================================================= 71// Structures 72// ============================================================================= 73 74struct HashEntry { 75 size_t slot; 76 HashEntry* prev; 77 HashEntry* next; 78 size_t numEntries; 79 // fields above "size" are NOT sent to the host 80 size_t size; 81 size_t allocations; 82 uintptr_t backtrace[0]; 83}; 84 85struct HashTable { 86 pthread_mutex_t lock; 87 size_t count; 88 HashEntry* slots[HASHTABLE_SIZE]; 89}; 90 91/* Entry in malloc dispatch table. */ 92typedef void* (*MallocDebugCalloc)(size_t, size_t); 93typedef void (*MallocDebugFree)(void*); 94typedef struct mallinfo (*MallocDebugMallinfo)(); 95typedef void* (*MallocDebugMalloc)(size_t); 96typedef size_t (*MallocDebugMallocUsableSize)(const void*); 97typedef void* (*MallocDebugMemalign)(size_t, size_t); 98typedef int (*MallocDebugPosixMemalign)(void**, size_t, size_t); 99#if defined(HAVE_DEPRECATED_MALLOC_FUNCS) 100typedef void* (*MallocDebugPvalloc)(size_t); 101#endif 102typedef void* (*MallocDebugRealloc)(void*, size_t); 103#if defined(HAVE_DEPRECATED_MALLOC_FUNCS) 104typedef void* (*MallocDebugValloc)(size_t); 105#endif 106 107struct MallocDebug { 108 MallocDebugCalloc calloc; 109 MallocDebugFree free; 110 MallocDebugMallinfo mallinfo; 111 MallocDebugMalloc malloc; 112 MallocDebugMallocUsableSize malloc_usable_size; 113 MallocDebugMemalign memalign; 114 MallocDebugPosixMemalign posix_memalign; 115#if defined(HAVE_DEPRECATED_MALLOC_FUNCS) 116 MallocDebugPvalloc pvalloc; 117#endif 118 MallocDebugRealloc realloc; 119#if defined(HAVE_DEPRECATED_MALLOC_FUNCS) 120 MallocDebugValloc valloc; 121#endif 122}; 123 124typedef bool (*MallocDebugInit)(HashTable*); 125typedef void (*MallocDebugFini)(int); 126 127// ============================================================================= 128// log functions 129// ============================================================================= 130 131#define debug_log(format, ...) \ 132 __libc_format_log(ANDROID_LOG_DEBUG, "malloc_leak_check", (format), ##__VA_ARGS__ ) 133#define error_log(format, ...) \ 134 __libc_format_log(ANDROID_LOG_ERROR, "malloc_leak_check", (format), ##__VA_ARGS__ ) 135#define info_log(format, ...) \ 136 __libc_format_log(ANDROID_LOG_INFO, "malloc_leak_check", (format), ##__VA_ARGS__ ) 137 138#endif // MALLOC_DEBUG_COMMON_H 139