*BSD News Article 8626


Return to BSD News archive

Newsgroups: comp.unix.bsd
Path: sserve!manuel.anu.edu.au!munnari.oz.au!bunyip.cc.uq.oz.au!citec!sgccseh
From: sgccseh@citec.oz.au (Steve Hocking)
Subject: Patch for memory leak in kvm.c
Organization: CITEC
Date: Wed, 9 Dec 1992 01:10:24 GMT
Message-ID: <1992Dec9.011024.1320@citec.oz.au>
Summary: fixes a leak that occurs on multiple invocations of kvm_getprocs
Keywords: fixit, waah baby
Lines: 62

Find enclosed a patch that fixes a memory leak that occurs whenever
kvm_getprocs is called multiple times. This is not usually a problem, as
programs such as ps only call it once, but during my porting of top, it
became quite a problem (5Mb+ process very quickly) It does assume that
you've applied the 2 patches that came in Terry's patch kit.


	Cheers,


	Stephen

*** kvm.c.old	Wed Dec  9 10:39:00 1992
--- kvm.c	Wed Dec  9 10:45:05 1992
***************
*** 396,401 ****
--- 396,403 ----
  kvm_getprocs(what, arg)
  	int what, arg;
  {
+ 	static int ocopysize = -1;
+ 
  	if (kvminit == 0 && kvm_init(NULL, NULL, NULL, 0) == -1)
  		return (NULL);
  	if (!deadkernel) {
***************
*** 406,415 ****
  			return (-1);
  		}
  		copysize = ret;
! 		if ((kvmprocbase = (struct kinfo_proc *)malloc(copysize)) 
! 		     == NULL) {
! 			seterr("out of memory");
! 			return (-1);
  		}
  		if ((ret = getkerninfo(what, kvmprocbase, &copysize, 
  		     arg)) == -1) {
--- 408,421 ----
  			return (-1);
  		}
  		copysize = ret;
! 		if (copysize > ocopysize)
! 		{
! 			ocopysize = copysize;
! 			if ((kvmprocbase = (struct kinfo_proc *)realloc(kvmprocbase, copysize)) 
! 		     	== NULL) {
! 				seterr("out of memory");
! 				return (-1);
! 			}
  		}
  		if ((ret = getkerninfo(what, kvmprocbase, &copysize, 
  		     arg)) == -1) {
-- 
-----------------------------------------------------------------------------
          "Toddlers are the stormtroopers of the Lord of Entropy"

Stephen Hocking                                    sgccseh@citecuc.citec.oz.au
-- 
-----------------------------------------------------------------------------
          "Toddlers are the stormtroopers of the Lord of Entropy"

Stephen Hocking                                    sgccseh@citecuc.citec.oz.au