Skip to contentSkip to conversation

eal: mark rte_malloc with malloc attribute

RFC New Stephen Hemminger 1 participant 1 message Jul 30, 2020

lore
Share & tools
lore
#0 lore raw

RFC 1/1 eal: mark rte_malloc with malloc attribute

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.

---
 lib/librte_eal/include/rte_common.h | 11 +++++++++++
 lib/librte_eal/include/rte_malloc.h |  4 ++++
 2 files changed, 15 insertions(+)
lib/librte_eal/include/rte_common.h +11 -0
index 8f487a563dfc..6b85374c0fe5 100644
@@ -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
lib/librte_eal/include/rte_malloc.h +4 -0
index 42ca05182f8e..721560122c70 100644
@@ -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);
 
Signature · 1 line
2.27.0