summaryrefslogtreecommitdiff
path: root/chromium/third_party/ipcz/src/ipcz/driver_memory.cc
blob: fe4e18155a2afcc2f0087626d2178606f5064593 (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
// Copyright 2022 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 "ipcz/driver_memory.h"

#include <algorithm>
#include <cstdint>
#include <limits>
#include <utility>

#include "ipcz/ipcz.h"
#include "third_party/abseil-cpp/absl/base/macros.h"

namespace ipcz {

DriverMemory::DriverMemory() = default;

DriverMemory::DriverMemory(DriverObject memory) : memory_(std::move(memory)) {
  if (memory_.is_valid()) {
    IpczSharedMemoryInfo info = {/*.size =*/ sizeof(info)};
    IpczResult result = memory_.driver()->GetSharedMemoryInfo(
        memory_.handle(), IPCZ_NO_FLAGS, nullptr, &info);
    ABSL_ASSERT(result == IPCZ_RESULT_OK);
    size_ = info.region_num_bytes;
  }
}

DriverMemory::DriverMemory(const IpczDriver& driver, size_t num_bytes)
    : size_(num_bytes) {
  ABSL_ASSERT(num_bytes > 0);
  IpczDriverHandle handle;
  IpczResult result =
      driver.AllocateSharedMemory(num_bytes, IPCZ_NO_FLAGS, nullptr, &handle);
  ABSL_ASSERT(result == IPCZ_RESULT_OK);
  memory_ = DriverObject(driver, handle);
}

DriverMemory::DriverMemory(DriverMemory&& other) = default;

DriverMemory& DriverMemory::operator=(DriverMemory&& other) = default;

DriverMemory::~DriverMemory() = default;

DriverMemory DriverMemory::Clone() {
  ABSL_ASSERT(is_valid());

  IpczDriverHandle handle;
  IpczResult result = memory_.driver()->DuplicateSharedMemory(
      memory_.handle(), 0, nullptr, &handle);
  ABSL_ASSERT(result == IPCZ_RESULT_OK);

  return DriverMemory(DriverObject(*memory_.driver(), handle));
}

DriverMemoryMapping DriverMemory::Map() {
  ABSL_ASSERT(is_valid());
  void* address;
  IpczDriverHandle mapping_handle;
  IpczResult result = memory_.driver()->MapSharedMemory(
      memory_.handle(), 0, nullptr, &address, &mapping_handle);
  ABSL_ASSERT(result == IPCZ_RESULT_OK);
  return DriverMemoryMapping(*memory_.driver(), mapping_handle, address, size_);
}

DriverMemoryWithMapping::DriverMemoryWithMapping() = default;

DriverMemoryWithMapping::DriverMemoryWithMapping(DriverMemory memory,
                                                 DriverMemoryMapping mapping)
    : memory(std::move(memory)), mapping(std::move(mapping)) {}

DriverMemoryWithMapping::DriverMemoryWithMapping(DriverMemoryWithMapping&&) =
    default;

DriverMemoryWithMapping& DriverMemoryWithMapping::operator=(
    DriverMemoryWithMapping&&) = default;

DriverMemoryWithMapping::~DriverMemoryWithMapping() = default;

}  // namespace ipcz