Skip to content

Commit

Permalink
src: malloced_unique_ptr & make_malloced_unique
Browse files Browse the repository at this point in the history
  • Loading branch information
refack committed Oct 20, 2018
1 parent b3ae915 commit 5bd49eb
Showing 1 changed file with 16 additions and 0 deletions.
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

0 comments on commit 5bd49eb

Please sign in to comment.