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 13, 2018
1 parent 401dac3 commit bca838b
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 @@ -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

0 comments on commit bca838b

Please sign in to comment.