Commit graph

2459 commits

Author SHA1 Message Date
Andreas Kling
9d8da1697e AK: Automatically shrink HashTable when removing entries
If the utilization of a HashTable (size vs capacity) goes below 20%,
we'll now shrink the table down to capacity = (size * 2).

This fixes an issue where tables would grow infinitely when inserting
and removing keys repeatedly. Basically, we would accumulate deleted
buckets with nothing reclaiming them, and eventually deciding that we
needed to grow the table (because we grow if used+deleted > limit!)

I found this because HashTable iteration was taking a suspicious amount
of time in Core::EventLoop::get_next_timer_expiration(). Turns out the
timer table kept growing in capacity over time. That made iteration
slower and slower since HashTable iterators visit every bucket.
2022-03-07 00:08:22 +01:00
Andreas Kling
eb829924da AK: Remove return value from HashTable::remove() and HashMap::remove()
This was only used by remove_all_matching(), where it's no longer used.
2022-03-07 00:08:22 +01:00
Andreas Kling
623bdd8b6a AK: Simplify HashTable::remove_all_matching()
Just walk the table from start to finish, deleting buckets as we go.
This removes the need for remove() to return an iterator, which is
preventing me from implementing hash table auto-shrinking.
2022-03-07 00:08:22 +01:00
Linus Groh
1719862d12 LibWeb: Hide some debug logging behind CANVAS_RENDERING_CONTEXT_2D_DEBUG
This can be quite noisy and isn't generally useful information.
2022-03-04 23:03:29 +01:00
Peter Ross
34108547b6 AK: Print NaN and infinite numbers in PrintfImplementation 2022-03-02 11:40:37 +01:00
Lucas CHOLLET
39bfc48ea7 AK: Add Time::from_ticks()
This helper allows Time to be constructed from a tick count and a ticks
per second value.
2022-02-28 20:09:37 +01:00
Timur Sultanov
406b3fc3fe AK: Correctly process precision modifiers in printf 2022-02-28 14:08:24 +01:00
kleines Filmröllchen
98058f7efe AK: Add FixedPoint base 2 logarithm
The log base 2 is implemented using the binary logarithm algorithm
by Clay Turner (see the link in the comment)
2022-02-28 13:59:31 +01:00
kleines Filmröllchen
30002c2ccb AK: Add bit shift to FixedPoint 2022-02-28 13:59:31 +01:00
kleines Filmröllchen
5d6e3441fe AK: Fix FixedPoint to integral comparisons
Add tests to ensure that the fixed point numbers compare correctly to
integrals
2022-02-28 13:59:31 +01:00
Idan Horowitz
feb00b7105 Everywhere: Make JSON serialization fallible
This allows us to eliminate a major source of infallible allocation in
the Kernel, as well as lay down the groundwork for OOM fallibility in
userland.
2022-02-27 20:37:57 +01:00
Idan Horowitz
9da8c78133 AK: Add a try variant of StringBuilder::append_escaped_for_json
This will allow us to make a fallible version of the JSON serializers.
2022-02-27 20:37:57 +01:00
kleines Filmröllchen
7e18e6e37b AK: Skip over initial empty chunks in DisjointChunks
This will be caught by new test cases: when the initial chunk is empty,
a dereference before calling operator++ on the iterator will crash as
the initial chunk's size is never checked.
2022-02-27 00:11:14 +03:30
kleines Filmröllchen
4ec599aae5 AK: Make DisjointChunks support FixedArray
This extracts the shatter_chunk logic, because it needs to be different
for FixedArray.
2022-02-27 00:11:14 +03:30
kleines Filmröllchen
ee9eef1fa8 AK: Make DisjointChunk::append move the new chunk
Previously, although we were taking a moved chunk, we still copied it
into our chunk list. This makes DisjointChunk compatible with containers
that don't have a copy constructor but a move constructor.
2022-02-27 00:11:14 +03:30
kleines Filmröllchen
6e5bf7ac6f AK: Export DisjointSpans into the global namespace
I think we just forgot when we added it.
2022-02-27 00:11:14 +03:30
kleines Filmröllchen
75eb5e7984 AK: Add at() indexing methods to FixedArray 2022-02-27 00:11:14 +03:30
kleines Filmröllchen
09df8f812a AK: Allow creating a FixedArray from an initializer list 2022-02-27 00:11:14 +03:30
kleines Filmröllchen
65f34504e9 AK: Add FixedArray::is_empty()
This also exists on Vector, and although it's less needed here, it's
perfectly reasonable to have.
2022-02-27 00:11:14 +03:30
Arne Elster
b7110c2a34 AK: Add constructor to create Span from Array
It's a convenience constructor. But it also seems more consistent
to allow a Span being made from both raw and managed arrays.
2022-02-26 16:01:26 +01:00
Arne Elster
6a0cac7cdb AK: Array can tell its inner type
Array should be able to tell the type of the elements it contains.
2022-02-26 16:01:26 +01:00
Andreas Kling
f2663f477f AK: Ignore whitespace while decoding base64
This matches how other implementations behave.

