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
|
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "evas_cs.h"
#ifdef EVAS_CSERVE
EAPI Mem *
evas_cserve_mem_new(int size, const char *name)
{
Mem *m;
static int id = 0;
char buf[PATH_MAX];
m = calloc(1, sizeof(Mem));
if (!m) return NULL;
if (name)
snprintf(buf, sizeof(buf), "/evas-shm-%x.%s", getuid(), name);
else
{
id++;
snprintf(buf, sizeof(buf), "/evas-shm-%x.%x.%x", getuid(), getpid(), id);
}
m->id = id;
m->offset = 0;
m->name = strdup(buf);
if (!m->name)
{
free(m);
return NULL;
}
m->size = size;
m->fd = shm_open(m->name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
if (m->fd < 0)
{
free(m->name);
free(m);
return NULL;
}
if (ftruncate(m->fd, m->size) < 0)
{
shm_unlink(m->name);
close(m->fd);
free(m->name);
free(m);
return NULL;
}
eina_mmap_safety_enabled_set(EINA_TRUE);
m->data = mmap(NULL, m->size, PROT_READ | PROT_WRITE, MAP_SHARED, m->fd, 0);
if (m->data == MAP_FAILED)
{
shm_unlink(m->name);
close(m->fd);
free(m->name);
free(m);
return NULL;
}
m->ref = 1;
m->write = 1;
return m;
}
EAPI void
evas_cserve_mem_free(Mem *m)
{
shm_unlink(m->name);
munmap(m->data, m->size);
close(m->fd);
free(m->name);
free(m);
}
EAPI Mem *
evas_cserve_mem_open(int pid, int id, const char *name, int size, int do_write)
{
Mem *m;
char buf[PATH_MAX];
m = calloc(1, sizeof(Mem));
if (!m) return NULL;
if (name)
snprintf(buf, sizeof(buf), "/evas-shm-%x.%s", getuid(), name);
else
snprintf(buf, sizeof(buf), "/evas-shm-%x.%x.%x", getuid(), pid, id);
m->name = strdup(buf);
if (!m->name)
{
free(m);
return NULL;
}
m->size = size;
if (do_write)
m->fd = shm_open(m->name, O_RDWR, S_IRUSR | S_IWUSR);
else
m->fd = shm_open(m->name, O_RDONLY, S_IRUSR);
if (m->fd < 0)
{
free(m->name);
free(m);
return NULL;
}
m->write = do_write;
eina_mmap_safety_enabled_set(EINA_TRUE);
if (do_write)
m->data = mmap(NULL, m->size, PROT_READ | PROT_WRITE, MAP_SHARED, m->fd, 0);
else
m->data = mmap(NULL, m->size, PROT_READ, MAP_SHARED, m->fd, 0);
if (m->data == MAP_FAILED)
{
close(m->fd);
free(m->name);
free(m);
return NULL;
}
m->ref = 1;
return m;
}
EAPI void
evas_cserve_mem_close(Mem *m)
{
munmap(m->data, m->size);
close(m->fd);
free(m->name);
free(m);
}
EAPI Eina_Bool
evas_cserve_mem_resize(Mem *m, int size)
{
if (m->size == size) return 1;
if (m->write)
{
if (ftruncate(m->fd, size) < 0) return 0;
munmap(m->data, m->size);
eina_mmap_safety_enabled_set(EINA_TRUE);
m->data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, m->fd, 0);
}
else
{
munmap(m->data, m->size);
eina_mmap_safety_enabled_set(EINA_TRUE);
m->data = mmap(NULL, size, PROT_READ, MAP_SHARED, m->fd, 0);
}
if (m->data == MAP_FAILED)
{
m->data = NULL;
return 0;
}
m->size = size;
return 1;
}
EAPI void
evas_cserve_mem_del(int pid, int id)
{
char buf[PATH_MAX];
snprintf(buf, sizeof(buf), "/evas-shm-%x.%x.%x", getuid(), pid, id);
shm_unlink(buf);
}
#endif
|