Commit graph

744 commits

Author SHA1 Message Date
joshua stein ac25438d54 Build: clean up build system, use one shared Makefile
Allow everything to be built from the top level directory with just
'make', cleaned with 'make clean', and installed with 'make
install'.  Also support these in any particular subdirectory.

Specifying 'make VERBOSE=1' will print each ld/g++/etc. command as
it runs.

Kernel and early host tools (IPCCompiler, etc.) are built as
object.host.o so that they don't conflict with other things built
with the cross-compiler.
2019-12-20 20:20:54 +01:00
Hüseyin ASLITÜRK 86c6fec890 AK: Add Vector::find_first_index(const T&) 2019-12-19 10:34:33 +01:00
Andreas Kling e3c0e75055 AK: Add String::equals_ignoring_case(StringView) 2019-12-18 12:43:53 +01:00
Andreas Kling fe874bc455 JsonValue: Fix wrong return type of as_u32() and friends 2019-12-12 21:17:26 +01:00
Andreas Kling 9641f74310 AK: Teach URL::complete_url() how to resolve URL's starting with "/" 2019-12-10 21:13:00 +01:00
Andreas Kling 7248c34e35 AK: SinglyLinkedList::size_slow() should return size_t 2019-12-09 17:51:21 +01:00
Andreas Kling 6f4c380d95 AK: Use size_t for the length of strings
Using int was a mistake. This patch changes String, StringImpl,
StringView and StringBuilder to use size_t instead of int for lengths.
Obviously a lot of code needs to change as a result of this.
2019-12-09 17:51:21 +01:00
Andreas Kling 1726c17d0d AK: Handle LogStream operator<<(size_t)
This has been an annoyingly missing feature for some time.
2019-12-09 17:51:21 +01:00
William McPherson aa8b40dce6 Shell: Cache PATH for faster tab completion
This patch reduces the O(n) tab completion to something like O(log(n)).
The cache is just a sorted vector of strings and we binary search it to
get a string matching our input, and then check the surrounding strings
to see if we need to remove any characters. Also we no longer stat each
file every time.

Also added an #include in BinarySearch since it was using size_t. Oops.

If `export` is called, we recache. Need to implement the `hash` builtin
for when an executable has been added to a directory in PATH.
2019-12-05 17:09:22 +01:00
Sergey Bugaev cf7910fc1e AK: Implement %n printf specifier
This is a special specifier that does not output anything to the stream,
but saves the number of already output chars to the provided pointer.

This is apparently used by GNU Nano.
2019-12-05 12:29:11 +01:00
Andreas Kling 8f80879676 AK: StringView::lines() should keep empty lines 2019-12-02 20:41:15 +01:00
Andreas Kling 61f298faf3 AK: Add DoublyLinkedList::prepend()
Also make it possible to remove() with a value-type Iterator.
2019-12-02 18:36:10 +01:00
William McPherson d7177212fd AK: Add a BinarySearch template implementation
binary_search takes a haystack, a size, a needle and a compare function.
The compare function should return negative if a < b, positive if a > b
and 0 if a == b. The "sane default" compare function is integral_compare
which implements this with subtraction a - b.
binary_search returns a pointer to a matching element, NOT necessarily
the FIRST matching element. It returns a nullptr if the element was not
found.

This patch includes tests for binary_search.
2019-12-02 15:30:45 +01:00
Tommy Nguyen 2eb5793d55 LibMarkdown: Handle CRLF line endings
Previously, MDDocument only split on Unix-style line endings. This adds
a new function to StringView which handles LF, CR and CRLF.
2019-12-02 13:52:42 +01:00
Andreas Kling 7aaea085de AK: Allow BufferStream to serialize/deserialize floats 2019-12-02 09:19:56 +01:00
Andreas Kling f75a6b9daa Kernel: Demangle kernel C++ symbols correctly again
I broke this while implementing module linking. Also move the actual
demangling work to AK, in AK::demangle(const char*)
2019-11-29 14:59:15 +01:00
Andreas Kling a91c17c0eb AK: Add a query string component to URL
It's missing query string parsing from new URLs, but you can set the
query string programmatically, and it will be part of the URL when
serialized through to_string().
2019-11-25 21:21:27 +01:00
Andreas Kling 0405ab91aa LibHTML+AK: Move URL completion from Document to AK::URL
Completing a relative URL based on a base URL seems like generally
useful functionality.
2019-11-19 17:46:36 +01:00
Andreas Kling 39e2b69153 AK: Atomic.h needs <stddef.h> for ptrdiff_t 2019-11-16 12:18:25 +01:00
Andreas Kling e89cdd504c AK: Fix leak in WeakPtr(WeakPtr&&) and WeakPtr::operator=(WeakPtr&&)
We were forgetting to adopt the WeakLink, causing a reference leak.
This ended up costing us one allocation per exec(), with this stack:

    kmalloc_impl()
    Inode::set_vmo()
    InodeVMObject::create_with_inode()
    Process::do_exec()
    Process::exec()
    Process::sys$execve()

This was a pain to track down, in the end I caught it by dumping out
every live kmalloc pointer between runs and diffing the sets. Then it
was just a matter of matching the pointer to a call stack and looking
at what went wrong. :^)
2019-11-15 16:07:39 +01:00
Andreas Kling d016d5e365 HackStudio: Start fleshing out the GUI for a GUI designer :^)
I'll be reconstructing parts of the VisualBuilder application here and
then we can retire VisualBuilder entirely once all the functionality
is available in HackStudio.
2019-11-09 00:41:00 +01:00
Andreas Kling 870be9d71e AK: Add Vector::take(index)
This removes an item from the vector and returns it.
2019-11-07 20:38:33 +01:00
Andreas Kling 68e23bca3f AK: Delete operator!() and operator bool() from the Nonnull pointers
Since NonnullRefPtr and NonnullOwnPtr cannot be null, it is pointless
to convert them to a bool, since it would always be true.

