File indexing completed on 2024-04-21 05:41:32

0001 /*
0002     SPDX-FileCopyrightText: 2016-2017 Milian Wolff <mail@milianw.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include <iostream>
0008 #include <malloc.h>
0009 #include <unistd.h>
0010 
0011 #include <benchutil.h>
0012 
0013 using namespace std;
0014 
0015 int main()
0016 {
0017     const auto log2_max = 17;
0018     const auto max_steps = log2_max * 2 + 1;
0019     size_t cost[max_steps];
0020     int sizes[max_steps];
0021 
0022     const auto baseline = mallinfo2().uordblks;
0023 
0024     for (int i = 1; i < max_steps; ++i) {
0025         int size = 1 << i / 2;
0026         if (i % 2) {
0027             size += size / 2;
0028         }
0029         sizes[i] = size;
0030         auto ptr = malloc(size);
0031         escape(ptr); // prevent the compiler from optimizing the malloc away
0032         cost[i] = mallinfo2().uordblks;
0033         free(ptr);
0034     }
0035 
0036     cout << "requested\t|\tactual\t|\toverhead\n";
0037     for (int i = 1; i < max_steps; ++i) {
0038         const auto actual = (cost[i] - baseline);
0039         cout << sizes[i] << "\t\t|\t" << actual << "\t|\t" << (actual - sizes[i]) << '\n';
0040     }
0041     return 0;
0042 }