-
Notifications
You must be signed in to change notification settings - Fork 0
/
sharedmemory.c
272 lines (247 loc) · 8.32 KB
/
sharedmemory.c
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
271
272
#include "sharedmemory.h"
#include "mapping.h"
#include "utils.h"
PSHAREDMEMORY CreateSharedMemory(STRING mapping_name, int size){
int symbol_size=33554432+4;//32Kb for saving symbol.(Max 1024 Variables)
int data_size=size;
int memory_size=symbol_size+data_size;
PSHAREDMEMORY mem=(PSHAREDMEMORY) malloc(sizeof(SHAREDMEMORY));
char* mapping_name0=qfs(2,"LIBIPC_",mapping_name);
PMAPPING_KEY mapping_key= CreateKey(mapping_name0);
free(mapping_name0);
if (mapping_key!=NULL){
UINT32 RET;
RET=CreateOrOpenMemoryMapping(mapping_key,memory_size);
if (RET){
return NULL;
}
mem->mapping_key=mapping_key;
RET=MappingMemory(mapping_key);
if (RET){
return NULL;
}
PARRAYLIST symbol_list=AllocArrayList(1024,mapping_key->MemoryPointer,32);
PBYTEBUFFER data_buffer=AllocByteBuffer(data_size,mapping_key->MemoryPointer+symbol_size);
mem->data_buffer=data_buffer;
mem->symbol_list=symbol_list;
mem->SetElement=SHARED_MEMORY_SetElement;
mem->SetInt8=SHARED_MEMORY_SetInt8;
mem->SetInt16=SHARED_MEMORY_SetInt16;
mem->SetInt32=SHARED_MEMORY_SetInt32;
mem->SetInt64=SHARED_MEMORY_SetInt64;
mem->SetUInt8=SHARED_MEMORY_SetUInt8;
mem->SetUInt16=SHARED_MEMORY_SetUInt16;
mem->SetUInt32=SHARED_MEMORY_SetUInt32;
mem->SetUInt64=SHARED_MEMORY_SetUInt64;
mem->SetFloat32=SHARED_MEMORY_SetFloat32;
mem->SetFloat64=SHARED_MEMORY_SetFloat64;
mem->SetBoolean=SHARED_MEMORY_SetUInt8;
mem->GetElement=SHARED_MEMORY_GetElement;
mem->GetInt8=SHARED_MEMORY_GetInt8;
mem->GetInt16=SHARED_MEMORY_GetInt16;
mem->GetInt32=SHARED_MEMORY_GetInt32;
mem->GetInt64=SHARED_MEMORY_GetInt64;
mem->GetUInt8=SHARED_MEMORY_GetUInt8;
mem->GetUInt16=SHARED_MEMORY_GetUInt16;
mem->GetUInt32=SHARED_MEMORY_GetUInt32;
mem->GetUInt64=SHARED_MEMORY_GetUInt64;
mem->GetFloat32=SHARED_MEMORY_GetFloat32;
mem->GetFloat64=SHARED_MEMORY_GetFloat64;
mem->GetBoolean=SHARED_MEMORY_GetUInt8;
return mem;
}
return NULL;
}
int SHARED_MEMORY_SetElement(PSHAREDMEMORY memory, STRING name, void* data, int size){
LOCK(memory->mapping_key->MUTEX);
if (strlen(name)>24){
LG_ERR("%s","Variable Name Too Long(Max Variable Length is 24)");
UNLOCK(memory->mapping_key->MUTEX);
return -1;
}
//create symbol
char symbol[32]={0};
strcpy(symbol,name);
memcpy(symbol+24,&memory->data_buffer->Position,4);
memcpy(symbol+28,&size,4);
//search if exists
PARRAYLIST lst=memory->symbol_list;
for (int i = 0; i < lst->Position; ++i) {
char* symbol0=lst->Get(lst,i);
UINT8 flag=BOOL_TRUE;
for (int j = 0; j < 32; ++j) {
if (symbol[j]!=symbol0[j]){
flag=BOOL_FALSE;
}
}
if (flag){
int sPos;
int sSize;
memcpy(&sPos,symbol0+24,4);
memcpy(&sSize,symbol0+28,4);
if (size!=sSize){
UNLOCK(memory->mapping_key->MUTEX);
return -1;
}
int ret=memory->data_buffer->Write(memory->data_buffer,data,sPos,size);
if (ret){
UNLOCK(memory->mapping_key->MUTEX);
return -1;
}
UNLOCK(memory->mapping_key->MUTEX);
return 0;
}
}
//copy to memory mapping
int ret=memory->data_buffer->Append(memory->data_buffer,data,size);
if (ret){
UNLOCK(memory->mapping_key->MUTEX);
return -1;
}
//register element
memory->symbol_list->Add(memory->symbol_list,symbol);
UNLOCK(memory->mapping_key->MUTEX);
return 0;
}
int SHARED_MEMORY_SetInt8(PSHAREDMEMORY memory, STRING name, INT8 data){
return SHARED_MEMORY_SetElement(memory, name, &data, 1);
}
int SHARED_MEMORY_SetInt16(PSHAREDMEMORY memory, STRING name, INT16 data){
return SHARED_MEMORY_SetElement(memory, name, &data, 2);
}
int SHARED_MEMORY_SetInt32(PSHAREDMEMORY memory, STRING name, INT32 data){
return SHARED_MEMORY_SetElement(memory, name, &data, 4);
}
int SHARED_MEMORY_SetInt64(PSHAREDMEMORY memory, STRING name, INT64 data){
return SHARED_MEMORY_SetElement(memory, name, &data, 8);
}
int SHARED_MEMORY_SetUInt8(PSHAREDMEMORY memory, STRING name, UINT8 data){
return SHARED_MEMORY_SetElement(memory, name, &data, 1);
}
int SHARED_MEMORY_SetUInt16(PSHAREDMEMORY memory, STRING name, UINT16 data){
return SHARED_MEMORY_SetElement(memory, name, &data, 2);
}
int SHARED_MEMORY_SetUInt32(PSHAREDMEMORY memory, STRING name, UINT32 data){
return SHARED_MEMORY_SetElement(memory, name, &data, 4);
}
int SHARED_MEMORY_SetUInt64(PSHAREDMEMORY memory, STRING name, UINT64 data){
return SHARED_MEMORY_SetElement(memory, name, &data, 8);
}
int SHARED_MEMORY_SetFloat32(PSHAREDMEMORY memory, STRING name, FLOAT32 data){
return SHARED_MEMORY_SetElement(memory, name, &data, 4);
}
int SHARED_MEMORY_SetFloat64(PSHAREDMEMORY memory, STRING name, FLOAT64 data){
return SHARED_MEMORY_SetElement(memory, name, &data, 8);
}
void* SHARED_MEMORY_GetElement(PSHAREDMEMORY memory, STRING name){
LOCK(memory->mapping_key->MUTEX);
if (strlen(name)>24){
LG_ERR("%s","Variable Name Too Long(Max Variable Length is 24)");
UNLOCK(memory->mapping_key->MUTEX);
return NULL;
}
char symbol[24]={0};
strcpy(symbol,name);
PARRAYLIST lst=memory->symbol_list;
for (int i = 0; i < lst->Position; ++i) {
//compare p0-p24
char* symbol0=(char*)lst->Get(lst,i);
INT8 flag=BOOL_TRUE;
for (int j = 0; j < 24; ++j) {
if (symbol[j]!=symbol0[j]){
flag=BOOL_FALSE;
}
}
if (flag==BOOL_TRUE){
int sPos;
int sSize;
memcpy(&sPos,symbol0+24,4);
memcpy(&sSize,symbol0+28,4);
PBYTEBUFFER buffer=memory->data_buffer;
void* data=buffer->Read(buffer,sPos,sSize);
UNLOCK(memory->mapping_key->MUTEX);
return data;
}
free(symbol0);
}
UNLOCK(memory->mapping_key->MUTEX);
return NULL;
}
INT8 SHARED_MEMORY_GetInt8(PSHAREDMEMORY memory, STRING name){
void* data= SHARED_MEMORY_GetElement(memory,name);
INT8 i;
memcpy(&i,data,1);
free(data);
return i;
}
INT16 SHARED_MEMORY_GetInt16(PSHAREDMEMORY memory, STRING name){
void* data= SHARED_MEMORY_GetElement(memory,name);
INT16 i;
memcpy(&i,data,2);
free(data);
return i;
}
INT32 SHARED_MEMORY_GetInt32(PSHAREDMEMORY memory, STRING name){
void* data= SHARED_MEMORY_GetElement(memory,name);
INT32 i;
memcpy(&i,data,4);
free(data);
return i;
}
INT64 SHARED_MEMORY_GetInt64(PSHAREDMEMORY memory, STRING name){
void* data= SHARED_MEMORY_GetElement(memory,name);
INT64 i;
memcpy(&i,data,8);
free(data);
return i;
}
UINT8 SHARED_MEMORY_GetUInt8(PSHAREDMEMORY memory, STRING name){
void* data= SHARED_MEMORY_GetElement(memory,name);
UINT8 i;
memcpy(&i,data,1);
free(data);
return i;
}
UINT16 SHARED_MEMORY_GetUInt16(PSHAREDMEMORY memory, STRING name){
void* data= SHARED_MEMORY_GetElement(memory,name);
UINT16 i;
memcpy(&i,data,2);
free(data);
return i;
}
UINT32 SHARED_MEMORY_GetUInt32(PSHAREDMEMORY memory, STRING name){
void* data= SHARED_MEMORY_GetElement(memory,name);
UINT32 i;
memcpy(&i,data,4);
free(data);
return i;
}
UINT64 SHARED_MEMORY_GetUInt64(PSHAREDMEMORY memory, STRING name){
void* data= SHARED_MEMORY_GetElement(memory,name);
UINT64 i;
memcpy(&i,data,8);
free(data);
return i;
}
FLOAT32 SHARED_MEMORY_GetFloat32(PSHAREDMEMORY memory, STRING name){
void* data= SHARED_MEMORY_GetElement(memory,name);
FLOAT32 i;
memcpy(&i,data,4);
free(data);
return i;
}
FLOAT64 SHARED_MEMORY_GetFloat64(PSHAREDMEMORY memory, STRING name){
void* data= SHARED_MEMORY_GetElement(memory,name);
FLOAT64 i;
memcpy(&i,data,8);
free(data);
return i;
}
void CloseSharedMemory(PSHAREDMEMORY memory){
if (memory!=NULL){
FreeArrayList(memory->symbol_list);
FreeByteBuffer(memory->data_buffer);
EndMappingMemory(memory->mapping_key);
CloseMemoryMapping(memory->mapping_key);
}
}