CARMA C++
SharedMemory.h
Go to the documentation of this file.
1 
2 
13 #ifndef CARMA_BIMA_SHAREDMEMORY_H
14 #define CARMA_BIMA_SHAREDMEMORY_H
15 
16 // System includes
17 #include <unistd.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <sys/mman.h>
22 #include <stdlib.h>
23 
24 // C++ includes
25 #include <string>
26 #include <iostream>
27 #include <ostream>
28 #include <sstream>
29 #include <errno.h>
30 
31 // CARMA Tools include
32 #include "log4cpp/Category.hh"
33 
34 // CARMA Specific includes
35 #include "carma/util/Logger.h"
36 #include "carma/util/Program.h"
37 #include "carma/util/checking.h"
39 
41 
42 
43 #define BIMA_SHARED_MEMORY_DIR "/dev/shm/"
44 #define BIMA_SHARED_MEMORY_FILE "_shared_memory"
45 
46 #define CHAR_TYPE 'C'
47 #define USHT_TYPE 'S'
48 #define INT_TYPE 'I'
49 #define FLOAT_TYPE 'F'
50 #define DOUBLE_TYPE 'D'
51 
52 #define MAX_NAME_LEN 15
53 #define MAXHASH 768
54 #define MAXSTORE 4096
55 
56 #define SHMTRC( A, B ) putData( A, &B );
57 #define CSHMTRC( A, B, C ) A.putData( B, &C );
58 
59 namespace carma
60 {
61  namespace antenna
62  {
63  namespace bima
64  {
65 
66  class SharedMemory
67  {
68  public:
69 
70  SharedMemory();
71  SharedMemory( const char *name );
72 
73  void init( void );
74  void map( void );
75 
76  void getData( const char *name, char *value, int vsize = 1 );
77  void putData( const char *name, const char *value, int vsize = 1 );
78 
79  void putData( const char *name, char *value );
80 
81  void getData( const char *name, unsigned short *values, int vsize = 1 );
82  void putData( const char *name, unsigned short *values, int vsize = 1 );
83 
84  void getData( const char *name, int *values, int vsize = 1 );
85  void putData( const char *name, int *values, int vsize = 1 );
86 
87  void getData( const char *name, float *values, int vsize = 1 );
88  void putData( const char *name, float *values, int vsize = 1 );
89 
90  void getData( const char *name, double *values, int vsize = 1 );
91  void putData( const char *name, double *values, int vsize = 1 );
92 
93  void getVariableInfo( const char *name, char *type, int *size );
94 
95  bool dataNameExists( const char *name );
96 
97  void updateLinearInterpolator( const char * name,
98  const double x, const double y, const bool discontinuity );
99 
100  double evaluateLinearInterpolator( const char * name,
101  const double x );
102 
103  private:
104 
105  key_t nameToKey( const char *name )
106  {
107  key_t k = 0;
108 
109  if ( name != NULL )
110  {
111  int l = strlen( name );
112 
113  for ( int i = 0; i < l; i++ )
114  k = k ^ ( name[i] << ( 8 * ((i%4) + 1) ) );
115  }
116 
117  return k;
118  };
119 
120  struct hashentry
121  {
122  char name[MAX_NAME_LEN+1];
123  int index;
124  };
125 
126  struct chead
127  {
128  int length;
129  char type;
130  };
131 
132  union commentary
133  {
134  struct chead ahead;
135  int anint[2];
136  float afloat[2];
137  double adouble;
138  char achar[8];
139  };
140 
141  int *CNEXT;
142  struct hashentry *HASHB, *HASHP;
143  union commentary *COMMB;
144 
145  std::ostringstream _mapFileName;
146 
147  bool exists( const char *name, int &value );
148  void create( const char *name, char type, int n, int &index );
149  void fixname( const char *in, char *out );
150 
151 
152  void hashprobe( int &hashn, char *name, int &index );
153  void info( int &max, int &left, int &maxstore, int &next );
154 
155  protected:
156  log4cpp::Category &_logger;
157  };
158 
159  } // namespace bima
160  } // namespace antenna
161 } // namespace carma
162 
163 #endif // CARMA_BIMA_SHAREDMEMORY_H
Exception class for errors.
Header file for the CARMA checked build diagnostic macros.
Exception class for Shared Memory errors specific to BIMA Adapted from ErrorException.