1
/* S H A R E D M E M O R Y D R I V E R
2
=======================================
4
by Jerzy.Borkowski@obs.unige.ch
6
09-Mar-98 : initial version 1.0 released
7
23-Mar-98 : shared_malloc now accepts new handle as an argument
11
#include <sys/ipc.h> /* this is necessary for Solaris/Linux */
18
#include <sys/fcntl.h>
21
/* configuration parameters */
23
#define SHARED_MAXSEG (16) /* maximum number of shared memory blocks */
25
#define SHARED_KEYBASE (14011963) /* base for shared memory keys, may be overriden by getenv */
26
#define SHARED_FDNAME ("/tmp/.shmem-lockfile") /* template for lock file name */
28
#define SHARED_ENV_KEYBASE ("SHMEM_LIB_KEYBASE") /* name of environment variable */
29
#define SHARED_ENV_MAXSEG ("SHMEM_LIB_MAXSEG") /* name of environment variable */
31
/* useful constants */
33
#define SHARED_RDONLY (0) /* flag for shared_(un)lock, lock for read */
34
#define SHARED_RDWRITE (1) /* flag for shared_(un)lock, lock for write */
35
#define SHARED_WAIT (0) /* flag for shared_lock, block if cannot lock immediate */
36
#define SHARED_NOWAIT (2) /* flag for shared_lock, fail if cannot lock immediate */
37
#define SHARED_NOLOCK (0x100) /* flag for shared_validate function */
39
#define SHARED_RESIZE (4) /* flag for shared_malloc, object is resizeable */
40
#define SHARED_PERSIST (8) /* flag for shared_malloc, object is not deleted after last proc detaches */
42
#define SHARED_INVALID (-1) /* invalid handle for semaphore/shared memory */
44
#define SHARED_EMPTY (0) /* entries for shared_used table */
45
#define SHARED_USED (1)
47
#define SHARED_GRANUL (16384) /* granularity of shared_malloc allocation = phys page size, system dependent */
51
/* checkpoints in shared memory segments - might be omitted */
53
#define SHARED_ID_0 ('J') /* first byte of identifier in BLKHEAD */
54
#define SHARED_ID_1 ('B') /* second byte of identifier in BLKHEAD */
56
#define BLOCK_REG (0) /* value for tflag member of BLKHEAD */
57
#define BLOCK_SHARED (1) /* value for tflag member of BLKHEAD */
59
/* generic error codes */
63
#define SHARED_ERR_MIN_IDX SHARED_BADARG
64
#define SHARED_ERR_MAX_IDX SHARED_NORESIZE
67
#define DAL_SHM_FREE (0)
68
#define DAL_SHM_USED (1)
70
#define DAL_SHM_ID0 ('D')
71
#define DAL_SHM_ID1 ('S')
72
#define DAL_SHM_ID2 ('M')
74
#define DAL_SHM_SEGHEAD_ID (0x19630114)
80
/* BLKHEAD object is placed at the beginning of every memory segment (both
81
shared and regular) to allow automatic recognition of segments type */
84
{ struct BLKHEADstruct
85
{ char ID[2]; /* ID = 'JB', just as a checkpoint */
86
char tflag; /* is it shared memory or regular one ? */
87
int handle; /* this is not necessary, used only for non-resizeable objects via ptr */
89
double d; /* for proper alignment on every machine */
92
typedef void *SHARED_P; /* generic type of shared memory pointer */
94
typedef struct SHARED_GTABstruct /* data type used in global table */
95
{ int sem; /* access semaphore (1 field): process count */
96
int semkey; /* key value used to generate semaphore handle */
97
int key; /* key value used to generate shared memory handle (realloc changes it) */
98
int handle; /* handle of shared memory segment */
99
int size; /* size of shared memory segment */
100
int nprocdebug; /* attached proc counter, helps remove zombie segments */
101
char attr; /* attributes of shared memory object */
104
typedef struct SHARED_LTABstruct /* data type used in local table */
105
{ BLKHEAD *p; /* pointer to segment (may be null) */
106
int tcnt; /* number of threads in this process attached to segment */
107
int lkcnt; /* >=0 <- number of read locks, -1 - write lock */
108
long seekpos; /* current pointer position, read/write/seek operations change it */
112
/* system dependent definitions */
115
typedef struct flock flock_t;
119
#ifndef HAVE_UNION_SEMUN
122
struct semid_ds *buf;
123
unsigned short *array;
125
#define HAVE_UNION_SEMUN
129
typedef struct DAL_SHM_SEGHEAD_STRUCT DAL_SHM_SEGHEAD;
131
struct DAL_SHM_SEGHEAD_STRUCT
132
{ int ID; /* ID for debugging */
133
int h; /* handle of sh. mem */
134
int size; /* size of data area */
135
int nodeidx; /* offset of root object (node struct typically) */
144
void shared_cleanup(void); /* must be called at exit/abort */
145
int shared_init(int debug_msgs); /* must be called before any other shared memory routine */
146
int shared_recover(int id); /* try to recover dormant segment(s) after applic crash */
147
int shared_malloc(long size, int mode, int newhandle); /* allocate n-bytes of shared memory */
148
int shared_attach(int idx); /* attach to segment given index to table */
149
int shared_free(int idx); /* release shared memory */
150
SHARED_P shared_lock(int idx, int mode); /* lock segment for reading */
151
SHARED_P shared_realloc(int idx, long newsize); /* reallocate n-bytes of shared memory (ON LOCKED SEGMENT ONLY) */
152
int shared_size(int idx); /* get size of attached shared memory segment (ON LOCKED SEGMENT ONLY) */
153
int shared_attr(int idx); /* get attributes of attached shared memory segment (ON LOCKED SEGMENT ONLY) */
154
int shared_set_attr(int idx, int newattr); /* set attributes of attached shared memory segment (ON LOCKED SEGMENT ONLY) */
155
int shared_unlock(int idx); /* unlock segment (ON LOCKED SEGMENT ONLY) */
156
int shared_set_debug(int debug_msgs); /* set/reset debug mode */
157
int shared_set_createmode(int mode); /* set/reset debug mode */
158
int shared_list(int id); /* list segment(s) */
159
int shared_uncond_delete(int id); /* uncondintionally delete (NOWAIT operation) segment(s) */
162
int smem_shutdown(void);
163
int smem_setoptions(int options);
164
int smem_getoptions(int *options);
165
int smem_getversion(int *version);
166
int smem_open(char *filename, int rwmode, int *driverhandle);
167
int smem_create(char *filename, int *driverhandle);
168
int smem_close(int driverhandle);
169
int smem_remove(char *filename);
170
int smem_size(int driverhandle, OFF_T *size);
171
int smem_flush(int driverhandle);
172
int smem_seek(int driverhandle, OFF_T offset);
173
int smem_read(int driverhandle, void *buffer, long nbytes);
174
int smem_write(int driverhandle, void *buffer, long nbytes);