Commit Graph

855 Commits

Author SHA1 Message Date
Andreas Kling
ea839861e5 AK: Make Checked.h work with Clang
Apparently Clang does not have __builtin_foo_overflow_p()

Fixes #2044.
2020-05-01 17:30:47 +02:00
Andreas Kling
888e35f0fe AK: Add ALWAYS_INLINE, NEVER_INLINE and FLATTEN macros
It's tedious to write (and look at) [[gnu::always_inline]] etc. :^)
2020-04-30 11:43:25 +02:00
Sergey Bugaev
279cf9294a AK: Always inline trivial StringView constructors 2020-04-30 11:30:27 +02:00
Sergey Bugaev
135d29b498 AK: Assert that we don't create StringViews of negative length
Due to us using size_t for the length, the actual value will always be positive.
If, for example, we calculate the length as "0 - 1", we'll get SIZE_T_MAX. What
we can do is check that adding the characters pointer and the length together
doesn't overflow.
2020-04-30 11:30:27 +02:00
Sergey Bugaev
361a1b54d7 AK: Add Checked::addition_would_overflow()
And switch the two-argument version of Checked::multiplication_would_overflow()
to use __builtin_mul_overflow_p(). This helps GCC optimize the code better.
2020-04-30 11:30:27 +02:00
Linus Groh
cd81aa41f0 AK: Add FileSystemPath::is_absolute() 2020-04-28 09:36:09 +02:00
Brian Gianforcaro
74f3263cea AK: Add SinglyLinkedListIterator::is_begin()
It's useful to be able to tell if we are at the beginning of
the list when using the list as a queue.
2020-04-27 11:24:21 +02:00
Andreas Kling
9e365a6c1c AK: Make URL::to_string() produce a data URL for data URLs :^) 2020-04-26 22:59:12 +02:00
Andreas Kling
50c1eca9d4 AK: Add a simple and inefficient Base64 decoder 2020-04-26 22:57:00 +02:00
Andreas Kling
389eb1b693 AK: Teach URL how to parse data: URLs :^) 2020-04-26 22:56:58 +02:00
Brian Gianforcaro
1a80aa999a AK: Add timeval_to_timespec and timespec_to_timeval conversion methods
Add the ability to easily convert between timeval and timespec.
2020-04-26 21:31:52 +02:00
Andreas Kling
a59453d4b7 AK: Tweak exchange() implementation
Make it constexpr and do perfect forwarding.
2020-04-22 12:36:35 +02:00
Andreas Kling
ba3b561a40 AK: Simplify Result class so we can start using it
There were some ideas about how to use this class but we never actually
started using it, so let's just simplify it and get it ready for use.

The basic idea is: a function returns a Result<ValueType, ErrorType>.
Callers check if the result object is_error(). If so, an ErrorType can
be fetched with the error() getter. Otherwise, a ValueType is fetched
with the value() getter. That's it. :^)
2020-04-21 16:19:18 +02:00
Andreas Kling
59cd181ed9 AK: Remove unused Error template 2020-04-21 16:19:18 +02:00
Itamar
8a886e0e96 LibDebug: Parse line number information from DWARF format
DWARF line number information, if generated, is stored  in the
.debug_line section of an object file.

The information is encoded as instructions for a VM that is defined in
the DWARF specification.
By executing these instructions, we can extract the encoded line number
information.
2020-04-20 17:25:50 +02:00
Itamar
5c1b3ce42e AK: Allow having ref counted pointers to const object
We allow the ref-counting parts of an object to be mutated even when the
object itself is a const.

