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 #include "precompiled.hpp"
  26 #include "jfr/recorder/checkpoint/jfrCheckpointBlob.hpp"
  27 #include "jfr/recorder/checkpoint/jfrCheckpointWriter.hpp"
  28 
  29 JfrCheckpointBlob::JfrCheckpointBlob(const u1* checkpoint, size_t size) :
  30   _checkpoint(JfrCHeapObj::new_array<u1>(size)),
  31   _size(size),
  32   _next(),
  33   _written(false) {
  34   assert(checkpoint != NULL, "invariant");
  35   assert(_checkpoint != NULL, "invariant");
  36   memcpy(const_cast<u1*>(_checkpoint), checkpoint, size);
  37 }
  38 
  39 JfrCheckpointBlob::~JfrCheckpointBlob() {
  40   JfrCHeapObj::free(const_cast<u1*>(_checkpoint), _size);
  41 }
  42 
  43 const JfrCheckpointBlobHandle& JfrCheckpointBlob::next() const {
  44   return _next;
  45 }
  46 
  47 void JfrCheckpointBlob::write_this(JfrCheckpointWriter& writer) const {
  48   writer.bytes(_checkpoint, _size);
  49 }
  50 
  51 void JfrCheckpointBlob::exclusive_write(JfrCheckpointWriter& writer) const {
  52   if (!_written) {
  53     write_this(writer);
  54     _written = true;
  55   }
  56   if (_next.valid()) {
  57     _next->exclusive_write(writer);
  58   }
  59 }
  60 
  61 void JfrCheckpointBlob::write(JfrCheckpointWriter& writer) const {
  62   write_this(writer);
  63   if (_next.valid()) {
  64     _next->write(writer);
  65   }
  66 }
  67 
  68 void JfrCheckpointBlob::reset_write_state() const {
  69   if (_written) {
  70     _written = false;
  71   }
  72   if (_next.valid()) {
  73     _next->reset_write_state();
  74   }
  75 }
  76 
  77 void JfrCheckpointBlob::set_next(const JfrCheckpointBlobHandle& ref) {
  78   if (_next == ref) {
  79     return;
  80   }
  81   assert(_next != ref, "invariant");
  82   if (_next.valid()) {
  83     _next->set_next(ref);
  84     return;
  85   }
  86   _next = ref;
  87 }
  88 
  89 JfrCheckpointBlobHandle JfrCheckpointBlob::make(const u1* checkpoint, size_t size) {
  90   const JfrCheckpointBlob* cp_blob = new JfrCheckpointBlob(checkpoint, size);
  91   assert(cp_blob != NULL, "invariant");
  92   return JfrCheckpointBlobReference::make(cp_blob);
  93 }