| 1 | /* Copyright (c) 2014, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include <gtest/gtest.h> |
| 25 | #include "my_config.h" |
| 26 | |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | #include "unittest/gunit/benchmark.h" |
| 31 | #include "unittest/gunit/gunit_test_main.h" |
| 32 | |
| 33 | namespace calloc_unittest { |
| 34 | |
| 35 | static void malloc_test(size_t num_iterations) { |
| 36 | for (size_t i = 0; i < num_iterations; ++i) { |
| 37 | void *rawmem1 = malloc(malloc_chunk_size); |
| 38 | memset(rawmem1, 0, malloc_chunk_size); |
| 39 | void *rawmem2 = malloc(malloc_chunk_size); |
| 40 | memset(rawmem2, 0, malloc_chunk_size); |
| 41 | // We need to prevent the optimizer from removing the whole loop. |
| 42 | EXPECT_FALSE(compare_malloc_chunks(rawmem1, rawmem2, malloc_chunk_size)); |
| 43 | free(rawmem1); |
| 44 | free(rawmem2); |
| 45 | } |
| 46 | SetBytesProcessed(num_iterations * malloc_chunk_size); |
| 47 | } |
| 48 | |
| 49 | static void calloc_test(size_t num_iterations) { |
| 50 | for (size_t i = 0; i < num_iterations; ++i) { |
| 51 | void *rawmem1 = calloc(malloc_chunk_size, 1); |
| 52 | void *rawmem2 = calloc(malloc_chunk_size, 1); |
| 53 | // We need to prevent the optimizer from removing the whole loop. |
| 54 | EXPECT_FALSE(compare_malloc_chunks(rawmem1, rawmem2, malloc_chunk_size)); |
| 55 | free(rawmem1); |
| 56 | free(rawmem2); |
| 57 | } |
| 58 | SetBytesProcessed(num_iterations * malloc_chunk_size); |
| 59 | } |
| 60 | |
| 61 | static void malloc_test_warmup(size_t num_iterations) { |
| 62 | malloc_test(num_iterations); |
| 63 | } |
| 64 | |
| 65 | BENCHMARK(malloc_test_warmup) |
| 66 | BENCHMARK(malloc_test) |
| 67 | BENCHMARK(calloc_test) |
| 68 | |
| 69 | } // namespace calloc_unittest |