Version 5 (Anthony Rowe, 05/22/2007 03:22 am)

1 1 Anthony Rowe
2 1 Anthony Rowe
== Nano-RK Tips and Tricks ==
3 1 Anthony Rowe
Many of these suggestions are generally good practice for any resource constained development.
4 1 Anthony Rowe
5 4 Anthony Rowe
'''Don't Allocate Large Data Structures Inside Functions'''
6 2 Anthony Rowe
 * Allocating large data structures in functions puts them on the stack.  Typically your task's stack is only 128 bytes, so putting things like a network buffer on the stack will almost certainly cause a stack overflow. Instead, make large data structures global.
7 1 Anthony Rowe
8 1 Anthony Rowe
'''Avoid Recursive Function Calls'''
9 1 Anthony Rowe
 * Recursive function calls are also heavy when it comes to stack consumption.  Try to avoid them whenever possible
10 1 Anthony Rowe
11 1 Anthony Rowe
'''Use inline'''
12 3 Anthony Rowe
  * When it seems fit, use "inline" to avoid pushing things onto the stack for small functions.
13 3 Anthony Rowe
14 4 Anthony Rowe
'''Be Very Careful With Dynamic Memory'''
15 3 Anthony Rowe
 * Malloc does work, but if you frequently malloc and free memory you will create fragmentation.  Eventually this fragmentation can lead to malloc failing.  This is especially bad when you run your application for a few minutes and it seems fine, but then fails after a few hours.  In general, avoid using malloc whenever possible. If you must, use it in situations where the code can safely continue if allocation fails.
16 5 Anthony Rowe
17 5 Anthony Rowe
'''Use nrk_kprintf() Whenever Possible'''
18 5 Anthony Rowe
 * Strings used in normal printf() are stored in RAM!  nrk_kprintf() stores them in FLASH memory using the PSTR() macro.  Only use regular printf() when the string is dynamic (i.e. you use %d to print variables etc).