Showing posts with label C language. Show all posts
Showing posts with label C language. Show all posts

Thursday, August 4, 2011

gprof enabling

  • Add the ”-pg” option to every .o compilation + to the exe also.
  • Link with static versions of libraries that need being profiled.

Example given:

cd /vob/signalware/Master/scos
swmake gprof
cd /vob/signalware/Master/test/tcap/c7
rm /vob/signalware/Master/library/libscos.so
clearmake -V "PROT_FLAGS=-DC7_Q" gprof

Then running c7icptrans should produce a gmon.out.

To extract the information from gmon.out, use gprof:

gprof /export/home/omni_9S5/bin/test/c7icptrans gmon.out > gprof.txt

Tuesday, April 5, 2011

Print current date

#include sys/types.h
#include time.h
{
    char buf[40];time_t tm = time(0);struct tm* ptm = localtime(&tm);
    strftime(buf, 40, "%d-%b-%Y %T", ptm);
    fprintf(stderr,"==> jg: T6 expiration @ %s\n", buf);
 }

Test ith bit

(bits & 1<<i)>>i
gives 1 if set, and 0 otherwise.

Set ith bit

bits |= 1<<i

static inline vs. extern inline

  • “static inline” means “we have to have this function, if you use it but don’t inline it, then make a static version of it in this compilation unit”
  • “extern inline” means “I actually have an extern for this function, but if you want to inline it, here’s the inline-version”

Why do {...} while (0); in macros?

There are a couple of reasons:

  • (from Dave Miller) It gives you a basic block in which to declare local variables.
  • (from Ben Collins) It allows you to use more complex macros in conditional code. Imagine a macro of several lines of code like:
    #define FOO \
    printf(“arg is %s\n”, x); \
    do_something_useful(x);

Now imagine using it like:

if (blah 2) FOO(blah);
This interprets to:
if (blah  2)
    printf("arg is %s\n", blah);
    do_something_useful(blah);

As you can see, the if then only encompasses the printf(), and the do_something_useful() call is unconditional (not within the scope of the if), like you wanted it. So, by using a block like do{...}while(0), you would get this:

if (blah == 2)
    do {
        printf("arg is %s\n", blah);
        do_something_useful(blah);
    } while (0);

which is exactly what you want.

  • (from Per Persson) As both Miller and Collins point out, you want a block statement so you can have several lines of code and declare local variables. But then the natural thing would be to just use for example:
    #define exch(x,y) { int tmp; tmp=x; x=y; y=tmp; }

However that wouldn’t work in some cases. The following code is meant to be an if-statement with two branches:

if(x>y)
  exch(x,y);          // Branch 1
else  
  do_something();     // Branch 2

But it would be interpreted as an if-statement with only one branch:

if(x>y) {                     // Single-branch if-statement!!!
  int tmp;            // The one and only branch consists
  tmp = x;            // of the block.
  x = y;
  y = tmp;
}
;                             // empty statement
else                  // ERROR!!! "parse error before else" 
  do_something();

The problem is the semi-colon (;) coming directly after the block.

The solution for this is to sandwich the block between do and while(0). Then we have a single statement with the capabilities of a block, but not considered as being a block statement by the compiler.

Our if-statement now becomes:

if(x>y)
  do {
    int tmp;
    tmp = x;
    x = y;
    y = tmp;
  } while(0);
else
  do_something();

The # and ## Preprocessor Operators

The # and ## preprocessor operators are used when using a macro #define.

The # operator turns the argument it precedes into a quoted string. For example, given:

#define mkstr(s) # s

the preprocessor turns the line

printf(mkstr(I like C);

into

printf("I like C");

The ## operator concatenates two tokens. For example, given:

#define concat(a, b) a ## b
int xy=10;
printf("%d",concat(x, y);

the preprocessor turns the last line into:

printf("%d", xy);

struct pointer + 1

Given

toto_t* p_toto = ...;

then

p_tata = (tata_t*)(p_toto + 1)

means that p_tata points sizeof(toto_t) further than p_toto.

p_tata = (tata_t*) p_toto + 1

means that p_tata points sizeof(tata_t) further than p_toto.

Block / unblock signals

static sigset_t osset;
void block_signals()
{
    sigset_t sset;
    sigemptyset(&sset);
    sigaddset(&sset, SIGABRT);
/*     sigprocmask(SIG_BLOCK, &sset, &osset); */
    pthread_sigmask(SIG_BLOCK, &sset, &osset);
    signal(SIGABRT, SIG_DFL);
}

void unblock_signals()
{
    sigset_t sset;
    sigpending(&sset);
    if (sigismember(&sset, SIGABRT))
    {
        fprintf(stderr, "\njg ==> was interrupted\n");
        exit(1);
    }
/*     sigprocmask(SIG_SETMASK, &osset, NULL); */
    pthread_sigmask(SIG_SETMASK, &osset, NULL);
}