git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Project idea: strbuf allocation modes

From
Matthieu Moy <matthieu.moy@grenoble-inp.fr>
Date
Apr 22, 2014, 07:07 UTC
Message-ID
<vpqr44psuzf.fsf@anie.imag.fr>
In-Reply-To
<53512DB6.1070600@alum.mit.edu>
Michael Haggerty <mhagger@alum.mit.edu> writes:
Show 5 quoted lines
>     STRBUF_OWNS_MEMORY
>
>         The memory pointed to by buf is owned by this strbuf.  If this
>         bit is not set, then the memory should never be freed, and
>         (among other things) strbuf_detach() must always call xstrcpy().
I just foresee a small difficulty from the caller side:
            char path_buf[PATH_MAX];
            char *detached;
            struct strbuf path;
            strbuf_wrap_preallocated(&path, path_buf, 0, sizeof(path));
            ...
            detached = strbuf_detach(&path);

the wrapping/unwrapping of path_buf magically turned a stack-allocated buffer into a heap-allocated one. And the initial goal of avoiding malloc() is defeated. So, essentially, one should avoir using strbuf_wrap_preallocated with strbuf_detach, right? But I agree with Junio that if the API is properly used, everything should work. I'm just worried that we will add a bit more complexity to the API, and I'm not sure we can actually expect noticeable improvements in terms of performance (i.e. do we actually use strbuf for performance-critical stuff?).

In your proposal, would STRBUF_OWNS_MEMORY be a constant, or a flag that change when the internal buffer needs reallocation? My understanding is that it should change (if STRBUF_FIXED_MEMORY is not set), and the strbuf wrapping a preallocated buffer would become a "normal" strbuf when its internal buffer grows.

If so, your "strbuf_detach() must always call xstrcpy()" is to be understood as "if STRBUF_OWNS_MEMORY is still set when strbuf_detach() is called, then it must always call xstrcpy()", right?

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/
Previous: Junio C HamanoNext: Michael Haggerty
Message 6 of 8 in “Students project on Git (Ensimag)”
  1. Matthieu MoyApr 8, 2014
  2. Project idea: strbuf allocation modesMichael Haggerty, Apr 18, 2014
  3. Junio C HamanoApr 18, 2014
  4. Michael HaggertyApr 18, 2014
  5. Junio C HamanoApr 19, 2014
  6. Matthieu MoyApr 22, 2014
  7. Michael HaggertyApr 22, 2014
  8. Matthieu MoyApr 22, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.