mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-04-25 17:25:08 +02:00
Use mimalloc for Ladybird-owned allocations without overriding malloc(). Route kmalloc(), kcalloc(), krealloc(), and kfree() through mimalloc, and put the embedded Rust crates on the same allocator via a shared shim in AK/kmalloc.cpp. This also lets us drop kfree_sized(), since it no longer used its size argument. StringData, Utf16StringData, JS object storage, Rust error strings, and the CoreAudio playback helpers can all free their AK-backed storage with plain kfree(). Sanitizer builds still use the system allocator. LeakSanitizer does not reliably trace references stored in mimalloc-managed AK containers, so static caches and other long-lived roots can look leaked. Pass the old size into the Rust realloc shim so aligned fallback reallocations can move posix_memalign-backed blocks safely. Static builds still need a little linker help. macOS app binaries need the Rust allocator entry points forced in from liblagom-ak.a, while static ELF links can pull in identical allocator shim definitions from multiple Rust staticlibs. Keep the Apple -u flags and allow those duplicate shim symbols for LibJS and LibRegex links on Linux and BSD.
69 lines
1.5 KiB
C++
69 lines
1.5 KiB
C++
/*
|
|
* Copyright (c) 2018-2021, Andreas Kling <andreas@ladybird.org>
|
|
* Copyright (c) 2021, Daniel Bertalan <dani@danielbertalan.dev>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/Checked.h>
|
|
#include <AK/Platform.h>
|
|
#include <new>
|
|
#include <stdlib.h>
|
|
|
|
#if defined(AK_OS_SERENITY)
|
|
# define kcalloc calloc
|
|
# define kfree free
|
|
# define kmalloc malloc
|
|
# define krealloc realloc
|
|
# define kmalloc_good_size malloc_good_size
|
|
#else
|
|
[[nodiscard]] void* ak_kcalloc(size_t count, size_t size);
|
|
void ak_kfree(void* ptr);
|
|
[[nodiscard]] void* ak_kmalloc(size_t size);
|
|
[[nodiscard]] void* ak_krealloc(void* ptr, size_t size);
|
|
[[nodiscard]] size_t ak_kmalloc_good_size(size_t size);
|
|
|
|
[[nodiscard]] inline void* kcalloc(size_t count, size_t size)
|
|
{
|
|
return ak_kcalloc(count, size);
|
|
}
|
|
|
|
inline void kfree(void* ptr)
|
|
{
|
|
ak_kfree(ptr);
|
|
}
|
|
|
|
[[nodiscard]] inline void* kmalloc(size_t size)
|
|
{
|
|
return ak_kmalloc(size);
|
|
}
|
|
|
|
[[nodiscard]] inline void* krealloc(void* ptr, size_t size)
|
|
{
|
|
return ak_krealloc(ptr, size);
|
|
}
|
|
|
|
[[nodiscard]] inline size_t kmalloc_good_size(size_t size)
|
|
{
|
|
return ak_kmalloc_good_size(size);
|
|
}
|
|
#endif
|
|
|
|
using std::nothrow;
|
|
|
|
inline void* kmalloc_array(AK::Checked<size_t> a, AK::Checked<size_t> b)
|
|
{
|
|
auto size = a * b;
|
|
VERIFY(!size.has_overflow());
|
|
return kmalloc(size.value());
|
|
}
|
|
|
|
inline void* kmalloc_array(AK::Checked<size_t> a, AK::Checked<size_t> b, AK::Checked<size_t> c)
|
|
{
|
|
auto size = a * b * c;
|
|
VERIFY(!size.has_overflow());
|
|
return kmalloc(size.value());
|
|
}
|