Path: blob/master/src/hotspot/share/gc/shared/cardTable.hpp
40957 views
/*1* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_GC_SHARED_CARDTABLE_HPP25#define SHARE_GC_SHARED_CARDTABLE_HPP2627#include "memory/allocation.hpp"28#include "memory/memRegion.hpp"29#include "oops/oopsHierarchy.hpp"30#include "utilities/align.hpp"3132class CardTable: public CHeapObj<mtGC> {33friend class VMStructs;34public:35typedef uint8_t CardValue;3637// All code generators assume that the size of a card table entry is one byte.38// They need to be updated to reflect any change to this.39// This code can typically be found by searching for the byte_map_base() method.40STATIC_ASSERT(sizeof(CardValue) == 1);4142protected:43// The declaration order of these const fields is important; see the44// constructor before changing.45const MemRegion _whole_heap; // the region covered by the card table46size_t _guard_index; // index of very last element in the card47// table; it is set to a guard value48// (last_card) and should never be modified49size_t _last_valid_index; // index of the last valid element50const size_t _page_size; // page size used when mapping _byte_map51size_t _byte_map_size; // in bytes52CardValue* _byte_map; // the card marking array53CardValue* _byte_map_base;5455int _cur_covered_regions;5657// The covered regions should be in address order.58MemRegion* _covered;59// The committed regions correspond one-to-one to the covered regions.60// They represent the card-table memory that has been committed to service61// the corresponding covered region. It may be that committed region for62// one covered region corresponds to a larger region because of page-size63// roundings. Thus, a committed region for one covered region may64// actually extend onto the card-table space for the next covered region.65MemRegion* _committed;6667// The last card is a guard card, and we commit the page for it so68// we can use the card for verification purposes. We make sure we never69// uncommit the MemRegion for that page.70MemRegion _guard_region;7172inline size_t compute_byte_map_size();7374// Finds and return the index of the region, if any, to which the given75// region would be contiguous. If none exists, assign a new region and76// returns its index. Requires that no more than the maximum number of77// covered regions defined in the constructor are ever in use.78int find_covering_region_by_base(HeapWord* base);7980// Same as above, but finds the region containing the given address81// instead of starting at a given base address.82int find_covering_region_containing(HeapWord* addr);8384// Returns the leftmost end of a committed region corresponding to a85// covered region before covered region "ind", or else "NULL" if "ind" is86// the first covered region.87HeapWord* largest_prev_committed_end(int ind) const;8889// Returns the part of the region mr that doesn't intersect with90// any committed region other than self. Used to prevent uncommitting91// regions that are also committed by other regions. Also protects92// against uncommitting the guard region.93MemRegion committed_unique_to_self(int self, MemRegion mr) const;9495// Some barrier sets create tables whose elements correspond to parts of96// the heap; the CardTableBarrierSet is an example. Such barrier sets will97// normally reserve space for such tables, and commit parts of the table98// "covering" parts of the heap that are committed. At most one covered99// region per generation is needed.100static const int _max_covered_regions = 2;101102enum CardValues {103clean_card = (CardValue)-1,104105dirty_card = 0,106last_card = 1,107CT_MR_BS_last_reserved = 2108};109110// a word's worth (row) of clean card values111static const intptr_t clean_card_row = (intptr_t)(-1);112113public:114CardTable(MemRegion whole_heap);115virtual ~CardTable();116virtual void initialize();117118// The kinds of precision a CardTable may offer.119enum PrecisionStyle {120Precise,121ObjHeadPreciseArray122};123124// Tells what style of precision this card table offers.125PrecisionStyle precision() {126return ObjHeadPreciseArray; // Only one supported for now.127}128129// *** Barrier set functions.130131// Initialization utilities; covered_words is the size of the covered region132// in, um, words.133inline size_t cards_required(size_t covered_words) {134// Add one for a guard card, used to detect errors.135const size_t words = align_up(covered_words, card_size_in_words);136return words / card_size_in_words + 1;137}138139// Dirty the bytes corresponding to "mr" (not all of which must be140// covered.)141void dirty_MemRegion(MemRegion mr);142143// Clear (to clean_card) the bytes entirely contained within "mr" (not144// all of which must be covered.)145void clear_MemRegion(MemRegion mr);146147// Return true if "p" is at the start of a card.148bool is_card_aligned(HeapWord* p) {149CardValue* pcard = byte_for(p);150return (addr_for(pcard) == p);151}152153// Mapping from address to card marking array entry154CardValue* byte_for(const void* p) const {155assert(_whole_heap.contains(p),156"Attempt to access p = " PTR_FORMAT " out of bounds of "157" card marking array's _whole_heap = [" PTR_FORMAT "," PTR_FORMAT ")",158p2i(p), p2i(_whole_heap.start()), p2i(_whole_heap.end()));159CardValue* result = &_byte_map_base[uintptr_t(p) >> card_shift];160assert(result >= _byte_map && result < _byte_map + _byte_map_size,161"out of bounds accessor for card marking array");162return result;163}164165// The card table byte one after the card marking array166// entry for argument address. Typically used for higher bounds167// for loops iterating through the card table.168CardValue* byte_after(const void* p) const {169return byte_for(p) + 1;170}171172virtual void invalidate(MemRegion mr);173void clear(MemRegion mr);174void dirty(MemRegion mr);175176// Provide read-only access to the card table array.177const CardValue* byte_for_const(const void* p) const {178return byte_for(p);179}180const CardValue* byte_after_const(const void* p) const {181return byte_after(p);182}183184// Mapping from card marking array entry to address of first word185HeapWord* addr_for(const CardValue* p) const {186assert(p >= _byte_map && p < _byte_map + _byte_map_size,187"out of bounds access to card marking array. p: " PTR_FORMAT188" _byte_map: " PTR_FORMAT " _byte_map + _byte_map_size: " PTR_FORMAT,189p2i(p), p2i(_byte_map), p2i(_byte_map + _byte_map_size));190size_t delta = pointer_delta(p, _byte_map_base, sizeof(CardValue));191HeapWord* result = (HeapWord*) (delta << card_shift);192assert(_whole_heap.contains(result),193"Returning result = " PTR_FORMAT " out of bounds of "194" card marking array's _whole_heap = [" PTR_FORMAT "," PTR_FORMAT ")",195p2i(result), p2i(_whole_heap.start()), p2i(_whole_heap.end()));196return result;197}198199// Mapping from address to card marking array index.200size_t index_for(void* p) {201assert(_whole_heap.contains(p),202"Attempt to access p = " PTR_FORMAT " out of bounds of "203" card marking array's _whole_heap = [" PTR_FORMAT "," PTR_FORMAT ")",204p2i(p), p2i(_whole_heap.start()), p2i(_whole_heap.end()));205return byte_for(p) - _byte_map;206}207208CardValue* byte_for_index(const size_t card_index) const {209return _byte_map + card_index;210}211212// Resize one of the regions covered by the remembered set.213virtual void resize_covered_region(MemRegion new_region);214215// *** Card-table-RemSet-specific things.216217static uintx ct_max_alignment_constraint();218219// Apply closure "cl" to the dirty cards containing some part of220// MemRegion "mr".221void dirty_card_iterate(MemRegion mr, MemRegionClosure* cl);222223// Return the MemRegion corresponding to the first maximal run224// of dirty cards lying completely within MemRegion mr.225// If reset is "true", then sets those card table entries to the given226// value.227MemRegion dirty_card_range_after_reset(MemRegion mr, bool reset,228int reset_val);229230// Constants231enum SomePublicConstants {232card_shift = 9,233card_size = 1 << card_shift,234card_size_in_words = card_size / sizeof(HeapWord)235};236237static CardValue clean_card_val() { return clean_card; }238static CardValue dirty_card_val() { return dirty_card; }239static intptr_t clean_card_row_val() { return clean_card_row; }240241// Card marking array base (adjusted for heap low boundary)242// This would be the 0th element of _byte_map, if the heap started at 0x0.243// But since the heap starts at some higher address, this points to somewhere244// before the beginning of the actual _byte_map.245CardValue* byte_map_base() const { return _byte_map_base; }246247virtual bool is_in_young(oop obj) const = 0;248249// Print a description of the memory for the card table250virtual void print_on(outputStream* st) const;251252void verify();253void verify_guard();254255// val_equals -> it will check that all cards covered by mr equal val256// !val_equals -> it will check that all cards covered by mr do not equal val257void verify_region(MemRegion mr, CardValue val, bool val_equals) PRODUCT_RETURN;258void verify_not_dirty_region(MemRegion mr) PRODUCT_RETURN;259void verify_dirty_region(MemRegion mr) PRODUCT_RETURN;260};261262#endif // SHARE_GC_SHARED_CARDTABLE_HPP263264265