1/*
2 * Copyright (C) 2016-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 "DestructionMode.h"
29
30namespace JSC {
31
32class CellContainer;
33class Heap;
34class PreciseAllocation;
35class MarkedBlock;
36class Subspace;
37class VM;
38struct CellAttributes;
39
40class HeapCell {
41public:
42 enum Kind : int8_t {
43 JSCell,
44 JSCellWithInteriorPointers,
45 Auxiliary
46 };
47
48 HeapCell() { }
49
50 // We're intentionally only zapping the bits for the structureID and leaving
51 // the rest of the cell header bits intact for crash analysis uses.
52 enum ZapReason : int8_t { Unspecified, Destruction, StopAllocating };
53 void zap(ZapReason reason)
54 {
55 uint32_t* cellWords = bitwise_cast<uint32_t*>(this);
56 cellWords[0] = 0;
57 // Leaving cellWords[1] alone for crash analysis if needed.
58 cellWords[2] = reason;
59 }
60 bool isZapped() const { return !*bitwise_cast<const uint32_t*>(this); }
61
62 bool isLive();
63
64 bool isPreciseAllocation() const;
65 CellContainer cellContainer() const;
66 MarkedBlock& markedBlock() const;
67 PreciseAllocation& preciseAllocation() const;
68
69 // If you want performance and you know that your cell is small, you can do this instead:
70 // ASSERT(!cell->isPreciseAllocation());
71 // cell->markedBlock().vm()
72 // We currently only use this hack for callees to make CallFrame::vm() fast. It's not
73 // recommended to use it for too many other things, since the large allocation cutoff is
74 // a runtime option and its default value is small (400 bytes).
75 Heap* heap() const;
76 VM& vm() const;
77
78 size_t cellSize() const;
79 CellAttributes cellAttributes() const;
80 DestructionMode destructionMode() const;
81 Kind cellKind() const;
82 Subspace* subspace() const;
83
84 // Call use() after the last point where you need `this` pointer to be kept alive. You usually don't
85 // need to use this, but it might be necessary if you're otherwise referring to an object's innards
86 // but not the object itself.
87#if COMPILER(GCC_COMPATIBLE)
88 void use() const
89 {
90 asm volatile ("" : : "r"(this) : "memory");
91 }
92#else
93 void use() const;
94#endif
95};
96
97inline bool isJSCellKind(HeapCell::Kind kind)
98{
99 return kind == HeapCell::JSCell || kind == HeapCell::JSCellWithInteriorPointers;
100}
101
102inline bool hasInteriorPointers(HeapCell::Kind kind)
103{
104 return kind == HeapCell::Auxiliary || kind == HeapCell::JSCellWithInteriorPointers;
105}
106
107} // namespace JSC
108
109namespace WTF {
110
111class PrintStream;
112
113void printInternal(PrintStream&, JSC::HeapCell::Kind);
114
115} // namespace WTF
116
117