Commit graph

1760 commits

Author SHA1 Message Date
Brendan Coles fa28cc85e6 AK: Tests: TestURL: Add port_int_overflow_wrap test 2021-03-17 12:39:03 +01:00
Idan Horowitz ea5f83616e LibCompress+AK: Dont short-circuit error handling propagation
In the case that both the stream and the wrapped substream had errors
to be handled only one of the two would be resolved due to boolean
short circuiting. this commit ensures both are handled irregardless
of one another.
2021-03-16 14:56:50 +01:00
Idan Horowitz a955fd4156 LibCompress+AK: Propagate error handling to wrapped streams
This ensures that when a DeflateCompressor stream is cleared of any
errors its underlying wrapped streams (InputBitStream/InputMemoryStream)
will be cleared as well and wont fail a VERIFY on destruction.
2021-03-15 21:35:48 +01:00
Andreas Kling 64cb04996f AK: Make ByteBuffer::slice(0, size()) a freebie
If you want the whole buffer, we can just give you the buffer itself.
2021-03-15 18:01:11 +01:00
Idan Horowitz 1b7b503bae AK: Add fast paths for aligned bit writes in BitOutputStream
If the bit write is aligned (or has been aligned during the write) we can
write in multiples of 32/16/8 bits for increased performance.
2021-03-13 23:50:07 +01:00
Idan Horowitz 3c7aa56ae8 AK: Store BinaryHeap key-value pairs together for efficient swaps
The 2 seperate key and value arrays are replaced with a single struct pair
array that allows for a 2x reduction in loads/stores during element swaps
in the common case of same-sized keys and values.
2021-03-13 23:50:07 +01:00
Idan Horowitz 168cf6fac9 AK: Implement minimum BinaryHeap
This enables efficient implementations of priority queues,
and will also be used in LibCompress for efficient huffman
tree generation.
2021-03-13 20:07:25 +01:00
Idan Horowitz 0ddc0e45ae AK: Add OutputBitStream class
This will be used in the deflate compressor.
2021-03-13 20:07:25 +01:00
Ben Wiederhake 81079ae616 AK: Fix some overflows/underflows that weren't properly handled
Based on #5699. Closes #5699.
2021-03-13 10:17:28 +01:00
Tom 8a8bdb2cd7 AK: Add decrement operator to Checked 2021-03-13 10:17:28 +01:00
Andreas Kling d3f112f9a2 AK+LibCore: Remove empty files 2021-03-12 17:38:17 +01:00
Andreas Kling ef1e5db1d0 Everywhere: Remove klog(), dbg() and purge all LogStream usage :^)
Good-bye LogStream. Long live AK::Format!
2021-03-12 17:29:37 +01:00
Andreas Kling 1b2ea12062 AK: Add basic const iteration to IntrusiveList 2021-03-11 14:21:49 +01:00
Andreas Kling 4d30166d61 AK: Add Formatter for RefPtr 2021-03-09 22:10:41 +01:00
Mițca Dumitru 8f7aa1e03a AK: Include Assertions.h in StdLibExtras.h 2021-03-09 07:28:06 +01:00
Brian Gianforcaro c825159f01 AK: Add AK_ENUM_BITWISE_OPERATORS(..) to enable type-safe enum bitwise operations
This change introduces `AK_ENUM_BITWISE_OPERATORS(..)` which when
enabled for an enum, will automatically declare all the necessary
bitwise operators for that enum.

This allows bit masks enums to be used as first class, type safe, citizens.
2021-03-08 18:47:40 +01:00
Tom 183b2e71ba AK: Take advantage of constexpr in Time and add time conversion methods
By making the Time constructor constexpr we can optimize creating a
Time instance from hardcoded values.

Also add more functions to convert between Time and various time units.
2021-03-08 15:29:11 +01:00
Tom 42bc229500 AK: Initialize the AK::Time members
Since we tell the compiler to provide a default constructor we
need to initialize the member variables.
2021-03-08 15:29:11 +01:00
Andreas Kling 483b45db3f AK: JsonObject::value_or() fallback value should be a const reference 2021-03-08 11:50:36 +01:00
Ben Wiederhake b6472204e5 AK+Tests: Also test Checked<> with unsigned
As expected, Checked<unsigned> works as intended. However, we didn't have a
test for that, so I added one.

See also 90c070cb1d.
2021-03-07 17:31:25 +01:00
speles 50de653cc9 AK: Add optional fragment parameter to create_with_file_protocol()
Now that we use fragment for specifying starting selection in
FileManager we would benefit from providing it as argument instead of
setting it each time separately.
2021-03-07 11:00:36 +01:00
Brian Gianforcaro fc5b252010 AK: Implement IsEnum<T> and UnderlyingType<T> type traits
I needed these meta-programming type traits while working on something else.
Add basic support for these two type traits as well as some tests.
2021-03-05 11:30:13 +01:00
Liav A 4a5cf8c789 AK: Add a full memory barrier function based on atomic operations
We use atomic_signal_fence and atomic_thread_fence together to prevent
reordering of memory accesses by the CPU and the compiler.

The usage of these functions was suggested by @tomuta so we can be sure
that important memory accesses happen in the expected order :)
2021-03-05 11:29:34 +01:00
Andreas Kling 1208fc7d37 AK: Simplify Bitmap and implement in terms of BitmapView
Add Bitmap::view() and forward most of the calls to BitmapView since
the code was identical.

Bitmap is now primarily concerned with its dynamically allocated
backing store and BitmapView deals with the rest.
2021-03-04 11:25:45 +01:00
Andreas Kling 40552bb5fb AK: Add BitmapView class (like StringView but for bitmaps)
AK::Bitmap is an awkwardly modal class which can either own or wrap
the underlying data. To get ourselves out of this unpleasant situation,
this patch adds BitmapView to replace the wrapped mode.

A BitmapView is simply a { data pointer, bit count } tuple internally
and provides all the convenient functionality of a bitmap class.
2021-03-04 11:25:45 +01:00
AnotherTest e59a631511 HackStudio/LanguageServers: Move some components out of Cpp
This makes them available for use by other language servers.
Also as a bonus, update the Shell language server to discover some
symbols and add go-to-definition functionality :^)
2021-03-04 11:21:43 +01:00
William McPherson 2479ead718 Everywhere: Remove unnecessary clang-format offs
Mostly due to the fact that clang-format allows aligned comments via
AlignTrailingComments.

We could also use raw string literals in inline asm, which clang-format
deals with properly (and would be nicer in a lot of places).
2021-03-04 11:01:48 +01:00
Andreas Kling ddaeb294dc AK: Add Bitmap::is_null()
This class has a null state but there was no API to check for it.
2021-03-04 10:11:37 +01:00
Linus Groh a5d6962148 AK+Kernel: Remove NO_DISCARD macro hack
This was added as clang-format would mess up the formatting when using
[[nodiscard]] on a class, which is no longer the case.
2021-03-03 23:08:42 +01:00
Ben Wiederhake cf32f29af6 AK+LibM: Rename EXPECT_CLOSE to EXPECT_APPROXIMATE 2021-03-03 20:19:24 +01:00
Ben Wiederhake 5df014b8ff AK+LibM: Make EXPECT_CLOSE more useful during debugging 2021-03-03 20:19:24 +01:00
ihsinme 69f82ede7a AK: Fix OOO mistake in StackInfo.cpp
"!=" has higher priority than "=".
2021-03-03 11:09:44 +01:00
Ben Wiederhake 340813e087 AK: Make Time more usable 2021-03-02 08:36:08 +01:00
Ben Wiederhake e510c41fd2 Kernel: Prevent using copy_from_user() for timespec/timeval
These structs can be inconsistent, for example if the amount of microseconds is
negative or larger than 1'000'000. Therefore, they should not be copied as-is.
Use copy_time_from_user instead.
2021-03-02 08:36:08 +01:00
Ben Wiederhake bd6be910e5 AK: Implement C++ 'Time' type for easier time-calculations
This adds a bunch of code in the hope that other, wrong code can be deleted.

Related to #5315.
2021-03-02 08:36:08 +01:00
Ben Wiederhake b7c5d977c7 AK: Remove unused template 2021-03-02 08:36:08 +01:00
Ben Wiederhake 90c070cb1d AK+Tests: Test Checked for main functionality 2021-03-02 08:36:08 +01:00
Andrew Kaster 669b6c43aa AK/Lagom: Modify TestSuite to return how many tests failed from main
This allows us to remove the FAIL_REGEX logic from the CTest invocation
of AK and LibRegex tests, as they will return a non-zero exit code on
failure :^).

