summaryrefslogtreecommitdiffhomepage
path: root/compat/shm.c
blob: 95263a08ddde416d78cfac32491e3353dc29476c (plain)
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
#define BUILD_SHM
#include "shm.h"

#ifdef SHM_WIN32

#include <windows.h>

SHM_FUN(void, init, const char* shm_name, const char* mutex_name, int map_size)
{
    if (mutex_name != NULL)
    {
        self->mutex = CreateMutexA(NULL, false, mutex_name);

        if (!self->mutex)
            goto fail;
    }

    self->mapped_file = CreateFileMappingA(
        INVALID_HANDLE_VALUE,
        NULL,
        PAGE_READWRITE,
        0,
        (unsigned)map_size,
        shm_name);

    if (!self->mapped_file)
        goto fail;

    self->mem = MapViewOfFile(self->mapped_file,
                              FILE_MAP_WRITE,
                              0,
                              0,
                              (unsigned) map_size);

    if (!self->mem)
        goto fail;

    return;

fail:
    SHM_FUN_NAME(free)(self);
}

SHM_FUN0(void, free)
{
    if (self->mem)
        (void) UnmapViewOfFile(self->mem);

    if (self->mapped_file)
        (void) CloseHandle(self->mapped_file);

    if (self->mutex)
        (void) CloseHandle(self->mutex);

    self->mem = NULL;
    self->mapped_file = NULL;
    self->mutex = NULL;
}

SHM_FUN0(void, lock)
{
    if (self->mutex)
        (void)(WaitForSingleObject(self->mutex, INFINITE) == WAIT_OBJECT_0);
}

SHM_FUN0(void, unlock)
{
    (void) ReleaseMutex(self->mutex);
}

SHM_FUN0(bool, success)
{
    return self->mem != NULL;
}

#else

#include <stdio.h>
#include <string.h>
#include <sys/file.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <limits.h>
#include <unistd.h>
#include <sys/types.h>
#include <alloca.h>

//#pragma GCC diagnostic ignored "-Wunused-result"

SHM_FUN(void, init, const char *shm_name, const char* mutex_name, int map_size)
{
    char* filename = alloca(strlen(shm_name)+2);
    (void)mutex_name;

    self->mem = (void*)-1;
    self->fd = -1;
    self->size = 0;

    if (map_size <= 0)
        goto fail;

    self->size = map_size;
    strcpy(filename, "/");
    strcat(filename, shm_name);
    self->fd = shm_open(filename, O_RDWR | O_CREAT, 0600);
    (void)ftruncate(self->fd, (off_t)map_size);
    self->mem = mmap(NULL, (size_t)map_size, PROT_READ|PROT_WRITE, MAP_SHARED, self->fd, (off_t)0);

    if (self->mem == (void*)-1)
        goto fail;

    return;

fail:
    SHM_FUN_NAME(free)(self);
}

SHM_FUN0(void, free)
{
    if (self->mem != (void*)-1)
        (void)munmap(self->mem, self->size);
    if (self->fd != -1)
        (void)close(self->fd);

    self->mem = (void*)-1;
    self->fd = -1;
    self->size = 0;
}

SHM_FUN0(void, lock)
{
    flock(self->fd, LOCK_EX);
}

SHM_FUN0(void, unlock)
{
    flock(self->fd, LOCK_UN);
}

SHM_FUN0(bool, success)
{
    return self->mem != (void*) -1;
}

#endif

SHM_FUN0(void*, ptr)
{
    return self->mem;
}