This patch makes it an error to null-check one of these pointers.
2019-11-07 18:00:05 +01:00
Andreas Kling 6a8695e759 AK: Add Vector::prepend(T&&) 2019-11-07 10:35:13 +01:00
Andreas Kling 9a5e065229 AK: Always rebuild unit tests if AK headers change
This is a hack to avoid failing AK unit tests because it didn't even
try to rebuild.
2019-11-06 19:14:34 +01:00
Andreas Kling e33bbdb6ba AK: Remove unused AK::not_implemented()
Whatever this was supposed to be, it was ironically... not implemented.
2019-11-06 13:58:08 +01:00
Andreas Kling 49635e62fa LibELF: Move AK/ELF/ into Libraries/LibELF/
Let's arrange things like this instead. It didn't feel right for all of
the ELF handling code to live in AK.
2019-11-06 13:42:38 +01:00
Andreas Kling 2ad0ec325a AK: Get rid of TStyle (output styling helper for LogStream)
This didn't end up getting used, so let's get rid of it.
2019-11-06 11:37:03 +01:00
Andreas Kling 4623811ec3 AK: Let's just log unimplemented printf() format strings
It's too dang frustrating that we actually crash whenever we hit some
unimplemented printf specifier. Let's just log the whole format string
and carry on as best we can.
2019-11-04 20:48:30 +01:00
Andreas Kling 8c45891c80 AK: Allow overriding the Queue segment size with a template parameter 2019-11-03 12:33:51 +01:00
Andreas Kling 78a744da77 AK: Add Queue::head()
This returns a const T& for the first element in the queue, without
dequeuing it.
2019-11-03 12:09:19 +01:00
Andreas Kling a1ea3754a3 AK: Handle '%llu' in printf() (unsigned 64-bit integer)
I got a warning when using '%Q' since that's non-standard. This patch
makes our printf family accept '%llu'.
2019-11-02 10:35:08 +01:00
Andreas Kling 014f8ca8c4 AK: Allow JsonValue to store 64-bit integers internally
Add dedicated internal types for Int64 and UnsignedInt64. This makes it
a bit more straightforward to work with 64-bit numbers (instead of just
implicitly storing them as doubles.)
2019-10-29 16:36:50 +01:00
Andreas Kling 01c6088789 AK: Add String::contains(String)
This is just a wrapper around strstr() for now. There are many better
ways to search for a string within a string, but I'm just adding a nice
API at the moment. :^)
2019-10-28 19:08:48 +01:00
Andreas Kling 93dff5df34 AK: Add JsonArray::ensure_capacity()
This is helpful for anyone who knows up-front how many items are gonna
be appended to the JsonArray.
2019-10-23 14:55:21 +02:00
Andreas Kling 0cea80218d AK: Make it possible to store complex types in a CircularQueue
Previously we would not run destructors for items in a CircularQueue,
which would lead to memory leaks.