Also means that running a failing TestSuite-enabled test with the
run-test-and-shutdown script will actually print that the test failed.
2021-03-01 11:12:36 +01:00
Andrew Kaster e787738c24 Meta: Build AK and LibRegex tests in Lagom and for Serenity
These tests were never built for the serenity target. Move their Lagom
build steps to the Lagom CMakeLists.txt, and add serenity build steps
for them. Also, fix the build errors when building them with the
serenity cross-compiler :^)
2021-02-28 18:19:37 +01:00
Sviatoslav Peleshko 183ebaee91 LibWeb: Add actual document loading for the CSS (at)import rule 2021-02-28 10:27:32 +01:00
Andreas Kling daf18e7777 AK: Use Array iterator instead of indexing outside Array bounds 2021-02-27 11:48:25 +01:00
Andreas Kling 69d8ad52c4 AK: Always do bounds checking in Array::operator[] 2021-02-27 09:23:32 +01:00
Linus Groh e265054c12 Everywhere: Remove a bunch of redundant 'AK::' namespace prefixes
This is basically just for consistency, it's quite strange to see
multiple AK container types next to each other, some with and some
without the namespace prefix - we're 'using AK::Foo;' a lot and should
leverage that. :^)
2021-02-26 16:59:56 +01:00
Andreas Kling 42133a196a AK: Don't compare past '\0' in StringView::operator==(const char*)
We kept scanning the needle string even after hitting a null terminator
and that's clearly not right.

Found by oss-fuzz:
https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=31338
https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=31351
2021-02-24 22:13:04 +01:00
Brian Gianforcaro 31e1b08e15 AK: Add support for AK::StringView literals with operator""sv
A new operator, operator""sv was added as of C++17 to support
string_view literals. This allows string_views to be constructed
from string literals and with no runtime cost to find the string
length.

See: https://en.cppreference.com/w/cpp/string/basic_string_view/operator%22%22sv

This change implements that functionality in AK::StringView.
We do have to suppress some warnings about implementing reserved
operators as we are essentially implementing STL functions in AK
as we have no STL :).
2021-02-24 14:38:31 +01:00
Linus Groh 6ad3454bfb AK: Rename {DBGLN_NO => ENABLE}_COMPILETIME_FORMAT_CHECK
This is no longer limited to dbgln(). Also invert it to match all the
other ENABLE_FOO options.
2021-02-24 13:07:57 +01:00
AnotherTest 857cdee0d0 AK: Make dbgln_if() avoid evaluating the arguments when disabled
Naturally, this makes the `enabled` flag on dbgln() obsolete.
2021-02-24 13:07:28 +01:00
Andreas Kling 679cc154e6 Everywhere: Remove unused RELEASE_ASSERT macro 2021-02-23 21:11:53 +01:00
Andreas Kling 5d180d1f99 Everywhere: Rename ASSERT => VERIFY
(...and ASSERT_NOT_REACHED => VERIFY_NOT_REACHED)

Since all of these checks are done in release builds as well,
let's rename them to VERIFY to prevent confusion, as everyone is
used to assertions being compiled out in release.

We can introduce a new ASSERT macro that is specifically for debug
checks, but I'm doing this wholesale conversion first since we've
accumulated thousands of these already, and it's not immediately
obvious which ones are suitable for ASSERT.
2021-02-23 20:56:54 +01:00
Andreas Kling 145923bdc9 AK: Slap Optional with the ALWAYS_INLINE stick
I saw some Optional constructors when profiling the dynamic loader
and that seemed silly since we can inline them at no/little cost.
2021-02-23 17:42:05 +01:00
Andreas Kling 31ac93d051 AK: Optimize StringView::operator==(const char*) a little bit
Don't compute the strlen() of the string we're comparing against first.
This can save a lot of time if we're comparing against something that
already fails to match in the first few characters.
2021-02-23 17:41:18 +01:00
AnotherTest 7c2754c3a6 AK+Kernel+Userland: Enable some more compiletime format string checks
This enables format string checks for three more functions:
- String::formatted()
- Builder::appendff()
- KBufferBuilder::appendff()
2021-02-23 13:59:33 +01:00
AnotherTest 347d741afb AK+Userland: Extend the compiletime format string check to other functions
Thanks to @trflynn89 for the neat implicit consteval ctor trick!
This allows us to basically slap `CheckedFormatString` on any
formatting function, and have its format argument checked at compiletime.

Note that there is a validator bug where it doesn't parse inner replaced
fields like `{:~>{}}` correctly (what should be 'left align with next
argument as size' is parsed as `{:~>{` following a literal closing
brace), so the compiletime checks are disabled on these temporarily by
forcing them to be StringViews.

This commit also removes the now unused `AK::StringLiteral` type (which
was introduced for use with NTTP strings).
2021-02-23 13:59:33 +01:00
AnotherTest 644d981b2b AK: Untangle TestSuite assertions a bit 2021-02-23 13:59:33 +01:00
Linus Groh 4fafe14691 AK: Add String{,Utils}::to_snakecase()
This is an improved version of WrapperGenerator's snake_name(), which
seems like the kind of thing that could be useful elsewhere but would
end up getting duplicated - so let's add this to AK::String instead,
like to_{lowercase,uppercase}().
2021-02-21 19:47:47 +01:00
Ivan Hansgaard Hansen 46ca7d3cb5 AK: Alter ByteBuffer to utilise memcmp.
__builtin_memcmp is already heavily utilised in AK.
2021-02-21 16:34:51 +01:00
Andreas Kling a65f178ce8 AK: Do bounds checking (assertions) in Span::operator[] 2021-02-21 14:21:20 +01:00
Brian Gianforcaro df73a86875 AK: Add an implementation of Array<T, Size>::fill(...) 2021-02-21 12:54:39 +01:00
Brian Gianforcaro 21a959e29b AK: Add Span<T> constructor for arrays
The array constructor allows arrays to be easily treated
as generic span of data.
2021-02-21 12:54:39 +01:00
Brian Gianforcaro 5e84320ecb AK: Add safe memset() wrapper to ByteBuffer
In the interest memory safety and of removing as many
of foot guns as possible (like raw memset's which are
notorious for typo's), a zeroing method seems like a
useful utility to have on a buffer class.
2021-02-21 11:52:47 +01:00
AnotherTest 074e2ffdfd LibLine: Avoid refreshing the entire line when inserting at the end
This patchset allows the editor to avoid redrawing the entire line when
the changes cause no unrecoverable style updates, and are at the end of
the line (this applies to most normal typing situations).
Cases that this does not resolve:
- When the cursor is not at the end of the buffer
- When a display refresh changes the styles on the already-drawn parts
  of the line
- When the prompt has not yet been drawn, or has somehow changed

Fixes #5296.
2021-02-20 22:28:23 +01:00
Andreas Kling 81c6d8e98e AK: Make Nonnull*PtrVector use size_t for indexes
This was weird. It turns out these class were using int indexes and
sizes despite being derived from Vector which uses size_t.

Make the universe right again by using size_t here as well.
2021-02-20 18:34:32 +01:00
Andreas Kling 4d9ce42532 AK: Remove unused kprintf macro
This hasn't been used for quite some time.
2021-02-20 17:21:03 +01:00
Andreas Kling 8d98051551 AK+LibC: Remove dbgprintf() :^)
Everything has been moved to dbgln() or other AK::Format-based APIs.
We can finally get rid of this old thing.
2021-02-20 17:17:30 +01:00
Itamar 7ecb21afa7 AK: Add LexicalPath::relative_path 2021-02-20 15:53:37 +01:00
Andreas Kling 1f401def18 AK: Publish all_of() 2021-02-17 15:22:21 +01:00
Brian Gianforcaro 3356f438ca AK: Mark Optional getters as [[nodiscard]]
There is no reason to call a getter without observing the result, doing
so indicates an error in the code. Mark these methods as [[nodiscard]]
to find these cases.
2021-02-15 09:34:52 +01:00
AnotherTest 3fe7ac0924 LibCrypto: Make a better ASN.1 parser
And use it to parse RSA keys.
As a bonus, this one shouldn't be reading out of bounds or messing with
the stack (as much) anymore.
2021-02-14 13:30:10 +01:00
AnotherTest 4d40864b9d AK: Make the Bitmap::size_in_bytes() member function public
It's annoying to calculate it when it's already a member function.
2021-02-14 13:30:10 +01:00
Paul Scharnofske 35a1e12459 AK+Format: Don't cast to size_t when you want u64.
In Serenity, size_t is defined as u32, thus static_cast<size_t>(value)
truncates the value.
2021-02-13 19:47:06 +01:00
Ben Wiederhake 5963f2084e Utilities: Make syscall(1) explain what it's doing 2021-02-13 00:40:31 +01:00
Linus Groh feb66564d2 AK: Set DBGLN_NO_COMPILETIME_FORMAT_CHECK for any clang, not just < 12
This currently breaks the OSS-Fuzz build, and attempts to make it build
with clang >= 12 were unsuccessful, so let's just disable dbgln() checks
for any clang version.
2021-02-12 19:19:43 +01:00
Linus Groh ca69cab532 Revert "AK: Fix build with Clang>=12"
This reverts commit 338bb73289.

