<commit_before>/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SRC_TRACE_PROCESSOR_TRACE_BLOB_VIEW_H_
#define SRC_TRACE_PROCESSOR_TRACE_BLOB_VIEW_H_

#include <stddef.h>
#include <stdint.h>

#include <limits>
#include <memory>

#include "perfetto/base/logging.h"

namespace perfetto {
namespace trace_processor {

// This class is an equivalent of std::string_view for trace binary data.
// The main difference is that this class has also shared ownership of a portion
// of the raw trace.
// The underlying buffer will be freed once all the TraceBlobViews that refer
// to the same buffer have passed through the pipeline and been parsed.
class TraceBlobView {
 public:
  TraceBlobView(std::unique_ptr<uint8_t[]> buffer, size_t offset, size_t length)
      : shbuf_(SharedBuf(std::move(buffer))),
        offset_(static_cast<uint32_t>(offset)),
        length_(static_cast<uint32_t>(length)) {
    PERFETTO_DCHECK(offset <= std::numeric_limits<uint32_t>::max());
    PERFETTO_DCHECK(length <= std::numeric_limits<uint32_t>::max());
  }

  // Allow std::move().
  TraceBlobView(TraceBlobView&&) noexcept = default;
  TraceBlobView& operator=(TraceBlobView&&) = default;

  // Disable implicit copy.
  TraceBlobView(const TraceBlobView&) = delete;
  TraceBlobView& operator=(const TraceBlobView&) = delete;

  TraceBlobView slice(size_t offset, size_t length) {
    PERFETTO_DCHECK(offset + length <= offset_ + length_);
    return TraceBlobView(shbuf_, offset, length);
  }

  bool operator==(const TraceBlobView& rhs) const {
    return (shbuf_ == rhs.shbuf_) && (offset_ == rhs.offset_) &&
           (length_ == rhs.length_);
  }
  bool operator!=(const TraceBlobView& rhs) const { return !(*this == rhs); }

  inline const uint8_t* data() const { return start() + offset_; }

  size_t offset_of(const uint8_t* data) const {
    PERFETTO_DCHECK(data >= start() && data < (start() + offset_ + length_));
    return static_cast<size_t>(data - start());
  }

  // const std::shared_ptr<uint8_t[]>& buffer() const { return shbuf_; }
  size_t length() const { return length_; }

 private:
  // An equivalent to std::shared_ptr<uint8_t>, with the differnce that:
  // - Supports array types, available for shared_ptr only in C++17.
  // - Is not thread safe, which is not needed for our purposes.<commit_msg>trace_processor: Fix parsing of empty events<commit_after>/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SRC_TRACE_PROCESSOR_TRACE_BLOB_VIEW_H_
#define SRC_TRACE_PROCESSOR_TRACE_BLOB_VIEW_H_

#include <stddef.h>
#include <stdint.h>

#include <limits>
#include <memory>

#include "perfetto/base/logging.h"

namespace perfetto {
namespace trace_processor {

// This class is an equivalent of std::string_view for trace binary data.
// The main difference is that this class has also shared ownership of a portion
// of the raw trace.
// The underlying buffer will be freed once all the TraceBlobViews that refer
// to the same buffer have passed through the pipeline and been parsed.
class TraceBlobView {
 public:
  TraceBlobView(std::unique_ptr<uint8_t[]> buffer, size_t offset, size_t length)
      : shbuf_(SharedBuf(std::move(buffer))),
        offset_(static_cast<uint32_t>(offset)),
        length_(static_cast<uint32_t>(length)) {
    PERFETTO_DCHECK(offset <= std::numeric_limits<uint32_t>::max());
    PERFETTO_DCHECK(length <= std::numeric_limits<uint32_t>::max());
  }

  // Allow std::move().
  TraceBlobView(TraceBlobView&&) noexcept = default;
  TraceBlobView& operator=(TraceBlobView&&) = default;

  // Disable implicit copy.
  TraceBlobView(const TraceBlobView&) = delete;
  TraceBlobView& operator=(const TraceBlobView&) = delete;

  TraceBlobView slice(size_t offset, size_t length) {
    PERFETTO_DCHECK(offset + length <= offset_ + length_);
    return TraceBlobView(shbuf_, offset, length);
  }

  bool operator==(const TraceBlobView& rhs) const {
    return (shbuf_ == rhs.shbuf_) && (offset_ == rhs.offset_) &&
           (length_ == rhs.length_);
  }
  bool operator!=(const TraceBlobView& rhs) const { return !(*this == rhs); }

  inline const uint8_t* data() const { return start() + offset_; }

  size_t offset_of(const uint8_t* data) const {
    // When a field is size 0, data can be equal to start() + offset_ + length_.
    PERFETTO_DCHECK(data >= start() && data <= (start() + offset_ + length_));
    return static_cast<size_t>(data - start());
  }

  // const std::shared_ptr<uint8_t[]>& buffer() const { return shbuf_; }
  size_t length() const { return length_; }

 private:
  // An equivalent to std::shared_ptr<uint8_t>, with the differnce that:
  // - Supports array types, available for shared_ptr only in C++17.
  // - Is not thread safe, which is not needed for our purposes.
{'fraction_non_alphanumeric': 0.11045029736618521, 'fraction_numerical': 0.00798640611724724, 'mean_word_length': 4.236654804270462, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '915455', 'n_tokens_mistral': 1791, 'n_tokens_neox': 1744, 'n_words': 592}