1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 13:38:11 +00:00

AK+Kernel: Avoid double memory clearing of HashTable buckets

Since the allocated memory is going to be zeroed immediately anyway,
let's avoid redundantly scrubbing it with MALLOC_SCRUB_BYTE just before
that.

The latest versions of gcc and Clang can automatically do this malloc +
memset -> calloc optimization, but I've seen a couple of places where it
failed to be done.

This commit also adds a naive kcalloc function to the kernel that
doesn't (yet) eliminate the redundancy like the userland does.
This commit is contained in:
Daniel Bertalan 2022-03-14 23:59:16 +01:00 committed by Andreas Kling
parent cd21e03225
commit e3eb68dd58
4 changed files with 15 additions and 2 deletions

View file

@ -449,6 +449,18 @@ void* kmalloc(size_t size)
return ptr;
}
void* kcalloc(size_t count, size_t size)
{
if (Checked<size_t>::multiplication_would_overflow(count, size))
return nullptr;
size_t new_size = count * size;
auto* ptr = kmalloc(new_size);
// FIXME: Avoid redundantly scrubbing the memory in kmalloc()
if (ptr)
memset(ptr, 0, new_size);
return ptr;
}
void kfree_sized(void* ptr, size_t size)
{
if (!ptr)