summaryrefslogtreecommitdiff
path: root/src/generic/evas/common/shmfile.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/generic/evas/common/shmfile.c')
-rw-r--r--src/generic/evas/common/shmfile.c87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/generic/evas/common/shmfile.c b/src/generic/evas/common/shmfile.c
new file mode 100644
index 0000000..538a314
--- /dev/null
+++ b/src/generic/evas/common/shmfile.c
@@ -0,0 +1,87 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <sys/types.h>
8#include <unistd.h>
9#include <math.h>
10#include <netinet/in.h>
11#include <time.h>
12#include <sys/mman.h>
13#include <sys/stat.h>
14#include <fcntl.h>
15#include <string.h>
16#include <zlib.h>
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22int shm_fd = -1;
23static int shm_size = 0;
24void *shm_addr = NULL;
25char *shmfile = NULL;
26
27void
28shm_alloc(unsigned long dsize)
29{
30#ifdef HAVE_SHM_OPEN
31 if (!shmfile) shmfile = malloc(1024);
32 if (!shmfile) goto failed;
33 shmfile[0] = 0;
34 srand(time(NULL));
35 do
36 {
37 snprintf(shmfile, 1024, "/evas-loader.%i.%i",
38 (int)getpid(), (int)rand());
39 shm_fd = shm_open(shmfile, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
40 }
41 while (shm_fd < 0);
42
43 if (ftruncate(shm_fd, dsize) < 0)
44 {
45 close(shm_fd);
46 shm_unlink(shmfile);
47 shm_fd = -1;
48 goto failed;
49 }
50 shm_addr = mmap(NULL, dsize, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
51 if (shm_addr == MAP_FAILED)
52 {
53 close(shm_fd);
54 shm_unlink(shmfile);
55 shm_fd = -1;
56 goto failed;
57 }
58 shm_size = dsize;
59 return;
60failed:
61#endif
62 shm_addr = malloc(dsize);
63}
64
65void
66shm_free(void)
67{
68#ifdef HAVE_SHM_OPEN
69 if (shm_fd >= 0)
70 {
71 munmap(shm_addr, shm_size);
72 close(shm_fd);
73 shm_fd = -1;
74 shm_addr = NULL;
75 if (shmfile) free(shmfile);
76 shmfile = NULL;
77 return;
78 }
79#endif
80 free(shm_addr);
81 shm_addr = NULL;
82 shm_fd = -1;
83}
84
85#ifdef __cplusplus
86}
87#endif