Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
src: malloced_unique_ptr & make_malloced_unique
  • Loading branch information
refack committed Oct 13, 2018
commit bca838b7ddc4f89458c1fe06d91cfeb28decbb14
16 changes: 16 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,22 @@ template <typename T, typename U>
inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
const std::unordered_map<T, U>& map);

// Helper for `malloced_unique_ptr`
template<typename T>
struct MallocDeleter {
void operator()(T* ptr) const { free(ptr); }
};

// Specialization of `std::unique_ptr` for data allocated with `Malloc<t>`
template<typename T>
using malloced_unique_ptr = std::unique_ptr<T, MallocDeleter<T>>;

// Factory of `malloced_unique_ptr`
template<typename T>
malloced_unique_ptr<T> make_malloced_unique(size_t number_of_t) {
return malloced_unique_ptr<T>(Malloc<T>(number_of_t));
}

} // namespace node

#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
Expand Down