Closed Bug 1071967 Opened 10 years ago Closed 10 years ago

Replace ScopedFreePtr with UniquePtr in jsapi.cpp

Categories

(Core :: JavaScript Engine, defect)

defect
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla35

People

(Reporter: ggp, Assigned: ggp)

Details

Attachments

(1 file)

ScopedFreePtr is deprecated in favor of UniquePtr, and UniquePtr gives us the opportunity to specify which deallocation function to use, whereas ScopedFreePtr necessarily uses free() even for memory allocated with js_{m,c,re}alloc.
Comment on attachment 8494082 [details] [diff] [review]
Replace ScopedFreePtr with UniquePtr in jsapi.cpp

Review of attachment 8494082 [details] [diff] [review]:
-----------------------------------------------------------------

Great! Thanks for fixing that!
Attachment #8494082 - Flags: review?(terrence) → review+
> ScopedFreePtr is deprecated in favor of UniquePtr

I understand this is the consensus, and this patch does a fine job of converting.

But I wonder if it makes the code harder to read. The use of UniquePtr suggests two things: (a) it's a unique pointer, and (b) it'll be auto-freed. In this case, only (b) is needed/relevant. Reading the code, I think "oh this is a unique pointer" but then I see that every use of it involves reset() or get() and so then I think "oh, it's not really unique". Hmm.
"unique" refers to *ownership*.  UniquePtr is the single owner of whatever pointer it contains.  You can "borrow" a copy using get(), but that doesn't make it any less of a single owner of that pointer.  Automatic destruction is merely a logical consequence of UniquePtr being the single owner of whatever it contains.

reset() is just a helper method to force modification of a UniquePtr to a raw pointer, to use a form other than assignment to make the transfer of ownership clearer.  get() as noted doesn't change ownership, so it has no relevance to the meaning of UniquePtr here.

This would be better if both UTF8CharsToNewTwoByteCharsZ and InflateString returned UniquePtr<char16_t[], JS::FreePolicy>, in that you could use simple assignment instead of reset().  (You could also have a somewhat-cleaner ternary here, too, then.)  But that's mostly a cosmetic thing, when the use is as simple as here.
https://hg.mozilla.org/mozilla-central/rev/28fa0ca32114
Status: NEW → RESOLVED
Closed: 10 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla35
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: