1760c2415Smrg /* Memory management routines.
2*0bfacb9bSmrg Copyright (C) 2002-2020 Free Software Foundation, Inc.
3760c2415Smrg Contributed by Paul Brook <paul@nowt.org>
4760c2415Smrg
5760c2415Smrg This file is part of the GNU Fortran runtime library (libgfortran).
6760c2415Smrg
7760c2415Smrg Libgfortran is free software; you can redistribute it and/or
8760c2415Smrg modify it under the terms of the GNU General Public
9760c2415Smrg License as published by the Free Software Foundation; either
10760c2415Smrg version 3 of the License, or (at your option) any later version.
11760c2415Smrg
12760c2415Smrg Libgfortran is distributed in the hope that it will be useful,
13760c2415Smrg but WITHOUT ANY WARRANTY; without even the implied warranty of
14760c2415Smrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15760c2415Smrg GNU General Public License for more details.
16760c2415Smrg
17760c2415Smrg Under Section 7 of GPL version 3, you are granted additional
18760c2415Smrg permissions described in the GCC Runtime Library Exception, version
19760c2415Smrg 3.1, as published by the Free Software Foundation.
20760c2415Smrg
21760c2415Smrg You should have received a copy of the GNU General Public License and
22760c2415Smrg a copy of the GCC Runtime Library Exception along with this program;
23760c2415Smrg see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24760c2415Smrg <http://www.gnu.org/licenses/>. */
25760c2415Smrg
26760c2415Smrg #include "libgfortran.h"
27760c2415Smrg #include <errno.h>
28760c2415Smrg
29760c2415Smrg
30760c2415Smrg void *
xmalloc(size_t n)31760c2415Smrg xmalloc (size_t n)
32760c2415Smrg {
33760c2415Smrg void *p;
34760c2415Smrg
35760c2415Smrg if (n == 0)
36760c2415Smrg n = 1;
37760c2415Smrg
38760c2415Smrg p = malloc (n);
39760c2415Smrg
40760c2415Smrg if (p == NULL)
41760c2415Smrg os_error ("Memory allocation failed");
42760c2415Smrg
43760c2415Smrg return p;
44760c2415Smrg }
45760c2415Smrg
46760c2415Smrg
47760c2415Smrg void *
xmallocarray(size_t nmemb,size_t size)48760c2415Smrg xmallocarray (size_t nmemb, size_t size)
49760c2415Smrg {
50760c2415Smrg void *p;
51*0bfacb9bSmrg size_t prod;
52760c2415Smrg
53760c2415Smrg if (!nmemb || !size)
54*0bfacb9bSmrg prod = 1;
55*0bfacb9bSmrg else if (__builtin_mul_overflow (nmemb, size, &prod))
56760c2415Smrg {
57760c2415Smrg errno = ENOMEM;
58760c2415Smrg os_error ("Integer overflow in xmallocarray");
59760c2415Smrg }
60760c2415Smrg
61*0bfacb9bSmrg p = malloc (prod);
62760c2415Smrg
63760c2415Smrg if (!p)
64760c2415Smrg os_error ("Memory allocation failed in xmallocarray");
65760c2415Smrg
66760c2415Smrg return p;
67760c2415Smrg }
68760c2415Smrg
69760c2415Smrg
70760c2415Smrg /* calloc wrapper that aborts on error. */
71760c2415Smrg
72760c2415Smrg void *
xcalloc(size_t nmemb,size_t size)73760c2415Smrg xcalloc (size_t nmemb, size_t size)
74760c2415Smrg {
75760c2415Smrg if (!nmemb || !size)
76760c2415Smrg nmemb = size = 1;
77760c2415Smrg
78760c2415Smrg void *p = calloc (nmemb, size);
79760c2415Smrg if (!p)
80760c2415Smrg os_error ("Allocating cleared memory failed");
81760c2415Smrg
82760c2415Smrg return p;
83760c2415Smrg }
84760c2415Smrg
85760c2415Smrg
86760c2415Smrg void *
xrealloc(void * ptr,size_t size)87760c2415Smrg xrealloc (void *ptr, size_t size)
88760c2415Smrg {
89760c2415Smrg if (size == 0)
90760c2415Smrg size = 1;
91760c2415Smrg
92760c2415Smrg void *newp = realloc (ptr, size);
93760c2415Smrg if (!newp)
94760c2415Smrg os_error ("Memory allocation failure in xrealloc");
95760c2415Smrg
96760c2415Smrg return newp;
97760c2415Smrg }
98