summaryrefslogtreecommitdiff
path: root/AK
AgeCommit message (Collapse)Author
2021-09-06AK: Make Json{Array,Object}Serializer ignore append() return valuesAndreas Kling
This is in preparation for making KBufferBuilder::append() and friends return a KResult. Long-term we should come up with a solution that works for both kernel and userspace clients of the JSON API.
2021-09-06Everywhere: Make ByteBuffer::{create_*,copy}() OOM-safeAli Mohammad Pur
2021-09-06Everywhere: Use OOM-safe ByteBuffer APIs where possibleAli Mohammad Pur
If we can easily communicate failure, let's avoid asserting and report failure instead.
2021-09-06AK: Add OOM-safe ByteBuffer::try_{resize,append,ensure_capacity}() APIsAli Mohammad Pur
2021-09-05AK: Add AssertSize utility template to provide rich type size assertionsBrian Gianforcaro
This type is useful, as the sizes will be visible in the compiler error messages, as they will be part of the template parameters. This is not possible with a normal static_assert of the sizeof a type.
2021-09-05AK: Add a TRY(expression) macro to simplify the unwrap-or-return patternAndreas Kling
The way we use classes like Kernel::KResultOr<T> and AK::Result<T, E> makes checking for errors (and short-circuiting returns) quite verbose. This patch adds a new TRY(expression) macro that either evaluates to the released result of the expression if successful, or returns the error if not. Before: auto foo_or_error = get_foo(); if (foo_or_error.is_error()) return foo_or_error.release_error(); auto foo = foo_or_error.release_value(); After: auto foo = TRY(get_foo()); The macro uses a GNU C++ extension which is supported by GCC, Clang, Intel C++, and possibly others. It's not *ideal*, but since it makes our codebase considerably nicer, let's try(!) it out. :^) Co-authored-by: Ali Mohammad Pur <mpfard@serenityos.org>
2021-09-05AK+Kernel: Move KResult.h to Kernel/API for userspace accesssin-ack
This commit moves the KResult and KResultOr objects to Kernel/API to signify that they may now be freely used by userspace code at points where a syscall-related error result is to be expected. It also exposes KResult and KResultOr to the global namespace to make it nicer to use for userspace code.
2021-09-04AK+LibRegex: Disable construction of views from temporary StringsIdan Horowitz
2021-09-04AK: Add HashMap::ensure(key, callback)Andreas Kling
This function ensures that a key is present in the HashMap. If it's not present, it is inserted, and the corresponding value is initialized with whatever the callback returns. It allows us to express this: auto it = map.find(key); if (it == map.end()) { map.set(it, make_a_value()); it = map.find(key); } auto& value = it->value; Like this: auto& value = map.ensure(key, [] { return make_a_value(); }); Note that the callback is only invoked if we have to insert a missing key into the HashMap. This is important in case constructing the default value is expensive or otherwise undesirable.
2021-09-04AK: Make declaration of std::move and std::forward optionalStephan Unverwerth
This introduces a new define AK_DONT_REPLACE_STD that disables our own implementation of std::move and std::forward. Some ports include both STL and AK headers which causes conflicts when trying to resolve those functions. The port can define AK_DONT_REPLACE_STD before including Serenity headers in that case.
2021-09-04AK: Add rvalue-ref qualifiers for Optional's value() and value_or()Andreas Kling
This avoids a value copy when calling value() or value_or() on a temporary Optional. This is very common when using the HashMap::get() API like this: auto value = hash_map.get(key).value_or(fallback_value);
2021-09-04AK: Convert Optional.h to east-const styleAndreas Kling
2021-09-03Everywhere: Prevent risky implicit casts of (Nonnull)RefPtrDaniel Bertalan
Our existing implementation did not check the element type of the other pointer in the constructors and move assignment operators. This meant that some operations that would require explicit casting on raw pointers were done implicitly, such as: - downcasting a base class to a derived class (e.g. `Kernel::Inode` => `Kernel::ProcFSDirectoryInode` in Kernel/ProcFS.cpp), - casting to an unrelated type (e.g. `Promise<bool>` => `Promise<Empty>` in LibIMAP/Client.cpp) This, of course, allows gross violations of the type system, and makes the need to type-check less obvious before downcasting. Luckily, while adding the `static_ptr_cast`s, only two truly incorrect usages were found; in the other instances, our casts just needed to be made explicit.
2021-09-03AK: Rename create<T> => make_ref_counted<T>Andreas Kling
And also try_create<T> => try_make_ref_counted<T>. A global "create" was a bit much. The new name matches make<T> better, which we've used for making single-owner objects since forever.
2021-09-01AK: Move forward() into the std namespaceAndreas Kling
Same as we already did with move(). This allows compiler diagnostics and static analyzers like SonarCloud to detect more issues.
2021-09-01AK: Pass AK::Format TypeErasedFormatParams by reference in AK::StringBrian Gianforcaro
This silences a overeager warning in sonar cloud, warning that slicing could occur with `VariadicFormatParams` which derives from `TypeErasedFormatParams`. Reference: https://sonarcloud.io/project/issues?id=SerenityOS_serenity&issues=AXuVPBW3k92xXUF3qXTE&open=AXuVPBW3k92xXUF3qXTE This is a continuation of f0b3aa033134b788a28fe8cf8ff6028d0e7941e8.
2021-09-01AK: Remove dead store from Time:operator+Brian Gianforcaro
This is written, but never read again, and is thus useless.
2021-08-31AK: Add Statistics helperTobias Christiansen
This patch adds a helper to AK which allows for basic statistical analysis of values. The median algorithm is very naive and slow, but it works.
2021-08-31AK: Don't perform the shift when it's too large when decoding LEB128Ali Mohammad Pur
Prior to this, we calculated whether the shift was too large for the result, and then did the shift regardless. Found by OSS-Fuzz: https://oss-fuzz.com/testcase-detail/6046441716973568
2021-08-31AK: Make SinglyLinkedList::remove() publickleines Filmröllchen
This is a nice API to have outside of the class, and it is convenient for LibDSP.
2021-08-31AK: Add FixedPoint arithmetic helperHediadyoin1
Co-authored-by: Hendiadyoin1 <leon2002.la@gmail.com> Co-authored-by: kleines Filmröllchen <malu.bertsch@gmail.com>
2021-08-30AK: Return early from swap() when swapping the same objectTimothy Flynn
When swapping the same object, we could end up with a double-free error. This was found while quick-sorting a Vector of Variants holding complex types, reproduced by the new swap_same_complex_object test case.
2021-08-30AK: Use get_random() in IDAllocatorAndreas Kling
Also generate a new random ID on collision, instead of using the old ID + 1. :^) SonarCloud: https://sonarcloud.io/project/security_hotspots?id=SerenityOS_serenity&hotspots=AXuVPBMNk92xXUF3qWZd
2021-08-30AK: Add operator delete stub to all AK_MAKE_ETERNAL objectsBrian Gianforcaro
Static analysis correctly flags that we are missing an implementation for `operator delete` for all classes which are annotated with AK_MAKE_ETERNAL. To appease static analysis define an implementation which asserts to make sure no one ever calls delete on the object.
2021-08-30Everywhere: Pass AK::Format TypeErasedFormatParams by referenceBrian Gianforcaro
This silences a overeager warning in sonar cloud, warning that slicing could occur with `VariadicFormatParams` which derives from `TypeErasedFormatParams`. Reference: https://sonarcloud.io/project/issues?id=SerenityOS_serenity&issues=AXuVPBO_k92xXUF3qWsm&open=AXuVPBO_k92xXUF3qWsm
2021-08-27AK: Add Traits for Enumskleines Filmröllchen
Enums can be hashed as their underlying integral type. This allows enum keys in hash maps etc.
2021-08-26AK: Implement method to convert a String/StringView to title caseTimothy Flynn
This implementation preserves consecutive spaces in the orginal string.
2021-08-26AK: Make explode_byte depend on sizeof(FlatPtr) instead of ARCH(...)Ali Mohammad Pur
The assumption that FlatPtr is 64-bit on every platform except i686 is not correct, and also makes the definition of explode_byte() less nice to look at.
2021-08-24Userland: Remove IRC ClientAndreas Kling
The IRC Client application made some sense while our main communication hub was an IRC channel. Now that we've moved on, IRC is just a random protocol with no particular relevance to this project. This also has the benefit of removing one major client of the single- process Web::InProcessWebView class.
2021-08-23AK: Use explode_byte for pointer sanitizationHendiadyoin1
2021-08-23Kernel: Rename Processor::id() => current_id()Andreas Kling
And let id() be the non-static version that gives you the ID of a Processor object.
2021-08-22AK: Use POSIX specified types for Time::to_timespec and to_timevalRichard Wurth
The previous implementation assumed 64-bit time_t and 32-bit long, which is not true on some 32-bit systems
2021-08-21AK: Use `__builtin_bit_cast` if availableJesse Buhagiar
We now use the compiler's buitin version of bitcast if it's available instead of just resorting to using the builtin `memcpy`.
2021-08-19AK: Add GenericLexer API to consume an escaped Unicode code pointTimothy Flynn
This parsing is already duplicated between LibJS and LibRegex, and will shortly be needed in more places in those libraries. Move it to AK to prevent further duplication. This API will consume escaped Unicode code points of the form: \\u{code point} \\unnnn (where each n is a hexadecimal digit) \\unnnn\\unnnn (where the two escaped values are a surrogate pair)
2021-08-19AK: Move FormatParser definition from header to implementation fileTimothy Flynn
This is primarily to be able to remove the GenericLexer include out of Format.h as well. A subsequent commit will add AK::Result to GenericLexer, which will cause naming conflicts with other structures named Result. This can be avoided (for now) by preventing nearly every file in the system from implicitly including GenericLexer. Other changes in this commit are to add the GenericLexer include to files where it is missing.
2021-08-19AK: Add GenericLexer to forwarding headerTimothy Flynn
2021-08-19LibCore: Make DateTime's members signedIdan Horowitz
Core::DateTime is essentially a C++ wrapper of the tm struct, so we should support the same signed range as the underlying tm.
2021-08-19AK: Enable IntrusiveList self reference to be optimized out when emptyBrian Gianforcaro
If a member is an empty class, the standard normally stats that it needs to have a size of at least 1 byte in order to guarantee that the addresses of distinct objects of the same type are always distinct. However as of c++20, we can use [[no_unique_address]] to instruct the compiler that if the member has an empty type, it may optimize it to occupy no space.
2021-08-18AK+Kernel: StringView hash map Traits should not set peek type to StringBrian Gianforcaro
This typo / bug in the Traits<T> implementation for StringView caused AK::HashMap methods to return a `String` when looking up values out of a hash map of type HashTable<StringView,StringView>. This change fixes the typo, and fixes the only consumer, the kernel Commandline class.
2021-08-18AK: Add Utf8View::byte_offset_of overload for code point index lookupsTimothy Flynn
2021-08-15AK: Add adopt_nonnull_own_or_enomemsin-ack
This is basically a complement to adopt_nonnull_ref_or_enomem, and simplifies boilerplate for try_create functions which just return ENOMEM or the object based on whether it was able to allocate.
2021-08-15AK: Stop publishing detail namespaced functionsLenny Maiorani
Problem: - `AK::Detail::integer_sequence_generate_array` is published via a `using` directive in the `Array.h` header, but this is a `Detail` function. Solution: - Remove the `using` declaration.
2021-08-15AK: Pull RefCountedBase into the global namespaceAndreas Kling
2021-08-15AK: Add Time::is_negative() to detect negative time valuesBrian Gianforcaro
2021-08-15AK: Annotate AK::Time APIs as [[nodiscard]]Brian Gianforcaro
2021-08-15AK: Add Time::now_<clock_id> functions for obtaining the current timeBrian Gianforcaro
In the quest of removing as timespec / timeval usage in the Userland as possible, we need a way to conveniently retrieving the current clock time from the kernel and storing it in `AK::Time` format.
2021-08-15AK: Add adopt_nonnull_ref_or_enomemsin-ack
This gets rid of the ENOMEM boilerplate for handling memory allocation failures in the kernel.
2021-08-13IntrusiveRedBlackTree: Remove redundant subtraction of 0Lenny Maiorani
Problem: - ToT clang will not build due to casting `nullptr` to `u8*`. This is redundant because it casts to get a `0` then subtracts it. Solution: - Remove it since subtracting `0` doesn't do anything.
2021-08-13AK+Everywhere: Delete Variant's default constructorAli Mohammad Pur
This was exposed to the user by mistake, and even accumulated a bunch of users that didn't blow up out of sheer luck.
2021-08-13AK: Annotate String.count as [[nodiscard]]Brian Gianforcaro