svn commit: r334545 - in head/sys: contrib/zstd/lib/freebsd kern netinet/libalias sys
Mateusz Guzik
mjg at FreeBSD.org
Sat Jun 2 22:20:10 UTC 2018
Author: mjg
Date: Sat Jun 2 22:20:09 2018
New Revision: 334545
URL: https://svnweb.freebsd.org/changeset/base/334545
Log:
malloc: try to use builtins for zeroing at the callsite
Plenty of allocation sites pass M_ZERO and sizes which are small and known
at compilation time. Handling them internally in malloc loses this information
and results in avoidable calls to memset.
Instead, let the compiler take the advantage of it whenever possible.
Discussed with: jeff
Modified:
head/sys/contrib/zstd/lib/freebsd/stdlib.h
head/sys/kern/kern_malloc.c
head/sys/netinet/libalias/alias_mod.h
head/sys/sys/malloc.h
Modified: head/sys/contrib/zstd/lib/freebsd/stdlib.h
==============================================================================
--- head/sys/contrib/zstd/lib/freebsd/stdlib.h Sat Jun 2 22:12:57 2018 (r334544)
+++ head/sys/contrib/zstd/lib/freebsd/stdlib.h Sat Jun 2 22:20:09 2018 (r334545)
@@ -35,6 +35,7 @@
MALLOC_DECLARE(M_ZSTD);
+#undef malloc
#define malloc(x) (malloc)((x), M_ZSTD, M_WAITOK)
#define free(x) (free)((x), M_ZSTD)
#define calloc(a, b) (mallocarray)((a), (b), M_ZSTD, M_WAITOK | M_ZERO)
Modified: head/sys/kern/kern_malloc.c
==============================================================================
--- head/sys/kern/kern_malloc.c Sat Jun 2 22:12:57 2018 (r334544)
+++ head/sys/kern/kern_malloc.c Sat Jun 2 22:20:09 2018 (r334545)
@@ -549,7 +549,7 @@ malloc_dbg(caddr_t *vap, size_t *sizep, struct malloc_
* the allocation fails.
*/
void *
-malloc(size_t size, struct malloc_type *mtp, int flags)
+(malloc)(size_t size, struct malloc_type *mtp, int flags)
{
int indx;
caddr_t va;
Modified: head/sys/netinet/libalias/alias_mod.h
==============================================================================
--- head/sys/netinet/libalias/alias_mod.h Sat Jun 2 22:12:57 2018 (r334544)
+++ head/sys/netinet/libalias/alias_mod.h Sat Jun 2 22:20:09 2018 (r334545)
@@ -41,6 +41,7 @@ MALLOC_DECLARE(M_ALIAS);
/* Use kernel allocator. */
#if defined(_SYS_MALLOC_H_)
+#undef malloc
#define malloc(x) malloc(x, M_ALIAS, M_NOWAIT|M_ZERO)
#define calloc(n, x) mallocarray((n), (x), M_ALIAS, M_NOWAIT|M_ZERO)
#define free(x) free(x, M_ALIAS)
Modified: head/sys/sys/malloc.h
==============================================================================
--- head/sys/sys/malloc.h Sat Jun 2 22:12:57 2018 (r334544)
+++ head/sys/sys/malloc.h Sat Jun 2 22:20:09 2018 (r334545)
@@ -38,6 +38,9 @@
#define _SYS_MALLOC_H_
#include <sys/param.h>
+#ifdef _KERNEL
+#include <sys/systm.h>
+#endif
#include <sys/queue.h>
#include <sys/_lock.h>
#include <sys/_mutex.h>
@@ -183,6 +186,22 @@ void free(void *addr, struct malloc_type *type);
void free_domain(void *addr, struct malloc_type *type);
void *malloc(size_t size, struct malloc_type *type, int flags) __malloc_like
__result_use_check __alloc_size(1);
+#ifdef _KERNEL
+#define malloc(size, type, flags) ({ \
+ void *_malloc_item; \
+ size_t _size = (size); \
+ if (__builtin_constant_p(size) && __builtin_constant_p(flags) &&\
+ ((flags) & M_ZERO)) { \
+ _malloc_item = malloc(_size, type, (flags) &~ M_ZERO); \
+ if (((flags) & M_WAITOK) || _malloc_item != NULL) \
+ bzero(_malloc_item, _size); \
+ } else { \
+ _malloc_item = malloc(_size, type, flags); \
+ } \
+ _malloc_item; \
+})
+#endif
+
void *malloc_domain(size_t size, struct malloc_type *type, int domain,
int flags) __malloc_like __result_use_check __alloc_size(1);
void *mallocarray(size_t nmemb, size_t size, struct malloc_type *type,
More information about the svn-src-all
mailing list