NFFT  3.4.1
malloc.c
1 /*
2  * Copyright (c) 2002, 2017 Jens Keiner, Stefan Kunis, Daniel Potts
3  *
4  * This program is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 51
16  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #include<stdlib.h>
20 
21 #include "api.h"
22 
23 Y(malloc_type_function) Y(malloc_hook) = 0;
24 Y(free_type_function) Y(free_hook) = 0;
25 Y(die_type_function) Y(die_hook) = 0;
26 
27 void *Y(malloc)(size_t n)
28 {
29  void *p;
30 
31  if (Y(malloc_hook))
32  return Y(malloc_hook)(n);
33 
34  if (n == 0)
35  n = 1;
36 
37  p = FFTW(malloc)(n);
38 
39  if (!p)
40  Y(die)(STRINGIZE(Y(malloc)) ": out of memory\n");
41 
42  return p;
43 }
44 
45 void Y(free)(void *p)
46 {
47  if (p)
48  {
49  if (Y(free_hook))
50  {
51  Y(free_hook)(p);
52  return;
53  }
54  FFTW(free)(p);
55  }
56 }
57 
58 void Y(die)(const char *s)
59 {
60  if (Y(die_hook))
61  Y(die_hook)(s);
62 
63  exit(EXIT_FAILURE);
64 }