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.
27 #include "SharedMemory.h"
29 #include "ArgumentDecoder.h"
30 #include "ArgumentEncoder.h"
31 #include "Arguments.h"
33 #include <mach/mach_port.h>
34 #include <mach/mach_vm.h>
35 #include <mach/vm_map.h>
36 #include <wtf/RefPtr.h>
40 SharedMemory::Handle::Handle()
41 : m_port(MACH_PORT_NULL)
46 SharedMemory::Handle::~Handle()
49 mach_port_deallocate(mach_task_self(), m_port);
52 bool SharedMemory::Handle::isNull() const
57 void SharedMemory::Handle::encode(CoreIPC::ArgumentEncoder* encoder) const
59 encoder->encodeUInt64(m_size);
60 encoder->encode(CoreIPC::MachPort(m_port, MACH_MSG_TYPE_MOVE_SEND));
61 m_port = MACH_PORT_NULL;
64 bool SharedMemory::Handle::decode(CoreIPC::ArgumentDecoder* decoder, Handle& handle)
66 ASSERT(!handle.m_port);
67 ASSERT(!handle.m_size);
70 if (!decoder->decodeUInt64(size))
73 CoreIPC::MachPort machPort;
74 if (!decoder->decode(CoreIPC::Out(machPort)))
78 handle.m_port = machPort.port();
82 static inline void* toPointer(mach_vm_address_t address)
84 return reinterpret_cast<void*>(static_cast<uintptr_t>(address));
87 static inline mach_vm_address_t toVMAddress(void* pointer)
89 return static_cast<mach_vm_address_t>(reinterpret_cast<uintptr_t>(pointer));
92 PassRefPtr<SharedMemory> SharedMemory::create(size_t size)
96 mach_vm_address_t address;
97 kern_return_t kr = mach_vm_allocate(mach_task_self(), &address, round_page(size), VM_FLAGS_ANYWHERE);
98 if (kr != KERN_SUCCESS) {
99 LOG_ERROR("Failed to allocate mach_vm_allocate shared memory (%zu bytes) [error code: %x]", size, kr);
103 // Create a Mach port that represents the shared memory.
105 memory_object_size_t memoryObjectSize = round_page(size);
106 kr = mach_make_memory_entry_64(mach_task_self(), &memoryObjectSize, address, VM_PROT_DEFAULT, &port, MACH_PORT_NULL);
108 if (kr != KERN_SUCCESS) {
109 LOG_ERROR("Failed to create a mach port for shared memory [error code: %x]", kr);
110 mach_vm_deallocate(mach_task_self(), address, round_page(size));
114 ASSERT(memoryObjectSize >= round_page(size));
116 RefPtr<SharedMemory> sharedMemory(adoptRef(new SharedMemory));
117 sharedMemory->m_size = size;
118 sharedMemory->m_data = toPointer(address);
119 sharedMemory->m_port = port;
121 return sharedMemory.release();
124 static inline vm_prot_t machProtection(SharedMemory::Protection protection)
126 switch (protection) {
127 case SharedMemory::ReadOnly:
129 case SharedMemory::ReadWrite:
130 return VM_PROT_READ | VM_PROT_WRITE;
133 ASSERT_NOT_REACHED();
137 PassRefPtr<SharedMemory> SharedMemory::create(const Handle& handle, Protection protection)
143 vm_prot_t vmProtection = machProtection(protection);
144 mach_vm_address_t mappedAddress = 0;
145 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);
146 if (kr != KERN_SUCCESS)
149 RefPtr<SharedMemory> sharedMemory(adoptRef(new SharedMemory));
150 sharedMemory->m_size = handle.m_size;
151 sharedMemory->m_data = toPointer(mappedAddress);
152 sharedMemory->m_port = MACH_PORT_NULL;
154 return sharedMemory.release();
157 SharedMemory::~SharedMemory()
160 kern_return_t kr = mach_vm_deallocate(mach_task_self(), toVMAddress(m_data), round_page(m_size));
161 ASSERT_UNUSED(kr, kr == KERN_SUCCESS);
165 kern_return_t kr = mach_port_deallocate(mach_task_self(), m_port);
166 ASSERT_UNUSED(kr, kr == KERN_SUCCESS);
170 bool SharedMemory::createHandle(Handle& handle, Protection protection)
172 ASSERT(!handle.m_port);
173 ASSERT(!handle.m_size);
175 mach_vm_address_t address = toVMAddress(m_data);
176 memory_object_size_t size = round_page(m_size);
180 if (protection == ReadWrite && m_port) {
181 // Just re-use the port we have.
183 if (mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_SEND, 1) != KERN_SUCCESS)
186 // Create a mach port that represents the shared memory.
187 kern_return_t kr = mach_make_memory_entry_64(mach_task_self(), &size, address, machProtection(protection), &port, MACH_PORT_NULL);
188 if (kr != KERN_SUCCESS)
191 ASSERT(size >= round_page(m_size));
194 handle.m_port = port;
195 handle.m_size = size;
200 unsigned SharedMemory::systemPageSize()
205 } // namespace WebKit