aboutsummaryrefslogtreecommitdiffstats
path: root/include/os
diff options
context:
space:
mode:
authorRichard Yao <[email protected]>2023-05-26 18:47:52 -0400
committerGitHub <[email protected]>2023-05-26 15:47:52 -0700
commit0f03a411615a797425de488eecfaaf63fc41acfe (patch)
tree8716d218bd3e42d453da04046671e3df371626ed /include/os
parent20494d47d22a60964274c73db3b22bc385eb9667 (diff)
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 <[email protected]> Signed-off-by: Richard Yao <[email protected]> Closes #14827
Diffstat (limited to 'include/os')
-rw-r--r--include/os/freebsd/spl/sys/kmem.h3
-rw-r--r--include/os/linux/spl/sys/kmem.h16
-rw-r--r--include/os/linux/spl/sys/vmem.h6
3 files changed, 14 insertions, 11 deletions
diff --git a/include/os/freebsd/spl/sys/kmem.h b/include/os/freebsd/spl/sys/kmem.h
index 27d290863..c63379931 100644
--- a/include/os/freebsd/spl/sys/kmem.h
+++ b/include/os/freebsd/spl/sys/kmem.h
@@ -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);
diff --git a/include/os/linux/spl/sys/kmem.h b/include/os/linux/spl/sys/kmem.h
index 594425f7b..8a203f7bb 100644
--- a/include/os/linux/spl/sys/kmem.h
+++ b/include/os/linux/spl/sys/kmem.h
@@ -31,10 +31,10 @@
#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);
/*
diff --git a/include/os/linux/spl/sys/vmem.h b/include/os/linux/spl/sys/vmem.h
index e77af2a7a..92585a17e 100644
--- a/include/os/linux/spl/sys/vmem.h
+++ b/include/os/linux/spl/sys/vmem.h
@@ -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);