My Project
 All Classes Files Functions Variables Enumerations Enumerator Friends Macros Pages
drvrsmem.h
1 #ifndef drvrsmem_h
2 #define drvrsmem_h
3 /* S H A R E D M E M O R Y D R I V E R
4  =======================================
5 
6  by Jerzy.Borkowski@obs.unige.ch
7 
8 09-Mar-98 : initial version 1.0 released
9 23-Mar-98 : shared_malloc now accepts new handle as an argument
10 */
11 
12 
13 #include <sys/ipc.h> /* this is necessary for Solaris/Linux */
14 #include <sys/shm.h>
15 #include <sys/sem.h>
16 
17 #ifdef _AIX
18 #include <fcntl.h>
19 #else
20 #include <sys/fcntl.h>
21 #endif
22 
23  /* configuration parameters */
24 
25 #define SHARED_MAXSEG (16) /* maximum number of shared memory blocks */
26 
27 #define SHARED_KEYBASE (14011963) /* base for shared memory keys, may be overriden by getenv */
28 #define SHARED_FDNAME ("/tmp/.shmem-lockfile") /* template for lock file name */
29 
30 #define SHARED_ENV_KEYBASE ("SHMEM_LIB_KEYBASE") /* name of environment variable */
31 #define SHARED_ENV_MAXSEG ("SHMEM_LIB_MAXSEG") /* name of environment variable */
32 
33  /* useful constants */
34 
35 #define SHARED_RDONLY (0) /* flag for shared_(un)lock, lock for read */
36 #define SHARED_RDWRITE (1) /* flag for shared_(un)lock, lock for write */
37 #define SHARED_WAIT (0) /* flag for shared_lock, block if cannot lock immediate */
38 #define SHARED_NOWAIT (2) /* flag for shared_lock, fail if cannot lock immediate */
39 #define SHARED_NOLOCK (0x100) /* flag for shared_validate function */
40 
41 #define SHARED_RESIZE (4) /* flag for shared_malloc, object is resizeable */
42 #define SHARED_PERSIST (8) /* flag for shared_malloc, object is not deleted after last proc detaches */
43 
44 #define SHARED_INVALID (-1) /* invalid handle for semaphore/shared memory */
45 
46 #define SHARED_EMPTY (0) /* entries for shared_used table */
47 #define SHARED_USED (1)
48 
49 #define SHARED_GRANUL (16384) /* granularity of shared_malloc allocation = phys page size, system dependent */
50 
51 
52 
53  /* checkpoints in shared memory segments - might be omitted */
54 
55 #define SHARED_ID_0 ('J') /* first byte of identifier in BLKHEAD */
56 #define SHARED_ID_1 ('B') /* second byte of identifier in BLKHEAD */
57 
58 #define BLOCK_REG (0) /* value for tflag member of BLKHEAD */
59 #define BLOCK_SHARED (1) /* value for tflag member of BLKHEAD */
60 
61  /* generic error codes */
62 
63 #define SHARED_OK (0)
64 
65 #define SHARED_ERR_MIN_IDX SHARED_BADARG
66 #define SHARED_ERR_MAX_IDX SHARED_NORESIZE
67 
68 
69 #define DAL_SHM_FREE (0)
70 #define DAL_SHM_USED (1)
71 
72 #define DAL_SHM_ID0 ('D')
73 #define DAL_SHM_ID1 ('S')
74 #define DAL_SHM_ID2 ('M')
75 
76 #define DAL_SHM_SEGHEAD_ID (0x19630114)
77 
78 
79 
80  /* data types */
81 
82 /* BLKHEAD object is placed at the beginning of every memory segment (both
83  shared and regular) to allow automatic recognition of segments type */
84 
85 typedef union
86  { struct BLKHEADstruct
87  { char ID[2]; /* ID = 'JB', just as a checkpoint */
88  char tflag; /* is it shared memory or regular one ? */
89  int handle; /* this is not necessary, used only for non-resizeable objects via ptr */
90  } s;
91  double d; /* for proper alignment on every machine */
92  } BLKHEAD;
93 
94 typedef void *SHARED_P; /* generic type of shared memory pointer */
95 
96 typedef struct SHARED_GTABstruct /* data type used in global table */
97  { int sem; /* access semaphore (1 field): process count */
98  int semkey; /* key value used to generate semaphore handle */
99  int key; /* key value used to generate shared memory handle (realloc changes it) */
100  int handle; /* handle of shared memory segment */
101  int size; /* size of shared memory segment */
102  int nprocdebug; /* attached proc counter, helps remove zombie segments */
103  char attr; /* attributes of shared memory object */
104  } SHARED_GTAB;
105 
106 typedef struct SHARED_LTABstruct /* data type used in local table */
107  { BLKHEAD *p; /* pointer to segment (may be null) */
108  int tcnt; /* number of threads in this process attached to segment */
109  int lkcnt; /* >=0 <- number of read locks, -1 - write lock */
110  long seekpos; /* current pointer position, read/write/seek operations change it */
111  } SHARED_LTAB;
112 
113 
114  /* system dependent definitions */
115 
116 #ifndef HAVE_FLOCK_T
117 typedef struct flock flock_t;
118 #define HAVE_FLOCK_T
119 #endif
120 
121 #ifndef HAVE_UNION_SEMUN
122 union semun
123  { int val;
124  struct semid_ds *buf;
125  unsigned short *array;
126  };
127 #define HAVE_UNION_SEMUN
128 #endif
129 
130 
132 
134  { int ID; /* ID for debugging */
135  int h; /* handle of sh. mem */
136  int size; /* size of data area */
137  int nodeidx; /* offset of root object (node struct typically) */
138  };
139 
140  /* API routines */
141 
142 #ifdef __cplusplus
143 extern "C" {
144 #endif
145 
146 void shared_cleanup(void); /* must be called at exit/abort */
147 int shared_init(int debug_msgs); /* must be called before any other shared memory routine */
148 int shared_recover(int id); /* try to recover dormant segment(s) after applic crash */
149 int shared_malloc(long size, int mode, int newhandle); /* allocate n-bytes of shared memory */
150 int shared_attach(int idx); /* attach to segment given index to table */
151 int shared_free(int idx); /* release shared memory */
152 SHARED_P shared_lock(int idx, int mode); /* lock segment for reading */
153 SHARED_P shared_realloc(int idx, long newsize); /* reallocate n-bytes of shared memory (ON LOCKED SEGMENT ONLY) */
154 int shared_size(int idx); /* get size of attached shared memory segment (ON LOCKED SEGMENT ONLY) */
155 int shared_attr(int idx); /* get attributes of attached shared memory segment (ON LOCKED SEGMENT ONLY) */
156 int shared_set_attr(int idx, int newattr); /* set attributes of attached shared memory segment (ON LOCKED SEGMENT ONLY) */
157 int shared_unlock(int idx); /* unlock segment (ON LOCKED SEGMENT ONLY) */
158 int shared_set_debug(int debug_msgs); /* set/reset debug mode */
159 int shared_set_createmode(int mode); /* set/reset debug mode */
160 int shared_list(int id); /* list segment(s) */
161 int shared_uncond_delete(int id); /* uncondintionally delete (NOWAIT operation) segment(s) */
162 
163 int smem_init(void);
164 int smem_shutdown(void);
165 int smem_setoptions(int options);
166 int smem_getoptions(int *options);
167 int smem_getversion(int *version);
168 int smem_open(char *filename, int rwmode, int *driverhandle);
169 int smem_create(char *filename, int *driverhandle);
170 int smem_close(int driverhandle);
171 int smem_remove(char *filename);
172 int smem_size(int driverhandle, OFF_T *size);
173 int smem_flush(int driverhandle);
174 int smem_seek(int driverhandle, OFF_T offset);
175 int smem_read(int driverhandle, void *buffer, long nbytes);
176 int smem_write(int driverhandle, void *buffer, long nbytes);
177 
178 #ifdef __cplusplus
179 }
180 #endif
181 #endif
Definition: drvrsmem.h:133
Definition: drvrsmem.h:106
Definition: drvrsmem.h:86
Definition: drvrsmem.h:96
Definition: drvrsmem.h:85
Definition: tVideoCapabilitiesEml.cc:67
Definition: drvrsmem.h:122