From owner-freebsd-hackers Sun Apr 6 06:21:41 1997 Return-Path: Received: (from root@localhost) by freefall.freebsd.org (8.8.5/8.8.5) id GAA20781 for hackers-outgoing; Sun, 6 Apr 1997 06:21:41 -0700 (PDT) Received: from hda.hda.com (hda-bicnet.bicnet.net [207.198.1.121]) by freefall.freebsd.org (8.8.5/8.8.5) with ESMTP id GAA20768 for ; Sun, 6 Apr 1997 06:21:35 -0700 (PDT) Received: (from dufault@localhost) by hda.hda.com (8.8.5/8.8.5) id JAA10420; Sun, 6 Apr 1997 09:19:04 -0400 (EDT) From: Peter Dufault Message-Id: <199704061319.JAA10420@hda.hda.com> Subject: Re: How to declare device driver variables and data structures ? In-Reply-To: from Hellmuth Michaelis at "Apr 6, 97 12:04:01 pm" To: hm@kts.org Date: Sun, 6 Apr 1997 09:19:03 -0400 (EDT) Cc: freebsd-hackers@freebsd.org X-Mailer: ELM [version 2.4ME+ PL25 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: owner-hackers@freebsd.org X-Loop: FreeBSD.org Precedence: bulk > Inside a device driver or a piece of kernel software, how should one decleare > the size of variables (if one has the choice): > > i.e. if i know a variable can only have values 0 ... 32, shall i make it > an unsigned char ? Is it slower to make such variables an unsigned int > or an unsigned short ? What are the space vs. speed considerations ? There is something special about that data type so make it a typedef, or for small ranges, an enum. > i.e. if i know a variable can only have values 0 ... 2048, shall i make it > an unsigned short ? Is it slower to make such variables an unsigned int ? It is an 11 bit temperature sensor? Make it a temp_sensor. I'd probably then make temp_sensor an unsigned int. You may have a memory starved target where you conditionally make it an unsigned short, or if you have arrays of 4096 of them make it a short. > another i.e. for boolean variables, is it better to make them chars, shorts > or ints ? signed or unsigned ? Personally I always make booleans ints because the knowledge that an int is a boolean is deep in C, and boolean-ness is best indicated by naming (e.g., temp_is_high()) than type (boolean temp_check()) I'll never convince other people of this, though, and I try to preserve the approach of whatever I'm working on. > What is the speed of unsigned variables vs. signed variables ? On a modern X86? Everything is drowned out by accessing an I/O port in the driver. I'm sure there are instructions for both flavors and they are about the same. > What is with future ports to other architectures ? I do run my software on other architectures, usually smaller ones, and that is where typedefs that correspond to the data type usage come in handy. > How does this all apply to variables being part of structures ? Recursively? One point is that attempts to micro optimize size using smallest possible data types may be pointless in modern architectures due to alignment issues. -- Peter Dufault (dufault@hda.com) Realtime Machine Control and Simulation HD Associates, Inc. Voice: 508 433 6936