huangcm
2025-02-24 69ed55dec4b2116a19e4cca4393cbc014fce5fb2
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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
 
#include "base/memory/ref_counted_memory.h"
 
#include <utility>
 
#include "base/logging.h"
#include "base/memory/read_only_shared_memory_region.h"
 
namespace base {
 
bool RefCountedMemory::Equals(
    const scoped_refptr<RefCountedMemory>& other) const {
  return other.get() &&
         size() == other->size() &&
         (memcmp(front(), other->front(), size()) == 0);
}
 
RefCountedMemory::RefCountedMemory() = default;
 
RefCountedMemory::~RefCountedMemory() = default;
 
const unsigned char* RefCountedStaticMemory::front() const {
  return data_;
}
 
size_t RefCountedStaticMemory::size() const {
  return length_;
}
 
RefCountedStaticMemory::~RefCountedStaticMemory() = default;
 
RefCountedBytes::RefCountedBytes() = default;
 
RefCountedBytes::RefCountedBytes(const std::vector<unsigned char>& initializer)
    : data_(initializer) {
}
 
RefCountedBytes::RefCountedBytes(const unsigned char* p, size_t size)
    : data_(p, p + size) {}
 
RefCountedBytes::RefCountedBytes(size_t size) : data_(size, 0) {}
 
scoped_refptr<RefCountedBytes> RefCountedBytes::TakeVector(
    std::vector<unsigned char>* to_destroy) {
  auto bytes = MakeRefCounted<RefCountedBytes>();
  bytes->data_.swap(*to_destroy);
  return bytes;
}
 
const unsigned char* RefCountedBytes::front() const {
  // STL will assert if we do front() on an empty vector, but calling code
  // expects a NULL.
  return size() ? &data_.front() : nullptr;
}
 
size_t RefCountedBytes::size() const {
  return data_.size();
}
 
RefCountedBytes::~RefCountedBytes() = default;
 
RefCountedString::RefCountedString() = default;
 
RefCountedString::~RefCountedString() = default;
 
// static
scoped_refptr<RefCountedString> RefCountedString::TakeString(
    std::string* to_destroy) {
  auto self = MakeRefCounted<RefCountedString>();
  to_destroy->swap(self->data_);
  return self;
}
 
const unsigned char* RefCountedString::front() const {
  return data_.empty() ? nullptr
                       : reinterpret_cast<const unsigned char*>(data_.data());
}
 
size_t RefCountedString::size() const {
  return data_.size();
}
 
RefCountedSharedMemory::RefCountedSharedMemory(
    std::unique_ptr<SharedMemory> shm,
    size_t size)
    : shm_(std::move(shm)), size_(size) {
  DCHECK(shm_);
  DCHECK(shm_->memory());
  DCHECK_GT(size_, 0U);
  DCHECK_LE(size_, shm_->mapped_size());
}
 
RefCountedSharedMemory::~RefCountedSharedMemory() = default;
 
const unsigned char* RefCountedSharedMemory::front() const {
  return static_cast<const unsigned char*>(shm_->memory());
}
 
size_t RefCountedSharedMemory::size() const {
  return size_;
}
 
RefCountedSharedMemoryMapping::RefCountedSharedMemoryMapping(
    ReadOnlySharedMemoryMapping mapping)
    : mapping_(std::move(mapping)), size_(mapping_.size()) {
  DCHECK_GT(size_, 0U);
}
 
RefCountedSharedMemoryMapping::~RefCountedSharedMemoryMapping() = default;
 
const unsigned char* RefCountedSharedMemoryMapping::front() const {
  return static_cast<const unsigned char*>(mapping_.memory());
}
 
size_t RefCountedSharedMemoryMapping::size() const {
  return size_;
}
 
// static
scoped_refptr<RefCountedSharedMemoryMapping>
RefCountedSharedMemoryMapping::CreateFromWholeRegion(
    const ReadOnlySharedMemoryRegion& region) {
  ReadOnlySharedMemoryMapping mapping = region.Map();
  if (!mapping.IsValid())
    return nullptr;
  return MakeRefCounted<RefCountedSharedMemoryMapping>(std::move(mapping));
}
 
}  //  namespace base