Skip to content

Commit

Permalink
Bug 1145051: Replace uses of NS_ATTR_MALLOC with new MOZ_ALLOCATOR fr…
Browse files Browse the repository at this point in the history
…om mfbt/Attributes.h. r=glandium

--HG--
extra : rebase_source : 294215445f084687ed7fa51b88e7a22e586447a2
  • Loading branch information
Jim Blandy committed Mar 19, 2015
1 parent 9f8de4b commit a778e9f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 42 deletions.
15 changes: 0 additions & 15 deletions configure.in
Original file line number Diff line number Diff line change
Expand Up @@ -3436,13 +3436,6 @@ AC_CACHE_CHECK(for __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));],
Expand Down Expand Up @@ -3486,14 +3479,6 @@ 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
Expand Down
15 changes: 0 additions & 15 deletions js/src/configure.in
Original file line number Diff line number Diff line change
Expand Up @@ -2659,13 +2659,6 @@ AC_CACHE_CHECK(for __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));],
Expand Down Expand Up @@ -2708,14 +2701,6 @@ 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
Expand Down
18 changes: 9 additions & 9 deletions memory/mozalloc/mozalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
#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)
Expand Down Expand Up @@ -76,24 +76,24 @@ extern "C" {
*/

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) */


Expand All @@ -108,13 +108,13 @@ MFBT_API int moz_posix_memalign(void **ptr, size_t alignment, size_t size)

#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) */


Expand Down
29 changes: 29 additions & 0 deletions mfbt/Attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,35 @@
# 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

/**
Expand Down
3 changes: 0 additions & 3 deletions xpcom/xpcom-config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
/* 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

Expand Down

0 comments on commit a778e9f

Please sign in to comment.