Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 6 Apr 1997 09:19:03 -0400 (EDT)
From:      Peter Dufault <dufault@hda.com>
To:        hm@kts.org
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: How to declare device driver variables and data structures ?
Message-ID:  <199704061319.JAA10420@hda.hda.com>
In-Reply-To: <m0wDonp-00002GC@ernie.kts.org> from Hellmuth Michaelis at "Apr 6, 97 12:04:01 pm"

next in thread | previous in thread | raw e-mail | index | archive | help
> 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



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