This didn't work, the OSS-Fuzz build (using clang 12) is still failing.
We'll just disable dbgln() checks when compiling with any clang for now.
2021-02-12 19:19:43 +01:00
Andreas Kling acf341862a AK: Remove operators && and || from DistinctNumeric
These don't do short-circuit evaluation, and so I ran into some
some very subtle side-effects when converting code to DistinctNumeric.

In code like this:

    MyDistinctNumeric n;
    if (n && check_thing(n))
        return;

There would be no short-circuit evaluation if the return type of
check_thing() was implicitly convertible to MyDistinctNumeric.

Ran into this while making Ext2FS::GroupIndex a DistinctNumeric.
2021-02-12 13:27:48 +01:00
Andreas Kling e00f519cdd AK: Add formatter for DistinctNumeric 2021-02-12 12:31:09 +01:00
Andreas Kling 900865975a AK: Allow default-constructing DistinctNumeric
This makes it much more useful as a replacement type for integers.
It's zeroed out by default.
2021-02-12 11:59:15 +01:00
DexesTTP 2acbb811b1 LibCore: Added FileWatcher, a binding for the watch_file syscall
This wrapper abstracts the watch_file setup and file handling, and
allows using the watch_file events as part of the event loop via the
Core::Notifier class.

Also renames the existing DirectoryWatcher class to BlockingFileWatcher,
and adds support for the Modified mode in this class.
2021-02-11 13:13:32 +01:00
Andreas Kling 497d48a8de AK: Make IsUnsigned<T> behave as you would expect 2021-02-10 22:23:54 +01:00
Andreas Kling 548f8a0644 AK: TypeCasts.h should include Assertions.h 2021-02-10 09:13:29 +01:00
AnotherTest 338bb73289 AK: Fix build with Clang>=12
Build failure as in https://oss-fuzz-build-logs.storage.googleapis.com/log-79750138-f41e-4f39-8812-7c536f1d2e35.txt
Clang does not appear to like using consteval functions' arguments as
constant expressions, so move all the arguments that need to appear as
constant expressions into the template parameters for now.

This patch should fix the OSS-Fuzz build.
2021-02-10 08:01:47 +01:00
Andreas Kling 764af6cdec AK: Use StringBuilder::appendff() instead of appendf() 2021-02-09 19:18:13 +01:00
Itamar 2d2b3ba5ed LibCpp: Include CPP_DEBUG in AK/Debug.h 2021-02-08 23:10:38 +01:00
AnotherTest 20765da2a4 AK: Add dbgln() format checking
This checks the following things:
- No unclosed braces in format string
    `dbgln("a:{}}", a)` where the '}}' would be interpreted as a
    literal '}'
    `dbgln("a:{", a)` where someone with a faulty keyboard like mine
    could generate
- No extra closed braces in format string
    `dbgln("a:{{}", a)` where the '{{' would interpreted as a literal '{'
    `dbgln("a:}", a)` where someone with a faulty keyboard could
    generate
- No references to nonexistent arguments
    `dbgln("a:{} b:{}", a)` where the value of `b` is not in the
    arguments list
- No unconsumed argument
    `dbgln("a:{1}", not_used, 1)` where `not_used` is extraneous
2021-02-08 18:08:55 +01:00
AnotherTest 09a43969ba Everywhere: Replace dbgln<flag>(...) with dbgln_if(flag, ...)
Replacement made by `find Kernel Userland -name '*.h' -o -name '*.cpp' | sed -i -Ee 's/dbgln\b<(\w+)>\(/dbgln_if(\1, /g'`
2021-02-08 18:08:55 +01:00
AnotherTest f0d85acc94 AK: Add an iota_array() function that can generate an array
...of increasing values with an optional offset.
2021-02-08 18:08:55 +01:00
Ben Wiederhake de947acaf9 AK: Clean up includes around printf/vdbgprintf 2021-02-08 18:03:57 +01:00
Ben Wiederhake c6a42ab5c3 Everywhere: Remove unnecessary headers 4/4
Arbitrarily split up to make git bisect easier.

These unnecessary #include's were found by combining an automated tool (which
determined likely candidates) and some brain power (which decided whether
the #include is also semantically superfluous).
2021-02-08 18:03:57 +01:00
Ben Wiederhake cc89606ba8 Everywhere: Remove unnecessary headers 3/4
Arbitrarily split up to make git bisect easier.

These unnecessary #include's were found by combining an automated tool (which
determined likely candidates) and some brain power (which decided whether
the #include is also semantically superfluous).
2021-02-08 18:03:57 +01:00
Ben Wiederhake fa2b5ca6f6 Everywhere: Remove unnecessary headers 2/4
Arbitrarily split up to make git bisect easier.

These unnecessary #include's were found by combining an automated tool (which
determined likely candidates) and some brain power (which decided whether
the #include is also semantically superfluous).
2021-02-08 18:03:57 +01:00
Ben Wiederhake c9bb887c55 Everywhere: Remove unnecessary headers 1/4
Arbitrarily split up to make git bisect easier.

These unnecessary #include's were found by combining an automated tool (which
determined likely candidates) and some brain power (which decided whether
the #include is also semantically superfluous).

My favorite #include:

    #include "Applications/Piano/Music.h" // You can't have too much music in life!
2021-02-08 18:03:57 +01:00
Ben Wiederhake 0a2304ba05 Everywhere: Fix weird includes 2021-02-08 18:03:57 +01:00
Ben Wiederhake 667b417d9f AK: remove unused and uninteresting return value
The return value is always be 'count', even in the case of 0.

Note that the return value of TypedTransfer::copy() is likewise uninteresting,
but apparently it is beig used. Hence this patch does not touch it.
2021-02-08 18:03:57 +01:00
Andreas Kling 7c6de80e81 AK: Publish AK::any_of by default 2021-02-07 11:35:08 +01:00
Andreas Kling 8e7ad28a33 AK: Avoid UB in TypedTransfer
Don't be calling __builtin_memfoo() with null pointer arguments.

Found by KUBSAN :^)
2021-02-05 21:23:11 +01:00
asynts 6a00e338a8 Make it possible to overwrite debug macros locally.
Leaking macros across headers is a terrible thing, but I can't think of
a better way of achieving this.

  - We need some way of modifying debug macros from CMake to implement
    ENABLE_ALL_THE_DEBUG_MACROS.

  - We need some way of modifying debug macros in specific source files
    because otherwise we need to rebuild too many files.

This was done using the following script:

    sed -i -E 's/#cmakedefine01 ([A-Z0-9_]+)/#ifndef \1\n\0\n#endif\n/' AK/Debug.h.in
    sed -i -E 's/#cmakedefine01 ([A-Z0-9_]+)/#ifndef \1\n\0\n#endif\n/' Kernel/Debug.h.in
2021-02-04 18:26:22 +01:00
Mart G 9068398f6b AK: Make single pivot quick_sort guarantee a max stack depth of log(n)
- The change to quick_sort requires SimpleIterator to support
  assignment.
- Rename quick_sort to single_pivot_quick_sort to make it easier
  to choose a specific implementation (not based on signature).
- Ensure single_pivot_quick_sort does not copy the pivots
- Expand sorts_without_copy test case to cover both single and dual
  pivot implementations.
2021-02-01 09:52:36 +01:00
Linus Groh e0066ff2d7 AK: Add URL::to_string_encoded()
The result of to_string() passed to urlencode(), with some characters
excluded - basically like JavaScript's encodeURI().
2021-01-31 19:05:55 +01:00
Linus Groh 50e3b122c7 AK: Add optional parameter for excluding chars to urlencode() 2021-01-31 19:05:55 +01:00
Lenny Maiorani be5311be99 Vector: Correctly pass args to insert, insert_before_matching, prepend
Problem:
- Using regular functions rather than function templates results in
  the arguments not being deduced. This then requires the same
  function to be written multiple times and for `move` to be used
  rather than `forward`.

