Commit Graph

2270 Commits

Author SHA1 Message Date
kleines Filmröllchen
1d144ed6fc AK: Remove clear() from FixedArray and fixate its allocation guarantees
FixedArray always *almost* had the following allocation guarantees:
There is (possibly) one allocation in the constructor and one (or more)
deallocation(s) in the destructor. No other operation allocates or
deallocates. With this removal of the public clear() method, which
nobody except the test used anyways, those guarantees are now completely
true and furthermore fixated with an explanatory comment.
2022-01-13 11:17:44 +01:00
kleines Filmröllchen
9bf2d0b718 AK: Disable NoAllocationGuard on Lagom
Because we don't have our LibC on Lagom, the allocation guard global
flag doesn't exist and NoAllocationGuard fails to build on Lagom.
Whoops. For now, just disable NoAllocationGuard's functionality here.
2022-01-13 11:17:44 +01:00
Andreas Kling
a702b6ec42 AK: Remove unnecessary null checks in RedBlackTree 2022-01-12 14:52:47 +01:00
Brian Gianforcaro
142e099001 AK: Implement StringView::for_each_split_view
StringView::for_each_split_view allows you to process the splits in a
StringView without needing to allocate a Vector<StringView> to store
each of the parts.

Since we migrated the implementation from the normal split_view path, we
can also re-implement split_view in terms of for_each_split_view.
2022-01-12 13:03:44 +01:00
Brian Gianforcaro
cd42f64bc7 AK: Rewrite StringView::split_view(char..), using StringView 2022-01-12 13:03:44 +01:00
Timothy Flynn
2b5840296a AK: Add AK::Time factory method to construct from individual time fields 2022-01-11 23:56:35 +01:00
Timothy Flynn
548643bcc9 AK: Redeclare a few AK::Time helpers as constexpr
This is to allow using these methods within an upcoming constexpr
factory method.
2022-01-11 23:56:35 +01:00
Andreas Kling
a4b4b358ff AK+Kernel: Remove one_ref_left() footgun
This mechanism was unsafe to use in any multithreaded context, since
the hook function was invoked on a raw pointer *after* decrementing
the local ref count.

Since we don't use it for anything anymore, let's just get rid of it.
2022-01-11 01:12:16 +01:00
Timothy Flynn
3dccaa39d8 AK: Define a traits helper for case-insensitive StringView hashing
Currently, we define a CaseInsensitiveStringTraits structure for String.
Using this structure for StringView involves allocating a String from
that view, and a second string to convert that intermediate string to
lowercase.

This defines CaseInsensitiveStringViewTraits (and the underlying helper
case_insensitive_string_hash) to avoid allocations.
2022-01-11 00:36:45 +01:00
kleines Filmröllchen
2f50d8f4d3 AK+LibC+LibPthread: Introduce NoAllocationGuard
NoAllocationGuard is an RAII stack guard that prevents allocations
while it exists. This is done through a thread-local global flag which
causes malloc to crash on a VERIFY if it is false. The guard allows for
recursion.

The intended use case for this class is in real-time audio code. In such
code, allocations are really bad, and this is an easy way of dynamically
enforcing the no-allocations rule while giving the user good feedback if
it is violated. Before real-time audio code is executed, e.g. in LibDSP,
a NoAllocationGuard is instantiated. This is not done with this commit,
as currently some code in LibDSP may still incorrectly allocate in real-
time situations.

Other use cases for the Kernel have also been added, so this commit
builds on the previous to add the support both in Userland and in the
Kernel.
2022-01-11 00:08:58 +01:00
Marcus Nilsson
4459cb33ed LibGUI+AK: Add DRAG_DEBUG opt and put drag operations behind dbgln_if
No need to have this enabled all the time.
2022-01-10 14:23:04 +01:00
Rafał Babiarz
0012c03eb5 AK: Added human_readable_time() method for "unsaved changes" dialogs 2022-01-09 20:25:48 -08:00
Stephan Unverwerth
f9c5c120f6 AK/SIMD: Suppress psabi warnings and add explanatory comment 2022-01-09 21:03:08 +01:00
Stephan Unverwerth
444a15bad3 AK: Add SIMDMath.h with vectorized version of math functions 2022-01-09 16:21:13 +03:30
Stephan Unverwerth
7adcdecc7b AK: Add SIMDExtras.h with SIMD related functions
Adds a header to AK with helper functions for writing vectorized code.

