< prev index next >

src/share/vm/gc/g1/ptrQueue.cpp

Print this page
rev 12859 : imported patch pqsize

@@ -31,45 +31,48 @@
 #include "runtime/thread.inline.hpp"
 
 #include <new>
 
 PtrQueue::PtrQueue(PtrQueueSet* qset, bool permanent, bool active) :
-  _qset(qset), _buf(NULL), _index(0), _sz(0), _active(active),
-  _permanent(permanent), _lock(NULL)
+  _qset(qset),
+  _active(active),
+  _permanent(permanent),
+  _index(0),
+  _capacity_in_bytes(0),
+  _buf(NULL),
+  _lock(NULL)
 {}
 
 PtrQueue::~PtrQueue() {
   assert(_permanent || (_buf == NULL), "queue must be flushed before delete");
 }
 
 void PtrQueue::flush_impl() {
   if (_buf != NULL) {
-    BufferNode* node = BufferNode::make_node_from_buffer(_buf, _index);
+    BufferNode* node = BufferNode::make_node_from_buffer(_buf, index());
     if (is_empty()) {
       // No work to do.
       qset()->deallocate_buffer(node);
     } else {
       qset()->enqueue_complete_buffer(node);
     }
     _buf = NULL;
-    _index = 0;
+    set_index(0);
   }
 }
 
 
 void PtrQueue::enqueue_known_active(void* ptr) {
-  assert(_index <= _sz, "Invariant.");
-  assert(_index == 0 || _buf != NULL, "invariant");
-
   while (_index == 0) {
     handle_zero_index();
   }
 
-  assert(_index > 0, "postcondition");
-  _index -= sizeof(void*);
-  _buf[byte_index_to_index(_index)] = ptr;
-  assert(_index <= _sz, "Invariant.");
+  assert(_buf != NULL, "postcondition");
+  assert(index() > 0, "postcondition");
+  assert(index() <= capacity(), "invariant");
+  _index -= _element_size;
+  _buf[index()] = ptr;
 }
 
 void PtrQueue::locking_enqueue_completed_buffer(BufferNode* node) {
   assert(_lock->owned_by_self(), "Required.");
 

@@ -83,28 +86,26 @@
   // case.
   _lock->lock_without_safepoint_check();
 }
 
 
-BufferNode* BufferNode::allocate(size_t byte_size) {
-  assert(byte_size > 0, "precondition");
-  assert(is_size_aligned(byte_size, sizeof(void**)),
-         "Invalid buffer size " SIZE_FORMAT, byte_size);
+BufferNode* BufferNode::allocate(size_t size) {
+  size_t byte_size = size * sizeof(void*);
   void* data = NEW_C_HEAP_ARRAY(char, buffer_offset() + byte_size, mtGC);
   return new (data) BufferNode;
 }
 
 void BufferNode::deallocate(BufferNode* node) {
   node->~BufferNode();
   FREE_C_HEAP_ARRAY(char, node);
 }
 
 PtrQueueSet::PtrQueueSet(bool notify_when_complete) :
+  _buffer_size(0),
   _max_completed_queue(0),
   _cbl_mon(NULL), _fl_lock(NULL),
   _notify_when_complete(notify_when_complete),
-  _sz(0),
   _completed_buffers_head(NULL),
   _completed_buffers_tail(NULL),
   _n_completed_buffers(0),
   _process_completed_threshold(0), _process_completed(false),
   _buf_free_list(NULL), _buf_free_list_sz(0)

@@ -131,32 +132,30 @@
   _fl_lock = fl_lock;
   _fl_owner = (fl_owner != NULL) ? fl_owner : this;
 }
 
 void** PtrQueueSet::allocate_buffer() {
-  assert(_sz > 0, "Didn't set a buffer size.");
   BufferNode* node = NULL;
   {
     MutexLockerEx x(_fl_owner->_fl_lock, Mutex::_no_safepoint_check_flag);
     node = _fl_owner->_buf_free_list;
     if (node != NULL) {
       _fl_owner->_buf_free_list = node->next();
       _fl_owner->_buf_free_list_sz--;
     }
   }
   if (node == NULL) {
-    node = BufferNode::allocate(_sz);
+    node = BufferNode::allocate(buffer_size());
   } else {
     // Reinitialize buffer obtained from free list.
     node->set_index(0);
     node->set_next(NULL);
   }
   return BufferNode::make_buffer_from_node(node);
 }
 
 void PtrQueueSet::deallocate_buffer(BufferNode* node) {
-  assert(_sz > 0, "Didn't set a buffer size.");
   MutexLockerEx x(_fl_owner->_fl_lock, Mutex::_no_safepoint_check_flag);
   node->set_next(_fl_owner->_buf_free_list);
   _fl_owner->_buf_free_list = node;
   _fl_owner->_buf_free_list_sz++;
 }

