Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: free up memory before re-setting URLHost value #18357

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ class URLHost {
Value value_;
HostType type_ = HostType::H_FAILED;

inline void Reset() {
using string = std::string;
switch (type_) {
case HostType::H_DOMAIN: value_.domain.~string(); break;
case HostType::H_OPAQUE: value_.opaque.~string(); break;
default: break;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After resettig the value_, can you reset the type_ as well by setting it to HostType::H_FAILED?

Copy link
Contributor Author

@prog1dev prog1dev Jan 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done a7c310f

type_ = HostType::H_FAILED;
}

// Setting the string members of the union with = is brittle because
// it relies on them being initialized to a state that requires no
// destruction of old data.
Expand All @@ -101,23 +111,20 @@ class URLHost {
// These helpers are the easiest solution but we might want to consider
// just not forcing strings into an union.
inline void SetOpaque(std::string&& string) {
Reset();
type_ = HostType::H_OPAQUE;
new(&value_.opaque) std::string(std::move(string));
}

inline void SetDomain(std::string&& string) {
Reset();
type_ = HostType::H_DOMAIN;
new(&value_.domain) std::string(std::move(string));
}
};

URLHost::~URLHost() {
using string = std::string;
switch (type_) {
case HostType::H_DOMAIN: value_.domain.~string(); break;
case HostType::H_OPAQUE: value_.opaque.~string(); break;
default: break;
}
Reset();
}

#define ARGS(XX) \
Expand Down