4 * Created on: Aug 13, 2009
11 #ifdef BOWTIE_SHARED_MEM
22 extern void notifySharedMem(void *mem, size_t len);
24 extern void waitSharedMem(void *mem, size_t len);
26 #define ALLOC_SHARED_U8 allocSharedMem<uint8_t>
27 #define ALLOC_SHARED_U32 allocSharedMem<uint32_t>
28 #define FREE_SHARED shmdt
29 #define NOTIFY_SHARED notifySharedMem
30 #define WAIT_SHARED waitSharedMem
32 #define SHMEM_UNINIT 0xafba4242
33 #define SHMEM_INIT 0xffaa6161
36 * Tries to allocate a shared-memory chunk for a given file of a given size.
39 bool allocSharedMem(std::string fname,
47 // Calculate key given string
48 key_t key = (key_t)hash_string(fname);
51 // Reserve 4 bytes at the end for silly synchronization
52 size_t shmemLen = len + 4;
54 cerr << "Reading " << len << "+4 bytes into shared memory for " << memName << endl;
58 // Create the shrared-memory block
59 if((shmid = shmget(key, shmemLen, IPC_CREAT | 0666)) < 0) {
61 cerr << "Out of memory allocating shared area " << memName << endl;
62 } else if(errno == EACCES) {
63 cerr << "EACCES" << endl;
64 } else if(errno == EEXIST) {
65 cerr << "EEXIST" << endl;
66 } else if(errno == EINVAL) {
67 cerr << "Warning: shared-memory chunk's segment size doesn't match expected size (" << (shmemLen) << ")" << endl
68 << "Deleteing old shared memory block and trying again." << endl;
69 shmid = shmget(key, 0, 0);
70 if((ret = shmctl(shmid, IPC_RMID, &ds)) < 0) {
71 cerr << "shmctl returned " << ret
72 << " for IPC_RMID, errno is " << errno
73 << ", shmid is " << shmid << endl;
76 cerr << "Deleted shared mem chunk with shmid " << shmid << endl;
79 } else if(errno == ENOENT) {
80 cerr << "ENOENT" << endl;
81 } else if(errno == ENOSPC) {
82 cerr << "ENOSPC" << endl;
84 cerr << "shmget returned " << shmid << " for and errno is " << errno << endl;
88 ptr = (T*)shmat(shmid, 0, 0);
89 if(ptr == (void*)-1) {
90 cerr << "Failed to attach " << memName << " to shared memory with shmat()." << endl;
94 cerr << memName << " pointer returned by shmat() was NULL." << endl;
97 // Did I create it, or did I just attach to one created by
99 if((ret = shmctl(shmid, IPC_STAT, &ds)) < 0) {
100 cerr << "shmctl returned " << ret << " for IPC_STAT and errno is " << errno << endl;
103 if(ds.shm_segsz != shmemLen) {
104 cerr << "Warning: shared-memory chunk's segment size (" << ds.shm_segsz
105 << ") doesn't match expected size (" << shmemLen << ")" << endl
106 << "Deleteing old shared memory block and trying again." << endl;
107 if((ret = shmctl(shmid, IPC_RMID, &ds)) < 0) {
108 cerr << "shmctl returned " << ret << " for IPC_RMID and errno is " << errno << endl;
116 bool initid = (((volatile uint32_t*)((char*)ptr + len))[0] == SHMEM_INIT);
117 if(ds.shm_cpid == getpid() && !initid) {
119 cerr << " I (pid = " << getpid() << ") created the "
120 << "shared memory for " << memName << endl;
122 // Set this value just off the end of the chunk to
123 // indicate that the data hasn't been read yet.
124 ((volatile uint32_t*)((char*)ptr + len))[0] = SHMEM_UNINIT;
128 cerr << " I (pid = " << getpid()
129 << ") did not create the shared memory for "
130 << memName << ". Pid " << ds.shm_cpid << " did." << endl;
138 #define ALLOC_SHARED_U8(...) 0
139 #define ALLOC_SHARED_U32(...) 0
140 #define FREE_SHARED(...)
141 #define NOTIFY_SHARED(...)
142 #define WAIT_SHARED(...)
144 #endif /*BOWTIE_SHARED_MEM*/
146 #endif /* SHMEM_H_ */