Solution:
- Collapse multiple function overloads to a single function template
  with a deduced argument. This allows the argument to be a forwarding
  reference and bind to either an l-value or r-value and forward the
  value.

Note:
- `append` is not being changed because there are several overloads
  for appending single values and concatenating vectors. This
  conflation needs to be addressed first.
2021-01-31 10:48:12 +01:00
Lenny Maiorani 537bedbf38 HashTable: Correctly pass args to set
Problem:
- Using regular functions rather than function templates results in
  the arguments not being deduced. This then requires the same
  function to be written multiple times and for `move` to be used
  rather than `forward`.

Solution:
- Collapse multiple function overloads to a single function template
  with a deduced argument. This allows the argument to be a forwarding
  reference and bind to either an l-value or r-value and forward the
  value.
2021-01-31 10:48:12 +01:00
Lenny Maiorani 2490fc79ad CircularQueue: Correctly pass args to enqueue
Problem:
- Using regular functions rather than function templates results in
  the arguments not being deduced. This then requires the same
  function to be written multiple times and for `move` to be used
  rather than `forward`.

Solution:
- Collapse multiple function overloads to a single function template
  with a deduced argument. This allows the argument to be a forwarding
  reference and bind to either an l-value or r-value and forward the
  value.
2021-01-31 10:48:12 +01:00
Lenny Maiorani a2501c3981 CircularDeque: Correctly pass args to enqueue
Problem:
- Using regular functions rather than function templates results in
  the arguments not being deduced. This then requires the same
  function to be written multiple times and for `move` to be used
  rather than `forward`.

Solution:
- Collapse multiple function overloads to a single function template
  with a deduced argument. This allows the argument to be a forwarding
  reference and bind to either an l-value or r-value and forward the
  value.
2021-01-31 10:48:12 +01:00
Lenny Maiorani b754121da7 Queue: Correctly pass args to enqueue
Problem:
- Using regular functions rather than function templates results in
  the arguments not being deduced. This then requires the same
  function to be written multiple times and for `move` to be used
  rather than `forward`.

Solution:
- Collapse multiple function overloads to a single function template
  with a deduced argument. This allows the argument to be a forwarding
  reference and bind to either an l-value or r-value and forward the
  value.
2021-01-31 10:48:12 +01:00
Lenny Maiorani 73d6c73b48 SinglyLinkedList: Correctly pass args to append, insert_before, insert_after
Problem:
- Using regular functions rather than function templates results in
  the arguments not being deduced. This then requires the same
  function to be written multiple times and for `move` to be used
  rather than `forward`.

Solution:
- Collapse multiple function overloads to a single function template
  with a deduced argument. This allows the argument to be a forwarding
  reference and bind to either an l-value or r-value and forward the
  value.
2021-01-31 10:48:12 +01:00
Lenny Maiorani 5cee5725e7 SinglyLinkedListWithCount: Correctly pass args to append, insert_before, insert_after
Problem:
- Using regular functions rather than function templates results in
  the arguments not being deduced. This then requires the same
  function to be written multiple times and for `move` to be used
  rather than `forward`.

Solution:
- Collapse multiple function overloads to a single function template
  with a deduced argument. This allows the argument to be a forwarding
  reference and bind to either an l-value or r-value and forward the
  value.
2021-01-31 10:48:12 +01:00
Andreas Kling dc17e01c99 AK: Allow Checked += Checked, and other such operations
The overflow state from both Checkeds is OR'ed in the result.
2021-01-30 13:52:12 +01:00
Ben Wiederhake 4332dfb964 LibGfx: Fix dynamic bitmasks in BMPs
I overlooked a corner case where we might call the built-in ctz() on zero.

Furthermore, the calculation of the shift was wrong and the results were often
unusable.

Both issue were caused by a forgotten 36daeee34f.
This time I made sure to look at bmpsuite_files first, and now they look good.

Found by OSS-Fuzz:
https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=28985
2021-01-30 09:23:18 +01:00
Luke 3f5532d43e LibWeb: Flesh out prepare_script and execute_script
This fills in a bunch of the FIXMEs that was in prepare_script.

execute_script is almost finished, it's just missing the module side.

As an aside, let's not assert when inserting a script element with
innerHTML.
2021-01-29 08:49:50 +01:00
AnotherTest e55d227f93 AK: Provide traits for DistinctNumeric<T> 2021-01-28 17:35:41 +01:00
Andreas Kling 7ec8f83a7f Lagom+AK: Remove remains of clang -Wconsumed usage
We stopped using that warning ages ago since it confused the compiler.
2021-01-28 09:14:49 +01:00
Tom 95bfc12ff4 AK: Include the processor id in log messages 2021-01-27 22:48:41 +01:00
Itamar c96b6987c4 LibCpp: Add the beginning of a C++ parser
This parser will be used by the C++ langauge server to provide better
auto-complete (& maybe also other things in the future).

It is designed to be error tolerant, and keeps track of the position
spans of the AST nodes, which should be useful later for incremental
parsing.
2021-01-27 21:10:57 +01:00
asynts 7cf0c7cc0d Meta: Split debug defines into multiple headers.
The following script was used to make these changes:

    #!/bin/bash
    set -e

    tmp=$(mktemp -d)

    echo "tmp=$tmp"

    find Kernel \( -name '*.cpp' -o -name '*.h' \) | sort > $tmp/Kernel.files
    find . \( -path ./Toolchain -prune -o -path ./Build -prune -o -path ./Kernel -prune \) -o \( -name '*.cpp' -o -name '*.h' \) -print | sort > $tmp/EverythingExceptKernel.files

    cat $tmp/Kernel.files | xargs grep -Eho '[A-Z0-9_]+_DEBUG' | sort | uniq > $tmp/Kernel.macros
    cat $tmp/EverythingExceptKernel.files | xargs grep -Eho '[A-Z0-9_]+_DEBUG' | sort | uniq > $tmp/EverythingExceptKernel.macros

    comm -23 $tmp/Kernel.macros $tmp/EverythingExceptKernel.macros > $tmp/Kernel.unique
    comm -1 $tmp/Kernel.macros $tmp/EverythingExceptKernel.macros > $tmp/EverythingExceptKernel.unique

    cat $tmp/Kernel.unique | awk '{ print "#cmakedefine01 "$1 }' > $tmp/Kernel.header
    cat $tmp/EverythingExceptKernel.unique | awk '{ print "#cmakedefine01 "$1 }' > $tmp/EverythingExceptKernel.header

    for macro in $(cat $tmp/Kernel.unique)
    do
        cat $tmp/Kernel.files | xargs grep -l $macro >> $tmp/Kernel.new-includes ||:
    done
    cat $tmp/Kernel.new-includes | sort > $tmp/Kernel.new-includes.sorted

    for macro in $(cat $tmp/EverythingExceptKernel.unique)
    do
        cat $tmp/Kernel.files | xargs grep -l $macro >> $tmp/Kernel.old-includes ||:
    done
    cat $tmp/Kernel.old-includes | sort > $tmp/Kernel.old-includes.sorted

    comm -23 $tmp/Kernel.new-includes.sorted $tmp/Kernel.old-includes.sorted > $tmp/Kernel.includes.new
    comm -13 $tmp/Kernel.new-includes.sorted $tmp/Kernel.old-includes.sorted > $tmp/Kernel.includes.old
    comm -12 $tmp/Kernel.new-includes.sorted $tmp/Kernel.old-includes.sorted > $tmp/Kernel.includes.mixed

    for file in $(cat $tmp/Kernel.includes.new)
    do
        sed -i -E 's/#include <AK\/Debug\.h>/#include <Kernel\/Debug\.h>/' $file
    done

    for file in $(cat $tmp/Kernel.includes.mixed)
    do
        echo "mixed include in $file, requires manual editing."
    done
