summaryrefslogtreecommitdiffstats
path: root/sys/kern
diff options
context:
space:
mode:
authorpjd <pjd@FreeBSD.org>2005-11-03 13:48:59 +0000
committerpjd <pjd@FreeBSD.org>2005-11-03 13:48:59 +0000
commit445c73f2695593bf80daadad4aac48160c064ff8 (patch)
treea8ac7d23ec78020d4b6117742c5bebf08113310a /sys/kern
parentc2b1833590814fc424c40c61d708839d0ce385c3 (diff)
downloadFreeBSD-src-445c73f2695593bf80daadad4aac48160c064ff8.zip
FreeBSD-src-445c73f2695593bf80daadad4aac48160c064ff8.tar.gz
Detect memory leaks when memory type is being destroyed.
This is very helpful for detecting kernel modules memory leaks on unload. Discussed and reviewed by: rwatson
Diffstat (limited to 'sys/kern')
-rw-r--r--sys/kern/kern_malloc.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/sys/kern/kern_malloc.c b/sys/kern/kern_malloc.c
index 6073e9c..4b707aa 100644
--- a/sys/kern/kern_malloc.c
+++ b/sys/kern/kern_malloc.c
@@ -608,7 +608,10 @@ void
malloc_uninit(void *data)
{
struct malloc_type_internal *mtip;
+ struct malloc_type_stats *mtsp;
struct malloc_type *mtp, *temp;
+ long temp_allocs, temp_bytes;
+ int i;
mtp = data;
KASSERT(mtp->ks_handle != NULL, ("malloc_deregister: cookie NULL"));
@@ -625,6 +628,24 @@ malloc_uninit(void *data)
kmemstatistics = mtp->ks_next;
kmemcount--;
mtx_unlock(&malloc_mtx);
+
+ /*
+ * Look for memory leaks.
+ */
+ temp_allocs = temp_bytes = 0;
+ for (i = 0; i < MAXCPU; i++) {
+ mtsp = &mtip->mti_stats[i];
+ temp_allocs += mtsp->mts_numallocs;
+ temp_allocs -= mtsp->mts_numfrees;
+ temp_bytes += mtsp->mts_memalloced;
+ temp_bytes -= mtsp->mts_memfreed;
+ }
+ if (temp_allocs > 0 || temp_bytes > 0) {
+ printf("Warning: memory type %s leaked memory on destroy "
+ "(%ld allocations, %ld bytes leaked).\n", mtp->ks_shortdesc,
+ temp_allocs, temp_bytes);
+ }
+
uma_zfree(mt_zone, mtip);
}
OpenPOWER on IntegriCloud