An important detail is that we allow invoking 'will_be_destroyed' and
'one_ref_left', which are not required to be const qualified, on const
objects.
2020-04-20 17:25:50 +02:00
Sergey Bugaev
0aeff9c0c4 AK: Add URL::create_with_url_or_path()
This is an utility to create a URL from a given string, which may be either a
URL such as http://example.com (which will be used as-is), or a file path such
as /etc/fstab (which will be transformed into file:///etc/fstab).
2020-04-19 11:14:26 +02:00
Sergey Bugaev
5a96a6565b AK: Consider more URLs invalid
Not just http or https. This fixes "foo" being recognized as a valid URL with
protocol "foo", empty host and empty path.
2020-04-19 11:14:26 +02:00
Andreas Kling
79eee65372 AK: Add URL::create_with_file_protocol(path)
This is a convenience helper that allows you to easily construct a
file:// URL from an absolute path.
2020-04-18 22:02:04 +02:00
wilsonk
75f6454de7
AK: Dual pivot quicksort implementation (#1838) 2020-04-18 13:58:49 +02:00
Stephan Unverwerth
b77ceecb02 AK: Add StringView::contains(char) 2020-04-17 15:22:31 +02:00
Andreas Kling
2db2b8b0ef AK: Add FlyString::is_empty() 2020-04-16 20:22:54 +02:00
Andreas Kling
63b8c6913c AK: Add Checked<T>::multiplication_would_overflow()
This allows you to comfortably test if multiply 2 or 3 values would
cause arithmetic overflow.
2020-04-15 17:14:18 +02:00
Andreas Kling
9c54bad241 AK: Add a Checked<T> template
A Checked<T> is a boxed integer type that asserts if you try to use its
value after an arithmetic overflow.
2020-04-15 16:58:46 +02:00
Andreas Kling
9a5dba9e09 AK: Add MakeUnsigned<T> helper template 2020-04-15 16:58:46 +02:00
Andreas Kling
ca2052ae4a AK: Add a simple NumericLimits<T> template
This provides min(), max() and is_signed() for the basic integer types.
2020-04-15 16:58:46 +02:00
Andreas Kling
b237ba57ea AK: Add a little test for String::split()
Just to verify that the parts are all null-terminated.
2020-04-14 15:36:25 +02:00
Itamar
e207de8449 LibELF: Add find_demangled_function
Also, added AK::String::index_of and fixed a bug in ELF::Loader::symbol_ptr
2020-04-13 23:20:59 +02:00
Andreas Kling
02e0fab19a AK: Let FlyString::hash() assume that the string was already hashed
Since the FlyString deduplication mechanism uses a HashTable, we know
that any StringImpl inside a non-null FlyString will already have its
lazily computed hash.
2020-04-13 12:27:05 +02:00
Andreas Kling
26a8984d03 AK: Inline Optional functions more aggressively
This turns into much less code in the most common cases, here's why:
The normal Optional usage pattern is something like:

    auto foo = get_me_an_optional();
    if (foo.has_value())
        do_stuff_with(foo.value());

In this typical scenario, we check has_value() before calling value().
Without inlining, value() will double-check has_value() itself and
assert if it fails. Inlining allows the compiler to optimize all of
this away.
2020-04-12 20:37:51 +02:00
Andreas Kling
3bbc2c7300 AK: Add LogStream operator<< overloads for float and double 2020-04-12 19:23:39 +02:00
Linus Groh
c8d0a2eb3c AK: Parse query and fragment in URL::parse() 2020-04-12 01:18:39 +02:00
Linus Groh
21a61b276b AK: Support fragment in URL 2020-04-12 01:18:39 +02:00
Andreas Kling
b1555381ee AK: Recompute URL validity after changing protocol/host/path
This allows you to build URLs by calling setters on an empty URL and
actually get a valid URL at the end.
2020-04-11 23:11:10 +02:00
Andreas Kling
c993c7e3dc AK: String::contains() should say no if needle or haystack is null 2020-04-11 14:33:05 +02:00
Andreas Kling
8cbb8491cb AK: Add FlyString::hash() 2020-04-10 16:26:59 +02:00
Paul Redmond
a9779e12ea
AK: Appending 0 bytes to a ByteBuffer should be a no-op (#1699)
- inserting an empty string into LibLine Editor triggered an assertion
  trying to grow a ByteBuffer by 0 bytes.
2020-04-08 17:04:37 +02:00
Andreas Kling
19be842b5b AK: Disable the consumable annotation checking to fix Clang build
Clang keeps whining that NonnullFooPtrs are in "unknown" state and I'm
not sure how to resolve that right now. Disable the checking until we
can figure it out.
2020-04-07 17:30:16 +02:00
Andreas Kling
f8942411ac AK: Add forward() overload that refuses to forward lvalue as rvalue
This matches what other forward() implementations do.
2020-04-07 15:56:19 +02:00
Emanuel Sprung
154dcd1ed6 AK: Allow %m.nf specifier for double/float in printf to set fraction with
This patch adds the missing part for the printf of double values to specify
the length of the fraction part. For GVariant, a default of %.2 is used.
2020-04-07 09:02:02 +02:00
Andreas Kling
20e58c5513 AK: Make dbgprintf() and dbgputstr() go to stderr on non-Serenity hosts 2020-04-06 10:49:27 +02:00
Andreas Kling
0d48fb9a87 AK: Add out() and warn() streams that forward to stdout and stderr
Our C++ code generator tools have been relying on host-side dbg() being
forwarded to stdout until now. Now they use out() instead.

Hopefully this will make it easier and more enticing to use streams in
userspace programs as well. :^)
2020-04-06 10:49:27 +02:00
nimelehin
c0a4cf5e8d Kernel: Support best fit allocation policy in kmalloc()
Add find_best_fit() which implements best fit allocation algorithm.
Kmalloc now uses a best fit allocation policy for large allocations.
2020-04-06 08:33:13 +02:00
nimelehin
fbcc798f3f Bitmap: Add Bitmap::find_next_range_of_unset_bits()
AK::Bitmap is extended with find_next_range_of_unset_bits().
The function is implemented using count_trailing_zeroes_32(), which is
optimized on many platforms, that gives a huge performance boost.
Functions find_longest_range_of_unset_bits() and find_first_fit() are
implemented with find_next_range_of_unset_bits(). According to
benchmarks, they are 60-100% faster.
2020-04-06 08:31:01 +02:00
nimelehin
471db7ce0a AK: Add count_trailing_zeroes_32()
Add count_trailing_zeroes_32() which is implemented with builtins
if available, otherwise there's a generic fallback.
2020-04-06 08:28:57 +02:00
nimelehin
8137ef6252 AK: Add Bitmap::find_first_fit()
Add find_first_fit() which implements first fit algorithm.
2020-04-06 08:27:39 +02:00
nimelehin
73901c9b2b AK: Add Bitmap::set_range()
Add set_range() which sets a range of bits to requested value.
Fix code style.
2020-04-06 08:27:17 +02:00
Andreas Kling
76bb0fab2d AK: Make FlyString a little less hilariously unoptimized :^) 2020-04-06 08:26:26 +02:00
Andreas Kling
4f200def9c AK: Stop allowing implicit downcast with OwnPtr and NonnullOwnPtr
Same issue here as we had with RefPtr and NonnullRefPtr.

Since we can't make copies of an owning pointer, we don't get quite the
same static_ptr_cast<T> here. Instead I've only added a new templated
version of OwnPtr::release_nonnull() in this patch, to solve the only
issue that popped up.

I'm not sure what the best solution here is, but this works for now.
2020-04-05 11:32:30 +02:00
Andreas Kling
1d468ed6d3 AK: Stop allowing implicit downcast with RefPtr and NonnullRefPtr
We were allowing this dangerous kind of thing:

RefPtr<Base> base;
RefPtr<Derived> derived = base;

This patch changes the {Nonnull,}RefPtr constructors so this is no
longer possible.

To downcast one of these pointers, there is now static_ptr_cast<T>:

RefPtr<Derived> derived = static_ptr_cast<Derived>(base);

Fixing this exposed a ton of cowboy-downcasts in various places,
which we're now forced to fix. :^)
2020-04-05 11:19:00 +02:00
AnotherTest
7d0bf9b5a9 Kernel+AK: Separate out MACAddress and move it into AK 2020-04-05 09:50:48 +02:00
Tibor Nagy
1bf93d504f AK: Break on end of input in JsonParser::consume_quoted_string
Fixes #1599
2020-04-04 10:31:01 +02:00
AnotherTest
df7062aac5 AK: Add an overwrite API to ByteBuffer
Since ByteBuffer is a Buffer, it should allow us to overwrite parts of
it that we have allocated.
This comes in useful in handling unsequenced writes like handling
fragmented ip packets :^)
2020-04-03 09:42:13 +02:00
Emanuel Sprung
d0d093fef5 AK: Remove relative path in AK testsuite
With relative filenames in the executable code, the executable is basically not
relocatable. This makes out-of-source builds unneccesery hard. This patchset moves
the relative link into the filesystem: that can be handled much easier :^)
2020-04-03 09:07:19 +02:00
Emanuel Sprung
b995a499d3 AK: Add equals method to JsonValue to semantically compare two values.
This patchsets adds the semantic check of two values. One first approach
was to compare the (generated) json strings of the two values. This works
out in the most cases, but not with numbers, where "1.0" and "1" in JSON
format are semantically the same. Therefore, this patch adds deep (recursive)
check of two JsonValues.
2020-04-01 22:12:19 +02:00
Emanuel Sprung
2577712a1c AK: Add String::replace() functionality
This adds a replace functionality that replaces a string that contains
occurences of a "needle" by a "replacement" value. With "all_occurences"
enabled, all occurences are being replaced, otherwise only the first
occurence is being replaced.
2020-04-01 21:47:23 +02:00
Andreas Kling
806d3d8e79 AK: Add adopt_own() to create a NonnullOwnPtr<T> from a T& 2020-04-01 21:05:35 +02:00
Emanuel Sprung
c54855682c AK: A few JSON improvements
* Add double number to object serializer

