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

memory inefficiency in builtin-commit-tree.c - add_buffer() - in git version 1.5.2

From
TSTim Stewart <tim.j.stewart@gmail.com>
Date
Jul 12, 2007, 16:35 UTC
Message-ID
<23f4d5c70707120935k7a784f59rb3bb27ac76cb52b7@mail.gmail.com>
In-Reply-To
<23f4d5c70707120933l1d1664adgd12042a35f4629cd@mail.gmail.com>

The function init_buffer allocates 16,384 bytes. This function then sets the size of the buffer at 0 bytes.

Then when add_buffer is called, it calculates how much memory the added bytes will take up here:

        alloc = (size + 32767) & ~32767;
Since size is 0, alloc will be 0.  So the next line:
         if (newsize > alloc) {

will always evaluate to true (unless newsize is 0) and then the function reallocs the buffer at twice it's current size.

This all but guarantees that the memory allocated in init_buffer will be realloc'ed.

Here is the code for your reference:
static void init_buffer(char **bufp, unsigned int *sizep)
{
        *bufp = xmalloc(BLOCKING);
        *sizep = 0;
}
static void add_buffer(char **bufp, unsigned int *sizep, const char *fmt, ...)
{
        char one_line[2048];
        va_list args;
        int len;
        unsigned long alloc, size, newsize;
        char *buf;
        va_start(args, fmt);
        len = vsnprintf(one_line, sizeof(one_line), fmt, args);
        va_end(args);
        size = *sizep;
        newsize = size + len + 1;
        alloc = (size + 32767) & ~32767;
        buf = *bufp;
        if (newsize > alloc) {
                alloc = (newsize + 32767) & ~32767;
                buf = xrealloc(buf, alloc);
                *bufp = buf;
        }
        *sizep = newsize - 1;
        memcpy(buf + size, one_line, len);
}
Message 1 of 1 in “memory inefficiency in builtin-commit-tree.c - add_buffer() - in git version 1.5.2”
  1. Tim StewartJul 12, 2007

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.