blob: 31adebc70a3351004159d54a83e147964bbccde2 [file] [log] [blame]
// Copyright (C) 2008 The Android Open Source Project
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
#include <limits.h>
#include <stdint.h>
#include "UniquePtr.h"
#include "globals.h"
#include "logging.h"
#include "mem_map.h"
namespace art {
class Object;
// <offset> is the difference from .base to a pointer address.
// <index> is the index of .bits that contains the bit representing
// <offset>.
#define HB_OFFSET_TO_INDEX(offset_) \
((offset_) / kAlignment / kBitsPerWord)
#define HB_INDEX_TO_OFFSET(index_) \
((index_) * kAlignment * kBitsPerWord)
#define HB_OFFSET_TO_BYTE_INDEX(offset_) \
(HB_OFFSET_TO_INDEX(offset_) * sizeof(*(reinterpret_cast<HeapBitmap*>(0))->words_))
// Pack the bits in backwards so they come out in address order
// when using CLZ.
#define HB_OFFSET_TO_MASK(offset_) \
(1 << (31-(((uintptr_t)(offset_) / kAlignment) % kBitsPerWord)))
class HeapBitmap {
static const size_t kAlignment = 8;
typedef void Callback(Object* obj, void* arg);
typedef void ScanCallback(Object* obj, void* finger, void* arg);
typedef void SweepCallback(size_t numPtrs, void** ptrs, void* arg);
static HeapBitmap* Create(const char* name, byte* base, size_t length);
inline void Set(const Object* obj) {
Modify(obj, true);
inline void Clear(const Object* obj) {
Modify(obj, false);
void Clear();
inline bool Test(const Object* obj) {
uintptr_t addr = reinterpret_cast<uintptr_t>(obj);
DCHECK(HasAddress(obj)) << obj;
DCHECK(words_ != NULL);
DCHECK_GE(addr, base_);
if (addr <= max_) {
const uintptr_t offset = addr - base_;
return (words_[HB_OFFSET_TO_INDEX(offset)] & HB_OFFSET_TO_MASK(offset)) != 0;
} else {
return false;
bool HasAddress(const void* addr) const;
void VisitRange(uintptr_t base, uintptr_t max, Callback* visitor, void* arg) const;
void Walk(Callback* callback, void* arg);
void ScanWalk(uintptr_t base, uintptr_t max, ScanCallback* thunk, void* arg);
static void SweepWalk(const HeapBitmap& live,
const HeapBitmap& mark,
uintptr_t base, uintptr_t max,
SweepCallback* thunk, void* arg);
HeapBitmap(const void* base, size_t length)
: words_(NULL),
base_(reinterpret_cast<uintptr_t>(base)) {
inline void Modify(const Object* obj, bool do_set) {
uintptr_t addr = reinterpret_cast<uintptr_t>(obj);
DCHECK_GE(addr, base_);
const uintptr_t offset = addr - base_;
const size_t index = HB_OFFSET_TO_INDEX(offset);
const word mask = HB_OFFSET_TO_MASK(offset);
DCHECK_LT(index, num_bytes_ / kWordSize);
if (do_set) {
if (addr > max_) {
max_ = addr;
words_[index] |= mask;
} else {
words_[index] &= ~mask;
bool Init(const char* name, const byte* base, size_t length);
UniquePtr<MemMap> mem_map_;
word* words_;
size_t num_bytes_;
// The base address, which corresponds to the word containing the
// first bit in the bitmap.
uintptr_t base_;
// The highest pointer value ever returned by an allocation from
// this heap. I.e., the highest address that may correspond to a
// set bit. If there are no bits set, (max_ < base_).
uintptr_t max_;
const char* name_;
} // namespace art