2021-01-26 21:20:00 +01:00
asynts eea72b9b5c Everywhere: Hook up remaining debug macros to Debug.h. 2021-01-25 09:47:36 +01:00
asynts 8465683dcf Everywhere: Debug macros instead of constexpr.
This was done with the following script:

    find . \( -name '*.cpp' -o -name '*.h' -o -name '*.in' \) -not -path './Toolchain/*' -not -path './Build/*' -exec sed -i -E 's/dbgln<debug_([a-z_]+)>/dbgln<\U\1_DEBUG>/' {} \;

    find . \( -name '*.cpp' -o -name '*.h' -o -name '*.in' \) -not -path './Toolchain/*' -not -path './Build/*' -exec sed -i -E 's/if constexpr \(debug_([a-z0-9_]+)/if constexpr \(\U\1_DEBUG/' {} \;
2021-01-25 09:47:36 +01:00
asynts bb483f7ef4 Everywhere: Name debug macros more consistently.
Personally, I prefer the naming convention DEBUG_FOO over FOO_DEBUG, but
the majority of the debug macros are already named in the latter naming
convention, so I just enforce consistency here.

This was done with the following script:

    find . \( -name '*.cpp' -o -name '*.h' -o -name '*.in' \) -not -path './Toolchain/*' -not -path './Build/*' -exec sed -i -E 's/DEBUG_PATH/PATH_DEBUG/' {} \;
2021-01-25 09:47:36 +01:00
asynts acdcf59a33 Everywhere: Remove unnecessary debug comments.
It would be tempting to uncomment these statements, but that won't work
with the new changes.

This was done with the following commands:

    find . \( -name '*.cpp' -o -name '*.h' -o -name '*.in' \) -not -path './Toolchain/*' -not -path './Build/*' -exec awk -i inplace '$0 !~ /\/\/#define/ { if (!toggle) { print; } else { toggle = !toggle } } ; $0 ~/\/\/#define/ { toggle = 1 }' {} \;

    find . \( -name '*.cpp' -o -name '*.h' -o -name '*.in' \) -not -path './Toolchain/*' -not -path './Build/*' -exec awk -i inplace '$0 !~ /\/\/ #define/ { if (!toggle) { print; } else { toggle = !toggle } } ; $0 ~/\/\/ #define/ { toggle = 1 }' {} \;
2021-01-25 09:47:36 +01:00
asynts 1a3a0836c0 Everywhere: Use CMake to generate AK/Debug.h.
This was done with the help of several scripts, I dump them here to
easily find them later:

    awk '/#ifdef/ { print "#cmakedefine01 "$2 }' AK/Debug.h.in

    for debug_macro in $(awk '/#ifdef/ { print $2 }' AK/Debug.h.in)
    do
        find . \( -name '*.cpp' -o -name '*.h' -o -name '*.in' \) -not -path './Toolchain/*' -not -path './Build/*' -exec sed -i -E 's/#ifdef '$debug_macro'/#if '$debug_macro'/' {} \;
    done

    # Remember to remove WRAPPER_GERNERATOR_DEBUG from the list.
    awk '/#cmake/ { print "set("$2" ON)" }' AK/Debug.h.in
2021-01-25 09:47:36 +01:00
asynts a7d5fbb8af AK+Format: Add dmesgln() to replace klog(). 2021-01-23 16:46:26 +01:00
asynts 1c1e577a5e Everywhere: Deprecate dbg(). 2021-01-23 16:46:26 +01:00
asynts ea7b7d8ceb Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
asynts fb8d3635d9 Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
asynts 24888457d5 Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
asynts 3f23a58fa1 Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
asynts 7d783d8b84 Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
asynts 5c5665c1e7 Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
asynts 6dc2c38fd0 Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
asynts 7dc3a5ce3f Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
asynts d69a8a9958 Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
asynts 9229ba0fe9 Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
asynts 27bc48e06c Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
asynts 7b0a1a98d9 Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
asynts a348ab55b0 Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
asynts c6ebca5b45 Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
asynts dd727d1fec Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
asynts 67583bc424 Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
asynts 78b2be5a2a Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
asynts 9d588cc9cc Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
asynts 5356aae3cc Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
asynts 663a6141d8 AK: Add set_debug_enabled method. 2021-01-22 22:14:30 +01:00
Tom 7581b64705 AK: Add Vector::remove overload for removing entire ranges 2021-01-17 20:30:31 +01:00
Tom b17a889320 Kernel: Add safe atomic functions
This allows us to perform atomic operations on potentially unsafe
user space pointers.
2021-01-17 20:30:31 +01:00
Andreas Kling 7a0bc2fdb8 LibGfx: Convert all the dbg() in BMPLoader to dbgln()
Also get rid of the awkward IF_BMP_DEBUG macro while we're here.
2021-01-17 15:42:10 +01:00
Andreas Kling 65fa0c2774 AK: Make is<T>(input) use input.fast_is<T>() if available
This allows classes to provide an optimized is<T> via the fast_is<T>()
member function.
2021-01-17 14:42:23 +01:00
Andreas Kling bf0719092f Kernel+Userland: Remove shared buffers (shbufs)
All users of this mechanism have been switched to anonymous files and
passing file descriptors with sendfd()/recvfd().

Shbufs got us where we are today, but it's time we say good-bye to them
and welcome a much more idiomatic replacement. :^)
2021-01-17 09:07:32 +01:00
Andreas Kling 05dbfe9ab6 Kernel: Remove sys$shbuf_seal() and userland wrappers
There are no remaining users of this syscall so let it go. :^)
2021-01-17 00:18:01 +01:00
Andreas Kling b818cf898e Kernel+Userland: Remove sys$shbuf_allow_all() and userland wrappers
Nobody is using globally shared shbufs anymore, so let's remove them.
2021-01-16 22:43:03 +01:00
Andreas Kling de31e82f97 Kernel: Remove sys$shbuf_set_volatile() and userland wrappers
There are no remaining users of this syscall so let's remove it! :^)
2021-01-16 14:52:04 +01:00
asynts 94bb544c33 Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.

This commit touches some dbg() calls which are enclosed in macros. This
should be fine because with the new constexpr stuff, we ensure that the
stuff actually compiles.
2021-01-16 11:54:35 +01:00
asynts 4953c73fc1 AK: Add enabled template parameter to dbgln. 2021-01-16 11:54:35 +01:00
Linus Groh 7ad9b116f7 AK: Add String::join() helper function
This is a simple wrapper around StringBuilder::join().
2021-01-15 23:26:47 +01:00
Linus Groh e83799dc02 AK: Add JsonArray(const Vector<T>) constructor
This simplifies creating a JsonArray from a Vector<T> (when there's a
JsonValue(T) constructor overload for T, that is).
2021-01-15 23:26:47 +01:00
Lenny Maiorani 6d6b3f9523 Badge: Access to underlying type
Problem:
- Access to the underlying type is not provided. This limits
  metaprogramming and usage in function templates.

Solution:
- Provide public access to the underlying type.
- Add test to ensure the underlying type is accessible.
2021-01-15 09:44:21 +01:00
Lenny Maiorani 53afdc0106 StringView: Implement find_first_of in terms of AK::find
Problem:
- The implementation of `find_first_of` is coupled to the
  implementation of `StringView`.

Solution:
- Decouple the implementation of `find_first_of` from the class by
  using a generic `find` algorithm.
2021-01-15 09:42:42 +01:00
Lenny Maiorani d11727ff28 AK: Implement generic any_of algorithm
Problem:
- Raw loops are often written to validate that any values in a
  container meet a predicate, but raw loops are not as expressive as
  functions implementing well-named algorithms and are error-prone.

Solution:
- Implement a very generic form of `any_of`.
2021-01-15 09:42:42 +01:00
AnotherTest 4fe27ec2a7 AK: Use StringView::find() in StringView::split_view()
This fixes #4926.
2021-01-12 23:36:20 +01:00
AnotherTest 39442e6d4f AK: Add String{View,}::find(StringView)
I personally mistook `find_first_of(StringView)` to be analogous to this
so let's add a `find()` method that actually searches the string.
2021-01-12 23:36:20 +01:00
Andreas Kling 1a08ac72ad LibC+Everywhere: Remove open_with_path_length() in favor of open()
This API was a mostly gratuitous deviation from POSIX that gave up some
portability in exchange for avoiding the occasional strlen().

I don't think that was actually achieving anything valuable, so let's
just chill out and have the same open() API as everyone else. :^)
2021-01-12 23:34:01 +01:00
Lenny Maiorani e6f907a155 AK: Simplify constructors and conversions from nullptr_t
Problem:
- Many constructors are defined as `{}` rather than using the ` =
  default` compiler-provided constructor.
- Some types provide an implicit conversion operator from `nullptr_t`
  instead of requiring the caller to default construct. This violates
  the C++ Core Guidelines suggestion to declare single-argument
  constructors explicit
  (https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c46-by-default-declare-single-argument-constructors-explicit).

