Bug 1145051: Replace uses of NS_ATTR_MALLOC with new MOZ_ALLOCATOR from mfbt/Attributes.h. r=glandium
authorJim Blandy <jimb@mozilla.com>
Wed, 18 Mar 2015 23:56:08 -0700
changeset 239494 5de65d408dbccab3f52af0c04377b0f790bc7dac
parent 239493 fb2f2efbd38cd7fec1abd874175ad8cc9c27f15f
child 239495 79bdb9d8ca5bf2effdc988ba8b9982a2d9fc8455
push id58545
push userjblandy@mozilla.com
push dateThu, 16 Apr 2015 17:31:37 +0000
treeherdermozilla-inbound@47abda42667c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersglandium
bugs1145051
milestone40.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1145051: Replace uses of NS_ATTR_MALLOC with new MOZ_ALLOCATOR from mfbt/Attributes.h. r=glandium
configure.in
js/src/configure.in
memory/mozalloc/mozalloc.h
mfbt/Attributes.h
xpcom/xpcom-config.h.in
--- a/configure.in
+++ b/configure.in
@@ -3431,23 +3431,16 @@ dnl See if compiler supports some gcc-st
 
 AC_CACHE_CHECK(for __attribute__((always_inline)),
                ac_cv_attribute_always_inline,
                [AC_TRY_COMPILE([inline void f(void) __attribute__((always_inline));],
                                [],
                                ac_cv_attribute_always_inline=yes,
                                ac_cv_attribute_always_inline=no)])
 
-AC_CACHE_CHECK(for __attribute__((malloc)),
-               ac_cv_attribute_malloc,
-               [AC_TRY_COMPILE([void* f(int) __attribute__((malloc));],
-                               [],
-                               ac_cv_attribute_malloc=yes,
-                               ac_cv_attribute_malloc=no)])
-
 AC_CACHE_CHECK(for __attribute__((warn_unused_result)),
                ac_cv_attribute_warn_unused,
                [AC_TRY_COMPILE([int f(void) __attribute__((warn_unused_result));],
                                [],
                                ac_cv_attribute_warn_unused=yes,
                                ac_cv_attribute_warn_unused=no)])
 
 dnl End of C++ language/feature checks
@@ -3481,24 +3474,16 @@ if test -n "${CPU_ARCH}" -a -n "${TARGET
     AC_DEFINE_UNQUOTED(TARGET_XPCOM_ABI, ["${TARGET_XPCOM_ABI}"])
 fi
 
 dnl Mozilla specific options
 dnl ========================================================
 dnl The macros used for command line options
 dnl are defined in build/autoconf/altoptions.m4.
 
-dnl If the compiler supports these attributes, define them as
-dnl convenience macros.
-if test "$ac_cv_attribute_malloc" = yes ; then
-  AC_DEFINE(NS_ATTR_MALLOC, [__attribute__((malloc))])
-else
-  AC_DEFINE(NS_ATTR_MALLOC,)
-fi
-
 if test "$ac_cv_attribute_warn_unused" = yes ; then
   AC_DEFINE(NS_WARN_UNUSED_RESULT, [__attribute__((warn_unused_result))])
 else
   AC_DEFINE(NS_WARN_UNUSED_RESULT,)
 fi
 
 dnl We can't run TRY_COMPILE tests on Windows, so hard-code some
 dnl features that Windows actually does support.
--- a/js/src/configure.in
+++ b/js/src/configure.in
@@ -2654,23 +2654,16 @@ dnl See if compiler supports some gcc-st
 
 AC_CACHE_CHECK(for __attribute__((always_inline)),
                ac_cv_attribute_always_inline,
                [AC_TRY_COMPILE([inline void f(void) __attribute__((always_inline));],
                                [],
                                ac_cv_attribute_always_inline=yes,
                                ac_cv_attribute_always_inline=no)])
 
