blob: 9333de96c1706218c1c4fcfaa16e7782aba71668 [file] [log] [blame]
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -08001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29/*
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -080030 * Contains definition of structures, global variables, and implementation of
31 * routines that are used by malloc leak detection code and other components in
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080032 * the system. The trick is that some components expect these data and
33 * routines to be defined / implemented in libc.so library, regardless
34 * whether or not MALLOC_LEAK_CHECK macro is defined. To make things even
35 * more tricky, malloc leak detection code, implemented in
36 * libc_malloc_debug.so also requires access to these variables and routines
37 * (to fill allocation entry hash table, for example). So, all relevant
38 * variables and routines are defined / implemented here and exported
39 * to all, leak detection code and other components via dynamic (libc.so),
40 * or static (libc.a) linking.
41 */
42
43#include <stdlib.h>
44#include <pthread.h>
45#include <unistd.h>
46#include "dlmalloc.h"
47#include "malloc_debug_common.h"
48
49/*
50 * In a VM process, this is set to 1 after fork()ing out of zygote.
51 */
52int gMallocLeakZygoteChild = 0;
53
54pthread_mutex_t gAllocationsMutex = PTHREAD_MUTEX_INITIALIZER;
55HashTable gHashTable;
56
57// =============================================================================
58// output functions
59// =============================================================================
60
61static int hash_entry_compare(const void* arg1, const void* arg2)
62{
Christopher Tate52e7d3d2010-08-09 13:43:46 -070063 int result;
64
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080065 HashEntry* e1 = *(HashEntry**)arg1;
66 HashEntry* e2 = *(HashEntry**)arg2;
67
Christopher Tate52e7d3d2010-08-09 13:43:46 -070068 // if one or both arg pointers are null, deal gracefully
69 if (e1 == NULL) {
70 result = (e2 == NULL) ? 0 : 1;
71 } else if (e2 == NULL) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080072 result = -1;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080073 } else {
Christopher Tate52e7d3d2010-08-09 13:43:46 -070074 size_t nbAlloc1 = e1->allocations;
75 size_t nbAlloc2 = e2->allocations;
76 size_t size1 = e1->size & ~SIZE_FLAG_MASK;
77 size_t size2 = e2->size & ~SIZE_FLAG_MASK;
78 size_t alloc1 = nbAlloc1 * size1;
79 size_t alloc2 = nbAlloc2 * size2;
80
81 // sort in descending order by:
82 // 1) total size
83 // 2) number of allocations
84 //
85 // This is used for sorting, not determination of equality, so we don't
86 // need to compare the bit flags.
Christopher Tate52e7d3d2010-08-09 13:43:46 -070087 if (alloc1 > alloc2) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080088 result = -1;
Christopher Tate52e7d3d2010-08-09 13:43:46 -070089 } else if (alloc1 < alloc2) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080090 result = 1;
91 } else {
Christopher Tate52e7d3d2010-08-09 13:43:46 -070092 if (nbAlloc1 > nbAlloc2) {
93 result = -1;
94 } else if (nbAlloc1 < nbAlloc2) {
95 result = 1;
96 } else {
97 result = 0;
98 }
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080099 }
100 }
101 return result;
102}
103
104/*
105 * Retrieve native heap information.
106 *
107 * "*info" is set to a buffer we allocate
108 * "*overallSize" is set to the size of the "info" buffer
109 * "*infoSize" is set to the size of a single entry
110 * "*totalMemory" is set to the sum of all allocations we're tracking; does
111 * not include heap overhead
112 * "*backtraceSize" is set to the maximum number of entries in the back trace
113 */
114void get_malloc_leak_info(uint8_t** info, size_t* overallSize,
115 size_t* infoSize, size_t* totalMemory, size_t* backtraceSize)
116{
117 // don't do anything if we have invalid arguments
118 if (info == NULL || overallSize == NULL || infoSize == NULL ||
119 totalMemory == NULL || backtraceSize == NULL) {
120 return;
121 }
tedbo9d8be542010-10-05 13:06:06 -0700122 *totalMemory = 0;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800123
124 pthread_mutex_lock(&gAllocationsMutex);
125
126 if (gHashTable.count == 0) {
127 *info = NULL;
128 *overallSize = 0;
129 *infoSize = 0;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800130 *backtraceSize = 0;
131 goto done;
132 }
133
134 void** list = (void**)dlmalloc(sizeof(void*) * gHashTable.count);
135
136 // get the entries into an array to be sorted
137 int index = 0;
138 int i;
139 for (i = 0 ; i < HASHTABLE_SIZE ; i++) {
140 HashEntry* entry = gHashTable.slots[i];
141 while (entry != NULL) {
142 list[index] = entry;
143 *totalMemory = *totalMemory +
144 ((entry->size & ~SIZE_FLAG_MASK) * entry->allocations);
145 index++;
146 entry = entry->next;
147 }
148 }
149
150 // XXX: the protocol doesn't allow variable size for the stack trace (yet)
151 *infoSize = (sizeof(size_t) * 2) + (sizeof(intptr_t) * BACKTRACE_SIZE);
152 *overallSize = *infoSize * gHashTable.count;
153 *backtraceSize = BACKTRACE_SIZE;
154
155 // now get A byte array big enough for this
156 *info = (uint8_t*)dlmalloc(*overallSize);
157
158 if (*info == NULL) {
159 *overallSize = 0;
The Android Open Source Project95faece2010-04-08 11:11:53 -0700160 goto out_nomem_info;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800161 }
162
163 qsort((void*)list, gHashTable.count, sizeof(void*), hash_entry_compare);
164
165 uint8_t* head = *info;
166 const int count = gHashTable.count;
167 for (i = 0 ; i < count ; i++) {
168 HashEntry* entry = list[i];
169 size_t entrySize = (sizeof(size_t) * 2) + (sizeof(intptr_t) * entry->numEntries);
170 if (entrySize < *infoSize) {
171 /* we're writing less than a full entry, clear out the rest */
The Android Open Source Project95faece2010-04-08 11:11:53 -0700172 memset(head + entrySize, 0, *infoSize - entrySize);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800173 } else {
174 /* make sure the amount we're copying doesn't exceed the limit */
175 entrySize = *infoSize;
176 }
177 memcpy(head, &(entry->size), entrySize);
178 head += *infoSize;
179 }
180
The Android Open Source Project95faece2010-04-08 11:11:53 -0700181out_nomem_info:
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800182 dlfree(list);
183
184done:
185 pthread_mutex_unlock(&gAllocationsMutex);
186}
187
188void free_malloc_leak_info(uint8_t* info)
189{
190 dlfree(info);
191}
192
193struct mallinfo mallinfo()
194{
195 return dlmallinfo();
196}
197
Ian Rogers99908912012-08-17 17:28:15 -0700198size_t malloc_usable_size(void* mem)
199{
200 return dlmalloc_usable_size(mem);
201}
202
203void* valloc(size_t bytes)
204{
205 return dlvalloc(bytes);
206}
207
208void* pvalloc(size_t bytes)
209{
210 return dlpvalloc(bytes);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800211}
212
213/* Support for malloc debugging.
214 * Note that if USE_DL_PREFIX is not defined, it's assumed that memory
215 * allocation routines are implemented somewhere else, so all our custom
216 * malloc routines should not be compiled at all.
217 */
218#ifdef USE_DL_PREFIX
219
220/* Table for dispatching malloc calls, initialized with default dispatchers. */
221const MallocDebug __libc_malloc_default_dispatch __attribute__((aligned(32))) =
222{
223 dlmalloc, dlfree, dlcalloc, dlrealloc, dlmemalign
224};
225
226/* Selector of dispatch table to use for dispatching malloc calls. */
227const MallocDebug* __libc_malloc_dispatch = &__libc_malloc_default_dispatch;
228
229void* malloc(size_t bytes) {
230 return __libc_malloc_dispatch->malloc(bytes);
231}
232void free(void* mem) {
233 __libc_malloc_dispatch->free(mem);
234}
235void* calloc(size_t n_elements, size_t elem_size) {
236 return __libc_malloc_dispatch->calloc(n_elements, elem_size);
237}
238void* realloc(void* oldMem, size_t bytes) {
239 return __libc_malloc_dispatch->realloc(oldMem, bytes);
240}
241void* memalign(size_t alignment, size_t bytes) {
242 return __libc_malloc_dispatch->memalign(alignment, bytes);
243}
244
245/* We implement malloc debugging only in libc.so, so code bellow
246 * must be excluded if we compile this file for static libc.a
247 */
248#ifndef LIBC_STATIC
249#include <sys/system_properties.h>
250#include <dlfcn.h>
251#include "logd.h"
252
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800253/* Table for dispatching malloc calls, depending on environment. */
254static MallocDebug gMallocUse __attribute__((aligned(32))) = {
255 dlmalloc, dlfree, dlcalloc, dlrealloc, dlmemalign
256};
257
258extern char* __progname;
259
260/* Handle to shared library where actual memory allocation is implemented.
261 * This library is loaded and memory allocation calls are redirected there
262 * when libc.debug.malloc environment variable contains value other than
263 * zero:
264 * 1 - For memory leak detections.
265 * 5 - For filling allocated / freed memory with patterns defined by
266 * CHK_SENTINEL_VALUE, and CHK_FILL_FREE macros.
267 * 10 - For adding pre-, and post- allocation stubs in order to detect
268 * buffer overruns.
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800269 * Note that emulator's memory allocation instrumentation is not controlled by
270 * libc.debug.malloc value, but rather by emulator, started with -memcheck
271 * option. Note also, that if emulator has started with -memcheck option,
272 * emulator's instrumented memory allocation will take over value saved in
273 * libc.debug.malloc. In other words, if emulator has started with -memcheck
274 * option, libc.debug.malloc value is ignored.
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800275 * Actual functionality for debug levels 1-10 is implemented in
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800276 * libc_malloc_debug_leak.so, while functionality for emultor's instrumented
277 * allocations is implemented in libc_malloc_debug_qemu.so and can be run inside
278 * the emulator only.
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800279 */
280static void* libc_malloc_impl_handle = NULL;
281
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800282/* Make sure we have MALLOC_ALIGNMENT that matches the one that is
283 * used in dlmalloc. Emulator's memchecker needs this value to properly
284 * align its guarding zones.
285 */
286#ifndef MALLOC_ALIGNMENT
287#define MALLOC_ALIGNMENT ((size_t)8U)
288#endif /* MALLOC_ALIGNMENT */
289
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700290/* This variable is set to the value of property libc.debug.malloc.backlog,
291 * when the value of libc.debug.malloc = 10. It determines the size of the
292 * backlog we use to detect multiple frees. If the property is not set, the
293 * backlog length defaults to an internal constant defined in
294 * malloc_debug_check.c
295 */
296unsigned int malloc_double_free_backlog;
297
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800298/* Initializes memory allocation framework once per process. */
299static void malloc_init_impl(void)
300{
301 const char* so_name = NULL;
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800302 MallocDebugInit malloc_debug_initialize = NULL;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800303 unsigned int qemu_running = 0;
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800304 unsigned int debug_level = 0;
305 unsigned int memcheck_enabled = 0;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800306 char env[PROP_VALUE_MAX];
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800307 char memcheck_tracing[PROP_VALUE_MAX];
Iliyan Malchev7d2e24e2012-05-29 16:46:17 -0700308 char debug_program[PROP_VALUE_MAX];
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800309
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800310 /* Get custom malloc debug level. Note that emulator started with
311 * memory checking option will have priority over debug level set in
312 * libc.debug.malloc system property. */
313 if (__system_property_get("ro.kernel.qemu", env) && atoi(env)) {
314 qemu_running = 1;
315 if (__system_property_get("ro.kernel.memcheck", memcheck_tracing)) {
316 if (memcheck_tracing[0] != '0') {
317 // Emulator has started with memory tracing enabled. Enforce it.
318 debug_level = 20;
319 memcheck_enabled = 1;
320 }
321 }
322 }
323
324 /* If debug level has not been set by memcheck option in the emulator,
325 * lets grab it from libc.debug.malloc system property. */
326 if (!debug_level && __system_property_get("libc.debug.malloc", env)) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800327 debug_level = atoi(env);
328 }
329
330 /* Debug level 0 means that we should use dlxxx allocation
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800331 * routines (default). */
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800332 if (!debug_level) {
333 return;
334 }
335
Iliyan Malchev7d2e24e2012-05-29 16:46:17 -0700336 /* If libc.debug.malloc.program is set and is not a substring of progname,
337 * then exit.
338 */
339 if (__system_property_get("libc.debug.malloc.program", debug_program)) {
340 if (!strstr(__progname, debug_program)) {
341 return;
342 }
343 }
344
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800345 // Lets see which .so must be loaded for the requested debug level
346 switch (debug_level) {
347 case 1:
348 case 5:
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700349 case 10: {
350 char debug_backlog[PROP_VALUE_MAX];
351 if (__system_property_get("libc.debug.malloc.backlog", debug_backlog)) {
352 malloc_double_free_backlog = atoi(debug_backlog);
353 info_log("%s: setting backlog length to %d\n",
354 __progname, malloc_double_free_backlog);
355 }
356
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800357 so_name = "/system/lib/libc_malloc_debug_leak.so";
358 break;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700359 }
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800360 case 20:
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800361 // Quick check: debug level 20 can only be handled in emulator.
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800362 if (!qemu_running) {
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800363 error_log("%s: Debug level %d can only be set in emulator\n",
364 __progname, debug_level);
365 return;
366 }
367 // Make sure that memory checking has been enabled in emulator.
368 if (!memcheck_enabled) {
369 error_log("%s: Memory checking is not enabled in the emulator\n",
370 __progname);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800371 return;
372 }
373 so_name = "/system/lib/libc_malloc_debug_qemu.so";
374 break;
375 default:
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800376 error_log("%s: Debug level %d is unknown\n",
377 __progname, debug_level);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800378 return;
379 }
380
381 // Load .so that implements the required malloc debugging functionality.
382 libc_malloc_impl_handle = dlopen(so_name, RTLD_LAZY);
383 if (libc_malloc_impl_handle == NULL) {
384 error_log("%s: Missing module %s required for malloc debug level %d\n",
385 __progname, so_name, debug_level);
386 return;
387 }
388
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800389 // Initialize malloc debugging in the loaded module.
390 malloc_debug_initialize =
391 dlsym(libc_malloc_impl_handle, "malloc_debug_initialize");
392 if (malloc_debug_initialize == NULL) {
393 error_log("%s: Initialization routine is not found in %s\n",
394 __progname, so_name);
395 dlclose(libc_malloc_impl_handle);
396 return;
397 }
398 if (malloc_debug_initialize()) {
399 dlclose(libc_malloc_impl_handle);
400 return;
401 }
402
403 if (debug_level == 20) {
404 // For memory checker we need to do extra initialization.
405 int (*memcheck_initialize)(int, const char*) =
406 dlsym(libc_malloc_impl_handle, "memcheck_initialize");
407 if (memcheck_initialize == NULL) {
408 error_log("%s: memcheck_initialize routine is not found in %s\n",
409 __progname, so_name);
410 dlclose(libc_malloc_impl_handle);
411 return;
412 }
413 if (memcheck_initialize(MALLOC_ALIGNMENT, memcheck_tracing)) {
414 dlclose(libc_malloc_impl_handle);
415 return;
416 }
417 }
418
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800419 // Initialize malloc dispatch table with appropriate routines.
420 switch (debug_level) {
421 case 1:
422 __libc_android_log_print(ANDROID_LOG_INFO, "libc",
423 "%s using MALLOC_DEBUG = %d (leak checker)\n",
424 __progname, debug_level);
425 gMallocUse.malloc =
426 dlsym(libc_malloc_impl_handle, "leak_malloc");
427 gMallocUse.free =
428 dlsym(libc_malloc_impl_handle, "leak_free");
429 gMallocUse.calloc =
430 dlsym(libc_malloc_impl_handle, "leak_calloc");
431 gMallocUse.realloc =
432 dlsym(libc_malloc_impl_handle, "leak_realloc");
433 gMallocUse.memalign =
434 dlsym(libc_malloc_impl_handle, "leak_memalign");
435 break;
436 case 5:
437 __libc_android_log_print(ANDROID_LOG_INFO, "libc",
438 "%s using MALLOC_DEBUG = %d (fill)\n",
439 __progname, debug_level);
440 gMallocUse.malloc =
441 dlsym(libc_malloc_impl_handle, "fill_malloc");
442 gMallocUse.free =
443 dlsym(libc_malloc_impl_handle, "fill_free");
444 gMallocUse.calloc = dlcalloc;
445 gMallocUse.realloc =
446 dlsym(libc_malloc_impl_handle, "fill_realloc");
447 gMallocUse.memalign =
448 dlsym(libc_malloc_impl_handle, "fill_memalign");
449 break;
450 case 10:
451 __libc_android_log_print(ANDROID_LOG_INFO, "libc",
452 "%s using MALLOC_DEBUG = %d (sentinels, fill)\n",
453 __progname, debug_level);
454 gMallocUse.malloc =
455 dlsym(libc_malloc_impl_handle, "chk_malloc");
456 gMallocUse.free =
457 dlsym(libc_malloc_impl_handle, "chk_free");
458 gMallocUse.calloc =
459 dlsym(libc_malloc_impl_handle, "chk_calloc");
460 gMallocUse.realloc =
461 dlsym(libc_malloc_impl_handle, "chk_realloc");
462 gMallocUse.memalign =
463 dlsym(libc_malloc_impl_handle, "chk_memalign");
464 break;
465 case 20:
466 __libc_android_log_print(ANDROID_LOG_INFO, "libc",
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800467 "%s[%u] using MALLOC_DEBUG = %d (instrumented for emulator)\n",
468 __progname, getpid(), debug_level);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800469 gMallocUse.malloc =
470 dlsym(libc_malloc_impl_handle, "qemu_instrumented_malloc");
471 gMallocUse.free =
472 dlsym(libc_malloc_impl_handle, "qemu_instrumented_free");
473 gMallocUse.calloc =
474 dlsym(libc_malloc_impl_handle, "qemu_instrumented_calloc");
475 gMallocUse.realloc =
476 dlsym(libc_malloc_impl_handle, "qemu_instrumented_realloc");
477 gMallocUse.memalign =
478 dlsym(libc_malloc_impl_handle, "qemu_instrumented_memalign");
479 break;
480 default:
481 break;
482 }
483
484 // Make sure dispatch table is initialized
485 if ((gMallocUse.malloc == NULL) ||
486 (gMallocUse.free == NULL) ||
487 (gMallocUse.calloc == NULL) ||
488 (gMallocUse.realloc == NULL) ||
489 (gMallocUse.memalign == NULL)) {
490 error_log("%s: Cannot initialize malloc dispatch table for debug level"
491 " %d: %p, %p, %p, %p, %p\n",
492 __progname, debug_level,
493 gMallocUse.malloc, gMallocUse.free,
494 gMallocUse.calloc, gMallocUse.realloc,
495 gMallocUse.memalign);
496 dlclose(libc_malloc_impl_handle);
497 libc_malloc_impl_handle = NULL;
498 } else {
499 __libc_malloc_dispatch = &gMallocUse;
500 }
501}
502
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700503static void malloc_fini_impl(void)
504{
505 if (libc_malloc_impl_handle) {
506 MallocDebugFini malloc_debug_finalize = NULL;
507 malloc_debug_finalize =
508 dlsym(libc_malloc_impl_handle, "malloc_debug_finalize");
509 if (malloc_debug_finalize)
510 malloc_debug_finalize();
511 }
512}
513
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800514static pthread_once_t malloc_init_once_ctl = PTHREAD_ONCE_INIT;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700515static pthread_once_t malloc_fini_once_ctl = PTHREAD_ONCE_INIT;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800516
517#endif // !LIBC_STATIC
518#endif // USE_DL_PREFIX
519
520/* Initializes memory allocation framework.
521 * This routine is called from __libc_init routines implemented
522 * in libc_init_static.c and libc_init_dynamic.c files.
523 */
524void malloc_debug_init(void)
525{
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800526 /* We need to initialize malloc iff we implement here custom
527 * malloc routines (i.e. USE_DL_PREFIX is defined) for libc.so */
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800528#if defined(USE_DL_PREFIX) && !defined(LIBC_STATIC)
529 if (pthread_once(&malloc_init_once_ctl, malloc_init_impl)) {
530 error_log("Unable to initialize malloc_debug component.");
531 }
532#endif // USE_DL_PREFIX && !LIBC_STATIC
533}
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700534
535void malloc_debug_fini(void)
536{
537 /* We need to finalize malloc iff we implement here custom
538 * malloc routines (i.e. USE_DL_PREFIX is defined) for libc.so */
539#if defined(USE_DL_PREFIX) && !defined(LIBC_STATIC)
540 if (pthread_once(&malloc_fini_once_ctl, malloc_fini_impl)) {
541 error_log("Unable to finalize malloc_debug component.");
542 }
543#endif // USE_DL_PREFIX && !LIBC_STATIC
544}