1/*
2 * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25#ifndef SHARE_VM_GC_G1_G1FROMCARDCACHE_HPP
26#define SHARE_VM_GC_G1_G1FROMCARDCACHE_HPP
27
28#include "memory/allocation.hpp"
29#include "utilities/ostream.hpp"
30
31// G1FromCardCache remembers the most recently processed card on the heap on
32// a per-region and per-thread basis.
33class G1FromCardCache : public AllStatic {
34 private:
35  // Array of card indices. Indexed by heap region (rows) and thread (columns) to minimize
36  // thread contention.
37  // This order minimizes the time to clear all entries for a given region during region
38  // freeing. I.e. a single clear of a single memory area instead of multiple separate
39  // accesses with a large stride per region.
40  static int** _cache;
41  static uint _max_regions;
42  static size_t _static_mem_size;
43
44 public:
45  enum {
46    InvalidCard = -1 // Card value of an invalid card, i.e. a card index not otherwise used.
47  };
48
49  static void clear(uint region_idx);
50
51  // Returns true if the given card is in the cache at the given location, or
52  // replaces the card at that location and returns false.
53  static bool contains_or_replace(uint worker_id, uint region_idx, int card) {
54    int card_in_cache = at(worker_id, region_idx);
55    if (card_in_cache == card) {
56      return true;
57    } else {
58      set(worker_id, region_idx, card);
59      return false;
60    }
61  }
62
63  static int at(uint worker_id, uint region_idx) {
64    return _cache[region_idx][worker_id];
65  }
66
67  static void set(uint worker_id, uint region_idx, int val) {
68    _cache[region_idx][worker_id] = val;
69  }
70
71  static void initialize(uint num_par_rem_sets, uint max_num_regions);
72
73  static void invalidate(uint start_idx, size_t num_regions);
74
75  static void print(outputStream* out = tty) PRODUCT_RETURN;
76
77  static size_t static_mem_size() {
78    return _static_mem_size;
79  }
80};
81
82#endif // SHARE_VM_GC_G1_G1FROMCARDCACHE_HPP
83