blob: 2f783cf9173d2cc503f2b4daa229135a65fb0a35 [file] [log] [blame]
Primiano Tuccia6166482017-11-20 13:05:45 +00001/*
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 TRACING_SRC_IPC_POSIX_SHARED_MEMORY_H_
18#define TRACING_SRC_IPC_POSIX_SHARED_MEMORY_H_
19
20#include <stddef.h>
21
22#include <memory>
23
24#include "base/scoped_file.h"
25#include "tracing/core/shared_memory.h"
26
27namespace perfetto {
28
29// Implements the SharedMemory and its factory for the posix-based transport.
Primiano Tuccia6166482017-11-20 13:05:45 +000030class PosixSharedMemory : public SharedMemory {
31 public:
32 class Factory : public SharedMemory::Factory {
33 public:
34 ~Factory() override;
35 std::unique_ptr<SharedMemory> CreateSharedMemory(size_t) override;
36 };
37
Primiano Tucci69ca1262017-11-20 19:42:22 +000038 // Create a brand new SHM region (the service uses this).
39 static std::unique_ptr<PosixSharedMemory> Create(size_t size);
40
41 // Mmaps a file descriptor to an existing SHM region (the producer uses this).
42 static std::unique_ptr<PosixSharedMemory> AttachToFd(base::ScopedFile);
43
44 ~PosixSharedMemory() override;
45
46 int fd() const { return fd_.get(); }
47
Primiano Tuccia6166482017-11-20 13:05:45 +000048 // SharedMemory implementation.
Primiano Tucci69ca1262017-11-20 19:42:22 +000049 void* start() const override { return start_; }
50 size_t size() const override { return size_; }
51
52 private:
53 static std::unique_ptr<PosixSharedMemory> MapFD(base::ScopedFile, size_t);
54
55 PosixSharedMemory(void* start, size_t size, base::ScopedFile);
56 PosixSharedMemory(const PosixSharedMemory&) = delete;
57 PosixSharedMemory& operator=(const PosixSharedMemory&) = delete;
58
59 void* const start_;
60 const size_t size_;
61 base::ScopedFile fd_;
Primiano Tuccia6166482017-11-20 13:05:45 +000062};
63
64} // namespace perfetto
65
66#endif // TRACING_SRC_IPC_POSIX_SHARED_MEMORY_H_