1 /*
   2  * Copyright (c) 2013, 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 #include "precompiled.hpp"
  26 #include "gc_implementation/g1/g1PreserveMarkQueue.hpp"
  27 #include "memory/allocation.hpp"
  28 #include "memory/allocation.inline.hpp"
  29 #include "oops/oopsHierarchy.hpp"
  30 
  31 class G1PreserveMarkChunk {
  32   const size_t _capacity;
  33   size_t _write_index;
  34   size_t _read_index;
  35   G1PreserveMarkQueueEntry* _data;
  36 
  37  public:
  38   G1PreserveMarkChunk(size_t capacity) : _capacity(capacity), _write_index(0), _read_index(0), _next(NULL) {
  39     _data = NEW_C_HEAP_ARRAY(G1PreserveMarkQueueEntry, _capacity, mtGC);
  40   }
  41 
  42   ~G1PreserveMarkChunk() {
  43     FREE_C_HEAP_ARRAY(G1PreserveMarkQueueEntry, _data, mtGC);
  44   }
  45 
  46   bool has_data() {
  47     return _write_index > _read_index;
  48   }
  49 
  50   bool is_full() {
  51     assert(_write_index <= _capacity, "written too far");
  52     return _write_index == _capacity;
  53   }
  54 
  55   void append(const G1PreserveMarkQueueEntry& entry) {
  56     assert(!is_full(), "overflow");
  57     _data[_write_index++] = entry;
  58   }
  59 
  60   G1PreserveMarkQueueEntry remove_first() {
  61     assert(has_data(), "empty");
  62     return _data[_read_index++];
  63   }
  64 
  65   G1PreserveMarkChunk* _next;
  66 };
  67 
  68 bool G1PreserveMarkQueue::has_data() {
  69   return _current_read_chunk != NULL && _current_read_chunk->has_data();
  70 }
  71 
  72 void G1PreserveMarkQueue::append(const oop& obj, const markOop& mark) {
  73   G1PreserveMarkQueueEntry entry = {obj, mark};
  74   if (_current_write_chunk == NULL) {
  75     assert(_current_read_chunk == NULL, "must be");
  76     _current_write_chunk = new G1PreserveMarkChunk(new_chunk_capacity());
  77     _current_read_chunk = _current_write_chunk;
  78   } else if (_current_write_chunk->is_full()) {
  79     G1PreserveMarkChunk* new_chunk = new G1PreserveMarkChunk(new_chunk_capacity());
  80     _current_write_chunk->_next = new_chunk;
  81     _current_write_chunk = new_chunk;
  82   }
  83 
  84   assert(!_current_write_chunk->is_full(), "full");
  85 
  86   _current_write_chunk->append(entry);
  87 }
  88 
  89 G1PreserveMarkQueueEntry G1PreserveMarkQueue::remove_first() {
  90   assert(has_data(), "empty");
  91   G1PreserveMarkQueueEntry entry = _current_read_chunk->remove_first();
  92 
  93   if (!_current_read_chunk->has_data()) {
  94     G1PreserveMarkChunk* old_chunk = _current_read_chunk;
  95     _current_read_chunk = old_chunk->_next;
  96 
  97     if (old_chunk == _current_write_chunk) {
  98       assert(_current_read_chunk == NULL, "we are deleting the last chunk");
  99       _current_write_chunk = NULL;
 100     }
 101 
 102     delete old_chunk;
 103   }
 104 
 105   return entry;
 106 }
 107 
 108 /////////////// Unit tests ///////////////
 109 
 110 #ifndef PRODUCT
 111 
 112 void g1PreserveMarkChunkTests() {
 113   const int chunk_size = 5;
 114 
 115   G1PreserveMarkChunk* chunk = new G1PreserveMarkChunk(chunk_size);
 116 
 117   assert(!chunk->is_full(), "");
 118   assert(!chunk->has_data(), "");
 119 
 120   for (int i = 0; i < chunk_size; i++) {
 121     G1PreserveMarkQueueEntry entry = {(oop)i, (markOop)(chunk_size - i)};
 122     chunk->append(entry);
 123   }
 124 
 125   assert(chunk->is_full(), "");
 126   assert(chunk->has_data(), "");
 127 
 128   for (int i = 0; i < chunk_size; i++) {
 129     assert(chunk->has_data(), "");
 130     G1PreserveMarkQueueEntry entry = chunk->remove_first();
 131     assert((oop)i == entry.obj, "");
 132     assert((markOop)(chunk_size - i) == entry.mark, "");
 133   }
 134 
 135   assert(chunk->is_full(), "");
 136   assert(!chunk->has_data(), "");
 137 
 138   delete chunk;
 139 }
 140 
 141 void G1PreserveMarkQueue::verify_empty() {
 142   assert(_current_write_chunk == NULL, "_current_write_chunk not null");
 143   assert(_current_read_chunk == NULL, "_current_read_chunk not null");
 144 }
 145 
 146 void g1PreserveMarkQueueUse(G1PreserveMarkQueue& queue) {
 147   assert(!queue.has_data(), "");
 148 
 149   const int num_entries = 51;
 150   for (int i = 0; i < num_entries; i++) {
 151     queue.append((oop)i, (markOop)(num_entries - i));
 152   }
 153 
 154   assert(queue.has_data(), "");
 155 
 156   for (int i = 0; i < num_entries; i++) {
 157     assert(queue.has_data(), "");
 158     G1PreserveMarkQueueEntry entry = queue.remove_first();
 159     assert((oop)i == entry.obj, err_msg("wrong data (" PTR_FORMAT ") at index %d", (oopDesc*)(entry.obj), i));
 160     assert((markOop)(num_entries - i) == entry.mark, err_msg("wrong data (" PTR_FORMAT ") at index %d", entry.mark, i));
 161   }
 162 
 163   assert(!queue.has_data(), "");
 164 
 165   queue.verify_empty();
 166 }
 167 
 168 void g1PreserveMarkQueueTestOneUse() {
 169   G1PreserveMarkQueue queue(5, 10);
 170   g1PreserveMarkQueueUse(queue);
 171 }
 172 
 173 
 174 void g1PreserveMarkQueueTestMultiUse() {
 175   G1PreserveMarkQueue queue(5, 10);
 176   g1PreserveMarkQueueUse(queue);
 177   g1PreserveMarkQueueUse(queue);
 178   g1PreserveMarkQueueUse(queue);
 179 }
 180 
 181 void g1PreserveMarkQueueTests() {
 182   g1PreserveMarkQueueTestOneUse();
 183   g1PreserveMarkQueueTestMultiUse();
 184 }
 185 
 186 #endif
 187 
 188