]> git.proxmox.com Git - mirror_zfs.git/commitdiff
Use __attribute__((malloc)) on memory allocation functions
authorRichard Yao <richard.yao@alumni.stonybrook.edu>
Fri, 26 May 2023 22:47:52 +0000 (18:47 -0400)
committerGitHub <noreply@github.com>
Fri, 26 May 2023 22:47:52 +0000 (15:47 -0700)
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

include/os/freebsd/spl/sys/kmem.h
include/os/linux/spl/sys/kmem.h
include/os/linux/spl/sys/vmem.h
include/sys/abd.h
lib/libspl/include/umem.h

index 27d290863c0bce8c236de0bd8efd5b511e6eb8ca..c633799318d5a65f0fe2a290494a8094bd81acf2 100644 (file)
@@ -75,7 +75,7 @@ typedef struct kmem_cache {
 extern uint64_t spl_kmem_cache_inuse(kmem_cache_t *cache);
 extern uint64_t spl_kmem_cache_entry_size(kmem_cache_t *cache);
 
-__attribute__((alloc_size(1)))
+__attribute__((malloc, alloc_size(1)))
 void *zfs_kmem_alloc(size_t size, int kmflags);
 void zfs_kmem_free(void *buf, size_t size);
 uint64_t kmem_size(void);
@@ -83,6 +83,7 @@ kmem_cache_t *kmem_cache_create(const char *name, size_t bufsize, size_t align,
     int (*constructor)(void *, void *, int), void (*destructor)(void *, void *),
     void (*reclaim)(void *) __unused, void *private, vmem_t *vmp, int cflags);
 void kmem_cache_destroy(kmem_cache_t *cache);
+__attribute__((malloc))
 void *kmem_cache_alloc(kmem_cache_t *cache, int flags);
 void kmem_cache_free(kmem_cache_t *cache, void *buf);
 boolean_t kmem_cache_reap_active(void);
index 594425f7b29783466f970591e1a9195c26a58678..8a203f7bb8e22ee3267dd8398f4d8d5ba09f79c0 100644 (file)
 #include <linux/vmalloc.h>
 
 extern int kmem_debugging(void);
-extern char *kmem_vasprintf(const char *fmt, va_list ap)
-    __attribute__((format(printf, 1, 0)));
-extern char *kmem_asprintf(const char *fmt, ...)
-    __attribute__((format(printf, 1, 2)));
+__attribute__((format(printf, 1, 0)))
+extern char *kmem_vasprintf(const char *fmt, va_list ap);
+__attribute__((format(printf, 1, 2)))
+extern char *kmem_asprintf(const char *fmt, ...);
 extern char *kmem_strdup(const char *str);
 extern void kmem_strfree(char *str);
 
@@ -186,10 +186,10 @@ extern unsigned int spl_kmem_alloc_max;
 #define        kmem_free(ptr, sz)      spl_kmem_free((ptr), (sz))
 #define        kmem_cache_reap_active  spl_kmem_cache_reap_active
 
-extern void *spl_kmem_alloc(size_t sz, int fl, const char *func, int line)
-    __attribute__((alloc_size(1)));
-extern void *spl_kmem_zalloc(size_t sz, int fl, const char *func, int line)
-    __attribute__((alloc_size(1)));
+__attribute__((malloc, alloc_size(1)))
+extern void *spl_kmem_alloc(size_t sz, int fl, const char *func, int line);
+__attribute__((malloc, alloc_size(1)))
+extern void *spl_kmem_zalloc(size_t sz, int fl, const char *func, int line);
 extern void spl_kmem_free(const void *ptr, size_t sz);
 
 /*
index e77af2a7a48cb777c12f211922ed61d7a36ab20f..92585a17e2635ac9457d5bf5dc2055505b10f7e9 100644 (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);
index 82c51cb05cbcdecd670da3475e6fecb11eb41444..750f9986c1da60869e6f1107a759b3372747cd50 100644 (file)
@@ -86,10 +86,15 @@ extern int zfs_abd_scatter_enabled;
  * Allocations and deallocations
  */
 
+__attribute__((malloc))
 abd_t *abd_alloc(size_t, boolean_t);
+__attribute__((malloc))
 abd_t *abd_alloc_linear(size_t, boolean_t);
+__attribute__((malloc))
 abd_t *abd_alloc_gang(void);
+__attribute__((malloc))
 abd_t *abd_alloc_for_io(size_t, boolean_t);
+__attribute__((malloc))
 abd_t *abd_alloc_sametype(abd_t *, size_t);
 boolean_t abd_size_alloc_linear(size_t);
 void abd_gang_add(abd_t *, abd_t *, boolean_t);
index 77c21672125307f68072aa22cd000c8b14ecff5f..9039212baf14fe0c93d115f210858bb1bbfc0ada 100644 (file)
@@ -83,7 +83,7 @@ const char *_umem_debug_init(void);
 const char *_umem_options_init(void);
 const char *_umem_logging_init(void);
 
-__attribute__((alloc_size(1)))
+__attribute__((malloc, alloc_size(1)))
 static inline void *
 umem_alloc(size_t size, int flags)
 {
@@ -96,7 +96,7 @@ umem_alloc(size_t size, int flags)
        return (ptr);
 }
 
-__attribute__((alloc_size(1)))
+__attribute__((malloc, alloc_size(1)))
 static inline void *
 umem_alloc_aligned(size_t size, size_t align, int flags)
 {
@@ -118,7 +118,7 @@ umem_alloc_aligned(size_t size, size_t align, int flags)
        return (ptr);
 }
 
-__attribute__((alloc_size(1)))
+__attribute__((malloc, alloc_size(1)))
 static inline void *
 umem_zalloc(size_t size, int flags)
 {
@@ -188,6 +188,7 @@ umem_cache_destroy(umem_cache_t *cp)
        umem_free(cp, sizeof (umem_cache_t));
 }
 
+__attribute__((malloc))
 static inline void *
 umem_cache_alloc(umem_cache_t *cp, int flags)
 {