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 20, 2018
commit 5bd49ebc4fe05dabc338297722912be8ccad5dde
16 changes: 16 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,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` that used `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