-AC_CACHE_CHECK(for __attribute__((malloc)),
-               ac_cv_attribute_malloc,
-               [AC_TRY_COMPILE([void* f(int) __attribute__((malloc));],
-                               [],
-                               ac_cv_attribute_malloc=yes,
-                               ac_cv_attribute_malloc=no)])
-
 AC_CACHE_CHECK(for __attribute__((warn_unused_result)),
                ac_cv_attribute_warn_unused,
                [AC_TRY_COMPILE([int f(void) __attribute__((warn_unused_result));],
                                [],
                                ac_cv_attribute_warn_unused=yes,
                                ac_cv_attribute_warn_unused=no)])
 
 dnl End of C++ language/feature checks
@@ -2703,24 +2696,16 @@ if test -n "${CPU_ARCH}" -a -n "${TARGET
     TARGET_XPCOM_ABI="${CPU_ARCH}-${TARGET_COMPILER_ABI}"
 fi
 
 dnl Mozilla specific options
 dnl ========================================================
 dnl The macros used for command line options
 dnl are defined in build/autoconf/altoptions.m4.
 
-dnl If the compiler supports these attributes, define them as
-dnl convenience macros.
-if test "$ac_cv_attribute_malloc" = yes ; then
-  AC_DEFINE(NS_ATTR_MALLOC, [__attribute__((malloc))])
-else
-  AC_DEFINE(NS_ATTR_MALLOC,)
-fi
-
 if test "$ac_cv_attribute_warn_unused" = yes ; then
   AC_DEFINE(NS_WARN_UNUSED_RESULT, [__attribute__((warn_unused_result))])
 else
   AC_DEFINE(NS_WARN_UNUSED_RESULT,)
 fi
 
 dnl We can't run TRY_COMPILE tests on Windows, so hard-code some
 dnl features that Windows actually does support.
--- a/memory/mozalloc/mozalloc.h
+++ b/memory/mozalloc/mozalloc.h
@@ -35,18 +35,18 @@
 #else
 #  define MOZALLOC_INLINE inline
 #endif
 
 /* Workaround build problem with Sun Studio 12 */
 #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
 #  undef NS_WARN_UNUSED_RESULT
 #  define NS_WARN_UNUSED_RESULT
-#  undef NS_ATTR_MALLOC
-#  define NS_ATTR_MALLOC
+#  undef MOZ_ALLOCATOR
+#  define MOZ_ALLOCATOR
 #endif
 
 #if defined(__cplusplus)
 extern "C" {
 #endif /* ifdef __cplusplus */
 
 /*
  * We need to use malloc_impl and free_impl in this file when they are
@@ -71,55 +71,55 @@ extern "C" {
  * pointers if memory is exhausted: their return value must be checked.
  *
  * All these allocation functions are *guaranteed* to return a pointer
  * to memory allocated in such a way that that memory can be freed by
  * passing that pointer to |free()|.
  */
 
 MFBT_API void* moz_xmalloc(size_t size)
-    NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT;
+    MOZ_ALLOCATOR;
 
 MFBT_API void* moz_xcalloc(size_t nmemb, size_t size)
-    NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT;
+    MOZ_ALLOCATOR;
 
 MFBT_API void* moz_xrealloc(void* ptr, size_t size)
-    NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT;
+    MOZ_ALLOCATOR;
 
 MFBT_API char* moz_xstrdup(const char* str)
-    NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT;
+    MOZ_ALLOCATOR;
 
 MFBT_API size_t moz_malloc_usable_size(void *ptr);
 
 MFBT_API size_t moz_malloc_size_of(const void *ptr);
 
 #if defined(HAVE_STRNDUP)
 MFBT_API char* moz_xstrndup(const char* str, size_t strsize)
-    NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT;
+    MOZ_ALLOCATOR;
 #endif /* if defined(HAVE_STRNDUP) */
 
 
 #if defined(HAVE_POSIX_MEMALIGN)
 MFBT_API int moz_xposix_memalign(void **ptr, size_t alignment, size_t size)
     NS_WARN_UNUSED_RESULT;
 
 MFBT_API int moz_posix_memalign(void **ptr, size_t alignment, size_t size)
     NS_WARN_UNUSED_RESULT;
 #endif /* if defined(HAVE_POSIX_MEMALIGN) */
 
 
 #if defined(HAVE_MEMALIGN)
 MFBT_API void* moz_xmemalign(size_t boundary, size_t size)
-    NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT;
+    MOZ_ALLOCATOR;
 #endif /* if defined(HAVE_MEMALIGN) */
 
 
 #if defined(HAVE_VALLOC)
 MFBT_API void* moz_xvalloc(size_t size)
-    NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT;
+    MOZ_ALLOCATOR;
 #endif /* if defined(HAVE_VALLOC) */
 
 
 #ifdef __cplusplus
 } /* extern "C" */
 #endif /* ifdef __cplusplus */
 
 
