Debian Codegraph @

js/src/gc/Allocator.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 // SpiderMonkey GC allocation API.
8
9 #ifndef gc_Allocator_h
10 #define gc_Allocator_h
11
12 #include "mozilla/OperatorNewExtensions.h"
13
14 #include <stdint.h>
15
16 #include "gc/AllocKind.h"
17 #include "gc/GCEnum.h"
18 #include "js/HeapAPI.h"
19 #include "js/TypeDecls.h"
20
21 namespace js {
22 namespace gc {
23
24 class AllocSite;
25 struct Cell;
26 class BufferAllocator;
27 class TenuredCell;
28 class TenuringTracer;
29
30 // Allocator implementation functions. SpiderMonkey code outside this file
31 // should use:
32 //
33 // cx->newCell<T>(...)
34 //
35 // or optionally:
36 //
37 // cx->newCell<T, AllowGC::NoGC>(...)
38 //
39 // `friend` js::gc::CellAllocator in a subtype T of Cell in order to allow it to
40 // be allocated with cx->newCell<T>(...). The friend declaration will allow
41 // calling T's constructor.
42 //
43 // The parameters will be passed to a type-specific function or constructor. For
44 // nursery-allocatable types, see e.g. the NewString, NewObject, and NewBigInt
45 // methods. For all other types, the parameters will be forwarded to the
46 // constructor.
47 class CellAllocator {
48 public:
49 // This is the entry point for all allocation, though callers should still not
50 // use this directly. Use cx->newCell<T>(...) instead.
51 //
52 // After a successful allocation the caller must fully initialize the thing
53 // before calling any function that can potentially trigger GC. This will
54 // ensure that GC tracing never sees junk values stored in the partially
55 // initialized thing.
56 template <typename T, AllowGC allowGC = CanGC, typename... Args>
57 static inline T* NewCell(JSContext* cx, Args&&... args);
58 friend class BufferAllocator;
59
60 private:
61 // Allocate a string. Use cx->newCell<T>([heap]).
62 //
63 // Use for nursery-allocatable strings. Returns a value cast to the correct
64 // type. Non-nursery-allocatable strings will go through the fallback
65 // tenured-only allocation path.
66 template <typename T, AllowGC allowGC, typename... Args>
67 static T* NewString(JSContext* cx, Heap heap, Args&&... args);
68
69 template <typename T, AllowGC allowGC>
70 static T* NewBigInt(JSContext* cx, Heap heap);
71
72 template <typename T, AllowGC allowGC>
73 static T* NewObject(JSContext* cx, AllocKind kind, Heap heap,
74 const JSClass* clasp, AllocSite* site = nullptr);
75
76 // Allocate all other kinds of GC thing.
77 template <typename T, AllowGC allowGC, typename... Args>
78 static T* NewTenuredCell(JSContext* cx, Args&&... args);
79
80 // Allocate a cell in the nursery, unless |heap| is Heap::Tenured or nursery
81 // allocation is disabled for |traceKind| in the current zone.
82 template <JS::TraceKind traceKind, AllowGC allowGC>
83 static void* AllocNurseryOrTenuredCell(JSContext* cx, AllocKind allocKind,
84 size_t thingSize, Heap heap,
85 AllocSite* site);
86 friend class TenuringTracer;
87
88 template <AllowGC allowGC>
89 static void* RetryNurseryAlloc(JSContext* cx, JS::TraceKind traceKind,
90 AllocKind allocKind, size_t thingSize,
91 AllocSite* site);
92 template <AllowGC allowGC>
93 static void* AllocTenuredCellForNurseryAlloc(JSContext* cx, AllocKind kind);
94
95 // Allocate a cell in the tenured heap.
96 template <AllowGC allowGC>
97 static void* AllocTenuredCell(JSContext* cx, AllocKind kind);
98
99 template <AllowGC allowGC>
100 static void* AllocTenuredCellUnchecked(JS::Zone* zone, AllocKind kind);
101
102 static void* RetryTenuredAlloc(JS::Zone* zone, AllocKind kind);
103
104 #ifdef JS_GC_ZEAL
105 static AllocSite* MaybeGenerateMissingAllocSite(JSContext* cx,
106 JS::TraceKind traceKind,
107 AllocSite* site);
108 #endif
109
110 #ifdef DEBUG
111 static void CheckIncrementalZoneState(JS::Zone* zone, void* ptr);
112 #endif
113
114 static inline Heap CheckedHeap(Heap heap);
115 };
116
117 // Buffer allocator public API.
118
119 size_t GetGoodAllocSize(size_t requiredBytes);
120 size_t GetGoodPower2AllocSize(size_t requiredBytes);
121 size_t GetGoodElementCount(size_t requiredCount, size_t elementSize);
122 size_t GetGoodPower2ElementCount(size_t requiredCount, size_t elementSize);
123 void* AllocBuffer(JS::Zone* zone, size_t bytes, bool nurseryOwned);
124 void* ReallocBuffer(JS::Zone* zone, void* alloc, size_t bytes,
125 bool nurseryOwned);
126 void FreeBuffer(JS::Zone* zone, void* alloc);
127
128 // Indicate whether |alloc| is a buffer allocation as opposed to a fixed size GC
129 // cell. Does not work for malloced memory.
130 bool IsBufferAlloc(void* alloc);
131
132 bool IsNurseryOwned(JS::Zone* zone, void* alloc);
133
134 size_t GetAllocSize(JS::Zone* zone, void* alloc);
135
136 // Buffer allocator GC-internal API.
137
138 void* AllocBufferInGC(JS::Zone* zone, size_t bytes, bool nurseryOwned);
139 bool IsBufferAllocMarkedBlack(JS::Zone* zone, void* alloc);
140 void TraceBufferEdgeInternal(JSTracer* trc, Cell* owner, void** bufferp,
141 const char* name);
142 void MarkTenuredBuffer(JS::Zone* zone, void* alloc);
143
144 } // namespace gc
145 } // namespace js
146
147 #endif // gc_Allocator_h