svn commit: r367389 - in head/sys: kern sys

Mateusz Guzik mjg at FreeBSD.org
Thu Nov 5 16:21:22 UTC 2020


Author: mjg
Date: Thu Nov  5 16:21:21 2020
New Revision: 367389
URL: https://svnweb.freebsd.org/changeset/base/367389

Log:
  malloc: add a helper returning size allocated for given request
  
  Sample usage: kernel modules can decide whether to stick to malloc or
  create their own zone.
  
  Reviewed by:	markj
  Differential Revision:	https://reviews.freebsd.org/D27097

Modified:
  head/sys/kern/kern_malloc.c
  head/sys/sys/malloc.h

Modified: head/sys/kern/kern_malloc.c
==============================================================================
--- head/sys/kern/kern_malloc.c	Thu Nov  5 16:00:57 2020	(r367388)
+++ head/sys/kern/kern_malloc.c	Thu Nov  5 16:21:21 2020	(r367389)
@@ -1031,6 +1031,23 @@ reallocf(void *addr, size_t size, struct malloc_type *
 }
 
 /*
+ * 	malloc_size: returns the number of bytes allocated for a request of the
+ * 		     specified size
+ */
+size_t
+malloc_size(size_t size)
+{
+	int indx;
+
+	if (size > kmem_zmax)
+		return (0);
+	if (size & KMEM_ZMASK)
+		size = (size & ~KMEM_ZMASK) + KMEM_ZBASE;
+	indx = kmemsize[size >> KMEM_ZSHIFT];
+	return (kmemzones[indx].kz_size);
+}
+
+/*
  *	malloc_usable_size: returns the usable size of the allocation.
  */
 size_t

Modified: head/sys/sys/malloc.h
==============================================================================
--- head/sys/sys/malloc.h	Thu Nov  5 16:00:57 2020	(r367388)
+++ head/sys/sys/malloc.h	Thu Nov  5 16:21:21 2020	(r367389)
@@ -250,6 +250,7 @@ void	malloc_type_allocated(struct malloc_type *type, u
 void	malloc_type_freed(struct malloc_type *type, unsigned long size);
 void	malloc_type_list(malloc_type_list_func_t *, void *);
 void	malloc_uninit(void *);
+size_t	malloc_size(size_t);
 size_t	malloc_usable_size(const void *);
 void	*realloc(void *addr, size_t size, struct malloc_type *type, int flags)
 	    __result_use_check __alloc_size(2);


More information about the svn-src-head mailing list