@@ -175,17 +174,17 @@
     BufferNode::deallocate(node);
   }
 }
 
 void PtrQueue::handle_zero_index() {
-  assert(_index == 0, "Precondition.");
+  assert(index() == 0, "precondition");
 
   // This thread records the full buffer and allocates a new one (while
   // holding the lock if there is one).
   if (_buf != NULL) {
     if (!should_enqueue_buffer()) {
-      assert(_index > 0, "the buffer can only be re-used if it's not full");
+      assert(index() > 0, "the buffer can only be re-used if it's not full");
       return;
     }
 
     if (_lock) {
       assert(_lock->owned_by_self(), "Required.");

@@ -204,11 +203,11 @@
       // _lock is released (while enqueueing the completed buffer)
       // the thread that acquires _lock will skip this code,
       // preventing the subsequent the multiple enqueue, and
       // install a newly allocated buffer below.
 
-      BufferNode* node = BufferNode::make_node_from_buffer(_buf, _index);
+      BufferNode* node = BufferNode::make_node_from_buffer(_buf, index());
       _buf = NULL;         // clear shared _buf field
 
       locking_enqueue_completed_buffer(node); // enqueue completed buffer
 
       // While the current thread was enqueueing the buffer another thread

@@ -217,24 +216,25 @@
       // thread doesn't overwrite the buffer allocated by the other thread
       // and potentially losing some dirtied cards.
 
       if (_buf != NULL) return;
     } else {
-      BufferNode* node = BufferNode::make_node_from_buffer(_buf, _index);
+      BufferNode* node = BufferNode::make_node_from_buffer(_buf, index());
       if (qset()->process_or_enqueue_complete_buffer(node)) {
         // Recycle the buffer. No allocation.
         assert(_buf == BufferNode::make_buffer_from_node(node), "invariant");
-        assert(_sz == qset()->buffer_size(), "invariant");
-        _index = _sz;
+        assert(capacity() == qset()->buffer_size(), "invariant");
+        reset();
         return;
       }
     }
   }
-  // Reallocate the buffer
+  // Set capacity in case this is the first allocation.
+  set_capacity(qset()->buffer_size());
+  // Allocate a new buffer.
   _buf = qset()->allocate_buffer();
-  _sz = qset()->buffer_size();
-  _index = _sz;
+  reset();
 }
 
 bool PtrQueueSet::process_or_enqueue_complete_buffer(BufferNode* node) {
   if (Thread::current()->is_Java_thread()) {
     // We don't lock. It is fine to be epsilon-precise here.

@@ -294,12 +294,12 @@
   guarantee(completed_buffers_list_length() ==  _n_completed_buffers,
             "Completed buffer length is wrong.");
 }
 
 void PtrQueueSet::set_buffer_size(size_t sz) {
-  assert(_sz == 0 && sz > 0, "Should be called only once.");
-  _sz = sz * sizeof(void*);
+  assert(_buffer_size == 0 && sz > 0, "Should be called only once.");
+  _buffer_size = sz;
 }
 
 // Merge lists of buffers. Notify the processing threads.
 // The source queue is emptied as a result. The queues
 // must share the monitor.
< prev index next >