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 -
| Tarkan Erimer | Re: Dual-Licensing Linux Kernel with GPL V2 and GPL V3 |
| Greg KH | [GIT PATCH] driver core patches against 2.6.24 |
| Justin Piszcz | exception Emask 0x0 SAct 0x1 / SErr 0x0 action 0x2 frozen |
| Heiko Carstens | Re: -mm merge plans for 2.6.23 -- sys_fallocate |
git: | |
| Gerrit Renker | [PATCH 27/37] dccp: Integration of dynamic feature activation - part 2 (server side) |
| David Miller | [GIT]: Networking |
| Frans Pop | svc: failed to register lockdv1 RPC service (errno 97). |
| Radu Rendec | htb parallelism on multi-core platforms |
