-
Notifications
You must be signed in to change notification settings - Fork 2
/
gd.c
140 lines (111 loc) · 2.53 KB
/
gd.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
/*
* global data shared with all plugins
* qianfan Zhao <[email protected]>
*/
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include "imgeditor.h"
#include "gd_private.h"
static int fd_gd = -1;
static struct global_data *gd = NULL;
struct global_data *imgeditor_get_gd(void)
{
assert(gd != NULL);
return gd;
}
#define IMGEDITOR_GD_SIZE 16384
static const char *imgeditor_gd_id(char *s, size_t sz)
{
snprintf(s, sz, "imgeditor-%d.gd", getpid());
return s;
}
int imgeditor_core_setup_gd(void)
{
char gdid[128];
int fd, ret;
static_assert(sizeof(struct global_data) <= IMGEDITOR_GD_SIZE,
"global_data overrange");
imgeditor_gd_id(gdid, sizeof(gdid));
fd = shm_open(gdid, O_RDWR | O_CREAT, 0660);
if (fd < 0) {
fprintf(stderr, "Error: create shm(%s) failed(%m)\n",
gdid);
return fd;
}
ret = ftruncate(fd, IMGEDITOR_GD_SIZE);
if (ret < 0) {
fprintf(stderr, "Error: enlarge shm failed\n");
goto done;
}
gd = mmap(NULL, IMGEDITOR_GD_SIZE, PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0);
if (gd == MAP_FAILED) {
fprintf(stderr, "Error: mmap gd failed\n");
ret = -1;
goto done;
}
fd_gd = fd;
done:
if (ret < 0) {
close(fd);
shm_unlink(gdid);
}
return ret;
}
int imgeditor_plugin_setup_gd(void)
{
char gdid[128];
int fd;
imgeditor_gd_id(gdid, sizeof(gdid));
fd = shm_open(gdid, O_RDWR, 0660);
if (fd < 0) {
fprintf(stderr, "Error: plugin open shm %s failed(%m)\n", gdid);
return fd;
}
gd = mmap(NULL, IMGEDITOR_GD_SIZE, PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0);
if (gd == MAP_FAILED) {
fprintf(stderr, "Error: plugin mmap gd failed\n");
close(fd);
return -1;
}
fd_gd = fd;
return 0;
}
void imgeditor_free_gd(void)
{
char gdid[128];
if (gd) {
munmap(gd, IMGEDITOR_GD_SIZE);
close(fd_gd);
shm_unlink(imgeditor_gd_id(gdid, sizeof(gdid)));
gd = NULL;
fd_gd = -1;
}
}
int get_verbose_level(void)
{
return imgeditor_get_gd()->verbose_level;
}
void gd_export_imgeditor(struct imgeditor *imgeditor)
{
struct global_data *gd = imgeditor_get_gd();
if (gd->export_imgeditor_counts < GD_MAX_IMGEDITOR) {
gd->export_imgeditors[gd->export_imgeditor_counts] = imgeditor;
gd->export_imgeditor_counts++;
}
}
struct imgeditor *gd_get_imgeditor(const char *name)
{
struct global_data *gd = imgeditor_get_gd();
for (size_t i = 0; i < gd->export_imgeditor_counts; i++) {
struct imgeditor *imgeditor = gd->export_imgeditors[i];
if (!strcmp(imgeditor->name, name))
return imgeditor;
}
return NULL;
}