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

Re: Project idea: strbuf allocation modes

From
Michael Haggerty <mhagger@alum.mit.edu>
Date
Apr 18, 2014, 20:04 UTC
Message-ID
<5351854E.1080602@alum.mit.edu>
In-Reply-To
<xmqq8ur2d04g.fsf@gitster.dls.corp.google.com>
On 04/18/2014 07:21 PM, Junio C Hamano wrote:
Show 31 quoted lines
> Michael Haggerty <mhagger@alum.mit.edu> writes:
> 
>> The Idea
>> ========
>>
>> I would like to see strbuf enhanced to allow it to use memory that it
>> doesn't own (for example, stack-allocated memory), while (optionally)
>> allowing it to switch over to using allocated memory if the string grows
>> past the size of the pre-allocated buffer.
> 
> I'd like to see these characteristics, but I would want to see that
> this is done entirely internally inside the strbuf implementation
> without any API impact, other than the initialisation.  I do not
> think the current API contract is too rigid to allow us doing so.
> 
>  - The API users may peek strbuf.buf in-place until they perform an
>    operation that makes it longer (at which point the .buf pointer
>    may point at a new piece of memory).
> 
>  - The API users may strbuf_detach() to obtain a piece of memory
>    that belongs to them (at which point the strbuf becomes empty),
>    hence needs to be freed by the callers.
> 
> As long as the above two promises are kept intact, it is all
> internal to the strbuf implementation, current iteration of which
> does not have any initial (possibly static) allocation other than
> the fixed "terminating NUL", but your updated one may take a caller
> supplied piece of memory that is designed to outlive the strbuf
> itself as its initial allocation and the memory ownership can be
> left as an internal implementation detail to the strbuf without
> bothering the callers.

I think that's exactly what I described. The only thing that would have to change in the strbuf behavior (aside from initialization) is that a buffer-growing operation might die if the string would grow outside of the bounds of the existing buffer when STRBUF_FIXED_MEMORY is set.

Michael
-- 
Michael Haggerty
mhagger@alum.mit.edu
http://softwareswirl.blogspot.com/
Previous: Junio C HamanoNext: Junio C Hamano
Message 4 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.