Co-authored-by: Hendiadyoin <leon2002.la@gmail.com>
2022-01-09 16:21:13 +03:30
Maciej
ad2551e6b8 AK: Add ByteBuffer::append(char) 2022-01-09 00:18:46 +01:00
creator1creeper1
f73afbb5ae AK: Reorder functions in FixedArray so that mutable comes before const 2022-01-08 22:54:05 +01:00
creator1creeper1
18a2685c6a AK: Reorder access in FixedArray so that m_size comes before m_elements 2022-01-08 22:54:05 +01:00
creator1creeper1
3c05261611 AK+Everywhere: Make FixedArray OOM-safe
FixedArray now doesn't expose any infallible constructors anymore.
Rather, it exposes fallible methods. Therefore, it can be used for
OOM-safe code.
This commit also converts the rest of the system to use the new API.
However, as an example, VMObject can't take advantage of this yet,
as we would have to endow VMObject with a fallible static
construction method, which would require a very fundamental change
to VMObject's whole inheritance hierarchy.
2022-01-08 22:54:05 +01:00
Andreas Kling
0e70759271 AK: Unbreak ref counting hooks in RefCounted
Same fix as 5871072ed3, but for userspace
this time. Regressed in c4a0f01b02.
2022-01-08 19:38:00 +01:00
Andreas Kling
c4a0f01b02 AK+Kernel: Use requires expression when invoking ref counting hooks
Replace some old-school template trickery with C++20 requires. :^)
2022-01-08 16:31:14 +01:00
mjz19910
10ec98dd38 Everywhere: Fix spelling mistakes 2022-01-07 15:44:42 +01:00
Schlufi
55a7738837 AK: Use a full-period xorshift PRNG for double_hash
The previous implementation had some pretty short cycles and two fixed
points (1711463637 and 2389024350). If two keys hashed to one of these
values insertions and lookups would loop forever.
This version is based on a standard xorshift PRNG with period 2**32-1.
The all-zero state is usually forbidden, so we insert it into the cycle
at an arbitrary location.
2022-01-07 12:34:44 +01:00
mjz19910
3102d8e160 Everywhere: Fix many spelling errors 2022-01-07 10:56:59 +01:00
Timothy Flynn
bb9db5937a AK: Add Time.h helper to compute the number of days since epoch 2022-01-05 20:05:12 +01:00
Timothy Flynn
0c75a14b4f AK: Move TimeSpecType concept inside the AK namespace
This is just to allow removing the 'clang-format off' directive. This
concept is only used within this header, so it doesn't need to be in the
global namespace.
2022-01-05 20:05:12 +01:00
Andreas Kling
558fb0a04a AK: Make Vector::remove_all_matching() return removal success
This matches the behavior of other remove_*_matching() functions.
2022-01-05 18:57:14 +01:00
Andreas Kling
5279a04c78 AK: Make Hash{Map,Table}::remove_all_matching() return removal success
These functions now return whether one or more entries were removed.
2022-01-05 18:57:14 +01:00
Andreas Kling
376e5ef912 AK: Add HashMap::remove_all_matching(predicate)
This removes all matching entries from a hash map in a single pass.
2022-01-05 18:57:14 +01:00
Andreas Kling
54cf42fac1 AK: Add HashTable::remove_all_matching(predicate)
This removes all matching entries from a table in a single pass.
2022-01-05 18:57:14 +01:00
Brian Gianforcaro
1a2aad287f AK: Disable Vector insert/empend/prepend + a append overload in Kernel
We have whittled away at the usages of these AK::Vector APIs in the
Kernel. This change disables them from being visible when building
the Kernel to make sure no new usages get added.
2022-01-05 14:04:18 +01:00
Brian Gianforcaro
48206be121 AK: Use MUST + try_empend so AK::Trie continues to compile in the kernel 2022-01-05 14:04:18 +01:00
Brian Gianforcaro
7d27798c8d AK: Use unchecked_append in AK::Stack, as we always validate the size
This removes one additional usage of Vector::append that stops us from
disabling it when compiling the Kernel.
2022-01-05 14:04:18 +01:00
Jelle Raaijmakers
8da0925d6d AK: Add mix 2022-01-04 17:48:28 +00:00
B0IIZZ
6124050187 AK: Fix UFixedBigInt comparison operators
Instead of using the specified type U like we want,
we were using the type T all along when comparing with
smaller integers. This is now fixed.
2022-01-04 06:01:22 +00:00
James Puleo
5b164e9934 AK: Include utility from the STD if we aren't replacing STD
If we didn't define our own `move` and `forward` and inject it into the
`std` namespace, then we would error just after trying to `using` those,
if no one has included it before. Now, we will include `utility` from
the STD if we aren't replacing the `std`.
2022-01-02 12:05:12 +01:00
Daniel Bertalan
1d2f78682b Kernel+AK: Eliminate a couple of temporary String allocations 2021-12-30 14:16:03 +01:00
Andreas Kling
ac7ce12123 Kernel: Remove the kmalloc_eternal heap :^)
This was a premature optimization from the early days of SerenityOS.
The eternal heap was a simple bump pointer allocator over a static
byte array. My original idea was to avoid heap fragmentation and improve
data locality, but both ideas were rooted in cargo culting, not data.

