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
This commit is contained in:
Mateusz Guzik 2018-06-02 22:20:09 +00:00
parent c51e28f46f
commit 34c538c356
4 changed files with 22 additions and 1 deletions

View File

@ -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)

View File

@ -549,7 +549,7 @@ malloc_dbg(caddr_t *vap, size_t *sizep, struct malloc_type *mtp,
* 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;

View File

@ -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)

View File

@ -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,