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/. */
10 #include "mozilla/OperatorNewExtensions.h"
14 #include "gc/AllocKind.h"
15 #include "gc/GCEnum.h"
16 #include "js/TypeDecls.h"
26 // Allocator implementation functions. SpiderMonkey code outside this file
29 // cx->newCell<T>(...)
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
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
52 template <typename T
, js::AllowGC allowGC
= CanGC
, typename
... Args
>
53 static inline T
* NewCell(JSContext
* cx
, Args
&&... args
);
56 template <AllowGC allowGC
>
57 static void* RetryNurseryAlloc(JSContext
* cx
, JS::TraceKind traceKind
,
58 AllocKind allocKind
, size_t thingSize
,
60 template <AllowGC allowGC
>
61 static void* TryNewTenuredCell(JSContext
* cx
, AllocKind kind
,
64 #if defined(DEBUG) || defined(JS_GC_ZEAL) || defined(JS_OOM_BREAKPOINT)
65 template <AllowGC allowGC
>
66 static bool PreAllocChecks(JSContext
* cx
, AllocKind kind
);
68 template <AllowGC allowGC
>
69 static bool PreAllocChecks(JSContext
* cx
, AllocKind kind
) {
75 static void CheckIncrementalZoneState(JSContext
* cx
, void* ptr
);
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
,
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
);
115 #endif // gc_Allocator_h