summaryrefslogtreecommitdiff
path: root/AK/ByteBuffer.h
AgeCommit message (Collapse)Author
2022-11-06Everywhere: Remove redundant inequality comparison operatorsDaniel Bertalan
C++20 can automatically synthesize `operator!=` from `operator==`, so there is no point in writing such functions by hand if all they do is call through to `operator==`. This fixes a compile error with compilers that implement P2468 (Clang 16 currently). This paper restores the C++17 behavior that if both `T::operator==(U)` and `T::operator!=(U)` exist, `U == T` won't be rewritten in reverse to call `T::operator==(U)`. Removing `!=` operators makes the rewriting possible again. See https://reviews.llvm.org/D134529#3853062
2022-11-01AK: Use size_t for ByteBuffer offsetsTimothy Flynn
These should be unsigned values. An east-const conversion was also performed by clang-format 15.
2022-07-14AK: Add Traits<ByteBuffer>::hash()Linus Groh
2022-06-27AK: Perform a resize in ByteBuffer::get_bytes_for_writing()Lucas CHOLLET
ByteBuffer::get_bytes_for_writing() was only ensuring capacity before this patch. The method needs to call resize to register the appended data, otherwise it will be overwritten with next data addition.
2022-06-13AK/ByteBuffer+Everywhere: Handle errors in ByteBuffer::slice()Matthias Zimmerman
2022-05-12AK+DHCPClient: Fix false positive gcc 12 warningsDaniel Bertalan
The compiler would complain about `__builtin_memcpy` in ByteBuffer::copy writing out of bounds, as it isn't able to deduce the invariant that the inline buffer is only used when the requested size is smaller than the inline capacity. The other change is more bizarre. If the destructor's declaration exists, gcc complains about a `delete` operation causing an out-of-bounds array access. error: array subscript 'DHCPv4Client::__as_base [0]' is partly outside array bounds of 'unsigned char [8]' [-Werror=array-bounds] 14 | ~DHCPv4Client() = default; | ^ This looks like a compiler bug, and I'll report it if I find a suitable reduced reproducer.
2022-02-13AK: Don't call memcpy() in ByteBuffer::append(u8)Andreas Kling
We can emit way nicer code for the just-append-a-single-byte case.
2022-01-24Everywhere: Convert ByteBuffer factory methods from Optional -> ErrorOrSam Atkins
Apologies for the enormous commit, but I don't see a way to split this up nicely. In the vast majority of cases it's a simple change. A few extra places can use TRY instead of manual error checking though. :^)
2022-01-13AK: Add ByteBuffer::{must_,}get_bytes_for_writing()sin-ack
This is useful for writing new data at the end of a ByteBuffer. For instance, with the Stream API: auto pending_bytes = TRY(stream.pending_bytes()); auto receive_buffer = TRY(buffer.get_bytes_for_writing( pending_bytes)); TRY(stream.read(receive_buffer));
2022-01-09AK: Add ByteBuffer::append(char)Maciej
2021-12-16AK: Use __builtin_memmove for ByteBuffer and Span's overwritesin-ack
__builtin_memcpy will fail when the target area and the source area overlap. Using __builtin_memmove will handle this case as well.
2021-11-14AK: Resolve clang-tidy readability-qualified-auto warningsAndrew Kaster
... In files included by Kernel/Process.cpp and Kernel/Thread.cpp
2021-11-14AK: Resolve clang-tidy readability-bool-conversion warningsAndrew Kaster
... In files included by Kernel/Process.cpp and Kernel/Thread.cpp
2021-11-11Everywhere: Pass AK::ReadonlyBytes by valueAndreas Kling
2021-11-10AK: Make ByteBuffer::try_* functions return ErrorOr<void>Andreas Kling
Same as Vector, ByteBuffer now also signals allocation failure by returning an ENOMEM Error instead of a bool, allowing us to use the TRY() and MUST() patterns.
2021-09-06Everywhere: Make ByteBuffer::{create_*,copy}() OOM-safeAli Mohammad Pur
2021-09-06AK: Add OOM-safe ByteBuffer::try_{resize,append,ensure_capacity}() APIsAli Mohammad Pur
2021-07-11AK: Don't forget to kfree_sized() in ByteBufferAndreas Kling
2021-07-11AK: Don't use realloc() in AK::ByteBufferAndreas Kling
This class is the only reason we have to support krealloc() in the kernel heap, something which adds a lot of complexity. Let's move towards a simpler path and do malloc+memset in the ByteBuffer code (where we know the sizes anyway.)
2021-07-11AK: Use kfree_sized() in AK::ByteBufferAndreas Kling
2021-06-12AK: Add ByteBuffer::append(ReadonlyBytes)Matthew Olsson
2021-06-08LibC+AK: Remove our custom macros from <assert.h>Gunnar Beutner
Other software might not expect these to be defined and behave differently if they _are_ defined, e.g. scummvm which checks if the TODO macro is defined and fails to build if it is.
2021-05-31AK: Split the ByteBuffer::trim method into two methodsGunnar Beutner
This allows us to mark the slow part (i.e. where we copy the buffer) as NEVER_INLINE because this should almost never get called and therefore should also not get inlined into callers.
2021-05-31AK: Remove the public ByteBuffer::trim methodGunnar Beutner
This removes the public trim() method because it is no longer necessary. Callers can instead use resize().
2021-05-31AK: Replace ByteBuffer::grow with resize()/ensure_capacity()Gunnar Beutner
Previously ByteBuffer::grow() behaved like Vector<T>::resize(). However the function name was somewhat ambiguous - and so this patch updates ByteBuffer to behave more like Vector<T> by replacing grow() with resize() and adding an ensure_capacity() method. This also lets the user change the buffer's capacity without affecting the size which was not previously possible. Additionally this patch makes the capacity() method public (again).
2021-05-30Revert "AK: Fix accidentally-quadratic behavior in StringBuilder"Ben Wiederhake
This reverts commit 2d011961c94ac81700c366537f52208a4c55db92.
2021-05-30AK: Fix accidentally-quadratic behavior in StringBuilderBen Wiederhake
Found by OSS Fuzz: #34451 (old bug) Related commit: 3908a49661a00e15621748dcb2b0424f29433571
2021-05-27AK: Convince GCC that m_outline_capacity isn't being readGunnar Beutner
Previously GCC came to the conclusion that we were reading m_outline_capacity via ByteBuffer(ByteBuffer const&) -> grow() -> capacity() even though that could never be the case because m_size is 0 at that point which means we have an inline buffer and capacity() would return inline_capacity in that case without reading m_outline_capacity. This makes GCC inline parts of the grow() function into the ByteBuffer copy constructor which seems sufficient for GCC to realize that m_outline_capacity isn't actually being read.
2021-05-27AK: Explicitly initialize buffer member in ByteBufferAndrew Kaster
When compiling the Kernel with Og, the compiler complains that m_outline_capacity might be uninitialized when calling capacity() Note that this fix is not really what we want. Ideally only outline buffer and outline capacity would need initialized, not the entire inline buffer. However, clang considers the class to not be default-constructible if we make that change, while gcc accepts it.
2021-05-16AK: Don't call memcpy with null argument in ByteBuffer::copy()Andrew Kaster
This was happening in TestBase64.test_decode, while copying an empty string.
2021-05-16AK+Userland: Remove nullability feature for the ByteBuffer typeGunnar Beutner
Nobody seems to use this particular feature, in fact there were some bugs which were uncovered by removing operator bool.
2021-05-16AK: Turn ByteBuffer into a value typeGunnar Beutner
Previously ByteBuffer would internally hold a RefPtr to the byte buffer and would behave like a reference type, i.e. copying a ByteBuffer would not create a duplicate byte buffer, but rather two objects which refer to the same internal buffer. This also changes ByteBuffer so that it has some internal capacity much like the Vector<T> type. Unlike Vector<T> however a byte buffer's data may be uninitialized. With this commit ByteBuffer makes use of the kmalloc_good_size() API to pick an optimal allocation size for its internal buffer.
2021-05-14AK: Avoid allocations in ByteBufferGunnar Beutner
Creating a ByteBuffer involves two allocations: -One for the ByteBufferImpl object -Another one for the actual byte buffer This changes the ByteBuffer and ByteBufferImpl classes so only one allocation is necessary.
2021-05-14AK: Avoid passing nullptr to __buitin_memcpy() in ByteBuffer::grow()Ali Mohammad Pur
2021-04-23AK: Rename adopt() to adopt_ref()Andreas Kling
This makes it more symmetrical with adopt_own() (which is used to create a NonnullOwnPtr from the result of a naked new.)
2021-04-22Everything: Move to SPDX license identifiers in all files.Brian Gianforcaro
SPDX License Identifiers are a more compact / standardized way of representing file license information. See: https://spdx.dev/resources/use/#identifiers This was done with the `ambr` search and replace tool. ambr --no-parent-ignore --key-from-file --rep-from-file key.txt rep.txt *
2021-04-21AK: Decorate most of ByteBuffer with [[nodiscard]]Andreas Kling
2021-03-15AK: Make ByteBuffer::slice(0, size()) a freebieAndreas Kling
If you want the whole buffer, we can just give you the buffer itself.
2021-03-12Everywhere: Remove klog(), dbg() and purge all LogStream usage :^)Andreas Kling
Good-bye LogStream. Long live AK::Format!
2021-02-23Everywhere: Rename ASSERT => VERIFYAndreas Kling
(...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-21AK: Add safe memset() wrapper to ByteBufferBrian Gianforcaro
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-08Everywhere: Remove unnecessary headers 3/4Ben Wiederhake
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-01-12AK: Simplify constructors and conversions from nullptr_tLenny Maiorani
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-02AK: Remove redundant compare() functions.asynts
2020-12-30AK: Add a ByteBuffer::copy(ReadonlyBytes) overloadAnotherTest
2020-12-19AK: Remove awkward ByteBuffer construction modes (wrap & adopt)Andreas Kling
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-14Kernel: Generate a coredump file when a process crashesItamar
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-08-30AK: Fix ByteBuffer zero bytes allocationsTom
2020-08-22AK: Prevent confusing silent misuse of ByteBufferBen Wiederhake
Thankfully, this hasn't happened in any other code yet, but it happened while I was trying something out. Using '==' on two ByteBuffers to check whether they're equal seemed straight-forward, so I ran into the trap.
2020-08-15AK: Rename span() to bytes() when appropriate.asynts
I originally defined the bytes() method for the String class, because it made it obvious that it's a span of bytes instead of span of characters. This commit makes this more consistent by defining a bytes() method when the type of the span is known to be u8. Additionaly, the cast operator to Bytes is overloaded for ByteBuffer and such.