1% progression on ACID3. :^)
2022-02-25 19:54:13 +01:00
Andreas Kling
dd7eb3d6d8 AK: Add String::split_view(Function<bool(char)>)
This allows you to split around a custom separator, and enables
expressive code like this:

    string.split_view(is_ascii_space);
2022-02-25 19:38:31 +01:00
Linus Groh
1752f7720e AK: Add forward declaration for Utf8CodePointIterator 2022-02-23 21:53:30 +00:00
Linus Groh
b253bca807 AK: Add optional format string parameter to String{,Builder}::join()
Allow specifying a custom format string that's being used for each item
instead of hardcoding "{}".
2022-02-23 21:53:30 +00:00
Linus Groh
f8ff8c8dd6 AK: Add Traits<Span<T>>::hash() 2022-02-23 21:53:30 +00:00
kleines Filmröllchen
604309327e AK: Add the Fundamental concept 2022-02-23 00:42:49 +00:00
kleines Filmröllchen
7418bdb85f AK: Prevent Atomic with complex types
This would throw some really weird linker errors, so let's prevent it
from happening instead!
2022-02-23 00:42:49 +00:00
Max Wipfli
d29d9462e9 AK: Suppress clang-tidy warning on TODO()
This adds a NOLINT directive to the definition of the TODO() macro.
clang-tidy wants the assert replaced with a static_assert, since the
macro simply resolves to assert(false). This is obviously nonsensical,
since we want the code to still compile even with TODO().

The same fix has already been implemented for VERIFY_NOT_REACHED().
2022-02-21 19:01:16 +02:00
Andreas Kling
4b900bc100 AK: Add fast path in String::trim() and String::trim_whitespace()
If the trimmed string would be the entire string, just return *this
instead of creating a new StringImpl.
2022-02-19 14:45:59 +01:00
Andreas Kling
2dd3b54827 AK: Make CaseInsensitiveStringTraits allocation-free
Instead of calling String::to_lowercase(), do case-insensitive hashing
and comparison.
2022-02-19 14:45:59 +01:00
Gunnar Beutner
7a65bd8efb AK: Don't use x86 assembly when building for non-x86 targets
This allows Lagom to be built successfully for Apple M1.

Fixes #12644.
2022-02-19 13:13:22 +02:00
Ben Abraham
ae346cff6b LibWeb: Add partially functioning Worker API
Add a partial implementation of HTML5 Worker API.
Messages can be sent from the inner context externally.
2022-02-17 22:45:21 +01:00
Sam Atkins
1a4dd47d5f AK: VERIFY inside release_value_but_fixme_should_propagate_errors()
While the code did already VERIFY that the ErrorOr holds a value, this
was done by Variant, so the error message was just that `has<T>()` is
false. This is less helpful than I would like, especially if backtraces
are not working and this is all you have to go on. Adding this extra
VERIFY means the assertion message (`!is_error()`) is easier to
understand.
2022-02-16 19:49:41 -05:00
Idan Horowitz
cec669a89a AK: Exclude StringUtils String APIs from the Kernel
These APIs are only used by userland, and String is OOM-infallible,
so let's just ifdef it out of the Kernel.
2022-02-16 22:21:37 +01:00
Idan Horowitz
4c6a1f4db2 AK: Exclude StringView String APIs from the Kernel
These APIs are only used by userland, and String is OOM-infallible,
so let's just ifdef it out of the Kernel.
2022-02-16 22:21:37 +01:00
Idan Horowitz
8f093e91e0 AK: Exclude StringBuilder String APIs from the Kernel
These APIs are only used by userland, and String is OOM-infallible,
so let's just ifdef it out of the Kernel.
2022-02-16 22:21:37 +01:00
Idan Horowitz
43e5c326e2 AK: Exclude JsonValue String APIs from the Kernel
These APIs are only used by userland, and String is OOM-infallible,
so let's just ifdef it out of the Kernel.
2022-02-16 22:21:37 +01:00
Idan Horowitz
e95470702e AK: Use string_view() instead of to_string() in Formatter<wchar_t>
This let's us avoid a heap allocation.
2022-02-16 22:21:37 +01:00
Idan Horowitz
b22cb40565 AK: Exclude GenericLexer String APIs from the Kernel
These APIs are only used by userland, and String is OOM-infallible,
so let's just ifdef it out of the Kernel.
2022-02-16 22:21:37 +01:00
Idan Horowitz
3219ce3d61 AK: Return KString instead of String from encode_hex in the Kernel
This let's us propagate allocation errors from this API.
2022-02-16 22:21:37 +01:00
Idan Horowitz
316fa0c3f3 AK+Kernel: Specialize Trie for NNOP<KString> and use it in UnveilNode
This let's us avoid the infallible String allocations.
2022-02-16 22:21:37 +01:00
Idan Horowitz
7f44e54ad6 AK+Kernel: Return KString from UUID::to_string() in the Kernel
This lets us safely handle allocation failure.
2022-02-16 22:21:37 +01:00
Idan Horowitz
9277d2dce2 AK+Kernel: Return KString from MACAddress::to_string() in the Kernel
This lets us safely handle allocation failure.
2022-02-16 22:21:37 +01:00
Idan Horowitz
6098ffa120 AK+Kernel: Return KString from IPv4Address::to_string() in the Kernel
This lets us safely handle allocation failure.
2022-02-16 22:21:37 +01:00
serenitydev
23c72c6728 AK: Fix userland parsing of rounded floating point numbers
Parse JSON floating point literals properly,
No longer throwing a SyntaxError when the decimal portion
of the number exceeds the capacity of u32.

