Back to index

nagios-plugins  1.4.16
Functions
malloca.c File Reference
#include <config.h>
#include "malloca.h"

Go to the source code of this file.

Functions

void * mmalloca (size_t n)

Function Documentation

void* mmalloca ( size_t  n)

Definition at line 68 of file malloca.c.

{
#if HAVE_ALLOCA
  /* Allocate one more word, that serves as an indicator for malloc()ed
     memory, so that freea() of an alloca() result is fast.  */
  size_t nplus = n + HEADER_SIZE;

  if (nplus >= n)
    {
      char *p = (char *) malloc (nplus);

      if (p != NULL)
        {
          size_t slot;

          p += HEADER_SIZE;

          /* Put a magic number into the indicator word.  */
          ((int *) p)[-1] = MAGIC_NUMBER;

          /* Enter p into the hash table.  */
          slot = (unsigned long) p % HASH_TABLE_SIZE;
          ((struct header *) (p - HEADER_SIZE))->next = mmalloca_results[slot];
          mmalloca_results[slot] = p;

          return p;
        }
    }
  /* Out of memory.  */
  return NULL;
#else
# if !MALLOC_0_IS_NONNULL
  if (n == 0)
    n = 1;
# endif
  return malloc (n);
#endif
}