summaryrefslogtreecommitdiffstats
path: root/src/zstd/contrib/pzstd/utils/Buffer.h
blob: f69c3b4d9f7abd9d293d0005af4c7f99827e8703 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Copyright (c) 2016-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under both the BSD-style license (found in the
 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
 * in the COPYING file in the root directory of this source tree).
 */
#pragma once

#include "utils/Range.h"

#include <array>
#include <cstddef>
#include <memory>

namespace pzstd {

/**
 * A `Buffer` has a pointer to a shared buffer, and a range of the buffer that
 * it owns.
 * The idea is that you can allocate one buffer, and write chunks into it
 * and break off those chunks.
 * The underlying buffer is reference counted, and will be destroyed when all
 * `Buffer`s that reference it are destroyed.
 */
class Buffer {
  std::shared_ptr<unsigned char> buffer_;
  MutableByteRange range_;

  static void delete_buffer(unsigned char* buffer) {
    delete[] buffer;
  }

 public:
  /// Construct an empty buffer that owns no data.
  explicit Buffer() {}

  /// Construct a `Buffer` that owns a new underlying buffer of size `size`.
  explicit Buffer(std::size_t size)
      : buffer_(new unsigned char[size], delete_buffer),
        range_(buffer_.get(), buffer_.get() + size) {}

  explicit Buffer(std::shared_ptr<unsigned char> buffer, MutableByteRange data)
      : buffer_(buffer), range_(data) {}

  Buffer(Buffer&&) = default;
  Buffer& operator=(Buffer&&) & = default;

  /**
   * Splits the data into two pieces: [begin, begin + n), [begin + n, end).
   * Their data both points into the same underlying buffer.
   * Modifies the original `Buffer` to point to only [begin + n, end).
   *
   * @param n  The offset to split at.
   * @returns  A buffer that owns the data [begin, begin + n).
   */
  Buffer splitAt(std::size_t n) {
    auto firstPiece = range_.subpiece(0, n);
    range_.advance(n);
    return Buffer(buffer_, firstPiece);
  }

  /// Modifies the buffer to point to the range [begin + n, end).
  void advance(std::size_t n) {
    range_.advance(n);
  }

  /// Modifies the buffer to point to the range [begin, end - n).
  void subtract(std::size_t n) {
    range_.subtract(n);
  }

  /// Returns a read only `Range` pointing to the `Buffer`s data.
  ByteRange range() const {
    return range_;
  }
  /// Returns a mutable `Range` pointing to the `Buffer`s data.
  MutableByteRange range() {
    return range_;
  }

  const unsigned char* data() const {
    return range_.data();
  }

  unsigned char* data() {
    return range_.data();
  }

  std::size_t size() const {
    return range_.size();
  }

  bool empty() const {
    return range_.empty();
  }
};
}