blob: b63ddb4f890fee88a979675aee69fc80564a74d7 [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/*
30 * Contains implementation of memeory allocation routines instrumented for
31 * usage in the emulator to detect memory allocation violations, such as
32 * memory leaks, buffer overruns, etc.
33 */
34
35#include <stdlib.h>
36#include <pthread.h>
37#include <unistd.h>
38#include "dlmalloc.h"
39#include "logd.h"
40
41// This file should be included into the build only when
42// MALLOC_QEMU_INSTRUMENT macro is defined.
43#ifndef MALLOC_QEMU_INSTRUMENT
44#error MALLOC_QEMU_INSTRUMENT is not defined.
45#endif // MALLOC_QEMU_INSTRUMENT
46
47// =============================================================================
48// log functions
49// =============================================================================
50
51#define debug_log(format, ...) \
52 __libc_android_log_print(ANDROID_LOG_DEBUG, "malloc_qemu", (format), ##__VA_ARGS__ )
53#define error_log(format, ...) \
54 __libc_android_log_print(ANDROID_LOG_ERROR, "malloc_qemu", (format), ##__VA_ARGS__ )
55#define info_log(format, ...) \
56 __libc_android_log_print(ANDROID_LOG_INFO, "malloc_qemu", (format), ##__VA_ARGS__ )
57
58void* qemu_instrumented_malloc(size_t bytes)
59{
60 return dlmalloc(bytes);
61}
62
63void qemu_instrumented_free(void* mem)
64{
65 dlfree(mem);
66}
67
68void* qemu_instrumented_calloc(size_t n_elements, size_t elem_size)
69{
70 return dlcalloc(n_elements, elem_size);
71}
72
73void* qemu_instrumented_realloc(void* mem, size_t bytes)
74{
75 return dlrealloc(mem, bytes);
76}
77
78void* qemu_instrumented_memalign(size_t alignment, size_t bytes)
79{
80 return dlmemalign(alignment, bytes);
81}