Re: [PATCH 1/3] Added generic string handling code.
- From
- Timo Sirainen <tss@iki.fi>
- Date
- May 20, 2007, 10:27 UTC
- Message-ID
- <B5F56D1C-4EC2-4A79-9489-B4F1C9FC9BA7@iki.fi>
- In-Reply-To
- <20070520100155.GB3106@steel.home>
On 20.5.2007, at 13.01, Alex Riesen wrote:
Show 6 quoted lines
> Timo Sirainen, Sun, May 20, 2007 04:24:29 +0200: >> So here's my try on starting with something simple. Unlike almost all >> other string handling libraries, it doesn't allocate the memory >> dynamically. > > Sometimes you _need_ dinamic memory allocation.
It's easy to use the same str_*() functions to implement dynamic memory allocation. I think I could have done a bit different naming, like maybe:
extern struct string *str_alloc(unsigned int len); extern void str_append(struct string *str, const char *cstr);
#define static_string(name, size) .. #define sstr_append(str, cstr) str_append(&(str).string, cstr)
Show 7 quoted lines
>> This makes it really easy to convert existing code to use it. I'm >> including some example changes in the other patches. Besides making >> the code safer, it can also make it faster, especially those >> strcat() replacements. > > It is also bigger, heavier on stack and sometimes slower because of > more function calls involved.
I would hardly call 8 extra bytes on stack heavier. Also if this was used everywhere I wouldn't be surprised if it made the code faster, because it would remove a lot of overflow checking code so more code will fit into L1 cache.