diff --git a/src/brpc/details/controller_private_accessor.h b/src/brpc/details/controller_private_accessor.h index 07a071bdc8..ea0d30e116 100644 --- a/src/brpc/details/controller_private_accessor.h +++ b/src/brpc/details/controller_private_accessor.h @@ -63,7 +63,7 @@ class ControllerPrivateAccessor { } void move_in_server_receiving_sock(SocketUniquePtr& ptr) { - CHECK(_cntl->_current_call.sending_sock == NULL); + CHECK(_cntl->_current_call.sending_sock == nullptr); _cntl->_current_call.sending_sock.reset(ptr.release()); } diff --git a/src/brpc/details/has_epollrdhup.cpp b/src/brpc/details/has_epollrdhup.cpp index dd085ad8fc..9a49f56bd5 100644 --- a/src/brpc/details/has_epollrdhup.cpp +++ b/src/brpc/details/has_epollrdhup.cpp @@ -41,8 +41,7 @@ static unsigned int check_epollrdhup() { if (socketpair(AF_UNIX, SOCK_STREAM, 0, (int*)fds) < 0) { return 0; } - epoll_event evt = { static_cast(EPOLLIN | EPOLLRDHUP | EPOLLET), - { NULL }}; + epoll_event evt = { static_cast(EPOLLIN | EPOLLRDHUP | EPOLLET), { nullptr }}; if (epoll_ctl(epfd, EPOLL_CTL_ADD, fds[0], &evt) < 0) { return 0; } diff --git a/src/brpc/details/health_check.cpp b/src/brpc/details/health_check.cpp index 7cf4e32bcf..5bb3b02397 100644 --- a/src/brpc/details/health_check.cpp +++ b/src/brpc/details/health_check.cpp @@ -100,8 +100,8 @@ void* HealthCheckManager::AppCheck(void* arg) { done->cntl.http_request().uri() = done->hc_option.health_check_path; ControllerPrivateAccessor(&done->cntl).set_health_check_call(); done->last_check_time_ms = butil::cpuwide_time_ms(); - done->channel.CallMethod(NULL, &done->cntl, NULL, NULL, done); - return NULL; + done->channel.CallMethod(nullptr, &done->cntl, nullptr, nullptr, done); + return nullptr; } void OnAppHealthCheckDone::Run() { @@ -187,10 +187,10 @@ bool HealthCheckTask::OnTriggeringTask(timespec* next_abstime) { } } - // g_vars must not be NULL because it is newed at the creation of + // g_vars must not be nullptr because it is newed at the creation of // first Socket. When g_vars is used, the socket is at health-checking // state, which means the socket must be created and then g_vars can - // not be NULL. + // not be nullptr. g_vars->nhealthcheck << 1; int hc = 0; if (ptr->_user) { diff --git a/src/brpc/details/hpack.cpp b/src/brpc/details/hpack.cpp index e627e81d25..c9c707c9b9 100644 --- a/src/brpc/details/hpack.cpp +++ b/src/brpc/details/hpack.cpp @@ -38,7 +38,7 @@ struct IndexTableOptions { IndexTableOptions() : max_size(0) , start_index(0) - , static_table(NULL) + , static_table(nullptr) , static_table_size(0) , need_indexes(false) {} @@ -83,7 +83,7 @@ DISALLOW_COPY_AND_ASSIGN(IndexTable); const Header* HeaderAt(int index) const { if (BAIDU_UNLIKELY(index < _start_index)) { - return NULL; + return nullptr; } return _header_queue.bottom(index - _start_index); }; @@ -319,10 +319,10 @@ DISALLOW_COPY_AND_ASSIGN(HuffmanTree); const HuffmanNode* node(NodeId id) const { if (id == 0u) { - return NULL; + return nullptr; } if (id > _node_memory.size()) { - return NULL; + return nullptr; } return &_node_memory[id - 1]; } @@ -386,7 +386,7 @@ DISALLOW_COPY_AND_ASSIGN(HuffmanEncoder); _out->push_back(_partial_byte); _partial_byte = 0; _remain_bit = 0; - _out = NULL; + _out = nullptr; ++_out_bytes; } @@ -496,8 +496,8 @@ inline void EncodeInteger(butil::IOBufAppender* out, uint8_t msb, } // Static variables -static HuffmanTree* s_huffman_tree = NULL; -static IndexTable* s_static_table = NULL; +static HuffmanTree* s_huffman_tree = nullptr; +static IndexTable* s_static_table = nullptr; static pthread_once_t s_create_once = PTHREAD_ONCE_INIT; static void CreateStaticTableOrDie() { @@ -530,7 +530,7 @@ static const size_t MAX_HPACK_INTEGER = 10 * 1024 * 1024ul; inline ssize_t DecodeInteger(butil::IOBufBytesIterator& iter, uint8_t prefix_size, uint32_t* value) { - if (iter == NULL) { + if (iter == nullptr) { return 0; // No enough data } uint8_t first_byte = *iter; @@ -615,7 +615,7 @@ inline void EncodeString(butil::IOBufAppender* out, const std::string& s, } inline ssize_t DecodeString(butil::IOBufBytesIterator& iter, std::string* out) { - if (iter == NULL) { + if (iter == nullptr) { return 0; } const bool huffman = *iter & 0x80; @@ -634,7 +634,7 @@ inline ssize_t DecodeString(butil::IOBufBytesIterator& iter, std::string* out) { return in_bytes; } HuffmanDecoder d(out, s_huffman_tree); - for (; iter != NULL && length; ++iter, --length) { + for (; iter != nullptr && length; ++iter, --length) { if (d.Decode(*iter) != 0) { return -1; } @@ -646,19 +646,19 @@ inline ssize_t DecodeString(butil::IOBufBytesIterator& iter, std::string* out) { } HPacker::HPacker() - : _encode_table(NULL) - , _decode_table(NULL) { + : _encode_table(nullptr) + , _decode_table(nullptr) { CreateStaticTableOnceOrDie(); } HPacker::~HPacker() { if (_encode_table) { delete _encode_table; - _encode_table = NULL; + _encode_table = nullptr; } if (_decode_table) { delete _decode_table; - _decode_table = NULL; + _decode_table = nullptr; } } @@ -752,7 +752,7 @@ inline ssize_t HPacker::DecodeWithKnownPrefix( } if (index != 0) { const Header* indexed_header = HeaderAt(index); - if (indexed_header == NULL) { + if (indexed_header == nullptr) { LOG(ERROR) << "No header at index=" << index; return -1; } @@ -776,7 +776,7 @@ inline ssize_t HPacker::DecodeWithKnownPrefix( ssize_t HPacker::Decode(butil::IOBufBytesIterator& iter, Header* h) { ssize_t skipped_bytes = 0; decode_next: - if (iter == NULL) { + if (iter == nullptr) { return 0; } const uint8_t first_byte = *iter; @@ -799,7 +799,7 @@ ssize_t HPacker::Decode(butil::IOBufBytesIterator& iter, Header* h) { return index_bytes; } const Header* indexed_header = HeaderAt(index); - if (indexed_header == NULL) { + if (indexed_header == nullptr) { LOG(ERROR) << "No header at index=" << index; return -1; } diff --git a/src/brpc/details/http_message.cpp b/src/brpc/details/http_message.cpp index 003bafa074..13beb67a01 100644 --- a/src/brpc/details/http_message.cpp +++ b/src/brpc/details/http_message.cpp @@ -117,7 +117,7 @@ int HttpMessage::on_header_value(http_parser *parser, } if (FLAGS_http_verbose) { butil::IOBufBuilder* vs = http_message->_vmsgbuilder.get(); - if (vs == NULL) { + if (vs == nullptr) { vs = new butil::IOBufBuilder; http_message->_vmsgbuilder.reset(vs); if (parser->type == HTTP_REQUEST) { @@ -177,7 +177,7 @@ int HttpMessage::on_headers_complete(http_parser *parser) { URI& uri = headers.uri(); if (uri._host.empty()) { const std::string* host_header = headers.GetHeader("host"); - if (host_header != NULL) { + if (host_header != nullptr) { uri.SetHostAndPort(*host_header); } } @@ -236,7 +236,7 @@ int HttpMessage::UnlockAndFlushToBodyReader(std::unique_lock& mu) butil::Status st = r->OnReadOnePart(blk.data(), blk.size()); if (!st.ok()) { mu.lock(); - _body_reader = NULL; + _body_reader = nullptr; mu.unlock(); r->OnEndOfMessage(st); return -1; @@ -274,7 +274,7 @@ int HttpMessage::OnBody(const char *at, const size_t length) { // the body is probably streaming data which is too long to print. header().status_code() == HTTP_STATUS_OK) { LOG(INFO) << '\n' << _vmsgbuilder->buf(); - _vmsgbuilder.reset(NULL); + _vmsgbuilder.reset(nullptr); } else { if (_vbodylen < (size_t)FLAGS_http_verbose_max_body_length) { int plen = std::min(length, (size_t)FLAGS_http_verbose_max_body_length @@ -291,7 +291,7 @@ int HttpMessage::OnBody(const char *at, const size_t length) { } if (!_read_body_progressively) { // Normal read. - if (NULL != _current_source_iobuf) { + if (nullptr != _current_source_iobuf) { _current_source_iobuf->append_to( &_body, length, _parsed_block_size + (at - _current_block_base)); } else { @@ -302,7 +302,7 @@ int HttpMessage::OnBody(const char *at, const size_t length) { // Progressive read. std::unique_lock mu(_body_mutex); ProgressiveReader* r = _body_reader; - while (r == NULL) { + while (r == nullptr) { // When _body is full, the sleep-waiting may block parse handler // of the protocol. A more efficient solution is to remove the // socket from epoll and add it back when the _body is not full, @@ -328,7 +328,7 @@ int HttpMessage::OnBody(const char *at, const size_t length) { return 0; } mu.lock(); - _body_reader = NULL; + _body_reader = nullptr; mu.unlock(); r->OnEndOfMessage(st); return -1; @@ -341,10 +341,10 @@ int HttpMessage::OnMessageComplete() { - (size_t)FLAGS_http_verbose_max_body_length << " bytes>"; } LOG(INFO) << '\n' << _vmsgbuilder->buf(); - _vmsgbuilder.reset(NULL); + _vmsgbuilder.reset(nullptr); } _cur_header.clear(); - _cur_value = NULL; + _cur_value = nullptr; if (!_read_body_progressively) { // Normal read. _stage = HTTP_ON_MESSAGE_COMPLETE; @@ -353,7 +353,7 @@ int HttpMessage::OnMessageComplete() { // Progressive read. std::unique_lock mu(_body_mutex); _stage = HTTP_ON_MESSAGE_COMPLETE; - if (_body_reader != NULL) { + if (_body_reader != nullptr) { // Solve the case: SetBodyReader quit at ntry=MAX_TRY with non-empty // _body and the remaining _body is just the last part. // Make sure _body is emptied. @@ -362,7 +362,7 @@ int HttpMessage::OnMessageComplete() { } mu.lock(); ProgressiveReader* r = _body_reader; - _body_reader = NULL; + _body_reader = nullptr; mu.unlock(); r->OnEndOfMessage(butil::Status()); } @@ -379,7 +379,7 @@ class FailAllRead : public ProgressiveReader { void OnEndOfMessage(const butil::Status&) {} }; -static FailAllRead* s_fail_all_read = NULL; +static FailAllRead* s_fail_all_read = nullptr; static pthread_once_t s_fail_all_read_once = PTHREAD_ONCE_INIT; static void CreateFailAllRead() { s_fail_all_read = new FailAllRead; } @@ -393,7 +393,7 @@ void HttpMessage::SetBodyReader(ProgressiveReader* r) { int ntry = 0; do { std::unique_lock mu(_body_mutex); - if (_body_reader != NULL) { + if (_body_reader != nullptr) { mu.unlock(); return r->OnEndOfMessage( butil::Status(EPERM, "SetBodyReader is called more than once")); @@ -456,7 +456,7 @@ HttpMessage::HttpMessage(bool read_body_progressively, HttpMessage::~HttpMessage() { if (_body_reader) { ProgressiveReader* saved_body_reader = _body_reader; - _body_reader = NULL; + _body_reader = nullptr; // Successfully ended message is ended in OnMessageComplete() or // SetBodyReader() and _body_reader should be null-ed. Non-null // _body_reader here just means the socket is broken before completion @@ -499,7 +499,7 @@ ssize_t HttpMessage::ParseFromIOBuf(const butil::IOBuf &buf) { _parsed_block_size = 0; _current_source_iobuf = &buf; BRPC_SCOPE_EXIT { - _current_source_iobuf = NULL; + _current_source_iobuf = nullptr; }; size_t nprocessed = 0; for (size_t i = 0; i < buf.backing_block_num(); ++i) { @@ -632,7 +632,7 @@ void MakeRawHttpRequest(butil::IOBuf* request, //the request-target consists of only the host name and port number of //the tunnel destination, separated by a colon. For example, //Host: server.example.com:80 - if (h->GetHeader("host") == NULL) { + if (h->GetHeader("host") == nullptr) { os << "Host: "; if (!uri.host().empty()) { os << uri.host(); @@ -652,15 +652,15 @@ void MakeRawHttpRequest(butil::IOBuf* request, it != h->HeaderEnd(); ++it) { os << it->first << ": " << it->second << BRPC_CRLF; } - if (h->GetHeader("Accept") == NULL) { + if (h->GetHeader("Accept") == nullptr) { os << "Accept: */*" BRPC_CRLF; } // The fake "curl" user-agent may let servers return plain-text results. - if (h->GetHeader("User-Agent") == NULL) { + if (h->GetHeader("User-Agent") == nullptr) { os << "User-Agent: brpc/1.0 curl/7.0" BRPC_CRLF; } const std::string& user_info = h->uri().user_info(); - if (!user_info.empty() && h->GetHeader("Authorization") == NULL) { + if (!user_info.empty() && h->GetHeader("Authorization") == nullptr) { // NOTE: just assume user_info is well formatted, namely // ":". Users are very unlikely to add extra // characters in this part and even if users did, most of them are diff --git a/src/brpc/details/http_message.h b/src/brpc/details/http_message.h index ae4a016dc9..14f8fbb70f 100644 --- a/src/brpc/details/http_message.h +++ b/src/brpc/details/http_message.h @@ -114,21 +114,21 @@ class HttpMessage { // For mutual exclusion between on_body and SetBodyReader. butil::Mutex _body_mutex; // Read body progressively - ProgressiveReader* _body_reader{NULL}; + ProgressiveReader* _body_reader{nullptr}; butil::IOBuf _body; size_t _body_size{0}; bool _body_too_large{false}; // Store the IOBuf information in `ParseFromIOBuf' // for later zero-copy usage in `OnBody'. - const butil::IOBuf* _current_source_iobuf{NULL}; - const char* _current_block_base{NULL}; + const butil::IOBuf* _current_source_iobuf{nullptr}; + const char* _current_block_base{nullptr}; size_t _parsed_block_size{0}; // Parser related members struct http_parser _parser; std::string _cur_header; - std::string *_cur_value{NULL}; + std::string *_cur_value{nullptr}; protected: // Only valid when -http_verbose is on @@ -141,7 +141,7 @@ std::ostream& operator<<(std::ostream& os, const http_parser& parser); // Serialize a http request. // header: may be modified in some cases // remote_side: used when "Host" is absent -// content: could be NULL. +// content: could be nullptr. void MakeRawHttpRequest(butil::IOBuf* request, HttpHeader* header, const butil::EndPoint& remote_side, @@ -149,7 +149,7 @@ void MakeRawHttpRequest(butil::IOBuf* request, // Serialize a http response. // header: may be modified in some cases -// content: cleared after usage. could be NULL. +// content: cleared after usage. could be nullptr. void MakeRawHttpResponse(butil::IOBuf* response, HttpHeader* header, butil::IOBuf* content); diff --git a/src/brpc/details/http_parser.cpp b/src/brpc/details/http_parser.cpp index a4aa4d1276..2c68cffca3 100644 --- a/src/brpc/details/http_parser.cpp +++ b/src/brpc/details/http_parser.cpp @@ -96,7 +96,7 @@ do { \ return (ER); \ } \ } \ - FOR##_mark = NULL; \ + FOR##_mark = nullptr; \ } \ } while (0) @@ -2424,7 +2424,7 @@ http_parser_parse_url(const char *buf, size_t buflen, int is_connect, if (u->field_set & (1 << UF_PORT)) { /* Don't bother with endp; we've already validated the string */ - unsigned long v = strtoul(buf + u->field_data[UF_PORT].off, NULL, 10); + unsigned long v = strtoul(buf + u->field_data[UF_PORT].off, nullptr, 10); /* Ports have a max value of 2^16 */ if (v > 0xffff) { diff --git a/src/brpc/details/jemalloc_profiler.cpp b/src/brpc/details/jemalloc_profiler.cpp index fdd06fb4ef..2091eda7d4 100644 --- a/src/brpc/details/jemalloc_profiler.cpp +++ b/src/brpc/details/jemalloc_profiler.cpp @@ -137,7 +137,7 @@ static std::string JeProfileDump() { } const char* p_prof_name = prof_name; - int ret = mallctl("prof.dump", NULL, NULL, (void*)&p_prof_name, sizeof(p_prof_name)); + int ret = mallctl("prof.dump", nullptr, nullptr, (void*)&p_prof_name, sizeof(p_prof_name)); if (ret != 0) { LOG(WARNING) << "mallctl set prof.dump:" << p_prof_name << " err, ret:" << ret; return ""; diff --git a/src/brpc/details/mesalink_ssl_helper.cpp b/src/brpc/details/mesalink_ssl_helper.cpp index aa83fa6c35..171f045b4a 100644 --- a/src/brpc/details/mesalink_ssl_helper.cpp +++ b/src/brpc/details/mesalink_ssl_helper.cpp @@ -138,7 +138,7 @@ void ExtractHostnames(X509* x, std::vector* hostnames) { struct FreeSSL { inline void operator()(SSL* ssl) const { - if (ssl != NULL) { + if (ssl != nullptr) { SSL_free(ssl); } } @@ -146,7 +146,7 @@ struct FreeSSL { struct FreeBIO { inline void operator()(BIO* io) const { - if (io != NULL) { + if (io != nullptr) { BIO_free(io); } } @@ -154,7 +154,7 @@ struct FreeBIO { struct FreeX509 { inline void operator()(X509* x) const { - if (x != NULL) { + if (x != nullptr) { X509_free(x); } } @@ -162,7 +162,7 @@ struct FreeX509 { struct FreeEVPKEY { inline void operator()(EVP_PKEY* k) const { - if (k != NULL) { + if (k != nullptr) { EVP_PKEY_free(k); } } @@ -177,7 +177,7 @@ static int LoadCertificate(SSL_CTX* ctx, std::unique_ptr kbio( BIO_new_mem_buf((void*)private_key.c_str(), -1)); std::unique_ptr key( - PEM_read_bio_PrivateKey(kbio.get(), NULL, 0, NULL)); + PEM_read_bio_PrivateKey(kbio.get(), nullptr, 0, nullptr)); if (SSL_CTX_use_PrivateKey(ctx, key.get()) != 1) { LOG(ERROR) << "Fail to load " << private_key << ": " << SSLError(ERR_get_error()); @@ -205,7 +205,7 @@ static int LoadCertificate(SSL_CTX* ctx, } } std::unique_ptr x( - PEM_read_bio_X509(cbio.get(), NULL, 0, NULL)); + PEM_read_bio_X509(cbio.get(), nullptr, 0, nullptr)); if (!x) { LOG(ERROR) << "Fail to parse " << certificate << ": " << SSLError(ERR_get_error()); @@ -221,8 +221,8 @@ static int LoadCertificate(SSL_CTX* ctx, // Load the certificate chain //SSL_CTX_clear_chain_certs(ctx); - X509* ca = NULL; - while ((ca = PEM_read_bio_X509(cbio.get(), NULL, 0, NULL))) { + X509* ca = nullptr; + while ((ca = PEM_read_bio_X509(cbio.get(), nullptr, 0, nullptr))) { if (SSL_CTX_add_extra_chain_cert(ctx, ca) != 1) { LOG(ERROR) << "Fail to load chain certificate in " << certificate << ": " << SSLError(ERR_get_error()); @@ -251,16 +251,16 @@ static int SetSSLOptions(SSL_CTX* ctx, const std::string& ciphers, if (verify.verify_depth > 0) { std::string cafile = verify.ca_file_path; if (!cafile.empty()) { - if (SSL_CTX_load_verify_locations(ctx, cafile.c_str(), NULL) == 0) { + if (SSL_CTX_load_verify_locations(ctx, cafile.c_str(), nullptr) == 0) { LOG(ERROR) << "Fail to load CA file " << cafile << ": " << SSLError(ERR_get_error()); return -1; } } SSL_CTX_set_verify(ctx, (SSL_VERIFY_PEER - | SSL_VERIFY_FAIL_IF_NO_PEER_CERT), NULL); + | SSL_VERIFY_FAIL_IF_NO_PEER_CERT), nullptr); } else { - SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL); + SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, nullptr); } return 0; @@ -271,21 +271,21 @@ SSL_CTX* CreateClientSSLContext(const ChannelSSLOptions& options) { SSL_CTX_new(TLSv1_2_client_method())); if (!ssl_ctx) { LOG(ERROR) << "Fail to new SSL_CTX: " << SSLError(ERR_get_error()); - return NULL; + return nullptr; } if (!options.client_cert.certificate.empty() && LoadCertificate(ssl_ctx.get(), options.client_cert.certificate, - options.client_cert.private_key, NULL) != 0) { - return NULL; + options.client_cert.private_key, nullptr) != 0) { + return nullptr; } int protocols = ParseSSLProtocols(options.protocols); if (protocols < 0 || SetSSLOptions(ssl_ctx.get(), options.ciphers, protocols, options.verify) != 0) { - return NULL; + return nullptr; } SSL_CTX_set_session_cache_mode(ssl_ctx.get(), SSL_SESS_CACHE_CLIENT); @@ -300,12 +300,12 @@ SSL_CTX* CreateServerSSLContext(const std::string& certificate, SSL_CTX_new(TLSv1_2_server_method())); if (!ssl_ctx) { LOG(ERROR) << "Fail to new SSL_CTX: " << SSLError(ERR_get_error()); - return NULL; + return nullptr; } if (LoadCertificate(ssl_ctx.get(), certificate, private_key, hostnames) != 0) { - return NULL; + return nullptr; } int protocols = TLSv1 | TLSv1_1 | TLSv1_2; @@ -314,7 +314,7 @@ SSL_CTX* CreateServerSSLContext(const std::string& certificate, } if (SetSSLOptions(ssl_ctx.get(), options.ciphers, protocols, options.verify) != 0) { - return NULL; + return nullptr; } /* SSL_CTX_set_timeout(ssl_ctx.get(), options.session_lifetime_s); */ @@ -324,19 +324,19 @@ SSL_CTX* CreateServerSSLContext(const std::string& certificate, } SSL* CreateSSLSession(SSL_CTX* ctx, SocketId id, int fd, bool server_mode) { - if (ctx == NULL) { + if (ctx == nullptr) { LOG(WARNING) << "Lack SSL_ctx to create an SSL session"; - return NULL; + return nullptr; } SSL* ssl = SSL_new(ctx); - if (ssl == NULL) { + if (ssl == nullptr) { LOG(ERROR) << "Fail to SSL_new: " << SSLError(ERR_get_error()); - return NULL; + return nullptr; } if (SSL_set_fd(ssl, fd) != 1) { LOG(ERROR) << "Fail to SSL_set_fd: " << SSLError(ERR_get_error()); SSL_free(ssl); - return NULL; + return nullptr; } if (server_mode) { diff --git a/src/brpc/details/method_status.cpp b/src/brpc/details/method_status.cpp index 3bed6bf209..d23283fbd7 100644 --- a/src/brpc/details/method_status.cpp +++ b/src/brpc/details/method_status.cpp @@ -159,7 +159,7 @@ int HandleResponseWritten(bthread_id_t id, void* data, int /*error_code*/) { ConcurrencyRemover::~ConcurrencyRemover() { if (_status) { _status->OnResponded(_c->ErrorCode(), butil::cpuwide_time_us() - _received_us); - _status = NULL; + _status = nullptr; } ServerPrivateAccessor(_c->server()).RemoveConcurrency(_c); } diff --git a/src/brpc/details/method_status.h b/src/brpc/details/method_status.h index 9b7f070991..fab56e90fc 100644 --- a/src/brpc/details/method_status.h +++ b/src/brpc/details/method_status.h @@ -37,8 +37,8 @@ class MethodStatus : public Describable { // Call this function when the method is about to be called. // Returns false when the method is overloaded. If rejected_cc is not - // NULL, it's set with the rejected concurrency. - bool OnRequested(int* rejected_cc = NULL, Controller* cntl = NULL); + // nullptr, it's set with the rejected concurrency. + bool OnRequested(int* rejected_cc = nullptr, Controller* cntl = nullptr); // Call this when the method just finished. // `error_code' : The error code obtained from the controller. Equal to @@ -96,7 +96,7 @@ class ConcurrencyRemover { inline bool MethodStatus::OnRequested(int* rejected_cc, Controller* cntl) { const int cc = _nconcurrency.fetch_add(1, butil::memory_order_relaxed) + 1; - if (NULL == _cl || _cl->OnRequested(cc, cntl)) { + if (nullptr == _cl || _cl->OnRequested(cc, cntl)) { return true; } if (rejected_cc) { @@ -112,7 +112,7 @@ inline void MethodStatus::OnResponded(int error_code, int64_t latency) { } else { _nerror_bvar << 1; } - if (NULL != _cl) { + if (nullptr != _cl) { _cl->OnResponded(error_code, latency); } } diff --git a/src/brpc/details/naming_service_thread.cpp b/src/brpc/details/naming_service_thread.cpp index 7eb005e8f0..2303af1f26 100644 --- a/src/brpc/details/naming_service_thread.cpp +++ b/src/brpc/details/naming_service_thread.cpp @@ -56,7 +56,7 @@ inline bool operator==(const NSKey& k1, const NSKey& k2) { typedef butil::FlatMap NamingServiceMap; // Construct on demand to make the code work before main() -static NamingServiceMap* g_nsthread_map = NULL; +static NamingServiceMap* g_nsthread_map = nullptr; static pthread_mutex_t g_nsthread_map_mutex = PTHREAD_MUTEX_INITIALIZER; NamingServiceThread::Actions::Actions(NamingServiceThread* owner) @@ -64,7 +64,7 @@ NamingServiceThread::Actions::Actions(NamingServiceThread* owner) , _wait_id(INVALID_BTHREAD_ID) , _has_wait_error(false) , _wait_error(0) { - CHECK_EQ(0, bthread_id_create(&_wait_id, NULL, NULL)); + CHECK_EQ(0, bthread_id_create(&_wait_id, nullptr, nullptr)); } NamingServiceThread::Actions::~Actions() { @@ -161,7 +161,7 @@ void NamingServiceThread::Actions::ResetServers( _sockets.end()); } std::vector removed_ids; - ServerNodeWithId2ServerId(_removed_sockets, &removed_ids, NULL); + ServerNodeWithId2ServerId(_removed_sockets, &removed_ids, nullptr); { BAIDU_SCOPED_LOCK(_owner->_mutex); @@ -207,7 +207,7 @@ void NamingServiceThread::Actions::ResetServers( } void NamingServiceThread::Actions::EndWait(int error_code) { - if (bthread_id_trylock(_wait_id, NULL) == 0) { + if (bthread_id_trylock(_wait_id, nullptr) == 0) { _wait_error = error_code; _has_wait_error.store(true, butil::memory_order_release); bthread_id_unlock_and_destroy(_wait_id); @@ -225,7 +225,7 @@ int NamingServiceThread::Actions::WaitForFirstBatchOfServers() { NamingServiceThread::NamingServiceThread() : _tid(0) - , _ns(NULL) + , _ns(nullptr) , _actions(this) { } @@ -235,22 +235,22 @@ NamingServiceThread::~NamingServiceThread() { if (!_protocol.empty()) { const NSKey key(_protocol, _service_name, _options.channel_signature); std::unique_lock mu(g_nsthread_map_mutex); - if (g_nsthread_map != NULL) { + if (g_nsthread_map != nullptr) { NamingServiceThread** ptr = g_nsthread_map->seek(key); - if (ptr != NULL && *ptr == this) { + if (ptr != nullptr && *ptr == this) { g_nsthread_map->erase(key); } } } if (_tid) { bthread_stop(_tid); - bthread_join(_tid, NULL); + bthread_join(_tid, nullptr); _tid = 0; } { BAIDU_SCOPED_LOCK(_mutex); std::vector to_be_removed; - ServerNodeWithId2ServerId(_last_sockets, &to_be_removed, NULL); + ServerNodeWithId2ServerId(_last_sockets, &to_be_removed, nullptr); if (!_last_sockets.empty()) { for (std::map::iterator @@ -263,20 +263,20 @@ NamingServiceThread::~NamingServiceThread() { if (_ns) { _ns->Destroy(); - _ns = NULL; + _ns = nullptr; } } void* NamingServiceThread::RunThis(void* arg) { static_cast(arg)->Run(); - return NULL; + return nullptr; } int NamingServiceThread::Start(NamingService* naming_service, const std::string& protocol, const std::string& service_name, const GetNamingServiceThreadOptions* opt_in) { - if (naming_service == NULL) { + if (naming_service == nullptr) { LOG(ERROR) << "Param[naming_service] is NULL"; return -1; } @@ -290,7 +290,7 @@ int NamingServiceThread::Start(NamingService* naming_service, if (_ns->RunNamingServiceReturnsQuickly()) { RunThis(this); } else { - int rc = bthread_start_urgent(&_tid, NULL, RunThis, this); + int rc = bthread_start_urgent(&_tid, nullptr, RunThis, this); if (rc) { LOG(ERROR) << "Fail to create bthread: " << berror(rc); return rc; @@ -337,7 +337,7 @@ void NamingServiceThread::ServerNodeWithId2ServerId( int NamingServiceThread::AddWatcher(NamingServiceWatcher* watcher, const NamingServiceFilter* filter) { - if (watcher == NULL) { + if (watcher == nullptr) { LOG(ERROR) << "Param[watcher] is NULL"; return -1; } @@ -354,7 +354,7 @@ int NamingServiceThread::AddWatcher(NamingServiceWatcher* watcher, } int NamingServiceThread::RemoveWatcher(NamingServiceWatcher* watcher) { - if (watcher == NULL) { + if (watcher == nullptr) { LOG(ERROR) << "Param[watcher] is NULL"; return -1; } @@ -391,14 +391,14 @@ static const char* ParseNamingServiceUrl(const char* url, char* protocol) { // Accepting "[^:]{1,MAX_PROTOCOL_LEN}://*.*" // ^^^^^^^^^^^^^^^^^^^^^^^^ ^^^ // protocol service_name - if (__builtin_expect(url != NULL, 1)) { + if (__builtin_expect(url != nullptr, 1)) { const char* p1 = url; while (*p1 != ':') { if (p1 < url + MAX_PROTOCOL_LEN && *p1) { protocol[p1 - url] = *p1; ++p1; } else { - return NULL; + return nullptr; } } if (p1 <= url + MAX_PROTOCOL_LEN) { @@ -409,7 +409,7 @@ static const char* ParseNamingServiceUrl(const char* url, char* protocol) { } } } - return NULL; + return nullptr; } int GetNamingServiceThread( @@ -418,12 +418,12 @@ int GetNamingServiceThread( const GetNamingServiceThreadOptions* options) { char protocol[MAX_PROTOCOL_LEN + 1]; const char* const service_name = ParseNamingServiceUrl(url, protocol); - if (service_name == NULL) { + if (service_name == nullptr) { LOG(ERROR) << "Invalid naming service url=" << url; return -1; } const NamingService* source_ns = NamingServiceExtension()->Find(protocol); - if (source_ns == NULL) { + if (source_ns == nullptr) { LOG(ERROR) << "Unknown protocol=" << protocol; return -1; } @@ -433,19 +433,14 @@ int GetNamingServiceThread( butil::intrusive_ptr nsthread; { std::unique_lock mu(g_nsthread_map_mutex); - if (g_nsthread_map == NULL) { - g_nsthread_map = new (std::nothrow) NamingServiceMap; - if (NULL == g_nsthread_map) { - mu.unlock(); - LOG(ERROR) << "Fail to new g_nsthread_map"; - return -1; - } + if (g_nsthread_map == nullptr) { + g_nsthread_map = new NamingServiceMap; if (g_nsthread_map->init(64) != 0) { LOG(WARNING) << "Fail to init g_nsthread_map"; } } NamingServiceThread*& ptr = (*g_nsthread_map)[key]; - if (ptr != NULL) { + if (ptr != nullptr) { if (ptr->AddRefManually() == 0) { // The ns thread's last intrusive_ptr was just destructed and // the removal-from-global-map-code in ptr->~NamingServiceThread() @@ -453,18 +448,13 @@ int GetNamingServiceThread( // thread. // Notice that we don't need to remove the reference because // the object is already destructing. - ptr = NULL; + ptr = nullptr; } else { nsthread.reset(ptr, false); } } - if (ptr == NULL) { - NamingServiceThread* thr = new (std::nothrow) NamingServiceThread; - if (thr == NULL) { - mu.unlock(); - LOG(ERROR) << "Fail to new NamingServiceThread"; - return -1; - } + if (ptr == nullptr) { + NamingServiceThread* thr = new NamingServiceThread; ptr = thr; nsthread.reset(ptr); new_thread = true; @@ -491,7 +481,7 @@ int GetNamingServiceThread( void NamingServiceThread::Describe(std::ostream& os, const DescribeOptions& options) const { - if (_ns == NULL) { + if (_ns == nullptr) { os << "null"; } else { _ns->Describe(os, options); diff --git a/src/brpc/details/naming_service_thread.h b/src/brpc/details/naming_service_thread.h index f01fbea6a4..befb475c80 100644 --- a/src/brpc/details/naming_service_thread.h +++ b/src/brpc/details/naming_service_thread.h @@ -101,7 +101,7 @@ class NamingServiceThread : public SharedObject, public Describable { void EndWait(int error_code); int AddWatcher(NamingServiceWatcher* w, const NamingServiceFilter* f); - int AddWatcher(NamingServiceWatcher* w) { return AddWatcher(w, NULL); } + int AddWatcher(NamingServiceWatcher* w) { return AddWatcher(w, nullptr); } int RemoveWatcher(NamingServiceWatcher* w); void Describe(std::ostream& os, const DescribeOptions&) const override; diff --git a/src/brpc/details/server_private_accessor.h b/src/brpc/details/server_private_accessor.h index aacf283564..d553b4dcfa 100644 --- a/src/brpc/details/server_private_accessor.h +++ b/src/brpc/details/server_private_accessor.h @@ -111,12 +111,12 @@ class ScopedNonServiceError { ~ScopedNonServiceError() { if (_server) { ServerPrivateAccessor(_server).AddError(); - _server = NULL; + _server = nullptr; } } const Server* release() { const Server* tmp = _server; - _server = NULL; + _server = nullptr; return tmp; } private: diff --git a/src/brpc/details/sparse_minute_counter.h b/src/brpc/details/sparse_minute_counter.h index 3834afab9e..d498cec6ba 100644 --- a/src/brpc/details/sparse_minute_counter.h +++ b/src/brpc/details/sparse_minute_counter.h @@ -41,7 +41,7 @@ template class SparseMinuteCounter { Item(int64_t ts, const T& v) : timestamp_ms(ts), value(v) {} }; public: - SparseMinuteCounter() : _q(NULL) {} + SparseMinuteCounter() : _q(nullptr) {} ~SparseMinuteCounter() { DestroyQueue(_q); } // Add `value' into this counter at timestamp `now_ms' @@ -145,7 +145,7 @@ template bool SparseMinuteCounter::TryPop(int64_t now_ms, T* popped) { if (_q) { const Item* const oldest = _q->top(); - if (oldest == NULL || now_ms < oldest->timestamp_ms + 60000) { + if (oldest == nullptr || now_ms < oldest->timestamp_ms + 60000) { return false; } *popped = oldest->value; diff --git a/src/brpc/details/ssl_helper.cpp b/src/brpc/details/ssl_helper.cpp index 52246980aa..1673db1d87 100644 --- a/src/brpc/details/ssl_helper.cpp +++ b/src/brpc/details/ssl_helper.cpp @@ -49,10 +49,10 @@ bool SupportsPeerNameVerification() { } #ifndef OPENSSL_NO_DH -static DH* g_dh_1024 = NULL; -static DH* g_dh_2048 = NULL; -static DH* g_dh_4096 = NULL; -static DH* g_dh_8192 = NULL; +static DH* g_dh_1024 = nullptr; +static DH* g_dh_2048 = nullptr; +static DH* g_dh_4096 = nullptr; +static DH* g_dh_8192 = nullptr; #endif // OPENSSL_NO_DH static const char* const PEM_START = "-----BEGIN"; @@ -202,18 +202,18 @@ static void SSLMessageCallback(int write_p, int version, int content_type, #if defined(OPENSSL_IS_BORINGSSL) || (OPENSSL_VERSION_NUMBER >= 0x10101000L) static pthread_once_t g_ssl_keylog_once = PTHREAD_ONCE_INIT; -static FILE* g_ssl_keylog_file = NULL; +static FILE* g_ssl_keylog_file = nullptr; static void InitSSLKeyLogFile() { const char* path = getenv("SSLKEYLOGFILE"); - if (path == NULL || path[0] == '\0') { + if (path == nullptr || path[0] == '\0') { return; } g_ssl_keylog_file = fopen(path, "ae"); - if (g_ssl_keylog_file == NULL) { + if (g_ssl_keylog_file == nullptr) { PLOG(WARNING) << "Fail to open SSLKEYLOGFILE=" << path; } else { - setvbuf(g_ssl_keylog_file, NULL, _IOLBF, 0); + setvbuf(g_ssl_keylog_file, nullptr, _IOLBF, 0); LOG(WARNING) << "SSLKEYLOGFILE is enabled (path: " << path << "). " << "Sensitive TLS session keys will be written to this file. " << "This feature is intended for debugging only and should NOT be used in production environments."; @@ -222,7 +222,7 @@ static void InitSSLKeyLogFile() { static void SSLKeyLogCallback(const SSL* ssl, const char* line) { (void)ssl; - if (line == NULL || g_ssl_keylog_file == NULL) { + if (line == nullptr || g_ssl_keylog_file == nullptr) { return; } // Write the full key log line with newline in one call to keep output atomic. @@ -231,7 +231,7 @@ static void SSLKeyLogCallback(const SSL* ssl, const char* line) { static void MaybeSetKeyLogCallback(SSL_CTX* ctx) { pthread_once(&g_ssl_keylog_once, InitSSLKeyLogFile); - if (ctx != NULL && g_ssl_keylog_file != NULL) { + if (ctx != nullptr && g_ssl_keylog_file != nullptr) { SSL_CTX_set_keylog_callback(ctx, SSLKeyLogCallback); } } @@ -268,10 +268,10 @@ static DH* SSLGetDHCallback(SSL* ssl, int exp, int keylen) { void ExtractHostnames(X509* x, std::vector* hostnames) { #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME STACK_OF(GENERAL_NAME)* names = (STACK_OF(GENERAL_NAME)*) - X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); + X509_get_ext_d2i(x, NID_subject_alt_name, nullptr, nullptr); if (names) { for (size_t i = 0; i < static_cast(sk_GENERAL_NAME_num(names)); i++) { - char* str = NULL; + char* str = nullptr; GENERAL_NAME* name = sk_GENERAL_NAME_value(names, i); if (name->type == GEN_DNS) { if (ASN1_STRING_to_UTF8((unsigned char**)&str, @@ -289,7 +289,7 @@ void ExtractHostnames(X509* x, std::vector* hostnames) { int i = -1; X509_NAME* xname = X509_get_subject_name(x); while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { - char* str = NULL; + char* str = nullptr; X509_NAME_ENTRY* entry = X509_NAME_get_entry(xname, i); const int len = ASN1_STRING_to_UTF8((unsigned char**)&str, X509_NAME_ENTRY_get_data(entry)); @@ -303,7 +303,7 @@ void ExtractHostnames(X509* x, std::vector* hostnames) { struct FreeSSL { inline void operator()(SSL* ssl) const { - if (ssl != NULL) { + if (ssl != nullptr) { SSL_free(ssl); } } @@ -311,7 +311,7 @@ struct FreeSSL { struct FreeBIO { inline void operator()(BIO* io) const { - if (io != NULL) { + if (io != nullptr) { BIO_free(io); } } @@ -319,7 +319,7 @@ struct FreeBIO { struct FreeX509 { inline void operator()(X509* x) const { - if (x != NULL) { + if (x != nullptr) { X509_free(x); } } @@ -327,7 +327,7 @@ struct FreeX509 { struct FreeEVPKEY { inline void operator()(EVP_PKEY* k) const { - if (k != NULL) { + if (k != nullptr) { EVP_PKEY_free(k); } } @@ -342,7 +342,7 @@ static int LoadCertificate(SSL_CTX* ctx, std::unique_ptr kbio( BIO_new_mem_buf((void*)private_key.c_str(), -1)); std::unique_ptr key( - PEM_read_bio_PrivateKey(kbio.get(), NULL, 0, NULL)); + PEM_read_bio_PrivateKey(kbio.get(), nullptr, 0, nullptr)); if (SSL_CTX_use_PrivateKey(ctx, key.get()) != 1) { LOG(ERROR) << "Fail to load " << private_key << ": " << SSLError(ERR_get_error()); @@ -371,7 +371,7 @@ static int LoadCertificate(SSL_CTX* ctx, } } std::unique_ptr x( - PEM_read_bio_X509_AUX(cbio.get(), NULL, 0, NULL)); + PEM_read_bio_X509_AUX(cbio.get(), nullptr, 0, nullptr)); if (!x) { LOG(ERROR) << "Fail to parse " << certificate << ": " << SSLError(ERR_get_error()); @@ -389,13 +389,13 @@ static int LoadCertificate(SSL_CTX* ctx, #if (OPENSSL_VERSION_NUMBER >= 0x10002000L) SSL_CTX_clear_chain_certs(ctx); #else - if (ctx->extra_certs != NULL) { + if (ctx->extra_certs != nullptr) { sk_X509_pop_free(ctx->extra_certs, X509_free); - ctx->extra_certs = NULL; + ctx->extra_certs = nullptr; } #endif - X509* ca = NULL; - while ((ca = PEM_read_bio_X509(cbio.get(), NULL, 0, NULL))) { + X509* ca = nullptr; + while ((ca = PEM_read_bio_X509(cbio.get(), nullptr, 0, nullptr))) { if (SSL_CTX_add_extra_chain_cert(ctx, ca) != 1) { LOG(ERROR) << "Fail to load chain certificate in " << certificate << ": " << SSLError(ERR_get_error()); @@ -420,7 +420,7 @@ static int LoadCertificate(SSL_CTX* ctx, return -1; } - if (hostnames != NULL) { + if (hostnames != nullptr) { ExtractHostnames(x.get(), hostnames); } return 0; @@ -481,22 +481,22 @@ static int SetSSLOptions(SSL_CTX* ctx, const std::string& ciphers, } if (verify.verify_mode == VerifyMode::VERIFY_FAIL_IF_NO_PEER_CERT) { SSL_CTX_set_verify(ctx, (SSL_VERIFY_PEER - | SSL_VERIFY_FAIL_IF_NO_PEER_CERT), NULL); + | SSL_VERIFY_FAIL_IF_NO_PEER_CERT), nullptr); } else if (verify.verify_mode == VerifyMode::VERIFY_PEER) { - SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL); + SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, nullptr); } else if (verify.verify_mode == VerifyMode::VERIFY_NONE) { - SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL); + SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, nullptr); } else { // for forward compatibility SSL_CTX_set_verify(ctx, (SSL_VERIFY_PEER - | SSL_VERIFY_FAIL_IF_NO_PEER_CERT), NULL); + | SSL_VERIFY_FAIL_IF_NO_PEER_CERT), nullptr); } SSL_CTX_set_verify_depth(ctx, verify.verify_depth); std::string cafile = verify.ca_file_path; if (cafile.empty()) { cafile = X509_get_default_cert_area() + std::string("/cert.pem"); } - if (SSL_CTX_load_verify_locations(ctx, cafile.c_str(), NULL) == 0) { + if (SSL_CTX_load_verify_locations(ctx, cafile.c_str(), nullptr) == 0) { if (verify.ca_file_path.empty()) { LOG(WARNING) << "Fail to load default CA file " << cafile << ": " << SSLError(ERR_get_error()); @@ -536,7 +536,7 @@ static int SetSSLOptions(SSL_CTX* ctx, const std::string& ciphers, LOG(ERROR) << "Expected peer name requires peer verification"; return -1; } - SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL); + SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, nullptr); } SSL_CTX_set_info_callback(ctx, SSLInfoCallback); @@ -587,28 +587,28 @@ SSL_CTX* CreateClientSSLContext(const ChannelSSLOptions& options) { SSL_CTX_new(SSLv23_client_method())); if (!ssl_ctx) { LOG(ERROR) << "Fail to new SSL_CTX: " << SSLError(ERR_get_error()); - return NULL; + return nullptr; } MaybeSetKeyLogCallback(ssl_ctx.get()); if (!options.client_cert.certificate.empty() && LoadCertificate(ssl_ctx.get(), options.client_cert.certificate, - options.client_cert.private_key, NULL) != 0) { - return NULL; + options.client_cert.private_key, nullptr) != 0) { + return nullptr; } int protocols = ParseSSLProtocols(options.protocols); if (protocols < 0 || SetSSLOptions(ssl_ctx.get(), options.ciphers, protocols, options.verify) != 0) { - return NULL; + return nullptr; } if (!options.alpn_protocols.empty()) { std::vector alpn_list; if (!BuildALPNProtocolList(options.alpn_protocols, alpn_list)) { - return NULL; + return nullptr; } SSL_CTX_set_alpn_protos(ssl_ctx.get(), alpn_list.data(), alpn_list.size()); } @@ -626,13 +626,13 @@ SSL_CTX* CreateServerSSLContext(const std::string& certificate, SSL_CTX_new(SSLv23_server_method())); if (!ssl_ctx) { LOG(ERROR) << "Fail to new SSL_CTX: " << SSLError(ERR_get_error()); - return NULL; + return nullptr; } MaybeSetKeyLogCallback(ssl_ctx.get()); if (LoadCertificate(ssl_ctx.get(), certificate, private_key, hostnames) != 0) { - return NULL; + return nullptr; } int protocols = TLSv1 | TLSv1_1 | TLSv1_2 | TLSv1_3; @@ -641,7 +641,7 @@ SSL_CTX* CreateServerSSLContext(const std::string& certificate, } if (SetSSLOptions(ssl_ctx.get(), options.ciphers, protocols, options.verify) != 0) { - return NULL; + return nullptr; } #ifdef SSL_MODE_RELEASE_BUFFERS @@ -659,13 +659,13 @@ SSL_CTX* CreateServerSSLContext(const std::string& certificate, SSL_CTX_set_tmp_dh_callback(ssl_ctx.get(), SSLGetDHCallback); #if !defined(OPENSSL_NO_ECDH) && defined(SSL_CTX_set_tmp_ecdh) - EC_KEY* ecdh = NULL; + EC_KEY* ecdh = nullptr; int i = OBJ_sn2nid(options.ecdhe_curve_name.c_str()); - if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { + if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == nullptr)) { LOG(ERROR) << "Fail to find ECDHE named curve=" << options.ecdhe_curve_name << ": " << SSLError(ERR_get_error()); - return NULL; + return nullptr; } SSL_CTX_set_tmp_ecdh(ssl_ctx.get(), ecdh); EC_KEY_free(ecdh); @@ -676,26 +676,26 @@ SSL_CTX* CreateServerSSLContext(const std::string& certificate, // Set ALPN callback to choose application protocol when alpns is not empty. if (alpns != nullptr && !alpns->empty()) { if (SetServerALPNCallback(ssl_ctx.get(), alpns) != 0) { - return NULL; + return nullptr; } } return ssl_ctx.release(); } SSL* CreateSSLSession(SSL_CTX* ctx, SocketId id, int fd, bool server_mode) { - if (ctx == NULL) { + if (ctx == nullptr) { LOG(WARNING) << "Lack SSL_ctx to create an SSL session"; - return NULL; + return nullptr; } SSL* ssl = SSL_new(ctx); - if (ssl == NULL) { + if (ssl == nullptr) { LOG(ERROR) << "Fail to SSL_new: " << SSLError(ERR_get_error()); - return NULL; + return nullptr; } if (SSL_set_fd(ssl, fd) != 1) { LOG(ERROR) << "Fail to SSL_set_fd: " << SSLError(ERR_get_error()); SSL_free(ssl); - return NULL; + return nullptr; } if (server_mode) { @@ -785,7 +785,7 @@ static unsigned long SSLGetThreadId() { // may crash probably due to some TLS data used inside OpenSSL // Also according to performance test, there is little difference // between pthread mutex and bthread mutex -static butil::Mutex* g_ssl_mutexs = NULL; +static butil::Mutex* g_ssl_mutexs = nullptr; static void SSLLockCallback(int mode, int n, const char* file, int line) { (void)file; @@ -818,94 +818,94 @@ int SSLThreadInit() { #ifndef OPENSSL_NO_DH static DH* SSLGetDH1024() { - BIGNUM* p = get_rfc2409_prime_1024(NULL); + BIGNUM* p = get_rfc2409_prime_1024(nullptr); if (!p) { - return NULL; + return nullptr; } // See RFC 2409, Section 6 "Oakley Groups" // for the reason why 2 is used as generator. - BIGNUM* g = NULL; + BIGNUM* g = nullptr; BN_dec2bn(&g, "2"); if (!g) { BN_free(p); - return NULL; + return nullptr; } DH *dh = DH_new(); if (!dh) { BN_free(p); BN_free(g); - return NULL; + return nullptr; } - DH_set0_pqg(dh, p, NULL, g); + DH_set0_pqg(dh, p, nullptr, g); return dh; } static DH* SSLGetDH2048() { - BIGNUM* p = get_rfc3526_prime_2048(NULL); + BIGNUM* p = get_rfc3526_prime_2048(nullptr); if (!p) { - return NULL; + return nullptr; } // See RFC 3526, Section 3 "2048-bit MODP Group" // for the reason why 2 is used as generator. - BIGNUM* g = NULL; + BIGNUM* g = nullptr; BN_dec2bn(&g, "2"); if (!g) { BN_free(p); - return NULL; + return nullptr; } DH* dh = DH_new(); if (!dh) { BN_free(p); BN_free(g); - return NULL; + return nullptr; } - DH_set0_pqg(dh, p, NULL, g); + DH_set0_pqg(dh, p, nullptr, g); return dh; } static DH* SSLGetDH4096() { - BIGNUM* p = get_rfc3526_prime_4096(NULL); + BIGNUM* p = get_rfc3526_prime_4096(nullptr); if (!p) { - return NULL; + return nullptr; } // See RFC 3526, Section 5 "4096-bit MODP Group" // for the reason why 2 is used as generator. - BIGNUM* g = NULL; + BIGNUM* g = nullptr; BN_dec2bn(&g, "2"); if (!g) { BN_free(p); - return NULL; + return nullptr; } DH *dh = DH_new(); if (!dh) { BN_free(p); BN_free(g); - return NULL; + return nullptr; } - DH_set0_pqg(dh, p, NULL, g); + DH_set0_pqg(dh, p, nullptr, g); return dh; } static DH* SSLGetDH8192() { - BIGNUM* p = get_rfc3526_prime_8192(NULL); + BIGNUM* p = get_rfc3526_prime_8192(nullptr); if (!p) { - return NULL; + return nullptr; } // See RFC 3526, Section 7 "8192-bit MODP Group" // for the reason why 2 is used as generator. - BIGNUM* g = NULL; + BIGNUM* g = nullptr; BN_dec2bn(&g, "2"); if (!g) { BN_free(g); - return NULL; + return nullptr; } DH *dh = DH_new(); if (!dh) { BN_free(p); BN_free(g); - return NULL; + return nullptr; } - DH_set0_pqg(dh, p, NULL, g); + DH_set0_pqg(dh, p, nullptr, g); return dh; } @@ -913,19 +913,19 @@ static DH* SSLGetDH8192() { int SSLDHInit() { #ifndef OPENSSL_NO_DH - if ((g_dh_1024 = SSLGetDH1024()) == NULL) { + if ((g_dh_1024 = SSLGetDH1024()) == nullptr) { LOG(ERROR) << "Fail to initialize DH-1024"; return -1; } - if ((g_dh_2048 = SSLGetDH2048()) == NULL) { + if ((g_dh_2048 = SSLGetDH2048()) == nullptr) { LOG(ERROR) << "Fail to initialize DH-2048"; return -1; } - if ((g_dh_4096 = SSLGetDH4096()) == NULL) { + if ((g_dh_4096 = SSLGetDH4096()) == nullptr) { LOG(ERROR) << "Fail to initialize DH-4096"; return -1; } - if ((g_dh_8192 = SSLGetDH8192()) == NULL) { + if ((g_dh_8192 = SSLGetDH8192()) == nullptr) { LOG(ERROR) << "Fail to initialize DH-8192"; return -1; } @@ -970,7 +970,7 @@ void Print(std::ostream& os, SSL* ssl, const char* sep) { void Print(std::ostream& os, X509* cert, const char* sep) { BIO* buf = BIO_new(BIO_s_mem()); - if (buf == NULL) { + if (buf == nullptr) { return; } BIO_printf(buf, "subject="); @@ -990,7 +990,7 @@ void Print(std::ostream& os, X509* cert, const char* sep) { BIO_printf(buf, "%sissuer=", sep); X509_NAME_print(buf, X509_get_issuer_name(cert), 0); - char* bufp = NULL; + char* bufp = nullptr; int len = BIO_get_mem_data(buf, &bufp); os << butil::StringPiece(bufp, len); } diff --git a/src/brpc/details/ssl_helper.h b/src/brpc/details/ssl_helper.h index 815285c23d..97fa7328a9 100644 --- a/src/brpc/details/ssl_helper.h +++ b/src/brpc/details/ssl_helper.h @@ -60,7 +60,7 @@ bool SupportsPeerNameVerification(); struct FreeSSLCTX { inline void operator()(SSL_CTX* ctx) const { - if (ctx != NULL) { + if (ctx != nullptr) { SSL_CTX_free(ctx); } } diff --git a/src/brpc/details/tcmalloc_extension.cpp b/src/brpc/details/tcmalloc_extension.cpp index 6f0c9e45e3..a8704270f3 100644 --- a/src/brpc/details/tcmalloc_extension.cpp +++ b/src/brpc/details/tcmalloc_extension.cpp @@ -24,7 +24,7 @@ namespace { typedef MallocExtension* (*GetInstanceFn)(); static pthread_once_t g_get_instance_fn_once = PTHREAD_ONCE_INIT; -static GetInstanceFn g_get_instance_fn = NULL; +static GetInstanceFn g_get_instance_fn = nullptr; static void InitGetInstanceFn() { g_get_instance_fn = (GetInstanceFn)dlsym( RTLD_NEXT, "_ZN15MallocExtension8instanceEv"); @@ -43,11 +43,11 @@ MallocExtension* BAIDU_WEAK MallocExtension::instance() { if (g_get_instance_fn) { return g_get_instance_fn(); } - return NULL; + return nullptr; } bool IsHeapProfilerEnabled() { - return MallocExtension::instance() != NULL; + return MallocExtension::instance() != nullptr; } bool IsTCMallocEnabled() { @@ -56,7 +56,7 @@ bool IsTCMallocEnabled() { static bool check_TCMALLOC_SAMPLE_PARAMETER() { char* str = getenv("TCMALLOC_SAMPLE_PARAMETER"); - if (str == NULL) { + if (str == nullptr) { return false; } char* endptr; diff --git a/src/brpc/details/tcmalloc_extension.h b/src/brpc/details/tcmalloc_extension.h index 037393ec90..ee23b725fa 100644 --- a/src/brpc/details/tcmalloc_extension.h +++ b/src/brpc/details/tcmalloc_extension.h @@ -168,14 +168,14 @@ class PERFTOOLS_DLL_DECL MallocExtension { // Get the named "property"'s value. Returns true if the property // is known. Returns false if the property is not a valid property // name for the current malloc implementation. - // REQUIRES: property != NULL; value != NULL + // REQUIRES: property != nullptr; value != nullptr virtual bool GetNumericProperty(const char* property, size_t* value); // Set the named "property"'s value. Returns true if the property // is known and writable. Returns false if the property is not a // valid property name for the current malloc implementation, or // is not writable. - // REQUIRES: property != NULL + // REQUIRES: property != nullptr virtual bool SetNumericProperty(const char* property, size_t value); // Mark the current thread as "idle". This routine may optionally @@ -232,14 +232,14 @@ class PERFTOOLS_DLL_DECL MallocExtension { // p must have been allocated by this malloc implementation, // must not be an interior pointer -- that is, must be exactly // the pointer returned to by malloc() et al., not some offset - // from that -- and should not have been freed yet. p may be NULL. + // from that -- and should not have been freed yet. p may be nullptr. // (Currently only implemented in tcmalloc; other implementations // will return 0.) // This is equivalent to malloc_size() in OS X, malloc_usable_size() // in glibc, and _msize() for windows. virtual size_t GetAllocatedSize(void* p); - // The current malloc implementation. Always non-NULL. + // The current malloc implementation. Always non-nullptr. static MallocExtension* instance(); // Change the malloc implementation. Typically called by the @@ -301,7 +301,7 @@ class PERFTOOLS_DLL_DECL MallocExtension { // // It is the responsibility of the caller to "delete[]" the returned array. // - // May return NULL to indicate no results. + // May return nullptr to indicate no results. // // This is an internal extension. Callers should use the more // convenient "GetHeapSample(string*)" method defined above. diff --git a/src/brpc/details/usercode_backup_pool.cpp b/src/brpc/details/usercode_backup_pool.cpp index 338038ae39..fba05e84eb 100644 --- a/src/brpc/details/usercode_backup_pool.cpp +++ b/src/brpc/details/usercode_backup_pool.cpp @@ -67,7 +67,7 @@ static pthread_cond_t s_usercode_cond = PTHREAD_COND_INITIALIZER; static pthread_once_t s_usercode_init = PTHREAD_ONCE_INIT; butil::static_atomic g_usercode_inplace = BUTIL_STATIC_ATOMIC_INIT(0); bool g_too_many_usercode = false; -static UserCodeBackupPool* s_usercode_pool = NULL; +static UserCodeBackupPool* s_usercode_pool = nullptr; static int GetUserCodeInPlace(void*) { return g_usercode_inplace.load(butil::memory_order_relaxed); @@ -75,7 +75,7 @@ static int GetUserCodeInPlace(void*) { static size_t GetUserCodeQueueSize(void*) { BAIDU_SCOPED_LOCK(s_usercode_mutex); - return (s_usercode_pool != NULL ? s_usercode_pool->queue.size() : 0); + return (s_usercode_pool != nullptr ? s_usercode_pool->queue.size() : 0); } static double GetInPoolElapseInSecond(void* arg) { @@ -83,8 +83,8 @@ static double GetInPoolElapseInSecond(void* arg) { } UserCodeBackupPool::UserCodeBackupPool() - : inplace_var("rpc_usercode_inplace", GetUserCodeInPlace, NULL) - , queue_size_var("rpc_usercode_queue_size", GetUserCodeQueueSize, NULL) + : inplace_var("rpc_usercode_inplace", GetUserCodeInPlace, nullptr) + , queue_size_var("rpc_usercode_queue_size", GetUserCodeQueueSize, nullptr) , inpool_count("rpc_usercode_backup_count") , inpool_per_second("rpc_usercode_backup_second", &inpool_count) , inpool_elapse_s(GetInPoolElapseInSecond, &inpool_elapse_us) @@ -94,7 +94,7 @@ UserCodeBackupPool::UserCodeBackupPool() static void* UserCodeRunner(void* args) { butil::PlatformThread::SetNameSimple("brpc_user_code_runner"); static_cast(args)->UserCodeRunningLoop(); - return NULL; + return nullptr; } int UserCodeBackupPool::Init() { @@ -102,7 +102,7 @@ int UserCodeBackupPool::Init() { // during termination of program). for (int i = 0; i < FLAGS_usercode_backup_threads; ++i) { pthread_t th; - if (pthread_create(&th, NULL, UserCodeRunner, this) != 0) { + if (pthread_create(&th, nullptr, UserCodeRunner, this) != 0) { LOG(ERROR) << "Fail to create UserCodeRunner"; return -1; } @@ -120,7 +120,7 @@ void UserCodeBackupPool::UserCodeRunningLoop() { int64_t last_time = butil::cpuwide_time_us(); while (true) { bool blocked = false; - UserCode usercode = { NULL, NULL }; + UserCode usercode = { nullptr, nullptr }; { BAIDU_SCOPED_LOCK(s_usercode_mutex); while (queue.empty()) {