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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "mojo/core/ipcz_driver/ring_buffer.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include "base/check_op.h"
#include "base/containers/span.h"
#include "base/numerics/safe_math.h"
#include "mojo/core/ipcz_driver/shared_buffer_mapping.h"
namespace mojo::core::ipcz_driver {
RingBuffer::RingBuffer(scoped_refptr<SharedBufferMapping> mapping)
: mapping_(std::move(mapping)) {}
RingBuffer::~RingBuffer() = default;
size_t RingBuffer::Write(base::span<const uint8_t> source) {
auto bytes = MapRange(ComplementRange(data_range_));
const size_t first_chunk_size = std::min(source.size(), bytes.first.size());
const size_t second_chunk_size =
std::min(source.size() - first_chunk_size, bytes.second.size());
bytes.first.copy_prefix_from(source.first(first_chunk_size));
bytes.second.copy_prefix_from(
source.subspan(first_chunk_size, second_chunk_size));
const size_t write_size = first_chunk_size + second_chunk_size;
bool ok = ExtendDataRange(write_size);
DCHECK(ok);
return write_size;
}
bool RingBuffer::WriteAll(base::span<const uint8_t> source) {
if (source.size() > available_capacity()) {
return false;
}
const size_t n = Write(source);
DCHECK_EQ(n, source.size());
return true;
}
size_t RingBuffer::Read(base::span<uint8_t> target) {
const size_t n = Peek(target);
bool ok = DiscardAll(n);
DCHECK(ok);
return n;
}
bool RingBuffer::ReadAll(base::span<uint8_t> target) {
if (target.size() > data_size()) {
return false;
}
const size_t n = Read(target);
DCHECK_EQ(n, target.size());
return true;
}
size_t RingBuffer::Peek(base::span<uint8_t> target) {
auto bytes = MapRange(data_range_);
const size_t first_chunk_size = std::min(target.size(), bytes.first.size());
const size_t second_chunk_size =
std::min(target.size() - first_chunk_size, bytes.second.size());
target.copy_prefix_from(bytes.first.first(first_chunk_size));
target.subspan(first_chunk_size)
.copy_prefix_from(bytes.second.first(second_chunk_size));
return first_chunk_size + second_chunk_size;
}
bool RingBuffer::PeekAll(base::span<uint8_t> target) {
if (target.size() > data_size()) {
return false;
}
const size_t n = Peek(target);
DCHECK_EQ(n, target.size());
return true;
}
size_t RingBuffer::Discard(size_t n) {
const size_t num_bytes = std::min(n, data_size());
bool ok = DiscardAll(num_bytes);
DCHECK(ok);
return num_bytes;
}
bool RingBuffer::DiscardAll(size_t n) {
if (n > data_size()) {
return false;
}
size_t new_offset;
if (!base::CheckAdd(data_range_.offset, n).AssignIfValid(&new_offset)) {
return false;
}
data_range_ = {.offset = new_offset % capacity(), .size = data_size() - n};
return true;
}
bool RingBuffer::ExtendDataRange(size_t n) {
if (n > available_capacity()) {
return false;
}
data_range_.size += n;
return true;
}
void RingBuffer::Serialize(SerializedState& state) {
state.offset = base::checked_cast<uint32_t>(data_range_.offset);
state.size = base::checked_cast<uint32_t>(data_range_.size);
}
bool RingBuffer::Deserialize(const SerializedState& state) {
const uint32_t data_offset = state.offset;
const uint32_t data_size = state.size;
if (data_offset >= capacity() || data_size > capacity()) {
return false;
}
data_range_ = {.offset = data_offset, .size = data_size};
return true;
}
RingBuffer::SplitBytes RingBuffer::MapRange(
const RingBuffer::Range& range) const {
DCHECK_LE(range.offset, capacity());
const size_t first_chunk_size =
std::min(range.size, capacity() - range.offset);
return {
mapping().bytes().subspan(range.offset).first(first_chunk_size),
mapping().bytes().first(range.size - first_chunk_size),
};
}
RingBuffer::Range RingBuffer::ComplementRange(const Range& range) const {
const size_t buffer_size = capacity();
DCHECK_LE(range.offset, buffer_size);
DCHECK_LE(range.size, buffer_size);
const size_t end = range.offset + range.size;
return {
.offset = end >= buffer_size ? end - buffer_size : end,
.size = buffer_size - range.size,
};
}
base::span<uint8_t> RingBuffer::GetAvailableCapacityView() const {
return MapRange(ComplementRange(data_range_)).first;
}
base::span<const uint8_t> RingBuffer::GetReadableDataView() const {
return MapRange(data_range_).first;
}
} // namespace mojo::core::ipcz_driver
|