NFFT  3.3.0
malloc.c
1 /*
2  * Copyright (c) 2002, 2015 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 /* $Id: util.c 3483 2010-04-23 19:02:34Z keiner $ */
20 
21 #include<stdlib.h>
22 
23 #include "api.h"
24 
25 Y(malloc_type_function) Y(malloc_hook) = 0;
26 Y(free_type_function) Y(free_hook) = 0;
27 Y(die_type_function) Y(die_hook) = 0;
28 
29 void *Y(malloc)(size_t n)
30 {
31  void *p;
32 
33  if (Y(malloc_hook))
34  return Y(malloc_hook)(n);
35 
36  if (n == 0)
37  n = 1;
38 
39  p = FFTW(malloc)(n);
40 
41  if (!p)
42  Y(die)(STRINGIZE(Y(malloc)) ": out of memory\n");
43 
44  return p;
45 }
46 
47 void Y(free)(void *p)
48 {
49  if (p)
50  {
51  if (Y(free_hook))
52  {
53  Y(free_hook)(p);
54  return;
55  }
56  FFTW(free)(p);
57  }
58 }
59 
60 void Y(die)(const char *s)
61 {
62  if (Y(die_hook))
63  Y(die_hook)(s);
64 
65  exit(EXIT_FAILURE);
66 }