Interface-Specific Types
Most commonly used data types in the kernel have their own
typedef statement, thus preventing any portability problems. For
example, a process identifier (pid) is usually pid_t instead of
int. Using pid_t masks any possible difference in the
actual data typing. I use the expression ``interface-specific'' to
refer to the programming interface to specific data items.
Other data items that belong to a specific ``standard'' type can be
considered interface-specific as well. A jiffy count, for instance, is
always unsigned long, independent of its actual size--would
you like using jiffy_t every so often?
I’m concentrating here on the first class of interface-specific
types, the ones ending in _t.
The complete list of _t types appears in
<linux/types.h>, but the list is rarely useful. When you need
a specific type, you’ll find it in the prototype of the functions
you need to call or in the data structures you use.
Whenever your driver uses functions that require such ``custom'' types and you don’t follow the convention, the compiler issues a warning; if you use the -Wall compiler flag and are careful to remove all the warnings, you can feel confident that your code is portable.
The main problem with _t data items is that when
you need to print them, it’s not always easy to choose the right
printk or printf format, and warnings you
resolve on one architecture reappear on another. For example, how
would you print a size_t which is unsigned long on some platforms ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access