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 
30 template <typename Adapter, typename AP>
bind()31 inline 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 
42 template <typename Adapter, typename AP>
soft_reset()43 inline void StorageHost<Adapter, AP>::soft_reset() {
44   this->set_start_pos(this->current_pos());
45 }
46 
47 template <typename Adapter, typename AP>
hard_reset()48 inline 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 
54 template <typename Adapter, typename AP>
cancel()55 inline void StorageHost<Adapter, AP>::cancel() {
56   this->set_end_pos(NULL);
57 }
58 
59 template <typename Adapter, typename AP>
is_backed()60 inline bool StorageHost<Adapter, AP>::is_backed() {
61   return _adapter.storage() != NULL;
62 }
63 
64 template <typename Adapter, typename AP>
accommodate(size_t used,size_t requested)65 inline 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 
76 template <typename Adapter, typename AP>
commit()77 inline 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 
87 template <typename Adapter, typename AP>
release()88 inline void StorageHost<Adapter, AP>::release() {
89   _adapter.release();
90 }
91 
92 template <typename Adapter, typename AP>
StorageHost(typename Adapter::StorageType * storage,Thread * thread)93 inline StorageHost<Adapter, AP>::StorageHost(typename Adapter::StorageType* storage, Thread* thread) : Position<AP>(), _adapter(storage, thread) {
94   bind();
95 }
96 
97 template <typename Adapter, typename AP>
StorageHost(typename Adapter::StorageType * storage,size_t size)98 inline StorageHost<Adapter, AP>::StorageHost(typename Adapter::StorageType* storage, size_t size) : Position<AP>(), _adapter(storage, size) {
99   bind();
100 }
101 
102 template <typename Adapter, typename AP>
StorageHost(Thread * thread)103 inline StorageHost<Adapter, AP>::StorageHost(Thread* thread) : Position<AP>(), _adapter(thread) {
104   bind();
105 }
106 
107 template <typename Adapter, typename AP>
is_valid() const108 inline bool StorageHost<Adapter, AP>::is_valid() const {
109   return this->end_pos() != NULL;
110 }
111 
112 template <typename Adapter, typename AP>
storage()113 inline typename Adapter::StorageType* StorageHost<Adapter, AP>::storage() {
114   return _adapter.storage();
115 }
116 
117 template <typename Adapter, typename AP>
set_storage(typename Adapter::StorageType * storage)118 inline void StorageHost<Adapter, AP>::set_storage(typename Adapter::StorageType* storage) {
119   _adapter.set_storage(storage);
120   bind();
121 }
122 
123 template <typename Adapter, typename AP>
flush()124 inline void StorageHost<Adapter, AP>::flush() {
125   this->accommodate(this->is_valid() ? this->used_size() : 0, 0);
126 }
127 
128 template <typename Adapter, typename AP>
seek(intptr_t offset)129 inline 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