If you are making changes to MySQL, it is recommended that you follow the same coding guidelines as MySQL developers. This will make it easier for your code to work with what's already written, help you avoid bugs, and increase the chances of your patches being accepted by MySQL developers without serious modifications.
MySQL developers make their coding guidelines publicly available at http://dev.mysql.com/doc/internals/en/index.html.
In addition, I will provide a reorganized summary with some extra tips and comments from my own experience.
Here are some guidelines for preserving the code's stability while making changes:
Always remember that, more often than not, you are in a thread and must follow the rules of thread-safe programming.
Most global variables have an associated mutex that other threads will lock before accessing it. Make sure to learn which mutex is associated with each global variable, and lock it when accessing that variable.
Be aware that you have very little stack space available.
Chunks of memory larger than 100 bytes or so should be allocated
sql_alloc( ) or
When possible, choose
for small allocations.
) allocates memory from a pre-allocated connection
memory pool, while
is just a wrapper around the regular
malloc( ) call.
sql_alloc( ) can be called anywhere in
the stack of execution below
). To verify the stack position in question, set a breakpoint ...