This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Policy: alloca vs. malloc?


On 06/07/2012 02:55 AM, Jakub Jelinek wrote:
> __libc_use_alloca does actually more than that, it isn't a fixed limit
> comparison, but if the size is larger than PTHREAD_STACK_MIN / 4,
> it limits use of alloca to current thread stack size / 4 (and
> __MAX_ALLOCA_CUTOFF as a maximum).

Thanks, I fixed this by changing __MAX_ALLOCA_CUTOFF to 4000 in the
example.

On 06/07/2012 07:59 PM, Siddhesh Poyarekar wrote:
> There should be a check here to ensure that buf is not NULL.

Thanks, I fixed that in the wiki.

> the only
> 'memory optimization' to speak of here is the malloc chunk overhead on
> the heap, which isn't much to brag about.

Sorry, I don't follow.  Both versions use the same amount of heap memory.
When using the stack, the alloca version allocates fewer bytes than the
non-alloca version.  Granted, this is not a huge amount of memory savings
(it's bounded by the size of the local array) but it does save some space.

I tried to clarify this by changing the wording to the following:

   Use of {{{alloca}}} is a memory optimization. That is, the above example
   is close in behavior to the following, except that the {{{alloca}}}
   version consumes only the stack space needed, rather than always consuming
   approximately 4000 bytes on the stack.

Also, I removed one of the examples with a fixed-size array (the point
is made sufficently in one example; it doesn't need two).  And I changed
the example with a growing stack-array to fix some other problems.  Here's
the new example.  It's tricker than what I'd like but the issue is
a tricky one....

=====

* If the amount of storage is not known in advance but may grow without bound, you can start with a small buffer on the stack and switch to malloc if it gets to be too large for the stack. While the storage is on the stack, you can grow it by using extend_alloca. For example: 

    struct foo buffer[10];
    struct foo *buf = buffer;
    size_t bufsize = sizeof buffer;
    void *allocated = NULL;
    size_t needed;
    while (bufsize < (needed = do_work_with (buf, bufsize)))
      {
        if (__libc_use_alloca (needed))
          {
            size_t size = bufsize;
            void *newbuf = extend_alloca (buf, bufsize, needed);
            buf = memmove (newbuf, buf, size);
          }
        else
          {
            void *newbuf = realloc (allocated, needed);
            if (! newbuf)
              {
                needed = 0;
                break;
              }
            if (! allocated)
              memcpy (newbuf, buf, bufsize);
            buf = allocated = newbuf;
            bufsize = needed;
          }
      }
    free (allocated);
    return needed; /* This is zero on allocation failure.  */


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]