Path: blob/master/src/hotspot/share/jfr/leakprofiler/sampling/sampleList.cpp
41155 views
/*1* Copyright (c) 2017, 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/sampleList.hpp"27#include "oops/oop.inline.hpp"2829SampleList::SampleList(size_t limit, size_t cache_size) :30_free_list(),31_in_use_list(),32_last_resolved(NULL),33_allocated(0),34_limit(limit),35_cache_size(cache_size) {36}3738SampleList::~SampleList() {39deallocate_samples(_free_list);40deallocate_samples(_in_use_list);41}4243ObjectSample* SampleList::last() const {44return _in_use_list.head();45}4647ObjectSample* SampleList::first() const {48return _in_use_list.tail();49}5051const ObjectSample* SampleList::last_resolved() const {52return _last_resolved;53}5455void SampleList::set_last_resolved(const ObjectSample* sample) {56assert(last() == sample, "invariant");57_last_resolved = sample;58}5960void SampleList::link(ObjectSample* sample) {61assert(sample != NULL, "invariant");62_in_use_list.prepend(sample);63}6465void SampleList::unlink(ObjectSample* sample) {66assert(sample != NULL, "invariant");67if (_last_resolved == sample) {68_last_resolved = sample->next();69}70reset(_in_use_list.remove(sample));71}7273ObjectSample* SampleList::reuse(ObjectSample* sample) {74assert(sample != NULL, "invariant");75unlink(sample);76link(sample);77return sample;78}7980void SampleList::populate_cache() {81if (_free_list.count() < _cache_size) {82const size_t cache_delta = _cache_size - _free_list.count();83for (size_t i = 0; i < cache_delta; ++i) {84ObjectSample* sample = newSample();85if (sample != NULL) {86_free_list.append(sample);87}88}89}90}9192ObjectSample* SampleList::newSample() const {93if (_limit == _allocated) {94return NULL;95}96++_allocated;97return new ObjectSample();98}99100ObjectSample* SampleList::get() {101ObjectSample* sample = _free_list.head();102if (sample != NULL) {103link(_free_list.remove(sample));104} else {105sample = newSample();106if (sample != NULL) {107_in_use_list.prepend(sample);108}109}110if (_cache_size > 0 && sample != NULL) {111populate_cache();112}113return sample;114}115116void SampleList::release(ObjectSample* sample) {117assert(sample != NULL, "invariant");118unlink(sample);119_free_list.append(sample);120}121122void SampleList::deallocate_samples(List& list) {123if (list.count() > 0) {124ObjectSample* sample = list.head();125while (sample != NULL) {126list.remove(sample);127delete sample;128sample = list.head();129}130}131assert(list.count() == 0, "invariant");132}133134void SampleList::reset(ObjectSample* sample) {135assert(sample != NULL, "invariant");136sample->reset();137}138139bool SampleList::is_full() const {140return _in_use_list.count() == _limit;141}142143size_t SampleList::count() const {144return _in_use_list.count();145}146147148