1/*
2 * Copyright (C) 2017-2019 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#include "BAssert.h"
29#include "BInline.h"
30#include "Gigacage.h"
31
32namespace bmalloc {
33
34enum class HeapKind {
35 Primary,
36 PrimitiveGigacage,
37 JSValueGigacage
38};
39
40static constexpr unsigned numHeaps = 3;
41
42BINLINE bool isGigacage(HeapKind heapKind)
43{
44 switch (heapKind) {
45 case HeapKind::Primary:
46 return false;
47 case HeapKind::PrimitiveGigacage:
48 case HeapKind::JSValueGigacage:
49 return true;
50 }
51 BCRASH();
52 return false;
53}
54
55BINLINE Gigacage::Kind gigacageKind(HeapKind kind)
56{
57 switch (kind) {
58 case HeapKind::Primary:
59 BCRASH();
60 return Gigacage::Primitive;
61 case HeapKind::PrimitiveGigacage:
62 return Gigacage::Primitive;
63 case HeapKind::JSValueGigacage:
64 return Gigacage::JSValue;
65 }
66 BCRASH();
67 return Gigacage::Primitive;
68}
69
70BINLINE HeapKind heapKind(Gigacage::Kind kind)
71{
72 switch (kind) {
73 case Gigacage::ReservedForFlagsAndNotABasePtr:
74 RELEASE_BASSERT_NOT_REACHED();
75 case Gigacage::Primitive:
76 return HeapKind::PrimitiveGigacage;
77 case Gigacage::JSValue:
78 return HeapKind::JSValueGigacage;
79 }
80 BCRASH();
81 return HeapKind::Primary;
82}
83
84BINLINE bool isActiveHeapKindAfterEnsuringGigacage(HeapKind kind)
85{
86 switch (kind) {
87 case HeapKind::PrimitiveGigacage:
88 case HeapKind::JSValueGigacage:
89 if (Gigacage::wasEnabled())
90 return true;
91 return false;
92 default:
93 return true;
94 }
95}
96
97BEXPORT bool isActiveHeapKind(HeapKind);
98
99BINLINE HeapKind mapToActiveHeapKindAfterEnsuringGigacage(HeapKind kind)
100{
101 switch (kind) {
102 case HeapKind::PrimitiveGigacage:
103 case HeapKind::JSValueGigacage:
104 if (Gigacage::wasEnabled())
105 return kind;
106 return HeapKind::Primary;
107 default:
108 return kind;
109 }
110}
111
112BEXPORT HeapKind mapToActiveHeapKind(HeapKind);
113
114} // namespace bmalloc
115
116