blob: fecc2a95b8b9df7061bc5e3de3bee06bae68e80c [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
22#define HB_OBJECT_ALIGNMENT 8
Carl Shapiroeff16fb2010-08-19 14:29:41 -070023#define HB_BITS_PER_WORD (sizeof(unsigned long) * CHAR_BIT)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080024
25/* <offset> is the difference from .base to a pointer address.
26 * <index> is the index of .bits that contains the bit representing
27 * <offset>.
28 */
29#define HB_OFFSET_TO_INDEX(offset_) \
30 ((uintptr_t)(offset_) / HB_OBJECT_ALIGNMENT / HB_BITS_PER_WORD)
31#define HB_INDEX_TO_OFFSET(index_) \
32 ((uintptr_t)(index_) * HB_OBJECT_ALIGNMENT * HB_BITS_PER_WORD)
33
Carl Shapirod25566d2010-03-11 20:39:47 -080034#define HB_OFFSET_TO_BYTE_INDEX(offset_) \
35 (HB_OFFSET_TO_INDEX(offset_) * sizeof(*((HeapBitmap *)0)->bits))
36
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080037/* Pack the bits in backwards so they come out in address order
38 * when using CLZ.
39 */
40#define HB_OFFSET_TO_MASK(offset_) \
41 (1 << \
42 (31-(((uintptr_t)(offset_) / HB_OBJECT_ALIGNMENT) % HB_BITS_PER_WORD)))
43
Carl Shapiro7511ae12010-08-19 16:54:01 -070044typedef struct {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080045 /* The bitmap data, which points to an mmap()ed area of zeroed
46 * anonymous memory.
47 */
Carl Shapiro9cbece22010-07-12 17:45:51 -070048 unsigned long *bits;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080049
Carl Shapiro8d724802010-02-14 18:40:47 -080050 /* The size of the used memory pointed to by bits, in bytes. This
51 * value changes when the bitmap is shrunk.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080052 */
53 size_t bitsLen;
54
Carl Shapiro8d724802010-02-14 18:40:47 -080055 /* The real size of the memory pointed to by bits. This is the
56 * number of bytes we requested from the allocator and does not
57 * change.
58 */
59 size_t allocLen;
60
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080061 /* The base address, which corresponds to the first bit in
62 * the bitmap.
63 */
64 uintptr_t base;
65
66 /* The highest pointer value ever returned by an allocation
67 * from this heap. I.e., the highest address that may correspond
68 * to a set bit. If there are no bits set, (max < base).
69 */
70 uintptr_t max;
Carl Shapiro7511ae12010-08-19 16:54:01 -070071} HeapBitmap;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080072
Carl Shapiroc38b7ed2011-02-08 17:43:43 -080073/*
74 * Callback types used by the walking routines.
75 */
Carl Shapiro57ee2702010-08-27 13:06:48 -070076typedef void BitmapCallback(void *addr, void *arg);
Carl Shapiro38d710b2010-08-31 16:48:31 -070077typedef void BitmapScanCallback(void *addr, void *finger, void *arg);
Carl Shapiro57ee2702010-08-27 13:06:48 -070078typedef void BitmapSweepCallback(size_t numPtrs, void **ptrs, void *arg);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080079
80/*
81 * Initialize a HeapBitmap so that it points to a bitmap large
82 * enough to cover a heap at <base> of <maxSize> bytes, where
83 * objects are guaranteed to be HB_OBJECT_ALIGNMENT-aligned.
84 */
85bool dvmHeapBitmapInit(HeapBitmap *hb, const void *base, size_t maxSize,
86 const char *name);
87
88/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080089 * Clean up any resources associated with the bitmap.
90 */
91void dvmHeapBitmapDelete(HeapBitmap *hb);
92
93/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080094 * Fill the bitmap with zeroes. Returns the bitmap's memory to
95 * the system as a side-effect.
96 */
97void dvmHeapBitmapZero(HeapBitmap *hb);
98
99/*
Carl Shapiroc38b7ed2011-02-08 17:43:43 -0800100 * Returns true if the address range of the bitmap covers the object
101 * address.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800102 */
Carl Shapiroc38b7ed2011-02-08 17:43:43 -0800103bool dvmHeapBitmapCoversAddress(const HeapBitmap *hb, const void *obj);
Carl Shapiro38d710b2010-08-31 16:48:31 -0700104
105/*
Carl Shapiroc38b7ed2011-02-08 17:43:43 -0800106 * Applies the callback function to each set address in the bitmap.
Carl Shapiro38d710b2010-08-31 16:48:31 -0700107 */
Carl Shapiroc38b7ed2011-02-08 17:43:43 -0800108void dvmHeapBitmapWalk(const HeapBitmap *bitmap,
109 BitmapCallback *callback, void *callbackArg);
110
111/*
112 * Like dvmHeapBitmapWalk but takes a callback function with a finger
113 * address.
114 */
115void dvmHeapBitmapScanWalk(HeapBitmap *bitmap,
Carl Shapiro6c355e52011-03-02 15:43:39 -0800116 uintptr_t base, uintptr_t max,
Carl Shapiroc38b7ed2011-02-08 17:43:43 -0800117 BitmapScanCallback *callback, void *arg);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800118
119/*
Carl Shapiro38d710b2010-08-31 16:48:31 -0700120 * Walk through the bitmaps in increasing address order, and find the
121 * object pointers that correspond to garbage objects. Call
122 * <callback> zero or more times with lists of these object pointers.
123 *
Carl Shapiro879011d2010-12-02 16:41:28 -0800124 * The callback is not permitted to increase the max of either bitmap.
Carl Shapiro38d710b2010-08-31 16:48:31 -0700125 */
126void dvmHeapBitmapSweepWalk(const HeapBitmap *liveHb, const HeapBitmap *markHb,
Carl Shapiro21260712010-12-09 15:24:11 -0800127 uintptr_t base, uintptr_t max,
Carl Shapiro38d710b2010-08-31 16:48:31 -0700128 BitmapSweepCallback *callback, void *callbackArg);
129
Carl Shapiroc38b7ed2011-02-08 17:43:43 -0800130#endif /* _DALVIK_HEAP_BITMAP */