-
Notifications
You must be signed in to change notification settings - Fork 935
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
Configurable UTSTRING_INITIAL_SIZE #146
Open
silvioprog
wants to merge
4
commits into
troydhanson:master
Choose a base branch
from
silvioprog:master
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 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8a71eed
utstring.h: adds the `UTSTRING_INITIAL_SIZE` macro allowing to change…
silvioprog f1a92f3
utstring.h: applies patch attached to https://github.com/troydhanson/…
silvioprog 4c0ea40
utstring.h: fixes alert "Macro argument should be enclosed in parenth…
silvioprog b1c4523
Tip from: https://github.com/troydhanson/uthash/pull/146#discussion_r…
silvioprog 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,10 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
#define oom() exit(-1) | ||
#endif | ||
|
||
#ifndef UTSTRING_INITIAL_SIZE | ||
#define UTSTRING_INITIAL_SIZE 0x64 | ||
#endif | ||
|
||
typedef struct { | ||
char *d; /* pointer to allocated buffer */ | ||
size_t n; /* allocated capacity */ | ||
|
@@ -58,33 +62,33 @@ do { \ | |
(s)->d = utstring_tmp; \ | ||
(s)->n += (amt); \ | ||
} \ | ||
} while(0) | ||
} while (0) | ||
|
||
#define utstring_init(s) \ | ||
do { \ | ||
(s)->n = 0; (s)->i = 0; (s)->d = NULL; \ | ||
utstring_reserve(s,100); \ | ||
utstring_reserve(s,UTSTRING_INITIAL_SIZE); \ | ||
(s)->d[0] = '\0'; \ | ||
} while(0) | ||
} while (0) | ||
|
||
#define utstring_done(s) \ | ||
do { \ | ||
if ((s)->d != NULL) free((s)->d); \ | ||
(s)->n = 0; \ | ||
} while(0) | ||
} while (0) | ||
|
||
#define utstring_free(s) \ | ||
do { \ | ||
utstring_done(s); \ | ||
free(s); \ | ||
} while(0) | ||
} while (0) | ||
|
||
#define utstring_new(s) \ | ||
do { \ | ||
s = (UT_string*)malloc(sizeof(UT_string)); \ | ||
if (!s) oom(); \ | ||
(s) = (UT_string*)malloc(sizeof(UT_string)); \ | ||
if (!(s)) oom(); \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Merged as 29fc26a — thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're welcome dude! 👍 |
||
utstring_init(s); \ | ||
} while(0) | ||
} while (0) | ||
|
||
#define utstring_renew(s) \ | ||
do { \ | ||
|
@@ -93,29 +97,29 @@ do { \ | |
} else { \ | ||
utstring_new(s); \ | ||
} \ | ||
} while(0) | ||
} while (0) | ||
|
||
#define utstring_clear(s) \ | ||
do { \ | ||
(s)->i = 0; \ | ||
(s)->d[0] = '\0'; \ | ||
} while(0) | ||
} while (0) | ||
|
||
#define utstring_bincpy(s,b,l) \ | ||
do { \ | ||
utstring_reserve((s),(l)+1); \ | ||
if (l) memcpy(&(s)->d[(s)->i], b, l); \ | ||
(s)->i += (l); \ | ||
(s)->d[(s)->i]='\0'; \ | ||
} while(0) | ||
} while (0) | ||
|
||
#define utstring_concat(dst,src) \ | ||
do { \ | ||
utstring_reserve((dst),((src)->i)+1); \ | ||
if ((src)->i) memcpy(&(dst)->d[(dst)->i], (src)->d, (src)->i); \ | ||
(dst)->i += (src)->i; \ | ||
(dst)->d[(dst)->i]='\0'; \ | ||
} while(0) | ||
} while (0) | ||
|
||
#define utstring_len(s) ((s)->i) | ||
|
||
|
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.
(A) If somebody has a strong use-case for tweaking this specific parameter (
UTSTRING_INITIAL_SIZE
), then I might be swayed; but a priori, I'd rather see this particular diff come as part of a larger, comprehensive thinking-out of utstring's allocation machinery. Like, maybe it should be using a primitive such asutstring_realloc
that the user is allowed to-D
efine, and maybe we need to think about what realloc-of-size-zero means, and so on.(B) Writing
0x64
when you mean100
is just as confusing as writing0100
when you mean64
. I don't know why you'd do that.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.
(A) 1. On embedded systems (e.g: an AVR/PIC with just 512 of RAM), the programmer could choose a value smaller than 100; On common fast systems, it can be increased (1 kB, 8 kB) via building system.
(B) Oops, sorry. It was just a habit from microcontroller coding! 😅
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.
(B) additional info: It uses the same concept: https://github.com/troydhanson/libut/blob/master/src/utvector.c#L29 . (however
INITIAL_SIZE
should be prefixed toUTVECTOR_INITIAL_SIZE
avoiding to conflict on user macros)