--- a/mfbt/Attributes.h
+++ b/mfbt/Attributes.h
@@ -278,16 +278,45 @@
 #    define MOZ_TSAN_BLACKLIST MOZ_NEVER_INLINE __attribute__((no_sanitize_thread))
 #  else
 #    define MOZ_TSAN_BLACKLIST /* nothing */
 #  endif
 #else
 #  define MOZ_TSAN_BLACKLIST /* nothing */
 #endif
 
+/**
+ * MOZ_ALLOCATOR tells the compiler that the function it marks returns either a
+ * "fresh", "pointer-free" block of memory, or nullptr. "Fresh" means that the
+ * block is not pointed to by any other reachable pointer in the program.
+ * "Pointer-free" means that the block contains no pointers to any valid object
+ * in the program. It may be initialized with other (non-pointer) values.
+ *
+ * Placing this attribute on appropriate functions helps GCC analyze pointer
+ * aliasing more accurately in their callers.
+ *
+ * GCC warns if a caller ignores the value returned by a function marked with
+ * MOZ_ALLOCATOR: it is hard to imagine cases where dropping the value returned
+ * by a function that meets the criteria above would be intentional.
+ *
+ * Place this attribute after the argument list and 'this' qualifiers of a
+ * function definition. For example, write
+ *
+ *   void *my_allocator(size_t) MOZ_ALLOCATOR;
+ *
+ * or
+ *
+ *   void *my_allocator(size_t bytes) MOZ_ALLOCATOR { ... }
+ */
+#if defined(__GNUC__) || defined(__clang__)
+#  define MOZ_ALLOCATOR __attribute__ ((malloc, warn_unused_result))
+#else
+#  define MOZ_ALLOCATOR
+#endif
+
 #ifdef __cplusplus
 
 /**
  * MOZ_WARN_UNUSED_RESULT tells the compiler to emit a warning if a function's
  * return value is not used by the caller.
  *
  * Place this attribute at the very beginning of a function definition. For
  * example, write
--- a/xpcom/xpcom-config.h.in
+++ b/xpcom/xpcom-config.h.in
@@ -13,18 +13,15 @@
 #undef CPP_THROW_NEW
 
 /* Define if the c++ compiler can resolve ambiguity with |using| */
 #undef HAVE_CPP_AMBIGUITY_RESOLVING_USING
 
 /* Define if a dyanmic_cast to void* gives the most derived object */
 #undef HAVE_CPP_DYNAMIC_CAST_TO_VOID_PTR
 
-/* Define to either __attribute__((malloc)) or nothing */
-#undef NS_ATTR_MALLOC
-
 /* Define to either __attribute__((warn_unused_result)) or nothing */
 #undef NS_WARN_UNUSED_RESULT
 
 /* Define to a string describing the XPCOM ABI in use */
 #undef TARGET_XPCOM_ABI
 
 #endif /* _XPCOM_CONFIG_H_ */