git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Tim Stewart" <tim.j.stewart@gmail.com>
To: git@vger.kernel.org
Subject: memory inefficiency in builtin-commit-tree.c - add_buffer() - in git version 1.5.2
Date: Thu, 12 Jul 2007 10:35:13 -0600	[thread overview]
Message-ID: <23f4d5c70707120935k7a784f59rb3bb27ac76cb52b7@mail.gmail.com> (raw)
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);
}

           reply	other threads:[~2007-07-12 16:35 UTC|newest]

Thread overview: expand[flat|nested]  mbox.gz  Atom feed
 [parent not found: <23f4d5c70707120933l1d1664adgd12042a35f4629cd@mail.gmail.com>]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=23f4d5c70707120935k7a784f59rb3bb27ac76cb52b7@mail.gmail.com \
    --to=tim.j.stewart@gmail.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).