Add trace support for calloc and realloc.
[lttng-ust.git] / liblttng-ust-libc-wrapper / lttng-ust-malloc.c
CommitLineData
b27f8e75
MD
1/*
2 * Copyright (C) 2009 Pierre-Marc Fournier
1622ba22 3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
c39c72ee
PMF
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library 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 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
e541a28d
PMF
20#define _GNU_SOURCE
21#include <dlfcn.h>
22#include <sys/types.h>
23#include <stdio.h>
f95b2888 24#include <pthread.h>
1622ba22
MD
25
26#define TRACEPOINT_DEFINE
27#define TRACEPOINT_CREATE_PROBES
28#include "ust_libc.h"
fbd8191b 29
f95b2888
SS
30#define STATIC_CALLOC_LEN 4096
31static char static_calloc_buf[STATIC_CALLOC_LEN];
32static size_t static_calloc_buf_offset;
33static pthread_mutex_t static_calloc_mutex = PTHREAD_MUTEX_INITIALIZER;
34
35static void *static_calloc(size_t nmemb, size_t size)
36{
37 size_t prev_offset;
38
39 pthread_mutex_lock(&static_calloc_mutex);
40 if (nmemb * size > sizeof(static_calloc_buf) - static_calloc_buf_offset) {
41 pthread_mutex_unlock(&static_calloc_mutex);
42 return NULL;
43 }
44 prev_offset = static_calloc_buf_offset;
45 static_calloc_buf_offset += nmemb * size;
46 pthread_mutex_unlock(&static_calloc_mutex);
47 return &static_calloc_buf[prev_offset];
48}
49
e541a28d
PMF
50void *malloc(size_t size)
51{
f95b2888 52 static void *(*plibc_malloc)(size_t size);
1c184644
PMF
53 void *retval;
54
b27f8e75 55 if (plibc_malloc == NULL) {
e541a28d 56 plibc_malloc = dlsym(RTLD_NEXT, "malloc");
b27f8e75 57 if (plibc_malloc == NULL) {
e541a28d
PMF
58 fprintf(stderr, "mallocwrap: unable to find malloc\n");
59 return NULL;
60 }
61 }
1c184644 62 retval = plibc_malloc(size);
1622ba22 63 tracepoint(ust_libc, malloc, size, retval);
1c184644
PMF
64 return retval;
65}
66
67void free(void *ptr)
68{
f95b2888
SS
69 static void (*plibc_free)(void *ptr);
70
71 /* Check whether the memory was allocated with
72 * static_calloc, in which case there is nothing
73 * to free.
74 */
75 if ((char *)ptr >= static_calloc_buf &&
76 (char *)ptr < static_calloc_buf + STATIC_CALLOC_LEN) {
77 return;
78 }
1c184644 79
b27f8e75 80 if (plibc_free == NULL) {
1c184644 81 plibc_free = dlsym(RTLD_NEXT, "free");
b27f8e75 82 if (plibc_free == NULL) {
1c184644 83 fprintf(stderr, "mallocwrap: unable to find free\n");
eb0f0951 84 return;
1c184644
PMF
85 }
86 }
1622ba22 87 tracepoint(ust_libc, free, ptr);
eb0f0951 88 plibc_free(ptr);
e541a28d 89}
f95b2888
SS
90
91void *calloc(size_t nmemb, size_t size)
92{
93 static void *(*volatile plibc_calloc)(size_t nmemb, size_t size);
94 void *retval;
95
96 if (plibc_calloc == NULL) {
97 /*
98 * Temporarily redirect to static_calloc,
99 * until the dlsym lookup has completed.
100 */
101 plibc_calloc = static_calloc;
102 plibc_calloc = dlsym(RTLD_NEXT, "calloc");
103 if (plibc_calloc == NULL) {
104 fprintf(stderr, "callocwrap: unable to find calloc\n");
105 return NULL;
106 }
107 }
108 retval = plibc_calloc(nmemb, size);
109 tracepoint(ust_libc, calloc, nmemb, size, retval);
110 return retval;
111}
112
113void *realloc(void *ptr, size_t size)
114{
115 static void *(*plibc_realloc)(void *ptr, size_t size);
116 void *retval;
117
118 if (plibc_realloc == NULL) {
119 plibc_realloc = dlsym(RTLD_NEXT, "realloc");
120 if (plibc_realloc == NULL) {
121 fprintf(stderr, "reallocwrap: unable to find realloc\n");
122 return NULL;
123 }
124 }
125 retval = plibc_realloc(ptr, size);
126 tracepoint(ust_libc, realloc, ptr, size, retval);
127 return retval;
128}
This page took 0.033084 seconds and 4 git commands to generate.