Backed out changeset 4191b252db9b (bug 1886734) for causing build bustages @netwerk...
[gecko.git] / js / src / gc / Allocator.h
blobc317bfb10bf2986ab4586a6db10c92d4c6468505
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/. */
7 #ifndef gc_Allocator_h
8 #define gc_Allocator_h
10 #include "mozilla/OperatorNewExtensions.h"
12 #include <stdint.h>
14 #include "gc/AllocKind.h"
15 #include "gc/GCEnum.h"
16 #include "js/TypeDecls.h"
18 namespace js {
19 namespace gc {
21 class AllocSite;
22 struct Cell;
23 class TenuredCell;
24 class TenuringTracer;
26 // Allocator implementation functions. SpiderMonkey code outside this file
27 // should use:
29 // cx->newCell<T>(...)
31 // or optionally:
33 // cx->newCell<T, AllowGC::NoGC>(...)
35 // `friend` js::gc::CellAllocator in a subtype T of Cell in order to allow it to
36 // be allocated with cx->newCell<T>(...). The friend declaration will allow
37 // calling T's constructor.
39 // The parameters will be passed to a type-specific function or constructor. For
40 // nursery-allocatable types, see e.g. the NewString, NewObject, and NewBigInt
41 // methods. For all other types, the parameters will be forwarded to the
42 // constructor.
43 class CellAllocator {
44 public:
45 // This is the entry point for all allocation, though callers should still not
46 // use this directly. Use cx->newCell<T>(...) instead.
48 // After a successful allocation the caller must fully initialize the thing
49 // before calling any function that can potentially trigger GC. This will
50 // ensure that GC tracing never sees junk values stored in the partially
51 // initialized thing.
52 template <typename T, js::AllowGC allowGC = CanGC, typename... Args>
53 static inline T* NewCell(JSContext* cx, Args&&... args);
55 private:
56 template <AllowGC allowGC>
57 static void* RetryNurseryAlloc(JSContext* cx, JS::TraceKind traceKind,
58 AllocKind allocKind, size_t thingSize,
59 AllocSite* site);
60 template <AllowGC allowGC>
61 static void* TryNewTenuredCell(JSContext* cx, AllocKind kind,
62 size_t thingSize);
64 #if defined(DEBUG) || defined(JS_GC_ZEAL) || defined(JS_OOM_BREAKPOINT)
65 template <AllowGC allowGC>
66 static bool PreAllocChecks(JSContext* cx, AllocKind kind);
67 #else
68 template <AllowGC allowGC>
69 static bool PreAllocChecks(JSContext* cx, AllocKind kind) {
70 return true;
72 #endif
74 #ifdef DEBUG
75 static void CheckIncrementalZoneState(JSContext* cx, void* ptr);
76 #endif
78 static inline gc::Heap CheckedHeap(gc::Heap heap);
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, gc::AllocKind allocKind,
84 size_t thingSize, gc::Heap heap,
85 AllocSite* site);
86 friend class TenuringTracer;
88 // Allocate a cell in the tenured heap.
89 template <AllowGC allowGC>
90 static void* AllocTenuredCell(JSContext* cx, gc::AllocKind kind, size_t size);
92 // Allocate a string. Use cx->newCell<T>([heap]).
94 // Use for nursery-allocatable strings. Returns a value cast to the correct
95 // type. Non-nursery-allocatable strings will go through the fallback
96 // tenured-only allocation path.
97 template <typename T, AllowGC allowGC, typename... Args>
98 static T* NewString(JSContext* cx, gc::Heap heap, Args&&... args);
100 template <typename T, AllowGC allowGC>
101 static T* NewBigInt(JSContext* cx, Heap heap);
103 template <typename T, AllowGC allowGC>
104 static T* NewObject(JSContext* cx, gc::AllocKind kind, gc::Heap heap,
105 const JSClass* clasp, gc::AllocSite* site = nullptr);
107 // Allocate all other kinds of GC thing.
108 template <typename T, AllowGC allowGC, typename... Args>
109 static T* NewTenuredCell(JSContext* cx, Args&&... args);
112 } // namespace gc
113 } // namespace js
115 #endif // gc_Allocator_h