linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andreas Dilger <adilger@turbolinux.com>
To: Matthew Dharm <mdharm-kernel@one-eyed-alien.net>
Cc: Kernel Developer List <linux-kernel@vger.kernel.org>
Subject: Re: Simple example of using slab allocator?
Date: Mon, 18 Jun 2001 10:56:36 -0600 (MDT)	[thread overview]
Message-ID: <200106181656.f5IGuamN013348@webber.adilger.int> (raw)
In-Reply-To: <20010615151901.G28394@one-eyed-alien.net> "from Matthew Dharm at Jun 15, 2001 03:19:01 pm"

Matthew Dharm writes:
> For 2.5, I'm planning on switching my driver over to the slab allocator,
> for a variety of reasons.  Does anyone have a _dead_ simple example of how
> to use such a beast?  I've seen the various web pages and document
> explaining the API, but I love to see working examples for reference (and
> to fill in the blanks).

The slab allocator IS dead simple to use, basically:

- driver global variable:

kmem_cache_t *usb_mass_cachep;
	
- in the driver init function:

	usb_mass_cachep = kmem_cache_create("usb_mass_cache",
					    sizeof(struct whatever),
					    0, SLAB_HWCACHE_ALIGN,
					    NULL, NULL);
	(check for NULL usb_mass_slab)

- in the driver cleanup function:

	if (usb_mass_cachep && kmem_cache_destroy(usb_mass_cachep))
		printk(KERN_ERR "usb_mass_cache: not all structures freed\n");

- wherever you need an item from the slab cache:

	whateverp = kmem_cache_alloc(usb_mass_cachep, GFP_KERNEL);
	(check for NULL whateverp)

- when you are done with it:

	kmem_cache_free(usb_mass_cachep, whateverp);

Notes:
- if you have a slab leak and you don't free all of the items (hence the slab
  cache is not removed), you will probably get an oops when you reload the
  driver.  You can only have one slab cache per name ("usb_mass_cache" here).
- You may need different alignment (SLAB_HWCACHE_ALIGN), or not
- You may need different allocation policy (GFP_KERNEL), or not

Cheers, Andreas
-- 
Andreas Dilger  \ "If a man ate a pound of pasta and a pound of antipasto,
                 \  would they cancel out, leaving him still hungry?"
http://www-mddsp.enel.ucalgary.ca/People/adilger/               -- Dogbert

  reply	other threads:[~2001-06-18 16:57 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-06-15 22:19 Simple example of using slab allocator? Matthew Dharm
2001-06-18 16:56 ` Andreas Dilger [this message]
2001-06-19  6:05   ` Matthew Dharm
2001-06-19  7:51     ` Andreas Dilger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200106181656.f5IGuamN013348@webber.adilger.int \
    --to=adilger@turbolinux.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mdharm-kernel@one-eyed-alien.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).