-
Notifications
You must be signed in to change notification settings - Fork 936
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
Add utstring_release #95
Open
tbeu
wants to merge
3
commits into
troydhanson:master
Choose a base branch
from
tbeu:add-utstring-release
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this isn't going to be a macro, I think it needs the word
_UNUSED_
in front of it.No comment on the functionality; I'll let @troydhanson take it or leave it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have done this kind of thing before manually. So, I think it's a useful operation. Agreed on the need for
_UNUSED_
. I think the name_release
is so-so. I want to imply that it's releasing it "to" the caller. Maybeutstring_disown
. Thoughts? Incidentally I once or twice wrote the opposite function that adopts the string you give it and makes a utstring around it. From that point of view,utstring_own
andutstring_disown
would make a pair.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Surely the opposite of
utstring_disown
would beutstring_adopt
!The longer I look at the existing utstring interface, the more I think it'll never quite line up with my C++ish expectations. Seems like I get a utstring by calling
utstring_new(p)
, but then to put a string into it I have to useutstring_bincpy(p, "mydata", strlen("mydata"))
(there's noutstring_strcpy
), and then when I'm done I doutstring_free(p); p = NULL;
(if I forget that reset-to-null operation, I'll get a double-free if Iutstring_renew(p)
— I thinkutstring_renew
is an unusual wart in the API).With this patch, if I want to "release/disown" the string, I do either
char *s = utstring_body(p); utstring_init(p);
or elsechar *s = utstring_release(p); p = NULL;
(again forgetting the reset-to-null might lead to double-frees) — and if I want to "release/disown" a non-alloc'ed utstring, I have to use the first method, becauseutstring_release
always wants tofree
the utstring I give it.I'd want to separate the act of "disowning the buffer" and the act of "destroying the (possibly non-alloc'ed) utstring object itself":
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@troydhanson wrote:
I can compellingly argue that in the C++ worldview they'd definitely be separate. But I can't argue that utstring currently follows the C++ worldview, because of the "renew" verb and the lack of
(s)=NULL
s in some places and the incomplete(?) support for UT_strings allocated on the stack. So if utstring isn't C++ish, there's no syllogistic reason for it to have to makedisown
andfree
separate operations.The only thing I can say for sure is that
disown/free
should be separated if-and-only-ifadopt/new
are separated; i.e. if it's possible for an existing and/or stack-allocated utstring toadopt
a new buffer, then symmetrically we must make it possible for a utstring todisown
its buffer without vanishing and/or having been heap-allocated. Whereas, ifutstring_adopt
involvesmalloc(sizeof (UT_string))
, thenutstring_disown
must involvefree(s)
. Does that makes sense to everyone involved?