Commit graph

22 commits

Author SHA1 Message Date
Sergey Bugaev
2fbc37befc LibJS: Fix undefined behavior in HeapBlock
In C++, it's invalid to cast a block of memory to a complex type without
invoking its constructor. It's even more invalid to simply cast a pointer to a
block of memory to a pointer to *an abstract type*.

To fix this, make sure FreelistEntry is a concrete type, and call its
constructor whenever appropriate.
2020-06-01 17:37:44 +02:00
Marcin Gasperowicz
f4985ca113 LibJS: Use __APPLE__ instead of __MACH__ for MacOS build
This is regarding PR #234. Sergey pointed out that not every Mach is Darwin.
2020-05-23 18:09:07 +02:00
Marcin Gasperowicz
c21dc21f36
Build: Make Lagom build under macOS (#2341)
Lagom now builds under macOS. Only two minor adjustments were required:

* LibCore TCP/UDP code can't use `SOCK_{NONBLOCK,CLOEXEC}` on macOS,
use ioctl() and fcntl() instead

* LibJS `Heap` code pthread usage ported to MacOS
2020-05-23 15:31:30 +02:00
AnotherTest
0901b17126 LibJS: Be a bit more explicit about sizeof(buf) / sizeof(FlatPtr)
This (seemingly) no-op cast communicates our intention to clang
2020-05-08 21:02:47 +02:00
Emanuele Torre
8bd9f7e50e LibJS: run clang-format on all the files 2020-05-05 09:15:16 +02:00
Andreas Kling
f7a1696087 LibJS: Add MarkedValueList and use it for argument passing
A MarkedValueList is basically a Vector<JS::Value> that registers with
the Heap and makes sure that the stored values don't get GC'd.

Before this change, we were unsafely keeping Vector<JS::Value> in some
places, which is out-of-reach for the live reference finding logic
since Vector puts its elements on the heap by default.

We now pass all the JavaScript tests even when running with "js -g",
which does a GC on every heap allocation.
2020-04-19 17:34:33 +02:00
Andreas Kling
2a9e29fbb8 LibJS: Add DeferGC, a RAII way to prevent GC temporarily 2020-04-19 12:09:32 +02:00
Andreas Kling
1b391d78ae LibJS: Allow cells to mark null pointers
This simplifies the cell visiting functions by letting them not worry
about the pointers they pass to the visitor being null.
2020-04-16 16:10:38 +02:00
Andreas Kling
e5da1cc566 LibJS: Throw real TypeError, ReferenceError, etc objects
Instead of just throwing Error objects with a name string, we now throw
the real Error subclass types. :^)
2020-04-10 13:09:35 +02:00
Andreas Kling
9aaf19f4de LibJS: Do a garbage collection every N allocations (N=10'000)
To prevent the heap from growing infinitely large, we now do a full GC
every 10'000 allocations. :^)
2020-04-06 15:54:46 +02:00
Andreas Kling
7a5ef0a87f LibJS: Disable HEAP_DEBUG logging on non-SerenityOS platforms
This makes it a bit easier to work with LibJS on Linux for now.
2020-03-25 09:49:14 +01:00
Andreas Kling
b2f005125d LibJS: Always collect all garbage when destroying Heap
When the Heap is going down, it's our last chance to run destructors,
so add a separate collector mode where we simply skip over the marking
phase and go directly to sweeping. This causes everything to get swept
and all live cells get destroyed.

This way, valgrind reports 0 leaks on exit. :^)
2020-03-23 14:11:19 +01:00
Andreas Kling
1dd71bd68f LibJS: Put some more Heap debug logging behind HEAP_DEBUG 2020-03-23 13:45:01 +01:00
Andreas Kling
79e065f0a2 LibJS: Port garbage collector to Linux
Well that was easy. LibJS can now run on Linux :^)
2020-03-23 13:14:57 +01:00
Andreas Kling
2a7dbac0c5 LibJS: Include the cell size in HeapBlock mmap names
HeapBlocks now show up in SystemMonitor as "LibJS: HeapBlock(32)" :^)
2020-03-21 13:12:16 +01:00
Andreas Kling
6c3afca686 LibJS: Round cell sizes up to a multiple of 16 bytes
This increases HeapBlock utilization significantly (and reduces overall
memory usage.)
2020-03-21 11:49:18 +01:00
Andreas Kling
2106dafd62 LibJS: Delete fully-empty HeapBlocks after garbage collection
We now deallocate GC blocks when they are found to have no live cells
inside them.
2020-03-21 11:46:47 +01:00
Andreas Kling
961b4f2577 LibJS: Add missing copyright headers 2020-03-18 20:21:06 +01:00
Andreas Kling
a119b61782 LibJS: Add Handle<T>, a strong C++ handle for keeping GC objects alive
This is pretty heavy and unoptimized, but it will do the trick for now.
Basically, Heap now has a HashTable<HandleImpl*> and you can call
JS::make_handle(T*) to construct a Handle<T> that guarantees that the
pointee will always survive GC until the Handle<T> is destroyed.
2020-03-18 20:03:17 +01:00
Andreas Kling
cb2e7d1c5f LibJS+js: Add a debug option (js -g) to GC after every allocation
This is very useful for discovering collector bugs.
2020-03-16 19:18:46 +01:00
Andreas Kling
ab404a2f88 LibJS: Implement basic conservative garbage collection
We now scan the stack and CPU registers for potential pointers into the
GC heap, and include any valid Cell pointers in the set of roots.

This works pretty well but we'll also need to solve marking of things
passed to native functions, since those are currently in Vector<Value>
and the Vector storage is on the heap (not scanned.)
2020-03-16 19:14:09 +01:00
Andreas Kling
19452230cd LibJS: Add "Heap" and "Runtime" subdirectories
Let's try to keep LibJS tidy as it expands. :^)
2020-03-16 14:37:19 +01:00