summaryrefslogtreecommitdiff
path: root/kit/shared_memory.posix.c
blob: fa0db986ca2f6493941d6dd045ab947fbaf28ea3 (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
#include "shared_memory.h"

#if !defined(_WIN32) || defined(__CYGWIN__)
#  include <stdio.h>
#  include <string.h>
#  include <sys/mman.h>
#  include <sys/stat.h>
#  include <fcntl.h>
#  include <unistd.h>
#  include <assert.h>

kit_shared_memory_t kit_shared_memory_open(kit_str_t name, i64 size,
                                           i32 mode) {
  kit_shared_memory_t mem;

  memset(&mem, 0, sizeof mem);

  assert(size > 0);
  assert(name.size > 0);
  assert(name.size + 1 <= NAME_MAX);
  assert(name.values != NULL);

  if (size <= 0) {
    mem.status = KIT_ERROR_INVALID_SIZE;
    return mem;
  }

  if (name.size <= 0) {
    mem.status = KIT_ERROR_INVALID_NAME;
    return mem;
  }

  if (name.size + 1 > NAME_MAX) {
    mem.status = KIT_ERROR_NAME_TOO_LONG;
    return mem;
  }

  for (i64 i = 0; i < name.size; i++)
    if (name.values[i] == '/' || name.values[i] == '\\') {
      mem.status = KIT_ERROR_INVALID_NAME;
      return mem;
    }

  mem._name[0] = '/';
  memcpy(mem._name + 1, name.values, name.size);
  mem._name[1 + name.size] = '\0';

  i32 fd = shm_open(mem._name,
                    mode == KIT_SHARED_MEMORY_CREATE
                        ? O_RDWR | O_CREAT | O_EXCL
                        : O_RDWR,
                    mode == KIT_SHARED_MEMORY_CREATE ? 0660 : 0);

  if (fd == -1) {
    mem.status = KIT_ERROR_OPEN_FAILED;
    return mem;
  }

  if (mode == KIT_SHARED_MEMORY_CREATE && ftruncate(fd, size) == -1) {
    shm_unlink(mem._name);
    assert(0);
    mem.status = KIT_ERROR_TRUNCATE_FAILED;
    return mem;
  }

  void *p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
                 0);

  close(fd);

  if (p == MAP_FAILED) {
    shm_unlink(mem._name);
    mem.status = KIT_ERROR_MAP_FAILED;
    return mem;
  }

  mem.status = KIT_OK;
  mem.size   = size;
  mem.bytes  = (u8 *) p;
  mem._owned = (mode == KIT_SHARED_MEMORY_CREATE);
  return mem;
}

s32 kit_shared_memory_close(kit_shared_memory_t *mem) {
  assert(mem != NULL);

  if (mem == NULL)
    return KIT_ERROR_INVALID_ARGUMENT;

  s32 status = KIT_OK;

  if (munmap(mem->bytes, mem->size) != 0)
    status |= KIT_ERROR_UNMAP_FAILED;
  if (mem->_owned && shm_unlink(mem->_name) != 0)
    status |= KIT_ERROR_UNLINK_FAILED;

  return status;
}

s32 kit_shared_memory_clean(kit_str_t name) {
  assert(name.size > 0);
  assert(name.size + 1 <= NAME_MAX);
  assert(name.values != NULL);

  if (name.size <= 0)
    return KIT_ERROR_INVALID_NAME;

  if (name.size + 1 > NAME_MAX)
    return KIT_ERROR_NAME_TOO_LONG;

  for (i64 i = 0; i < name.size; i++)
    if (name.values[i] == '/' || name.values[i] == '\\')
      return KIT_ERROR_INVALID_NAME;

  char buf[NAME_MAX + 1] = "/";
  memcpy(buf + 1, name.values, name.size);
  buf[1 + name.size] = '\0';

  if (shm_unlink(buf) != 0)
    return KIT_ERROR_UNLINK_FAILED;

  return KIT_OK;
}
#endif