Commit graph

1328 commits

Author SHA1 Message Date
Linus Groh bbe787a0af Everywhere: Re-format with clang-format-11
Compared to version 10 this fixes a bunch of formatting issues, mostly
around structs/classes with attributes like [[gnu::packed]], and
incorrect insertion of spaces in parameter types ("T &"/"T &&").
I also removed a bunch of // clang-format off/on and FIXME comments that
are no longer relevant - on the other hand it tried to destroy a couple of
neatly formatted comments, so I had to add some as well.
2020-12-31 21:51:00 +01:00
Linus Groh a8ac8c6a8f AK: Add missing 'template' keywords in TypeList
This caused the AK build to be broken on OpenBSD (and possibly other
platforms / compiler versions).
Fixes #4692.
2020-12-31 16:16:07 +01:00
asynts a7c014125f AK: Add operator* and operator-> overloads in Optional. 2020-12-31 00:51:12 +01:00
Tom 34b3d92a13 AK: Fix some WeakPtr copy constructor variants not copying the link 2020-12-31 00:39:43 +01:00
Tom 54eeb8ee9a AK: Fix a race condition with WeakPtr<T>::strong_ref and destruction
Since RefPtr<T> decrements the ref counter to 0 and after that starts
destructing the object, there is a window where the ref count is 0
and the weak references have not been revoked.

Also change WeakLink to be able to obtain a strong reference
concurrently and block revoking instead, which should happen a lot
less often.

Fixes a problem observed in #4621
2020-12-31 00:39:43 +01:00
AnotherTest 6422a04cda AK+ProtocolServer: Properly close download stream fd's
This makes the issue of running out of openable pipes in the
ProtocolServer process much less likely (but still possible).
2020-12-30 20:37:41 +01:00
asynts 7e62ffbc6e AK+Format: Remove TypeErasedFormatParams& from format function. 2020-12-30 20:33:53 +01:00
AnotherTest 4e203f7e2d AK: Add {Input,Output}FileStream
Unlike the ones in LibCore, these only wrap an stdio FILE* (or an fd,
which they close when destroyed).
2020-12-30 13:31:55 +01:00
AnotherTest bf7cda414f AK: Add a ByteBuffer::copy(ReadonlyBytes) overload 2020-12-30 13:31:55 +01:00
Andrew Kaster 7d49ea9836 AK: Replace some SFINAE with requires clauses, clean up existing ones
Add requires clauses to constraints on InputStream and OutputStream
operator<< / operator>>. Make the constraint on String::number a
requires clause instead of SFINAE. Also, fix some unecessary IsSame in
Trie where specialized traits exist for the given use cases.
2020-12-30 13:28:49 +01:00
Andrew Kaster 0c51778510 AK: Move String::number entirely to header file
Use SFINAE to enforce the fact that it's supposed to only be called for
Arithmetic types, rather than counting on the linker to tell us that an
instantiation of String::number(my_arg) was not found. This also adds
String::number for floating point types as a side-effect.
2020-12-30 11:32:20 +01:00
Andrew Kaster b4eb734204 AK: Add tests for type traits and IndexSequence
Use TypeLists to add test for IsIntegral, IsFloatingPoint, IsVoid,
IsNullPointer, IsArithmetic, IsFundamental, and AddConst type traits.

More can "easily" be added once the TypeList and macro magic is squinted
at for long enough :).
2020-12-30 11:32:20 +01:00
Andrew Kaster 3bf77f01a7 AK: Add a TypeList class for expanded compile-time tools
Also add IndexSequence and associated helpers. The TypeList class can be
queried for what type is at a certain index, and there are two helper
functions: for_each_type, and for_each_type_zipped.

for_each_type will invoke a lambda with a TypeWrapper object for
each type in the type list. The original type can be obtained by
extracting the ::Type from the type of your generic lambda's one
argument.

for_each_type_zipped will walk two TypeLists in lockstep and pass a
TypeWrapper object for the current index in each list to a generic
lambda. The original type from the TypeList can again be extracted via
the ::Type of the generic lambda's two parameters.
2020-12-30 11:32:20 +01:00
Andrew Kaster fe4b44b489 AK: Add IsArithmetic and IsFundamental type traits
Also, make sure to using AK::IsNullPointer
2020-12-30 11:32:20 +01:00
Andrew Kaster 874df07ffd AK: Correct name in TestMain for TestTrie
Copy paste error :)
2020-12-30 11:32:20 +01:00
Andrew Kaster 06b6f838d6 AK: Use MacOS pthread_get_stacksize_np to get stack size for StackInfo
Seems Rust and OpenJDK both had issues with getting accurate stack size
for the main thread with MacOS Maverick and above. Apply a variant of
their workarounds. We could probably assume 8MB in all cases just to
be safe, as the only user of AK::StackInfo right now is lib JS's heap
for determining possible pointer candidates. But, this approach should
work if userspace apps start trying to add custom guard pages, as well.
2020-12-30 11:28:50 +01:00
asynts 50d24e4f98 AK: Make binary_search signature more generic. 2020-12-30 02:13:30 +01:00
Linus Groh 1ed72cc580 AK: Add HashMap(std::initializer_list<Entry>) constructor
This allows us to construct a HashMap from an initializer list like so:

    HashMap<K, V> hash_map = { { K, V }, { K, V } { K, V } };