This patch fixes that, and also adds a basic unit test for the class.
2019-10-23 12:27:43 +02:00
Andreas Kling 835496375f URL: https:// URLs should default to port 443 2019-10-21 17:19:17 +02:00
Andreas Kling 9c434d8c6a JsonObject: Add JsonObject::has(key) 2019-10-21 14:47:18 +02:00
Andreas Kling b74a433809 URL: Unbreak the serialization test
http:// URLs no longer include ":80" when serialized, since port 80 is
implied by the protocol. Non-standard ports are still serialized.
2019-10-21 14:46:36 +02:00
Andrew Kaster 138abb9098 ELF: Fail layout when program header hooks return nullptr (#673)
ELFLoader::layout() had a "failed" variable that was never set. This
patch checks the return value of each hook (alloc/map section and tls)
and fails the load if they return null.

I also needed to patch Process so that the alloc_section_hook and 
map_section_hook actually return nullptr when allocating a region fails.

Fixes #664 :)
2019-10-20 16:24:42 +02:00
Drew Stratford 67041f3a8c AK: Add CircularDeque.
This class inherits from CircularQueue and adds the ability dequeue
from the end of the queue using dequeue_end().

Note that I had to make some of CircularQueue's fields protected to
properly implement dequeue_end.
2019-10-20 10:51:12 +02:00
Andreas Kling 96f9e6a64f String: Define operator>(String) 2019-10-19 20:54:47 +02:00
Andreas Kling f4e6dae6fe UTF-8: Add Utf8CodepointIterator::codepoint_length_in_bytes()
This allows you to retrieve the length (in bytes) of the codepoint the
iterator is currently pointing at.
2019-10-18 22:49:23 +02:00
Andreas Kling ab9e6166e8 AK: Add String::hash() 2019-10-18 17:17:47 +02:00
Andreas Kling f1f928670e URL: Parse URLs that lack a path (e.g "http://serenityos.org") 2019-10-17 23:39:31 +02:00
Tom b0773a8ea6 AK: Add Atomic.h
Use gcc built-in atomics
2019-10-12 19:30:59 +02:00
Andreas Kling bf5a65d934 URL: No need to include ":80" when serializing http:// URLs 2019-10-10 22:06:25 +02:00
Andreas Kling a7f538fb63 AK: Make String compile on platforms where size_t==u32
This kind of thing is a bit annoying. On Serenity, size_t is the same
size as u32, but not the same type. Because of "long" or whatever.
This patch makes String not complain about duplicate overloads.
2019-10-07 10:56:44 +02:00
Andreas Kling d64c054d25 AK: URL should support file:// URL's
Also add some setters since this class was very setter-less.
2019-10-05 10:14:42 +02:00
Andreas Kling 9e7560fae9 AK: Make Bitmap constructors public to allow make<Bitmap>()
I don't love this, but I also don't love the Bitmap class in general.
2019-10-01 19:58:07 +02:00
Andreas Kling 4bfd4dc6c7 AK: Remove empty files JsonArray.cpp and JsonObject.cpp 2019-10-01 11:24:54 +02:00
Andreas Kling 8f45a259fc ByteBuffer: Remove pointer() in favor of data()
We had two ways to get the data inside a ByteBuffer. That was silly.
2019-09-30 08:57:01 +02:00
Andreas Kling 99fc7033b5 AK: Add StringBuilder::length() and trim(int)
Sometimes you want to trim a byte or two off the end.
2019-09-29 16:20:09 +02:00
Sergey Bugaev 08d9883306 AK: Add StringBuilder::string_view() and StringBuilder::clear()
The former allows you to inspect the string while it's being built.
It's an explicit method rather than `operator StringView()` because
you must remember you can only look at it in between modifications;
appending to the StringBuilder invalidates the StringView.

The latter lets you clear the state of a StringBuilder explicitly, to
start from an empty string again.
2019-09-28 18:29:42 +02:00
Sergey Bugaev 127d168def AK: Add a keep_empty argument to String[View]::substring{_view} 2019-09-28 18:29:42 +02:00
Andreas Kling 7f9a33dba1 Kernel: Make Region single-owner instead of ref-counted
This simplifies the ownership model and makes Region easier to reason
about. Userspace Regions are now primarily kept by Process::m_regions.

Kernel Regions are kept in various OwnPtr<Regions>'s.

Regions now only ever get unmapped when they are destroyed.
2019-09-27 14:25:42 +02:00
Andreas Kling 9c3c117f05 ELF: Make code a little more buildable on other platforms
Patch from Anonymous.
2019-09-27 13:30:23 +02:00
Andreas Kling 0c72e0c09f AK: Add AK_MAKE_NONMOVABLE 2019-09-22 00:17:29 +02:00
Andreas Kling 1c692e87a6 Kernel: Move kmalloc() into a Kernel/Heap/ directory 2019-09-16 09:01:44 +02:00
Andreas Kling 9f99e285d1 Utf8View: Don't print potentially unterminated string in debug message 2019-09-15 15:46:40 +02:00
Andreas Kling 09e89cc55d Revert "AK: Made Strings reversible"
This reverts commit 26e81ad574.

We forgot to consider UTF-8 here. String is UTF-8 and we need to be
careful about things like this.
2019-09-13 14:37:25 +02:00
Jesse Buhagiar 26e81ad574 AK: Made Strings reversible
`AK::String` can now be reversed via AK::String::reverse(). This makes
life a lot easier for functions like `itoa()`, where the output
ends up being backwards. Very much not like the normal STL
(which requires an `std::reverse` object) way of doing things.

A call to reverse returns a new `AK::String` so as to not upset any
of the possible references to the same `StringImpl` shared between
Strings.
2019-09-13 13:54:07 +02:00
MinusGix a574e1ab84 TestStringView: Add test for starts_with 2019-09-13 09:22:30 +02:00
MinusGix 05f641a5a9 StringView: Add starts_with method 2019-09-13 09:22:30 +02:00
Andreas Kling 81caf95136 printf: %w, %b, and %p should be zero-padded but not left-padded
This fixes the goofy issue with %p coming out as "     0x123" instead
of "0x00000123".
2019-09-11 20:08:11 +02:00
Andreas Kling 1427c20f6a AK: Add LogStream operator<< for ByteBuffer 2019-09-11 19:35:14 +02:00
Andreas Kling 5cd04a6ad8 AK: Add String::number(size_t) overload 2019-09-11 18:58:33 +02:00
Sergey Bugaev 1fb6a7d893 AK: Fix buffer overrun in Utf8CodepointIterator::operator++
The old implementation tried to move forward as long as the current
byte looks like a UTF-8 character continuation byte (has its two
most significant bits set to 10). This is correct as long as we assume
the string is actually valid UTF-8, which we do (we also have a separate
method that can check whether it is the case).

We can't, however, assume that the data after the end of our string
is also valid UTF-8 (in fact, we're not even allowed to look at data
outside out string, but it happens to a valid memory region most of
the time). If the byte after the end of our string also has its most
significant bits set to 10, we would move one byte forward, and then
fail the m_length > 0 assertion.

One way to fix this would be to add a length check inside the loop
condition. The other one, implemented in this commit, is to reimplement
the whole function in terms of decode_first_byte(), which gives us
the length as encoded in the first byte. This also brings it more
in line with the other functions around it that do UTF-8 decoding.
2019-09-08 17:45:10 +02:00
Conrad Pankoff 9273589c5e AK: Pad %b and %w to two and four places in printf 2019-09-08 08:49:29 +02:00
Andreas Kling ec6bceaa08 Kernel: Support thread-local storage
This patch adds support for TLS according to the x86 System V ABI.
Each thread gets a thread-specific memory region, and the GS segment
register always points _to a pointer_ to the thread-specific memory.

In other words, to access thread-local variables, userspace programs
start by dereferencing the pointer at [gs:0].

The Process keeps a master copy of the TLS segment that new threads
should use, and when a new thread is created, they get a copy of it.
It's basically whatever the PT_TLS program header in the ELF says.
2019-09-07 15:55:36 +02:00
Andreas Kling bcfdf9ffa7 AK: Add a useful align_up_to(value, power_of_two) function 2019-09-07 15:39:26 +02:00
Andreas Kling d10ca2b010 AK: When printf assert on unsupported specifier, specify which one!
We were asserting without saying why. That's a bit unhelpful. :^)
2019-09-06 20:27:58 +02:00
Andreas Kling 73fdbba59c AK: Rename <AK/AKString.h> to <AK/String.h>
This was a workaround to be able to build on case-insensitive file
systems where it might get confused about <string.h> vs <String.h>.

Let's just not support building that way, so String.h can have an
objectively nicer name. :^)
2019-09-06 15:36:54 +02:00
Andreas Kling fb39e46d3d Utf8View: Try fixing the travis-ci build
There's some overload ambiguity when doing Utf8View("literal")
2019-09-05 19:06:39 +02:00
Sergey Bugaev 55197ed4ef AK: Log UTF-8 validation errors 2019-09-05 16:37:39 +02:00
Sergey Bugaev c379f43d2a AK: Add some more utility methods to Utf8View 2019-09-05 16:37:39 +02:00
Andreas Kling cc98ea1956 Json: Add serializer fast-path for string values
Passing these through the generic JsonValue path was causing us to
instantiate temporary JsonValues that incurred a heap allocation.
This avoids that by adding specialized overloads for string types.
2019-09-04 14:40:11 +02:00
Conrad Pankoff 87808e535a AK: Fix printf %x padding and %p length 2019-09-03 15:23:13 +02:00
Conrad Pankoff 40daefd3dc AK: Abort on unknown printf formatting characters
Right now if we encounter an unknown character, printf (and its related
functions) fail in a really bad way, where they forget to pull things off
the stack. This usually leads to a crash somewhere else, which is hard to
debug.

