Re: [PATCH 1/4] stringbuf: A string buffer implementation

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Eric St-Laurent
Date: Tuesday, October 23, 2007 - 7:19 pm

On Tue, 2007-10-23 at 17:12 -0400, Matthew Wilcox wrote:

I don't know if copy-on-write semantics are really useful for current
in-kernel uses, but I've coded and used a C++ string class like this in
the past:

struct string_data
{
	int nrefs;
	unsigned len;
	unsigned capacity;
	//char data[capacity];	/* allocated along string_data */
};

struct string	/* or typedef in C... */
{
	struct string *data;
};

[ struct string_data is a hidden implementation detail, only struct
string is exposed ]

Multiple string objects can share the same data, by increasing the nrefs
count, a new data is allocated if the string is modified and nrefs > 1.

Not having to iterate over the string to calculate it's length,
allocating a larger buffer to eliminate re-allocation and copy-on-write
semantics make a string like this a vast performance improvement over a
normal C string for a minimal (about 3 ints per data buffer) memory
cost.

By using it correctly it can prevents buffer overflows.

You still always null terminate the string stored in data to directly
use it a normal C string.

You also statically allocate an empty string which is shared by all
"uninitialized" or empty strings.


Even without copy-on-write semantics and reference counting, I think
this approach is better because it uses 1 less "object" and allocation:

struct string - "handle" (pointer really) to string data
struct string_data - string data

versus:

struct stringbuf *sb - pointer to string object
struct stringbuf - string object
char *s (member of stringbuf) - string data



Best regards,

- Eric


-
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH 1/4] stringbuf: A string buffer implementation, Matthew Wilcox, (Tue Oct 23, 2:12 pm)
[PATCH 2/4] isdn: Use stringbuf, Matthew Wilcox, (Tue Oct 23, 2:12 pm)
[PATCH 3/4] sound: Use stringbuf, Matthew Wilcox, (Tue Oct 23, 2:12 pm)
[PATCH 4/4] partitions: Fix non-atomic printk, Matthew Wilcox, (Tue Oct 23, 2:12 pm)
Re: [PATCH 1/4] stringbuf: A string buffer implementation, Linus Torvalds, (Tue Oct 23, 4:43 pm)
Re: [PATCH 1/4] stringbuf: A string buffer implementation, Matthew Wilcox, (Tue Oct 23, 6:49 pm)
Re: [PATCH 1/4] stringbuf: A string buffer implementation, Eric St-Laurent, (Tue Oct 23, 7:19 pm)
Re: [PATCH 1/4] stringbuf: A string buffer implementation, Matthew Wilcox, (Tue Oct 23, 7:30 pm)
Re: [PATCH 1/4] stringbuf: A string buffer implementation, Matthew Wilcox, (Tue Oct 23, 7:35 pm)
Re: [PATCH 1/4] stringbuf: A string buffer implementation, Andrew Morton, (Tue Oct 23, 7:45 pm)
Re: [PATCH 1/4] stringbuf: A string buffer implementation, Eric St-Laurent, (Tue Oct 23, 7:48 pm)
Re: [PATCH 1/4] stringbuf: A string buffer implementation, Florian Weimer, (Wed Oct 24, 6:21 am)
Re: [PATCH 1/4] stringbuf: A string buffer implementation, Matthew Wilcox, (Wed Oct 24, 7:02 am)
Re: [PATCH 3/4] sound: Use stringbuf, Takashi Iwai, (Wed Oct 24, 7:18 am)
Re: [PATCH 3/4] sound: Use stringbuf, Takashi Iwai, (Wed Oct 24, 7:50 am)
Re: [PATCH 1/4] stringbuf: A string buffer implementation, Matthew Wilcox, (Wed Oct 24, 8:30 am)
Re: [PATCH 3/4] sound: Use stringbuf, Matthew Wilcox, (Wed Oct 24, 9:01 am)
Re: [PATCH 1/4] stringbuf: A string buffer implementation, Pavel Machek, (Sat Oct 27, 12:31 am)
Re: [PATCH 1/4] stringbuf: A string buffer implementation, Denys Vlasenko, (Tue Oct 30, 8:26 am)