• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SRC_TRACING_IPC_POSIX_SHARED_MEMORY_H_
18 #define SRC_TRACING_IPC_POSIX_SHARED_MEMORY_H_
19 
20 #include "perfetto/base/build_config.h"
21 
22 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) ||   \
23     PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) || \
24     PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE)
25 
26 #include <stddef.h>
27 
28 #include <memory>
29 
30 #include "perfetto/base/build_config.h"
31 #include "perfetto/ext/base/scoped_file.h"
32 #include "perfetto/ext/tracing/core/shared_memory.h"
33 
34 namespace perfetto {
35 
36 // Implements the SharedMemory and its factory for the posix-based transport.
37 class PosixSharedMemory : public SharedMemory {
38  public:
39   class Factory : public SharedMemory::Factory {
40    public:
41     ~Factory() override;
42     std::unique_ptr<SharedMemory> CreateSharedMemory(size_t) override;
43   };
44 
45   // Create a brand new SHM region.
46   static std::unique_ptr<PosixSharedMemory> Create(size_t size);
47 
48   // Mmaps a file descriptor to an existing SHM region. If
49   // |require_seals_if_supported| is true and the system supports
50   // memfd_create(), the FD is required to be a sealed memfd with F_SEAL_SEAL,
51   // F_SEAL_GROW, and F_SEAL_SHRINK seals set (otherwise, nullptr is returned).
52   // May also return nullptr if mapping fails for another reason (e.g. OOM).
53   static std::unique_ptr<PosixSharedMemory> AttachToFd(
54       base::ScopedFile,
55       bool require_seals_if_supported = true);
56 
57   ~PosixSharedMemory() override;
58 
fd()59   int fd() const { return fd_.get(); }
60 
61   // SharedMemory implementation.
start()62   void* start() const override { return start_; }
size()63   size_t size() const override { return size_; }
64 
65  private:
66   static std::unique_ptr<PosixSharedMemory> MapFD(base::ScopedFile, size_t);
67 
68   PosixSharedMemory(void* start, size_t size, base::ScopedFile);
69   PosixSharedMemory(const PosixSharedMemory&) = delete;
70   PosixSharedMemory& operator=(const PosixSharedMemory&) = delete;
71 
72   void* const start_;
73   const size_t size_;
74   base::ScopedFile fd_;
75 };
76 
77 }  // namespace perfetto
78 
79 #endif  // OS_LINUX || OS_ANDROID || OS_APPLE
80 #endif  // SRC_TRACING_IPC_POSIX_SHARED_MEMORY_H_
81