blob: dd374cc2025281c3cc7c03bf9c5df64e77dc162c [file] [log] [blame]
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef _DALVIK_HEAP_BITMAP
17#define _DALVIK_HEAP_BITMAP
18
Carl Shapiroeff16fb2010-08-19 14:29:41 -070019#include <limits.h>
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080020#include <stdint.h>
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080021
Carl Shapiroae188c62011-04-08 13:11:58 -070022#ifdef __cplusplus
23extern "C" {
24#endif
25
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080026#define HB_OBJECT_ALIGNMENT 8
Carl Shapiroeff16fb2010-08-19 14:29:41 -070027#define HB_BITS_PER_WORD (sizeof(unsigned long) * CHAR_BIT)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080028
29/* <offset> is the difference from .base to a pointer address.
30 * <index> is the index of .bits that contains the bit representing
31 * <offset>.
32 */
33#define HB_OFFSET_TO_INDEX(offset_) \
34 ((uintptr_t)(offset_) / HB_OBJECT_ALIGNMENT / HB_BITS_PER_WORD)
35#define HB_INDEX_TO_OFFSET(index_) \
36 ((uintptr_t)(index_) * HB_OBJECT_ALIGNMENT * HB_BITS_PER_WORD)
37
Carl Shapirod25566d2010-03-11 20:39:47 -080038#define HB_OFFSET_TO_BYTE_INDEX(offset_) \
39 (HB_OFFSET_TO_INDEX(offset_) * sizeof(*((HeapBitmap *)0)->bits))
40
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080041/* Pack the bits in backwards so they come out in address order
42 * when using CLZ.
43 */
44#define HB_OFFSET_TO_MASK(offset_) \
45 (1 << \
46 (31-(((uintptr_t)(offset_) / HB_OBJECT_ALIGNMENT) % HB_BITS_PER_WORD)))
47
Carl Shapiro7511ae12010-08-19 16:54:01 -070048typedef struct {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080049 /* The bitmap data, which points to an mmap()ed area of zeroed
50 * anonymous memory.
51 */
Carl Shapiro9cbece22010-07-12 17:45:51 -070052 unsigned long *bits;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080053
Carl Shapiro8d724802010-02-14 18:40:47 -080054 /* The size of the used memory pointed to by bits, in bytes. This
55 * value changes when the bitmap is shrunk.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080056 */
57 size_t bitsLen;
58
Carl Shapiro8d724802010-02-14 18:40:47 -080059 /* The real size of the memory pointed to by bits. This is the
60 * number of bytes we requested from the allocator and does not
61 * change.
62 */
63 size_t allocLen;
64
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080065 /* The base address, which corresponds to the first bit in
66 * the bitmap.
67 */
68 uintptr_t base;
69
70 /* The highest pointer value ever returned by an allocation
71 * from this heap. I.e., the highest address that may correspond
72 * to a set bit. If there are no bits set, (max < base).
73 */
74 uintptr_t max;
Carl Shapiro7511ae12010-08-19 16:54:01 -070075} HeapBitmap;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080076
Carl Shapiroc38b7ed2011-02-08 17:43:43 -080077/*
78 * Callback types used by the walking routines.
79 */
Carl Shapiro57ee2702010-08-27 13:06:48 -070080typedef void BitmapCallback(void *addr, void *arg);
Carl Shapiro38d710b2010-08-31 16:48:31 -070081typedef void BitmapScanCallback(void *addr, void *finger, void *arg);
Carl Shapiro57ee2702010-08-27 13:06:48 -070082typedef void BitmapSweepCallback(size_t numPtrs, void **ptrs, void *arg);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080083
84/*
85 * Initialize a HeapBitmap so that it points to a bitmap large
86 * enough to cover a heap at <base> of <maxSize> bytes, where
87 * objects are guaranteed to be HB_OBJECT_ALIGNMENT-aligned.
88 */
89bool dvmHeapBitmapInit(HeapBitmap *hb, const void *base, size_t maxSize,
90 const char *name);
91
92/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080093 * Clean up any resources associated with the bitmap.
94 */
95void dvmHeapBitmapDelete(HeapBitmap *hb);
96
97/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080098 * Fill the bitmap with zeroes. Returns the bitmap's memory to
99 * the system as a side-effect.
100 */
101void dvmHeapBitmapZero(HeapBitmap *hb);
102
103/*
Carl Shapiroc38b7ed2011-02-08 17:43:43 -0800104 * Returns true if the address range of the bitmap covers the object
105 * address.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800106 */
Carl Shapiroc38b7ed2011-02-08 17:43:43 -0800107bool dvmHeapBitmapCoversAddress(const HeapBitmap *hb, const void *obj);
Carl Shapiro38d710b2010-08-31 16:48:31 -0700108
109/*
Carl Shapiroc38b7ed2011-02-08 17:43:43 -0800110 * Applies the callback function to each set address in the bitmap.
Carl Shapiro38d710b2010-08-31 16:48:31 -0700111 */
Carl Shapiroc38b7ed2011-02-08 17:43:43 -0800112void dvmHeapBitmapWalk(const HeapBitmap *bitmap,
113 BitmapCallback *callback, void *callbackArg);
114
115/*
116 * Like dvmHeapBitmapWalk but takes a callback function with a finger
117 * address.
118 */
119void dvmHeapBitmapScanWalk(HeapBitmap *bitmap,
Carl Shapiro6c355e52011-03-02 15:43:39 -0800120 uintptr_t base, uintptr_t max,
Carl Shapiroc38b7ed2011-02-08 17:43:43 -0800121 BitmapScanCallback *callback, void *arg);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800122
123/*
Carl Shapiro38d710b2010-08-31 16:48:31 -0700124 * Walk through the bitmaps in increasing address order, and find the
125 * object pointers that correspond to garbage objects. Call
126 * <callback> zero or more times with lists of these object pointers.
127 *
Carl Shapiro879011d2010-12-02 16:41:28 -0800128 * The callback is not permitted to increase the max of either bitmap.
Carl Shapiro38d710b2010-08-31 16:48:31 -0700129 */
130void dvmHeapBitmapSweepWalk(const HeapBitmap *liveHb, const HeapBitmap *markHb,
Carl Shapiro21260712010-12-09 15:24:11 -0800131 uintptr_t base, uintptr_t max,
Carl Shapiro38d710b2010-08-31 16:48:31 -0700132 BitmapSweepCallback *callback, void *callbackArg);
133
Carl Shapiroae188c62011-04-08 13:11:58 -0700134#ifdef __cplusplus
135}
136#endif
137
Carl Shapiroc38b7ed2011-02-08 17:43:43 -0800138#endif /* _DALVIK_HEAP_BITMAP */