src/hotspot/share/jfr/writers/jfrTypeWriterHost.hpp
branchJEP-349-branch
changeset 57870 00860d9caf4d
parent 57360 5d043a159d5c
child 58157 9dca61a7df19
equal deleted inserted replaced
57862:84ef29ccac56 57870:00860d9caf4d
       
     1 /*
       
     2  * Copyright (c) 2017, 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_JFRTYPEWRITERHOST_HPP
       
    26 #define SHARE_JFR_WRITERS_JFRTYPEWRITERHOST_HPP
       
    27 
       
    28 #include "jfr/recorder/checkpoint/jfrCheckpointWriter.hpp"
       
    29 #include "jfr/utilities/jfrTypes.hpp"
       
    30 #include "memory/allocation.hpp"
       
    31 
       
    32 template <typename WriterImpl, u4 ID>
       
    33 class JfrTypeWriterHost : public StackObj {
       
    34  private:
       
    35   WriterImpl _impl;
       
    36   JfrCheckpointWriter* _writer;
       
    37   JfrCheckpointContext _ctx;
       
    38   int64_t _count_offset;
       
    39   int _count;
       
    40   bool _skip_header;
       
    41  public:
       
    42   JfrTypeWriterHost(JfrCheckpointWriter* writer,
       
    43                     bool class_unload = false,
       
    44                     bool skip_header = false) : _impl(writer, class_unload),
       
    45                                                 _writer(writer),
       
    46                                                 _ctx(writer->context()),
       
    47                                                 _count(0),
       
    48                                                 _skip_header(skip_header) {
       
    49     assert(_writer != NULL, "invariant");
       
    50     if (!_skip_header) {
       
    51       _writer->write_type((JfrTypeId)ID);
       
    52       _count_offset = _writer->reserve(sizeof(u4)); // Don't know how many yet
       
    53     }
       
    54   }
       
    55 
       
    56   ~JfrTypeWriterHost() {
       
    57     if (_count == 0) {
       
    58       // nothing written, restore context for rewind
       
    59       _writer->set_context(_ctx);
       
    60       return;
       
    61     }
       
    62     assert(_count > 0, "invariant");
       
    63     if (!_skip_header) {
       
    64       _writer->write_count(_count, _count_offset);
       
    65     }
       
    66   }
       
    67 
       
    68   bool operator()(typename WriterImpl::Type const & value) {
       
    69     this->_count += _impl(value);
       
    70     return true;
       
    71   }
       
    72 
       
    73   int count() const   { return _count; }
       
    74   void add(int count) { _count += count; }
       
    75 };
       
    76 
       
    77 typedef int(*type_write_operation)(JfrCheckpointWriter*, const void*);
       
    78 
       
    79 template <typename T, type_write_operation op>
       
    80 class JfrTypeWriterImplHost {
       
    81  private:
       
    82   JfrCheckpointWriter* _writer;
       
    83  public:
       
    84   typedef T Type;
       
    85   JfrTypeWriterImplHost(JfrCheckpointWriter* writer, bool class_unload = false) : _writer(writer) {}
       
    86   int operator()(T const& value) {
       
    87     return op(this->_writer, value);
       
    88   }
       
    89 };
       
    90 
       
    91 template <typename T, typename Predicate, type_write_operation op>
       
    92 class JfrPredicatedTypeWriterImplHost : public JfrTypeWriterImplHost<T, op> {
       
    93  private:
       
    94   Predicate _predicate;
       
    95   typedef JfrTypeWriterImplHost<T, op> Parent;
       
    96  public:
       
    97   JfrPredicatedTypeWriterImplHost(JfrCheckpointWriter* writer, bool class_unload = false) :
       
    98     Parent(writer), _predicate(class_unload) {}
       
    99   int operator()(T const& value) {
       
   100     return _predicate(value) ? Parent::operator()(value) : 0;
       
   101   }
       
   102 };
       
   103 
       
   104 #endif // SHARE_JFR_WRITERS_JFRTYPEWRITERHOST_HPP