This patch makes printf abort as soon as it encounters a formatting
character that it can't handle. This is not the optimal solution, but it
is an improvement for debugging.
2019-09-02 08:29:21 +02:00
Conrad Pankoff a92939b766 AK: Support %i as an alias for %d in printf 2019-09-02 08:28:31 +02:00
Conrad Pankoff 4a4e66b2d0 Kernel/AK: Add is_zero helpers for IP and MAC addresses 2019-08-29 06:25:06 +02:00
Sergey Bugaev 5d3696174b AK: Add a Utf8View type for iterating over UTF-8 codepoints
Utf8View wraps a StringView and implements begin() and end() that
return a Utf8CodepointIterator, which parses UTF-8-encoded Unicode
codepoints and returns them as 32-bit integers.

This is the first step towards supporting emojis in Serenity ^)
https://github.com/SerenityOS/serenity/issues/490
2019-08-28 13:46:02 +02:00
Conrad Pankoff 970e0147f7 AK: Make printf %x actually work properly
When printing hex numbers, we were printing the wrong thing sometimes. This
was because we were dividing the digit to print by 15 instead of 16. Also,
dividing by 16 is the same as shifting four bits to the right, which is a
bit closer to our actual intention in this case, so let's use a shift
instead.
2019-08-28 06:43:04 +02:00
Sergey Bugaev 216b7b3b80 JSON: Port JsonArray and JsonObject serialization to serializers
This way, primitive JsonValue serialization is still handled by
JsonValue::serialize(), but JsonArray and JsonObject serialization
always goes through serializer classes. This is no less efficient
if you have the whole JSON in memory already.
2019-08-27 14:56:09 +02:00
Sergey Bugaev 56f5c14d86 JSON: Add JSON serializers
These are two new types that allow serializing JSON on-the-fly
as it's generated, without building the whole JSON in memory
first.
2019-08-27 14:56:09 +02:00
Andreas Kling e29fd3cd20 Kernel: Display virtual addresses as V%p instead of L%x
The L was a leftover from when these were called linear addresses.
2019-08-26 11:31:58 +02:00
Andreas Kling 8f50d75184 AK: Make HashTable.h compile inside the SDL2 port 2019-08-25 17:47:48 +02:00
Andreas Kling d38bd3935b AK: Add StringView::hash()
This grabs the hash from the underlying StringImpl if there is one,
otherwise it's computed on the fly.
2019-08-25 06:45:31 +02:00
Andreas Kling bb32fd8bfa AK: Add HashMap::find() with customizable finder callback
This will allow clients to search the map without having to instantiate
a key value.
2019-08-25 06:45:27 +02:00
Andreas Kling cd8278e489 AK: Add String::operator==(StringView)
Comparing a String to a StringView would instantiate a temporary
String just for the comparison. Let's not do that. :^)
2019-08-25 06:45:19 +02:00
Andreas Kling a00419ed77 AK: Optional::operator bool() should consume the Optional
We use consumable annotations to catch bugs where you get the .value()
of an Optional before verifying that it's okay.

The bug here was that only has_value() would set the consumed state,
even though operator bool() does the same job.
2019-08-25 06:45:09 +02:00
Andreas Kling 56eaf9b033 AK: Make FileSystemPath better at handling relative paths
Relative paths now canonicalize into a string starting with "./"
Previously, "foo" would be canonicalized as "/foo" which was clearly
not right.
2019-08-23 19:58:16 +02:00
Andreas Kling a5cdd0afa5 NonnullPtrVector: Add ptr_at() getters for accessing the NonnullPtr
Normally you want to access the T&, but sometimes you need to grab at
the NonnullPtr, for example when moving it out of the Vector before
a call to remove(). This is generally a weird pattern, but I don't have
a better solution at the moment.
2019-08-19 19:04:52 +02:00
Andreas Kling 5465795dc3 AK: The printf family was mixing up case and alternate form settings 2019-08-18 16:14:21 +02:00
Andreas Kling e3f3c980bf IntrusiveList: Make Iterator::operator* return a T&
This makes iteration a little more pleasant :^)
2019-08-17 11:25:32 +02:00
Andreas Kling 5122caf9a8 LogStream: Prefix userspace dbg() output with "ProcessName(PID): "
Using the new get_process_name() syscall, we can automatically prefix
all userspace debug logging.

Hopefully this is more helpful than annoying. We'll find out! :^)
2019-08-15 20:55:45 +02:00
Andreas Kling d64e698bf1 AK: Add a simple TriState type
enum class TriState : u8 { False, True, Unknown };
2019-08-15 20:53:12 +02:00
Andreas Kling 2349dc1a21 StringView: Add StringView::operator==(StringView)
Previously we'd implicitly convert the second StringView to a String
when comparing two StringViews, which is obviously not what we wanted.
2019-08-15 14:09:27 +02:00
Andreas Kling eaa9cf58f5 AK: Use int_hash() to generate less idiotic hashes for {Nonnull,}OwnPtr 2019-08-14 21:21:15 +02:00
Andreas Kling 1ac963b5c8 JsonParser: "" is an empty string, not a null value 2019-08-14 15:01:42 +02:00
Andreas Kling fdcff7d15e AK: Make it possible to use HashMap<K, NonnullOwnPtr>::get()
Add the concept of a PeekType to Traits<T>. This is the type we'll
return (wrapped in an Optional) from HashMap::get().

The PeekType for OwnPtr<T> and NonnullOwnPtr<T> is const T*,
which means that HashMap::get() will return an Optional<const T*> for
maps-of-those.
2019-08-14 11:47:38 +02:00
Andreas Kling f75b1127b2 OwnPtr: Add a way to turn an OwnPtr into a NonnullOwnPtr
Okay, so, OwnPtr<T>::release_nonnull() returns a NonnullOwnPtr<T>.
It assumes that the OwnPtr is non-null to begin with.

