This is an automated email from the ASF dual-hosted git repository.

wwbmmm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brpc.git


The following commit(s) were added to refs/heads/master by this push:
     new a22d3052 Refactor NULL with nullptr in butil/memory (#3441)
a22d3052 is described below

commit a22d3052c8964e90c51ed7f6bf9455bd611669b4
Author: Bright Chen <[email protected]>
AuthorDate: Sat Aug 15 13:56:15 2026 +0800

    Refactor NULL with nullptr in butil/memory (#3441)
---
 src/butil/memory/aligned_memory.cc           |  4 +--
 src/butil/memory/linked_ptr.h                |  6 ++--
 src/butil/memory/ref_counted.h               | 10 +++---
 src/butil/memory/ref_counted_memory.cc       |  8 ++---
 src/butil/memory/ref_counted_memory.h        |  6 ++--
 src/butil/memory/scoped_ptr.h                | 46 ++++++++++++++--------------
 src/butil/memory/singleton.cc                |  2 +-
 src/butil/memory/singleton.h                 | 14 ++++-----
 src/butil/memory/singleton_on_pthread_once.h |  4 +--
 src/butil/memory/weak_ptr.cc                 |  2 +-
 src/butil/memory/weak_ptr.h                  | 18 +++++------
 11 files changed, 60 insertions(+), 60 deletions(-)

diff --git a/src/butil/memory/aligned_memory.cc 
b/src/butil/memory/aligned_memory.cc
index 186032fe..678caf74 100644
--- a/src/butil/memory/aligned_memory.cc
+++ b/src/butil/memory/aligned_memory.cc
@@ -16,7 +16,7 @@ void* AlignedAlloc(size_t size, size_t alignment) {
   DCHECK_GT(size, 0U);
   DCHECK_EQ(alignment & (alignment - 1), 0U);
   DCHECK_EQ(alignment % sizeof(void*), 0U);
-  void* ptr = NULL;
+  void* ptr = nullptr;
 #if defined(COMPILER_MSVC)
   ptr = _aligned_malloc(size, alignment);
 // Android technically supports posix_memalign(), but does not expose it in
@@ -28,7 +28,7 @@ void* AlignedAlloc(size_t size, size_t alignment) {
   ptr = memalign(alignment, size);
 #else
   if (posix_memalign(&ptr, alignment, size))
-    ptr = NULL;
+    ptr = nullptr;
 #endif
   // Since aligned allocations may fail for non-memory related reasons, force a
   // crash if we encounter a failed allocation; maintaining consistent behavior
diff --git a/src/butil/memory/linked_ptr.h b/src/butil/memory/linked_ptr.h
index 5773b176..a70daf59 100644
--- a/src/butil/memory/linked_ptr.h
+++ b/src/butil/memory/linked_ptr.h
@@ -80,7 +80,7 @@ class linked_ptr {
 
   // Take over ownership of a raw pointer.  This should happen as soon as
   // possible after the object is created.
-  explicit linked_ptr(T* ptr = NULL) { capture(ptr); }
+  explicit linked_ptr(T* ptr = nullptr) { capture(ptr); }
   ~linked_ptr() { depart(); }
 
   // Copy an existing linked_ptr<>, adding ourselves to the list of references.
@@ -107,7 +107,7 @@ class linked_ptr {
   }
 
   // Smart pointer members.
-  void reset(T* ptr = NULL) {
+  void reset(T* ptr = nullptr) {
     depart();
     capture(ptr);
   }
@@ -120,7 +120,7 @@ class linked_ptr {
     bool last = link_.depart();
     CHECK(last);
     T* v = value_;
-    value_ = NULL;
+    value_ = nullptr;
     return v;
   }
 
diff --git a/src/butil/memory/ref_counted.h b/src/butil/memory/ref_counted.h
index fb8bc72d..b0e03b87 100644
--- a/src/butil/memory/ref_counted.h
+++ b/src/butil/memory/ref_counted.h
@@ -232,7 +232,7 @@ class RefCountedData
 //   void some_other_function() {
 //     scoped_refptr<MyFoo> foo = new MyFoo();
 //     ...
-//     foo = NULL;  // explicitly releases |foo|
+//     foo = nullptr;  // explicitly releases |foo|
 //     ...
 //     if (foo)
 //       foo->Method(param);
@@ -247,7 +247,7 @@ class RefCountedData
 //     scoped_refptr<MyFoo> b;
 //
 //     b.swap(a);
-//     // now, |b| references the MyFoo object, and |a| references NULL.
+//     // now, |b| references the MyFoo object, and |a| references nullptr.
 //   }
 //
 // To make both |a| and |b| in the above example reference the same MyFoo
@@ -266,7 +266,7 @@ class scoped_refptr {
  public:
   typedef T element_type;
 
-  scoped_refptr() : ptr_(NULL) {
+  scoped_refptr() : ptr_(nullptr) {
   }
 
   scoped_refptr(T* p) : ptr_(p) {
@@ -308,7 +308,7 @@ class scoped_refptr {
   operator T*() const { return ptr_; }
 
   T* operator->() const {
-    assert(ptr_ != NULL);
+    assert(ptr_ != nullptr);
     return ptr_;
   }
 
@@ -344,7 +344,7 @@ class scoped_refptr {
 
   // Release ownership of ptr_, keeping its reference counter unchanged.
   T* release() WARN_UNUSED_RESULT {
-      T* saved_ptr = NULL;
+      T* saved_ptr = nullptr;
       swap(&saved_ptr);
       return saved_ptr;
   }
diff --git a/src/butil/memory/ref_counted_memory.cc 
b/src/butil/memory/ref_counted_memory.cc
index 0ffd04ff..c79e0ea1 100644
--- a/src/butil/memory/ref_counted_memory.cc
+++ b/src/butil/memory/ref_counted_memory.cc
@@ -49,8 +49,8 @@ RefCountedBytes* RefCountedBytes::TakeVector(
 
 const unsigned char* RefCountedBytes::front() const {
   // STL will assert if we do front() on an empty vector, but calling code
-  // expects a NULL.
-  return size() ? &data_.front() : NULL;
+  // expects a nullptr.
+  return size() ? &data_.front() : nullptr;
 }
 
 size_t RefCountedBytes::size() const {
@@ -71,7 +71,7 @@ RefCountedString* RefCountedString::TakeString(std::string* 
to_destroy) {
 }
 
 const unsigned char* RefCountedString::front() const {
-  return data_.empty() ? NULL :
+  return data_.empty() ? nullptr :
          reinterpret_cast<const unsigned char*>(data_.data());
 }
 
@@ -86,7 +86,7 @@ RefCountedMallocedMemory::RefCountedMallocedMemory(
 }
 
 const unsigned char* RefCountedMallocedMemory::front() const {
-  return length_ ? data_ : NULL;
+  return length_ ? data_ : nullptr;
 }
 
 size_t RefCountedMallocedMemory::size() const {
diff --git a/src/butil/memory/ref_counted_memory.h 
b/src/butil/memory/ref_counted_memory.h
index 1d7b928d..d8a323d8 100644
--- a/src/butil/memory/ref_counted_memory.h
+++ b/src/butil/memory/ref_counted_memory.h
@@ -21,7 +21,7 @@ class BUTIL_EXPORT RefCountedMemory
     : public butil::RefCountedThreadSafe<RefCountedMemory> {
  public:
   // Retrieves a pointer to the beginning of the data we point to. If the data
-  // is empty, this will return NULL.
+  // is empty, this will return nullptr.
   virtual const unsigned char* front() const = 0;
 
   // Size of the memory pointed to.
@@ -46,9 +46,9 @@ class BUTIL_EXPORT RefCountedMemory
 class BUTIL_EXPORT RefCountedStaticMemory : public RefCountedMemory {
  public:
   RefCountedStaticMemory()
-      : data_(NULL), length_(0) {}
+      : data_(nullptr), length_(0) {}
   RefCountedStaticMemory(const void* data, size_t length)
-      : data_(static_cast<const unsigned char*>(length ? data : NULL)),
+      : data_(static_cast<const unsigned char*>(length ? data : nullptr)),
         length_(length) {}
 
   // Overridden from RefCountedMemory:
diff --git a/src/butil/memory/scoped_ptr.h b/src/butil/memory/scoped_ptr.h
index 0f435cd6..f3356bf9 100644
--- a/src/butil/memory/scoped_ptr.h
+++ b/src/butil/memory/scoped_ptr.h
@@ -58,7 +58,7 @@
 //     TakesOwnership(ptr.Pass());           // ptr no longer owns Foo("yay").
 //     scoped_ptr<Foo> ptr2 = CreateFoo();   // ptr2 owns the return Foo.
 //     scoped_ptr<Foo> ptr3 =                // ptr3 now owns what was in ptr2.
-//         PassThru(ptr2.Pass());            // ptr2 is correspondingly NULL.
+//         PassThru(ptr2.Pass());            // ptr2 is correspondingly 
nullptr.
 //   }
 //
 // Notice that if you do not call Pass() when returning from PassThru(), or
@@ -214,7 +214,7 @@ class scoped_ptr_impl {
   }
 
   ~scoped_ptr_impl() {
-    if (data_.ptr != NULL) {
+    if (data_.ptr != nullptr) {
       // Not using get_deleter() saves one function call in non-optimized
       // builds.
       static_cast<D&>(data_)(data_.ptr);
@@ -223,7 +223,7 @@ class scoped_ptr_impl {
 
   void reset(T* p) {
     // This is a self-reset, which is no longer allowed: 
http://crbug.com/162971
-    RELEASE_ASSERT(p == NULL || p != data_.ptr);
+    RELEASE_ASSERT(p == nullptr || p != data_.ptr);
 
     // Note that running data_.ptr = p can lead to undefined behavior if
     // get_deleter()(get()) deletes this. In order to prevent this, reset()
@@ -235,13 +235,13 @@ class scoped_ptr_impl {
     // then it will incorrectly dispatch calls to |p| rather than the original
     // value of |data_.ptr|.
     //
-    // During the transition period, set the stored pointer to NULL while
+    // During the transition period, set the stored pointer to nullptr while
     // deleting the object. Eventually, this safety check will be removed to
     // prevent the scenario initially described from occuring and
     // http://crbug.com/176091 can be closed.
     T* old = data_.ptr;
-    data_.ptr = NULL;
-    if (old != NULL)
+    data_.ptr = nullptr;
+    if (old != nullptr)
       static_cast<D&>(data_)(old);
     data_.ptr = p;
   }
@@ -262,7 +262,7 @@ class scoped_ptr_impl {
 
   T* release() {
     T* old_ptr = data_.ptr;
-    data_.ptr = NULL;
+    data_.ptr = nullptr;
     return old_ptr;
   }
 
@@ -292,7 +292,7 @@ class scoped_ptr_impl {
 // A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T>
 // automatically deletes the pointer it holds (if any).
 // That is, scoped_ptr<T> owns the T object that it points to.
-// Like a T*, a scoped_ptr<T> may hold either NULL or a pointer to a T object.
+// Like a T*, a scoped_ptr<T> may hold either nullptr or a pointer to a T 
object.
 // Also like T*, scoped_ptr<T> is thread-compatible, and once you
 // dereference it, you get the thread safety guarantees of T.
 //
@@ -317,8 +317,8 @@ class scoped_ptr {
   typedef T element_type;
   typedef D deleter_type;
 
-  // Constructor.  Defaults to initializing with NULL.
-  scoped_ptr() : impl_(NULL) { }
+  // Constructor.  Defaults to initializing with nullptr.
+  scoped_ptr() : impl_(nullptr) { }
 
   // Constructor.  Takes ownership of p.
   explicit scoped_ptr(element_type* p) : impl_(p) { }
@@ -363,16 +363,16 @@ class scoped_ptr {
 
   // Reset.  Deletes the currently owned object, if any.
   // Then takes ownership of a new object, if given.
-  void reset(element_type* p = NULL) { impl_.reset(p); }
+  void reset(element_type* p = nullptr) { impl_.reset(p); }
 
   // Accessors to get the owned object.
   // operator* and operator-> will assert() if there is no current object.
   element_type& operator*() const {
-    assert(impl_.get() != NULL);
+    assert(impl_.get() != nullptr);
     return *impl_.get();
   }
   element_type* operator->() const  {
-    assert(impl_.get() != NULL);
+    assert(impl_.get() != nullptr);
     return impl_.get();
   }
   element_type* get() const { return impl_.get(); }
@@ -393,7 +393,7 @@ class scoped_ptr {
       scoped_ptr::*Testable;
 
  public:
-  operator Testable() const { return impl_.get() ? &scoped_ptr::impl_ : NULL; }
+  operator Testable() const { return impl_.get() ? &scoped_ptr::impl_ : 
nullptr; }
 
   // Comparison operators.
   // These return whether two scoped_ptr refer to the same object, not just to
@@ -408,8 +408,8 @@ class scoped_ptr {
 
   // Release a pointer.
   // The return value is the current pointer held by this object.
-  // If this object holds a NULL pointer, the return value is NULL.
-  // After this operation, this object will hold a NULL pointer,
+  // If this object holds a nullptr pointer, the return value is nullptr.
+  // After this operation, this object will hold a nullptr pointer,
   // and will not own the object any more.
   element_type* release() WARN_UNUSED_RESULT {
     return impl_.release();
@@ -451,8 +451,8 @@ class scoped_ptr<T[], D> {
   typedef T element_type;
   typedef D deleter_type;
 
-  // Constructor.  Defaults to initializing with NULL.
-  scoped_ptr() : impl_(NULL) { }
+  // Constructor.  Defaults to initializing with nullptr.
+  scoped_ptr() : impl_(nullptr) { }
 
   // Constructor. Stores the given array. Note that the argument's type
   // must exactly match T*. In particular:
@@ -483,11 +483,11 @@ class scoped_ptr<T[], D> {
 
   // Reset.  Deletes the currently owned array, if any.
   // Then takes ownership of a new object, if given.
-  void reset(element_type* array = NULL) { impl_.reset(array); }
+  void reset(element_type* array = nullptr) { impl_.reset(array); }
 
   // Accessors to get the owned array.
   element_type& operator[](size_t i) const {
-    assert(impl_.get() != NULL);
+    assert(impl_.get() != nullptr);
     return impl_.get()[i];
   }
   element_type* get() const { return impl_.get(); }
@@ -503,7 +503,7 @@ class scoped_ptr<T[], D> {
       scoped_ptr::*Testable;
 
  public:
-  operator Testable() const { return impl_.get() ? &scoped_ptr::impl_ : NULL; }
+  operator Testable() const { return impl_.get() ? &scoped_ptr::impl_ : 
nullptr; }
 
   // Comparison operators.
   // These return whether two scoped_ptr refer to the same object, not just to
@@ -518,8 +518,8 @@ class scoped_ptr<T[], D> {
 
   // Release a pointer.
   // The return value is the current pointer held by this object.
-  // If this object holds a NULL pointer, the return value is NULL.
-  // After this operation, this object will hold a NULL pointer,
+  // If this object holds a nullptr pointer, the return value is nullptr.
+  // After this operation, this object will hold a nullptr pointer,
   // and will not own the object any more.
   element_type* release() WARN_UNUSED_RESULT {
     return impl_.release();
diff --git a/src/butil/memory/singleton.cc b/src/butil/memory/singleton.cc
index 1e3bdf2e..4b9e3f59 100644
--- a/src/butil/memory/singleton.cc
+++ b/src/butil/memory/singleton.cc
@@ -12,7 +12,7 @@ subtle::AtomicWord WaitForInstance(subtle::AtomicWord* 
instance) {
   // Handle the race. Another thread beat us and either:
   // - Has the object in BeingCreated state
   // - Already has the object created...
-  // We know value != NULL.  It could be kBeingCreatedMarker, or a valid ptr.
+  // We know value != nullptr.  It could be kBeingCreatedMarker, or a valid 
ptr.
   // Unless your constructor can be very time consuming, it is very unlikely
   // to hit this race.  When it does, we just spin and yield the thread until
   // the object has been created.
diff --git a/src/butil/memory/singleton.h b/src/butil/memory/singleton.h
index ff132bc4..0916b9dd 100644
--- a/src/butil/memory/singleton.h
+++ b/src/butil/memory/singleton.h
@@ -121,17 +121,17 @@ const bool 
LeakySingletonTraits<Type>::kAllowedToAccessOnNonjoinableThread = tru
 template <typename Type>
 struct StaticMemorySingletonTraits {
   // WARNING: User has to deal with get() in the singleton class
-  // this is traits for returning NULL.
+  // this is traits for returning nullptr.
   static Type* New() {
-    // Only constructs once and returns pointer; otherwise returns NULL.
+    // Only constructs once and returns pointer; otherwise returns nullptr.
     if (butil::subtle::NoBarrier_AtomicExchange(&dead_, 1))
-      return NULL;
+      return nullptr;
 
     return new(buffer_.void_data()) Type();
   }
 
   static void Delete(Type* p) {
-    if (p != NULL)
+    if (p != nullptr)
       p->Type::~Type();
   }
 
@@ -252,7 +252,7 @@ class Singleton {
     // Object isn't created yet, maybe we will get to create it, let's try...
     if (butil::subtle::Acquire_CompareAndSwap(
           &instance_, 0, butil::internal::kBeingCreatedMarker) == 0) {
-      // instance_ was NULL and is now kBeingCreatedMarker.  Only one thread
+      // instance_ was nullptr and is now kBeingCreatedMarker.  Only one thread
       // will ever get here.  Threads might be spinning on us, and they will
       // stop right after we do this store.
       Type* newval = Traits::New();
@@ -265,8 +265,8 @@ class Singleton {
       butil::subtle::Release_Store(
           &instance_, reinterpret_cast<butil::subtle::AtomicWord>(newval));
 
-      if (newval != NULL && Traits::kRegisterAtExit) {
-        butil::AtExitManager::RegisterCallback(OnExit, NULL);
+      if (newval != nullptr && Traits::kRegisterAtExit) {
+        butil::AtExitManager::RegisterCallback(OnExit, nullptr);
       }
 
       return newval;
diff --git a/src/butil/memory/singleton_on_pthread_once.h 
b/src/butil/memory/singleton_on_pthread_once.h
index 9699bba7..bc0b609e 100644
--- a/src/butil/memory/singleton_on_pthread_once.h
+++ b/src/butil/memory/singleton_on_pthread_once.h
@@ -69,8 +69,8 @@ inline T* get_leaky_singleton() {
         GetLeakySingleton<T>::g_leaky_singleton_untyped);
 }
 
-// True(non-NULL) if the singleton is created.
-// The returned object (if not NULL) can be used directly.
+// True(non-nullptr) if the singleton is created.
+// The returned object (if not nullptr) can be used directly.
 template <typename T>
 inline T* has_leaky_singleton() {
     return reinterpret_cast<T*>(
diff --git a/src/butil/memory/weak_ptr.cc b/src/butil/memory/weak_ptr.cc
index 0954572a..2887a8f5 100644
--- a/src/butil/memory/weak_ptr.cc
+++ b/src/butil/memory/weak_ptr.cc
@@ -50,7 +50,7 @@ WeakReference WeakReferenceOwner::GetRef() const {
 void WeakReferenceOwner::Invalidate() {
   if (flag_.get()) {
     flag_->Invalidate();
-    flag_ = NULL;
+    flag_ = nullptr;
   }
 }
 
diff --git a/src/butil/memory/weak_ptr.h b/src/butil/memory/weak_ptr.h
index fd65bc92..7e4ae4c3 100644
--- a/src/butil/memory/weak_ptr.h
+++ b/src/butil/memory/weak_ptr.h
@@ -3,7 +3,7 @@
 // found in the LICENSE file.
 
 // Weak pointers are pointers to an object that do not affect its lifetime,
-// and which may be invalidated (i.e. reset to NULL) by the object, or its
+// and which may be invalidated (i.e. reset to nullptr) by the object, or its
 // owner, at any time, most commonly when the object is about to be deleted.
 
 // Weak pointers are useful when an object needs to be accessed safely by one
@@ -189,7 +189,7 @@ template <typename T> class WeakPtrFactory;
 template <typename T>
 class WeakPtr : public internal::WeakPtrBase {
  public:
-  WeakPtr() : ptr_(NULL) {
+  WeakPtr() : ptr_(nullptr) {
   }
 
   // Allow conversion from U to T provided U "is a" T. Note that this
@@ -198,14 +198,14 @@ class WeakPtr : public internal::WeakPtrBase {
   WeakPtr(const WeakPtr<U>& other) : WeakPtrBase(other), ptr_(other.ptr_) {
   }
 
-  T* get() const { return ref_.is_valid() ? ptr_ : NULL; }
+  T* get() const { return ref_.is_valid() ? ptr_ : nullptr; }
 
   T& operator*() const {
-    DCHECK(get() != NULL);
+    DCHECK(get() != nullptr);
     return *get();
   }
   T* operator->() const {
-    DCHECK(get() != NULL);
+    DCHECK(get() != nullptr);
     return get();
   }
 
@@ -220,11 +220,11 @@ class WeakPtr : public internal::WeakPtrBase {
   typedef T* WeakPtr::*Testable;
 
  public:
-  operator Testable() const { return get() ? &WeakPtr::ptr_ : NULL; }
+  operator Testable() const { return get() ? &WeakPtr::ptr_ : nullptr; }
 
   void reset() {
     ref_ = internal::WeakReference();
-    ptr_ = NULL;
+    ptr_ = nullptr;
   }
 
  private:
@@ -244,7 +244,7 @@ class WeakPtr : public internal::WeakPtrBase {
   }
 
   // This pointer is only valid when ref_.is_valid() is true.  Otherwise, its
-  // value is undefined (as opposed to NULL).
+  // value is undefined (as opposed to nullptr).
   T* ptr_;
 };
 
@@ -260,7 +260,7 @@ class WeakPtrFactory {
   }
 
   ~WeakPtrFactory() {
-    ptr_ = NULL;
+    ptr_ = nullptr;
   }
 
   WeakPtr<T> GetWeakPtr() {


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to