Bug 1423461 - Use mozilla/Likely.h in mozalloc.cpp. r?njn draft
authorMike Hommey <mh+mozilla@glandium.org>
Wed, 06 Dec 2017 16:05:08 +0900
changeset 708195 653db78c48078f009526866c2fecb4eb9e7be084
parent 708087 b9f84a0f2b7556b6d70bee52cddcf17b2ed9fb37
child 708196 853c5c695612b6aa007a936ff2adf1e3a5d3eb30
push id92320
push userbmo:mh+mozilla@glandium.org
push dateWed, 06 Dec 2017 10:46:02 +0000
reviewersnjn
bugs1423461
milestone59.0a1
Bug 1423461 - Use mozilla/Likely.h in mozalloc.cpp. r?njn Back when mozalloc.cpp was written, mozilla/Likely.h didn't exist.
memory/mozalloc/mozalloc.cpp
--- a/memory/mozalloc/mozalloc.cpp
+++ b/memory/mozalloc/mozalloc.cpp
@@ -54,77 +54,70 @@ MOZ_MEMORY_API char *strndup_impl(const 
 
 #include <errno.h>
 #include <new>                  // for std::bad_alloc
 #include <string.h>
 
 #include <sys/types.h>
 
 #include "mozilla/Assertions.h"
+#include "mozilla/Likely.h"
 #include "mozilla/mozalloc.h"
 #include "mozilla/mozalloc_oom.h"  // for mozalloc_handle_oom
 
-#ifdef __GNUC__
-#define LIKELY(x)    (__builtin_expect(!!(x), 1))
-#define UNLIKELY(x)  (__builtin_expect(!!(x), 0))
-#else
-#define LIKELY(x)    (x)
-#define UNLIKELY(x)  (x)
-#endif
-
 void*
 moz_xmalloc(size_t size)
 {
     void* ptr = malloc_impl(size);
-    if (UNLIKELY(!ptr && size)) {
+    if (MOZ_UNLIKELY(!ptr && size)) {
         mozalloc_handle_oom(size);
         return moz_xmalloc(size);
     }
     return ptr;
 }
 
 void*
 moz_xcalloc(size_t nmemb, size_t size)
 {
     void* ptr = calloc_impl(nmemb, size);
-    if (UNLIKELY(!ptr && nmemb && size)) {
+    if (MOZ_UNLIKELY(!ptr && nmemb && size)) {
         mozalloc_handle_oom(size);
         return moz_xcalloc(nmemb, size);
     }
     return ptr;
 }
 
 void*
 moz_xrealloc(void* ptr, size_t size)
 {
     void* newptr = realloc_impl(ptr, size);
-    if (UNLIKELY(!newptr && size)) {
+    if (MOZ_UNLIKELY(!newptr && size)) {
         mozalloc_handle_oom(size);
         return moz_xrealloc(ptr, size);
     }
     return newptr;
 }
 
 char*
 moz_xstrdup(const char* str)
 {
     char* dup = strdup_impl(str);
-    if (UNLIKELY(!dup)) {
+    if (MOZ_UNLIKELY(!dup)) {
         mozalloc_handle_oom(0);
         return moz_xstrdup(str);
     }
     return dup;
 }
 
 #if defined(HAVE_STRNDUP)
 char*
 moz_xstrndup(const char* str, size_t strsize)
 {
     char* dup = strndup_impl(str, strsize);
-    if (UNLIKELY(!dup)) {
+    if (MOZ_UNLIKELY(!dup)) {
         mozalloc_handle_oom(strsize);
         return moz_xstrndup(str, strsize);
     }
     return dup;
 }
 #endif  // if defined(HAVE_STRNDUP)
 
 #ifndef HAVE_MEMALIGN
@@ -132,17 +125,17 @@ moz_xstrndup(const char* str, size_t str
 // necessarily come with a declaration.
 extern "C" void* memalign(size_t, size_t);
 #endif
 
 void*
 moz_xmemalign(size_t boundary, size_t size)
 {
     void* ptr = memalign_impl(boundary, size);
-    if (UNLIKELY(!ptr && EINVAL != errno)) {
+    if (MOZ_UNLIKELY(!ptr && EINVAL != errno)) {
         mozalloc_handle_oom(size);
         return moz_xmemalign(boundary, size);
     }
     // non-NULL ptr or errno == EINVAL
     return ptr;
 }
 
 size_t