malloc.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/BuiltinWrappers.h>
  7. #include <AK/Debug.h>
  8. #include <AK/ScopedValueRollback.h>
  9. #include <AK/Vector.h>
  10. #include <LibELF/AuxiliaryVector.h>
  11. #include <assert.h>
  12. #include <errno.h>
  13. #include <mallocdefs.h>
  14. #include <pthread.h>
  15. #include <serenity.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <sys/internals.h>
  20. #include <sys/mman.h>
  21. #include <syscall.h>
  22. class PthreadMutexLocker {
  23. public:
  24. ALWAYS_INLINE explicit PthreadMutexLocker(pthread_mutex_t& mutex)
  25. : m_mutex(mutex)
  26. {
  27. lock();
  28. __heap_is_stable = false;
  29. }
  30. ALWAYS_INLINE ~PthreadMutexLocker()
  31. {
  32. __heap_is_stable = true;
  33. unlock();
  34. }
  35. ALWAYS_INLINE void lock() { pthread_mutex_lock(&m_mutex); }
  36. ALWAYS_INLINE void unlock() { pthread_mutex_unlock(&m_mutex); }
  37. private:
  38. pthread_mutex_t& m_mutex;
  39. };
  40. #define RECYCLE_BIG_ALLOCATIONS
  41. static pthread_mutex_t s_malloc_mutex = PTHREAD_MUTEX_INITIALIZER;
  42. bool __heap_is_stable = true;
  43. constexpr size_t number_of_hot_chunked_blocks_to_keep_around = 16;
  44. constexpr size_t number_of_cold_chunked_blocks_to_keep_around = 16;
  45. constexpr size_t number_of_big_blocks_to_keep_around_per_size_class = 8;
  46. static bool s_log_malloc = false;
  47. static bool s_scrub_malloc = true;
  48. static bool s_scrub_free = true;
  49. static bool s_profiling = false;
  50. static bool s_in_userspace_emulator = false;
  51. ALWAYS_INLINE static void ue_notify_malloc(const void* ptr, size_t size)
  52. {
  53. if (s_in_userspace_emulator)
  54. syscall(SC_emuctl, 1, size, (FlatPtr)ptr);
  55. }
  56. ALWAYS_INLINE static void ue_notify_free(const void* ptr)
  57. {
  58. if (s_in_userspace_emulator)
  59. syscall(SC_emuctl, 2, (FlatPtr)ptr, 0);
  60. }
  61. ALWAYS_INLINE static void ue_notify_realloc(const void* ptr, size_t size)
  62. {
  63. if (s_in_userspace_emulator)
  64. syscall(SC_emuctl, 3, size, (FlatPtr)ptr);
  65. }
  66. ALWAYS_INLINE static void ue_notify_chunk_size_changed(const void* block, size_t chunk_size)
  67. {
  68. if (s_in_userspace_emulator)
  69. syscall(SC_emuctl, 4, chunk_size, (FlatPtr)block);
  70. }
  71. struct MemoryAuditingSuppressor {
  72. ALWAYS_INLINE MemoryAuditingSuppressor()
  73. {
  74. if (s_in_userspace_emulator)
  75. syscall(SC_emuctl, 7);
  76. }
  77. ALWAYS_INLINE ~MemoryAuditingSuppressor()
  78. {
  79. if (s_in_userspace_emulator)
  80. syscall(SC_emuctl, 8);
  81. }
  82. };
  83. struct MallocStats {
  84. size_t number_of_malloc_calls;
  85. size_t number_of_big_allocator_hits;
  86. size_t number_of_big_allocator_purge_hits;
  87. size_t number_of_big_allocs;
  88. size_t number_of_hot_empty_block_hits;
  89. size_t number_of_cold_empty_block_hits;
  90. size_t number_of_cold_empty_block_purge_hits;
  91. size_t number_of_block_allocs;
  92. size_t number_of_blocks_full;
  93. size_t number_of_free_calls;
  94. size_t number_of_big_allocator_keeps;
  95. size_t number_of_big_allocator_frees;
  96. size_t number_of_freed_full_blocks;
  97. size_t number_of_hot_keeps;
  98. size_t number_of_cold_keeps;
  99. size_t number_of_frees;
  100. };
  101. static MallocStats g_malloc_stats = {};
  102. static size_t s_hot_empty_block_count { 0 };
  103. static ChunkedBlock* s_hot_empty_blocks[number_of_hot_chunked_blocks_to_keep_around] { nullptr };
  104. static size_t s_cold_empty_block_count { 0 };
  105. static ChunkedBlock* s_cold_empty_blocks[number_of_cold_chunked_blocks_to_keep_around] { nullptr };
  106. struct Allocator {
  107. size_t size { 0 };
  108. size_t block_count { 0 };
  109. ChunkedBlock::List usable_blocks;
  110. ChunkedBlock::List full_blocks;
  111. };
  112. struct BigAllocator {
  113. Vector<BigAllocationBlock*, number_of_big_blocks_to_keep_around_per_size_class> blocks;
  114. };
  115. // Allocators will be initialized in __malloc_init.
  116. // We can not rely on global constructors to initialize them,
  117. // because they must be initialized before other global constructors
  118. // are run. Similarly, we can not allow global destructors to destruct
  119. // them. We could have used AK::NeverDestoyed to prevent the latter,
  120. // but it would have not helped with the former.
  121. alignas(Allocator) static u8 g_allocators_storage[sizeof(Allocator) * num_size_classes];
  122. alignas(BigAllocator) static u8 g_big_allocators_storage[sizeof(BigAllocator)];
  123. static inline Allocator (&allocators())[num_size_classes]
  124. {
  125. return reinterpret_cast<Allocator(&)[num_size_classes]>(g_allocators_storage);
  126. }
  127. static inline BigAllocator (&big_allocators())[1]
  128. {
  129. return reinterpret_cast<BigAllocator(&)[1]>(g_big_allocators_storage);
  130. }
  131. static Allocator* allocator_for_size(size_t size, size_t& good_size)
  132. {
  133. for (size_t i = 0; size_classes[i]; ++i) {
  134. if (size <= size_classes[i]) {
  135. good_size = size_classes[i];
  136. return &allocators()[i];
  137. }
  138. }
  139. good_size = PAGE_ROUND_UP(size);
  140. return nullptr;
  141. }
  142. #ifdef RECYCLE_BIG_ALLOCATIONS
  143. static BigAllocator* big_allocator_for_size(size_t size)
  144. {
  145. if (size == 65536)
  146. return &big_allocators()[0];
  147. return nullptr;
  148. }
  149. #endif
  150. extern "C" {
  151. static void* os_alloc(size_t size, const char* name)
  152. {
  153. int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_PURGEABLE;
  154. #if ARCH(X86_64)
  155. flags |= MAP_RANDOMIZED;
  156. #endif
  157. auto* ptr = serenity_mmap(nullptr, size, PROT_READ | PROT_WRITE, flags, 0, 0, ChunkedBlock::block_size, name);
  158. VERIFY(ptr != nullptr);
  159. if (ptr == MAP_FAILED) {
  160. errno = ENOMEM;
  161. return nullptr;
  162. }
  163. return ptr;
  164. }
  165. static void os_free(void* ptr, size_t size)
  166. {
  167. int rc = munmap(ptr, size);
  168. assert(rc == 0);
  169. }
  170. enum class CallerWillInitializeMemory {
  171. No,
  172. Yes,
  173. };
  174. #ifndef NO_TLS
  175. // HACK: This is a __thread - marked thread-local variable. If we initialize it globally here, VERY weird errors happen.
  176. // The initialization happens in __malloc_init() and pthread_create_helper().
  177. __thread bool s_allocation_enabled;
  178. #endif
  179. static void* malloc_impl(size_t size, CallerWillInitializeMemory caller_will_initialize_memory)
  180. {
  181. #ifndef NO_TLS
  182. VERIFY(s_allocation_enabled);
  183. #endif
  184. if (s_log_malloc)
  185. dbgln("LibC: malloc({})", size);
  186. if (!size) {
  187. // Legally we could just return a null pointer here, but this is more
  188. // compatible with existing software.
  189. size = 1;
  190. }
  191. g_malloc_stats.number_of_malloc_calls++;
  192. size_t good_size;
  193. auto* allocator = allocator_for_size(size, good_size);
  194. PthreadMutexLocker locker(s_malloc_mutex);
  195. if (!allocator) {
  196. size_t real_size = round_up_to_power_of_two(sizeof(BigAllocationBlock) + size, ChunkedBlock::block_size);
  197. if (real_size < size) {
  198. dbgln_if(MALLOC_DEBUG, "LibC: Detected overflow trying to do big allocation of size {} for {}", real_size, size);
  199. errno = ENOMEM;
  200. return nullptr;
  201. }
  202. #ifdef RECYCLE_BIG_ALLOCATIONS
  203. if (auto* allocator = big_allocator_for_size(real_size)) {
  204. if (!allocator->blocks.is_empty()) {
  205. g_malloc_stats.number_of_big_allocator_hits++;
  206. auto* block = allocator->blocks.take_last();
  207. int rc = madvise(block, real_size, MADV_SET_NONVOLATILE);
  208. bool this_block_was_purged = rc == 1;
  209. if (rc < 0) {
  210. perror("madvise");
  211. VERIFY_NOT_REACHED();
  212. }
  213. if (mprotect(block, real_size, PROT_READ | PROT_WRITE) < 0) {
  214. perror("mprotect");
  215. VERIFY_NOT_REACHED();
  216. }
  217. if (this_block_was_purged) {
  218. g_malloc_stats.number_of_big_allocator_purge_hits++;
  219. new (block) BigAllocationBlock(real_size);
  220. }
  221. ue_notify_malloc(&block->m_slot[0], size);
  222. return &block->m_slot[0];
  223. }
  224. }
  225. #endif
  226. auto* block = (BigAllocationBlock*)os_alloc(real_size, "malloc: BigAllocationBlock");
  227. if (block == nullptr) {
  228. dbgln_if(MALLOC_DEBUG, "LibC: Failed to do big allocation of size {} for {}", real_size, size);
  229. return nullptr;
  230. }
  231. g_malloc_stats.number_of_big_allocs++;
  232. new (block) BigAllocationBlock(real_size);
  233. ue_notify_malloc(&block->m_slot[0], size);
  234. return &block->m_slot[0];
  235. }
  236. ChunkedBlock* block = nullptr;
  237. for (auto& current : allocator->usable_blocks) {
  238. if (current.free_chunks()) {
  239. block = &current;
  240. break;
  241. }
  242. }
  243. if (!block && s_hot_empty_block_count) {
  244. g_malloc_stats.number_of_hot_empty_block_hits++;
  245. block = s_hot_empty_blocks[--s_hot_empty_block_count];
  246. if (block->m_size != good_size) {
  247. new (block) ChunkedBlock(good_size);
  248. ue_notify_chunk_size_changed(block, good_size);
  249. char buffer[64];
  250. snprintf(buffer, sizeof(buffer), "malloc: ChunkedBlock(%zu)", good_size);
  251. set_mmap_name(block, ChunkedBlock::block_size, buffer);
  252. }
  253. allocator->usable_blocks.append(*block);
  254. }
  255. if (!block && s_cold_empty_block_count) {
  256. g_malloc_stats.number_of_cold_empty_block_hits++;
  257. block = s_cold_empty_blocks[--s_cold_empty_block_count];
  258. int rc = madvise(block, ChunkedBlock::block_size, MADV_SET_NONVOLATILE);
  259. bool this_block_was_purged = rc == 1;
  260. if (rc < 0) {
  261. perror("madvise");
  262. VERIFY_NOT_REACHED();
  263. }
  264. rc = mprotect(block, ChunkedBlock::block_size, PROT_READ | PROT_WRITE);
  265. if (rc < 0) {
  266. perror("mprotect");
  267. VERIFY_NOT_REACHED();
  268. }
  269. if (this_block_was_purged || block->m_size != good_size) {
  270. if (this_block_was_purged)
  271. g_malloc_stats.number_of_cold_empty_block_purge_hits++;
  272. new (block) ChunkedBlock(good_size);
  273. ue_notify_chunk_size_changed(block, good_size);
  274. }
  275. allocator->usable_blocks.append(*block);
  276. }
  277. if (!block) {
  278. g_malloc_stats.number_of_block_allocs++;
  279. char buffer[64];
  280. snprintf(buffer, sizeof(buffer), "malloc: ChunkedBlock(%zu)", good_size);
  281. block = (ChunkedBlock*)os_alloc(ChunkedBlock::block_size, buffer);
  282. if (block == nullptr) {
  283. return nullptr;
  284. }
  285. new (block) ChunkedBlock(good_size);
  286. allocator->usable_blocks.append(*block);
  287. ++allocator->block_count;
  288. }
  289. --block->m_free_chunks;
  290. void* ptr = block->m_freelist;
  291. if (ptr) {
  292. block->m_freelist = block->m_freelist->next;
  293. } else {
  294. ptr = block->m_slot + block->m_next_lazy_freelist_index * block->m_size;
  295. block->m_next_lazy_freelist_index++;
  296. }
  297. VERIFY(ptr);
  298. if (block->is_full()) {
  299. g_malloc_stats.number_of_blocks_full++;
  300. dbgln_if(MALLOC_DEBUG, "Block {:p} is now full in size class {}", block, good_size);
  301. allocator->usable_blocks.remove(*block);
  302. allocator->full_blocks.append(*block);
  303. }
  304. dbgln_if(MALLOC_DEBUG, "LibC: allocated {:p} (chunk in block {:p}, size {})", ptr, block, block->bytes_per_chunk());
  305. if (s_scrub_malloc && caller_will_initialize_memory == CallerWillInitializeMemory::No)
  306. memset(ptr, MALLOC_SCRUB_BYTE, block->m_size);
  307. ue_notify_malloc(ptr, size);
  308. return ptr;
  309. }
  310. static void free_impl(void* ptr)
  311. {
  312. #ifndef NO_TLS
  313. VERIFY(s_allocation_enabled);
  314. #endif
  315. ScopedValueRollback rollback(errno);
  316. if (!ptr)
  317. return;
  318. g_malloc_stats.number_of_free_calls++;
  319. void* block_base = (void*)((FlatPtr)ptr & ChunkedBlock::ChunkedBlock::block_mask);
  320. size_t magic = *(size_t*)block_base;
  321. PthreadMutexLocker locker(s_malloc_mutex);
  322. if (magic == MAGIC_BIGALLOC_HEADER) {
  323. auto* block = (BigAllocationBlock*)block_base;
  324. #ifdef RECYCLE_BIG_ALLOCATIONS
  325. if (auto* allocator = big_allocator_for_size(block->m_size)) {
  326. if (allocator->blocks.size() < number_of_big_blocks_to_keep_around_per_size_class) {
  327. g_malloc_stats.number_of_big_allocator_keeps++;
  328. allocator->blocks.append(block);
  329. size_t this_block_size = block->m_size;
  330. if (mprotect(block, this_block_size, PROT_NONE) < 0) {
  331. perror("mprotect");
  332. VERIFY_NOT_REACHED();
  333. }
  334. if (madvise(block, this_block_size, MADV_SET_VOLATILE) != 0) {
  335. perror("madvise");
  336. VERIFY_NOT_REACHED();
  337. }
  338. return;
  339. }
  340. }
  341. #endif
  342. g_malloc_stats.number_of_big_allocator_frees++;
  343. os_free(block, block->m_size);
  344. return;
  345. }
  346. assert(magic == MAGIC_PAGE_HEADER);
  347. auto* block = (ChunkedBlock*)block_base;
  348. dbgln_if(MALLOC_DEBUG, "LibC: freeing {:p} in allocator {:p} (size={}, used={})", ptr, block, block->bytes_per_chunk(), block->used_chunks());
  349. if (s_scrub_free)
  350. memset(ptr, FREE_SCRUB_BYTE, block->bytes_per_chunk());
  351. auto* entry = (FreelistEntry*)ptr;
  352. entry->next = block->m_freelist;
  353. block->m_freelist = entry;
  354. if (block->is_full()) {
  355. size_t good_size;
  356. auto* allocator = allocator_for_size(block->m_size, good_size);
  357. dbgln_if(MALLOC_DEBUG, "Block {:p} no longer full in size class {}", block, good_size);
  358. g_malloc_stats.number_of_freed_full_blocks++;
  359. allocator->full_blocks.remove(*block);
  360. allocator->usable_blocks.prepend(*block);
  361. }
  362. ++block->m_free_chunks;
  363. if (!block->used_chunks()) {
  364. size_t good_size;
  365. auto* allocator = allocator_for_size(block->m_size, good_size);
  366. if (s_hot_empty_block_count < number_of_hot_chunked_blocks_to_keep_around) {
  367. dbgln_if(MALLOC_DEBUG, "Keeping hot block {:p} around", block);
  368. g_malloc_stats.number_of_hot_keeps++;
  369. allocator->usable_blocks.remove(*block);
  370. s_hot_empty_blocks[s_hot_empty_block_count++] = block;
  371. return;
  372. }
  373. if (s_cold_empty_block_count < number_of_cold_chunked_blocks_to_keep_around) {
  374. dbgln_if(MALLOC_DEBUG, "Keeping cold block {:p} around", block);
  375. g_malloc_stats.number_of_cold_keeps++;
  376. allocator->usable_blocks.remove(*block);
  377. s_cold_empty_blocks[s_cold_empty_block_count++] = block;
  378. mprotect(block, ChunkedBlock::block_size, PROT_NONE);
  379. madvise(block, ChunkedBlock::block_size, MADV_SET_VOLATILE);
  380. return;
  381. }
  382. dbgln_if(MALLOC_DEBUG, "Releasing block {:p} for size class {}", block, good_size);
  383. g_malloc_stats.number_of_frees++;
  384. allocator->usable_blocks.remove(*block);
  385. --allocator->block_count;
  386. os_free(block, ChunkedBlock::block_size);
  387. }
  388. }
  389. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/malloc.html
  390. void* malloc(size_t size)
  391. {
  392. MemoryAuditingSuppressor suppressor;
  393. void* ptr = malloc_impl(size, CallerWillInitializeMemory::No);
  394. if (s_profiling)
  395. perf_event(PERF_EVENT_MALLOC, size, reinterpret_cast<FlatPtr>(ptr));
  396. return ptr;
  397. }
  398. // This is a Microsoft extension, and is not found on other Unix-like systems.
  399. // FIXME: Implement aligned_alloc() instead
  400. //
  401. // This is used in libc++ to implement C++17 aligned new/delete.
  402. //
  403. // Both Unix-y alternatives to _aligned_malloc(), the C11 aligned_alloc() and
  404. // posix_memalign() say that the resulting pointer can be deallocated with
  405. // regular free(), which means that the allocator has to keep track of the
  406. // requested alignments. By contrast, _aligned_malloc() is paired with
  407. // _aligned_free(), so it can be easily implemented on top of malloc().
  408. void* _aligned_malloc(size_t size, size_t alignment)
  409. {
  410. if (popcount(alignment) != 1) {
  411. errno = EINVAL;
  412. return nullptr;
  413. }
  414. alignment = max(alignment, sizeof(void*));
  415. if (Checked<size_t>::addition_would_overflow(size, alignment)) {
  416. errno = ENOMEM;
  417. return nullptr;
  418. }
  419. void* ptr = malloc(size + alignment);
  420. if (!ptr) {
  421. errno = ENOMEM;
  422. return nullptr;
  423. }
  424. auto aligned_ptr = (void*)(((FlatPtr)ptr + alignment) & ~(alignment - 1));
  425. ((void**)aligned_ptr)[-1] = ptr;
  426. return aligned_ptr;
  427. }
  428. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/free.html
  429. void free(void* ptr)
  430. {
  431. MemoryAuditingSuppressor suppressor;
  432. if (s_profiling)
  433. perf_event(PERF_EVENT_FREE, reinterpret_cast<FlatPtr>(ptr), 0);
  434. ue_notify_free(ptr);
  435. free_impl(ptr);
  436. }
  437. void _aligned_free(void* ptr)
  438. {
  439. if (ptr)
  440. free(((void**)ptr)[-1]);
  441. }
  442. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/calloc.html
  443. void* calloc(size_t count, size_t size)
  444. {
  445. MemoryAuditingSuppressor suppressor;
  446. if (Checked<size_t>::multiplication_would_overflow(count, size)) {
  447. errno = ENOMEM;
  448. return nullptr;
  449. }
  450. size_t new_size = count * size;
  451. auto* ptr = malloc_impl(new_size, CallerWillInitializeMemory::Yes);
  452. if (ptr)
  453. memset(ptr, 0, new_size);
  454. return ptr;
  455. }
  456. size_t malloc_size(void const* ptr)
  457. {
  458. MemoryAuditingSuppressor suppressor;
  459. if (!ptr)
  460. return 0;
  461. void* page_base = (void*)((FlatPtr)ptr & ChunkedBlock::block_mask);
  462. auto* header = (const CommonHeader*)page_base;
  463. auto size = header->m_size;
  464. if (header->m_magic == MAGIC_BIGALLOC_HEADER)
  465. size -= sizeof(CommonHeader);
  466. else
  467. VERIFY(header->m_magic == MAGIC_PAGE_HEADER);
  468. return size;
  469. }
  470. size_t malloc_good_size(size_t size)
  471. {
  472. size_t good_size;
  473. allocator_for_size(size, good_size);
  474. return good_size;
  475. }
  476. void* realloc(void* ptr, size_t size)
  477. {
  478. MemoryAuditingSuppressor suppressor;
  479. if (!ptr)
  480. return malloc(size);
  481. if (!size) {
  482. free(ptr);
  483. return nullptr;
  484. }
  485. auto existing_allocation_size = malloc_size(ptr);
  486. if (size <= existing_allocation_size) {
  487. ue_notify_realloc(ptr, size);
  488. return ptr;
  489. }
  490. auto* new_ptr = malloc(size);
  491. if (new_ptr) {
  492. memcpy(new_ptr, ptr, min(existing_allocation_size, size));
  493. free(ptr);
  494. }
  495. return new_ptr;
  496. }
  497. void __malloc_init()
  498. {
  499. #ifndef NO_TLS
  500. // HACK: This is a __thread - marked thread-local variable. If we initialize it globally, VERY weird errors happen.
  501. // Therefore, we need to do the initialization here and in pthread_create_helper().
  502. s_allocation_enabled = true;
  503. #endif
  504. s_in_userspace_emulator = (int)syscall(SC_emuctl, 0) != -ENOSYS;
  505. if (s_in_userspace_emulator) {
  506. // Don't bother scrubbing memory if we're running in UE since it
  507. // keeps track of heap memory anyway.
  508. s_scrub_malloc = false;
  509. s_scrub_free = false;
  510. }
  511. if (secure_getenv("LIBC_NOSCRUB_MALLOC"))
  512. s_scrub_malloc = false;
  513. if (secure_getenv("LIBC_NOSCRUB_FREE"))
  514. s_scrub_free = false;
  515. if (secure_getenv("LIBC_LOG_MALLOC"))
  516. s_log_malloc = true;
  517. if (secure_getenv("LIBC_PROFILE_MALLOC"))
  518. s_profiling = true;
  519. for (size_t i = 0; i < num_size_classes; ++i) {
  520. new (&allocators()[i]) Allocator();
  521. allocators()[i].size = size_classes[i];
  522. }
  523. new (&big_allocators()[0])(BigAllocator);
  524. }
  525. void serenity_dump_malloc_stats()
  526. {
  527. dbgln("# malloc() calls: {}", g_malloc_stats.number_of_malloc_calls);
  528. dbgln();
  529. dbgln("big alloc hits: {}", g_malloc_stats.number_of_big_allocator_hits);
  530. dbgln("big alloc hits that were purged: {}", g_malloc_stats.number_of_big_allocator_purge_hits);
  531. dbgln("big allocs: {}", g_malloc_stats.number_of_big_allocs);
  532. dbgln();
  533. dbgln("empty hot block hits: {}", g_malloc_stats.number_of_hot_empty_block_hits);
  534. dbgln("empty cold block hits: {}", g_malloc_stats.number_of_cold_empty_block_hits);
  535. dbgln("empty cold block hits that were purged: {}", g_malloc_stats.number_of_cold_empty_block_purge_hits);
  536. dbgln("block allocs: {}", g_malloc_stats.number_of_block_allocs);
  537. dbgln("filled blocks: {}", g_malloc_stats.number_of_blocks_full);
  538. dbgln();
  539. dbgln("# free() calls: {}", g_malloc_stats.number_of_free_calls);
  540. dbgln();
  541. dbgln("big alloc keeps: {}", g_malloc_stats.number_of_big_allocator_keeps);
  542. dbgln("big alloc frees: {}", g_malloc_stats.number_of_big_allocator_frees);
  543. dbgln();
  544. dbgln("full block frees: {}", g_malloc_stats.number_of_freed_full_blocks);
  545. dbgln("number of hot keeps: {}", g_malloc_stats.number_of_hot_keeps);
  546. dbgln("number of cold keeps: {}", g_malloc_stats.number_of_cold_keeps);
  547. dbgln("number of frees: {}", g_malloc_stats.number_of_frees);
  548. }
  549. }