31 Commits
Author SHA1 Message Date
Lucas Chollet 5d4da773be LibC: Make MB_CUR_MAX have a value compatible with our supported locales
According to POSIX:
"In the POSIX locale the value of {MB_CUR_MAX} shall be 1."
As we only support "POSIX"-compatible locales, let's make MB_CUR_MAX
equal to 1. Note that if we start to support other locales, this macro
will need to defer to a runtime function.
2026-09-21 20:12:39 -04:00
Lucas Chollet d8e8dfeeec LibC: Define MB_CUR_MAX and MB_LEN_MAX in their specified headers
Both locations are specified by POSIX and these values are only user
facing for the moment, so no need to have them in a private header.
2026-09-21 20:12:39 -04:00
implicitfield f5b66e7481 LibC+less: Remove optional includes from stdlib.h
LLVM's libcxx/include/stdlib.h has this to say about how it includes the
system's stdlib.h:

"""
The inclusion of the system's <stdlib.h> is intentionally done once
outside of any include guards because some code expects to be able to
include the underlying system header multiple times to get different
definitions based on the macros that are set before inclusion.
"""

Since our stdlib.h includes math.h (which is more-or-less optional, at
least macOS doesn't seem to do this), we end up in a broken include
cycle (because libcxx has its own math.h, which then includes its own
stdlib.h, getting us back to where we started from.) Due to this, LLVM
never sees our definition of ldiv_t, which breaks the toolchain build.

Just to illustrate further, the situation looks basically like this:
```
// a.h
#include "b.h"

#ifndef A
#define A
foo_t bar = 0;
#endif

// b.h
#ifndef B
#define B

#include "c.h"
typedef int foo_t;

#endif

// c.h
#ifndef C
#define C

#include "a.h"

#endif

// main.c
#include "a.h"
int main() {};
```

To work around this, this patch makes stdlib.h not pull in any extra
headers, since other systems don't appear to do this, and this isn't
exactly required by POSIX either (the exact wording in the spec is
"Inclusion of the <stdlib.h> header may also make visible all symbols
from <stddef.h>, <limits.h>, <math.h>, and <sys/wait.h>.")
2025-03-06 18:40:24 -05:00
Beckett Normington 8bc1f1b63e LibC: Add drand48, srand48
These functions are required for the `perl5` port's random function to
operate correctly. As a bonus, this allows us to remove a nasty patch
that replaces `drand48` with `random`.
2023-08-28 00:40:35 +02:00
Tim Schumacher aae106e37b LibC: Add most of the "header may make visible" dependencies from POSIX 2023-07-19 00:19:35 -06:00
MacDue b16ec1880c LibC+LibCore: Remove serenity_setenv()
This was called from LibCore and passed raw StringView data that may
not be null terminated, then incorrectly passed those strings to
getenv() and also tried printing them with just the %s format
specifier.
2023-02-05 16:40:51 +01:00
MacDue eea4dc5bfe LibCore+LibC: Add putenv() wrapper
This is made safe with a special serenity_putenv() function in LibC.
2023-02-05 16:40:51 +01:00
EWouters 7c93eabffe LibC: Implement mkstemps() in stdlib and add a test
`mkstemps` generates a unique temporary file name from a pattern like
`prefixXXXXXXsuffix` where `prefix` and `suffix` can be any string with
only characters that are valid in a filename. The second parameter is
the length of the suffix.

`mkstemp` is `mkstemps` with suffix length 0, so to avoid code
duplication it calls `mkstemps`. It is unlikely this has any
significant performance impact on SerenityOS.

`generate_unique_filename` now takes the suffix length as a `size_t`.
The original behavior of this function is preserved when specifying a
suffix length of 0. All original uses of this function have been
adapted.

`mkstemps()` was added because it is required by version 4.6.3 of the
ccache port.
2022-10-06 16:14:40 +01:00
Tim Schumacher 88ff01bb17 LibC: Remove _aligned_malloc and _aligned_free
We now have a proper aligned allocation implementation, and the
toolchain patch to make Clang use the intermediary implementation
has already been removed in an earlier iteration.
2022-09-16 05:39:28 +00:00
Peter Elliott 4e0adb638d LibC: Implement posix_memalign(3) and aligned_alloc(3)
Some ports linked against posix_memalign, but didn't use it, and others
used it if it was Available. So I decided to implement posix_memalign.

My implementation adds almost no overhead to regular mallocs. However,
if an alignment is specified, it will use the smallest ChunkedBlock, for
which aligned chunks exist, and simply use one of the chunks that is
aligned. If it cannot use a ChunkedBlock, for size or alignment reasons,
it will use a BigAllocationBlock, and return a pointer to the first
aligned address past the start of the block. This implementation
supports alignments up to 32768, due to the limitations of the
BigAllocationBlock technique.
2022-05-20 22:18:54 +02:00
Tim Schumacher dcd76db319 LibC: Hide posix_memalign by default
This should keep ports from preferring `posix_memalign` over other
implementations of aligned memory.
2022-05-03 21:53:19 +02:00
Andreas Kling 02c18bf6de LibC: Stub out posix_memalign() 2022-04-21 11:50:38 +02:00
Idan Horowitz 086969277e Everywhere: Run clang-format 2022-04-01 21:24:45 +01:00
Lucas CHOLLET ddf9987c39 LibCore+LibC: Add wrapper for setenv
I also added a common interface with StringView compatible parameters:

int serenity_setenv(const char*, ssize_t, const char*, ssize_t, int)

This function is called by both C and C++ API for setenv().
2022-03-02 18:08:05 +01:00
Daniel Bertalan c7423dc20b LibC: Make the address argument of malloc_size a pointer to const
We don't mutate the pointed-to memory, so let's be const correct.

Fixes building the `mimalloc` library that's optionally used by the mold
linker (note that it isn't enabled yet as I haven't tested it).
2022-01-12 14:54:12 +01:00
Daniel Bertalan b9c753f6f9 LibC+LibDl: Declare functions taking no arguments as taking void
In C++, a function declaration with an empty parameter list means that
the function takes no arguments. In C, however, it means that the
function takes an unspecified number of parameters.

What we did previously was therefore non-conforming. This caused a
config check to fail in the curl port, as it was able to redeclare
`rand` as taking an int parameter.
2022-01-08 19:22:00 +01:00
Ali Mohammad Pur e717ca32d1 LibC: Implement ungetwc() 2021-12-21 21:24:36 +03:30
Michel Hermier 0bfb7f9675 LibC: Move _abort next to abort
It should be now the only user of it, and it is more logical to have it
in `stdlib.h` than in `assert.h`
2021-12-18 21:01:10 -08:00
Michel Hermier 35a5305264 LibC: Remove undefined __generate_unique_filename declaration 2021-12-18 21:01:10 -08:00
Daniel Bertalan 40e7ac9967 LibC: Implement _aligned_malloc and _aligned_free
C++17 introduced aligned versions of `new` and `delete`, which are
automatically called by the compiler when allocating over-aligned
objects. As with the regular allocator functions, these are generally
thin wrappers around LibC.

We did not have support for aligned allocations in LibC, so this was not
possible. While libstdc++ has a fallback implementation, libc++ does
not, so the aligned allocation function was disabled internally. This
made building the LLVM port with Clang impossible.

Note that while the Microsoft docs say that aligned_malloc and
_aligned_free are declared in `malloc.h`, libc++ doesn't #include that
file, but instead relies on the definition coming from `stdlib.h`.
Therefore, I chose to declare it in that file instead of creating a new
LibC header.

I chose not to implement the more Unix-y `memalign`, `posix_memalign`,
or the C11 `aligned_alloc`, because that would require us to
significantly alter the memory allocator's internals. See the comment in
malloc.cpp.
2021-11-14 16:46:21 +00:00
Idan Horowitz f12da0af13 LibC+LibELF: Move getauxval and AT_* flags to sys/auxv.h 2021-10-28 11:24:36 +02:00
Daniel Bertalan b3ac24a939 LibC: Use a sensible MB_CUR_MAX value
We always use UTF-8, meaning that a single `wchar_t` might be converted
into up to 4 `char`s. This would cause a buffer overflow if something
actually relied on this being the right value.
2021-10-23 23:31:43 -07:00
Jelle Raaijmakers 0e990a4be8 LibC: Implement mblen() 2021-06-04 10:39:41 +02:00
Tim Schumacher cd970928a0 LibC: Implement getprogname and setprogname 2021-05-30 14:56:50 +01:00
Gunnar Beutner f89e8fb71a AK+LibC: Implement malloc_good_size() and use it for Vector/HashTable
This implements the macOS API malloc_good_size() which returns the
true allocation size for a given requested allocation size. This
allows us to make use of all the available memory in a malloc chunk.

For example, for a malloc request of 35 bytes our malloc would
internally use a chunk of size 64, however the remaining 29 bytes
would be unused.

Knowing the true allocation size allows us to request more usable
memory that would otherwise be wasted and make that available for
Vector, HashTable and potentially other callers in the future.
2021-05-15 16:30:14 +02:00
Gunnar Beutner 9ffc2fe840 LibC: Implement the _Exit function
libstdc++v3 checks whether this function is available and makes
certain functions available in the std namespace if so.
2021-05-09 15:35:01 +02:00
Holden Green 18a641f3f8 Implemented llabs() in stdlib.h/cpp. 2021-04-29 09:34:27 +02:00
Brian Gianforcaro 1682f0b760 Everything: Move to SPDX license identifiers in all files.
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-22 11:22:27 +02:00
Mițca Dumitru 857b0e1dc3 LibC: Add imaxdiv and lldiv 2021-03-09 07:28:06 +01:00
Andreas Kling fc4eae87f8 LibC: Don't honor LIBC_* malloc debugging flags in AT_SECURE context
Just ignore all these environment flags if the AT_SECURE flag is set in
the program's auxiliary vector.

This prevents a user from tricking set-uid programs into dumping debug
information via environment flags.
2021-01-31 14:37:21 +01:00
Andreas Kling 13d7c09125 Libraries: Move to Userland/Libraries/ 2021-01-12 12:17:46 +01:00