2020-12-29 15:22:15 +01:00
asynts 620b73b3d5 AK+Format: Accept unsigned long in replacement fields.
I ran into this exact but at least twenty times in Serenity alone. The
C++ Standard dictates that 'unsigned long' and 'unsigned long long' are
distinct types even though on most platforms they are usually both 64
bit integers.

Also it wasn't possible to evaluate IsIntegral<T> for types that were
not integers since it used MakeUnsigned<T> internally.
2020-12-29 02:36:32 +01:00
AnotherTest cbe0a8b403 AK: Allow trailing '*'s in a glob pattern to match nothing
Fixes part of #4607.
2020-12-29 02:35:52 +01:00
Andreas Kling 13594b7146 LibGfx+AK: Make text elision work with multi-byte characters
This was causing WindowServer and Taskbar to crash sometimes when the
stars aligned and we tried cutting off a string ending with "..." right
on top of an emoji. :^)
2020-12-28 23:54:10 +01:00
Andrew Kaster 8ce7df73fb AK: Enable AK::SharedBuffer for all platforms
A future patch could do some MacOS specific things for
set_volatile/set_nonvolatile. For now, swap out the defined(__linux__)
branches for simple not __serenity__ branches.
2020-12-28 19:35:32 +01:00
Andrew Kaster 02fcf3974e AK/Userland: Use AK/Endian.h for portable byte swapping in ntpquery
Create macros for the byte swap operations one would expect to be in
endian.h or byteswap.h in AK/Endian.h. It's likely a similar/different
change will be needed for BSDs, but there's no github action for those
added to the project yet.
2020-12-28 19:35:32 +01:00
Andrew Kaster 1219c65249 AK: Add CLOCK_*_COARSE aliases for darwin and BSD variants
The coarse clocks in time.h are a linux extension that we've adopted.
MacOS and the BSDs don't have it, so we need an alias in a platform
header for Lagom builds.
2020-12-28 19:35:32 +01:00
Andreas Kling 20e2700952 Kernel: Allow Userspace<T> pointers with invalid content
It's not an error to create a Userspace<T> that points to kernel memory
as the point of Userspace<T> is not to validate the address, but rather
to choose safe overloads that do validation before any data transfer
takes place.

