2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2018-10-10 06:53:07 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
2026-01-22 09:07:37 -03:00
|
|
|
#include <AK/AtomicRefCounted.h>
|
2025-04-06 10:53:04 -03:00
|
|
|
#include <AK/NonnullRefPtr.h>
|
2020-07-27 09:15:37 -03:00
|
|
|
#include <AK/Span.h>
|
2019-06-18 04:26:36 -03:00
|
|
|
#include <AK/Types.h>
|
|
|
|
|
#include <AK/kmalloc.h>
|
2018-10-10 06:53:07 -03:00
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
2019-06-07 12:13:23 -03:00
|
|
|
enum ShouldChomp {
|
2019-05-28 06:53:16 -03:00
|
|
|
NoChomp,
|
|
|
|
|
Chomp
|
|
|
|
|
};
|
2018-11-06 21:19:35 -02:00
|
|
|
|
2021-07-11 08:23:13 -03:00
|
|
|
size_t allocation_size_for_stringimpl(size_t length);
|
|
|
|
|
|
2026-01-22 09:07:37 -03:00
|
|
|
class ByteStringImpl : public AtomicRefCounted<ByteStringImpl> {
|
2018-10-10 06:53:07 -03:00
|
|
|
public:
|
2025-04-06 11:26:37 -03:00
|
|
|
static NonnullRefPtr<ByteStringImpl const> create_uninitialized(size_t length, char*& buffer);
|
|
|
|
|
static NonnullRefPtr<ByteStringImpl const> create(char const* cstring, ShouldChomp = NoChomp);
|
|
|
|
|
static NonnullRefPtr<ByteStringImpl const> create(char const* cstring, size_t length, ShouldChomp = NoChomp);
|
|
|
|
|
static NonnullRefPtr<ByteStringImpl const> create(ReadonlyBytes, ShouldChomp = NoChomp);
|
2023-02-19 18:59:26 -03:00
|
|
|
|
2019-06-18 04:26:36 -03:00
|
|
|
void operator delete(void* ptr)
|
|
|
|
|
{
|
AK: Adopt mimalloc v2 as main allocator
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.
2026-04-03 12:06:58 -03:00
|
|
|
kfree(ptr);
|
2019-06-18 04:26:36 -03:00
|
|
|
}
|
|
|
|
|
|
2025-04-06 11:26:37 -03:00
|
|
|
static ByteStringImpl& the_empty_stringimpl();
|
2018-10-10 06:53:07 -03:00
|
|
|
|
2025-04-06 11:26:37 -03:00
|
|
|
~ByteStringImpl();
|
2018-10-10 06:53:07 -03:00
|
|
|
|
2019-12-09 13:45:40 -03:00
|
|
|
size_t length() const { return m_length; }
|
2020-08-23 07:56:46 -03:00
|
|
|
// Includes NUL-terminator.
|
2022-04-01 14:58:27 -03:00
|
|
|
char const* characters() const { return &m_inline_buffer[0]; }
|
2020-07-27 09:15:37 -03:00
|
|
|
|
2025-08-30 03:18:47 -03:00
|
|
|
ALWAYS_INLINE ReadonlyBytes bytes() const LIFETIME_BOUND { return { characters(), length() }; }
|
|
|
|
|
ALWAYS_INLINE StringView view() const LIFETIME_BOUND { return { characters(), length() }; }
|
2020-07-27 09:15:37 -03:00
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
char const& operator[](size_t i) const
|
2019-05-28 06:53:16 -03:00
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(i < m_length);
|
2019-06-20 08:21:56 -03:00
|
|
|
return characters()[i];
|
2019-05-28 06:53:16 -03:00
|
|
|
}
|
2018-10-10 06:53:07 -03:00
|
|
|
|
2025-04-06 11:26:37 -03:00
|
|
|
bool operator==(ByteStringImpl const& other) const
|
2020-10-05 12:15:49 -03:00
|
|
|
{
|
|
|
|
|
if (length() != other.length())
|
|
|
|
|
return false;
|
2021-11-06 17:12:16 -03:00
|
|
|
return __builtin_memcmp(characters(), other.characters(), length()) == 0;
|
2020-10-05 12:15:49 -03:00
|
|
|
}
|
|
|
|
|
|
2018-10-10 06:53:07 -03:00
|
|
|
unsigned hash() const
|
|
|
|
|
{
|
2019-06-20 08:21:56 -03:00
|
|
|
if (!m_has_hash)
|
2018-12-20 23:10:45 -02:00
|
|
|
compute_hash();
|
2018-10-10 06:53:07 -03:00
|
|
|
return m_hash;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-13 07:05:19 -03:00
|
|
|
unsigned existing_hash() const
|
|
|
|
|
{
|
|
|
|
|
return m_hash;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-18 18:56:53 -03:00
|
|
|
unsigned case_insensitive_hash() const;
|
|
|
|
|
|
2018-10-10 06:53:07 -03:00
|
|
|
private:
|
2026-01-23 06:31:09 -03:00
|
|
|
friend struct EmptyByteStringImpl;
|
|
|
|
|
|
2019-06-07 12:13:23 -03:00
|
|
|
enum ConstructTheEmptyStringImplTag {
|
2019-05-28 06:53:16 -03:00
|
|
|
ConstructTheEmptyStringImpl
|
|
|
|
|
};
|
2026-01-23 06:31:09 -03:00
|
|
|
explicit constexpr ByteStringImpl(ConstructTheEmptyStringImplTag)
|
2019-05-28 06:53:16 -03:00
|
|
|
{
|
|
|
|
|
}
|
2018-10-10 06:53:07 -03:00
|
|
|
|
2019-06-07 12:13:23 -03:00
|
|
|
enum ConstructWithInlineBufferTag {
|
2019-05-28 06:53:16 -03:00
|
|
|
ConstructWithInlineBuffer
|
|
|
|
|
};
|
2025-04-06 11:26:37 -03:00
|
|
|
ByteStringImpl(ConstructWithInlineBufferTag, size_t length);
|
2018-10-10 06:53:07 -03:00
|
|
|
|
2018-12-20 23:10:45 -02:00
|
|
|
void compute_hash() const;
|
2018-10-10 06:53:07 -03:00
|
|
|
|
2019-12-09 13:45:40 -03:00
|
|
|
size_t m_length { 0 };
|
2018-10-10 06:53:07 -03:00
|
|
|
mutable unsigned m_hash { 0 };
|
2019-06-20 08:21:56 -03:00
|
|
|
mutable bool m_has_hash { false };
|
2018-12-20 23:10:45 -02:00
|
|
|
char m_inline_buffer[0];
|
2018-10-10 06:53:07 -03:00
|
|
|
};
|
|
|
|
|
|
2021-07-11 08:23:13 -03:00
|
|
|
inline size_t allocation_size_for_stringimpl(size_t length)
|
|
|
|
|
{
|
2025-04-06 11:26:37 -03:00
|
|
|
return sizeof(ByteStringImpl) + (sizeof(char) * length) + sizeof(char);
|
2021-07-11 08:23:13 -03:00
|
|
|
}
|
|
|
|
|
|
2020-10-07 08:24:46 -03:00
|
|
|
template<>
|
2025-04-06 11:26:37 -03:00
|
|
|
struct Formatter<ByteStringImpl> : Formatter<StringView> {
|
|
|
|
|
ErrorOr<void> format(FormatBuilder& builder, ByteStringImpl const& value)
|
2020-10-07 08:24:46 -03:00
|
|
|
{
|
2021-11-15 21:15:21 -03:00
|
|
|
return Formatter<StringView>::format(builder, { value.characters(), value.length() });
|
2020-10-07 08:24:46 -03:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-10-10 06:53:07 -03:00
|
|
|
}
|
|
|
|
|
|
2022-11-26 08:18:30 -03:00
|
|
|
#if USING_AK_GLOBALLY
|
2018-11-06 21:19:35 -02:00
|
|
|
using AK::Chomp;
|
2020-11-28 11:37:44 -03:00
|
|
|
using AK::NoChomp;
|
2022-11-26 08:18:30 -03:00
|
|
|
#endif
|