Blame gl/tests/test-malloca.c

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