1 /* This file is part of the GNU libxmi package.  Copyright (C) 1998, 1999,
2    2000, 2005, Free Software Foundation, Inc.
3 
4    The GNU libxmi package is free software.  You may redistribute it
5    and/or modify it under the terms of the GNU General Public License as
6    published by the Free Software foundation; either version 2, or (at your
7    option) any later version.
8 
9    The GNU libxmi package is distributed in the hope that it will be
10    useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License along
15    with the GNU plotutils package; see the file COPYING.  If not, write to
16    the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
17    Boston, MA 02110-1301, USA. */
18 
19 /* Wrappers for standard storage allocation functions.  The tests for zero
20    size, etc., are necessitated by the way in which the original X11
21    scan-conversion code was written. */
22 
23 #include "sys-defines.h"
24 #include "extern.h"
25 
26 #include "xmi.h"
27 #include "mi_spans.h"
28 #include "mi_api.h"
29 
30 /* wrapper for malloc() */
31 void *
mi_xmalloc(size_t size)32 mi_xmalloc (size_t size)
33 {
34   void * p;
35 
36   if (size == 0)
37     return (void *)NULL;
38 
39   p = (void *) malloc (size);
40   if (p == (void *)NULL)
41     {
42       fprintf (stderr, "libxmi: ");
43       perror ("out of memory");
44       exit (EXIT_FAILURE);
45     }
46   return p;
47 }
48 
49 /* wrapper for calloc() */
50 void *
mi_xcalloc(size_t nmemb,size_t size)51 mi_xcalloc (size_t nmemb, size_t size)
52 {
53   void * p;
54 
55   if (size == 0)
56     return (void *)NULL;
57 
58   p = (void *) calloc (nmemb, size);
59   if (p == (void *)NULL)
60     {
61       fprintf (stderr, "libxmi: ");
62       perror ("out of memory");
63       exit (EXIT_FAILURE);
64     }
65   return p;
66 }
67 
68 /* wrapper for realloc() */
69 void *
mi_xrealloc(void * p,size_t size)70 mi_xrealloc (void * p, size_t size)
71 {
72   if (!p)
73     return mi_xmalloc (size);
74   else
75     {
76       if (size == 0)
77 	{
78 	  free (p);
79 	  return (void *)NULL;
80 	}
81 
82       p = (void *) realloc (p, size);
83       if (p == (void *)NULL)
84 	{
85 	  fprintf (stderr, "libxmi: ");
86 	  perror ("out of memory");
87 	  exit (EXIT_FAILURE);
88 	}
89       return p;
90     }
91 }
92