1 /*
   2  * Copyright (c) 2014, 2018, 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 "jfr/leakprofiler/chains/edgeQueue.hpp"
  27 #include "jfr/recorder/storage/jfrVirtualMemory.hpp"
  28 
  29 EdgeQueue::EdgeQueue(size_t reservation_size_bytes, size_t commit_block_size_bytes) :
  30   _vmm(NULL),
  31   _reservation_size_bytes(reservation_size_bytes),
  32   _commit_block_size_bytes(commit_block_size_bytes),
  33   _top_index(0),
  34   _bottom_index(0) {
  35 }
  36 
  37 bool EdgeQueue::initialize() {
  38   assert(_reservation_size_bytes >= _commit_block_size_bytes, "invariant");
  39   assert(_vmm == NULL, "invariant");
  40   _vmm = new JfrVirtualMemory();
  41   return _vmm != NULL && _vmm->initialize(_reservation_size_bytes, _commit_block_size_bytes, sizeof(Edge));
  42 }
  43 
  44 EdgeQueue::~EdgeQueue() {
  45   delete _vmm;
  46 }
  47 
  48 void EdgeQueue::add(const Edge* parent, const oop* ref) {
  49   assert(ref != NULL, "Null objects not allowed in EdgeQueue");
  50   assert(!is_full(), "EdgeQueue is full. Check is_full before adding another Edge");
  51   assert(!_vmm->is_full(), "invariant");
  52   void* const allocation = _vmm->new_datum();
  53   assert(allocation != NULL, "invariant");
  54   new (allocation)Edge(parent, ref);
  55   _top_index++;
  56   assert(_vmm->count() == _top_index, "invariant");
  57 }
  58 
  59 size_t EdgeQueue::top() const {
  60   return _top_index;
  61 }
  62 
  63 size_t EdgeQueue::bottom() const {
  64   return EdgeQueue::_bottom_index;
  65 }
  66 
  67 bool EdgeQueue::is_empty() const {
  68   return _top_index == _bottom_index;
  69 }
  70 
  71 bool EdgeQueue::is_full() const {
  72   return _vmm->is_full();
  73 }
  74 
  75 const Edge* EdgeQueue::remove() const {
  76   assert(!is_empty(), "EdgeQueue is empty. Check if empty before removing Edge");
  77   assert(!_vmm->is_empty(), "invariant");
  78   return (const Edge*)_vmm->get(_bottom_index++);
  79 }
  80 
  81 const Edge* EdgeQueue::element_at(size_t index) const {
  82   assert(index >= _bottom_index, "invariant");
  83   assert(index <_top_index, "invariant");
  84   return (Edge*)_vmm->get(index);
  85 }
  86 
  87 size_t EdgeQueue::reserved_size() const {
  88   assert(_vmm != NULL, "invariant");
  89   return _vmm->reserved_size();
  90 }
  91 
  92 size_t EdgeQueue::live_set() const {
  93   assert(_vmm != NULL, "invariant");
  94   return _vmm->live_set();
  95 }
  96 
  97 size_t EdgeQueue::sizeof_edge() const {
  98   assert(_vmm != NULL, "invariant");
  99   return _vmm->aligned_datum_size_bytes();
 100 }