]> granicus.if.org Git - icu/commitdiff
ICU-9342 implemented patch to support gcc's malloc and alloc_size attributes
authorDebabrata Sengupta <dsengup@svn.icu-project.org>
Fri, 10 Aug 2012 20:31:15 +0000 (20:31 +0000)
committerDebabrata Sengupta <dsengup@svn.icu-project.org>
Fri, 10 Aug 2012 20:31:15 +0000 (20:31 +0000)
X-SVN-Rev: 32151

icu4c/source/common/cmemory.h
icu4c/source/common/unicode/platform.h

index cbaceba0325ee3adba6f1064c608ae195a1ccdb7..38622c408bba8845db4fb6ae968bde38199565bc 100644 (file)
@@ -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.
index 2ddc9278e5f092dbeebcb4d4be2ea1ad4a3dfa50..50c121b9b027d75f9d99d1c5b19bf037fd069bd4 100644 (file)
 #   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
+
 /** @} */
 
 /*===========================================================================*/