Blame gnulib-tests/test-malloca.c

Packit 709fb3
/* Test of safe automatic memory allocation.
Packit 709fb3
   Copyright (C) 2005, 2007, 2009-2017 Free Software Foundation, Inc.
Packit 709fb3
Packit 709fb3
   This program is free software: you can redistribute it and/or modify
Packit 709fb3
   it under the terms of the GNU General Public License as published by
Packit 709fb3
   the Free Software Foundation; either version 3 of the License, or
Packit 709fb3
   (at your option) any later version.
Packit 709fb3
Packit 709fb3
   This program is distributed in the hope that it will be useful,
Packit 709fb3
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 709fb3
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 709fb3
   GNU General Public License for more details.
Packit 709fb3
Packit 709fb3
   You should have received a copy of the GNU General Public License
Packit 709fb3
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 709fb3
Packit 709fb3
/* Written by Bruno Haible <bruno@clisp.org>, 2005.  */
Packit 709fb3
Packit 709fb3
#include <config.h>
Packit 709fb3
Packit 709fb3
#include "malloca.h"
Packit 709fb3
Packit 709fb3
#include <stdlib.h>
Packit 709fb3
Packit 709fb3
static void
Packit 709fb3
do_allocation (int n)
Packit 709fb3
{
Packit 709fb3
  void *ptr = malloca (n);
Packit 709fb3
  freea (ptr);
Packit 709fb3
  safe_alloca (n);
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
void (*func) (int) = do_allocation;
Packit 709fb3
Packit 709fb3
int
Packit 709fb3
main ()
Packit 709fb3
{
Packit 709fb3
  int i;
Packit 709fb3
Packit 709fb3
  /* This slows down malloc a lot.  */
Packit 709fb3
  unsetenv ("MALLOC_PERTURB_");
Packit 709fb3
Packit 709fb3
  /* Repeat a lot of times, to make sure there's no memory leak.  */
Packit 709fb3
  for (i = 0; i < 50000; i++)
Packit 709fb3
    {
Packit 709fb3
      /* Try various values.
Packit 709fb3
         n = 0 gave a crash on Alpha with gcc-2.5.8.
Packit 709fb3
         Some versions of Mac OS X have a stack size limit of 512 KB.  */
Packit 709fb3
      func (34);
Packit 709fb3
      func (134);
Packit 709fb3
      func (399);
Packit 709fb3
      func (510823);
Packit 709fb3
      func (129321);
Packit 709fb3
      func (0);
Packit 709fb3
      func (4070);
Packit 709fb3
      func (4095);
Packit 709fb3
      func (1);
Packit 709fb3
      func (16582);
Packit 709fb3
    }
Packit 709fb3
Packit 709fb3
  return 0;
Packit 709fb3
}