• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/memory/ref_counted_memory.h"
6 
7 #include <utility>
8 
9 #include "base/check_op.h"
10 #include "base/memory/read_only_shared_memory_region.h"
11 
12 namespace base {
13 
Equals(const scoped_refptr<RefCountedMemory> & other) const14 bool RefCountedMemory::Equals(
15     const scoped_refptr<RefCountedMemory>& other) const {
16   return other && AsSpan() == other->AsSpan();
17 }
18 
19 RefCountedMemory::RefCountedMemory() = default;
20 RefCountedMemory::~RefCountedMemory() = default;
21 
22 RefCountedStaticMemory::RefCountedStaticMemory() = default;
23 RefCountedStaticMemory::~RefCountedStaticMemory() = default;
24 
RefCountedStaticMemory(base::span<const uint8_t> bytes)25 RefCountedStaticMemory::RefCountedStaticMemory(base::span<const uint8_t> bytes)
26     : bytes_(bytes) {}
27 
AsSpan() const28 base::span<const uint8_t> RefCountedStaticMemory::AsSpan() const {
29   return bytes_;
30 }
31 
32 RefCountedBytes::RefCountedBytes() = default;
33 RefCountedBytes::~RefCountedBytes() = default;
34 
RefCountedBytes(std::vector<uint8_t> initializer)35 RefCountedBytes::RefCountedBytes(std::vector<uint8_t> initializer)
36     : bytes_(std::move(initializer)) {}
37 
RefCountedBytes(base::span<const uint8_t> initializer)38 RefCountedBytes::RefCountedBytes(base::span<const uint8_t> initializer)
39     : bytes_(initializer.begin(), initializer.end()) {}
40 
RefCountedBytes(size_t size)41 RefCountedBytes::RefCountedBytes(size_t size) : bytes_(size, 0u) {}
42 
AsSpan() const43 base::span<const uint8_t> RefCountedBytes::AsSpan() const {
44   return bytes_;
45 }
46 
47 RefCountedString::RefCountedString() = default;
48 RefCountedString::~RefCountedString() = default;
49 
RefCountedString(std::string str)50 RefCountedString::RefCountedString(std::string str) : string_(std::move(str)) {}
51 
AsSpan() const52 base::span<const uint8_t> RefCountedString::AsSpan() const {
53   return base::as_byte_span(string_);
54 }
55 
56 RefCountedString16::RefCountedString16() = default;
57 RefCountedString16::~RefCountedString16() = default;
58 
RefCountedString16(std::u16string str)59 RefCountedString16::RefCountedString16(std::u16string str)
60     : string_(std::move(str)) {}
61 
AsSpan() const62 base::span<const uint8_t> RefCountedString16::AsSpan() const {
63   return base::as_byte_span(string_);
64 }
65 
RefCountedSharedMemoryMapping(ReadOnlySharedMemoryMapping mapping)66 RefCountedSharedMemoryMapping::RefCountedSharedMemoryMapping(
67     ReadOnlySharedMemoryMapping mapping)
68     : mapping_(std::move(mapping)) {
69   DCHECK_GT(mapping_.size(), 0u);
70 }
71 
72 RefCountedSharedMemoryMapping::~RefCountedSharedMemoryMapping() = default;
73 
AsSpan() const74 base::span<const uint8_t> RefCountedSharedMemoryMapping::AsSpan() const {
75   return mapping_.GetMemoryAsSpan<const uint8_t>();
76 }
77 
78 // static
79 scoped_refptr<RefCountedSharedMemoryMapping>
CreateFromWholeRegion(const ReadOnlySharedMemoryRegion & region)80 RefCountedSharedMemoryMapping::CreateFromWholeRegion(
81     const ReadOnlySharedMemoryRegion& region) {
82   ReadOnlySharedMemoryMapping mapping = region.Map();
83   if (!mapping.IsValid())
84     return nullptr;
85   return MakeRefCounted<RefCountedSharedMemoryMapping>(std::move(mapping));
86 }
87 
88 }  //  namespace base
89