"Fossies" - the Fresh Open Source Software Archive 
Member "FunctionCheck-3.2.0/src/libfc/fc_memory.c" (26 May 2012, 6768 Bytes) of package /linux/privat/old/FunctionCheck-3.2.0.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /*
2 * FunctionCheck profiler
3 * (C) Copyright 2000-2012 Yannick Perret
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /** fc_memory.c: **/
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <limits.h>
24 #include "fc_memory.h"
25 #include "fc_com.h"
26 #include "fc_global.h"
27 #include "fc_tools.h"
28
29 #ifdef HAVE_MALLOC_H
30
31 #ifdef HAVE_VALID_HOOKS
32
33 /** true if memory is not available **/
34 int fc_no_memory_hard = 0;
35
36 /* initial set of functions pointer for memory */
37 static __malloc_ptr_t(*old_malloc_hook) (size_t, __const __malloc_ptr_t);
38 static void (*old_free_hook) (__malloc_ptr_t, __const __malloc_ptr_t);
39 static __malloc_ptr_t(*old_realloc_hook) (__malloc_ptr_t, size_t, __const __malloc_ptr_t);
40 static __malloc_ptr_t(*old_memalign_hook) (size_t, size_t, __const __malloc_ptr_t);
41
42 /* prototypes for our memory hooks */
43 static __malloc_ptr_t my_malloc_hook(size_t, __const __malloc_ptr_t);
44 static void my_free_hook(__malloc_ptr_t, __const __malloc_ptr_t);
45 static __malloc_ptr_t my_realloc_hook(__malloc_ptr_t, size_t, __const __malloc_ptr_t);
46 static __malloc_ptr_t my_memalign_hook(size_t, size_t, __const __malloc_ptr_t);
47
48 #else
49
50 /** true if memory is not available **/
51 int fc_no_memory_hard = 1;
52
53 #endif // HAVE_VALID_HOOKS
54
55 #else
56
57 /** true if memory is not available **/
58 int fc_no_memory_hard = 1;
59
60 #endif // HAVE_MALLOC_H
61
62 /* flag to see if we are running */
63 static int fc_active_memory = 0;
64
65 /* init the memory hooks for memory profiling */
66 int fc_memory_init(void)
67 {
68 fc_active_memory = 1;
69 #ifdef HAVE_MALLOC_H
70 #ifdef HAVE_VALID_HOOKS
71
72 /* store the original pointers */
73 old_malloc_hook = __malloc_hook;
74 old_free_hook = __free_hook;
75 old_realloc_hook = __realloc_hook;
76 old_memalign_hook = __memalign_hook;
77
78 /* set our own pointers */
79 __malloc_hook = my_malloc_hook;
80 __free_hook = my_free_hook;
81 __realloc_hook = my_realloc_hook;
82 __memalign_hook = my_memalign_hook;
83 #endif // HAVE_VALID_HOOKS
84 #endif // HAVE_MALLOC_H
85 return 1;
86 }
87
88 /* stop the memory hooks */
89 int fc_memory_fini(void)
90 {
91 if (!fc_active_memory)
92 return 1;
93
94 #ifdef HAVE_MALLOC_H
95 #ifdef HAVE_VALID_HOOKS
96 /* just set the original pointers */
97 __malloc_hook = old_malloc_hook;
98 __free_hook = old_free_hook;
99 __realloc_hook = old_realloc_hook;
100 __memalign_hook = old_memalign_hook;
101 #endif // HAVE_VALID_HOOKS
102 #endif // HAVE_MALLOC_H
103
104 fc_active_memory = 0;
105
106 return 1;
107 }
108
109 #ifdef HAVE_MALLOC_H
110 #ifdef HAVE_VALID_HOOKS
111
112 /* my hooks */
113 __malloc_ptr_t my_malloc_hook(size_t size, __const __malloc_ptr_t where)
114 {
115 __malloc_ptr_t *result;
116
117 /* restore the old hook */
118 __malloc_hook = old_malloc_hook;
119 __free_hook = old_free_hook;
120 __realloc_hook = old_realloc_hook;
121 __memalign_hook = old_memalign_hook;
122
123 /* real call */
124 result = malloc(size);
125
126 /* re-save underlaying hook (cause it may change!) */
127 old_malloc_hook = __malloc_hook;
128 old_free_hook = __free_hook;
129 old_realloc_hook = __realloc_hook;
130 old_memalign_hook = __memalign_hook;
131
132 /* my treatments (protected, as they may call malloc) */
133 fc_com_malloc((void*) result, (unsigned int) size, (void*) where);
134
135 /* restore our own hooks */
136 __memalign_hook = my_memalign_hook;
137 __realloc_hook = my_realloc_hook;
138 __free_hook = my_free_hook;
139 __malloc_hook = my_malloc_hook;
140
141 return (result);
142 }
143
144 void my_free_hook(__malloc_ptr_t incoming, __const __malloc_ptr_t where)
145 {
146 /* restore the old hook */
147 __malloc_hook = old_malloc_hook;
148 __free_hook = old_free_hook;
149 __realloc_hook = old_realloc_hook;
150 __memalign_hook = old_memalign_hook;
151
152 /* real call */
153 free(incoming);
154
155 /* re-save underlaying hook (cause it may change!) */
156 old_malloc_hook = __malloc_hook;
157 old_free_hook = __free_hook;
158 old_realloc_hook = __realloc_hook;
159 old_memalign_hook = __memalign_hook;
160
161 /* my treatments (protected, as they may call free) */
162 fc_com_free((void*) incoming, (void*) where);
163
164 /* restore our own hooks */
165 __memalign_hook = my_memalign_hook;
166 __realloc_hook = my_realloc_hook;
167 __free_hook = my_free_hook;
168 __malloc_hook = my_malloc_hook;
169 }
170
171 __malloc_ptr_t my_realloc_hook(__malloc_ptr_t incoming, size_t size, __const __malloc_ptr_t where)
172 {
173 __malloc_ptr_t *result;
174
175 /* restore the old hook */
176 __malloc_hook = old_malloc_hook;
177 __free_hook = old_free_hook;
178 __realloc_hook = old_realloc_hook;
179 __memalign_hook = old_memalign_hook;
180
181 /* real call */
182 result = realloc(incoming, size);
183
184 /* re-save underlaying hook (cause it may change!) */
185 old_malloc_hook = __malloc_hook;
186 old_free_hook = __free_hook;
187 old_realloc_hook = __realloc_hook;
188 old_memalign_hook = __memalign_hook;
189
190 /* my treatments (protected, as they may call realloc) */
191 fc_com_realloc((void*) result, (void*) incoming,
192 (unsigned int) size, (void*) where);
193
194 /* restore our own hooks */
195 __memalign_hook = my_memalign_hook;
196 __realloc_hook = my_realloc_hook;
197 __free_hook = my_free_hook;
198 __malloc_hook = my_malloc_hook;
199
200 return (result);
201 }
202
203 __malloc_ptr_t my_memalign_hook(size_t size, size_t align, __const __malloc_ptr_t where)
204 {
205 __malloc_ptr_t *result;
206
207 /* restore the old hook */
208 __malloc_hook = old_malloc_hook;
209 __free_hook = old_free_hook;
210 __realloc_hook = old_realloc_hook;
211 __memalign_hook = old_memalign_hook;
212
213 /* real call */
214 result = memalign(align, size);
215
216 /* re-save underlaying hook (cause it may change!) */
217 old_malloc_hook = __malloc_hook;
218 old_free_hook = __free_hook;
219 old_realloc_hook = __realloc_hook;
220 old_memalign_hook = __memalign_hook;
221
222 /* my treatments (protected, as they may call memalign) */
223 fc_com_memalign((void*) result, (unsigned int) align,
224 (unsigned int) size, (void*) where);
225
226 /* restore our own hooks */
227 __memalign_hook = my_memalign_hook;
228 __realloc_hook = my_realloc_hook;
229 __free_hook = my_free_hook;
230 __malloc_hook = my_malloc_hook;
231
232 return (result);
233 }
234 #endif // HAVE_VALID_HOOKS
235 #endif // HAVE_MALLOC_H