Skip to content

Commit e0937f6

Browse files
committed
adding freezero and quick tests
1 parent 2729c8e commit e0937f6

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/override/malloc.cc

+16
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,22 @@ extern "C"
7171
}
7272
return ThreadAlloc::get().alloc<ZeroMem::YesZero, CoreDumpMem::NoDump>(sz);
7373
}
74+
75+
SNMALLOC_EXPORT void SNMALLOC_NAME_MANGLE(freezero)(void* p, size_t size)
76+
{
77+
if (SNMALLOC_UNLIKELY(p == nullptr))
78+
{
79+
return;
80+
}
81+
82+
size_t sz = bits::min(size, ThreadAlloc::get().alloc_size(p));
83+
/* we are not trying to be fast, here but disallowing to potentially
84+
* optimize away the memset call */
85+
void* (*volatile memset_fn)(void*, int, size_t) = memset;
86+
memset_fn(p, 0, sz);
87+
88+
ThreadAlloc::get().dealloc(p);
89+
}
7490
#endif
7591

7692
SNMALLOC_EXPORT

src/test/func/malloc/malloc.cc

+11
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,17 @@ int main(int argc, char** argv)
229229
check_result(size + 1, 1, our_malloc_conceal(size + 1), SUCCESS, false);
230230
}
231231

232+
our_freezero(nullptr, 1024);
233+
void* p = our_malloc_conceal(64);
234+
our_freezero(p, 128);
235+
if (((uint8_t*)p)[63] != 0)
236+
{
237+
abort();
238+
}
239+
240+
p = our_malloc_conceal(16);
241+
our_freezero(p, 0);
242+
232243
for (smallsizeclass_t sc = 0; sc < NUM_SMALL_SIZECLASSES; sc++)
233244
{
234245
const size_t size = sizeclass_to_size(sc);

0 commit comments

Comments
 (0)