4 * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
8 * Jaewon Lim <jaewon81.lim@samsung.com>
9 * Woojin Jung <woojin2.jung@samsung.com>
10 * Juyoung Kim <j0.kim@samsung.com>
11 * Anastasia Lyupa <a.lyupa@samsung.com>
12 * Dmitry Bogatov <d.bogatov@samsung.com>
14 * This library is free software; you can redistribute it and/or modify it under
15 * the terms of the GNU Lesser General Public License as published by the
16 * Free Software Foundation; either version 2.1 of the License, or (at your option)
19 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
20 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
22 * License for more details.
24 * You should have received a copy of the GNU Lesser General Public License
25 * along with this library; if not, write to the Free Software Foundation, Inc., 51
26 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 * - Samsung RnD Institute Russia
43 #include "probeinfo.h"
44 #include "dacollection.h"
46 #include "da_memory.h"
49 static enum DaOptions _sopt = OPT_ALLOC;
51 void *malloc(size_t size)
53 static void* (*mallocp)(size_t);
54 DECLARE_VARIABLE_STANDARD;
57 rtdl_next_current_set_once(mallocp);
61 pret = (*mallocp)(size);
63 if(pret != NULL && gProbeBlockCount == 0)
64 add_memory_hash(pret, size, MEMTYPE_ALLOC,
65 blockresult ? MEM_EXTERNAL : MEM_INTERNAL);
68 POST_PACK_PROBEBLOCK_BEGIN();
71 PACK_COMMON_BEGIN(MSG_PROBE_MEMORY, API_ID_malloc,
73 PACK_COMMON_END('p', pret, newerrno, blockresult);
74 PACK_MEMORY(size, MEMORY_API_ALLOC, pret);
77 POST_PACK_PROBEBLOCK_END();
84 static void (*freep)(void *);
85 DECLARE_VARIABLE_STANDARD;
87 rtdl_next_current_set_once(freep);
91 if(ptr != NULL && gProbeBlockCount == 0)
92 del_memory_hash(ptr, MEMTYPE_FREE, NULL);
96 POST_PACK_PROBEBLOCK_BEGIN();
99 PACK_COMMON_BEGIN(MSG_PROBE_MEMORY, API_ID_free,
100 "p", (int64_t)(int) ptr);
101 PACK_COMMON_END('v', 0, newerrno, blockresult);
102 PACK_MEMORY(0, MEMORY_API_FREE, ptr);
105 POST_PACK_PROBEBLOCK_END();
108 void *realloc(void *memblock, size_t size)
110 static void* (*reallocp)(void*, size_t);
111 DECLARE_VARIABLE_STANDARD;
114 rtdl_next_current_set_once(reallocp);
117 if(memblock != NULL && gProbeBlockCount == 0)
118 del_memory_hash(memblock, MEMTYPE_FREE, NULL);
120 pret = (*reallocp)(memblock, size);
122 if(pret != NULL && gProbeBlockCount == 0)
123 add_memory_hash(pret, size, MEMTYPE_ALLOC,
124 blockresult ? MEM_EXTERNAL : MEM_INTERNAL);
126 POST_PACK_PROBEBLOCK_BEGIN();
129 PACK_COMMON_BEGIN(MSG_PROBE_MEMORY, API_ID_realloc,
130 "px", voidp_to_uint64(memblock), (uint64_t) size);
131 PACK_COMMON_END('p', pret, newerrno, blockresult);
132 PACK_MEMORY(size, MEMORY_API_ALLOC, pret);
135 POST_PACK_PROBEBLOCK_END();
141 void *temp_calloc(size_t nelem, size_t elsize)
143 /* Magic number, but somewhy it is sufficent */
144 static char extra_mem[20] = {0};
145 return (nelem * elsize > sizeof(extra_mem))
150 void *calloc(size_t nelem, size_t elsize)
152 static void* (*callocp)(size_t, size_t);
153 DECLARE_VARIABLE_STANDARD;
158 * Calloc is called by `dlsym`, so we provide small amount
159 * of static memory via `temp_calloc`.
161 callocp = temp_calloc;
162 callocp = rtdl_next(__func__);
167 pret = (*callocp)(nelem, elsize);
169 if(pret != NULL && gProbeBlockCount == 0)
170 add_memory_hash(pret, nelem * elsize, MEMTYPE_ALLOC,
171 blockresult ? MEM_EXTERNAL : MEM_INTERNAL);
173 POST_PACK_PROBEBLOCK_BEGIN();
176 PACK_COMMON_BEGIN(MSG_PROBE_MEMORY, API_ID_calloc,
177 "xx", (uint64_t)nelem, (uint64_t)elsize);
178 PACK_COMMON_END('p', pret, newerrno, blockresult);
179 PACK_MEMORY(nelem * elsize, MEMORY_API_ALLOC, pret);
182 POST_PACK_PROBEBLOCK_END();