[lkml]   [2007]   [Oct]   [23]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
    SubjectRe: [PATCH 1/4] stringbuf: A string buffer implementation

    On Tue, 2007-10-23 at 17:12 -0400, Matthew Wilcox wrote:
    > Consecutive calls to printk are non-atomic, which leads to various
    > implementations for accumulating strings which can be printed in one call.
    > This is a generic string buffer which can also be used for non-printk
    > purposes. There is no sb_scanf implementation yet as I haven't identified
    > a user for it.
    > +
    > +struct stringbuf {
    > + char *s;
    > + int alloc;
    > + int len;
    > +};
    > +

    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

    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


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

    Best regards,

    - Eric

    To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
    the body of a message to
    More majordomo info at
    Please read the FAQ at

     \ /
      Last update: 2007-10-24 04:21    [W:0.021 / U:25.416 seconds]
    ©2003-2016 Jasper Spaans. hosted at Digital OceanAdvertise on this site