From owner-svn-src-head@freebsd.org Sat Jun 2 22:20:10 2018 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 958F6FEF061; Sat, 2 Jun 2018 22:20:10 +0000 (UTC) (envelope-from mjg@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 43B017BB5A; Sat, 2 Jun 2018 22:20:10 +0000 (UTC) (envelope-from mjg@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 20E4915C83; Sat, 2 Jun 2018 22:20:10 +0000 (UTC) (envelope-from mjg@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w52MK9vf026248; Sat, 2 Jun 2018 22:20:09 GMT (envelope-from mjg@FreeBSD.org) Received: (from mjg@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w52MK9TT026244; Sat, 2 Jun 2018 22:20:09 GMT (envelope-from mjg@FreeBSD.org) Message-Id: <201806022220.w52MK9TT026244@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: mjg set sender to mjg@FreeBSD.org using -f From: Mateusz Guzik Date: Sat, 2 Jun 2018 22:20:09 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r334545 - in head/sys: contrib/zstd/lib/freebsd kern netinet/libalias sys X-SVN-Group: head X-SVN-Commit-Author: mjg X-SVN-Commit-Paths: in head/sys: contrib/zstd/lib/freebsd kern netinet/libalias sys X-SVN-Commit-Revision: 334545 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 02 Jun 2018 22:20:10 -0000 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 +#ifdef _KERNEL +#include +#endif #include #include #include @@ -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,