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:
parent
c51e28f46f
commit
34c538c356
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=334545
@ -35,6 +35,7 @@
|
|||||||
|
|
||||||
MALLOC_DECLARE(M_ZSTD);
|
MALLOC_DECLARE(M_ZSTD);
|
||||||
|
|
||||||
|
#undef malloc
|
||||||
#define malloc(x) (malloc)((x), M_ZSTD, M_WAITOK)
|
#define malloc(x) (malloc)((x), M_ZSTD, M_WAITOK)
|
||||||
#define free(x) (free)((x), M_ZSTD)
|
#define free(x) (free)((x), M_ZSTD)
|
||||||
#define calloc(a, b) (mallocarray)((a), (b), M_ZSTD, M_WAITOK | M_ZERO)
|
#define calloc(a, b) (mallocarray)((a), (b), M_ZSTD, M_WAITOK | M_ZERO)
|
||||||
|
@ -549,7 +549,7 @@ malloc_dbg(caddr_t *vap, size_t *sizep, struct malloc_type *mtp,
|
|||||||
* the allocation fails.
|
* the allocation fails.
|
||||||
*/
|
*/
|
||||||
void *
|
void *
|
||||||
malloc(size_t size, struct malloc_type *mtp, int flags)
|
(malloc)(size_t size, struct malloc_type *mtp, int flags)
|
||||||
{
|
{
|
||||||
int indx;
|
int indx;
|
||||||
caddr_t va;
|
caddr_t va;
|
||||||
|
@ -41,6 +41,7 @@ MALLOC_DECLARE(M_ALIAS);
|
|||||||
|
|
||||||
/* Use kernel allocator. */
|
/* Use kernel allocator. */
|
||||||
#if defined(_SYS_MALLOC_H_)
|
#if defined(_SYS_MALLOC_H_)
|
||||||
|
#undef malloc
|
||||||
#define malloc(x) malloc(x, M_ALIAS, M_NOWAIT|M_ZERO)
|
#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 calloc(n, x) mallocarray((n), (x), M_ALIAS, M_NOWAIT|M_ZERO)
|
||||||
#define free(x) free(x, M_ALIAS)
|
#define free(x) free(x, M_ALIAS)
|
||||||
|
@ -38,6 +38,9 @@
|
|||||||
#define _SYS_MALLOC_H_
|
#define _SYS_MALLOC_H_
|
||||||
|
|
||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
|
#ifdef _KERNEL
|
||||||
|
#include <sys/systm.h>
|
||||||
|
#endif
|
||||||
#include <sys/queue.h>
|
#include <sys/queue.h>
|
||||||
#include <sys/_lock.h>
|
#include <sys/_lock.h>
|
||||||
#include <sys/_mutex.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 free_domain(void *addr, struct malloc_type *type);
|
||||||
void *malloc(size_t size, struct malloc_type *type, int flags) __malloc_like
|
void *malloc(size_t size, struct malloc_type *type, int flags) __malloc_like
|
||||||
__result_use_check __alloc_size(1);
|
__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,
|
void *malloc_domain(size_t size, struct malloc_type *type, int domain,
|
||||||
int flags) __malloc_like __result_use_check __alloc_size(1);
|
int flags) __malloc_like __result_use_check __alloc_size(1);
|
||||||
void *mallocarray(size_t nmemb, size_t size, struct malloc_type *type,
|
void *mallocarray(size_t nmemb, size_t size, struct malloc_type *type,
|
||||||
|
Loading…
Reference in New Issue
Block a user