From: Debabrata Sengupta Date: Fri, 10 Aug 2012 20:31:15 +0000 (+0000) Subject: ICU-9342 implemented patch to support gcc's malloc and alloc_size attributes X-Git-Tag: milestone-59-0-1~3672 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3ef2d173ca9621f12ff4bb7bc065bb0662c65bee;p=icu ICU-9342 implemented patch to support gcc's malloc and alloc_size attributes X-SVN-Rev: 32151 --- diff --git a/icu4c/source/common/cmemory.h b/icu4c/source/common/cmemory.h index cbaceba0325..38622c408bb 100644 --- a/icu4c/source/common/cmemory.h +++ b/icu4c/source/common/cmemory.h @@ -62,16 +62,16 @@ U_CAPI void uprv_checkValidMemory(const void *p, size_t n); #define uprv_memcmp(buffer1, buffer2, size) U_STANDARD_CPP_NAMESPACE memcmp(buffer1, buffer2,size) U_CAPI void * U_EXPORT2 -uprv_malloc(size_t s); +uprv_malloc(size_t s) U_MALLOC_ATTR U_ALLOC_SIZE_ATTR(1); U_CAPI void * U_EXPORT2 -uprv_realloc(void *mem, size_t size); +uprv_realloc(void *mem, size_t size) U_ALLOC_SIZE_ATTR(2); U_CAPI void U_EXPORT2 uprv_free(void *mem); U_CAPI void * U_EXPORT2 -uprv_calloc(size_t num, size_t size); +uprv_calloc(size_t num, size_t size) U_MALLOC_ATTR U_ALLOC_SIZE_ATTR2(1,2); /** * This should align the memory properly on any machine. diff --git a/icu4c/source/common/unicode/platform.h b/icu4c/source/common/unicode/platform.h index 2ddc9278e5f..50c121b9b02 100644 --- a/icu4c/source/common/unicode/platform.h +++ b/icu4c/source/common/unicode/platform.h @@ -392,6 +392,35 @@ # define U_HAVE_DEBUG_LOCATION_NEW 0 #endif +/* Compatibility with non clang compilers */ +#ifndef __has_attribute +# define __has_attribute(x) 0 +#endif + +/** + * \def U_MALLOC_ATTR + * Attribute to mark functions as malloc-like + * @internal + */ +#if defined(__GNUC__) && __GNUC__>=3 +# define U_MALLOC_ATTR __attribute__ ((__malloc__)) +#else +# define U_MALLOC_ATTR +#endif + +/** + * \def U_ALLOC_SIZE_ATTR + * Attribute to specify the size of the allocated buffer for malloc-like functions + * @internal + */ +#if (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))) || __has_attribute(alloc_size) +# define U_ALLOC_SIZE_ATTR(X) __attribute__ ((alloc_size(X))) +# define U_ALLOC_SIZE_ATTR2(X,Y) __attribute__ ((alloc_size(X,Y))) +#else +# define U_ALLOC_SIZE_ATTR(X) +# define U_ALLOC_SIZE_ATTR2(X,Y) +#endif + /** @} */ /*===========================================================================*/