Added tests to AK/TestJSON and LibJS/builtins/JSON/JSON.parse
2022-02-16 07:22:51 -05:00
Ali Mohammad Pur
a1cb2c371a AK+Kernel: OOM-harden most parts of Trie
The only part of Unveil that can't handle OOM gracefully is the
String::formatted() use in the node metadata.
2022-02-15 18:03:02 +02:00
Ali Mohammad Pur
80e6198563 AK: Conditionally disable a few variant ctors/assignments
We shouldn't let copy/move ctors or assignments be instantiated if the
assignee type does not have a copy/move constructor (even if they're not
used anywhere).
2022-02-15 18:03:02 +02:00
Ali Mohammad Pur
7a58c510e5 AK: Add a 'SpecializationOf' concept
The counterpart to the IsSpecializationOf<...> template.
2022-02-15 18:03:02 +02:00
Idan Horowitz
b32cf33a23 Kernel: Remove make_weak_ptr()
New users of WeakPtr in the kernel should use try_make_weak_ptr instead
2022-02-13 23:02:57 +01:00
Idan Horowitz
98c20b65cc AK+Kernel: Add an OOM-fallible try variant make_weak_ptr()
This will allow us to propagate allocation errors that may be raised by
the construction of the WeakLink.
2022-02-13 23:02:57 +01:00
Idan Horowitz
d6ea6c39a7 AK+Kernel: Rename try_make_weak_ptr to make_weak_ptr_if_nonnull
This matches the likes of the adopt_{own, ref}_if_nonnull family and
also frees up the name to allow us to eventually add OOM-fallible
versions of these functions.
2022-02-13 23:02:57 +01:00
Andreas Kling
c74b6c06a5 AK: Use ByteBuffer::append(u8) in StringBuilder single-char append 2022-02-13 14:44:36 +01:00
Andreas Kling
22f6f0fc9e AK: Don't call memcpy() in ByteBuffer::append(u8)
We can emit way nicer code for the just-append-a-single-byte case.
2022-02-13 14:44:36 +01:00
Idan Horowitz
871a53db76 AK: Make Bitmap construction OOM-fallible 2022-02-11 17:49:46 +02:00
davidot
fdbfe85a87 AK: Clear minimum when removing last node of RedBlackTree 2022-02-10 14:09:39 +00:00
davidot
2bddf157b1 AK: Fix RedBlackTree::find_smallest_not_below_iterator
Before this was incorrectly assuming that if the current node `n` was at
least the key and the left child of `n` was below the key that `n` was
always correct.
However, the right child(ren) of the left child of `n` could still be
at least the key.