* Handle negative double numbers correctly

* Handle \r and \n in quoted strings independently
  This improves the situation when keys contain \r or \n that currently
  has the effect that "a\rkey" and "a\nkey" in an JSON object are the
  same key value.
2020-03-31 13:42:39 +02:00
Emanuel Sprung
c925aaceb2 AK: Print double numbers with printf
This patchset allows double numbers to be printed with the printf function.
The fraction will always be printed as 6 digit number. This can be improved :^)
2020-03-31 13:42:39 +02:00
Itamar
6b74d38aab Kernel: Add 'ptrace' syscall
This commit adds a basic implementation of
the ptrace syscall, which allows one process
(the tracer) to control another process (the tracee).

While a process is being traced, it is stopped whenever a signal is
received (other than SIGCONT).

The tracer can start tracing another thread with PT_ATTACH,
which causes the tracee to stop.

From there, the tracer can use PT_CONTINUE
to continue the execution of the tracee,
or use other request codes (which haven't been implemented yet)
to modify the state of the tracee.

Additional request codes are PT_SYSCALL, which causes the tracee to
continue exection but stop at the next entry or exit from a syscall,
and PT_GETREGS which fethces the last saved register set of the tracee
(can be used to inspect syscall arguments and return value).

A special request code is PT_TRACE_ME, which is issued by the tracee
and causes it to stop when it calls execve and wait for the
tracer to attach.
2020-03-28 18:27:18 +01:00
Andreas Kling
95cc4c7e74 AK: Add some string comparison operators
Some of these are very inefficient. It's nice to have some optimization
opportunities in the future though. :^)
2020-03-28 09:11:13 +01:00
Sergey Bugaev
5bb18bf548 AK: Use print_string() for %c formatting
Instead of simply outputting the character. This way, we get proper padding
support and other niceties strings enjoy.
2020-03-26 08:01:16 +01:00
Andreas Kling
e34fe556ca AK: Fix JsonParser kernel build (no floats/doubles in kernel code) 2020-03-24 22:37:24 +01:00
Emanuel Sprung
bca5762542 AK: Add parsing of JSON double values
This patch adds the parsing of double values to the JSON parser.
There is another char buffer that get's filled when a "." is present
in the number parsing. When number finished, a divider is calculated
to transform the number behind the "." to the actual fraction value.
2020-03-24 22:20:07 +01:00
Emanuel Sprung
45921328e4 AK: Add get_or() method to JsonObject
This allows to retrieve a default value for items thare are not
available in the json object.
2020-03-24 22:20:07 +01:00
Andreas Kling
404de10a15 AK: Add FlyString::is_null() 2020-03-24 16:14:10 +01:00
Andreas Kling
7d862dd5fc AK: Reduce header dependency graph of String.h
String.h no longer pulls in StringView.h. We do this by moving a bunch
of String functions out-of-line.
2020-03-23 13:48:44 +01:00
Andreas Kling
c4a6d6ae9f AK: Add FlyString::to_lowercase() and LogStream operator<<(FlyString) 2020-03-22 19:07:02 +01:00
Andreas Kling
26bc3d4ea0 AK: Add FlyString::equals_ignoring_case(StringView)
And share the code with String by moving the logic to StringUtils. :^)
2020-03-22 13:07:45 +01:00
Andreas Kling
0efa47b7ef AK: Run clang-format on StringUtils.{cpp,h} 2020-03-22 13:04:04 +01:00
Andreas Kling
4f72f6b886 AK: Add FlyString, a simple flyweight string class
FlyString is a flyweight string class that wraps a RefPtr<StringImpl>
known to be unique among the set of FlyStrings. The class is very
unoptimized at the moment.

