blob: 12d0e65e201f7f1ba291d2a2bd2e91c65075bba9 [file] [log] [blame]
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -08001/*
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 Hughes3b297c42012-10-11 16:08:51 -070036#include <stdlib.h>
37
Elliott Hughes8f2a5a02013-03-15 15:30:25 -070038#include "libc_logging.h"
Elliott Hughes1e980b62013-01-17 18:36:06 -080039
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080040#define HASHTABLE_SIZE 1543
41#define BACKTRACE_SIZE 32
42/* flag definitions, currently sharing storage with "size" */
43#define SIZE_FLAG_ZYGOTE_CHILD (1<<31)
44#define SIZE_FLAG_MASK (SIZE_FLAG_ZYGOTE_CHILD)
45
46#define MAX_SIZE_T (~(size_t)0)
47
48// =============================================================================
49// Structures
50// =============================================================================
51
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080052struct HashEntry {
53 size_t slot;
54 HashEntry* prev;
55 HashEntry* next;
56 size_t numEntries;
57 // fields above "size" are NOT sent to the host
58 size_t size;
59 size_t allocations;
Elliott Hughes239e7a02013-01-25 17:13:45 -080060 uintptr_t backtrace[0];
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080061};
62
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080063struct HashTable {
64 size_t count;
65 HashEntry* slots[HASHTABLE_SIZE];
66};
67
68/* Entry in malloc dispatch table. */
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070069typedef void* (*MallocDebugMalloc)(size_t);
70typedef void (*MallocDebugFree)(void*);
71typedef void* (*MallocDebugCalloc)(size_t, size_t);
72typedef void* (*MallocDebugRealloc)(void*, size_t);
73typedef void* (*MallocDebugMemalign)(size_t, size_t);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080074struct MallocDebug {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070075 MallocDebugMalloc malloc;
76 MallocDebugFree free;
77 MallocDebugCalloc calloc;
78 MallocDebugRealloc realloc;
79 MallocDebugMemalign memalign;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080080};
81
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070082/* Malloc debugging initialization and finalization routines.
83 *
84 * These routines must be implemented in .so modules that implement malloc
85 * debugging. The are is called once per process from malloc_init_impl and
86 * malloc_fini_impl respectively.
87 *
88 * They are implemented in bionic/libc/bionic/malloc_debug_common.c when malloc
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -080089 * debugging gets initialized for the process.
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070090 *
91 * MallocDebugInit returns:
92 * 0 on success, -1 on failure.
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -080093 */
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070094typedef int (*MallocDebugInit)();
95typedef void (*MallocDebugFini)();
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070096
97// =============================================================================
98// log functions
99// =============================================================================
100
101#define debug_log(format, ...) \
Elliott Hughes1e980b62013-01-17 18:36:06 -0800102 __libc_format_log(ANDROID_LOG_DEBUG, "malloc_leak_check", (format), ##__VA_ARGS__ )
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700103#define error_log(format, ...) \
Elliott Hughes1e980b62013-01-17 18:36:06 -0800104 __libc_format_log(ANDROID_LOG_ERROR, "malloc_leak_check", (format), ##__VA_ARGS__ )
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700105#define info_log(format, ...) \
Elliott Hughes1e980b62013-01-17 18:36:06 -0800106 __libc_format_log(ANDROID_LOG_INFO, "malloc_leak_check", (format), ##__VA_ARGS__ )
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800107
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800108#endif // MALLOC_DEBUG_COMMON_H