Use __attribute__((malloc)) on memory allocation functions

This informs the C compiler that pointers returned from these functions
do not alias other functions, which allows it to do better code
optimization and should make the compiled code smaller.

References:
https://stackoverflow.com/a/53654773
https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-malloc-function-attribute
https://clang.llvm.org/docs/AttributeReference.html#malloc

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Richard Yao <richard.yao@alumni.stonybrook.edu>
Closes #14827
This commit is contained in:
Richard Yao
2023-05-26 18:47:52 -04:00
committed by GitHub
parent 20494d47d2
commit 0f03a41161
5 changed files with 23 additions and 14 deletions
+4 -2
View File
@@ -91,8 +91,10 @@ typedef struct vmem { } vmem_t;
#define vmem_zalloc(sz, fl) spl_vmem_zalloc((sz), (fl), __func__, __LINE__)
#define vmem_free(ptr, sz) spl_vmem_free((ptr), (sz))
extern void *spl_vmem_alloc(size_t sz, int fl, const char *func, int line);
extern void *spl_vmem_zalloc(size_t sz, int fl, const char *func, int line);
extern void *spl_vmem_alloc(size_t sz, int fl, const char *func, int line)
__attribute__((malloc, alloc_size(1)));
extern void *spl_vmem_zalloc(size_t sz, int fl, const char *func, int line)
__attribute__((malloc, alloc_size(1)));
extern void spl_vmem_free(const void *ptr, size_t sz);
int spl_vmem_init(void);