Solution:
- Change default constructors to use the compiler-provided default
  constructor.
- Remove implicit conversion operators from `nullptr_t` and change
  usage to enforce type consistency without conversion.
2021-01-12 09:11:45 +01:00
AnotherTest 7029a8f605 AK: Specialise convert_to_uint<T> and to_uint<T> for 'long' variants 2021-01-11 21:09:36 +01:00
Sahan Fernando ccf4368ca5 AK: Enable format string warnings for AK printf wrappers 2021-01-11 21:06:32 +01:00
Lenny Maiorani 1b2364846f SinglyLinkedList: Implement find in terms of AK::find
Problem:
- The implementation of `find` is coupled to the implementation of
  `SinglyLinkedList`.

Solution:
- Decouple the implementation of `find` from the class by using a
  generic `find` algorithm.
2021-01-11 19:45:05 +01:00
Lenny Maiorani 853cb8af5c DoublyLinkedList: Implement find in terms of AK::find
Problem:
- The implementation of `find` is coupled to the implementation of
  `DoublyLinkedList`.
- `append` and `prepend` are implemented multiple times so that
  r-value references can be moved from into the new node. This is
  probably not called very often because a pr-value or x-value needs
  to be used here.

Solution:
- Decouple the implementation of `find` from the class by using a
  generic `find` algorithm.
- Make `append` and `prepend` be function templates so that they can
  have binding references which can be forwarded.
2021-01-11 19:45:05 +01:00
Lenny Maiorani f99d1d3bd7 Vector: Implement find, find_if, find_first_matching in terms of AK::find*
Problem:
- The implementation of `find` is coupled to the implementation of `Vector`.
- `Vector::find` takes the predicate by value which might be expensive.

Solution:
- Decouple the implementation of `find` from `Vector` by using a
  generic `find` algorithm.
- Change the name of `find` with a predicate to `find_if` so that a
  binding reference can be used and the predicate can be forwarded to
  avoid copies.
- Change all the `find(pred)` call sites to use `find_if`.
2021-01-11 19:45:05 +01:00
Lenny Maiorani 4333a9a8d6 AK: Find a value in any container offering iterators
Problem:
- `find` is implemented inside of each container. This coupling
  requires that each container needs to individually provide `find`.

Solution:
- Decouple the `find` functionality from the container. This allows
  provides a `find` algorithm which can work with all
  containers. Containers can still provide their own `find` in the
  case where it can be optimized.
- This also allows for searching sub-ranges of a container rather than
  the entire container as some of the container-specific member
  functions enforced.

Note:
- @davidstone's talk from 2015 C++Now conference entitled "Functions
  Want to be Free" encourages this style:
  (https://www.youtube.com/watch?v=_lVlC0xzXDc), but it does come at
  the cost of composability.
- A logical follow-on to this is to provide a mechanism to use a
  short-hand function which automatically searches the entire
  container. This could automatically use the container-provided
  version if available so that functions which provide their own
  optimized version get the benefit.
2021-01-11 19:45:05 +01:00
asynts 723effd051 Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.Everything:
2021-01-11 11:55:47 +01:00
Lenny Maiorani 700f213011 TestTypeTraits: Fix incorrectly namespaced nullptr_t
Problem:
- Clang ToT fails to build `AK/Tests/TestTypeTraits.cpp` because
  `nullptr_t` is missing the `std` namespace qualifier.

Solution:
- Prepend the namespace qualifier.
2021-01-10 18:20:14 +01:00
Andreas Kling 2f3b901f7f AK: Make MappedFile heap-allocated and ref-counted
Let's adapt this class a bit better to how it's actually being used.

Instead of having valid/invalid states and storing an error in case
it's invalid, a MappedFile is now always valid, and the factory
function that creates it will return an OSError if mapping fails.
2021-01-10 16:49:13 +01:00
Andreas Kling a8681da4bf AK: Add AK::OSError, a wrapper for errno codes
This is a strongly typed carrier for "errno" error codes, intended for
use in return types, e.g Result<String, OSError>.
2021-01-10 16:46:13 +01:00
Tom 1fc7d65aad AK: Add static Singleton::get function to allow destructible globals
This enable using global raw pointers rather than Singleton objects,
which solves some problems because global Singleton object could
be deleted when destructors are run.
2021-01-09 21:12:31 +01:00
asynts 1160817a9e AK: Add Formatter<FormatString> as helper class. 2021-01-09 21:11:09 +01:00
Andreas Kling 4a83a37f79 AK: Add release_value() and release_error() to AK::Result
These are nice when you want to move something out of the result,
and match the API we already have for Optional.
2021-01-09 19:57:50 +01:00
Tom fb84f0ec9c AK: Add default memory order as template argument for Atomic<T>
This is useful for collecting statistics, e.g.
Atomic<unsigned, MemoryOrder::memory_order_relaxed> would allow
using operators such as ++ to use relaxed semantics throughout
without having to explicitly call fetch_add with the memory order.
2021-01-04 19:13:52 +01:00
Tom a6c459dd29 AK: Decorate RefCountedBase::try_ref with nodiscard
Because try_ref only increments the ref count if it returned true,
it is important that any caller properly acts upon the return value.
2021-01-04 19:13:52 +01:00
AnotherTest f3ecea1fb3 AK: Add String{,View}::is_whitespace()
+Tests!
2021-01-03 10:47:29 +01:00
asynts 2927656d85 AK: Use size_t in methods of Utf8View. 2021-01-02 01:37:22 +01:00
asynts 3aaece8733 AK: Remove redundant compare() functions. 2021-01-02 01:37:22 +01:00
asynts 632ff01e17 Piggyback: AK: Add formatter for std::nullptr_t. 2021-01-02 01:37:22 +01:00
Andrew Kaster 986544600a AK: Add Result<void, ErrorType> specialization, cleanup
Add a specialization for a void ValueType. This is useful if a generic
function wants to return a Result<T, E> where the user might not
actually care abut the T, and default it to void. In this case it
basically becomes Unexpected<E> instead of Result, but hey, it works :)
2021-01-01 23:01:48 +01:00
Liav A d22d29a29a AK: Add the UUID container
This container represents a universally unique identifier. This will be
used later in the kernel for GUID partitions.
2021-01-01 22:59:48 +01:00
asynts e77031ce67 AK: Deal with unsigned integers in binary search. 2021-01-01 22:23:13 +01:00
Andreas Kling 865f524d5b AK+LibGUI+LibWeb: Remove AK::TypeTraits in favor of RTTI-based helpers
Now that we have RTTI in userspace, we can do away with all this manual
hackery and use dynamic_cast.

We keep the is<T> and downcast<T> helpers since they still provide good
readability improvements. Note that unlike dynamic_cast<T>, downcast<T>
does not fail in a recoverable way, but will assert if the object being
casted is not a T.
2021-01-01 15:33:30 +01:00
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
Itamar b4842d33bb Kernel: Generate a coredump file when a process crashes
When a process crashes, we generate a coredump file and write it in
/tmp/coredumps/.

