From 7d4ac97cda38def7d38fbb3803e02f089ab5ef5f Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Tue, 23 Jun 2015 22:59:15 -0700 Subject: [PATCH] kernel - Add zero-on-instantiate objcache ctor * When creating kmalloc-backed pools, M_ZERO cannot be passed to objcache_get() because the underlying kmalloc only occurs when the objcache is not recycling a cached entry. * Add a feature to the objcache whereby the allocation from backing store is zero'd. The reuse case will not be zerod, so users of this type of objcache must properly reset/cleanup fields before disposing of the object. * Used by HAMMER2. --- sys/kern/kern_objcache.c | 14 ++++++++++++++ sys/sys/objcache.h | 1 + 2 files changed, 15 insertions(+) diff --git a/sys/kern/kern_objcache.c b/sys/kern/kern_objcache.c index c0df062d45..8decb00474 100644 --- a/sys/kern/kern_objcache.c +++ b/sys/kern/kern_objcache.c @@ -506,6 +506,20 @@ objcache_malloc_alloc(void *allocator_args, int ocflags) ocflags & OC_MFLAGS)); } +/* + * Wrapper for malloc allocation routines, with initial zeroing + * (but objects are not zerod on reuse from cache). + */ +void * +objcache_malloc_alloc_zero(void *allocator_args, int ocflags) +{ + struct objcache_malloc_args *alloc_args = allocator_args; + + return (kmalloc(alloc_args->objsize, alloc_args->mtype, + (ocflags & OC_MFLAGS) | M_ZERO)); +} + + void objcache_malloc_free(void *obj, void *allocator_args) { diff --git a/sys/sys/objcache.h b/sys/sys/objcache.h index 92f28ae47b..cc3c8c27ea 100644 --- a/sys/sys/objcache.h +++ b/sys/sys/objcache.h @@ -91,6 +91,7 @@ struct objcache_malloc_args { #ifdef _KERNEL void *objcache_malloc_alloc(void *allocator_args, int ocflags); +void *objcache_malloc_alloc_zero(void *allocator_args, int ocflags); void objcache_malloc_free(void *obj, void *allocator_args); void *objcache_nop_alloc(void *allocator_args, int ocflags); -- 2.41.0