forked from abcdls0905/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathios_sharemem.h
226 lines (184 loc) · 3.02 KB
/
ios_sharemem.h
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
//--------------------------------------------------------------------
// 文件名: ios_sharemem.h
// 内 容:
// 说 明:
// 创建日期: 2010年12月15日
// 创建人: 陆利民
// 版权所有: 苏州蜗牛电子有限公司
//--------------------------------------------------------------------
#ifndef _SYSTEM_IOS_SHAREMEM_H
#define _SYSTEM_IOS_SHAREMEM_H
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
// 进程间的共享内存
class CShareMem
{
public:
// 保证文件存在
static bool ConfirmFile(const char* name)
{
int fd = open(name, O_CREAT | O_EXCL | O_WRONLY, 0666);
if (fd < 0)
{
if (errno != EEXIST)
{
return false;
}
}
else
{
close(fd);
}
return true;
}
// 是否存在指定名字的共享内存
static bool Exists(const char* name)
{
// 共享文件
if (!ConfirmFile(name))
{
return false;
}
key_t key = ftok(name, 0);
if (-1 == key)
{
return false;
}
//if (shmget(key, 1, IPC_CREAT | IPC_EXCL) == -1)
//{
// if (errno == EEXIST)
// {
// return true;
// }
//}
if (shmget(key, 0, 0) == -1)
{
return false;
}
return true;
}
public:
CShareMem()
{
m_pName = NULL;
m_nSize = 0;
m_pMem = NULL;
m_shm_id = -1;
}
~CShareMem()
{
Destroy();
}
// 获得名称
const char* GetName() const
{
if (NULL == m_pName)
{
return "";
}
return m_pName;
}
// 获得长度
size_t GetSize() const
{
return m_nSize;
}
// 获得内存地址
void* GetMem() const
{
return m_pMem;
}
// 是否有效
bool IsValid() const
{
return (m_shm_id != -1) && (m_pMem != NULL);
}
// 获得或创建共享内存
bool Create(const char* name, size_t size, bool* exists = NULL)
{
size_t name_size = strlen(name) + 1;
char* pName = NEW char[name_size];
memcpy(pName, name, name_size);
if (m_pName)
{
delete[] m_pName;
}
m_pName = pName;
m_nSize = size;
if (exists)
{
*exists = false;
}
// 共享文件
if (!ConfirmFile(name))
{
return false;
}
key_t key = ftok(m_pName, 0);
if (key == -1)
{
return false;
}
m_shm_id = shmget(key, size, IPC_CREAT | IPC_EXCL | 00666);
if (m_shm_id == -1)
{
if (errno != EEXIST)
{
return false;
}
if (exists)
{
*exists = true;
}
m_shm_id = shmget(key, size, IPC_CREAT | 00666);
if (m_shm_id == -1)
{
return false;
}
}
// 映射到进程空间
m_pMem = shmat(m_shm_id, NULL, 0);
if ((void*)-1 == m_pMem)
{
m_pMem = NULL;
return false;
}
return true;
}
// 删除共享内存
bool Destroy()
{
if (m_pMem)
{
shmdt(m_pMem);
m_pMem = NULL;
}
if (m_shm_id != -1)
{
shmctl(m_shm_id, IPC_RMID, NULL);
m_shm_id = -1;
}
if (m_pName)
{
delete[] m_pName;
m_pName = NULL;
}
return true;
}
private:
CShareMem(const CShareMem&);
CShareMem& operator=(const CShareMem&);
private:
char* m_pName;
size_t m_nSize;
void* m_pMem;
int m_shm_id;
};
#endif // _SYSTEM_IOS_SHAREMEM_H