start adding LGPL headers
[ust.git] / libmallocwrap / mallocwrap.c
1 /* Copyright (C) 2009 Pierre-Marc Fournier
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18 #define _GNU_SOURCE
19 #include <dlfcn.h>
20 #include <sys/types.h>
21 #include <stdio.h>
22
23 #include "marker.h"
24
25 #if 0
26 INTERCEPT_PROTOTYPE(void, malloc, size_t size)
27 INTERCEPT_TRACE("size %d", size)
28 INTERCEPT_CALL_ARGS(size)
29 INTERCEPT()
30
31 #define INTERCEPT_FUNC(type, name, args...) \
32 __I_FUNC_TYPE(type) \
33 __I_FUNC_NAME(name) \
34 __I_FUNC_ARGS(args)
35
36 #define INTERCEPT_TRACE(fmt, args...) \
37 #define __I_TRACE_FMT fmt \
38 #define __I_TRACE_ARGS args
39
40 #define INTERCEPT_CALL_ARGS(args...) \
41 #define __I_CALL_ARGS args
42
43 #define INTERCEPT() \
44 __I_FUNC_TYPE __I_FUNC_NAME(__I_FUNC_ARGS) \
45 { \
46 static __I_FUNC_TYPE (*plibc_ ## __I_FUNC_NAME)(args) = NULL; \
47 \
48 if(plibc_ ## __I_FUNC_NAME == NULL) { \
49 plibc_ ## __I_FUNC_NAME = dlsym(RTLD_NEXT, "malloc"); \
50 if(plibc_ ## __I_FUNC_NAME == NULL) { \
51 fprintf(stderr, "mallocwrap: unable to find malloc\n"); \
52 return NULL; \
53 } \
54 } \
55 \
56 trace_mark(ust, __I_FUNC_NAME, __I_TRACE_FMT, __I_TRACE_ARGS); \
57 \
58 return plibc_ ## __I_FUNC_NAME (__I_CALL_ARGS); \
59 }
60 #endif
61
62 void *malloc(size_t size)
63 {
64 static void *(*plibc_malloc)(size_t size) = NULL;
65
66 void *retval;
67
68 if(plibc_malloc == NULL) {
69 plibc_malloc = dlsym(RTLD_NEXT, "malloc");
70 if(plibc_malloc == NULL) {
71 fprintf(stderr, "mallocwrap: unable to find malloc\n");
72 return NULL;
73 }
74 }
75
76 retval = plibc_malloc(size);
77
78 trace_mark(ust, malloc, "size %d ptr %p", (int)size, retval);
79
80 return retval;
81 }
82
83 void free(void *ptr)
84 {
85 static void *(*plibc_free)(void *ptr) = NULL;
86
87 if(plibc_free == NULL) {
88 plibc_free = dlsym(RTLD_NEXT, "free");
89 if(plibc_free == NULL) {
90 fprintf(stderr, "mallocwrap: unable to find free\n");
91 return NULL;
92 }
93 }
94
95 trace_mark(ust, free, "%p", ptr);
96
97 return plibc_free(ptr);
98 }
99
100 MARKER_LIB
This page took 0.030241 seconds and 4 git commands to generate.