Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use smart pointers #1935

Merged
merged 8 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/re.h
Original file line number Diff line number Diff line change
Expand Up @@ -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::unique_ptr<char[]> errbuf(new char[needed]);
regerror(ec, &re_, errbuf.get(), 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.get(), needed - 1);
}

return false;
Expand Down
10 changes: 5 additions & 5 deletions test/benchmark_min_time_flag_iters_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ 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::unique_ptr<const char*[]> fake_argv(
new const char*[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.get()));

TestReporter test_reporter;
const size_t returned_count =
Expand All @@ -63,6 +64,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;
}
14 changes: 7 additions & 7 deletions test/benchmark_min_time_flag_time_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,22 @@ 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::unique_ptr<const char*[]> fake_argv(
new const char*[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];
}

const char* no_suffix = "--benchmark_min_time=4";
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.get(), 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.get(), expected);

delete[] fake_argv;
return 0;
}
10 changes: 5 additions & 5 deletions test/benchmark_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ std::set<int64_t> ConstructRandomSet(int64_t size) {
}

std::mutex test_vector_mu;
std::vector<int>* test_vector = nullptr;
std::unique_ptr<std::vector<int>> test_vector;

} // end namespace

Expand Down Expand Up @@ -146,7 +146,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.reset(new std::vector<int>());
}
int i = 0;
for (auto _ : state) {
Expand All @@ -159,7 +159,7 @@ static void BM_SetupTeardown(benchmark::State& state) {
++i;
}
if (state.thread_index() == 0) {
delete test_vector;
test_vector.reset();
}
}
BENCHMARK(BM_SetupTeardown)->ThreadPerCpu();
Expand All @@ -181,7 +181,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.reset(new std::vector<int>(static_cast<size_t>(size)));
}

for (auto _ : state) {
Expand All @@ -193,7 +193,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);
Expand Down
10 changes: 5 additions & 5 deletions test/profiler_manager_iterations_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,17 @@ 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::unique_ptr<const char*[]> fake_argv(
new const char*[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.get()));

NullReporter null_reporter;
const size_t returned_count =
Expand All @@ -58,6 +59,5 @@ int main(int argc, char** argv) {
assert(end_profiler_iteration_count == 4);

benchmark::RegisterProfilerManager(nullptr);
delete[] fake_argv;
return 0;
}
Loading