diff options
author | Gunnar Beutner <gbeutner@serenityos.org> | 2021-05-15 10:06:41 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-05-15 16:30:14 +0200 |
commit | f89e8fb71a4893911ee5125f34bd5bbb99327d33 (patch) | |
tree | c22e44ba3db1066277af7bb4dbc3f49d40d903a0 /Userland/Libraries/LibC | |
parent | 4ab9d8736b9cb4199caed31e53ccdf30c202bee5 (diff) | |
download | serenity-f89e8fb71a4893911ee5125f34bd5bbb99327d33.zip |
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.
Diffstat (limited to 'Userland/Libraries/LibC')
-rw-r--r-- | Userland/Libraries/LibC/malloc.cpp | 7 | ||||
-rw-r--r-- | Userland/Libraries/LibC/stdlib.h | 1 |
2 files changed, 8 insertions, 0 deletions
diff --git a/Userland/Libraries/LibC/malloc.cpp b/Userland/Libraries/LibC/malloc.cpp index c175c02daf..43519cf296 100644 --- a/Userland/Libraries/LibC/malloc.cpp +++ b/Userland/Libraries/LibC/malloc.cpp @@ -392,6 +392,13 @@ size_t malloc_size(void* ptr) return size; } +size_t malloc_good_size(size_t size) +{ + size_t good_size; + allocator_for_size(size, good_size); + return good_size; +} + void* realloc(void* ptr, size_t size) { if (!ptr) diff --git a/Userland/Libraries/LibC/stdlib.h b/Userland/Libraries/LibC/stdlib.h index 480e570cc7..c63e7eebc5 100644 --- a/Userland/Libraries/LibC/stdlib.h +++ b/Userland/Libraries/LibC/stdlib.h @@ -21,6 +21,7 @@ __BEGIN_DECLS __attribute__((malloc)) __attribute__((alloc_size(1))) void* malloc(size_t); __attribute__((malloc)) __attribute__((alloc_size(1, 2))) void* calloc(size_t nmemb, size_t); size_t malloc_size(void*); +size_t malloc_good_size(size_t); void serenity_dump_malloc_stats(void); void free(void*); __attribute__((alloc_size(2))) void* realloc(void* ptr, size_t); |