-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathaprocess.cpp
More file actions
163 lines (135 loc) · 3.58 KB
/
aprocess.cpp
File metadata and controls
163 lines (135 loc) · 3.58 KB
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
#include "aprocess.h"
using namespace alt;
#if defined(linux) || defined(__APPLE__)
#include <stdlib.h>
#include <fcntl.h> // Для O_* констант
#include <sys/mman.h> // Для shm_open, mmap
#include <sys/stat.h> // Для mode констант
#include <unistd.h> // Для ftruncate, close
long long alt::processId()
{
return getpid();
}
struct sharedInternal
{
int shm_fd;
bool host;
string name;
};
static void* openSharedMemory(const string& name, uint8*& buffer, uintz size)
{
bool host = false;
int shm_fd = shm_open(name(), O_CREAT | O_EXCL | O_RDWR, 0666);
if (shm_fd != -1)
{
host = true;
if (ftruncate(shm_fd, size) == -1)
{
close(shm_fd);
shm_unlink(name());
return nullptr;
}
}
else
{
shm_fd = shm_open(name(), O_RDWR, 0666);
if (shm_fd == -1)
{
return nullptr;
}
}
// Маппим память в адресное пространство
uint8* shared_array = (uint8*)mmap(NULL, size,
PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
if (shared_array == MAP_FAILED)
{
close(shm_fd);
if (host)
shm_unlink(name());
return nullptr;
}
sharedInternal *hand = new sharedInternal;
hand->shm_fd = shm_fd;
hand->host = host;
hand->name = name;
buffer = shared_array;
return hand;
}
static void closeSharedMemory(sharedInternal *hand, void *buffer, uintz size)
{
// Очистка ресурсов
munmap(buffer, size);
close(hand->shm_fd);
if(hand->host)
shm_unlink(hand->name());
}
void processSharedMemory::cleanup(const string& name)
{
shm_unlink(name());
}
#else
#include <windows.h>
#include <stdio.h>
long long alt::processId()
{
return GetCurrentProcessId();
}
struct sharedInternal
{
HANDLE hMapFile;
};
static void* openSharedMemory(const string& name, uint8*& buffer, uintz size)
{
HANDLE hMapFile = CreateFileMappingA(
INVALID_HANDLE_VALUE, // Используем память, а не файл
NULL, // Защита по умолчанию
PAGE_READWRITE, // Чтение/запись
size>>32, // Размер (старшая часть)
size,// Размер (младшая часть)
name() // Имя объекта
);
if (hMapFile == NULL) {
return nullptr;
}
uint8* pArray = (uint8*)MapViewOfFile(
hMapFile, // Дескриптор объекта
FILE_MAP_ALL_ACCESS, // Доступ
0, 0, // Смещение
size // Размер
);
if (pArray == NULL) {
CloseHandle(hMapFile);
return nullptr;
}
sharedInternal *hand = new sharedInternal;
hand->hMapFile = hMapFile;
buffer = pArray;
return hand;
}
static void closeSharedMemory(sharedInternal *hand, void *buffer, uintz size)
{
UnmapViewOfFile(buffer);
CloseHandle(hand->hMapFile);
}
void processSharedMemory::cleanup(const string& name)
{
return;
}
#endif
processSharedMemory::processSharedMemory(const string& name, uintz size)
{
internal = openSharedMemory(name,buffer,size);
if(!internal)
buffer = nullptr;
else
buffer_size = size;
}
processSharedMemory::~processSharedMemory()
{
if(internal)
{
sharedInternal *hand = (sharedInternal*)internal;
closeSharedMemory(hand,buffer, buffer_size);
delete hand;
}
}