We would reserve 4 MiB at boot and only ended up using ~256 KiB, wasting
the rest.

This patch replaces all kmalloc_eternal() usage by regular kmalloc().
2021-12-28 21:02:38 +01:00
Andreas Kling
1c2aa7396d AK: Don't include AK::demangle() in KERNEL builds
This was not used anywhere in the kernel anyway.
2021-12-26 21:22:59 +01:00
Andreas Kling
8a51f64503 AK: Increase StringBuilder's inline buffer size from 128 to 256 bytes 2021-12-26 01:42:58 +01:00
Ali Mohammad Pur
986d63466e AK: Remove Variant<Ts...>::operator Variant<NewTs...>()
This is an interface to downcast(), which degrades errors into runtime
errors, and allows seemingly-correct-but-not-quite constructs like the
following to appear to compile, but fail at runtime:

    Variant<NonnullRefPtr<T>, U> foo = ...;
    Variant<RefPtr<T>, U> bar = foo;

The expectation here is that `foo` is converted to a RefPtr<T> if it
contains one, and remains a U otherwise, but in reality, the
NonnullRefPtr<T> variant is simply dropped on the floor, and the
resulting variant becomes invalid, failing the assertion in downcast().

This commit adds a Variant<Ts...>(Variant<NewTs...>) constructor that
ensures that no alternative can be left out at compiletime, for the
users that were using this interface for merely increasing the number of
alternatives (for instance, LibSQL's Value class).
2021-12-25 18:24:43 +03:30
Daniel Bertalan
2b1864c53a AK: Add missing Array.h include to CheckedFormatString.h
GCC 12 complains that iota_array is used before it's declared. GCC 11
works fine without it though.
2021-12-24 14:35:33 -08:00
Michel Hermier
4758dac218 AK: Make Disjoint*::is_empty() not call size
This is a raffinement of 49cbd4dcca.

Previously, the container was scanned to compute the size in the unhappy
path. Now, using `all_of` happy and unhappy path should be fast.
2021-12-24 05:55:34 -08:00
Michel Hermier
3a177b9209 AK: Add DisjointChunkc::ensure_capacity 2021-12-24 05:55:34 -08:00
Nick Johnson
08e4a1a4dc AK+Everywhere: Replace __builtin bit functions
In order to reduce our reliance on __builtin_{ffs, clz, ctz, popcount},
this commit removes all calls to these functions and replaces them with
the equivalent functions in AK/BuiltinWrappers.h.
2021-12-21 22:13:51 +01:00
Xavier Defrang
9e97823ff8 AK: Add convert_to_uint_from_octal 2021-12-21 13:13:04 -08:00
Ali Mohammad Pur
49cbd4dcca AK: Make DisjointChunks not query size() when there are no chunks 2021-12-21 22:10:07 +01:00
Ali Mohammad Pur
2df54a7b56 AK: Add Disjoint(Chunks Spans)::find(index)
For when the may or may not be out of bounds.
2021-12-21 22:10:07 +01:00
Ali Mohammad Pur
f0709c7a24 LibC+AK: Implement all sorts of wprintf variants 2021-12-21 21:24:36 +03:30
Andreas Kling
452a5531be AK+LibMain: Improve formatter for AK::Error in userspace
Print the full associated string metadata by default (if available.)
2021-12-20 21:13:42 +01:00