Note that this removes the value from the OwnPtr, as there can only be
a single owner.
2019-08-14 11:04:19 +02:00
Conrad Pankoff 06743932b8 AK: Support width/alt/caps/padding modifiers for %x in printf 2019-08-13 18:15:58 +02:00
Andreas Kling 2c7b0b8893 Vector: Use memcpy to implement remove() for trivial types
This is a lot faster than the generic code path.
Also added some unit testing for this.
2019-08-12 11:07:31 +02:00
Andreas Kling fb636389d6 URL: Add some convenience constructors 2019-08-10 19:31:37 +02:00
Andreas Kling ed43770b2f AK: Add a basic URL class to help us handle URL's
We're gonna need these as we start to write more networking programs.
2019-08-10 17:30:35 +02:00
Sergey Bugaev 79f867238a printf: Support dynamic fill widths
The printf formatting mini-language actually allows you
to pass a '*' character in place of the fill width specification,
in which case it eats one of the passed in arguments and uses it
as width, so implement that.
2019-08-10 08:46:22 +02:00
Andreas Kling 865a1b913c AK: Add Optional<T>(const U&)
This replaces Optional<T>(U&&) which clang-tidy complained may hide the
regular copy and move constructors. That's a good point, clang-tidy,
and I appreciate you pointing that out!
2019-08-08 18:34:59 +02:00
Andreas Kling bb9909548b AK: Add an iterator class for InlineLinkedList
This makes it possible to iterate over these with range-for. :^)
2019-08-08 13:39:40 +02:00
Andreas Kling 9889d170b9 JsonValue: Add as_string_or(String)
Return the contained string if the value *is* a string, otherwise it
returns the alternative string passed in the parameter.
2019-08-07 22:03:25 +02:00
Andreas Kling f6998b1817 JSON: Templatize the JSON serialization code
This makes it possible to use something other than a StringBuilder for
serialization (and to produce something other than a String.) :^)
2019-08-07 21:29:32 +02:00
Andreas Kling 43ec733b61 AK: Add a basic unit test for FileSystemPath
Just to make sure that things are on the up-and-up.
2019-08-07 21:07:02 +02:00
Andreas Kling 2e416b1b87 Vector: Add a test for growing a Vector beyond its inline capacity 2019-08-07 20:43:13 +02:00
Andreas Kling 5096eaa845 AK: Add a FixedArray<T> container
This is a simple array wrapper that knows its size. It has begin/end
so you can use range-for. It also has a resize() that reallocates.
2019-08-07 20:05:36 +02:00
Andreas Kling 5487f81b5d Vector: Use memcpy when dynamically growing Vectors of trivial types 2019-08-07 15:35:23 +02:00
Andreas Kling 6da6ca64d2 Vector: Use TypedTransfer in more parts of Vector
Make more Vector-of-trivial-type operations go fast :^)
2019-08-07 15:25:34 +02:00
Andreas Kling e8e85f5457 Vector: Use memcmp for comparing two vectors with trivial elements 2019-08-07 15:05:10 +02:00
Andreas Kling 6d97caf124 JsonParser: Scan ahead to find the first special char in quoted strings
This allows us to take advantage of the now-optimized (to do memmove())
Vector::append(const T*, int count) for collecting these strings.

This is a ~15% speedup on the load_4chan_catalog benchmark.
2019-08-07 11:57:51 +02:00
Andreas Kling b48b6c0caa Vector: Use memmove() for moving trivial types around more
This can definitely be improved with better trivial type detection and
by using the TypedTransfer template in more places.

It's a bit annoying that we can't get <type_traits> in Vector.h since
it's included in the toolchain compilation before we have libstdc++.
2019-08-07 11:55:20 +02:00
Andreas Kling 60c25228ee AK: Fix -Wconsumed warnings in Optional move-ctor and move-assign
Our protocol says we have to call has_value() before release_value().
The code was already safe, but the compiler had no way of knowing that.
2019-08-07 07:17:52 +02:00
Andreas Kling 2abfab2892 ELFLoader: Remove an uninteresting debug log message
This is showing up at the boundary between kernel and userspace stack
frames in backtraces, and looks silly.
2019-08-06 20:49:36 +02:00
Conrad Pankoff 73c998dbfc Kernel: Refactor TCP/IP stack
This has several significant changes to the networking stack.

* Significant refactoring of the TCP state machine. Right now it's
  probably more fragile than it used to be, but handles quite a lot
  more of the handshake process.
* `TCPSocket` holds a `NetworkAdapter*`, assigned during `connect()` or
  `bind()`, whichever comes first.
* `listen()` is now virtual in `Socket` and intended to be implemented
  in its child classes
* `listen()` no longer works without `bind()` - this is a bit of a
  regression, but listening sockets didn't work at all before, so it's
  not possible to observe the regression.
* A file is exposed at `/proc/net_tcp`, which is a JSON document listing
  the current TCP sockets with a bit of metadata.
* There's an `ETHERNET_VERY_DEBUG` flag for dumping packet's content out
  to `kprintf`. It is, indeed, _very debug_.
2019-08-06 16:21:17 +02:00
Andreas Kling 151e6a1818 AK: Fix leak in Optional(Optional&&)
We were *copying* the other Optional's value and then marking it as not
having a value. This prevented us from ever destroying the original.
2019-08-05 22:27:47 +02:00
Andreas Kling 9553ecfe01 AK: Optional::operator=(Optional&&) should clear movee's has_value bit
We were forgetting to clear m_has_value in the Optional being moved
from when using operator=(Optional&&).
2019-08-05 21:47:36 +02:00
Sergey Bugaev 55d7810fab AK: Do not keep an open FD in MmappedFile
We only need an open FD while establishing the mapping,
and can close it immediately after mmap() call.
2019-08-05 16:04:31 +02:00
Andreas Kling 0d0230ab10 JsonParser: Fold extract_while() into parse_number()
It wasn't unsed anywhere else anyway, and this is actually ~1% faster
on the load_4chan_catalog benchmark.
2019-08-04 20:23:46 +02:00
Andreas Kling a846ee76ea AK: Add VectorIterator::index()
A simple helper function that extracts the index of an iterator.
Note that the index is not valid if the iterator is end().
2019-08-04 20:18:18 +02:00
Andreas Kling 2a89bb2ac4 Vector: Add find() and some iterator improvements
Vector now has find() just like HashTable. I also made the iterator
comparison functions const-correct.
2019-08-04 19:21:08 +02:00
Andreas Kling 539985f4fe HashTable: Use the Bucket type in some places over SinglyLinkedList<T>
This is just for consistency, and we might want to switch to another
bucket type some day.
2019-08-04 19:20:20 +02:00
Andreas Kling 72b69b82bb JsonParser: Oops, fix build. 2019-08-04 18:59:06 +02:00
Andreas Kling 4e004a664f JsonParser: Cache the last seen string starting with each possible char
Keep a 256-entry string cache during parse to avoid creating some new
strings when possible. This cache is far from perfect but very cheap.
Since none of the strings are transient, this only costs us a couple of
pointers and a bit of ref-count manipulation.

