summaryrefslogtreecommitdiff
path: root/src/bin/evas/evas_cserve2_shm.c
blob: 99b7f250661cc9e9f2e969668ea1983663e5a57b (plain) (blame)
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
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "evas_cserve2.h"

#include <errno.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

struct _Shm_Mapping
{
   const char *name;
   size_t length;
   Eina_Inlist *segments;
};

typedef struct _Shm_Mapping Shm_Mapping;

struct _Shm_Handle
{
   EINA_INLIST;
   Shm_Mapping *mapping;
   off_t map_offset;
   off_t image_offset;
   size_t map_size;
   size_t image_size;
   int refcount;
   void *data;
};

static int id = 0;

size_t
cserve2_shm_size_normalize(size_t size)
{
   long pagesize;
   size_t normalized;

   pagesize = sysconf(_SC_PAGESIZE);
   if (pagesize < 1)
     {
        ERR("sysconf() reported weird value for PAGESIZE, assuming 4096.");
        pagesize = 4096;
     }

   normalized = ((size + pagesize - 1) / pagesize) * pagesize;

   return normalized;
}

Shm_Handle *
cserve2_shm_request(size_t size)
{
   Shm_Mapping *map;
   Shm_Handle *shm;
   char shmname[NAME_MAX];
   size_t map_size;
   int fd;

   map = calloc(1, sizeof(Shm_Mapping));
   if (!map)
     {
        ERR("Failed to allocate mapping handler.");
        return NULL;
     }

   do {
        snprintf(shmname, sizeof(shmname), "/evas-shm-img-%x-%d", (int)getuid(), id++);
        fd = shm_open(shmname, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
        if (fd == -1 && errno != EEXIST)
          {
             ERR("Failed to create shared memory object '%s': %m", shmname);
             free(map);
             return NULL;
          }
   } while (fd == -1);

   map_size = cserve2_shm_size_normalize(size);

   if (ftruncate(fd, map_size) == -1)
     {
        ERR("Failed to set size of shared file: %m");
        close(fd);
        free(map);
        return NULL;
     }
   close(fd);

   map->name = eina_stringshare_add(shmname);
   map->length = map_size;

   shm = calloc(1, sizeof(Shm_Handle));
   if (!shm)
     {
        ERR("Failed to allocate shared memory handler.");
        eina_stringshare_del(map->name);
        free(map);
        return NULL;
     }

   map->segments = eina_inlist_append(map->segments, EINA_INLIST_GET(shm));
   shm->mapping = map;
   shm->map_offset = 0;
   shm->image_offset = 0;

   shm->image_size = size;
   shm->map_size = map_size;

   return shm;
}

void
cserve2_shm_unref(Shm_Handle *shm)
{
   Shm_Mapping *map = shm->mapping;

   map->segments = eina_inlist_remove(map->segments, EINA_INLIST_GET(shm));

   if (shm->data)
     munmap(shm->data, shm->image_size);
   free(shm);

   if (map->segments)
     return;

   shm_unlink(map->name);
   eina_stringshare_del(map->name);
   free(map);
}

const char *
cserve2_shm_name_get(const Shm_Handle *shm)
{
   return shm->mapping->name;
}

off_t
cserve2_shm_map_offset_get(const Shm_Handle *shm)
{
   return shm->map_offset;
}

off_t
cserve2_shm_offset_get(const Shm_Handle *shm)
{
   return shm->image_offset;
}

size_t
cserve2_shm_map_size_get(const Shm_Handle *shm)
{
   return shm->map_size;
}

size_t
cserve2_shm_size_get(const Shm_Handle *shm)
{
   return shm->image_size;
}

void *
cserve2_shm_map(Shm_Handle *shm)
{
   int fd;
   const char *name;

   if (shm->refcount++)
     return shm->data;

   name = cserve2_shm_name_get(shm);

   fd = shm_open(name, O_RDWR, S_IWUSR);
   if (fd == -1)
     return MAP_FAILED;

   shm->data = mmap(NULL, shm->image_size, PROT_WRITE, MAP_SHARED,
                    fd, shm->image_offset);

   close(fd);

   return shm->data;
}

void
cserve2_shm_unmap(Shm_Handle *shm)
{
   if (--shm->refcount)
     return;

   munmap(shm->data, shm->image_size);
   shm->data = NULL;
}