The coredump file is an ELF file of type ET_CORE.
It contains a segment for every userspace memory region of the process,
and an additional PT_NOTE segment that contains the registers state for
each thread, and a additional data about memory regions
(e.g their name).
2020-12-14 23:05:53 +01:00
Conrad Pankoff a01d11ac19 AK: Add {encode,decode}_hex similar to {encode,decode}_base64 2020-12-13 11:00:20 +01:00
Conrad Pankoff 13f13a9e59 AK: Fix urlencode() with high byte values
Previously urlencode() would encode bytes above 127 incorrectly, printing
them as negative hex values.
2020-12-12 23:50:23 +01:00
xspager dd198e1a29 AK::URL: Fix setting the port number in the case it was the last element of the URL 2020-12-12 20:09:42 +01:00
Andreas Kling 5d0fda3d39 AK: Add String::substring(start)
This is a convenience API when you just want the rest of the string
starting at some index. We already had substring_view() in the same
flavor, so this is a complement to that.
2020-12-10 20:42:11 +01:00
AnotherTest ec4980e875 AK: Ensure dual_pivot_quick_sort does not copy the pivots
Also add a test that would fail to compile if quick_sort tries to copy
anything :P
2020-12-10 11:02:02 +01:00
asynts 5b104eedaf AK: Fix offset calculation error in DuplexMemoryStream::write. 2020-12-09 21:17:24 +01:00
asynts ce15c9a04c AK: Fix unsigned integer underflow in DuplexMemoryStream::write. 2020-12-09 21:17:24 +01:00
asynts 78eff163ea AK: Add String::substring_view(size_t). 2020-12-09 21:05:06 +01:00
AnotherTest 3c41487db7 AK: Fix reading across chunks in DuplexMemoryStream 2020-12-08 23:34:38 +01:00
AnotherTest 602d2ff856 AK: Implement DuplexMemoryStream::offset_of() in terms of memmem()
This fixes the FIXME about missing matches that go across chunk
boundaries.
2020-12-08 23:34:38 +01:00
AnotherTest 036b39cdfd AK: Implement memmem() for iterator haystacks
This uses the KMP algorithm to implement the search.
Also replaces the slow route of the normal memmem() with KMP, which
should be fairly faster (O(n + m) as opposed to O(n * m)) :^)
2020-12-08 23:34:38 +01:00
AnotherTest c85eaadb48 AK: Forward declare Nonnull{Own,Ref}PtrVector 2020-12-08 23:34:38 +01:00
Sahan Fernando d50934823a AK: Add header for SIMD vectorized types 2020-12-08 09:39:43 +01:00
Linus Groh 2313e58393 AK: Make Formatter<StringView> not choke on Mode::Character
Formatter<char> internally uses Formatter<StringView> when in
Mode::Character, but that would only accept Mode::{Default,String} and
ASSERT_NOT_REACHED() otherwise, causing String::formatted("{:c}", 'a')
to crash
2020-12-06 18:52:52 +01:00
Andrew Kaster 6919639190 AK: Add test for the will_be_destroyed and one_ref_left magic functions
Fixes a regression introduced by 5c1b3ce. The commit description there
asserts that the changes allow calling will_be_destroyed and
one_ref_left, which are not required to be const qualified. The
implementation in fact does require the methods to be const qualified,
because we forgot to add the const_cast inside the decltypes :^)
2020-12-06 15:49:33 +01:00
Linus Groh 4dcd23c2be AK: Pull Is{Integral,FloatingPoint} into the global namespace 2020-12-05 23:52:17 +01:00
Tom c6230b746d AK: Add insert_before/insert_after to InlineLinkedList 2020-12-02 13:02:04 +01:00
Linus Groh ba020a5907 AK: Fix logic error in urldecode() percent-decoding
We also need to append the raw consumed value if *either* of the two
characters after the % isn't a hex digit, not only if *both* aren't.

Fixes #4257.
2020-11-30 11:35:01 +01:00
AnotherTest c6ca8534a6 AK: Export ShouldChomp::NoChomp too
It's much more elegant to say 'should_chomp ? Chomp : NoChomp' than to
say 'if (should_chomp) ...(..., Chomp) else ...(...)'.
2020-11-29 20:32:10 +01:00
devashish 0b252c31b2 AK: Add missing GenericTraits<u8>
This enables us to use keys of type u8 in HashMaps.
2020-11-29 16:22:04 +01:00
DavidKorczynski ff8ac60184
Lagom: Various fixes to make Lagom run on OSS-Fuzz (#4176) 2020-11-27 23:57:02 +01:00
Andreas Kling 2f491e7769 AK: Always inline RefPtr::operator bool() and RefPtr::is_null() 2020-11-25 21:26:58 +01:00
Linus Groh 5dcd1c2709 AK: Don't return empty StringImpl from create() when char* starts with \0
When creating a StringImpl for a C string that starts with a null-byte,
we would ignore the explicitly given length and return the empty
StringImpl - presumably to check for "\0", but this leads to false
positives ("\0foo") so let's only care about the length.
2020-11-25 20:17:25 +01:00
Andreas Kling 54ade31d84 AK: Add some inline capacity to StringBuilder
This patch adds a 128-byte inline buffer that we use before switching
to using a dynamically growing ByteBuffer.

This allows us to avoid heap allocations in many cases, and totally
incidentally also speeds up @nico's favorite test, "disasm /bin/id"
more than 2x. :^)
2020-11-24 22:06:51 +01:00
Andreas Kling c33d71c5ff AK: Add IntrusiveList::take_last() 2020-11-24 16:37:55 +01:00
Sergey Bugaev fa2e3e2be4 LibIPC: Prepend each message with its size
This makes it much simpler to determine when we've read a complete message, and
will make it possible to integrate recvfd() in the future commit.
2020-11-23 18:37:40 +01:00
Sergey Bugaev d62346c0b1 AK: Add Vector::prepend() overload for multiple items
Much like with Vector::append(), you may want to append multiple items in one
go. It's actually more important to do this for prepending, because you don't
want to copy the rest of items further each time.
2020-11-23 18:37:40 +01:00
Andreas Kling dd43cf2657 AK: Use ALWAYS_INLINE all over NonnullPtrVector
I saw NonnullOwnPtrVector::at() in a profile and that was silly, as we
should definitely be inlining it.
2020-11-23 14:08:50 +01:00
Luke 819f099a8e AK: Add first_matching and last_matching to Vector
first_matching returns the first item in the vector that matches
the given condition.

last_matching returns the last item in the vector that matches
the given condition.
2020-11-22 18:20:56 +01:00
BenJilks 29ada654b1 AK: Fix base64 decoding '/'
When creating the lookup table, it wouldn't add the last
character
2020-11-22 16:07:00 +01:00
Lenny Maiorani 7d8a9bdb1e AK: Cleanup missing includes and #ifdef evaluation
Problem:
- Several files have missing includes. This results in complaints from
  `clang-tidy`.
- `#ifdef` is followed by `#elif <value>` which evaluates to `0`.

Solution:
- Add missing includes.
- Change to `#elif defined(<value>)`.
2020-11-22 11:35:53 +01:00
Lenny Maiorani 840c3b501d NeverDestroyed: Add tests
Problem:
- It is difficult to refactor because there are no tests to bind the
functionality.
- Arguments are not forwarded correctly to the constructor.

Solution:
- Add tests.
- Change constructor to take forwarding references.
2020-11-22 10:54:33 +01:00
Lenny Maiorani 178190ab52 MACAddress: Use all_of to implement is_zero
Problem:
- `is_zero()` is implemented by checking each value in the array by
  hand. This is error-prone and less expressive than using an
  algorithm.

Solution:
- Implement `is_zero()` in terms of `all_of`.
2020-11-21 19:25:14 +01:00
Lenny Maiorani 6e7e16a7ed AK: Implement generic all_of algorithm
Problem:
- Raw loops are often written to validate that all values in a
  container meet a predicate, but raw loops are not as expressive as
  functions implementing well-named algorithms and are error-prone.

Solution:
- Implement a very generic form of `all_of`.
2020-11-21 19:25:14 +01:00
Lenny Maiorani bdf3baa8ac MACAddress: AK::Array as member variable instead of C-array
Problem:
- C-style arrays do not automatically provide bounds checking and are
  less type safe overall.
- `__builtin_memcmp` is not a constant expression in the current gcc.

Solution:
- Change private m_data to be AK::Array.
- Eliminate constructor from C-style array.
- Change users of the C-style array constructor to use the default
  constructor.
- Change `operator==()` to be a hand-written comparison loop and let
  the optimizer figure out to use `memcmp`.
2020-11-20 21:18:14 +01:00
Lenny Maiorani 964d2e0dd0 MACAddress: constexpr support
Problem:
- `MACAddress` class is not usable in a compile-time context.
- `__builtin_memcpy` is not constexpr in gcc.

Solution:
- Decorate functions with `constexpr` keyword.
- Use default constructors and destructors.
- Change `__builtin_memcpy` to a hand-written `for` loop and let the
  compiler's optimizer take care of it.
- Add tests to ensure compile-time capabilities.
2020-11-19 14:03:47 +01:00
AnotherTest 4c343c5f26 AK: Fix OOB access in DuplexMemoryStream::offset_of()
This fixes an OOB access when the last read/written chunk is empty (as we _just_
started on a new chunk).
Also adds a test case to TestMemoryStream.
Found via human fuzzing in the shell:
```sh
for $(cat /dev/urandom) {
    clear
    match $it {
        ?* as (x) {
            echo $x
            sleep 1
        }
    }
}
```
would assert at some point.
2020-11-17 17:07:39 +01:00
Linus Groh d6a4c0c79e AK: Trim whitespace in StringUtils::convert_to_{int,uint,uint_from_hex}()
Personally I found this unintuitive at first, but it is in line with
strtol(), Python's int() or JavaScript's parseInt(), so I guess it makes
sense.