The cache hit rate on 4chan_catalog.json is ~33% and the speedup on
the load_4chan_catalog benchmark is ~7%.
2019-08-04 18:41:24 +02:00
Andreas Kling 3eb1a7f8f8 AK: Add a benchmark for parsing 4chan catalog JSON
I was able to get parsing time down to about 1/3 of the original time
by using callgrind+kcachegrind. There's definitely more improvements
that can be made here, but I'm gonna be happy with this for now. :^)
2019-08-04 11:57:32 +02:00
Andreas Kling b62a12c687 JsonParser: Some minor optimizations
- Return more specific types from parse_array() and parse_object().
- Don't create a throwaway String in extract_while().
- Use a StringView in parse_number() to avoid a throwaway String.
2019-08-04 11:47:21 +02:00
Andreas Kling 93596dc00d JsonObject: Add move constructors for JsonObject(Json{Array,Object}&&)
This also helps avoid JsonValue copying during parse.
2019-08-04 11:46:31 +02:00
Andreas Kling 82826104e0 Json: Add efficient copy and move constructors for Json{Array,Object}
This helps avoid copying JsonValues during parsing.
2019-08-04 11:45:16 +02:00
Andreas Kling cce2ea9bb0 AK: Add StringView::to_int()
This is a shameless copy-paste of String::to_int(). We should find some
way to share this code between String and StringView instead of having
two duplicate copies like this.
2019-08-04 11:44:20 +02:00
Andreas Kling c55129e573 JsonParser: Use Vector<char, 1024> instead of StringBuilder in parsing
This is a 10-12% speedup on the 4chan thread catalog JSON.
2019-08-04 10:38:15 +02:00
Andreas Kling 210550d4b3 JsonArray: Expose the value vector to the outside for convenience
Sometimes it's easier to just work with a const Vector<JsonValue>&,
so give clients the option of doing that.
2019-08-04 10:05:02 +02:00
Andreas Kling 6797f71e73 JsonParser: When encountering \uXXXX, just emit a "?" for now. 2019-08-04 08:58:45 +02:00
Andreas Kling 18aeda2e0d BufferStream: It's not an error to read up to the very end of a buffer
Don't signal an error before we've reached the very end of the buffer.
2019-08-03 19:20:05 +02:00
Andreas Kling 6e956f2a0b BufferStream: Add operator<<(const char*) overload
Ext2FS was relying on the presence of a (StringView) overload here and
when I added a (bool) overload, it started choosing that for literals.
2019-08-03 19:02:48 +02:00
Andreas Kling b04dae7faa BufferStream: Support "bool" as a streaming type 2019-08-03 17:02:42 +02:00
Andreas Kling c5903ec4bb AK: Remove two redundant RefPtr constructors.
We already have constructors for "const T*" and "const T&" so we don't
need to have non-const variants.
2019-08-03 14:16:05 +02:00
Andreas Kling fe25426ee4 AK: Simplify RefPtr and NonnullRefPtr's leak_ref() functions
Use AK::exchange() to switch out the internal storage. Also mark these
functions with [[nodiscard]] to provoke an compile-time error if they
are called without using the return value.
2019-08-02 12:05:09 +02:00
Andreas Kling 15866714da AK: Add anti-null assertions in RefPtr.
This gives us better error messages when dereferencing null RefPtrs.
2019-08-02 12:00:43 +02:00
Andreas Kling cbc1272810 AK: Fix ref leaks in RefPtr assignment operators.
Many of the RefPtr assignment operators would cause ref leaks when we
call them to assign a pointer that's already the one kept.
2019-08-02 11:56:55 +02:00
Andreas Kling 06f82901b7 AK: Explicitly delete NonnullRefPtr::operator=(RefPtr).
This gives us much better error messages when you try to use them.
Without this change, it would complain about the absence of functions
named ref() and deref() on RefPtr itself. With it, we instead get a
"hey, this function is deleted" error.

Change operator=(T&) to operator=T(const T&) also, to keep assigning
a const T& to a NonnullRefPtr working.
2019-08-02 11:51:28 +02:00
Andreas Kling 6db879ee66 AK: Fix ref leak in NonnullRefPtr::operator=(T&).
We would leak a ref when assigning a T& to a NonnullRefPtr that already
contains that same T.
2019-08-02 11:35:05 +02:00
Andreas Kling d9cc3e453c AK: Add assertions when dereferencing an OwnPtr.
This will make it immediately obvious what the problem is when you're
dereferencing a null OwnPtr.
2019-08-02 10:34:40 +02:00
Andreas Kling a9a1a5dfa9 AK: Add a test for iterating a HashTable during clear (should assert)
Ideally we should also verify that the assertion actually happens,
but we need some support in the TestSuite framework for that.
2019-08-02 09:25:35 +02:00
Andreas Kling 6560116b67 TestSuite: Hijack the ASSERT macros during unit tests.
Instead of aborting the program when we hit an assertion, just print a
message and keep going.

This allows us to write tests that provoke assertions on purpose.
2019-08-02 09:23:03 +02:00
Andreas Kling 31793b8f3a AK: Fix typo in the WeakPtr test. Behavior was actually correct.
Also remove an unused variable.
2019-08-02 09:21:42 +02:00
Andreas Kling 4e59300650 AK: Fix typo in TestVector.cpp, oops. 2019-08-02 08:59:26 +02:00
Andreas Kling 9230b42f28 AK: Use Vector::empend() a bit in the unit tests, and fix a bug.
There was a bug in the "prepend_vector_object" test but it was masked
by us not printing failures. (The bug was that we were adding three
elements to the "objects" vector and then checking that another
vector called "more_objects" indeed had three elements. Oops!)
2019-08-01 16:24:31 +02:00
Andreas Kling 8434548f14 TestSuite: Actually print failed comparions.. :^) 2019-08-01 16:22:50 +02:00
Andreas Kling eeff0cd570 AK: Don't allow constructing an OwnPtr from a const NonnullOwnPtr&
OwnPtr's must move around, they can't be copy constructed.
2019-08-01 15:46:18 +02:00
Andreas Kling 79ce75d862 AK: Add Vector::empend().
This is a complement to append() that works by constructing the new
element in-place via placement new and forwarded constructor arguments.

