From owner-freebsd-hackers Thu Jan 28 12:20:27 1999 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id MAA07327 for freebsd-hackers-outgoing; Thu, 28 Jan 1999 12:20:27 -0800 (PST) (envelope-from owner-freebsd-hackers@FreeBSD.ORG) Received: from whistle.com (s205m131.whistle.com [207.76.205.131]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id MAA07283 for ; Thu, 28 Jan 1999 12:20:20 -0800 (PST) (envelope-from archie@whistle.com) Received: (from smap@localhost) by whistle.com (8.7.5/8.6.12) id MAA08818; Thu, 28 Jan 1999 12:20:11 -0800 (PST) Received: from bubba.whistle.com( 207.76.205.7) by whistle.com via smap (V2.0) id xma008811; Thu, 28 Jan 99 12:19:41 -0800 Received: (from archie@localhost) by bubba.whistle.com (8.8.7/8.6.12) id MAA03434; Thu, 28 Jan 1999 12:19:41 -0800 (PST) From: Archie Cobbs Message-Id: <199901282019.MAA03434@bubba.whistle.com> Subject: Re: rules to allocate buffers in device drivers In-Reply-To: <199901281349.OAA24119@chouette.inria.fr> from Emmanuel Duros at "Jan 28, 99 02:49:03 pm" To: Emmanuel.Duros@sophia.inria.fr (Emmanuel Duros) Date: Thu, 28 Jan 1999 12:19:41 -0800 (PST) Cc: freebsd-hackers@FreeBSD.ORG X-Mailer: ELM [version 2.4ME+ PL38 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG 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 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