The SI prefixes "k", "M", "G" mean "10^3", "10^6", "10^9".
The IEC prefixes "Ki", "Mi", "Gi" mean "2^10", "2^20", "2^30".
Let's use the correct name, at least in code.
Only changes the name of the constants, no other behavior change.
If the space cannot be allocated, the original memory block shall remain
unchanged and the function should return nullptr.
Also add a function attribute and some null checks.
This adds a new header <sys/internals.h>, which provides access to LibC internals.
This is in the interest of type-checking LibC itself, as well as enabling less-hacky
access for uses like LinkDemo.
And, of course, this progresses LibC towards building cleanly with -Wmissing-declarations.
This enables a nice warning in case a function becomes dead code.
For example with the unused function malloc_good_size() :^)
I found these places by using -Wmissing-declarations.
The Kernel still shows these issues, which I think are false-positives,
but don't want to touch:
- Libraries/LibC/crt0.cpp:41:5: int _start(int, char**, char**)
Not sure how to handle this.
- Libraries/LibC/cxxabi.cpp:48:5: int __cxa_atexit(AtExitFunction, void*, void*)
- Libraries/LibC/cxxabi.cpp:58:6: void __cxa_finalize(void*)
Not sure how to tell the compiler that the compiler is already using them.
- Libraries/LibC/libcinit.cpp:36:6: void __libc_init()
- Libraries/LibC/libcinit.cpp:55:19: void __stack_chk_fail()
- Libraries/LibC/malloc.cpp:430:6: void __malloc_init()
- Libraries/LibC/stdio.cpp:562:6: void __stdio_init()
These are ninja-imported by other LibC functions.
Maybe we should have some kind of "internals.h" header.
This allows operator new and operator delete to be available to anyone
that links -lc (everyone) rather than just people that include
kmalloc.h (almost no one).
We now allocate 64KB at a time and divide them into chunks for handing
out to malloc() callers. This significantly reduces the number of
system calls made due to memory allocation.
This yields a ~15% speedup when compiling Process.cpp inside SerenityOS
(down from 24 sec to 20 sec on my machine.)
There's more performance on the table here, no doubt.
As suggested by Joshua, this commit adds the 2-clause BSD license as a
comment block to the top of every source file.
For the first pass, I've just added myself for simplicity. I encourage
everyone to add themselves as copyright holders of any file they've
added or modified in some significant way. If I've added myself in
error somewhere, feel free to replace it with the appropriate copyright
holder instead.
Going forward, all new source files should include a license header.
This is a bit sad, but, with the Allocators as static globals their
destructors were running before some user code. Which doesn't really
make much sense, as none of the members of (at least the basic one) do
any real heavy lifting or have many resources to RAII.
To avoid the problem, just mmap the memory for the global arrays of
Allocators in __malloc_init and let the Kernel collect the memory when
we're done with the process.
This extends the opportunistic protection of empty-but-kept-around to
also cover BigAllocationBlocks. Since we only cache 4KB BAB's at the
moment, this sees limited use, but it does work.
We now keep a separate queue of empty ChunkedBlocks in each allocator.
The underlying memory for each block is mprotect'ed with PROT_NONE to
provoke crashes on use-after-free.
This is not going to catch *all* use-after-frees, but if it catches
some, that's still pretty nice. :^)
The malloc memory region names are now updated to reflect their reuse
status: "malloc: ChunkedBlock(size) (free/reused)"
This one is a bit mysterious. I can't find any authoritative answer on what
the correct behavior is, but it seems reasonable to me that free() doesn't
step on errno, since it returns "void" and thus the caller won't know to
inspect errno anyway.