The STL calls this emplace_back() which looks ugly, so I'm inventing
a nice word for it instead. :^)
2019-08-01 15:35:45 +02:00
Andreas Kling a3ee35510f AK: Make Bitmap movable but not copyable.
We were falling back to an incorrect compiler-generated copy ctor for
this class, and let's not do that.

Found by PVS-Studio.
2019-08-01 11:34:36 +02:00
Andreas Kling 2923d39106 JsonParser: Merge the parsing of '\n' and '\r' in quoted strings 2019-08-01 10:45:37 +02:00
Andreas Kling f10e850644 HashTable: Assert on iteration attempt over table during clear/rehash
It doesn't seem sane to try to iterate over a HashTable while it's in
the middle of being cleared. Since this might cause strange problems,
this patch adds an assertion if an iterator is constructed during
clear() or rehash() of a HashTable.
2019-07-31 10:23:24 +02:00
Robin Burchell a82c60b22a Add Result<>, to use with/complement Error
An operation often has two pieces of underlying information:

* the data returned as a result from that operation
* an error that occurred while retrieving that data

Merely returning the data is not good enough. Result<> allows exposing
both the data, and the underlying error, and forces (via clang's
consumable attribute) you to check for the error before you try to
access the data.
2019-07-31 09:06:39 +02:00
Robin Burchell 7dd25141cd Add Error<>
Put simply, Error<> is a way of forcing error handling onto an API user.

Given a function like:

    bool might_work();

The following code might have been written previously:

    might_work(); // but what if it didn't?

The easy way to work around this is of course to [[nodiscard]] might_work.
But this doesn't work for more complex cases like, for instance, a
hypothetical read() function which might return one of _many_ errors
(typically signalled with an int, let's say).

    int might_read();

In such a case, the result is often _read_, but not properly handled. Like:

    return buffer.substr(0, might_read()); // but what if might_read returned an error?

This is where Error<> comes in:

    typedef Error<int, 0> ReadError;
    ReadError might_read();

    auto res = might_read();
    if (might_read.failed()) {
        switch (res.value()) {
        case EBADF:
            ...
        }
    }

Error<> uses clang's consumable attributes to force failed() to be
checked on an Error instance. If it's not checked, then you get smacked.
2019-07-31 09:06:39 +02:00
Robin Burchell 28362fcc57 Optional: Add consumable checks
These will, when building with clang, prevent using Optional::value
without first checking Optional::has_value() - at compile time.
2019-07-31 09:06:39 +02:00
rhin123 a175e76948 TextEditor: Include extension during SaveAs
When we save-as in the text editor we now auto-populate GFilePicker /w
the current name & extension.
2019-07-29 20:46:31 +02:00
Andreas Kling 57c29491a3 Kernel+AK: Remove AK/StdLibExtras.cpp, moving kernel stuff to Kernel/.
We had some kernel-specific gizmos in AK that should really just be in the
Kernel subdirectory instead. The only thing remaining after moving those
was mmx_memcpy() which I moved to the ARCH(i386)-specific section of
LibC/string.cpp.
2019-07-29 11:58:44 +02:00
Andreas Kling 66db6f4f92 AK: Add Queue::clear() to clear out a Queue. 2019-07-28 21:34:47 +02:00
Andreas Kling 6f397e23f1 ByteBuffer: Add slice_view(). Works like slice() but makes a wrapper only.
So we already have ByteBuffer::wrap() which is like a StringView for random
data. This might not be the best abstraction actually, but this will be
immediately useful so let's add it.
2019-07-27 15:26:51 +02:00
Andreas Kling 2f373a27a2 AK: Add RELEASE_ASSERT in non-Serenity builds. 2019-07-26 22:15:54 +02:00
Andreas Kling fc9a1a1328 AK: Fix NonnullRefPtr<T>::operator=(NonnullRefPtr<U>). 2019-07-26 08:04:33 +02:00
Andreas Kling 6ae95945aa AK: Add ScopedValueRollback::set_override_rollback_value().
This can be used if you change your mind about what value we should roll
back to. :^)
2019-07-25 15:21:16 +02:00
Andreas Kling 9fb2a65716 AK: Rename ValueRestorer => ScopedValueRollback.
Qt had a pretty good name for this concept, so let's steal it. :^)
2019-07-25 15:15:46 +02:00
Andreas Kling 1bd3fca585 AK: Shim open_with_path_length() for non-Serenity builds.
This is pretty ugly but I don't want to *not* use open_with_path_length()
so let's just shim it.
2019-07-25 14:25:45 +02:00
Andreas Kling 7c8debfe46 AK: Don't compile mmx_memcpy() outside of ARCH(I386). 2019-07-25 14:25:33 +02:00
Andreas Kling afc638c3ab AK: Allow NonnullRefPtr::ptr() when in "unknown" typestate.
Clang loses the typestate when passing NonnullRefPtr's via lambda captures.
This is unfortunate, but not much we can do about it. Allowing ptr() makes
it possible to use captured NonnullRefPtrs as you'd expect.
2019-07-25 11:51:24 +02:00
Andreas Kling 6a5446d6dd AK: Simplify NonnullPtrVector template a bit.
Add an "ElementType" typedef to NonnullOwnPtr and NonnullRefPtr to allow
clients to easily find the pointee type. Then use this to remove a template
argument from NonnullPtrVector. :^)
2019-07-25 11:10:28 +02:00
Andreas Kling 10d120dc85 AK: Share code between NonnullOwnPtrVector and NonnullRefPtrVector.
These can just inherit from a shared base template. Thanks to Robin for the
sweet idea :^)
2019-07-25 11:00:26 +02:00
Andreas Kling 0846986cac LogStream: Preserve errno for the lifetime of a LogStream object. 2019-07-25 07:00:33 +02:00
Andreas Kling 1d0b464618 AK: Make HashMap::get(Key) return an Optional<Value>.
This allows HashMap::get() to be used for value types that cannot be default
constructed (e.g NonnullOwnPtr.)
2019-07-24 10:25:43 +02:00
Andreas Kling 1686c4906b AK: Delete Vector::resize() from Nonnull{Own,Ref}PtrVector.
It's not possible to grow one of these vectors beyond what's already in them
since it's not possible to default-construct Nonnull{Own,Ref}Ptr.

