2010年9月20日 星期一

C preprocessor: # and ## for concatenation

## preprocessor:
It is often useful to merge two tokens into one while expanding macros. This is called token pasting or token concatenation. The `##' preprocessing operator performs token pasting. When a macro is expanded, the two tokens on either side of each `##' operator are combined into a single token, which then replaces the `##' and the two original tokens in the macro expansion. Usually both will be identifiers, or one will be an identifier and the other a preprocessing number. When pasted, they make a longer identifier. This isn't the only valid case. It is also possible to concatenate two numbers (or a number and a name, such as 1.5 and e3) into a number. Also, multi-character operators such as += can be formed by token pasting.

However, two tokens that don't together form a valid token cannot be pasted together. For example, you cannot concatenate x with + in either order. If you try, the preprocessor issues a warning and emits the two tokens. Whether it puts white space between the tokens is undefined. It is common to find unnecessary uses of `##' in complex macros. If you get this warning, it is likely that you can simply remove the `##'.

Both the tokens combined by `##' could come from the macro body, but you could just as well write them as one token in the first place. Token pasting is most useful when one or both of the tokens comes from a macro argument. If either of the tokens next to an `##' is a parameter name, it is replaced by its actual argument before `##' executes. As with stringification, the actual argument is not macro-expanded first. If the argument is empty, that `##' has no effect.

Keep in mind that the C preprocessor converts comments to whitespace before macros are even considered. Therefore, you cannot create a comment by concatenating `/' and `*'. You can put as much whitespace between `##' and its operands as you like, including comments, and you can put comments in arguments that will be concatenated. However, it is an error if `##' appears at either end of a macro body.

Consider a C program that interprets named commands. There probably needs to be a table of commands, perhaps an array of structures declared as follows:
struct command
     {
       char *name;
       void (*function) (void);
     };
     
     struct command commands[] =
     {
       { "quit", quit_command },
       { "help", help_command },
       ...
     };
It would be cleaner not to have to give each command name twice, once in the string constant and once in the function name. A macro which takes the name of a command as an argument can make this unnecessary. The string constant can be created with stringification, and the function name by concatenating the argument with `_command'. Here is how it is done:
#define COMMAND(NAME)  { #NAME, NAME ## _command }
     
     struct command commands[] =
     {
       COMMAND (quit),
       COMMAND (help),
       ...
     };

# preprocessor:
Formal parameters are not replaced within quoted strings. If, however, a parameter name is preceded by a # in the replacement text, the combination will be expanded into a quoted string with the parameter replaced by the actual argument. This can be combined with sting concatenation to make, for example, a debugging print macro:
#define dprint(expr) printf(#expr " = %g\n", expr)
When this is invoked, as in
dprint(x/y);
the macro is expaned into
printf("x/y" " = %g\n", x/y);
and the strings are concantenated, so the effect is
printf("x/y = %g\n", x/y);

Within the actual argument, each " is replaced by \" and each \ by \\, so the result is a legal string constant.

Refer to:
1. http://gcc.gnu.org/onlinedocs/cpp/Concatenation.html#Concatenation
2. The C Programming Language (Book)

How to use oprofile on embedded system for Android

The detailed steps are referred from http://www.omappedia.org/wiki/Android_Debugging#OProfile
Here, I just write down the steps for my developing environment.
My developing environment is an Android platform on embedded system. So, I have a host machine and a target machine which is an embedded system with ARM based processor.

On host machine:
1. build a linux kernel enabling oprofile
2. edit $MYDROID/external/oprofile/opimport_pull (for instance $MYDROID = ~/sk886x/source/skydroid1.6/platform/) as below
Remove the python version number from the first line eg. change
      #!/usr/bin/python2.4 -E
  to
      #!/usr/bin/python -E

  Append the following lines at the end of the file to generate cpuloads.txt and callgraph.png for further analysis 
      os.system(oprofile_event_dir + "/bin/opreport --session-dir=. >> cpuloads.txt") 
      os.system(oprofile_event_dir + "/bin/opreport --session-dir=. -p $OUT/symbols -l -t 0.1 >> cpuloads.txt") 
      os.system(oprofile_event_dir + "/bin/opreport -cg --session-dir=. -p $OUT/symbols > callgraph.txt") 
      os.system("cat callgraph.txt | gprof2dot.py -s -w -f oprofile -n 0.1 -e 0.1 | dot -Tpng -o callgraph.png")  

On target machine

3. turn on busybox
# busybox ash
4. make a dummy linux vmlinux
# echo 0 > /vmlinux
5. If you see "Cannot create directory /dev/oprofile: File exists do_setup failed#", it means that, OProfile is not built in the Kernel.
# opcontrol --setup
6.
# busybox grep " _text" /proc/kallsyms 
c0023000 T _text          
# busybox grep " _etext" /proc/kallsyms 
c054e000 A _etext 
7.
# opcontrol --vmlinux=/vmlinux --kernel-range=0xc0023000,0xc054e000 --event=CPU_CYCLES:64
8.
# echo 16 > /dev/oprofile/backtrace_depth
9. check the configuration status
# opcontrol --status
10. start oprofile
opcontrol --start

11. Run your program for analyzing the performance

12. Stop oprofile
opcontrol --stop

On host machine
13.
$ cd $MYDROID 
$ source build/envsetup.sh 
$ setpaths 
$ export ADBHOST=
14.
$ mkdir ~/oprofilepackage && cd ~/oprofilepackage 
$ tar xvjf 
$ cd $MYDROID
$ source build/envsetup.sh 
$ sed -i -e 's_$(call inherit-product, frameworks/base/data/sounds/OriginalAudio.mk)_#$(call inherit-product, frameworks/base/data/sounds/OriginalAudio.mk)_g' build/target/product/AndroidProducts.mk
$ setpaths
$ export MYDROID=${PWD}
$ ln -s $MYDROID/out/target/product/sk886x $MYDROID/out/target/product/generic
15. Set soft links of vmlinux and target program, for example mplayer
$ ln -s /path/to/linux/kernel/soruce/folder/vmlinux $OUT/symbols/vmlinux 
$ ln -s /path/to/mplayer/source/folder/mplayer $OUT/sybmols/system/bin/mplayer
16. pull the oprofile result to host machine and generate the analysis report
#MYDROID/external/oprofile/opimport_pull /path/to/new/folder/to/store/dump/and/results/

Then you can see cpuloads.txt in /path/to/new/folder/to/store/dump/and/results/

2010年9月14日 星期二

Data type definitions of integer in C language

In stdint.h
typedef signed char int8_t;
typedef unsigned char   uint8_t;
typedef short  int16_t;
typedef unsigned short  uint16_t;
typedef int  int32_t;
typedef unsigned   uint32_t;
typedef long long  int64_t;
typedef unsigned long long   uint64_t;

如何在codeblocks使用libcurl

1. 首先先去下載libcurl
2. 將lib folder裡面的檔案跟include裡面的檔案複製到codeblocks所使用gcc編譯器的相對lib和include folder裡面.
3. 在所要開發的project的setting裡面的project's build options..的link settings中的Other linker options 加入 -lcurl  和  -lcurldll
4. 完成程式編譯後, 將 libcurl的bin folder 裡面的 *.dll 複製到程式執行檔的位置

完成