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
This commit is contained in:
Mateusz Guzik 2020-11-05 16:21:21 +00:00
parent 870d4ba3ff
commit 16b971ed6d
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=367389
2 changed files with 18 additions and 0 deletions

View File

@ -1030,6 +1030,23 @@ reallocf(void *addr, size_t size, struct malloc_type *mtp, int flags)
return (mem);
}
/*
* 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.
*/

View File

@ -250,6 +250,7 @@ void malloc_type_allocated(struct malloc_type *type, unsigned long size);
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);