From 27173bd0dd93081098b8676a7b2156fd227c52d3 Mon Sep 17 00:00:00 2001 From: phk Date: Fri, 1 Nov 2002 18:58:12 +0000 Subject: [PATCH] Introduce malloc_last_fail() which returns the number of seconds since malloc(9) failed last time. This is intended to help code adjust memory usage to the current circumstances. A typical use could be: if (malloc_last_fail() < 60) reduce_cache_by_one(); --- sys/kern/kern_malloc.c | 16 ++++++++++++++++ sys/sys/malloc.h | 1 + 2 files changed, 17 insertions(+) diff --git a/sys/kern/kern_malloc.c b/sys/kern/kern_malloc.c index 3d6307a14dc..577b5da6dfd 100644 --- a/sys/kern/kern_malloc.c +++ b/sys/kern/kern_malloc.c @@ -46,6 +46,7 @@ #include #include #include +#include #include #include @@ -134,6 +135,16 @@ static int sysctl_kern_mprof(SYSCTL_HANDLER_ARGS); static int sysctl_kern_malloc(SYSCTL_HANDLER_ARGS); +/* time_uptime of last malloc(9) failure */ +static time_t t_malloc_fail; + +int +malloc_last_fail(void) +{ + + return (time_uptime - t_malloc_fail); +} + /* * malloc: * @@ -191,6 +202,11 @@ malloc(size, type, flags) ksp->ks_maxused = ksp->ks_memuse; mtx_unlock(&ksp->ks_mtx); + if (!(flags & M_NOWAIT)) + KASSERT(va != NULL, ("malloc(M_WAITOK) returned NULL")); + if (va == NULL) { + t_malloc_fail = time_uptime; + } return ((void *) va); } diff --git a/sys/sys/malloc.h b/sys/sys/malloc.h index b909d2efa01..f61043995af 100644 --- a/sys/sys/malloc.h +++ b/sys/sys/malloc.h @@ -108,6 +108,7 @@ void *contigmalloc(unsigned long size, struct malloc_type *type, int flags, void free(void *addr, struct malloc_type *type); void *malloc(unsigned long size, struct malloc_type *type, int flags); void malloc_init(void *); +int malloc_last_fail(void); void malloc_uninit(void *); void *realloc(void *addr, unsigned long size, struct malloc_type *type, int flags); -- 2.45.2