blob: 87600d64adea2b8afbbab16ccb3c06dd5339ee9a [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
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40#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
52typedef struct HashEntry HashEntry;
53struct HashEntry {
54 size_t slot;
55 HashEntry* prev;
56 HashEntry* next;
57 size_t numEntries;
58 // fields above "size" are NOT sent to the host
59 size_t size;
60 size_t allocations;
61 intptr_t backtrace[0];
62};
63
64typedef struct HashTable HashTable;
65struct HashTable {
66 size_t count;
67 HashEntry* slots[HASHTABLE_SIZE];
68};
69
70/* Entry in malloc dispatch table. */
71typedef struct MallocDebug MallocDebug;
72struct MallocDebug {
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -080073 /* Address of the actual malloc routine. */
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080074 void* (*malloc)(size_t bytes);
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -080075 /* Address of the actual free routine. */
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080076 void (*free)(void* mem);
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -080077 /* Address of the actual calloc routine. */
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080078 void* (*calloc)(size_t n_elements, size_t elem_size);
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -080079 /* Address of the actual realloc routine. */
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080080 void* (*realloc)(void* oldMem, size_t bytes);
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -080081 /* Address of the actual memalign routine. */
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080082 void* (*memalign)(size_t alignment, size_t bytes);
83};
84
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -080085/* Malloc debugging initialization routine.
86 * This routine must be implemented in .so modules that implement malloc
87 * debugging. This routine is called once per process from malloc_init_impl
88 * routine implemented in bionic/libc/bionic/malloc_debug_common.c when malloc
89 * debugging gets initialized for the process.
90 * Return:
91 * 0 on success, -1 on failure.
92 */
93typedef int (*MallocDebugInit)(void);
94
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080095#ifdef __cplusplus
96}; /* end of extern "C" */
97#endif
98
99#endif // MALLOC_DEBUG_COMMON_H