SharedMemoryMac.cpp revision 65f03d4f644ce73618e5f4f50dd694b26f55ae12
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#include "SharedMemory.h"
27
28#include "ArgumentDecoder.h"
29#include "ArgumentEncoder.h"
30#include "Arguments.h"
31#include "MachPort.h"
32#include <mach/mach_port.h>
33#include <mach/mach_vm.h>
34#include <mach/vm_map.h>
35#include <wtf/RefPtr.h>
36
37namespace WebKit {
38
39SharedMemory::Handle::Handle()
40    : m_port(MACH_PORT_NULL)
41    , m_size(0)
42{
43}
44
45SharedMemory::Handle::~Handle()
46{
47    if (m_port)
48        mach_port_deallocate(mach_task_self(), m_port);
49}
50
51bool SharedMemory::Handle::isNull() const
52{
53    return !m_port;
54}
55
56void SharedMemory::Handle::encode(CoreIPC::ArgumentEncoder* encoder) const
57{
58    encoder->encodeUInt64(m_size);
59    encoder->encode(CoreIPC::MachPort(m_port, MACH_MSG_TYPE_COPY_SEND));
60    m_port = MACH_PORT_NULL;
61}
62
63bool SharedMemory::Handle::decode(CoreIPC::ArgumentDecoder* decoder, Handle& handle)
64{
65    ASSERT(!handle.m_port);
66    ASSERT(!handle.m_size);
67
68    uint64_t size;
69    if (!decoder->decodeUInt64(size))
70        return false;
71
72    CoreIPC::MachPort machPort;
73    if (!decoder->decode(CoreIPC::Out(machPort)))
74        return false;
75
76    handle.m_size = size;
77    handle.m_port = machPort.port();
78    return true;
79}
80
81static inline void* toPointer(mach_vm_address_t address)
82{
83    return reinterpret_cast<void*>(static_cast<uintptr_t>(address));
84}
85
86static inline mach_vm_address_t toVMAddress(void* pointer)
87{
88    return static_cast<mach_vm_address_t>(reinterpret_cast<uintptr_t>(pointer));
89}
90
91PassRefPtr<SharedMemory> SharedMemory::create(size_t size)
92{
93    mach_vm_address_t address;
94    kern_return_t kr = mach_vm_allocate(mach_task_self(), &address, round_page(size), VM_FLAGS_ANYWHERE);
95    if (kr != KERN_SUCCESS)
96        return 0;
97
98    RefPtr<SharedMemory> sharedMemory(adoptRef(new SharedMemory));
99    sharedMemory->m_size = size;
100    sharedMemory->m_data = toPointer(address);
101
102    return sharedMemory.release();
103}
104
105static inline vm_prot_t machProtection(SharedMemory::Protection protection)
106{
107    switch (protection) {
108    case SharedMemory::ReadOnly:
109        return VM_PROT_READ;
110    case SharedMemory::ReadWrite:
111        return VM_PROT_READ | VM_PROT_WRITE;
112    }
113
114    ASSERT_NOT_REACHED();
115    return VM_PROT_NONE;
116}
117
118PassRefPtr<SharedMemory> SharedMemory::create(const Handle& handle, Protection protection)
119{
120    if (handle.isNull())
121        return 0;
122
123    // Map the memory.
124    vm_prot_t vmProtection = machProtection(protection);
125    mach_vm_address_t mappedAddress = 0;
126    kern_return_t kr = mach_vm_map(mach_task_self(), &mappedAddress, handle.m_size, 0, VM_FLAGS_ANYWHERE, handle.m_port, 0, false, vmProtection, vmProtection, VM_INHERIT_NONE);
127    if (kr != KERN_SUCCESS)
128        return 0;
129
130    RefPtr<SharedMemory> sharedMemory(adoptRef(new SharedMemory));
131    sharedMemory->m_size = handle.m_size;
132    sharedMemory->m_data = toPointer(mappedAddress);
133
134    return sharedMemory.release();
135}
136
137SharedMemory::~SharedMemory()
138{
139    if (!m_data)
140        return;
141
142    kern_return_t kr = mach_vm_deallocate(mach_task_self(), toVMAddress(m_data), round_page(m_size));
143    ASSERT_UNUSED(kr, kr == KERN_SUCCESS);
144}
145
146bool SharedMemory::createHandle(Handle& handle, Protection protection)
147{
148    ASSERT(!handle.m_port);
149    ASSERT(!handle.m_size);
150
151    mach_vm_address_t address = toVMAddress(m_data);
152    memory_object_size_t size = round_page(m_size);
153
154    // Create a mach port that represents the shared memory.
155    mach_port_t port;
156    kern_return_t kr = mach_make_memory_entry_64(mach_task_self(), &size, address, machProtection(protection), &port, MACH_PORT_NULL);
157    if (kr != KERN_SUCCESS)
158        return false;
159
160    handle.m_port = port;
161    handle.m_size = size;
162
163    return true;
164}
165
166unsigned SharedMemory::systemPageSize()
167{
168    return vm_page_size;
169}
170
171} // namespace WebKit
172