When to use FlyString:

- When you want O(1) string comparison
- When you want to deduplicate a lot of identical strings

When not to use FlyString:

- For strings that don't need either of the above features
- For strings that are likely to be unique
2020-03-22 13:03:43 +01:00
Andreas Kling
4eef3e5a09 AK: Add StringBuilder::join() for joining collections with a separator
This patch adds a generic StringBuilder::join(separator, collection):

    Vector<String> strings = { "well", "hello", "friends" };
    StringBuilder builder;
    builder.join("+ ", strings);
    builder.to_string(); // "well + hello + friends"
2020-03-20 14:41:23 +01:00
howar6hill
e07f50c398 AK: Add begin() and end() to String and StringView
Now it's possible to use range-based for loops with String and StringView.
2020-03-10 10:25:21 +01:00
Andreas Kling
b956e2d939 AK: Remove all the AK .host.o files on "make clean" in AK/Tests
This is a bit hackish, but sometimes these files stick around and mess
up rebuilds.
2020-03-08 21:40:40 +01:00
howar6hill
6da131d5db AK: Reduce code duplication in StringBuilder 2020-03-08 17:08:18 +01:00
howar6hill
d8df9c510c AK: Improve the API of StringBuilder 2020-03-08 17:08:18 +01:00
Liav A
0433c0780d AK: Use default constructor of Optional if an unset bit is not found 2020-03-08 14:13:30 +01:00
Andreas Kling
32f15f3c45 AK: Remove unused InlineLRUCache template
This was not used by anyone. If we ever need it, we can bring it back.
2020-03-08 13:13:34 +01:00
Andreas Kling
900f51ccd0 AK: Move memory stuff (fast memcpy, etc) to a separate header
Move the "fast memcpy" stuff out of StdLibExtras.h and into Memory.h.
This will break a ton of things that were relying on StdLibExtras.h
to include a bunch of other headers. Fix will follow immediately after.

