1/*
2 * Copyright (c) 2016, 2019, 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#ifndef SHARE_JFR_WRITERS_JFRSTORAGEHOST_INLINE_HPP
26#define SHARE_JFR_WRITERS_JFRSTORAGEHOST_INLINE_HPP
27
28#include "jfr/writers/jfrStorageHost.hpp"
29
30template <typename Adapter, typename AP>
31inline void StorageHost<Adapter, AP>::bind() {
32 if (is_backed()) {
33 this->hard_reset();
34 assert(is_valid(), "invariant");
35 return;
36 }
37 this->set_start_pos(NULL);
38 this->set_current_pos((const u1*)NULL);
39 this->set_end_pos(NULL);
40}
41
42template <typename Adapter, typename AP>
43inline void StorageHost<Adapter, AP>::soft_reset() {
44 this->set_start_pos(this->current_pos());
45}
46
47template <typename Adapter, typename AP>
48inline void StorageHost<Adapter, AP>::hard_reset() {
49 this->set_start_pos(_adapter.pos());
50 this->set_current_pos(_adapter.pos());
51 this->set_end_pos(_adapter.end());
52}
53
54template <typename Adapter, typename AP>
55inline void StorageHost<Adapter, AP>::cancel() {
56 this->set_end_pos(NULL);
57}
58
59template <typename Adapter, typename AP>
60inline bool StorageHost<Adapter, AP>::is_backed() {
61 return _adapter.storage() != NULL;
62}
63
64template <typename Adapter, typename AP>
65inline bool StorageHost<Adapter, AP>::accommodate(size_t used, size_t requested) {
66 if (!_adapter.flush(used, requested)) {
67 this->cancel();
68 return false;
69 }
70 assert(is_backed(), "invariant");
71 this->hard_reset();
72 this->set_current_pos(used);
73 return true;
74}
75
76template <typename Adapter, typename AP>
77inline void StorageHost<Adapter, AP>::commit() {
78 if (this->is_valid()) {
79 assert(_adapter.pos() == this->start_pos(), "invariant");
80 assert(_adapter.end() == this->end_pos(), "invariant");
81 u1* new_position = this->current_pos();
82 _adapter.commit(new_position);
83 this->set_start_pos(new_position);
84 }
85}
86
87template <typename Adapter, typename AP>
88inline void StorageHost<Adapter, AP>::release() {
89 _adapter.release();
90}
91
92template <typename Adapter, typename AP>
93inline StorageHost<Adapter, AP>::StorageHost(typename Adapter::StorageType* storage, Thread* thread) : Position<AP>(), _adapter(storage, thread) {
94 bind();
95}
96
97template <typename Adapter, typename AP>
98inline StorageHost<Adapter, AP>::StorageHost(typename Adapter::StorageType* storage, size_t size) : Position<AP>(), _adapter(storage, size) {
99 bind();
100}
101
102template <typename Adapter, typename AP>
103inline StorageHost<Adapter, AP>::StorageHost(Thread* thread) : Position<AP>(), _adapter(thread) {
104 bind();
105}
106
107template <typename Adapter, typename AP>
108inline bool StorageHost<Adapter, AP>::is_valid() const {
109 return this->end_pos() != NULL;
110}
111
112template <typename Adapter, typename AP>
113inline typename Adapter::StorageType* StorageHost<Adapter, AP>::storage() {
114 return _adapter.storage();
115}
116
117template <typename Adapter, typename AP>
118inline void StorageHost<Adapter, AP>::set_storage(typename Adapter::StorageType* storage) {
119 _adapter.set_storage(storage);
120 bind();
121}
122
123template <typename Adapter, typename AP>
124inline void StorageHost<Adapter, AP>::flush() {
125 this->accommodate(this->is_valid() ? this->used_size() : 0, 0);
126}
127
128template <typename Adapter, typename AP>
129inline void StorageHost<Adapter, AP>::seek(intptr_t offset) {
130 if (this->is_valid()) {
131 assert(offset >= 0, "negative offsets not supported");
132 assert(this->start_pos() + offset <= this->end_pos(), "invariant");
133 assert(this->start_pos() + offset >= this->start_pos(), "invariant");
134 this->set_current_pos(this->start_pos() + offset);
135 }
136}
137
138#endif // SHARE_JFR_WRITERS_JFRSTORAGEHOST_INLINE_HPP
139