Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 28 Jan 1999 12:19:41 -0800 (PST)
From:      Archie Cobbs <archie@whistle.com>
To:        Emmanuel.Duros@sophia.inria.fr (Emmanuel Duros)
Cc:        freebsd-hackers@FreeBSD.ORG
Subject:   Re: rules to allocate buffers in device drivers
Message-ID:  <199901282019.MAA03434@bubba.whistle.com>
In-Reply-To: <199901281349.OAA24119@chouette.inria.fr> from Emmanuel Duros at "Jan 28, 99 02:49:03 pm"

next in thread | previous in thread | raw e-mail | index | archive | help
Emmanuel Duros writes:
> I am currently writing a network device driver for FreeBSD and it is
> still unclear to me how to allocate memory.
> 
> It seems that a common way of doing it is something like:
> 
> u_char  *buffer;
> buffer = malloc( SIZE, M_DEVBUF, M_NOWAIT);

Yes this works.. you also have to make sure buffer != NULL
after calling malloc, as it can be with M_NOWAIT.

M_DEVBUF is the type. You must free it with the same type argument.

M_NOWAIT or M_WAITOK are the latter choices. Don't use M_WAITOK
from within an interrupt context, because it can put you to sleep.

Also, you should use the MALLOC() and FREE() macros in <sys/malloc.h>
instead.

> However I have not seen something like this in a device driver:
> 
> u_char buffer[SIZE];
> 
> Is there a particular reason for not allocating buffers statically ?

Generally for two reasons:

 - It's considered bad practice to allocate a static buffer (that
   *always* takes up memory) if it *might not* get used (ie, your
   device probe fails).
 - Drivers typically support multiple instances of the device,
   in which case you don't know how much memory you need until
   runtime.

-Archie

___________________________________________________________________________
Archie Cobbs   *   Whistle Communications, Inc.  *   http://www.whistle.com

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message



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