Rev 107 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | pj | 1 | /* |
2 | * Copyright (c) 1997-1999 Massachusetts Institute of Technology |
||
3 | * |
||
4 | * This program is free software; you can redistribute it and/or modify |
||
5 | * it under the terms of the GNU General Public License as published by |
||
6 | * the Free Software Foundation; either version 2 of the License, or |
||
7 | * (at your option) any later version. |
||
8 | * |
||
9 | * This program is distributed in the hope that it will be useful, |
||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
12 | * GNU General Public License for more details. |
||
13 | * |
||
14 | * You should have received a copy of the GNU General Public License |
||
15 | * along with this program; if not, write to the Free Software |
||
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
17 | * |
||
18 | */ |
||
19 | |||
20 | /* |
||
21 | * malloc.c -- memory allocation related functions |
||
22 | */ |
||
23 | |||
927 | pj | 24 | /* $Id: malloc.c,v 1.3 2005-01-08 15:00:28 pj Exp $ */ |
2 | pj | 25 | #ifdef FFTW_USING_CILK |
107 | pj | 26 | #include <cilk.h> |
27 | #include <cilk-compat.h> |
||
2 | pj | 28 | #endif |
29 | |||
107 | pj | 30 | #include <fftw-int.h> |
2 | pj | 31 | //#include <stdio.h> |
32 | #include <stdlib.h> |
||
33 | |||
34 | #ifdef HAVE_MALLOC_H |
||
35 | //#include <ports/malloc.h> |
||
36 | #endif |
||
37 | |||
38 | #include <kernel/kern.h> |
||
39 | #define EXIT_FAILURE 1 |
||
40 | |||
41 | fftw_malloc_type_function fftw_malloc_hook = 0; |
||
42 | fftw_free_type_function fftw_free_hook = 0; |
||
43 | fftw_die_type_function fftw_die_hook = 0; |
||
44 | |||
45 | /********************************************************** |
||
46 | * DEBUGGING CODE |
||
47 | ********************************************************** |
||
48 | gettato via!!! |
||
49 | */ |
||
50 | |||
51 | /********************************************************** |
||
52 | * NON DEBUGGING CODE |
||
53 | **********************************************************/ |
||
54 | /* production version, no hacks */ |
||
55 | |||
56 | void *fftw_malloc(size_t n) |
||
57 | { |
||
58 | void *p; |
||
59 | |||
60 | if (fftw_malloc_hook) |
||
61 | return fftw_malloc_hook(n); |
||
62 | |||
63 | if (n == 0) |
||
64 | n = 1; |
||
65 | |||
66 | p = malloc(n); |
||
67 | |||
68 | if (!p) |
||
69 | fftw_die("fftw_malloc: out of memory\n"); |
||
70 | |||
71 | return p; |
||
72 | } |
||
73 | |||
74 | void fftw_free(void *p) |
||
75 | { |
||
76 | if (p) { |
||
77 | if (fftw_free_hook) { |
||
78 | fftw_free_hook(p); |
||
79 | return; |
||
80 | } |
||
81 | free(p); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | /* die when fatal errors occur */ |
||
86 | void fftw_die(const char *s) |
||
87 | { |
||
88 | if (fftw_die_hook) |
||
89 | fftw_die_hook(s); |
||
90 | |||
91 | // fflush(stdout); |
||
92 | // fprintf(stderr, "fftw: %s", s); |
||
93 | |||
107 | pj | 94 | printk(KERN_ERR "fftw: %s", s); |
927 | pj | 95 | exit(EXIT_FAILURE); |
2 | pj | 96 | } |
97 | |||
98 | /* check for memory leaks when debugging */ |
||
99 | void fftw_check_memory_leaks(void) |
||
100 | { |
||
101 | extern int fftw_node_cnt, fftw_plan_cnt, fftw_twiddle_size; |
||
102 | |||
103 | #ifdef FFTW_DEBUG |
||
104 | if (fftw_malloc_cnt || fftw_malloc_total || |
||
105 | fftw_node_cnt || fftw_plan_cnt || fftw_twiddle_size) { |
||
106 | // fflush(stdout); |
||
107 | // fprintf(stderr, |
||
108 | kern_printf( |
||
109 | "MEMORY LEAK!!!\n" |
||
110 | "fftw_malloc = %d" |
||
111 | " node=%d plan=%d twiddle=%d\n" |
||
112 | "fftw_malloc_total = %d\n", |
||
113 | fftw_malloc_cnt, |
||
114 | fftw_node_cnt, fftw_plan_cnt, fftw_twiddle_size, |
||
115 | fftw_malloc_total); |
||
927 | pj | 116 | exit(EXIT_FAILURE); |
2 | pj | 117 | } |
118 | #else |
||
119 | if (fftw_node_cnt || fftw_plan_cnt || fftw_twiddle_size) { |
||
120 | // fflush(stdout); |
||
121 | // fprintf(stderr, |
||
122 | kern_printf( |
||
123 | "MEMORY LEAK!!!\n" |
||
124 | " node=%d plan=%d twiddle=%d\n", |
||
125 | fftw_node_cnt, fftw_plan_cnt, fftw_twiddle_size); |
||
927 | pj | 126 | exit(EXIT_FAILURE); |
2 | pj | 127 | } |
128 | #endif |
||
129 | } |
||
130 | |||
131 | void fftw_print_max_memory_usage(void) |
||
132 | { |
||
133 | #ifdef FFTW_DEBUG |
||
134 | printf("\nMaximum number of blocks allocated = %d\n" |
||
135 | "Maximum number of bytes allocated = %0.3f kB\n", |
||
136 | fftw_malloc_cnt_max, fftw_malloc_max / 1024.0); |
||
137 | #endif |
||
138 | } |