GLAST / LAT > DAQ and FSW > FSW > Doxygen Index> PBI / dev > doc_gather / linux-gcc


Interface   Data Structures   File List   Data Fields   Globals  

Ofs.h File Reference

Provides some additional OF's macros along the lines of the C language directives sizeof () and typeof (). More...

#include <stddef.h>

Defines

#define COUNT_OF(_array)   (sizeof (_array) / sizeof (*(_array)))
 Returns a count of the number of elements in _array.
#define OFFSET_OF(_structure, _member)   offsetof (_structure, _member)
 Returns the byte offset of _member within the named structure or typedef _structure.
#define SIZE_OF(_structure, _member)   (sizeof((((_structure *) 0) -> _member)))
 Returns the byte size of _member within the named structure or typedef _structure.


Detailed Description

Provides some additional OF's macros along the lines of the C language directives sizeof () and typeof ().

Author:
JJRussell - russell@slac.stanford.edu
   CVS $Id: Ofs.h,v 1.4 2011/08/04 18:47:16 russell Exp $

SYNOPIS
Provides some additional OF's macros. These are macros along the lines of the C language directives sizeof () and typeof (). Currently there are two such macros, COUNT_OF(_array), giving a count of the number of elements in an array and OFFSET_OF(_struct, _member), giving the byte offset of a structure member in the named structure.
USAGE
See the individual macros.
TARGET PLATFORMS
All FSW supported platforms.

Define Documentation

#define COUNT_OF ( _array   )     (sizeof (_array) / sizeof (*(_array)))

Returns a count of the number of elements in _array.

Parameters:
_array The name of the array to get the count of the number of elements.
Returns:
An integer giving the the count of the number of elements in array.
USAGE EXAMPLE
    int array[40];
    int count;
    .
    count = COUNT_OF(array);
Note:
Never one to pass up a chance to get a coding style statement in. Using COUNT_OF is almost always superior to any other method to get the number of elements in an array whose size is known at compile time. Consider a commonly used alternative.
   #define NBUF 512
   .
   .
   char buffer[NBUF];
   int  count;

   count = NBUF;
This is potentially dangerous if, for example, someone slips in
   #define NBUF 512
   .
   .
   char buffer[2*NBUF];
   int  count;
   .
   count = NBUF;
particularly if the line declaring the buffer and the line getting the count are physically separated in the code.

Bottom line is that COUNT_OF ALWAYS returns the correct number of the elements in an array whose size is known at compile time.

#define OFFSET_OF ( _structure,
_member   )     offsetof (_structure, _member)

Returns the byte offset of _member within the named structure or typedef _structure.

Parameters:
_structure The structure name or typedef of the structure
_member The name of the structure member to calculate the offset for.
Returns:
An integer giving the byte offset of _member within the named structure of typedef _structure.
USAGE EXAMPLES
    typedef struct _X { int a; int b; int c; } X;
    
    X *x;
    int offset_a = OFFSET_OF (X, a);
    int offset_b = OFFSET_OF (struct _X, b);
    int offset_c = OFFSET_OF (typeof (*x), c);
Note:
Again, regilion in the form of a style note. Do not overlook the usage of the typeof in the last example. If one wishes to know the offset within a structure pointed to by a variable, use the typeof operator. Otherwise, just as in the COUNT_OF example, one is guessing what the structure type is. If you guess wrong, you likely will be lucky enough that the misnamed structure does not have the named member within it. However, there is a good chance that it will, particularly if the misnamed structure is one of a family of similar sounding structures each, not coincidentally, having some identically named members.
There are circumstances where the other forms are viable, particularly when you wish to now the offset in a structure and have no instance of it hanging around. But, except in these particular cases, if you use the typeof operator whenever possible, you will always get the right answer.

Warning:
Since I wrote this is, I realized that typeof is currently a GCC feature only. It is floating around as a future standard, but currently it is not.

#define SIZE_OF ( _structure,
_member   )     (sizeof((((_structure *) 0) -> _member)))

Returns the byte size of _member within the named structure or typedef _structure.

Parameters:
_structure The structure name or typedef of the structure
_member The name of the structure member to calculate the sizeof.
Returns:
An integer giving the byte size of _member within the named structure of typedef _structure.
USAGE EXAMPLES
    typedef struct _X { int a; int b[4]; int c; } X;
    
    X *x;
    int size_a = SIZE_OF (X, a);
    int size_b = SIZE_OF (struct _X, b);
    int size_c = SIZE_OF (typeof (*x), c);


Generated on Thu Aug 4 14:20:46 2011 by  doxygen 1.5.8