Also added some tests which produced the wrong results before this.
2022-02-10 14:09:39 +00:00
Lenny Maiorani
e5d178528d AK: Change static base36 character map to function-local constexpr
Static variables consume memory and can be subject to less
optimization. This variable is only used in 1 place and can be moved
into the function and make it non-static.
2022-02-10 10:22:54 +00:00
Ali Mohammad Pur
e7dea10381 AK: Add RBTree::find_smallest_above_iterator(Key) 2022-02-09 20:57:41 +00:00
Ali Mohammad Pur
cb7becb067 LibTLS+RequestServer: Add an option to dump TLS keys to a log file
This file allows us to decrypt TLS messages in wireshark, which can help
immensely in debugging network stuff :^)
2022-02-09 21:23:25 +01:00
Lenny Maiorani
138d54e595 AK+Kernel: Alphabetize debug macros
This is not ASCII-betical because `_` comes after all the uppercase
characters. Treating `_` as a ` ` (space character), these lists are
now alphabetical.
2022-02-09 17:59:19 +00:00
Hendiadyoin1
fbb798f98c AK: Move integral log2 and exp to IntegerMath.h 2022-02-06 17:52:33 +00:00
Hendiadyoin1
9ba9691d19 AK: Use integral power for FixedPoint formatting
This removes an ifdef for the Kernel
2022-02-06 17:52:33 +00:00
Hendiadyoin1
581c23dc55 AK: Introduce IntegralMath.h starting with pow<I> 2022-02-06 17:52:33 +00:00
Andreas Kling
5dd4b3eaaa LibWeb: Put ResolvedCSSStyleDeclaration debug spam behind a macro
Blowing up the debug console with a fajillion FIXME's whenever you
navigate in the web inspector is no fun.
2022-02-06 16:22:58 +01:00
Ali Mohammad Pur
9cf1c382df AK: Replace 'consteval' with 'constexpr' in some Variant helpers
CLion and/or clangd didn't like the consteval and highlighted visit() as
an error, just replace it with constexpr as it makes no difference here.
2022-02-06 13:10:10 +01:00
Liav A
54845c4bf2 AK: Remove commented-out code from Bitmap container
Instead, add a note to explain that there's a const variant of data()
method in the parent BitmapView class.
2022-02-05 18:14:12 +00:00
Idan Horowitz
de7b5279cb AK: Make Vector::data() ALWAYS_INLINE
This was showing up in profiles of Browser, and it really shouldn't be.
2022-02-05 16:48:14 +01:00
Idan Horowitz
18b98f8c28 AK: Convert the try_make<T> factory function to use ErrorOr
This allows more ergonomic memory allocation failure related error
checking using the TRY macro.
2022-02-03 23:33:20 +01:00
Idan Horowitz
ba0a2a3e2f AK: Hide the infallible make<T> factory function from the Kernel
This function has no users, nor should it ever be used in the kernel,
as all allocation failures in the Kernel should be explicitly checked.
2022-02-03 23:33:20 +01:00
Idan Horowitz
7933a9b6c8 AK: Stop using the make<T> factory function in Trie
This function is infallible, and so we would like to exclude it from
the Kernel, which includes this header.
2022-02-03 23:33:20 +01:00
Idan Horowitz
a65bbbdb71 Kernel: Convert try_make_ref_counted to use ErrorOr
This allows more ergonomic memory allocation failure related error
checking using the TRY macro.
2022-02-03 23:33:20 +01:00
Idan Horowitz
be4c144524 AK: Support formatting Vectors with any inline_capacity
The default Vector type has its inline capacity set to 0, which means
any Vector with non-zero inline capacity was unformattable.
2022-02-03 23:33:20 +01:00
Andreas Kling
e86ab57078 AK+Kernel+LibSanitizer: Store "ubsan-is-deadly" flag as Atomic<bool> 2022-02-03 16:11:26 +01:00
Idan Horowitz
44e8c05c67 AK: Add a Utf16View::code_unit_offset_of(Utf16CodePointIterator) helper
This helper can be used to used to retrieve the code unit offset of an
active Utf16CodePointIterator efficiently.
2022-01-31 21:05:04 +02:00
Timothy Flynn
6efbafa6e0 Everywhere: Update copyrights with my new serenityos.org e-mail :^) 2022-01-31 18:23:22 +00:00
Timothy Flynn
94a346c9b9 AK: Add is_ascii_[binary,octal]_digit to CharacterTypes 2022-01-31 17:50:54 +00:00
Andreas Kling
51e5cc4e13 AK: Remove thread safety gunk from userspace WeakPtr
The implicit thread safety stuff is only needed by the kernel version
of WeakPtr, as userspace already makes no guarantees about this.
2022-01-31 16:19:23 +01:00
Daniel Bertalan
8473f6caee AK+Tests: Make null strings compare less than non-null strings
This behavior regressed in ca58c71faa.

