Path: blob/master/src/hotspot/share/jfr/leakprofiler/sampling/samplePriorityQueue.cpp
41155 views
/*1* Copyright (c) 2014, 2018, 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#include "precompiled.hpp"25#include "jfr/leakprofiler/sampling/objectSample.hpp"26#include "jfr/leakprofiler/sampling/samplePriorityQueue.hpp"27#include "memory/allocation.inline.hpp"28#include "oops/oop.inline.hpp"2930SamplePriorityQueue::SamplePriorityQueue(size_t size) :31_allocated_size(size),32_count(0),33_total(0) {34_items = NEW_C_HEAP_ARRAY(ObjectSample*, size, mtTracing);35memset(_items, 0, sizeof(ObjectSample*) * size);36}3738SamplePriorityQueue::~SamplePriorityQueue() {39FREE_C_HEAP_ARRAY(ObjectSample*, _items);40_items = NULL;41}4243void SamplePriorityQueue::push(ObjectSample* item) {44assert(item != NULL, "invariant");45assert(_items[_count] == NULL, "invariant");4647_items[_count] = item;48_items[_count]->set_index(_count);49_count++;50moveUp(_count - 1);51_total += item->span();52}5354size_t SamplePriorityQueue::total() const {55return _total;56}5758ObjectSample* SamplePriorityQueue::pop() {59if (_count == 0) {60return NULL;61}6263ObjectSample* const s = _items[0];64assert(s != NULL, "invariant");65swap(0, _count - 1);66_count--;67assert(s == _items[_count], "invariant");68// clear from heap69_items[_count] = NULL;70moveDown(0);71_total -= s->span();72return s;73}7475void SamplePriorityQueue::swap(int i, int j) {76ObjectSample* tmp = _items[i];77_items[i] = _items[j];78_items[j] = tmp;79_items[i]->set_index(i);80_items[j]->set_index(j);81}8283static int left(int i) {84return 2 * i + 1;85}8687static int right(int i) {88return 2 * i + 2;89}9091static int parent(int i) {92return (i - 1) / 2;93}9495void SamplePriorityQueue::moveDown(int i) {96do {97int j = -1;98int r = right(i);99if (r < _count && _items[r]->span() < _items[i]->span()) {100int l = left(i);101if (_items[l]->span() < _items[r]->span()) {102j = l;103} else {104j = r;105}106} else {107int l = left(i);108if (l < _count && _items[l]->span() < _items[i]->span()) {109j = l;110}111}112if (j >= 0) {113swap(i, j);114}115i = j;116} while (i >= 0);117118}119120void SamplePriorityQueue::moveUp(int i) {121int p = parent(i);122while (i > 0 && _items[i]->span() < _items[p]->span()) {123swap(i,p);124i = p;125p = parent(i);126}127}128129void SamplePriorityQueue::remove(ObjectSample* s) {130assert(s != NULL, "invariant");131const size_t realSpan = s->span();132s->set_span(0);133moveUp(s->index());134s->set_span(realSpan);135pop();136}137138int SamplePriorityQueue::count() const {139return _count;140}141142const ObjectSample* SamplePriorityQueue::peek() const {143return _count == 0 ? NULL : _items[0];144}145146ObjectSample* SamplePriorityQueue::item_at(int index) {147assert(index >= 0 && index < _count, "out of range");148return _items[index];149}150151152