Fixes #4097.
2020-11-17 09:48:35 +01:00
Lenny Maiorani fd97f23cef MACAddress: Unit testing for basic functionality
Problem:
- There are no unit tests for `MACAddress` class. This makes it
  difficult to refactor and ensure the same behavior.
- `m_data` private member variable is uninitialized leading to undefined
  behavior of `is_zero()`.

Solution:
- Add unit tests to cover basic functionality.
- Initialize `m_data`.
2020-11-17 09:47:50 +01:00
Lenny Maiorani 2a06b026ef Vector: C++20 equality operators
Problem:
- C++20 changes the way equality operators are generated. This results
  in overload ambiguity as reported by clang.

Solution:
- Remove `AK::Vector::operator!=` because it will be automatically
  generated in terms of `AK::Vector::operator==`.
- Change `AK::Vector::operator==` to be a function template so that
  overload resolution is not confused about `a == b` vs `b == a`.
- Add tests to ensure the behavior works.

Notes:
- There is more info available at
  https://brevzin.github.io/c++/2019/07/28/comparisons-cpp20/ for
  deeper discussion about overload resolution, operator rewriting, and
  generated functions.
2020-11-16 10:06:23 +01:00
Andreas Kling adabcf24ec Everywhere: Add missing <AK/ByteBuffer.h> includes
All of these files were getting ByteBuffer.h from someone else and then
using it. Let's include it explicitly.
2020-11-15 13:11:21 +01:00
Andreas Kling a5982f8605 AK: Mark SimpleIterator::operator*() as ALWAYS_INLINE
This gives the compiler enough information to optimize index validation
when using range-for to iterate over a Vector, drastically reducing the
cost of such loops.
2020-11-14 17:20:17 +01:00
Linus Groh d3ee3fc68a AK: Fix StringUtils::contains() case insensitive search
It would incorrectly return false if needle was at the end the string.
2020-11-14 10:11:26 +01:00
Andreas Kling cfc5d146d3 AK: Fix inverted condition in unsigned LEB128 decode 2020-11-13 11:05:46 +01:00
Lenny Maiorani f5ced347e6 AK: Prefer using instead of typedef
Problem:
- `typedef` is a keyword which comes from C and carries with it old
  syntax that is hard to read.
- Creating type aliases with the `using` keyword allows for easier
  future maintenance because it supports template syntax.
- There is inconsistent use of `typedef` vs `using`.

Solution:
- Use `clang-tidy`'s checker called `modernize-use-using` to update
  the syntax to use the newer syntax.
- Remove unused functions to make `clang-tidy` happy.
- This results in consistency within the codebase.
2020-11-12 10:19:04 +01:00
Lenny Maiorani 5570b16f6f RefPtrTraits: struct/class mismatch in forward declaration
Problem:
- Building with clang is broken because of the `struct` vs `class`
  mismatch between the definition and declaration.

Solution:
- Change `class` to `struct` in the forward declaration.
2020-11-11 20:25:29 +01:00
Lenny Maiorani 72d019f4a4 IPv4Address: constexpr support
Problem:
- IPv4Address class cannot be used in a compile-time context.
- A union is used by initializing one of the members and reading the
  non-active member. This is undefined behavior and not permitted in a
  `constexpr` context.

Solution:
- Eliminate undefined behavior by changing to a simple `u32` for
  storage instead of the union and performing mask/shift calculations
  for obtaining the individual octets.
- Decorate functions with `constexpr` where possible. Currently string
  formatting and optionals are not `constexpr`-capable so functions
  using those are left out.
- Modify tests to validate functionality in a `constexpr` context in
  addition to the run-time tests already being run. This ensures that
  functionality is the same in both contexts.
2020-11-11 12:18:25 +01:00
Tom 75f61fe3d9 AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.

Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.

In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-11-10 19:11:52 +01:00
Tom 3c1ef744f6 AK: Add RefPtrTraits to allow implementing custom null pointers
This adds the ability to implement custom null states that allow
storing state in null pointers.
2020-11-10 19:11:52 +01:00
Lenny Maiorani c5b26a0df8 IPv4Address: Unit tests
Problem:
- There is no direct unit testing of the IPv4Address functionality
  which makes refactoring difficult.

Solution:
- Add unit tests to cover the current functionality of
  IPv4Address. This will allow future refactorings with confidence.
2020-11-10 09:30:39 +01:00
asynts 32957745fb AK: Add formatters for floating point numbers. 2020-11-09 16:21:29 +01:00
asynts 3b3edbc4d2 AK: Rename new_out to out and new_warn to warn. 2020-11-09 16:21:29 +01:00
asynts 74438e6fdc AK: Remove out() and warn(). 2020-11-09 16:21:29 +01:00
Andreas Kling 81b7c072ed AK: Use reference algorithms for LEB128 parsing
This fixes a bug in signed LEB128 parsing (sign extension stage)
which would sometimes cause debug info to look very strange.
2020-11-08 22:38:27 +01:00
Linus Groh 9c3ead8f91 LibJS+AK: Move cross-platform stack bounds code from JS::Heap to AK::StackInfo
This will be useful for other things than the Heap, maybe even outside
of LibJS.
2020-11-08 16:51:54 +01:00
Nico Weber e673abb93f AK: Remove duplicate begin()/end() methods
begin()/end() returning a ConstItertor already exist further up
in this file. Nothing uses these redundant versions, and they are not
callable.
2020-11-07 18:28:35 +01:00
Andreas Kling e9403e2b03 AK: printf was not accounting for plus sign with "%+d"
We have to include the plus sign in the number of characters written,
otherwise sprintf() will put the null terminator too early.
2020-11-05 11:04:10 +01:00
Andreas Kling 575c483310 AK: Always include <new> from compiler before our operators new
We had competing inline definitions of the placement operators new.
Avoid this by having <AK/kmalloc.h> pull in <new> from the compiler
and always using their definitions instead.

I feel like there must be an elegant solution to this whole situation
with the operators, but I'm not sure what it is.
2020-11-05 09:59:30 +01:00
Brendan Coles 3e0e84dcd1 AK::URL: Check if URL requires a port set to be considered a valid URL
`AK::URL` will now check if the URL requires a port to be set using
`AK::URL.protocol_requires_port(protocol)`.

If the URL does not specify a port, and no default port for the URL
protocol is found with `AK::URL.default_port_for_protocol(protocol)`,
the URL is considered to be invalid.
2020-11-04 19:34:00 +01:00
AnotherTest 060ddd2a7a AK: Really disallow making OwnPtrs from refcounted types
This looks at three things:
- if the type has a typedef `AllowOwnPtr', respect that
- if not, disallow construction if both of `ref()' and `unref()' are
  present.
Note that in the second case, if a type only defines `ref()' or only
defines `unref()', an OwnPtr can be created, as a RefPtr of that type
would be ill-formed.

Also marks a `Performance' to explicitly allow OwnPtrs.
2020-11-03 19:14:34 +01:00
Andreas Kling 5e164052f6 AK+Kernel: Escape JSON keys & values
Grab the escaping logic from JSON string value serialization and use
it for serializing all keys and values.

Fixes #3917.
2020-11-02 12:56:36 +01:00
AnotherTest 0801b1fada AK: Make String::matches() capable of reporting match positions too
Also, rewrite StringUtils::match(), because the old implementation was
fairly broken, e.g. "acdcxb" would *not* match "a*?b".
2020-10-29 11:53:01 +01:00
AnotherTest f0e59f2dac AK: Add a `is_one_of()' to StringView
This copies the similar API from String.
2020-10-29 11:53:01 +01:00
asynts 607931268e CMake: Use CONFIGURE_DEPENDS in existing globs. 2020-10-29 11:52:47 +01:00
Linus Groh 1daa5158eb AK: Add GenericLexer::retreat()
This allows going back one character at a time, and then re-consume
previously consumed chars.
The code I need this for looks something like this:

    ASSERT(lexer.consume_specific('\\'));
    if (lexer.next_is("foo"))
        ...
    lexer.retreat();
    lexer.consume_escaped_character();  // This expects lexer.peek() == '\\'
2020-10-29 11:52:31 +01:00
asynts 1319ad476d AK: Deprecate warn(). 2020-10-25 18:52:51 +01:00
asynts 1254cbbd0b AK: Eradicate calls to warn(). 2020-10-25 18:52:51 +01:00