From owner-freebsd-hackers@FreeBSD.ORG Thu Dec 16 01:56:04 2004 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 151F016A4CE for ; Thu, 16 Dec 2004 01:56:04 +0000 (GMT) Received: from odin.ac.hmc.edu (Odin.AC.HMC.Edu [134.173.32.75]) by mx1.FreeBSD.org (Postfix) with ESMTP id CC4E343D60 for ; Thu, 16 Dec 2004 01:56:03 +0000 (GMT) (envelope-from brdavis@odin.ac.hmc.edu) Received: from odin.ac.hmc.edu (localhost.localdomain [127.0.0.1]) by odin.ac.hmc.edu (8.13.0/8.13.0) with ESMTP id iBG1u5Ze029694; Wed, 15 Dec 2004 17:56:05 -0800 Received: (from brdavis@localhost) by odin.ac.hmc.edu (8.13.0/8.13.0/Submit) id iBG1u5cH029693; Wed, 15 Dec 2004 17:56:05 -0800 Date: Wed, 15 Dec 2004 17:56:05 -0800 From: Brooks Davis To: Kenny Chamber Message-ID: <20041216015605.GA28633@odin.ac.hmc.edu> References: <41C0E74F.1070404@gem-hs.org> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="liOOAslEiF7prFVr" Content-Disposition: inline In-Reply-To: <41C0E74F.1070404@gem-hs.org> User-Agent: Mutt/1.4.1i X-Virus-Scanned: by amavisd-new X-Spam-Status: No, hits=0.0 required=8.0 tests=none autolearn=no version=2.63 X-Spam-Checker-Version: SpamAssassin 2.63 (2004-01-11) on odin.ac.hmc.edu cc: freebsd-hackers@freebsd.org Subject: Re: kernel malloc usage X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 16 Dec 2004 01:56:04 -0000 --liOOAslEiF7prFVr Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Dec 16, 2004 at 04:39:27AM +0300, Kenny Chamber wrote: > I have a question concerning the use of malloc to allocate small amounts > of memory for packet wrappers for certain packets. > Basically, I'm using malloc in much the same way as one would use it in > a standard userland program: allocating small chunks and freeing them > again a short(ish) time later. > A colleague said this was a bad idea, since malloc should only really be > used to allocate larger chunks (PAGE_SIZE and over) at driver > initialisation. Management and partition of the allocated space should > be done by the driver. His reason was that over time, the kernel heap > memory gets progressively more fragmented until it becomes difficult to > get larger blocks. > Is this true? I seem to remember that FreeBSD has a slab allocator, > which IIRC is particularly good at allocating small chunks. > > Is it a better idea to write my own allocators which use a larger block > of memory allocated by malloc at driver start-time, or can I use malloc > as in userland? Your colleague is wrong in most, but not all cases. We have a slab allocator which will do just fine with small allocations. However, if you are allocating these small chunks very rapidly or they are badly dissimilar to the standard bucket sizes (16,32,64,128,256,512,1024,2048,4096) you may wish to create your own zone to handle these bits for you which could be more efficent. One nice thing about zones is that they can pre-initalize things for you making your code simpler in some cases. See zone(9) for details. Because we provide the ability to create custom zones, the decision to do your own management form malloced blocks would almost always be wrong. -- Brooks --=20 Any statement of the form "X is the one, true Y" is FALSE. PGP fingerprint 655D 519C 26A7 82E7 2529 9BF0 5D8E 8BE9 F238 1AD4 --liOOAslEiF7prFVr Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.1 (GNU/Linux) iD8DBQFBwOs1XY6L6fI4GtQRAuLRAJ4uDidRRuKgjcGIYJJlm/UrZ2PasACdHYt9 xhVSczTlxYsxxoetNhUwMg8= =LhoP -----END PGP SIGNATURE----- --liOOAslEiF7prFVr--