Truly independent web browser
/*
* Copyright (c) 2026, Andreas Kling <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/HashTable.h>
#include <AK/NumericLimits.h>
#include <AK/Utf16String.h>
#include <AK/Vector.h>
namespace JS {
inline size_t saturating_add_external_memory_size(size_t lhs, size_t rhs)
{
if (rhs > NumericLimits<size_t>::max() - lhs)
return NumericLimits<size_t>::max();
return lhs + rhs;
}
inline size_t utf16_string_external_memory_size(Utf16String const& string)
{
if (!string.has_long_storage())
return 0;
auto code_unit_size = string.utf16_view().has_latin1_storage() ? sizeof(u8) : sizeof(char16_t);
if (string.length_in_code_units() > NumericLimits<size_t>::max() / code_unit_size)
return NumericLimits<size_t>::max();
return string.length_in_code_units() * code_unit_size;
}
template<typename T, size_t inline_capacity, FastLastAccess fast_last_access>
size_t vector_external_memory_size(Vector<T, inline_capacity, fast_last_access> const& vector)
{
if (vector.capacity() <= inline_capacity)
return 0;
return vector.capacity() * sizeof(T);
}
template<typename Map>
size_t hash_map_external_memory_size(Map const& map)
{
return map.capacity() * (sizeof(typename Map::KeyType) + sizeof(typename Map::ValueType));
}
template<typename T, typename TraitsForT, bool is_ordered>
size_t hash_table_external_memory_size(HashTable<T, TraitsForT, is_ordered> const& table)
{
return table.capacity() * sizeof(T);
}
}