From c0ab23cbf5f3edc92e4a6f16d7eb8d65aee867fe Mon Sep 17 00:00:00 2001 From: chenBright Date: Wed, 19 Aug 2026 22:51:26 +0800 Subject: [PATCH] Refactor NULL with nullptr in tools --- tools/parallel_http/parallel_http.cpp | 12 ++++++------ tools/rpc_press/info_thread.cpp | 16 +++++++-------- tools/rpc_press/info_thread.h | 6 +++--- tools/rpc_press/json_loader.cpp | 4 ++-- tools/rpc_press/pb_util.cpp | 10 +++++----- tools/rpc_press/rpc_press_impl.cpp | 18 ++++++++--------- tools/rpc_press/rpc_press_impl.h | 4 ++-- tools/rpc_replay/info_thread.cpp | 16 +++++++-------- tools/rpc_replay/info_thread.h | 6 +++--- tools/rpc_replay/rpc_replay.cpp | 26 ++++++++++++------------- tools/rpc_view/rpc_view.cpp | 2 +- tools/trackme_server/trackme_server.cpp | 10 +++++----- 12 files changed, 65 insertions(+), 65 deletions(-) diff --git a/tools/parallel_http/parallel_http.cpp b/tools/parallel_http/parallel_http.cpp index 24ae855cde..c11c2158dd 100644 --- a/tools/parallel_http/parallel_http.cpp +++ b/tools/parallel_http/parallel_http.cpp @@ -92,9 +92,9 @@ void* access_thread(void* void_args) { done->cntl.http_request().uri() = url; done->args = args; done->url = url; - channel.CallMethod(NULL, &done->cntl, NULL, NULL, done); + channel.CallMethod(nullptr, &done->cntl, nullptr, nullptr, done); } - return NULL; + return nullptr; } int main(int argc, char** argv) { @@ -106,7 +106,7 @@ int main(int argc, char** argv) { // } butil::ScopedFILE fp_guard; - FILE* fp = NULL; + FILE* fp = nullptr; if (!FLAGS_url_file.empty()) { fp_guard.reset(fopen(FLAGS_url_file.c_str(), "r")); if (!fp_guard) { @@ -117,7 +117,7 @@ int main(int argc, char** argv) { } else { fp = stdin; } - char* line_buf = NULL; + char* line_buf = nullptr; size_t line_len = 0; ssize_t nr = 0; std::deque url_list; @@ -144,7 +144,7 @@ int main(int argc, char** argv) { std::vector tids; tids.resize(FLAGS_thread_num); for (int i = 0; i < FLAGS_thread_num; ++i) { - CHECK_EQ(0, bthread_start_background(&tids[i], NULL, access_thread, &args[i])); + CHECK_EQ(0, bthread_start_background(&tids[i], nullptr, access_thread, &args[i])); } std::deque > output_queue; size_t nprinted = 0; @@ -200,7 +200,7 @@ int main(int argc, char** argv) { } for (int i = 0; i < FLAGS_thread_num; ++i) { - bthread_join(tids[i], NULL); + bthread_join(tids[i], nullptr); } for (int i = 0; i < FLAGS_thread_num; ++i) { while (args[i].current_concurrency.load(butil::memory_order_relaxed) != 0) { diff --git a/tools/rpc_press/info_thread.cpp b/tools/rpc_press/info_thread.cpp index fc3d8f87bb..e71684490d 100644 --- a/tools/rpc_press/info_thread.cpp +++ b/tools/rpc_press/info_thread.cpp @@ -22,8 +22,8 @@ namespace brpc { InfoThread::InfoThread() : _stop(false) , _tid(0) { - pthread_mutex_init(&_mutex, NULL); - pthread_cond_init(&_cond, NULL); + pthread_mutex_init(&_mutex, nullptr); + pthread_cond_init(&_cond, nullptr); } InfoThread::~InfoThread() { @@ -96,19 +96,19 @@ void InfoThread::run() { static void* run_info_thread(void* arg) { ((InfoThread*)arg)->run(); - return NULL; + return nullptr; } bool InfoThread::start(const InfoThreadOptions& options) { - if (options.latency_recorder == NULL || - options.error_count == NULL || - options.sent_count == NULL) { + if (options.latency_recorder == nullptr || + options.error_count == nullptr || + options.sent_count == nullptr) { LOG(ERROR) << "Some required options are NULL"; return false; } _options = options; _stop = false; - if (pthread_create(&_tid, NULL, run_info_thread, this) != 0) { + if (pthread_create(&_tid, nullptr, run_info_thread, this) != 0) { LOG(ERROR) << "Fail to create info_thread"; return false; } @@ -124,7 +124,7 @@ void InfoThread::stop() { _stop = true; pthread_cond_signal(&_cond); } - pthread_join(_tid, NULL); + pthread_join(_tid, nullptr); } } // namespace brpc diff --git a/tools/rpc_press/info_thread.h b/tools/rpc_press/info_thread.h index 3564f0567f..e47cb5a28e 100644 --- a/tools/rpc_press/info_thread.h +++ b/tools/rpc_press/info_thread.h @@ -29,9 +29,9 @@ struct InfoThreadOptions { bvar::Adder* error_count; InfoThreadOptions() - : latency_recorder(NULL) - , sent_count(NULL) - , error_count(NULL) {} + : latency_recorder(nullptr) + , sent_count(nullptr) + , error_count(nullptr) {} }; class InfoThread { diff --git a/tools/rpc_press/json_loader.cpp b/tools/rpc_press/json_loader.cpp index b338b71182..4ea1927d5a 100644 --- a/tools/rpc_press/json_loader.cpp +++ b/tools/rpc_press/json_loader.cpp @@ -75,7 +75,7 @@ bool JsonLoader::Reader::read_some() { // Ignore json only with spaces and newline static bool possibly_valid_json(const butil::IOBuf& json) { butil::IOBufAsZeroCopyInputStream it(json); - const void* data = NULL; + const void* data = nullptr; for (int size = 0; it.Next(&data, &size); ) { for (int i = 0; i < size; ++i) { char c = ((const char*)data)[i]; @@ -97,7 +97,7 @@ bool JsonLoader::Reader::get_next_json(butil::IOBuf* json1) { json1->clear(); while (1) { butil::IOBufAsZeroCopyInputStream it(_file_buf); - const void* data = NULL; + const void* data = nullptr; int size = 0; int total_size = 0; int skipped = 0; diff --git a/tools/rpc_press/pb_util.cpp b/tools/rpc_press/pb_util.cpp index 781c5cf0c6..338fa696e6 100644 --- a/tools/rpc_press/pb_util.cpp +++ b/tools/rpc_press/pb_util.cpp @@ -36,9 +36,9 @@ const MethodDescriptor* find_method_by_name(const string& service_name, Importer* importer) { const ServiceDescriptor* descriptor = importer->pool()->FindServiceByName(service_name); - if (NULL == descriptor) { + if (nullptr == descriptor) { LOG(FATAL) << "Fail to find service=" << service_name; - return NULL; + return nullptr; } return descriptor->FindMethodByName(method_name); } @@ -47,11 +47,11 @@ const Message* get_prototype_by_method_descriptor( const MethodDescriptor* descripter, bool is_input, DynamicMessageFactory* factory) { - if (NULL == descripter) { + if (nullptr == descripter) { LOG(FATAL) <<"Param[descripter] is NULL"; - return NULL; + return nullptr; } - const Descriptor* message_descriptor = NULL; + const Descriptor* message_descriptor = nullptr; if (is_input) { message_descriptor = descripter->input_type(); } else { diff --git a/tools/rpc_press/rpc_press_impl.cpp b/tools/rpc_press/rpc_press_impl.cpp index 8da10c1495..d5e99f23be 100644 --- a/tools/rpc_press/rpc_press_impl.cpp +++ b/tools/rpc_press/rpc_press_impl.cpp @@ -73,7 +73,7 @@ int PressClient::init() { } _method_descriptor = find_method_by_name( _options->service, _options->method, _importer); - if (NULL == _method_descriptor) { + if (nullptr == _method_descriptor) { LOG(ERROR) << "Fail to find method=" << _options->service << '.' << _options->method; return -1; @@ -92,22 +92,22 @@ void PressClient::call_method(brpc::Controller* cntl, Message* request, } RpcPress::RpcPress() - : _pbrpc_client(NULL) + : _pbrpc_client(nullptr) , _started(false) , _stop(false) - , _output_json(NULL) { + , _output_json(nullptr) { } RpcPress::~RpcPress() { if (_output_json) { fclose(_output_json); - _output_json = NULL; + _output_json = nullptr; } delete _importer; } int RpcPress::init(const PressOptions* options) { - if (NULL == options) { + if (nullptr == options) { LOG(ERROR) << "Param[options] is NULL" ; return -1; } @@ -133,7 +133,7 @@ int RpcPress::init(const PressOptions* options) { } ImportErrorPrinter error_printer; _importer = new google::protobuf::compiler::Importer(&sourceTree, &error_printer); - if (_importer->Import(proto_file.c_str()) == NULL) { + if (_importer->Import(proto_file.c_str()) == nullptr) { LOG(ERROR) << "Fail to import " << proto_file; return -1; } @@ -187,7 +187,7 @@ int RpcPress::init(const PressOptions* options) { void* RpcPress::sync_call_thread(void* arg) { ((RpcPress*)arg)->sync_client(); - return NULL; + return nullptr; } void RpcPress::handle_response(brpc::Controller* cntl, @@ -267,7 +267,7 @@ int RpcPress::start() { _ttid.resize(_options.test_thread_num); int ret = 0; for (int i = 0; i < _options.test_thread_num; i++) { - if ((ret = pthread_create(&_ttid[i], NULL, sync_call_thread, this)) != 0) { + if ((ret = pthread_create(&_ttid[i], nullptr, sync_call_thread, this)) != 0) { LOG(ERROR) << "Fail to create sending threads"; return -1; } @@ -289,7 +289,7 @@ int RpcPress::stop() { } _stop = true; for (size_t i = 0; i < _ttid.size(); i++) { - pthread_join(_ttid[i], NULL); + pthread_join(_ttid[i], nullptr); } _info_thr.stop(); return 0; diff --git a/tools/rpc_press/rpc_press_impl.h b/tools/rpc_press/rpc_press_impl.h index 03126fd6bd..56d1b4131b 100644 --- a/tools/rpc_press/rpc_press_impl.h +++ b/tools/rpc_press/rpc_press_impl.h @@ -76,8 +76,8 @@ class PressClient { PressClient(const PressOptions* options, google::protobuf::compiler::Importer* importer, google::protobuf::DynamicMessageFactory* factory) { - _method_descriptor = NULL; - _response_prototype = NULL; + _method_descriptor = nullptr; + _response_prototype = nullptr; _options = options; _importer = importer; _factory = factory; diff --git a/tools/rpc_replay/info_thread.cpp b/tools/rpc_replay/info_thread.cpp index f31e597141..5c6e09219a 100644 --- a/tools/rpc_replay/info_thread.cpp +++ b/tools/rpc_replay/info_thread.cpp @@ -22,8 +22,8 @@ namespace brpc { InfoThread::InfoThread() : _stop(false) , _tid(0) { - pthread_mutex_init(&_mutex, NULL); - pthread_cond_init(&_cond, NULL); + pthread_mutex_init(&_mutex, nullptr); + pthread_cond_init(&_cond, nullptr); } InfoThread::~InfoThread() { @@ -96,19 +96,19 @@ void InfoThread::run() { static void* run_info_thread(void* arg) { ((InfoThread*)arg)->run(); - return NULL; + return nullptr; } bool InfoThread::start(const InfoThreadOptions& options) { - if (options.latency_recorder == NULL || - options.error_count == NULL || - options.sent_count == NULL) { + if (options.latency_recorder == nullptr || + options.error_count == nullptr || + options.sent_count == nullptr) { LOG(ERROR) << "Some required options are NULL"; return false; } _options = options; _stop = false; - if (pthread_create(&_tid, NULL, run_info_thread, this) != 0) { + if (pthread_create(&_tid, nullptr, run_info_thread, this) != 0) { LOG(ERROR) << "Fail to create info_thread"; return false; } @@ -124,7 +124,7 @@ void InfoThread::stop() { _stop = true; pthread_cond_signal(&_cond); } - pthread_join(_tid, NULL); + pthread_join(_tid, nullptr); } } // brpc diff --git a/tools/rpc_replay/info_thread.h b/tools/rpc_replay/info_thread.h index bec30f27d4..39a67ac096 100644 --- a/tools/rpc_replay/info_thread.h +++ b/tools/rpc_replay/info_thread.h @@ -29,9 +29,9 @@ struct InfoThreadOptions { bvar::Adder* error_count; InfoThreadOptions() - : latency_recorder(NULL) - , sent_count(NULL) - , error_count(NULL) {} + : latency_recorder(nullptr) + , sent_count(nullptr) + , error_count(nullptr) {} }; class InfoThread { diff --git a/tools/rpc_replay/rpc_replay.cpp b/tools/rpc_replay/rpc_replay.cpp index 395da6b67e..98b4c96378 100644 --- a/tools/rpc_replay/rpc_replay.cpp +++ b/tools/rpc_replay/rpc_replay.cpp @@ -62,7 +62,7 @@ class ChannelGroup { if ((size_t)type < _chans.size()) { return _chans[(size_t)type]; } - return NULL; + return nullptr; } private: @@ -148,14 +148,14 @@ static void* replay_thread(void* arg) { brpc::SampleIterator it(FLAGS_dir); int j = 0; for (brpc::SampledRequest* sample = it.Next(); - !brpc::IsAskedToQuit() && sample != NULL; sample = it.Next(), ++j) { + !brpc::IsAskedToQuit() && sample != nullptr; sample = it.Next(), ++j) { std::unique_ptr sample_guard(sample); if ((j % FLAGS_thread_num) != thread_offset) { continue; } brpc::Channel* chan = chan_group->channel(sample->meta.protocol_type()); - if (chan == NULL) { + if (chan == nullptr) { LOG(ERROR) << "No channel on protocol=" << sample->meta.protocol_type(); continue; @@ -175,7 +175,7 @@ static void* replay_thread(void* arg) { cntl->http_request().SetHeader("Host", FLAGS_http_host); } cntl->request_attachment() = http_message.body().movable(); - req_ptr = NULL; + req_ptr = nullptr; } else if (sample->meta.protocol_type() == brpc::PROTOCOL_NSHEAD) { nshead_req.Clear(); memcpy(&nshead_req.head, sample->meta.nshead().c_str(), sample->meta.nshead().length()); @@ -192,14 +192,14 @@ static void* replay_thread(void* arg) { g_sent_count << 1; const int64_t start_time = butil::cpuwide_time_us(); if (FLAGS_qps <= 0) { - chan->CallMethod(NULL/*use rpc_dump_context in cntl instead*/, - cntl, req_ptr, NULL/*ignore response*/, NULL); + chan->CallMethod(nullptr/*use rpc_dump_context in cntl instead*/, + cntl, req_ptr, nullptr/*ignore response*/, nullptr); handle_response(cntl, start_time, true); } else { google::protobuf::Closure* done = brpc::NewCallback(handle_response, cntl, start_time, false); - chan->CallMethod(NULL/*use rpc_dump_context in cntl instead*/, - cntl, req_ptr, NULL/*ignore response*/, done); + chan->CallMethod(nullptr/*use rpc_dump_context in cntl instead*/, + cntl, req_ptr, nullptr/*ignore response*/, done); int64_t end_time = butil::monotonic_time_ns(); int64_t expected_time = last_expected_time + interval; if (end_time < expected_time) { @@ -212,7 +212,7 @@ static void* replay_thread(void* arg) { } } } - return NULL; + return nullptr; } int main(int argc, char* argv[]) { @@ -262,7 +262,7 @@ int main(int argc, char* argv[]) { if (!FLAGS_use_bthread) { pids.resize(FLAGS_thread_num); for (int i = 0; i < FLAGS_thread_num; ++i) { - if (pthread_create(&pids[i], NULL, replay_thread, &chan_group) != 0) { + if (pthread_create(&pids[i], nullptr, replay_thread, &chan_group) != 0) { LOG(ERROR) << "Fail to create pthread"; return -1; } @@ -271,7 +271,7 @@ int main(int argc, char* argv[]) { bids.resize(FLAGS_thread_num); for (int i = 0; i < FLAGS_thread_num; ++i) { if (bthread_start_background( - &bids[i], NULL, replay_thread, &chan_group) != 0) { + &bids[i], nullptr, replay_thread, &chan_group) != 0) { LOG(ERROR) << "Fail to create bthread"; return -1; } @@ -290,9 +290,9 @@ int main(int argc, char* argv[]) { for (int i = 0; i < FLAGS_thread_num; ++i) { if (!FLAGS_use_bthread) { - pthread_join(pids[i], NULL); + pthread_join(pids[i], nullptr); } else { - bthread_join(bids[i], NULL); + bthread_join(bids[i], nullptr); } } info_thr.stop(); diff --git a/tools/rpc_view/rpc_view.cpp b/tools/rpc_view/rpc_view.cpp index cdfb523d14..7249da36cb 100644 --- a/tools/rpc_view/rpc_view.cpp +++ b/tools/rpc_view/rpc_view.cpp @@ -145,7 +145,7 @@ class ViewServiceImpl : public ViewService { // Keep content as it is. client_cntl->request_attachment() = server_cntl->request_attachment(); - http_chan.CallMethod(NULL, client_cntl, NULL, NULL, + http_chan.CallMethod(nullptr, client_cntl, nullptr, nullptr, brpc::NewCallback( handle_response, client_cntl, target, server_cntl, done_guard.release())); diff --git a/tools/trackme_server/trackme_server.cpp b/tools/trackme_server/trackme_server.cpp index 76e09da9ea..92ba27f77b 100644 --- a/tools/trackme_server/trackme_server.cpp +++ b/tools/trackme_server/trackme_server.cpp @@ -124,7 +124,7 @@ BugsLoader::BugsLoader() bool BugsLoader::start(const std::string& bugs_file) { _bugs_file = bugs_file; - if (pthread_create(&_tid, NULL, run_this, this) != 0) { + if (pthread_create(&_tid, nullptr, run_this, this) != 0) { LOG(ERROR) << "Fail to create loading thread"; return false; } @@ -137,12 +137,12 @@ void BugsLoader::stop() { return; } _stop = true; - pthread_join(_tid, NULL); + pthread_join(_tid, nullptr); } void* BugsLoader::run_this(void* arg) { ((BugsLoader*)arg)->run(); - return NULL; + return nullptr; } void BugsLoader::run() { @@ -175,7 +175,7 @@ void BugsLoader::load_bugs() { return; } - char* line = NULL; + char* line = nullptr; size_t line_len = 0; ssize_t nr = 0; int nline = 0; @@ -247,7 +247,7 @@ void BugsLoader::load_bugs() { bool BugsLoader::find(int64_t revision, brpc::TrackMeResponse* response) { // Add reference to make sure the bug list is not deleted. std::shared_ptr local_list = _bug_list; - if (local_list.get() == NULL) { + if (local_list.get() == nullptr) { return false; } // Reading the list in this function is always safe because a BugList