1 /*
2  * Copyright (c) 2016, 2020, 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_JFREVENTWRITERHOST_INLINE_HPP
26 #define SHARE_JFR_WRITERS_JFREVENTWRITERHOST_INLINE_HPP
27 
28 #include "jfr/writers/jfrEventWriterHost.hpp"
29 
30 template <typename BE, typename IE, typename WriterPolicyImpl>
31 template <typename StorageType>
32 inline EventWriterHost<BE, IE, WriterPolicyImpl>::
EventWriterHost(StorageType * storage,Thread * thread)33 EventWriterHost(StorageType* storage, Thread* thread) : WriterHost<BE, IE, WriterPolicyImpl>(storage, thread) {}
34 
35 template <typename BE, typename IE, typename WriterPolicyImpl>
EventWriterHost(Thread * thread)36 inline EventWriterHost<BE, IE, WriterPolicyImpl>::EventWriterHost(Thread* thread)  : WriterHost<BE, IE, WriterPolicyImpl>(thread) {
37 }
38 
39 template <typename BE, typename IE, typename WriterPolicyImpl>
begin_write()40 inline void EventWriterHost<BE, IE, WriterPolicyImpl>::begin_write() {
41   assert(this->is_valid(), "invariant");
42   assert(!this->is_acquired(), "calling begin with writer already in acquired state!");
43   this->acquire();
44   assert(this->used_offset() == 0, "invariant");
45   assert(this->is_acquired(), "invariant");
46 }
47 
48 template <typename BE, typename IE, typename WriterPolicyImpl>
end_write(void)49 inline intptr_t EventWriterHost<BE, IE, WriterPolicyImpl>::end_write(void) {
50   assert(this->is_acquired(),
51     "state corruption, calling end with writer with non-acquired state!");
52   return this->is_valid() ? (intptr_t)this->used_offset() : 0;
53 }
54 
55 template <typename BE, typename IE, typename WriterPolicyImpl>
begin_event_write(bool large)56 inline void EventWriterHost<BE, IE, WriterPolicyImpl>::begin_event_write(bool large) {
57   assert(this->is_valid(), "invariant");
58   assert(!this->is_acquired(), "calling begin with writer already in acquired state!");
59   this->begin_write();
60   // reserve the event size slot
61   if (large) {
62     this->reserve(sizeof(u4));
63   } else {
64     this->reserve(sizeof(u1));
65   }
66 }
67 
68 template <typename BE, typename IE, typename WriterPolicyImpl>
end_event_write(bool large)69 inline intptr_t EventWriterHost<BE, IE, WriterPolicyImpl>::end_event_write(bool large) {
70   assert(this->is_acquired(), "invariant");
71   if (!this->is_valid()) {
72     this->release();
73     return 0;
74   }
75   u4 written = (u4)end_write();
76   if (large) {
77     // size written is larger than header reserve, so commit
78     if (written > sizeof(u4)) {
79       this->write_padded_at_offset(written, 0);
80       this->commit();
81     }
82   } else {
83     // abort if event size will not fit in one byte (compressed)
84     if (written > 127) {
85       this->reset();
86       written = 0;
87     } else {
88       // size written is larger than header reserve, so commit
89       if (written > sizeof(u1)) {
90         this->write_at_offset(written, 0);
91         this->commit();
92       }
93     }
94   }
95   this->release();
96   assert(!this->is_acquired(), "invariant");
97   return written;
98 }
99 #endif // SHARE_JFR_WRITERS_JFREVENTWRITERHOST_INLINE_HPP
100