1
0
mirror of https://github.com/google/benchmark.git synced 2025-03-29 13:30:38 +08:00

use smart pointers ()

* use smart pointers
* use vectors
* size_t
This commit is contained in:
dominic 2025-02-18 09:59:27 +00:00 committed by GitHub
parent afa46a38d9
commit 57efbfb3a0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 23 additions and 27 deletions

View File

@ -121,15 +121,13 @@ inline bool Regex::Init(const std::string& spec, std::string* error) {
if (ec != 0) {
if (error) {
size_t needed = regerror(ec, &re_, nullptr, 0);
char* errbuf = new char[needed];
regerror(ec, &re_, errbuf, needed);
std::vector<char> errbuf(needed);
regerror(ec, &re_, errbuf.data(), needed);
// regerror returns the number of bytes necessary to null terminate
// the string, so we move that when assigning to error.
BM_CHECK_NE(needed, 0);
error->assign(errbuf, needed - 1);
delete[] errbuf;
error->assign(errbuf.data(), needed - 1);
}
return false;

View File

@ -46,13 +46,13 @@ BENCHMARK(BM_MyBench);
int main(int argc, char** argv) {
// Make a fake argv and append the new --benchmark_min_time=<foo> to it.
int fake_argc = argc + 1;
const char** fake_argv = new const char*[static_cast<size_t>(fake_argc)];
for (int i = 0; i < argc; ++i) {
std::vector<const char*> fake_argv(static_cast<size_t>(fake_argc));
for (size_t i = 0; i < static_cast<size_t>(argc); ++i) {
fake_argv[i] = argv[i];
}
fake_argv[argc] = "--benchmark_min_time=4x";
fake_argv[static_cast<size_t>(argc)] = "--benchmark_min_time=4x";
benchmark::Initialize(&fake_argc, const_cast<char**>(fake_argv));
benchmark::Initialize(&fake_argc, const_cast<char**>(fake_argv.data()));
TestReporter test_reporter;
const size_t returned_count =
@ -63,6 +63,5 @@ int main(int argc, char** argv) {
const std::vector<benchmark::IterationCount> iters = test_reporter.GetIters();
assert(!iters.empty() && iters[0] == 4);
delete[] fake_argv;
return 0;
}

View File

@ -71,9 +71,9 @@ BENCHMARK(BM_MyBench);
int main(int argc, char** argv) {
// Make a fake argv and append the new --benchmark_min_time=<foo> to it.
int fake_argc = argc + 1;
const char** fake_argv = new const char*[static_cast<size_t>(fake_argc)];
std::vector<const char*> fake_argv(static_cast<size_t>(fake_argc));
for (int i = 0; i < argc; ++i) {
for (size_t i = 0; i < static_cast<size_t>(argc); ++i) {
fake_argv[i] = argv[i];
}
@ -81,12 +81,11 @@ int main(int argc, char** argv) {
const char* with_suffix = "--benchmark_min_time=4.0s";
double expected = 4.0;
fake_argv[argc] = no_suffix;
DoTestHelper(&fake_argc, fake_argv, expected);
fake_argv[static_cast<size_t>(argc)] = no_suffix;
DoTestHelper(&fake_argc, fake_argv.data(), expected);
fake_argv[argc] = with_suffix;
DoTestHelper(&fake_argc, fake_argv, expected);
fake_argv[static_cast<size_t>(argc)] = with_suffix;
DoTestHelper(&fake_argc, fake_argv.data(), expected);
delete[] fake_argv;
return 0;
}

View File

@ -12,6 +12,7 @@
#include <list>
#include <map>
#include <mutex>
#include <optional>
#include <set>
#include <sstream>
#include <string>
@ -51,7 +52,7 @@ std::set<int64_t> ConstructRandomSet(int64_t size) {
}
std::mutex test_vector_mu;
std::vector<int>* test_vector = nullptr;
std::optional<std::vector<int>> test_vector;
} // end namespace
@ -146,7 +147,7 @@ BENCHMARK(BM_StringCompare)->Range(1, 1 << 20);
static void BM_SetupTeardown(benchmark::State& state) {
if (state.thread_index() == 0) {
// No need to lock test_vector_mu here as this is running single-threaded.
test_vector = new std::vector<int>();
test_vector = std::vector<int>();
}
int i = 0;
for (auto _ : state) {
@ -159,7 +160,7 @@ static void BM_SetupTeardown(benchmark::State& state) {
++i;
}
if (state.thread_index() == 0) {
delete test_vector;
test_vector.reset();
}
}
BENCHMARK(BM_SetupTeardown)->ThreadPerCpu();
@ -181,7 +182,7 @@ static void BM_ParallelMemset(benchmark::State& state) {
int to = from + thread_size;
if (state.thread_index() == 0) {
test_vector = new std::vector<int>(static_cast<size_t>(size));
test_vector = std::vector<int>(static_cast<size_t>(size));
}
for (auto _ : state) {
@ -193,7 +194,7 @@ static void BM_ParallelMemset(benchmark::State& state) {
}
if (state.thread_index() == 0) {
delete test_vector;
test_vector.reset();
}
}
BENCHMARK(BM_ParallelMemset)->Arg(10 << 20)->ThreadRange(1, 4);

View File

@ -38,16 +38,16 @@ int main(int argc, char** argv) {
// Make a fake argv and append the new --benchmark_profiler_iterations=<foo>
// to it.
int fake_argc = argc + 1;
const char** fake_argv = new const char*[static_cast<size_t>(fake_argc)];
for (int i = 0; i < argc; ++i) {
std::vector<const char*> fake_argv(static_cast<size_t>(fake_argc));
for (size_t i = 0; i < static_cast<size_t>(argc); ++i) {
fake_argv[i] = argv[i];
}
fake_argv[argc] = "--benchmark_min_time=4x";
fake_argv[static_cast<size_t>(argc)] = "--benchmark_min_time=4x";
std::unique_ptr<benchmark::ProfilerManager> pm(new TestProfilerManager());
benchmark::RegisterProfilerManager(pm.get());
benchmark::Initialize(&fake_argc, const_cast<char**>(fake_argv));
benchmark::Initialize(&fake_argc, const_cast<char**>(fake_argv.data()));
NullReporter null_reporter;
const size_t returned_count =
@ -58,6 +58,5 @@ int main(int argc, char** argv) {
assert(end_profiler_iteration_count == 4);
benchmark::RegisterProfilerManager(nullptr);
delete[] fake_argv;
return 0;
}