summaryrefslogtreecommitdiff
path: root/legacy/evas/src/bin/evas_cserve2_shm.c
diff options
context:
space:
mode:
authorIván Briano <sachieru@gmail.com>2012-05-03 21:01:31 +0000
committerIván Briano <sachieru@gmail.com>2012-05-03 21:01:31 +0000
commit15328efb85bcf413025d787d1d58d812a407f25a (patch)
tree37c09205c159d8b9758ffdada5dff2248e632613 /legacy/evas/src/bin/evas_cserve2_shm.c
parentb8ade6a7cfa99ca19c054bd6b0bf488e63accbf4 (diff)
evas/cserve2: fix typo that kept cserve2 disabled
now seriously... Introducing Cache Serve 2. This cache server will initially load images for clients connected to it. It starts slave processes to load these images, and share the loaded images through shm with the clients. All the connection done between clients and the server goes through sockets. The cserve2 build option is turned on by default, while the old cserve was disabled, but in order to make clients use it, the environment variable EVAS_CSERVE2 must be set, and a server must be running. Clients will try to find the socket on a specified location using the environment variable EVAS_CSERVE2_SOCKET. If it's not defined, then the XDG_RUNTIME_DIR path should be used, and finally HOME, TMPDIR and /tmp. SVN revision: 70699
Diffstat (limited to 'legacy/evas/src/bin/evas_cserve2_shm.c')
-rw-r--r--legacy/evas/src/bin/evas_cserve2_shm.c147
1 files changed, 147 insertions, 0 deletions
diff --git a/legacy/evas/src/bin/evas_cserve2_shm.c b/legacy/evas/src/bin/evas_cserve2_shm.c
new file mode 100644
index 0000000000..36291f947c
--- /dev/null
+++ b/legacy/evas/src/bin/evas_cserve2_shm.c
@@ -0,0 +1,147 @@
1#ifdef HAVE_CONFIG_H
2# include "config.h"
3#endif
4
5#include "evas_cserve2.h"
6
7#include <errno.h>
8#include <sys/mman.h>
9#include <sys/stat.h>
10#include <fcntl.h>
11#include <unistd.h>
12
13struct _Shm_Mapping
14{
15 const char *name;
16 size_t length;
17 Eina_Inlist *segments;
18};
19
20typedef struct _Shm_Mapping Shm_Mapping;
21
22struct _Shm_Handle
23{
24 EINA_INLIST;
25 Shm_Mapping *mapping;
26 off_t map_offset;
27 off_t image_offset;
28 size_t map_size;
29 size_t image_size;
30};
31
32static int id = 0;
33
34Shm_Handle *
35cserve2_shm_request(size_t size)
36{
37 Shm_Mapping *map;
38 Shm_Handle *shm;
39 char shmname[NAME_MAX];
40 size_t map_size;
41 long pagesize;
42 int fd;
43
44 map = calloc(1, sizeof(Shm_Mapping));
45 if (!map)
46 {
47 ERR("Failed to allocate mapping handler.");
48 return NULL;
49 }
50
51 do {
52 snprintf(shmname, sizeof(shmname), "/evas-shm-img-%d", id++);
53 fd = shm_open(shmname, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
54 if (fd == -1 && errno != EEXIST)
55 {
56 ERR("Failed to create shared memory object '%s': %m", shmname);
57 free(map);
58 return NULL;
59 }
60 } while (fd == -1);
61
62 pagesize = sysconf(_SC_PAGESIZE);
63 if (pagesize < 1)
64 {
65 ERR("sysconf() reported weird value for PAGESIZE, assuming 4096.");
66 pagesize = 4096;
67 }
68
69 map_size = ((size + pagesize - 1) / pagesize) * pagesize;
70
71 if (ftruncate(fd, map_size) == -1)
72 {
73 ERR("Failed to set size of shared file: %m");
74 close(fd);
75 free(map);
76 return NULL;
77 }
78 close(fd);
79
80 map->name = eina_stringshare_add(shmname);
81 map->length = map_size;
82
83 shm = calloc(1, sizeof(Shm_Handle));
84 if (!shm)
85 {
86 ERR("Failed to allocate shared memory handler.");
87 eina_stringshare_del(map->name);
88 free(map);
89 return NULL;
90 }
91
92 map->segments = eina_inlist_append(map->segments, EINA_INLIST_GET(shm));
93 shm->mapping = map;
94 shm->map_offset = 0;
95 shm->image_offset = 0;
96
97 shm->image_size = size;
98 shm->map_size = map_size;
99
100 return shm;
101}
102
103void
104cserve2_shm_unref(Shm_Handle *shm)
105{
106 Shm_Mapping *map = shm->mapping;
107
108 map->segments = eina_inlist_remove(map->segments, EINA_INLIST_GET(shm));
109 free(shm);
110
111 if (map->segments)
112 return;
113
114 shm_unlink(map->name);
115 eina_stringshare_del(map->name);
116 free(map);
117}
118
119const char *
120cserve2_shm_name_get(const Shm_Handle *shm)
121{
122 return shm->mapping->name;
123}
124
125off_t
126cserve2_shm_map_offset_get(const Shm_Handle *shm)
127{
128 return shm->map_offset;
129}
130
131off_t
132cserve2_shm_offset_get(const Shm_Handle *shm)
133{
134 return shm->image_offset;
135}
136
137size_t
138cserve2_shm_map_size_get(const Shm_Handle *shm)
139{
140 return shm->map_size;
141}
142
143size_t
144cserve2_shm_size_get(const Shm_Handle *shm)
145{
146 return shm->image_size;
147}