Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-02 08:54:42

0001 // Copyright 2021 the V8 project authors. All rights reserved.
0002 // Use of this source code is governed by a BSD-style license that can be
0003 // found in the LICENSE file.
0004 
0005 #ifndef INCLUDE_CPPGC_EXPLICIT_MANAGEMENT_H_
0006 #define INCLUDE_CPPGC_EXPLICIT_MANAGEMENT_H_
0007 
0008 #include <cstddef>
0009 
0010 #include "cppgc/allocation.h"
0011 #include "cppgc/internal/logging.h"
0012 #include "cppgc/type-traits.h"
0013 
0014 namespace cppgc {
0015 
0016 class HeapHandle;
0017 
0018 namespace subtle {
0019 
0020 template <typename T>
0021 void FreeUnreferencedObject(HeapHandle& heap_handle, T& object);
0022 template <typename T>
0023 bool Resize(T& object, AdditionalBytes additional_bytes);
0024 
0025 }  // namespace subtle
0026 
0027 namespace internal {
0028 
0029 class ExplicitManagementImpl final {
0030  private:
0031   V8_EXPORT static void FreeUnreferencedObject(HeapHandle&, void*);
0032   V8_EXPORT static bool Resize(void*, size_t);
0033 
0034   template <typename T>
0035   friend void subtle::FreeUnreferencedObject(HeapHandle&, T&);
0036   template <typename T>
0037   friend bool subtle::Resize(T&, AdditionalBytes);
0038 };
0039 }  // namespace internal
0040 
0041 namespace subtle {
0042 
0043 /**
0044  * Informs the garbage collector that `object` can be immediately reclaimed. The
0045  * destructor may not be invoked immediately but only on next garbage
0046  * collection.
0047  *
0048  * It is up to the embedder to guarantee that no other object holds a reference
0049  * to `object` after calling `FreeUnreferencedObject()`. In case such a
0050  * reference exists, it's use results in a use-after-free.
0051  *
0052  * To aid in using the API, `FreeUnreferencedObject()` may be called from
0053  * destructors on objects that would be reclaimed in the same garbage collection
0054  * cycle.
0055  *
0056  * \param heap_handle The corresponding heap.
0057  * \param object Reference to an object that is of type `GarbageCollected` and
0058  *   should be immediately reclaimed.
0059  */
0060 template <typename T>
0061 void FreeUnreferencedObject(HeapHandle& heap_handle, T& object) {
0062   static_assert(IsGarbageCollectedTypeV<T>,
0063                 "Object must be of type GarbageCollected.");
0064   internal::ExplicitManagementImpl::FreeUnreferencedObject(heap_handle,
0065                                                            &object);
0066 }
0067 
0068 /**
0069  * Tries to resize `object` of type `T` with additional bytes on top of
0070  * sizeof(T). Resizing is only useful with trailing inlined storage, see e.g.
0071  * `MakeGarbageCollected(AllocationHandle&, AdditionalBytes)`.
0072  *
0073  * `Resize()` performs growing or shrinking as needed and may skip the operation
0074  * for internal reasons, see return value.
0075  *
0076  * It is up to the embedder to guarantee that in case of shrinking a larger
0077  * object down, the reclaimed area is not used anymore. Any subsequent use
0078  * results in a use-after-free.
0079  *
0080  * The `object` must be live when calling `Resize()`.
0081  *
0082  * \param object Reference to an object that is of type `GarbageCollected` and
0083  *   should be resized.
0084  * \param additional_bytes Bytes in addition to sizeof(T) that the object should
0085  *   provide.
0086  * \returns true when the operation was successful and the result can be relied
0087  *   on, and false otherwise.
0088  */
0089 template <typename T>
0090 bool Resize(T& object, AdditionalBytes additional_bytes) {
0091   static_assert(IsGarbageCollectedTypeV<T>,
0092                 "Object must be of type GarbageCollected.");
0093   return internal::ExplicitManagementImpl::Resize(
0094       &object, sizeof(T) + additional_bytes.value);
0095 }
0096 
0097 }  // namespace subtle
0098 }  // namespace cppgc
0099 
0100 #endif  // INCLUDE_CPPGC_EXPLICIT_MANAGEMENT_H_