|
|
< Previous PageNext Page > |
The naming conventions for C++ have been defined for some time in the document I/O Kit Device Driver Design Guidelines. However, no conventions have been given for standard C code. Because standard C has an even greater chance of namespace collision than C++, it is essential that you follow these guidelines when writing C code for use in the kernel.
Because C does not have the benefit of classes, it is much easier to run into a naming conflict between two functions. For this reason, the following conventions are suggested:
Declare all functions and (global) variables static where possible to prevent them from being seen in the global namespace. If you need to share these across files within your KEXT, you can achieve a similar effect by declaring them __private_extern__
.
Each function name should use Java-style reverse DNS naming. For example, if your company is apple.com, you should begin each function with com_apple_
.
Follow the reverse DNS name with the name of your project. For example, if you work at Apple and were working on project Schlassen, you would start each function name (in drivers) with com_apple_driver_schlassen_
.
Use hierarchical names if you anticipate multiple projects with similar names coming from different parts of your company or organization.
Use macro expansion to save typing, for example PROJECT_eat
could expand to com_apple_driver_schlassen_pickle_eat
.
If you anticipate that the last part of a function name may be the same as the last part of another function name (for example, PROJECT1_eat
and PROJECT2_eat
), you should change the names to avoid confusion (for example, PROJECT1_eatpickle
and PROJECT2_eatburger
).
Avoid the following reserved prefixes:
OS
os
IO
io
Apple
apple
AAPL
aapl
Avoid conflicting with any names already in the kernel, and do not use prefixes similar to those of existing kernel functions that you may be working with.
Never begin a function name with an underscore (_).
Under no circumstances should you use common names for your functions without prefixing them with the name of your project in some form. These are some examples of unacceptable names:
getuseridentity
get_user_info
find
search
sort
quicksort
merge
console_log
In short, picking any name that you would normally pick for a function is generally a bad idea, because every other developer writing code is likely to pick the same name for his or her function.
Occasional conflicts are a fact of life. However, by following these few simple rules, you should be able to avoid the majority of common namespace pitfalls.
< Previous PageNext Page > |
Last updated: 2006-11-07
|
Get information on Apple products.
Visit the Apple Store online or at retail locations. 1-800-MY-APPLE Copyright © 2007 Apple Inc. All rights reserved. | Terms of use | Privacy Notice |