1/*
2 * Copyright (C) 2010-2018 Apple Inc. All rights reserved.
3 * Copyright (C) 2017 Sony Interactive Entertainment Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24 * THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include <wtf/Forward.h>
30#include <wtf/Noncopyable.h>
31#include <wtf/RefCounted.h>
32
33#if USE(UNIX_DOMAIN_SOCKETS)
34#include "Attachment.h"
35#include <wtf/Optional.h>
36#endif
37
38#if OS(WINDOWS)
39#include <windows.h>
40#endif
41
42namespace IPC {
43class Decoder;
44class Encoder;
45}
46
47#if OS(DARWIN)
48namespace WTF {
49class MachSendRight;
50}
51#endif
52
53namespace WebKit {
54
55class SharedMemory : public RefCounted<SharedMemory> {
56public:
57 enum class Protection {
58 ReadOnly,
59 ReadWrite
60 };
61
62 class Handle {
63 WTF_MAKE_NONCOPYABLE(Handle);
64 public:
65 Handle();
66 ~Handle();
67 Handle(Handle&&);
68 Handle& operator=(Handle&&);
69
70 bool isNull() const;
71
72 void clear();
73
74 void encode(IPC::Encoder&) const;
75 static bool decode(IPC::Decoder&, Handle&);
76
77#if USE(UNIX_DOMAIN_SOCKETS)
78 IPC::Attachment releaseAttachment() const;
79 void adoptAttachment(IPC::Attachment&&);
80#endif
81 private:
82 friend class SharedMemory;
83#if USE(UNIX_DOMAIN_SOCKETS)
84 mutable IPC::Attachment m_attachment;
85#elif OS(DARWIN)
86 mutable mach_port_t m_port { MACH_PORT_NULL };
87 size_t m_size;
88#elif OS(WINDOWS)
89 mutable HANDLE m_handle;
90 size_t m_size;
91#endif
92 };
93
94 static RefPtr<SharedMemory> allocate(size_t);
95 static RefPtr<SharedMemory> create(void*, size_t, Protection);
96 static RefPtr<SharedMemory> map(const Handle&, Protection);
97#if USE(UNIX_DOMAIN_SOCKETS)
98 static RefPtr<SharedMemory> wrapMap(void*, size_t, int fileDescriptor);
99#elif OS(DARWIN)
100 static RefPtr<SharedMemory> wrapMap(void*, size_t, Protection);
101#endif
102#if OS(WINDOWS)
103 static RefPtr<SharedMemory> adopt(HANDLE, size_t, Protection);
104#endif
105
106 ~SharedMemory();
107
108 bool createHandle(Handle&, Protection);
109
110 size_t size() const { return m_size; }
111 void* data() const
112 {
113 ASSERT(m_data);
114 return m_data;
115 }
116
117#if OS(WINDOWS)
118 HANDLE handle() const { return m_handle; }
119#endif
120
121 // Return the system page size in bytes.
122 static unsigned systemPageSize();
123
124private:
125#if OS(DARWIN)
126 WTF::MachSendRight createSendRight(Protection) const;
127#endif
128
129 size_t m_size;
130 void* m_data;
131#if PLATFORM(COCOA)
132 Protection m_protection;
133#endif
134
135#if USE(UNIX_DOMAIN_SOCKETS)
136 Optional<int> m_fileDescriptor;
137 bool m_isWrappingMap { false };
138#elif OS(DARWIN)
139 mach_port_t m_port { MACH_PORT_NULL };
140#elif OS(WINDOWS)
141 HANDLE m_handle;
142#endif
143};
144
145};
146