Loading...
Searching...
No Matches
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#include <limits>
10
11#include "cppgc/allocation.h"
13#include "cppgc/type-traits.h"
14
15namespace cppgc {
16
17class HeapHandle;
18
19namespace subtle {
20
21template <typename T>
22void FreeUnreferencedObject(HeapHandle& heap_handle, T& object);
23template <typename T>
24bool Resize(T& object, AdditionalBytes additional_bytes);
25
26} // namespace subtle
27
28namespace internal {
29
31 private:
32 V8_EXPORT static void FreeUnreferencedObject(HeapHandle&, void*);
33 V8_EXPORT static bool Resize(void*, size_t);
34
35 template <typename T>
37 template <typename T>
39};
40} // namespace internal
41
42namespace subtle {
43
61template <typename T>
62void FreeUnreferencedObject(HeapHandle& heap_handle, T& object) {
63 static_assert(IsGarbageCollectedTypeV<T>,
64 "Object must be of type GarbageCollected.");
66 &object);
67}
68
90template <typename T>
91bool Resize(T& object, AdditionalBytes additional_bytes) {
92 static_assert(IsGarbageCollectedTypeV<T>,
93 "Object must be of type GarbageCollected.");
94 static_assert(sizeof(T) <= std::numeric_limits<uint32_t>::max());
95 const size_t new_object_size = sizeof(T) + additional_bytes.value;
96 // On 64-bit systems, since sizeof(T) and additional_bytes.value are both at
97 // most 32 bits, we know that their sum can't overflow. On 32-bit systems, we
98 // need to check it.
99#if !defined(V8_HOST_ARCH_64_BIT)
100 if (new_object_size < sizeof(T)) {
101 return false;
102 }
103#endif
104 return internal::ExplicitManagementImpl::Resize(&object, new_object_size);
105}
106
107} // namespace subtle
108} // namespace cppgc
109
110#endif // INCLUDE_CPPGC_EXPLICIT_MANAGEMENT_H_
Definition: heap-handle.h:21
Definition: explicit-management.h:30
friend void subtle::FreeUnreferencedObject(HeapHandle &, T &)
friend bool subtle::Resize(T &, AdditionalBytes)
bool Resize(T &object, AdditionalBytes additional_bytes)
Definition: explicit-management.h:91
void FreeUnreferencedObject(HeapHandle &heap_handle, T &object)
Definition: explicit-management.h:62
Definition: allocation.h:39
Definition: allocation.h:269
const uint32_t value
Definition: allocation.h:271
#define V8_EXPORT
Definition: v8config.h:874