2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef SharedMemory_h
27 #define SharedMemory_h
29 #include <wtf/Noncopyable.h>
30 #include <wtf/PassRefPtr.h>
31 #include <wtf/RefCounted.h>
33 #if PLATFORM(QT) || PLATFORM(GTK)
34 #include "Attachment.h"
35 #include <wtf/text/WTFString.h>
39 class ArgumentDecoder;
40 class ArgumentEncoder;
45 class SharedMemory : public RefCounted<SharedMemory> {
53 WTF_MAKE_NONCOPYABLE(Handle);
60 void encode(CoreIPC::ArgumentEncoder*) const;
61 static bool decode(CoreIPC::ArgumentDecoder*, Handle&);
63 #if USE(UNIX_DOMAIN_SOCKETS)
64 CoreIPC::Attachment releaseToAttachment() const;
65 void adoptFromAttachment(int fileDescriptor, size_t);
68 friend class SharedMemory;
70 mutable mach_port_t m_port;
72 mutable HANDLE m_handle;
73 #elif USE(UNIX_DOMAIN_SOCKETS)
74 mutable int m_fileDescriptor;
79 // Create a shared memory object with the given size. Will return 0 on failure.
80 static PassRefPtr<SharedMemory> create(size_t);
82 // Create a shared memory object from the given handle and the requested protection. Will return 0 on failure.
83 static PassRefPtr<SharedMemory> create(const Handle&, Protection);
86 static PassRefPtr<SharedMemory> adopt(HANDLE, size_t, Protection);
91 bool createHandle(Handle&, Protection);
93 size_t size() const { return m_size; }
94 void* data() const { return m_data; }
96 HANDLE handle() const { return m_handle; }
99 // Creates a copy-on-write copy of the first |size| bytes.
100 PassRefPtr<SharedMemory> createCopyOnWriteCopy(size_t) const;
102 // Return the system page size in bytes.
103 static unsigned systemPageSize();
112 #elif USE(UNIX_DOMAIN_SOCKETS)
113 int m_fileDescriptor;
119 #endif // SharedMemory_h