1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26#ifndef SharedMemory_h
27#define SharedMemory_h
28
29#include <wtf/Noncopyable.h>
30#include <wtf/PassRefPtr.h>
31#include <wtf/RefCounted.h>
32
33#if PLATFORM(QT) || PLATFORM(GTK) || PLATFORM(EFL)
34#include "Attachment.h"
35#include <wtf/text/WTFString.h>
36#endif
37
38namespace CoreIPC {
39    class ArgumentDecoder;
40    class ArgumentEncoder;
41}
42
43namespace WebKit {
44
45class SharedMemory : public RefCounted<SharedMemory> {
46public:
47    enum Protection {
48        ReadOnly,
49        ReadWrite
50    };
51
52    class Handle {
53        WTF_MAKE_NONCOPYABLE(Handle);
54    public:
55        Handle();
56        ~Handle();
57
58        bool isNull() const;
59
60        void encode(CoreIPC::ArgumentEncoder&) const;
61        static bool decode(CoreIPC::ArgumentDecoder&, Handle&);
62
63#if USE(UNIX_DOMAIN_SOCKETS)
64        CoreIPC::Attachment releaseToAttachment() const;
65        void adoptFromAttachment(int fileDescriptor, size_t);
66#endif
67    private:
68        friend class SharedMemory;
69#if OS(DARWIN)
70        mutable mach_port_t m_port;
71#elif OS(WINDOWS)
72        mutable HANDLE m_handle;
73#elif USE(UNIX_DOMAIN_SOCKETS)
74        mutable int m_fileDescriptor;
75#endif
76        size_t m_size;
77    };
78
79    // Create a shared memory object with the given size. Will return 0 on failure.
80    static PassRefPtr<SharedMemory> create(size_t);
81
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);
84
85    // Create a shared memory object with the given size by vm_copy'ing the given buffer.
86    // Will return 0 on failure.
87    static PassRefPtr<SharedMemory> createFromVMBuffer(void*, size_t);
88
89#if OS(WINDOWS)
90    static PassRefPtr<SharedMemory> adopt(HANDLE, size_t, Protection);
91#endif
92
93    ~SharedMemory();
94
95    bool createHandle(Handle&, Protection);
96
97    size_t size() const { return m_size; }
98    void* data() const { return m_data; }
99#if OS(WINDOWS)
100    HANDLE handle() const { return m_handle; }
101#endif
102
103    // Creates a copy-on-write copy of the first |size| bytes.
104    PassRefPtr<SharedMemory> createCopyOnWriteCopy(size_t) const;
105
106    // Return the system page size in bytes.
107    static unsigned systemPageSize();
108
109private:
110    size_t m_size;
111    void* m_data;
112    bool m_shouldVMDeallocateData;
113
114#if OS(DARWIN)
115    mach_port_t m_port;
116#elif OS(WINDOWS)
117    HANDLE m_handle;
118#elif USE(UNIX_DOMAIN_SOCKETS)
119    int m_fileDescriptor;
120#endif
121};
122
123};
124
125#endif // SharedMemory_h
126