Back to index

glibc  2.9
tst-mallocstate.c
Go to the documentation of this file.
00001 /* Copyright (C) 2001 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Wolfram Gloger <wg@malloc.de>, 2001.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <stdio.h>
00022 #include "malloc.h"
00023 
00024 static int errors = 0;
00025 
00026 static void
00027 merror (const char *msg)
00028 {
00029   ++errors;
00030   printf ("Error: %s\n", msg);
00031 }
00032 
00033 int
00034 main (void)
00035 {
00036   void *p1, *p2;
00037   void *save_state;
00038   long i;
00039 
00040   errno = 0;
00041 
00042   p1 = malloc (10);
00043   if (p1 == NULL)
00044     merror ("malloc (10) failed.");
00045 
00046   p2 = malloc (20);
00047   if (p2 == NULL)
00048     merror ("malloc (20) failed.");
00049 
00050   free (malloc (10));
00051 
00052   for (i=0; i<100; ++i)
00053     {
00054       save_state = malloc_get_state ();
00055       if (save_state == NULL)
00056        {
00057          merror ("malloc_get_state () failed.");
00058          break;
00059        }
00060       /*free (malloc (10)); This could change the top chunk! */
00061       malloc_set_state (save_state);
00062       p1 = realloc (p1, i*4 + 4);
00063       if (p1 == NULL)
00064        merror ("realloc (i*4) failed.");
00065       free (save_state);
00066     }
00067 
00068   p1 = realloc (p1, 40);
00069   free (p2);
00070   p2 = malloc (10);
00071   if (p2 == NULL)
00072     merror ("malloc (10) failed.");
00073   free (p1);
00074 
00075   return errors != 0;
00076 }
00077 
00078 /*
00079  * Local variables:
00080  * c-basic-offset: 2
00081  * End:
00082  */