| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |  * modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 1. Redistributions of source code must retain the above copyright notice, this | 
					
						
							|  |  |  |  *    list of conditions and the following disclaimer. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 2. Redistributions in binary form must reproduce the above copyright notice, | 
					
						
							|  |  |  |  *    this list of conditions and the following disclaimer in the documentation | 
					
						
							|  |  |  |  *    and/or other materials provided with the distribution. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
					
						
							|  |  |  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
					
						
							|  |  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
					
						
							|  |  |  |  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
					
						
							|  |  |  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
					
						
							|  |  |  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
					
						
							|  |  |  |  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
					
						
							|  |  |  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
					
						
							|  |  |  |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
					
						
							|  |  |  |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | #include <AK/Bitmap.h>
 | 
					
						
							|  |  |  | #include <AK/InlineLinkedList.h>
 | 
					
						
							| 
									
										
										
										
											2019-07-25 15:23:29 +02:00
										 |  |  | #include <AK/ScopedValueRollback.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-14 16:38:06 +02:00
										 |  |  | #include <AK/Vector.h>
 | 
					
						
							| 
									
										
										
										
											2019-08-25 23:51:27 +03:00
										 |  |  | #include <LibThread/Lock.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | #include <assert.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-19 20:52:12 +02:00
										 |  |  | #include <mallocdefs.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | #include <serenity.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:49:03 +02:00
										 |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-08 12:05:14 +01:00
										 |  |  | #include <string.h>
 | 
					
						
							| 
									
										
										
										
											2020-08-12 01:58:50 +02:00
										 |  |  | #include <sys/internals.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:49:03 +02:00
										 |  |  | #include <sys/mman.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | // FIXME: Thread safety.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //#define MALLOC_DEBUG
 | 
					
						
							| 
									
										
										
										
											2019-05-18 22:26:01 +02:00
										 |  |  | #define RECYCLE_BIG_ALLOCATIONS
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | #define MAGIC_PAGE_HEADER 0x42657274
 | 
					
						
							|  |  |  | #define MAGIC_BIGALLOC_HEADER 0x42697267
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:49:03 +02:00
										 |  |  | #define PAGE_ROUND_UP(x) ((((size_t)(x)) + PAGE_SIZE - 1) & (~(PAGE_SIZE - 1)))
 | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-16 20:25:51 +02:00
										 |  |  | ALWAYS_INLINE static void ue_notify_malloc(const void* ptr, size_t size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     send_secret_data_to_userspace_emulator(1, size, (FlatPtr)ptr); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ALWAYS_INLINE static void ue_notify_free(const void* ptr) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     send_secret_data_to_userspace_emulator(2, (FlatPtr)ptr, 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-25 23:51:27 +03:00
										 |  |  | static LibThread::Lock& malloc_lock() | 
					
						
							| 
									
										
										
										
											2019-07-13 18:36:19 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-08-25 23:51:27 +03:00
										 |  |  |     static u32 lock_storage[sizeof(LibThread::Lock) / sizeof(u32)]; | 
					
						
							|  |  |  |     return *reinterpret_cast<LibThread::Lock*>(&lock_storage); | 
					
						
							| 
									
										
										
										
											2019-07-13 18:36:19 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-11 21:36:03 +02:00
										 |  |  | constexpr size_t number_of_chunked_blocks_to_keep_around_per_size_class = 4; | 
					
						
							|  |  |  | constexpr size_t number_of_big_blocks_to_keep_around_per_size_class = 8; | 
					
						
							| 
									
										
										
										
											2019-05-02 15:56:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | static bool s_log_malloc = false; | 
					
						
							|  |  |  | static bool s_scrub_malloc = true; | 
					
						
							|  |  |  | static bool s_scrub_free = true; | 
					
						
							| 
									
										
										
										
											2020-02-02 20:28:29 +01:00
										 |  |  | static bool s_profiling = false; | 
					
						
							| 
									
										
										
										
											2020-02-16 14:07:48 +01:00
										 |  |  | static unsigned short size_classes[] = { 8, 16, 32, 64, 128, 252, 508, 1016, 2036, 4090, 8188, 16376, 32756, 0 }; | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | static constexpr size_t num_size_classes = sizeof(size_classes) / sizeof(unsigned short); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												AK: Rename KB, MB, GB to KiB, MiB, GiB
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.
											
										 
											2020-08-15 13:55:00 -04:00
										 |  |  | constexpr size_t block_size = 64 * KiB; | 
					
						
							| 
									
										
										
										
											2020-02-16 14:07:48 +01:00
										 |  |  | constexpr size_t block_mask = ~(block_size - 1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | struct CommonHeader { | 
					
						
							|  |  |  |     size_t m_magic; | 
					
						
							|  |  |  |     size_t m_size; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct BigAllocationBlock : public CommonHeader { | 
					
						
							|  |  |  |     BigAllocationBlock(size_t size) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_magic = MAGIC_BIGALLOC_HEADER; | 
					
						
							|  |  |  |         m_size = size; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     unsigned char* m_slot[0]; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct FreelistEntry { | 
					
						
							|  |  |  |     FreelistEntry* next; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 14:07:48 +01:00
										 |  |  | struct ChunkedBlock | 
					
						
							|  |  |  |     : public CommonHeader | 
					
						
							| 
									
										
										
										
											2019-06-07 11:49:03 +02:00
										 |  |  |     , public InlineLinkedListNode<ChunkedBlock> { | 
					
						
							| 
									
										
										
										
											2020-02-16 14:07:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  |     ChunkedBlock(size_t bytes_per_chunk) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_magic = MAGIC_PAGE_HEADER; | 
					
						
							|  |  |  |         m_size = bytes_per_chunk; | 
					
						
							|  |  |  |         m_free_chunks = chunk_capacity(); | 
					
						
							|  |  |  |         m_freelist = (FreelistEntry*)chunk(0); | 
					
						
							|  |  |  |         for (size_t i = 0; i < chunk_capacity(); ++i) { | 
					
						
							|  |  |  |             auto* entry = (FreelistEntry*)chunk(i); | 
					
						
							|  |  |  |             if (i != chunk_capacity() - 1) | 
					
						
							|  |  |  |                 entry->next = (FreelistEntry*)chunk(i + 1); | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |                 entry->next = nullptr; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ChunkedBlock* m_prev { nullptr }; | 
					
						
							|  |  |  |     ChunkedBlock* m_next { nullptr }; | 
					
						
							|  |  |  |     FreelistEntry* m_freelist { nullptr }; | 
					
						
							|  |  |  |     unsigned short m_free_chunks { 0 }; | 
					
						
							| 
									
										
										
										
											2020-07-21 22:47:51 +02:00
										 |  |  |     [[gnu::aligned(8)]] unsigned char m_slot[0]; | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-11 21:36:03 +02:00
										 |  |  |     void* chunk(size_t index) | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         return &m_slot[index * m_size]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-05-02 17:06:05 +02:00
										 |  |  |     bool is_full() const { return m_free_chunks == 0; } | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  |     size_t bytes_per_chunk() const { return m_size; } | 
					
						
							|  |  |  |     size_t free_chunks() const { return m_free_chunks; } | 
					
						
							|  |  |  |     size_t used_chunks() const { return chunk_capacity() - m_free_chunks; } | 
					
						
							| 
									
										
										
										
											2020-02-16 14:07:48 +01:00
										 |  |  |     size_t chunk_capacity() const { return (block_size - sizeof(ChunkedBlock)) / m_size; } | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 15:56:52 +02:00
										 |  |  | struct Allocator { | 
					
						
							|  |  |  |     size_t size { 0 }; | 
					
						
							|  |  |  |     size_t block_count { 0 }; | 
					
						
							| 
									
										
										
										
											2019-12-17 22:55:19 +03:00
										 |  |  |     size_t empty_block_count { 0 }; | 
					
						
							|  |  |  |     ChunkedBlock* empty_blocks[number_of_chunked_blocks_to_keep_around_per_size_class] { nullptr }; | 
					
						
							| 
									
										
										
										
											2019-05-02 17:06:05 +02:00
										 |  |  |     InlineLinkedList<ChunkedBlock> usable_blocks; | 
					
						
							|  |  |  |     InlineLinkedList<ChunkedBlock> full_blocks; | 
					
						
							| 
									
										
										
										
											2019-05-02 15:56:52 +02:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-14 16:38:06 +02:00
										 |  |  | struct BigAllocator { | 
					
						
							| 
									
										
										
										
											2019-05-18 22:26:01 +02:00
										 |  |  |     Vector<BigAllocationBlock*, number_of_big_blocks_to_keep_around_per_size_class> blocks; | 
					
						
							| 
									
										
										
										
											2019-05-14 16:38:06 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-18 11:43:21 +03:00
										 |  |  | // Allocators will be initialized in __malloc_init.
 | 
					
						
							|  |  |  | // We can not rely on global constructors to initialize them,
 | 
					
						
							|  |  |  | // because they must be initialized before other global constructors
 | 
					
						
							|  |  |  | // are run. Similarly, we can not allow global destructors to destruct
 | 
					
						
							|  |  |  | // them. We could have used AK::NeverDestoyed to prevent the latter,
 | 
					
						
							|  |  |  | // but it would have not helped with the former.
 | 
					
						
							|  |  |  | static u8 g_allocators_storage[sizeof(Allocator) * num_size_classes]; | 
					
						
							|  |  |  | static u8 g_big_allocators_storage[sizeof(BigAllocator)]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline Allocator (&allocators())[num_size_classes] | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return reinterpret_cast<Allocator(&)[num_size_classes]>(g_allocators_storage); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline BigAllocator (&big_allocators())[1] | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return reinterpret_cast<BigAllocator(&)[1]>(g_big_allocators_storage); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-05-02 15:56:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | static Allocator* allocator_for_size(size_t size, size_t& good_size) | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-07-11 21:36:03 +02:00
										 |  |  |     for (size_t i = 0; size_classes[i]; ++i) { | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  |         if (size <= size_classes[i]) { | 
					
						
							|  |  |  |             good_size = size_classes[i]; | 
					
						
							| 
									
										
										
										
											2020-02-18 11:43:21 +03:00
										 |  |  |             return &allocators()[i]; | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     good_size = PAGE_ROUND_UP(size); | 
					
						
							|  |  |  |     return nullptr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-14 16:38:06 +02:00
										 |  |  | static BigAllocator* big_allocator_for_size(size_t size) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-16 14:07:48 +01:00
										 |  |  |     if (size == 65536) | 
					
						
							| 
									
										
										
										
											2020-02-18 11:43:21 +03:00
										 |  |  |         return &big_allocators()[0]; | 
					
						
							| 
									
										
										
										
											2019-05-14 16:38:06 +02:00
										 |  |  |     return nullptr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | extern "C" { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-19 15:54:56 +02:00
										 |  |  | static void* os_alloc(size_t size, const char* name) | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-16 14:07:48 +01:00
										 |  |  |     auto* ptr = serenity_mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_PURGEABLE, 0, 0, block_size, name); | 
					
						
							|  |  |  |     ASSERT(ptr != MAP_FAILED); | 
					
						
							|  |  |  |     return ptr; | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void os_free(void* ptr, size_t size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int rc = munmap(ptr, size); | 
					
						
							|  |  |  |     assert(rc == 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 20:28:29 +01:00
										 |  |  | static void* malloc_impl(size_t size) | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-07-13 18:36:19 +02:00
										 |  |  |     LOCKER(malloc_lock()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  |     if (s_log_malloc) | 
					
						
							| 
									
										
										
										
											2019-09-29 21:02:13 +02:00
										 |  |  |         dbgprintf("LibC: malloc(%zu)\n", size); | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!size) | 
					
						
							|  |  |  |         return nullptr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     size_t good_size; | 
					
						
							|  |  |  |     auto* allocator = allocator_for_size(size, good_size); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!allocator) { | 
					
						
							| 
									
										
										
										
											2020-02-16 14:07:48 +01:00
										 |  |  |         size_t real_size = round_up_to_power_of_two(sizeof(BigAllocationBlock) + size, block_size); | 
					
						
							| 
									
										
										
										
											2019-05-18 22:26:01 +02:00
										 |  |  | #ifdef RECYCLE_BIG_ALLOCATIONS
 | 
					
						
							| 
									
										
										
										
											2019-05-14 16:38:06 +02:00
										 |  |  |         if (auto* allocator = big_allocator_for_size(real_size)) { | 
					
						
							|  |  |  |             if (!allocator->blocks.is_empty()) { | 
					
						
							|  |  |  |                 auto* block = allocator->blocks.take_last(); | 
					
						
							| 
									
										
										
										
											2019-12-17 23:01:19 +03:00
										 |  |  |                 int rc = madvise(block, real_size, MADV_SET_NONVOLATILE); | 
					
						
							|  |  |  |                 bool this_block_was_purged = rc == 1; | 
					
						
							|  |  |  |                 if (rc < 0) { | 
					
						
							|  |  |  |                     perror("madvise"); | 
					
						
							|  |  |  |                     ASSERT_NOT_REACHED(); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2019-12-02 19:59:53 +01:00
										 |  |  |                 if (mprotect(block, real_size, PROT_READ | PROT_WRITE) < 0) { | 
					
						
							|  |  |  |                     perror("mprotect"); | 
					
						
							|  |  |  |                     ASSERT_NOT_REACHED(); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2019-12-17 23:01:19 +03:00
										 |  |  |                 if (this_block_was_purged) | 
					
						
							|  |  |  |                     new (block) BigAllocationBlock(real_size); | 
					
						
							| 
									
										
										
										
											2020-07-16 20:25:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 ue_notify_malloc(&block->m_slot[0], size); | 
					
						
							| 
									
										
										
										
											2019-05-14 16:38:06 +02:00
										 |  |  |                 return &block->m_slot[0]; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-18 22:26:01 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-05-30 02:37:40 +02:00
										 |  |  |         auto* block = (BigAllocationBlock*)os_alloc(real_size, "malloc: BigAllocationBlock"); | 
					
						
							| 
									
										
										
										
											2019-05-02 02:58:06 +02:00
										 |  |  |         new (block) BigAllocationBlock(real_size); | 
					
						
							| 
									
										
										
										
											2020-07-16 20:25:51 +02:00
										 |  |  |         ue_notify_malloc(&block->m_slot[0], size); | 
					
						
							| 
									
										
										
										
											2019-05-02 02:58:06 +02:00
										 |  |  |         return &block->m_slot[0]; | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ChunkedBlock* block = nullptr; | 
					
						
							| 
									
										
										
										
											2019-05-02 15:56:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 17:06:05 +02:00
										 |  |  |     for (block = allocator->usable_blocks.head(); block; block = block->next()) { | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  |         if (block->free_chunks()) | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-17 22:55:19 +03:00
										 |  |  |     if (!block && allocator->empty_block_count) { | 
					
						
							|  |  |  |         block = allocator->empty_blocks[--allocator->empty_block_count]; | 
					
						
							| 
									
										
										
										
											2020-02-16 14:07:48 +01:00
										 |  |  |         int rc = madvise(block, block_size, MADV_SET_NONVOLATILE); | 
					
						
							| 
									
										
										
										
											2019-12-17 23:01:19 +03:00
										 |  |  |         bool this_block_was_purged = rc == 1; | 
					
						
							|  |  |  |         if (rc < 0) { | 
					
						
							|  |  |  |             perror("madvise"); | 
					
						
							|  |  |  |             ASSERT_NOT_REACHED(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-02-16 14:07:48 +01:00
										 |  |  |         rc = mprotect(block, block_size, PROT_READ | PROT_WRITE); | 
					
						
							| 
									
										
										
										
											2019-12-02 18:51:57 +01:00
										 |  |  |         if (rc < 0) { | 
					
						
							|  |  |  |             perror("mprotect"); | 
					
						
							|  |  |  |             ASSERT_NOT_REACHED(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-12-17 23:01:19 +03:00
										 |  |  |         if (this_block_was_purged) | 
					
						
							|  |  |  |             new (block) ChunkedBlock(good_size); | 
					
						
							| 
									
										
										
										
											2019-12-02 18:51:57 +01:00
										 |  |  |         allocator->usable_blocks.append(block); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  |     if (!block) { | 
					
						
							|  |  |  |         char buffer[64]; | 
					
						
							| 
									
										
										
										
											2019-06-22 16:30:32 +02:00
										 |  |  |         snprintf(buffer, sizeof(buffer), "malloc: ChunkedBlock(%zu)", good_size); | 
					
						
							| 
									
										
										
										
											2020-02-16 14:07:48 +01:00
										 |  |  |         block = (ChunkedBlock*)os_alloc(block_size, buffer); | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  |         new (block) ChunkedBlock(good_size); | 
					
						
							| 
									
										
										
										
											2019-05-02 17:06:05 +02:00
										 |  |  |         allocator->usable_blocks.append(block); | 
					
						
							| 
									
										
										
										
											2019-05-02 15:56:52 +02:00
										 |  |  |         ++allocator->block_count; | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     --block->m_free_chunks; | 
					
						
							|  |  |  |     void* ptr = block->m_freelist; | 
					
						
							|  |  |  |     block->m_freelist = block->m_freelist->next; | 
					
						
							| 
									
										
										
										
											2019-05-02 17:06:05 +02:00
										 |  |  |     if (block->is_full()) { | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | #ifdef MALLOC_DEBUG
 | 
					
						
							| 
									
										
										
										
											2019-09-29 21:02:13 +02:00
										 |  |  |         dbgprintf("Block %p is now full in size class %zu\n", block, good_size); | 
					
						
							| 
									
										
										
										
											2019-05-02 17:06:05 +02:00
										 |  |  | #endif
 | 
					
						
							|  |  |  |         allocator->usable_blocks.remove(block); | 
					
						
							|  |  |  |         allocator->full_blocks.append(block); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #ifdef MALLOC_DEBUG
 | 
					
						
							| 
									
										
										
										
											2019-09-29 21:02:13 +02:00
										 |  |  |     dbgprintf("LibC: allocated %p (chunk in block %p, size %zu)\n", ptr, block, block->bytes_per_chunk()); | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-07-15 21:54:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  |     if (s_scrub_malloc) | 
					
						
							|  |  |  |         memset(ptr, MALLOC_SCRUB_BYTE, block->m_size); | 
					
						
							| 
									
										
										
										
											2020-07-21 16:38:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ue_notify_malloc(ptr, size); | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  |     return ptr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 20:28:29 +01:00
										 |  |  | static void free_impl(void* ptr) | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-07-25 15:23:29 +02:00
										 |  |  |     ScopedValueRollback rollback(errno); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  |     if (!ptr) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-13 18:36:19 +02:00
										 |  |  |     LOCKER(malloc_lock()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-08 10:36:51 +01:00
										 |  |  |     void* block_base = (void*)((FlatPtr)ptr & block_mask); | 
					
						
							| 
									
										
										
										
											2020-02-16 14:07:48 +01:00
										 |  |  |     size_t magic = *(size_t*)block_base; | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (magic == MAGIC_BIGALLOC_HEADER) { | 
					
						
							| 
									
										
										
										
											2020-02-16 14:07:48 +01:00
										 |  |  |         auto* block = (BigAllocationBlock*)block_base; | 
					
						
							| 
									
										
										
										
											2019-05-18 22:26:01 +02:00
										 |  |  | #ifdef RECYCLE_BIG_ALLOCATIONS
 | 
					
						
							| 
									
										
										
										
											2019-05-14 16:38:06 +02:00
										 |  |  |         if (auto* allocator = big_allocator_for_size(block->m_size)) { | 
					
						
							|  |  |  |             if (allocator->blocks.size() < number_of_big_blocks_to_keep_around_per_size_class) { | 
					
						
							|  |  |  |                 allocator->blocks.append(block); | 
					
						
							| 
									
										
										
										
											2020-03-06 10:52:36 +01:00
										 |  |  |                 size_t this_block_size = block->m_size; | 
					
						
							|  |  |  |                 if (mprotect(block, this_block_size, PROT_NONE) < 0) { | 
					
						
							| 
									
										
										
										
											2019-12-02 19:59:53 +01:00
										 |  |  |                     perror("mprotect"); | 
					
						
							|  |  |  |                     ASSERT_NOT_REACHED(); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2020-03-06 10:52:36 +01:00
										 |  |  |                 if (madvise(block, this_block_size, MADV_SET_VOLATILE) != 0) { | 
					
						
							| 
									
										
										
										
											2019-12-17 23:01:19 +03:00
										 |  |  |                     perror("madvise"); | 
					
						
							|  |  |  |                     ASSERT_NOT_REACHED(); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2019-05-14 16:38:06 +02:00
										 |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-18 22:26:01 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-05-02 15:56:52 +02:00
										 |  |  |         os_free(block, block->m_size); | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert(magic == MAGIC_PAGE_HEADER); | 
					
						
							| 
									
										
										
										
											2020-02-16 14:07:48 +01:00
										 |  |  |     auto* block = (ChunkedBlock*)block_base; | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef MALLOC_DEBUG
 | 
					
						
							| 
									
										
										
										
											2019-07-17 12:54:18 +02:00
										 |  |  |     dbgprintf("LibC: freeing %p in allocator %p (size=%u, used=%u)\n", ptr, block, block->bytes_per_chunk(), block->used_chunks()); | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (s_scrub_free) | 
					
						
							| 
									
										
										
										
											2019-05-02 15:56:52 +02:00
										 |  |  |         memset(ptr, FREE_SCRUB_BYTE, block->bytes_per_chunk()); | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     auto* entry = (FreelistEntry*)ptr; | 
					
						
							| 
									
										
										
										
											2019-05-02 15:56:52 +02:00
										 |  |  |     entry->next = block->m_freelist; | 
					
						
							|  |  |  |     block->m_freelist = entry; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 17:06:05 +02:00
										 |  |  |     if (block->is_full()) { | 
					
						
							|  |  |  |         size_t good_size; | 
					
						
							|  |  |  |         auto* allocator = allocator_for_size(block->m_size, good_size); | 
					
						
							|  |  |  | #ifdef MALLOC_DEBUG
 | 
					
						
							|  |  |  |         dbgprintf("Block %p no longer full in size class %u\n", block, good_size); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |         allocator->full_blocks.remove(block); | 
					
						
							|  |  |  |         allocator->usable_blocks.prepend(block); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 15:56:52 +02:00
										 |  |  |     ++block->m_free_chunks; | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 15:56:52 +02:00
										 |  |  |     if (!block->used_chunks()) { | 
					
						
							|  |  |  |         size_t good_size; | 
					
						
							|  |  |  |         auto* allocator = allocator_for_size(block->m_size, good_size); | 
					
						
							|  |  |  |         if (allocator->block_count < number_of_chunked_blocks_to_keep_around_per_size_class) { | 
					
						
							|  |  |  | #ifdef MALLOC_DEBUG
 | 
					
						
							|  |  |  |             dbgprintf("Keeping block %p around for size class %u\n", block, good_size); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-12-02 18:51:57 +01:00
										 |  |  |             allocator->usable_blocks.remove(block); | 
					
						
							| 
									
										
										
										
											2019-12-17 22:55:19 +03:00
										 |  |  |             allocator->empty_blocks[allocator->empty_block_count++] = block; | 
					
						
							| 
									
										
										
										
											2020-02-16 14:07:48 +01:00
										 |  |  |             mprotect(block, block_size, PROT_NONE); | 
					
						
							|  |  |  |             madvise(block, block_size, MADV_SET_VOLATILE); | 
					
						
							| 
									
										
										
										
											2019-05-02 15:56:52 +02:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | #ifdef MALLOC_DEBUG
 | 
					
						
							|  |  |  |         dbgprintf("Releasing block %p for size class %u\n", block, good_size); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-05-02 17:06:05 +02:00
										 |  |  |         allocator->usable_blocks.remove(block); | 
					
						
							| 
									
										
										
										
											2019-05-02 15:56:52 +02:00
										 |  |  |         --allocator->block_count; | 
					
						
							| 
									
										
										
										
											2020-02-16 14:07:48 +01:00
										 |  |  |         os_free(block, block_size); | 
					
						
							| 
									
										
										
										
											2019-05-02 15:56:52 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 20:55:46 +02:00
										 |  |  | [[gnu::flatten]] void* malloc(size_t size) | 
					
						
							| 
									
										
										
										
											2020-02-02 20:28:29 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     void* ptr = malloc_impl(size); | 
					
						
							|  |  |  |     if (s_profiling) | 
					
						
							| 
									
										
										
										
											2020-03-08 10:36:51 +01:00
										 |  |  |         perf_event(PERF_EVENT_MALLOC, size, reinterpret_cast<FlatPtr>(ptr)); | 
					
						
							| 
									
										
										
										
											2020-02-02 20:28:29 +01:00
										 |  |  |     return ptr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 20:55:46 +02:00
										 |  |  | [[gnu::flatten]] void free(void* ptr) | 
					
						
							| 
									
										
										
										
											2020-02-02 20:28:29 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     if (s_profiling) | 
					
						
							| 
									
										
										
										
											2020-03-08 10:36:51 +01:00
										 |  |  |         perf_event(PERF_EVENT_FREE, reinterpret_cast<FlatPtr>(ptr), 0); | 
					
						
							| 
									
										
										
										
											2020-02-02 20:28:29 +01:00
										 |  |  |     free_impl(ptr); | 
					
						
							| 
									
										
										
										
											2020-07-16 20:25:51 +02:00
										 |  |  |     ue_notify_free(ptr); | 
					
						
							| 
									
										
										
										
											2020-02-02 20:28:29 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | void* calloc(size_t count, size_t size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     size_t new_size = count * size; | 
					
						
							|  |  |  |     auto* ptr = malloc(new_size); | 
					
						
							| 
									
										
										
										
											2020-08-13 20:14:28 +03:00
										 |  |  |     if (ptr) | 
					
						
							|  |  |  |         memset(ptr, 0, new_size); | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  |     return ptr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-30 02:37:40 +02:00
										 |  |  | size_t malloc_size(void* ptr) | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (!ptr) | 
					
						
							| 
									
										
										
										
											2019-05-30 02:37:40 +02:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2019-07-13 18:36:19 +02:00
										 |  |  |     LOCKER(malloc_lock()); | 
					
						
							| 
									
										
										
										
											2020-03-08 10:36:51 +01:00
										 |  |  |     void* page_base = (void*)((FlatPtr)ptr & block_mask); | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  |     auto* header = (const CommonHeader*)page_base; | 
					
						
							| 
									
										
										
										
											2019-05-30 02:37:40 +02:00
										 |  |  |     auto size = header->m_size; | 
					
						
							|  |  |  |     if (header->m_magic == MAGIC_BIGALLOC_HEADER) | 
					
						
							|  |  |  |         size -= sizeof(CommonHeader); | 
					
						
							|  |  |  |     return size; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-30 02:37:40 +02:00
										 |  |  | void* realloc(void* ptr, size_t size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!ptr) | 
					
						
							|  |  |  |         return malloc(size); | 
					
						
							| 
									
										
										
										
											2020-08-13 20:14:28 +03:00
										 |  |  |     if (!size) | 
					
						
							|  |  |  |         return nullptr; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-13 18:36:19 +02:00
										 |  |  |     LOCKER(malloc_lock()); | 
					
						
							| 
									
										
										
										
											2019-05-30 02:37:40 +02:00
										 |  |  |     auto existing_allocation_size = malloc_size(ptr); | 
					
						
							|  |  |  |     if (size <= existing_allocation_size) | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  |         return ptr; | 
					
						
							|  |  |  |     auto* new_ptr = malloc(size); | 
					
						
							| 
									
										
										
										
											2020-08-13 20:14:28 +03:00
										 |  |  |     if (new_ptr) { | 
					
						
							|  |  |  |         memcpy(new_ptr, ptr, min(existing_allocation_size, size)); | 
					
						
							|  |  |  |         free(ptr); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  |     return new_ptr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void __malloc_init() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-08-25 23:51:27 +03:00
										 |  |  |     new (&malloc_lock()) LibThread::Lock(); | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  |     if (getenv("LIBC_NOSCRUB_MALLOC")) | 
					
						
							|  |  |  |         s_scrub_malloc = false; | 
					
						
							|  |  |  |     if (getenv("LIBC_NOSCRUB_FREE")) | 
					
						
							|  |  |  |         s_scrub_free = false; | 
					
						
							|  |  |  |     if (getenv("LIBC_LOG_MALLOC")) | 
					
						
							|  |  |  |         s_log_malloc = true; | 
					
						
							| 
									
										
										
										
											2020-02-02 20:28:29 +01:00
										 |  |  |     if (getenv("LIBC_PROFILE_MALLOC")) | 
					
						
							|  |  |  |         s_profiling = true; | 
					
						
							| 
									
										
										
										
											2019-12-22 02:06:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (size_t i = 0; i < num_size_classes; ++i) { | 
					
						
							| 
									
										
										
										
											2020-02-18 11:43:21 +03:00
										 |  |  |         new (&allocators()[i]) Allocator(); | 
					
						
							|  |  |  |         allocators()[i].size = size_classes[i]; | 
					
						
							| 
									
										
										
										
											2019-12-22 02:06:50 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-18 11:43:21 +03:00
										 |  |  |     new (&big_allocators()[0])(BigAllocator); | 
					
						
							| 
									
										
										
										
											2019-05-02 02:23:39 +02:00
										 |  |  | } | 
					
						
							|  |  |  | } |