Skip to content

Commit 5bd49eb

Browse files
committed
src: malloced_unique_ptr & make_malloced_unique
Ref: nodejs#23641 Ref: nodejs#23543 (review) Ref: nodejs#23434
1 parent b3ae915 commit 5bd49eb

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/util.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,22 @@ template <typename T, typename U>
492492
inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
493493
const std::unordered_map<T, U>& map);
494494

495+
// Helper for `malloced_unique_ptr`
496+
template<typename T>
497+
struct MallocDeleter {
498+
void operator()(T* ptr) const { free(ptr); }
499+
};
500+
501+
// Specialization of `std::unique_ptr` that used `Malloc<t>`
502+
template<typename T>
503+
using malloced_unique_ptr = std::unique_ptr<T, MallocDeleter<T>>;
504+
505+
// Factory of `malloced_unique_ptr`
506+
template<typename T>
507+
malloced_unique_ptr<T> make_malloced_unique(size_t number_of_t) {
508+
return malloced_unique_ptr<T>(Malloc<T>(number_of_t));
509+
}
510+
495511
} // namespace node
496512

497513
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

0 commit comments

Comments
 (0)