| 1 | /* |
| 2 | * Copyright (C) 2016-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 | #pragma once |
| 27 | |
| 28 | #if ENABLE(WEBASSEMBLY) |
| 29 | |
| 30 | #include "WasmMemoryMode.h" |
| 31 | #include "WasmPageCount.h" |
| 32 | |
| 33 | #include <wtf/CagedPtr.h> |
| 34 | #include <wtf/Expected.h> |
| 35 | #include <wtf/Function.h> |
| 36 | #include <wtf/RefCounted.h> |
| 37 | #include <wtf/RefPtr.h> |
| 38 | #include <wtf/Vector.h> |
| 39 | #include <wtf/WeakPtr.h> |
| 40 | |
| 41 | namespace WTF { |
| 42 | class PrintStream; |
| 43 | } |
| 44 | |
| 45 | namespace JSC { |
| 46 | |
| 47 | namespace Wasm { |
| 48 | |
| 49 | class Instance; |
| 50 | |
| 51 | class Memory : public RefCounted<Memory> { |
| 52 | WTF_MAKE_NONCOPYABLE(Memory); |
| 53 | WTF_MAKE_FAST_ALLOCATED; |
| 54 | public: |
| 55 | void dump(WTF::PrintStream&) const; |
| 56 | |
| 57 | explicit operator bool() const { return !!m_memory; } |
| 58 | |
| 59 | enum NotifyPressure { NotifyPressureTag }; |
| 60 | enum SyncTryToReclaim { SyncTryToReclaimTag }; |
| 61 | enum GrowSuccess { GrowSuccessTag }; |
| 62 | |
| 63 | static Ref<Memory> create(); |
| 64 | static RefPtr<Memory> tryCreate(PageCount initial, PageCount maximum, WTF::Function<void(NotifyPressure)>&& notifyMemoryPressure, WTF::Function<void(SyncTryToReclaim)>&& syncTryToReclaimMemory, WTF::Function<void(GrowSuccess, PageCount, PageCount)>&& growSuccessCallback); |
| 65 | |
| 66 | ~Memory(); |
| 67 | |
| 68 | static size_t fastMappedRedzoneBytes(); |
| 69 | static size_t fastMappedBytes(); // Includes redzone. |
| 70 | static bool addressIsInActiveFastMemory(void*); |
| 71 | |
| 72 | void* memory() const { ASSERT(m_memory.getMayBeNull(size()) == m_memory.getUnsafe()); return m_memory.getMayBeNull(size()); } |
| 73 | size_t size() const { return m_size; } |
| 74 | PageCount sizeInPages() const { return PageCount::fromBytes(m_size); } |
| 75 | |
| 76 | PageCount initial() const { return m_initial; } |
| 77 | PageCount maximum() const { return m_maximum; } |
| 78 | |
| 79 | MemoryMode mode() const { return m_mode; } |
| 80 | |
| 81 | enum class GrowFailReason { |
| 82 | InvalidDelta, |
| 83 | InvalidGrowSize, |
| 84 | WouldExceedMaximum, |
| 85 | OutOfMemory, |
| 86 | }; |
| 87 | Expected<PageCount, GrowFailReason> grow(PageCount); |
| 88 | void registerInstance(Instance*); |
| 89 | |
| 90 | void check() { ASSERT(!deletionHasBegun()); } |
| 91 | |
| 92 | static ptrdiff_t offsetOfMemory() { return OBJECT_OFFSETOF(Memory, m_memory); } |
| 93 | static ptrdiff_t offsetOfSize() { return OBJECT_OFFSETOF(Memory, m_size); } |
| 94 | |
| 95 | private: |
| 96 | Memory(); |
| 97 | Memory(void* memory, PageCount initial, PageCount maximum, size_t mappedCapacity, MemoryMode, WTF::Function<void(NotifyPressure)>&& notifyMemoryPressure, WTF::Function<void(SyncTryToReclaim)>&& syncTryToReclaimMemory, WTF::Function<void(GrowSuccess, PageCount, PageCount)>&& growSuccessCallback); |
| 98 | Memory(PageCount initial, PageCount maximum, WTF::Function<void(NotifyPressure)>&& notifyMemoryPressure, WTF::Function<void(SyncTryToReclaim)>&& syncTryToReclaimMemory, WTF::Function<void(GrowSuccess, PageCount, PageCount)>&& growSuccessCallback); |
| 99 | |
| 100 | using CagedMemory = CagedPtr<Gigacage::Primitive, void, tagCagedPtr>; |
| 101 | CagedMemory m_memory; |
| 102 | size_t m_size { 0 }; |
| 103 | PageCount m_initial; |
| 104 | PageCount m_maximum; |
| 105 | size_t m_mappedCapacity { 0 }; |
| 106 | MemoryMode m_mode { MemoryMode::BoundsChecking }; |
| 107 | WTF::Function<void(NotifyPressure)> m_notifyMemoryPressure; |
| 108 | WTF::Function<void(SyncTryToReclaim)> m_syncTryToReclaimMemory; |
| 109 | WTF::Function<void(GrowSuccess, PageCount, PageCount)> m_growSuccessCallback; |
| 110 | Vector<WeakPtr<Instance>> m_instances; |
| 111 | }; |
| 112 | |
| 113 | } } // namespace JSC::Wasm |
| 114 | |
| 115 | #else |
| 116 | |
| 117 | namespace JSC { namespace Wasm { |
| 118 | |
| 119 | class Memory { |
| 120 | public: |
| 121 | static size_t maxFastMemoryCount() { return 0; } |
| 122 | static bool addressIsInActiveFastMemory(void*) { return false; } |
| 123 | }; |
| 124 | |
| 125 | } } // namespace JSC::Wasm |
| 126 | |
| 127 | #endif // ENABLE(WEBASSEMBLY) |
| 128 | |