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 <limits.h> |
31 | |
32 | namespace WTF { |
33 | class PrintStream; |
34 | } |
35 | |
36 | namespace JSC { namespace Wasm { |
37 | |
38 | class PageCount { |
39 | |
40 | public: |
41 | PageCount() |
42 | : m_pageCount(UINT_MAX) |
43 | { |
44 | static_assert(maxPageCount < UINT_MAX, "We rely on this here." ); |
45 | } |
46 | |
47 | PageCount(uint32_t pageCount) |
48 | : m_pageCount(pageCount) |
49 | { } |
50 | |
51 | void dump(WTF::PrintStream&) const; |
52 | |
53 | uint64_t bytes() const { return static_cast<uint64_t>(m_pageCount) * static_cast<uint64_t>(pageSize); } |
54 | uint32_t pageCount() const { return m_pageCount; } |
55 | |
56 | static bool isValid(uint32_t pageCount) |
57 | { |
58 | return pageCount <= maxPageCount; |
59 | } |
60 | |
61 | bool isValid() const |
62 | { |
63 | return isValid(m_pageCount); |
64 | } |
65 | |
66 | static PageCount fromBytes(uint64_t bytes) |
67 | { |
68 | RELEASE_ASSERT(bytes % pageSize == 0); |
69 | uint32_t numPages = bytes / pageSize; |
70 | RELEASE_ASSERT(PageCount::isValid(numPages)); |
71 | return PageCount(numPages); |
72 | } |
73 | |
74 | static PageCount max() |
75 | { |
76 | return PageCount(maxPageCount); |
77 | } |
78 | |
79 | explicit operator bool() const |
80 | { |
81 | return m_pageCount != UINT_MAX; |
82 | } |
83 | |
84 | bool operator<(const PageCount& other) const { return m_pageCount < other.m_pageCount; } |
85 | bool operator>(const PageCount& other) const { return m_pageCount > other.m_pageCount; } |
86 | bool operator>=(const PageCount& other) const { return m_pageCount >= other.m_pageCount; } |
87 | PageCount operator+(const PageCount& other) const |
88 | { |
89 | if (sumOverflows<uint32_t>(m_pageCount, other.m_pageCount)) |
90 | return PageCount(); |
91 | uint32_t newCount = m_pageCount + other.m_pageCount; |
92 | if (!PageCount::isValid(newCount)) |
93 | return PageCount(); |
94 | return PageCount(newCount); |
95 | } |
96 | |
97 | static constexpr uint32_t pageSize = 64 * KB; |
98 | private: |
99 | static constexpr uint32_t maxPageCount = static_cast<uint32_t>((1ull << 32) / pageSize); |
100 | |
101 | uint32_t m_pageCount; |
102 | }; |
103 | |
104 | } } // namespace JSC::Wasm |
105 | |
106 | #endif // ENABLE(WEBASSEMBLY) |
107 | |