mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:57:45 +00:00
LibC: Avoid double memory clearing in calloc()
calloc() was internally calling malloc_impl() which would scrub out all the allocated memory with the scrub byte (0xdc). We would then immediately zero-fill the memory. This was obviously a waste of time, and our hash tables were doing it all the time. :^)
This commit is contained in:
parent
0c44deeee2
commit
bb9cf083c4
1 changed files with 9 additions and 4 deletions
|
@ -166,7 +166,12 @@ static void os_free(void* ptr, size_t size)
|
||||||
assert(rc == 0);
|
assert(rc == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void* malloc_impl(size_t size)
|
enum class CallerWillInitializeMemory {
|
||||||
|
No,
|
||||||
|
Yes,
|
||||||
|
};
|
||||||
|
|
||||||
|
static void* malloc_impl(size_t size, CallerWillInitializeMemory caller_will_initialize_memory)
|
||||||
{
|
{
|
||||||
LOCKER(malloc_lock());
|
LOCKER(malloc_lock());
|
||||||
|
|
||||||
|
@ -265,7 +270,7 @@ static void* malloc_impl(size_t size)
|
||||||
}
|
}
|
||||||
dbgln_if(MALLOC_DEBUG, "LibC: allocated {:p} (chunk in block {:p}, size {})", ptr, block, block->bytes_per_chunk());
|
dbgln_if(MALLOC_DEBUG, "LibC: allocated {:p} (chunk in block {:p}, size {})", ptr, block, block->bytes_per_chunk());
|
||||||
|
|
||||||
if (s_scrub_malloc)
|
if (s_scrub_malloc && caller_will_initialize_memory == CallerWillInitializeMemory::No)
|
||||||
memset(ptr, MALLOC_SCRUB_BYTE, block->m_size);
|
memset(ptr, MALLOC_SCRUB_BYTE, block->m_size);
|
||||||
|
|
||||||
ue_notify_malloc(ptr, size);
|
ue_notify_malloc(ptr, size);
|
||||||
|
@ -356,7 +361,7 @@ static void free_impl(void* ptr)
|
||||||
|
|
||||||
[[gnu::flatten]] void* malloc(size_t size)
|
[[gnu::flatten]] void* malloc(size_t size)
|
||||||
{
|
{
|
||||||
void* ptr = malloc_impl(size);
|
void* ptr = malloc_impl(size, CallerWillInitializeMemory::No);
|
||||||
if (s_profiling)
|
if (s_profiling)
|
||||||
perf_event(PERF_EVENT_MALLOC, size, reinterpret_cast<FlatPtr>(ptr));
|
perf_event(PERF_EVENT_MALLOC, size, reinterpret_cast<FlatPtr>(ptr));
|
||||||
return ptr;
|
return ptr;
|
||||||
|
@ -373,7 +378,7 @@ static void free_impl(void* ptr)
|
||||||
void* calloc(size_t count, size_t size)
|
void* calloc(size_t count, size_t size)
|
||||||
{
|
{
|
||||||
size_t new_size = count * size;
|
size_t new_size = count * size;
|
||||||
auto* ptr = malloc(new_size);
|
auto* ptr = malloc_impl(new_size, CallerWillInitializeMemory::Yes);
|
||||||
if (ptr)
|
if (ptr)
|
||||||
memset(ptr, 0, new_size);
|
memset(ptr, 0, new_size);
|
||||||
return ptr;
|
return ptr;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue