start adding LGPL headers
[ust.git] / libmallocwrap / mallocwrap.c
CommitLineData
c39c72ee
PMF
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
e541a28d
PMF
18#define _GNU_SOURCE
19#include <dlfcn.h>
20#include <sys/types.h>
21#include <stdio.h>
22
fbd8191b
PMF
23#include "marker.h"
24
c61f7385
PMF
25#if 0
26INTERCEPT_PROTOTYPE(void, malloc, size_t size)
27INTERCEPT_TRACE("size %d", size)
28INTERCEPT_CALL_ARGS(size)
29INTERCEPT()
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
e541a28d
PMF
61
62void *malloc(size_t size)
63{
1c184644
PMF
64 static void *(*plibc_malloc)(size_t size) = NULL;
65
66 void *retval;
67
e541a28d
PMF
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 }
fbd8191b 75
1c184644
PMF
76 retval = plibc_malloc(size);
77
78 trace_mark(ust, malloc, "size %d ptr %p", (int)size, retval);
79
80 return retval;
81}
82
83void 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);
fbd8191b 96
1c184644 97 return plibc_free(ptr);
e541a28d 98}
fbd8191b
PMF
99
100MARKER_LIB
This page took 0.025591 seconds and 4 git commands to generate.