v8  9.4.146 (node 16.13.0)
V8 is Google's open source JavaScript engine
explicit-management.h
Go to the documentation of this file.
1 // Copyright 2021 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef INCLUDE_CPPGC_EXPLICIT_MANAGEMENT_H_
6 #define INCLUDE_CPPGC_EXPLICIT_MANAGEMENT_H_
7 
8 #include <cstddef>
9 
10 #include "cppgc/allocation.h"
11 #include "cppgc/internal/logging.h"
12 #include "cppgc/type-traits.h"
13 
14 namespace cppgc {
15 
16 class HeapHandle;
17 
18 namespace internal {
19 
20 V8_EXPORT void FreeUnreferencedObject(HeapHandle&, void*);
21 V8_EXPORT bool Resize(void*, size_t);
22 
23 } // namespace internal
24 
25 namespace subtle {
26 
27 /**
28  * Informs the garbage collector that `object` can be immediately reclaimed. The
29  * destructor may not be invoked immediately but only on next garbage
30  * collection.
31  *
32  * It is up to the embedder to guarantee that no other object holds a reference
33  * to `object` after calling `FreeUnreferencedObject()`. In case such a
34  * reference exists, it's use results in a use-after-free.
35  *
36  * To aid in using the API, `FreeUnreferencedObject()` may be called from
37  * destructors on objects that would be reclaimed in the same garbage collection
38  * cycle.
39  *
40  * \param heap_handle The corresponding heap.
41  * \param object Reference to an object that is of type `GarbageCollected` and
42  * should be immediately reclaimed.
43  */
44 template <typename T>
45 void FreeUnreferencedObject(HeapHandle& heap_handle, T& object) {
46  static_assert(IsGarbageCollectedTypeV<T>,
47  "Object must be of type GarbageCollected.");
48  internal::FreeUnreferencedObject(heap_handle, &object);
49 }
50 
51 /**
52  * Tries to resize `object` of type `T` with additional bytes on top of
53  * sizeof(T). Resizing is only useful with trailing inlined storage, see e.g.
54  * `MakeGarbageCollected(AllocationHandle&, AdditionalBytes)`.
55  *
56  * `Resize()` performs growing or shrinking as needed and may skip the operation
57  * for internal reasons, see return value.
58  *
59  * It is up to the embedder to guarantee that in case of shrinking a larger
60  * object down, the reclaimed area is not used anymore. Any subsequent use
61  * results in a use-after-free.
62  *
63  * The `object` must be live when calling `Resize()`.
64  *
65  * \param object Reference to an object that is of type `GarbageCollected` and
66  * should be resized.
67  * \param additional_bytes Bytes in addition to sizeof(T) that the object should
68  * provide.
69  * \returns true when the operation was successful and the result can be relied
70  * on, and false otherwise.
71  */
72 template <typename T>
73 bool Resize(T& object, AdditionalBytes additional_bytes) {
74  static_assert(IsGarbageCollectedTypeV<T>,
75  "Object must be of type GarbageCollected.");
76  return internal::Resize(&object, sizeof(T) + additional_bytes.value);
77 }
78 
79 } // namespace subtle
80 } // namespace cppgc
81 
82 #endif // INCLUDE_CPPGC_EXPLICIT_MANAGEMENT_H_