/** * @file mimalloc_test.cpp * @brief mimalloc 动态链接和基础功能验证 * * 测试覆盖: * 1. 基本分配/释放 * 2. 对齐分配 * 3. 线程本地堆 * 4. 跨线程回收 * 5. 统计信息 API */ #include #include #include #include #define TEST_ASSERT(cond, msg) \ if (!(cond)) { \ std::cerr << "✗ FAILED: " << msg << "\n"; \ std::cerr << " at " << __FILE__ << ":" << __LINE__ << "\n"; \ std::terminate(); \ } else { \ std::cout << "✓ PASSED: " << msg << "\n"; \ } void test_basic_alloc() { std::cout << "\n1. Basic Allocation\n"; void* ptr = mi_malloc(1024); TEST_ASSERT(ptr != nullptr, "mi_malloc returned non-null"); TEST_ASSERT(mi_is_in_heap_region(ptr), "Memory in mimalloc heap"); size_t usable = mi_usable_size(ptr); TEST_ASSERT(usable >= 1024, "Usable size >= requested"); std::cout << " Usable size: " << usable << " bytes\n"; mi_free(ptr); } void test_aligned_alloc() { std::cout << "\n2. Aligned Allocation\n"; void* ptr = mi_aligned_alloc(64, 1024); TEST_ASSERT(ptr != nullptr, "mi_aligned_alloc returned non-null"); TEST_ASSERT(reinterpret_cast(ptr) % 64 == 0, "64-byte alignment"); TEST_ASSERT(mi_is_in_heap_region(ptr), "Aligned memory in heap"); mi_free_aligned(ptr, 64); } void test_thread_local() { std::cout << "\n3. Thread-local Heap\n"; mi_heap_t* main_heap = mi_heap_get_default(); TEST_ASSERT(main_heap != nullptr, "Main thread has default heap"); void* main_ptr = mi_heap_malloc(main_heap, 256); TEST_ASSERT(main_ptr != nullptr, "Main heap allocation succeeded"); std::thread t([]() { mi_heap_t* thread_heap = mi_heap_get_default(); void* thread_ptr = mi_heap_malloc(thread_heap, 128); if (thread_ptr && mi_is_in_heap_region(thread_ptr)) { std::cout << " ✓ Thread-local heap works correctly\n"; mi_free(thread_ptr); } }); t.join(); mi_free(main_ptr); } void test_cross_thread_free() { std::cout << "\n4. Cross-thread Free\n"; void* ptr = nullptr; std::thread t1([&ptr]() { ptr = mi_malloc(512); std::cout << " Thread 1 allocated: " << ptr << "\n"; }); t1.join(); if (ptr) { mi_free(ptr); std::cout << " ✓ Cross-thread free succeeded\n"; } } void test_statistics() { std::cout << "\n5. Statistics API\n"; // 进程信息 size_t current_rss, peak_rss, page_faults; mi_process_info(nullptr, nullptr, nullptr, ¤t_rss, &peak_rss, nullptr, nullptr, &page_faults); std::cout << " Current RSS: " << (current_rss / 1024) << " KB\n"; std::cout << " Peak RSS: " << (peak_rss / 1024) << " KB\n"; std::cout << " Page faults: " << page_faults << "\n"; } int main() { SetConsoleOutputCP(CP_UTF8); std::cout << "╔══════════════════════════════════════════════╗\n"; std::cout << "║ MIMALLOC DYNAMIC LINKING TEST SUITE ║\n"; std::cout << "╚══════════════════════════════════════════════╝\n"; std::cout << "Version: " << mi_version() << "\n"; // 预检查 void* test_ptr = mi_malloc(100); if (test_ptr && mi_is_in_heap_region(test_ptr)) { std::cout << "✓ Mimalloc DLL correctly linked\n"; mi_free(test_ptr); } else { std::cerr << "✗ Mimalloc linking failed!\n"; return 1; } try { test_basic_alloc(); test_aligned_alloc(); test_thread_local(); test_cross_thread_free(); test_statistics(); std::cout << "\n╔══════════════════════════════════════════════╗\n"; std::cout << "║ 🎉 ALL TESTS PASSED! 🎉 ║\n"; std::cout << "╚══════════════════════════════════════════════╝\n"; return 0; } catch (...) { std::cerr << "\n✗ EXCEPTION CAUGHT\n"; return 1; } }