Add Vector::shrink() which can be used when you want to shrink the Vector
and delete resize() from the specialized Vectors.
2019-07-24 09:33:26 +02:00
Andreas Kling a635e62e6a AK: Add NonnullOwnPtrVector.
This works just like NonnullRefPtr, except for NonnullOwnPtr's instead.
NonnullOwnPtrVector<T> inherits from Vector<NonnullOwnPtr<T>>, and adds some
comforts on top, like making accessors return T& so we can chase dots (.)
instead of arrows (->) :^)
2019-07-24 08:49:44 +02:00
Andreas Kling 28da5b002f AK: Add NonnullOwnPtr.
This is just like OwnPtr (also single-owner), except it cannot be null.

NonnullOwnPtr is perfect as the return type of functions that never need to
return nullptr.

It's also useful as an argument type to encode the fact that the argument
must not be nullptr.

The make<Foo>() helper is changed to return NonnullOwnPtr<Foo>.

Note: You can move() out of a NonnullOwnPtr, and after that the object is
in an invalid state. Internally it will be a nullptr at this point, so we'll
still catch misuse, but the only thing that should be done in this state
is running the destructor. I've used consumable annotations to generate some
warnings when using a NonnullOwnPtr after moving from it, but these only
work when compiling with clang, so be aware of that.
2019-07-24 08:32:11 +02:00
Andreas Kling ecc35876af AK: Move clang-specific consumable annotation helpers to Platform.h 2019-07-24 08:16:59 +02:00
Andreas Kling adb7b1bc4c AK: Add Optional::value_or(T).
This is like value() but with a fallback in case there's no value set.
2019-07-24 07:26:02 +02:00
Andreas Kling facfaa50df AK: Remove unused Vector::shift_left().
I was using this for a makeshift queue, but now there is AK::Queue.
2019-07-21 21:46:03 +02:00
Andreas Kling af81645a2a Kernel+LibC: Add a dbgputstr() syscall for sending strings to debug output.
This is very handy for the DebugLogStream implementation, among others. :^)
2019-07-21 21:43:37 +02:00
Robin Burchell a3213659dd AK: Run host tests on make
Restructure the makefile a little so it only builds objects once, and
then run them on make clean.

This is a little slower (since we're relinking tests each makeall), but
it also ensures that it will work.
2019-07-21 18:48:44 +02:00
Andreas Kling 29a62558c4 AK: Fix off-by-one in Vector::prepend(Vector&&).
Caught by valgrind's uninitialized access checks on the Vector unit test.
Yay for finding bugs with valgrind on the unit tests! :^)
2019-07-21 12:55:39 +02:00
Andreas Kling 20c6edc976 AK: Make NonnullRefPtr::operator=(NonnullRefPtr<U>&&) cast incoming pointer.
Same as the RefPtr issue I just fixed. This makes it possible to assign a
NonnullRefPtr<Derived>&& to a NonnullRefPtr<Base>.
2019-07-21 12:55:39 +02:00
Robin Burchell a9db382f0e TestSuite: Don't leak the suite instance
Makes checking for leaks more straightforward
2019-07-21 11:51:10 +02:00
Robin Burchell fc479d1e20 TestSuite: instance() -> the(), and return a reference
To be more consistent with the rest of the codebase
2019-07-21 11:51:10 +02:00
Andreas Kling fa6f601170 AK: RefPtr::operator=(RefPtr<U>&&) needs to cast the incoming pointer.
Otherwise it's not possible to assign a RefPtr<Derived>&& to a RefPtr<Base>.
2019-07-21 11:37:24 +02:00
Andreas Kling aeae1cb5e2 AK: Add a unit test for Vector::prepend(Vector&&) with complex T.
It's good to verify that complex objects can be moved nicely by Vector.
2019-07-21 11:35:41 +02:00
Andreas Kling 4179283562 AK: Add some basic unit tests for WeakPtr. 2019-07-21 11:34:31 +02:00
Andreas Kling 2fedf36276 TestSuite: Make tests actually run (oops!)
We were not actually running any of the unit tests, only getting a pointer
to them. Thankfully they all pass, even after we start running them. :^)
2019-07-21 11:27:55 +02:00
Andreas Kling 67654ec529 AK: Add Vector::prepend(Vector&&).
Also included a good boy unit test.
2019-07-20 16:10:52 +02:00
Robin Burchell 53262cd08b AK: Introduce IntrusiveList
And use it in the scheduler.

IntrusiveList is similar to InlineLinkedList, except that rather than
making assertions about the type (and requiring inheritance), it
provides an IntrusiveListNode type that can be used to put an instance
into many different lists at once.

As a proof of concept, port the scheduler over to use it. The only
downside here is that the "list" global needs to know the position of
the IntrusiveListNode member, so we have to position things a little
awkwardly to make that happen. We also move the runnable lists to
Thread, to avoid having to publicize the node.
2019-07-19 15:42:30 +02:00
Robin Burchell 7de79d3bbb ELFImage: Sprinkle some constification love 2019-07-18 13:22:51 +02:00
Andreas Kling f60d7e5d1f JsonValue: Add to_uint(), to_int() and as_double().
The to_foo() functions are for converting when you might not be sure of the
underlying value type. The as_foo() family assumes that you know exactly
what the underlying value type is.
2019-07-18 08:18:39 +02:00
Robin Burchell 41d2c674d7 AK: Add a new TestSuite.h from my own work, adapted to match the existing one a bit
This gives a few new features:

* benchmarks
* the ability to run individual testcases easily
* timing of tests
2019-07-16 11:03:38 +02:00
Robin Burchell 6e90f00925 BufferStream: Add a function to fully reset state
This will allow us to reuse the same buffer with the same stream instance.
Needed in AudioServer.
2019-07-16 09:36:38 +02:00