Portability & Data Management

by Andrei Gorine



Listing One



#ifndef BASE_TYPES_DEFINED



typedef unsigned char   uint1;

typedef unsigned short  uint2;

typedef unsigned int    uint4;

typedef signed char     int1;

typedef short           int2;

typedef int             int4;



#endif



Listing Two



#define ALIGNEDPOS(pos, align) ( ((pos) + (align)-1) & ~((align)-1) )

pos = ALIGNEDPOS(pos, 4);





Listing Three



char c;

//(padding)

long  l;       - the address is aligned



Listing Four



#define handle_size      N

typedef uint1 hobject    [handle_size ];



Listing Five



typedef struct  appData_ { hobject h; } appData;

char c;

appData d;    /* d is not aligned */



Listing Six



typedef struct objhandle_t_

{

  ...

  obj_h         po;

  ...

  uint4         mo;

  uint2         code;

  ...

} objhandle_t;



Listing Seven



#define handle_size      N

#define handle_size_w

((( handle_size + (sizeof(void*) -1)) & ~(sizeof (void*) -1)) / sizeof(void*));



typedef void * hobject [handle_size_w ];



Listing Eight



#if defined( CFG_CHAR_CMP_SIGNED )

#define CMPCHARS(c1,c2) ((int)(signed char)(c1)-(int)(signed char)(c2) )

#elif defined( CFG_CHAR_CMP_UNSIGNED )

#define CMPCHARS(c1,c2) ((int)(unsigned char)(c1)-(int)(unsigned char)(c2) )

#else

#define CMPCHARS(c1,c2) ( (int)(char)(c1) - (int)(char)(c2) )

#endif



Listing Nine



struct S { int i; int j; };

S s = {3,4};



Listing Ten



struct S { int i; int j; };

S s;

s.i = 3; s.j = 4;



Listing Eleven



/* this is the TAS (test-and-set) latch template*/

void sys_yield()

{

  /* relinquish control to another thread */

}

void sys_delay(int msec)

{

  /* sleep */

}

int  sys_testandset( /*volatile*/ long * p_spinlock)

{

  /* The spinlock size is up to a long ;

   * This function performs the atomic swap (1, *p_spinlock) and returns

   * the previous spinlock value as an integer, which could be 1 or 0

  */

}



Listing Twelve



(a)

#ifndef SYS_WIN32_H__

#define SYS_WIN32_H__



/* sys.h definitions for WIN32 */



#define WIN32_LEAN_AND_MEAN

#include <windows.h>

#include <process.h>



#define sys_yield() SleepEx(0,1) /*yield()*/

#define sys_delay(msec) SleepEx(msec,1)

#define sys_testandset(ptr) InterlockedExchange(ptr,1)



#endif  /* SYS_WIN32_H__ */



(b)

#ifndef SYS_SOL_H__

#define SYS_SOL_H__



/* sys.h definitions for Solaris */



#include <sys/time.h>

#include <unistd.h>

#include <sched.h>



int  sys_testandset( /*volatile*/ long * p_spinlock)

{

  register char result = 1;

  volatile char *spinlock = ( volatile char * ) p_spinlock;

    __asm__ __volatile__(

        "   ldstub  [%2], %0    \n"

:       "=r"(result), "=m"(*spinlock)

:       "r"(spinlock));

    return (int) result;

}

void sys_yield()

{

  sched_yield();

}

void sys_delay(int msec)

{ /* */ }



(c)

#ifndef SYS_GHSI_H__

#define SYS_GHSI_H__



/* sys.h definitions for Green Hills Integrity OS */

#include <INTEGRITY.h>



void sys_yield()

{

  Yield();

}

void sys_delay(int msec)

{

}

int  sys_testandset(long * p_spinlock)

{

  return ! ( Success == TestAndSet(p_spinlock, 0, 1) );

}





Listing Thirteen



/* abstraction of write and read stream interfaces;

 * a stream handle is a pointer to the implementation-specific data

*/

typedef int (*stream_write)

  ( void *stream_handle, const void * from, unsigned nbytes);

typedef int (*stream_read)

  ( void *stream_handle, /*OUT*/ void * to, unsigned max_nbytes);

/* backup the database content to the output stream */

RETCODE db_backup

  ( void * stream_handle, stream_write output_stream_writer, void * app_data);

/* restore the database from input stream */

RETCODE  db_load

   ( void * stream_handle, stream_read input_stream_reader, void *app_data);





Listing Fourteen



int file_writer(void *stream_handle, const void * from, unsigned nbytes)

{

  FILE *f = (FILE*)stream_handle;

  int nbytes = fwrite(from,1,nbytes,f);

  return nbytes;

}

int file_reader(void *stream_handle, void * to, unsigned max_nbytes)

{

  FILE *f = (FILE*)stream_handle;

  int nbytes = fread(to,1,max_nbytes,f);

  return nbytes;

}



Listing Fifteen



#define channel_h void*

typedef int (*xstream_write)(channel_h ch, const void * from,

                             unsigned nbytes, void * app_data);

typedef int (*xstream_read) (channel_h ch, void * to,

                             unsigned max_nbytes, void* app_data);

typedef struct {

  xstream_write fsend;

  xstream_read  frecv;

  ...

 } channel_t, *channel_h;