Fixes #12213
2022-01-30 17:23:02 +00:00
Daniel Bertalan
1a4aad9b7e AK: Implement String's comparison operators in terms of StringView's 2022-01-29 23:08:27 +01:00
Daniel Bertalan
5b3ba2d9ad AK: Implement FlyString's comparison operators in terms of StringView's 2022-01-29 23:08:27 +01:00
Daniel Bertalan
ca58c71faa AK: Implement all comparison operators for StringView 2022-01-29 23:08:27 +01:00
Idan Horowitz
9b0d90a71d AK: Support using custom comparison operations for hash compatible keys 2022-01-29 23:01:23 +02:00
Lenny Maiorani
b0a54518d8 Everywhere: Remove redundant inline keyword
`constexpr` implies `inline` so when both are used it is redundant.
2022-01-29 21:45:17 +02:00
Pankaj Raghav
24c66b8442 AK: Fix log2 calculation for Integer
For a `N` bit integer X, the log2 calculation should be as follows:
(N - 1) - ctz(X)
2022-01-29 17:41:06 +02:00
Pankaj Raghav
61027e5303 AK: Add is_power_of_2 helper 2022-01-29 17:41:06 +02:00
Liav A
308e54bc19 AK+Kernel: Implement UUID mixed endianness support
This is being used by GUID partitions so the first three dash-delimited
fields of the GUID are stored in little endian order but the last two
fields are stored in big endian order, hence it's a representation which
is mixed.
2022-01-29 13:35:54 +02:00
kleines Filmröllchen
cfb8eeebe8 AK: Allow constructing a JsonArray from any array-like type 2022-01-28 23:40:25 +01:00
kleines Filmröllchen
2f631f7dc0 AK: Allow constructing a JsonValue from a StringView explicitly
The next commit will destroy overload detection otherwise, so let's add
this constructor. Currently, the same work is already done implicitly
through the implicit `String(StringView)` constructor.
2022-01-28 23:40:25 +01:00
kleines Filmröllchen
07977ad94c AK: Introduce the ArrayLike concept
The ArrayLike type concept focuses on array-like data accesses. This
means the ability to randomly index, obtain size information, as well as
being able to expose the storage pointer. The last two already have the
standard APIs `size` and `data`, respectively.

The ArrayLike concept should always be fulfilled by Vector, FixedArray
and Array as the three main array-like types. C-style arrays themselves
of course can't fulfil ArrayLike (which could be considered ironic), but
as we don't use them much anyways this isn't a problem.
2022-01-28 23:40:25 +01:00
Ali Mohammad Pur
bf82d9b5d7 AK: Simplify Variant's explicit overload detection mechanism a bit
This also allows us to remove the max-64-visitors restriction, and so
removes that assertion.
2022-01-28 00:50:04 +03:30
Timothy Flynn
01395169dc AK: Export the SameAs concept 2022-01-27 16:27:00 +00:00
Idan Horowitz
85437abfad Kernel: Support try-inserting RedBlackTree entry values by reference 2022-01-26 22:05:34 +00:00
Idan Horowitz
6e38076b48 AK: Use x86 specific fast path in fast_u32{fill, copy} on x86_64 2022-01-26 01:16:43 +02:00
Idan Horowitz
67ce9e28a5 AK: Standardize the behaviour of GenericLexer::consume_until overloads
Before this commit all consume_until overloads aside from the Predicate
one would consume (and ignore) the stop char/string, while the
Predicate overload would not, in order to keep behaviour consistent,
the other overloads no longer consume the stop char/string as well.
2022-01-25 13:41:09 +03:30
Idan Horowitz
d49d2c7ec4 AK: Add a consume_until(StringView) overload to GenericLexer
This allows us to skip a strlen call.
2022-01-25 13:41:09 +03:30
James Puleo
10b25d2a57 AK: Implement HashTable::try_ensure_capacity, as used in HashMap
This was used in `HashMap::try_ensure_capacity`, but was missing from
`HashTable`s implementation. No one had used
`HashMap::try_ensure_capacity` before so it went unnoticed!
2022-01-25 09:17:22 +01:00
Sam Atkins
c388a879d7 AK+Userland: Make AK::decode_base64 return ErrorOr 2022-01-24 22:36:09 +01:00
Sam Atkins
f590cd1850 AK+Userland: Make AK::decode_hex() return ErrorOr
This lets us propagate the reason why it failed up to the caller. :^)
2022-01-24 22:36:09 +01:00
Sam Atkins
45cf40653a Everywhere: Convert ByteBuffer factory methods from Optional -> ErrorOr
Apologies for the enormous commit, but I don't see a way to split this
up nicely. In the vast majority of cases it's a simple change. A few
extra places can use TRY instead of manual error checking though. :^)
2022-01-24 22:36:09 +01:00