1 | /* |
2 | * Copyright (C) 2017 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. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include "IsoAlignedMemoryAllocator.h" |
28 | #include "MarkedBlock.h" |
29 | |
30 | namespace JSC { |
31 | |
32 | IsoAlignedMemoryAllocator::IsoAlignedMemoryAllocator() |
33 | { |
34 | } |
35 | |
36 | IsoAlignedMemoryAllocator::~IsoAlignedMemoryAllocator() |
37 | { |
38 | for (unsigned i = 0; i < m_blocks.size(); ++i) { |
39 | void* block = m_blocks[i]; |
40 | if (!m_committed[i]) |
41 | WTF::fastCommitAlignedMemory(block, MarkedBlock::blockSize); |
42 | fastAlignedFree(block); |
43 | } |
44 | } |
45 | |
46 | void* IsoAlignedMemoryAllocator::tryAllocateAlignedMemory(size_t alignment, size_t size) |
47 | { |
48 | // Since this is designed specially for IsoSubspace, we know that we will only be asked to |
49 | // allocate MarkedBlocks. |
50 | RELEASE_ASSERT(alignment == MarkedBlock::blockSize); |
51 | RELEASE_ASSERT(size == MarkedBlock::blockSize); |
52 | |
53 | auto locker = holdLock(m_lock); |
54 | |
55 | m_firstUncommitted = m_committed.findBit(m_firstUncommitted, false); |
56 | if (m_firstUncommitted < m_blocks.size()) { |
57 | m_committed[m_firstUncommitted] = true; |
58 | void* result = m_blocks[m_firstUncommitted]; |
59 | WTF::fastCommitAlignedMemory(result, MarkedBlock::blockSize); |
60 | return result; |
61 | } |
62 | |
63 | void* result = tryFastAlignedMalloc(MarkedBlock::blockSize, MarkedBlock::blockSize); |
64 | if (!result) |
65 | return nullptr; |
66 | unsigned index = m_blocks.size(); |
67 | m_blocks.append(result); |
68 | m_blockIndices.add(result, index); |
69 | if (m_blocks.capacity() != m_committed.size()) |
70 | m_committed.resize(m_blocks.capacity()); |
71 | m_committed[index] = true; |
72 | return result; |
73 | } |
74 | |
75 | void IsoAlignedMemoryAllocator::freeAlignedMemory(void* basePtr) |
76 | { |
77 | auto locker = holdLock(m_lock); |
78 | |
79 | auto iter = m_blockIndices.find(basePtr); |
80 | RELEASE_ASSERT(iter != m_blockIndices.end()); |
81 | unsigned index = iter->value; |
82 | m_committed[index] = false; |
83 | m_firstUncommitted = std::min(index, m_firstUncommitted); |
84 | WTF::fastDecommitAlignedMemory(basePtr, MarkedBlock::blockSize); |
85 | } |
86 | |
87 | void IsoAlignedMemoryAllocator::dump(PrintStream& out) const |
88 | { |
89 | out.print("Iso(" , RawPointer(this), ")" ); |
90 | } |
91 | |
92 | void* IsoAlignedMemoryAllocator::tryAllocateMemory(size_t) |
93 | { |
94 | RELEASE_ASSERT_NOT_REACHED(); |
95 | } |
96 | |
97 | void IsoAlignedMemoryAllocator::freeMemory(void*) |
98 | { |
99 | RELEASE_ASSERT_NOT_REACHED(); |
100 | } |
101 | |
102 | void* IsoAlignedMemoryAllocator::tryReallocateMemory(void*, size_t) |
103 | { |
104 | RELEASE_ASSERT_NOT_REACHED(); |
105 | } |
106 | |
107 | } // namespace JSC |
108 | |
109 | |