Fixes #4581.
2020-12-27 23:43:15 +01:00
Nathan Lanza d1891f67ac
AK: Use direct-list-initialization for Vector::empend() (#4564)
clang trunk with -std=c++20 doesn't seem to properly look for an
aggregate initializer here when the type being constructed is a simple
aggregate (e.g. `struct Thing { int a; int b; };`). This template fails
to compile in a usage added 12/16/2020 in `AK/Trie.h`.

Both forms of initialization are supposed to call the
aggregate-initializers but direct-list-initialization delegating to
aggregate initializers is a new addition in c++20 that might not be
implemented yet.
2020-12-27 23:06:37 +01:00
Stephan Unverwerth d3524f47a0 LibJS: Implement (mostly) spec compliant version of Number.toString() 2020-12-27 23:04:09 +01:00
Brian Gianforcaro 21a5524d01 AK: Add NO_DISCARD macro to allow clang-format friendly class annotations
clang-format seems to barf on these attributes, to make it easier to
use these attributes and have clang-format not mangle the following code
we can hide them behind a macro so clang-format doesn't have to handle it.
2020-12-27 11:09:30 +01:00
AnotherTest 1c9d28d212 AK: Fix busted Trie test
This wasn't testing anything ^^'
2020-12-26 12:32:27 +01:00
AnotherTest ad646420dd AK: Make AK::IsSame<T, U>::value a constexpr bool
It being an enum value was preventing it from being used without `!!` in
requires clauses (bool also makes more sense anyway).
2020-12-26 12:32:27 +01:00
AnotherTest cb3348191b AK: Add a prefix tree implementation
`AK::Trie` can be keyed by any given hashable type, and can store any
metadata (including nothing at all).
Also adds a test.
2020-12-26 11:54:54 +01:00
Lenny Maiorani b2316701a8 Everywhere: void arguments to C functions
Problem:
- C functions with no arguments require a single `void` in the argument list.

Solution:
- Put the `void` in the argument list of functions in C header files.
2020-12-26 10:10:27 +01:00
Sahan Fernando 6b01d1cf14 LibC: Enable compiler warnings for printf format strings 2020-12-26 10:05:50 +01:00
Andreas Kling ed5c26d698 AK: Remove custom %w format string specifier
This was a non-standard specifier alias for %04x. This patch replaces
all uses of it with new-style formatting functions instead.
2020-12-25 17:05:05 +01:00
Andreas Kling cb2c8f71f4 AK: Remove custom %b format string specifier
This was a non-standard specifier alias for %02x. This patch replaces
all uses of it with new-style formatting functions instead.
2020-12-25 17:04:28 +01:00
Lenny Maiorani e4ce485309 CMake: Decouple cmake utility functions from top-level CMakeLists.txt
Problem:
- These utility functions are only used in `AK`, but are being defined
  in the top-level. This clutters the top-level.

Solution:
- Move the utility functions to `Meta/CMake/utils.cmake` and include
  where needed.
- Also, move `all_the_debug_macros.cmake` into `Meta/CMake` directory
  to consolidate the location of `*.cmake` script files.
2020-12-24 11:02:04 +01:00
Lenny Maiorani 8e1af483be CMake: Remove file globbing in AK/Tests
Problem:
- File globbing is performed at the time of build system
  generation. Any files which are not there at that time are not
  included. So, when a new file is added it is not built unless the
  build system is recreated.

Solution:
- Remove globbing from AK/Tests directory in favor of explicitly
  listing the files.
2020-12-23 20:51:29 +01:00
Andreas Kling eaa63fdda5 Kernel: Don't assert on PT_PEEK with kernelspace address
We were casting the address to Userspace<T> without validating it first
which is no good and will trap an assertion soon after.

Let's catch this sooner with an ASSERT in the Userspace<T> constructor
and update the PT_PEEK and PT_POKE handlers to avoid it.

Fixes #4505.
2020-12-23 14:50:20 +01:00
Sahan Fernando b37139e111 AK: Make JsonParser::parse_number properly parse >32bit ints 2020-12-21 00:15:44 +01:00
Sahan Fernando 1a12f964d4 AK: Test StringUtils::convert_to_int for different types 2020-12-21 00:15:44 +01:00
Sahan Fernando f469f44cc6 AK: Check for overflow in StringUtils::convert_to_int 2020-12-21 00:15:44 +01:00
Sahan Fernando 37df4bbd90 AK: Generalize AK::String::to_int() for more types 2020-12-21 00:15:44 +01:00
Lenny Maiorani 765936ebae
Everywhere: Switch from (void) to [[maybe_unused]] (#4473)
Problem:
- `(void)` simply casts the expression to void. This is understood to
  indicate that it is ignored, but this is really a compiler trick to
  get the compiler to not generate a warning.

Solution:
- Use the `[[maybe_unused]]` attribute to indicate the value is unused.

Note:
- Functions taking a `(void)` argument list have also been changed to
  `()` because this is not needed and shows up in the same grep
  command.
2020-12-21 00:09:48 +01:00
Lenny Maiorani 4421d98e30 AllOf: Common iterator types
Problem:
- Interface is too permissive. It permits iterators of different types
  as long as they are comparable.

Solution:
- Require iterators be the same type.
2020-12-20 21:13:10 +01:00
asynts 72cbca892a AK: Remove bogus test case for CircularDuplexStream. 2020-12-19 23:29:40 +01:00
Andreas Kling 6b2a178a7f AK: Remove awkward ByteBuffer construction modes (wrap & adopt)
ByteBuffer previously had a flag that determined whether it owned the
bytes inside it or not (m_owned.) Owned ByteBuffers would free() on
destruction and non-owned ones would not.

This was a huge source of confusion and made it hard to reason about
lifetimes since there were no compile-time clues about whether a buffer
was owned or non-owned.

The adopt mode was used at some point to take over ownership of a
random malloc'ed buffer, but nothing was using it so this patch removes
that as well.
2020-12-19 18:29:13 +01:00
Andreas Kling 050eb5afa8 AK: Mark some Span functions with [[nodiscard]]
I was confused by the trim() API, thinking it would mutate the span it
was called on. Mark all const functions that return a new span with
[[nodiscard]] so we can catch such mistakes.
2020-12-19 18:29:13 +01:00
Andreas Kling f82b0a78ef LibTLS+LibCrypto: More ByteBuffer -> Span conversion 2020-12-19 18:29:13 +01:00
Andreas Kling 8e20208dd6 LibTLS+LibCrypto: Replace a whole bunch of ByteBuffers with Spans 2020-12-19 18:29:13 +01:00
Sahan Fernando fb9a71bd6a AK: Add hash implementations for i16 and i64 2020-12-19 10:20:15 +01:00