Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 15 Dec 2004 17:56:05 -0800
From:      Brooks Davis <brooks@one-eyed-alien.net>
To:        Kenny Chamber <kenny@gem-hs.org>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: kernel malloc usage
Message-ID:  <20041216015605.GA28633@odin.ac.hmc.edu>
In-Reply-To: <41C0E74F.1070404@gem-hs.org>
References:  <41C0E74F.1070404@gem-hs.org>

next in thread | previous in thread | raw e-mail | index | archive | help

--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--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20041216015605.GA28633>