This makes it possible to include StdLibExtras.h from Types.h, which is
the main point of this exercise.
2020-03-08 13:06:51 +01:00
Andreas Kling
35d88f536c AK: Use __builtin_memset() and such to reduce header dependencies
We can use __builtin_memset() without including <string.h>.
This is pretty neat, as it will allow us to reduce the header deps
of AK templates a bit, if applied consistently.

Note that this is an enabling change for an upcoming #include removal.
2020-03-08 13:06:51 +01:00
Andreas Kling
b1058b33fb AK: Add global FlatPtr typedef. It's u32 or u64, based on sizeof(void*)
Use this instead of uintptr_t throughout the codebase. This makes it
possible to pass a FlatPtr to something that has u32 and u64 overloads.
2020-03-08 13:06:51 +01:00
Andreas Kling
b98d8ad5b0 AK: Add a Conditional<condition, TrueType, FalseType> template
This allows you to select a type based on a compile-time condition.
2020-03-08 13:06:51 +01:00
Andreas Kling
b866582d98 AK: Fix all the warnings in the AK tests 2020-03-06 11:22:23 +01:00
Andreas Kling
8bb361889c AK: Remove Optional::operator bool()
This was causing some obvious-in-hindsight but hard to spot bugs where
we'd implicitly convert the bool to an integer type and carry on with
the number 1 instead of the actual value().
2020-03-06 10:32:58 +01:00
Andreas Kling
dc039fdc7e AK: Simplify JsonObject and JsonArray API a little bit
Instead of set(const JsonValue&) and set(JsonValue&&), just do
set(JsonValue) and let callers move() if they want. This removes some
ambiguity and the compiler is smart enough to optimize it anyway.
2020-03-06 08:51:22 +01:00
Andreas Kling
783c99516a AK: LogStream should handle being passed a null const char* 2020-03-04 21:04:06 +01:00
Andreas Kling
686ade6b5a AK: Make quick_sort() a little more ergonomic
Now it actually defaults to "a < b" comparison, instead of forcing you
to provide a trivial less-than comparator. Also you can pass in any
collection type that has .begin() and .end() and we'll sort it for you.
2020-03-03 16:02:58 +01:00
Liav A
9440c45d6e AK: Add support for Kernel Log Stream 2020-03-02 22:23:39 +01:00
Andreas Kling
214f934465 Meta: Adjust some copyright dates by Fei Wu 2020-03-02 14:24:25 +01:00
Andreas Kling
f5b78c2a1c AK: Add missing copyright headers to StringUtils.{cpp,h} 2020-03-02 14:23:11 +01:00
howar6hill
d75fa80a7b
AK: Move to_int(), to_uint() implementations to StringUtils (#1338)
Provide wrappers in String and StringView. Add some tests for the
implementations.
2020-03-02 14:19:33 +01:00
howar6hill
055344f346 AK: Move the wildcard-matching implementation to StringUtils
Provide wrappers in the String and StringView classes, and add some tests.
2020-03-02 10:38:08 +01:00
howar6hill
2a30a020c1
AK: Add enqueue_begin() for the CircularDeque class (#1320)
Also add tests for CircularDeque.
2020-03-02 09:50:43 +01:00
howar6hill
4ed2ba264d
AK: Remove superfluous explicit in Bitmap (#1337) 2020-03-02 09:50:22 +01:00
Andreas Kling
22d0a6d92f AK: Remove unnecessary casts to size_t, after Vector changes
Now that Vector uses size_t, we can remove a whole bunch of redundant
casts to size_t.
2020-03-01 12:58:22 +01:00
Andreas Kling
dcd619bd46 Kernel: Merge the shbuf_get_size() syscall into shbuf_get()
Add an extra out-parameter to shbuf_get() that receives the size of the
shared buffer. That way we don't need to make a separate syscall to
get the size, which we always did immediately after.
2020-02-28 12:55:58 +01:00
Andreas Kling
a1514369d7 LibC: Move shbuf_* API's to <serenity.h> 2020-02-28 12:55:58 +01:00
Andreas Kling
f72e5bbb17 Kernel+LibC: Rename shared buffer syscalls to use a prefix
This feels a lot more consistent and Unixy:

    create_shared_buffer()   => shbuf_create()
    share_buffer_with()      => shbuf_allow_pid()
    share_buffer_globally()  => shbuf_allow_all()
    get_shared_buffer()      => shbuf_get()
    release_shared_buffer()  => shbuf_release()
    seal_shared_buffer()     => shbuf_seal()
    get_shared_buffer_size() => shbuf_get_size()

Also, "shared_buffer_id" is shortened to "shbuf_id" all around.
2020-02-28 12:55:58 +01:00
howar6hill
a19967d5c3 Tests: Fix a typo inTestRefPtr 2020-02-27 21:16:02 +01:00
William McPherson
121e7306c3 AK: Expose SinglyLinkedListIterator constructor
This commit replaces SinglyLinkedListIterator::universal_end() with an
empty SinglyLinkedListIterator(). Piano needs this in order to
initialize a member array of iterators without 84 lines of
universal_end().
2020-02-27 10:21:13 +01:00