-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shrmem.cpp
270 lines (247 loc) · 6.58 KB
/
Shrmem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
* @file Shrmem.cpp
*/
#include "Shrmem.h"
/***************************************************************************//**
* @author Hayden Waisanen
*
* @par Description:
* Create (or attach) shared memory of specified dimensions
*
* @par Class:
* Shrmem
*
* @param[in] int block_count_i - input desired block_count
* @param[in] int block_size_i - inputted desired block_size
*
* @returns n/a
*
******************************************************************************/
Shrmem::Shrmem(int block_count_i , int block_size_i)
{
keypath = "/dev/null";
keyid = 1;
shmkey = getShmkey();
//Set block info
info.block_size = block_size_i;
info.block_count = block_count_i;
//Add a block at the beginning to store block information
memsize = info.block_size*info.block_count+sizeof(block_info)+info.block_count*sizeof(int);
if(getShmadd() == -1)
{
exit(-1);
}
}
/***************************************************************************//**
* @author Hayden Waisanen
*
* @par Description:
* Simple getter for block_size
*
* @par Class:
* Shrmem
*
* @returns int - block_size
*
******************************************************************************/
int Shrmem::get_block_size()
{
return info.block_size;
}
/***************************************************************************//**
* @author Hayden Waisanen
*
* @par Description:
* Simple getter for block_count
*
* @par Class:
* Shrmem
*
* @returns int - block_count
*
******************************************************************************/
int Shrmem::get_block_count()
{
return info.block_count;
}
/***************************************************************************//**
* @author Hayden Waisanen
*
* @par Description:
* Simple getter for shm_address (skipping past metadata)
*
* @par Class:
* Shrmem
*
* @returns void * - address
*
******************************************************************************/
void* Shrmem::getAddress()
{
return (void*)(((char*)shm_address)+sizeof(block_info)+info.block_count*sizeof(int));
}
/***************************************************************************//**
* @author Hayden Waisanen
*
* @par Description:
* Simple getter for reader count array (skipping past metadata)
*
* @par Class:
* Shrmem
*
* @returns int * - address
*
******************************************************************************/
int * Shrmem::reader_array()
{
return (int *)(((char*)shm_address)+sizeof(block_info));
}
/***************************************************************************//**
* @author Hayden Waisanen
*
* @par Description:
* Detach memory and flag for removal
*
* @par Class:
* Shrmem
*
* @returns n/a
*
******************************************************************************/
Shrmem::~Shrmem()
{
int rc = shmdt(shm_address); //Detach memory
shmid_ds temp;
shmid_ds * buf = &temp;
shmctl(shmid, IPC_RMID, buf); //Flag for removal
}
/***************************************************************************//**
* @author Hayden Waisanen
*
* @par Description:
* Get a shmkey using ftok
*
* @par Class:
* Shrmem
*
* @returns key_t - shared memory key
*
******************************************************************************/
key_t Shrmem::getShmkey()
{
key_t semkey = ftok(keypath.c_str(), keyid);
if(semkey == (key_t) -1)
//cout << "Ftok failed" << endl;
return semkey;
}
/***************************************************************************//**
* @author Hayden Waisanen
*
* @par Description:
* Add shared memory to address space
*
* @par Class:
* Shrmem
*
* @returns int - shmid
*
******************************************************************************/
int Shrmem::getShmadd()
{
//Attempt to create share memory
shmid = shmget(shmkey, memsize, 0666 | IPC_CREAT | IPC_EXCL);
if (shmid == -1)
{
//Memory may have already been created, attempt to attach
//Attach to memory and get block size information
shmid = shmget(shmkey, sizeof(block_info), 0666);
if(shmid == -1)
{
return shmid;
}
shm_address = shmat(shmid, NULL, 0);
info = (*((block_info *)shm_address));
//Recalculate memsize
memsize = info.block_size*info.block_count+sizeof(block_info)+info.block_count*sizeof(int);
//Deleting current attachment
shmdt(shm_address);
//Reattach correct sized memory block
shmid = shmget(shmkey, memsize, 0666);
}
//Attach to memory and set blocks metadata
shm_address = shmat(shmid, NULL, 0);
(*(block_info *)shm_address) = info;
//Check for success
if ( shm_address==NULL )
{
printf("main: shmat() failed\n");
return -1;
}
}
/***************************************************************************//**
* @author Hayden Waisanen
*
* @par Description:
* Write string to specifed block
*
* @par Class:
* Shrmem
*
* @param[in] string message - message to write
* @param[in] int block - block to write to
*
* @returns n/a
*
******************************************************************************/
void Shrmem::write(string message, int block)
{
//Get pointer to start of block
void * ptr = (void *)((char *)getAddress() + block*info.block_size);
//Write message (clipping at info.block_size
strncpy((char *)ptr, message.c_str(), info.block_size);
//Ensure the message is null terminated
((char *)ptr)[info.block_size-1]= '\0';
}
/***************************************************************************//**
* @author Hayden Waisanen
*
* @par Description:
* Read string from specifed block
*
* @par Class:
* Shrmem
*
* @param[in] int block - block to read to
*
* @returns string - message in block
*
******************************************************************************/
string Shrmem::read(int block)
{
//Get pointer to start of block
void * ptr = (void *)((char *)getAddress() + block*info.block_size);
//Copy message to temporary string
string temp = (char *)ptr;
return temp;
}
/***************************************************************************//**
* @author Hayden Waisanen
*
* @par Description:
* Allows for indexing into Shrmem instances
*
* @par Class:
* Shrmem
*
* @param[in] int index - block index
*
* @returns Shrmem::Block
*
******************************************************************************/
Shrmem::Block Shrmem::operator[](int index)
{
Shrmem::Block b;
b.block_i = index;
b.parent = this;
return b;
}