[RFC] eal: mark rte_malloc with malloc attribute

Message ID 20200730171312.20351-1-stephen@networkplumber.org (mailing list archive)
State Superseded, archived
Delegated to: David Marchand
Headers
Series [RFC] eal: mark rte_malloc with malloc attribute |

Checks

Context Check Description
ci/checkpatch warning coding style issues
ci/Intel-compilation success Compilation OK

Commit Message

Stephen Hemminger July 30, 2020, 5:13 p.m. UTC
  Gcc (and recent versions of Clang) have a function attribute that
hints to the optimizer that it doesn't need to worry about aliasing
on a pointer returned from malloc.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/librte_eal/include/rte_common.h | 11 +++++++++++
 lib/librte_eal/include/rte_malloc.h |  4 ++++
 2 files changed, 15 insertions(+)
  

Patch

diff --git a/lib/librte_eal/include/rte_common.h b/lib/librte_eal/include/rte_common.h
index 8f487a563dfc..6b85374c0fe5 100644
--- a/lib/librte_eal/include/rte_common.h
+++ b/lib/librte_eal/include/rte_common.h
@@ -112,6 +112,17 @@  typedef uint16_t unaligned_uint16_t;
 #define __rte_restrict restrict
 #endif
 
+/**
+ * Mark function as returing a pointer which if non-NULL
+ * cannot alias any other valid pointer and that the memory
+ * contents are undefined. I.e behaves like malloc.
+ */
+#if RTE_CC_IS_GNU
+#define __rte_malloc __attribute__((malloc))
+#else
+#define __rte_malloc
+#endif
+
 /**
  * definition to mark a variable or function parameter as used so
  * as to avoid a compiler warning
diff --git a/lib/librte_eal/include/rte_malloc.h b/lib/librte_eal/include/rte_malloc.h
index 42ca05182f8e..721560122c70 100644
--- a/lib/librte_eal/include/rte_malloc.h
+++ b/lib/librte_eal/include/rte_malloc.h
@@ -79,6 +79,7 @@  rte_malloc(const char *type, size_t size, unsigned align);
  *     align is not a power of two).
  *   - Otherwise, the pointer to the allocated object.
  */
+__rte_malloc
 void *
 rte_zmalloc(const char *type, size_t size, unsigned align);
 
@@ -105,6 +106,7 @@  rte_zmalloc(const char *type, size_t size, unsigned align);
  *     align is not a power of two).
  *   - Otherwise, the pointer to the allocated object.
  */
+__rte_malloc
 void *
 rte_calloc(const char *type, size_t num, size_t size, unsigned align);
 
@@ -180,6 +182,7 @@  rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket);
  *     align is not a power of two).
  *   - Otherwise, the pointer to the allocated object.
  */
+__rte_malloc
 void *
 rte_malloc_socket(const char *type, size_t size, unsigned align, int socket);
 
@@ -236,6 +239,7 @@  rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket);
  *     align is not a power of two).
  *   - Otherwise, the pointer to the allocated object.
  */
+__rte_malloc
 void *
 rte_calloc_socket(const char *type, size_t num, size_t size, unsigned align, int socket);