summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2023-09-15 15:46:59 +0200
committerMitya Selivanov <automainint@guattari.tech>2023-09-15 15:46:59 +0200
commitbdcfa4e239d2c913c7045d0aa835ddd5a47e5436 (patch)
tree9751af52afbead783bd57b9d4eeda74b7906c266 /include
parent0354e4c4393e373f38d7e47103845680c1210104 (diff)
downloadkit-bdcfa4e239d2c913c7045d0aa835ddd5a47e5436.zip
Update single-header
Diffstat (limited to 'include')
-rw-r--r--include/kit.inl.h374
1 files changed, 187 insertions, 187 deletions
diff --git a/include/kit.inl.h b/include/kit.inl.h
index 417001f..55e05ab 100644
--- a/include/kit.inl.h
+++ b/include/kit.inl.h
@@ -482,42 +482,6 @@ void thrd_yield(void);
#endif
/*********************************************************************
* *
- * File: source/kit/shared_memory.h *
- * *
- *********************************************************************/
-#ifndef KIT_SHARED_MEMORY_H
-#define KIT_SHARED_MEMORY_H
-#if !defined(_WIN32) || defined(__CYGWIN__)
-# include <limits.h>
-#endif
-#ifdef __cplusplus
-extern "C" {
-#endif
-typedef struct {
- kit_status_t status;
- i64 size;
- u8 *bytes;
-#if defined(_WIN32) && !defined(__CYGWIN__)
- void *_handle;
-#else
- i8 _owned;
- i32 _fd;
- char _name[NAME_MAX + 1];
-#endif
-} kit_shared_memory_t;
-enum {
- KIT_SHARED_MEMORY_OPEN,
- KIT_SHARED_MEMORY_CREATE,
-};
-kit_shared_memory_t kit_shared_memory_open(kit_str_t name, i64 size,
- i32 mode);
-kit_status_t kit_shared_memory_close(kit_shared_memory_t *mem);
-#ifdef __cplusplus
-}
-#endif
-#endif
-/*********************************************************************
- * *
* File: source/kit/array_ref.h *
* *
*********************************************************************/
@@ -1839,6 +1803,42 @@ static i32 kit_socket_set_nonblocking(socket_t s) {
# endif
#endif
#endif
+/*********************************************************************
+ * *
+ * File: source/kit/shared_memory.h *
+ * *
+ *********************************************************************/
+#ifndef KIT_SHARED_MEMORY_H
+#define KIT_SHARED_MEMORY_H
+#if !defined(_WIN32) || defined(__CYGWIN__)
+# include <limits.h>
+#endif
+#ifdef __cplusplus
+extern "C" {
+#endif
+typedef struct {
+ kit_status_t status;
+ i64 size;
+ u8 *bytes;
+#if defined(_WIN32) && !defined(__CYGWIN__)
+ void *_handle;
+#else
+ i8 _owned;
+ i32 _fd;
+ char _name[NAME_MAX + 1];
+#endif
+} kit_shared_memory_t;
+enum {
+ KIT_SHARED_MEMORY_OPEN,
+ KIT_SHARED_MEMORY_CREATE,
+};
+kit_shared_memory_t kit_shared_memory_open(kit_str_t name, i64 size,
+ i32 mode);
+kit_status_t kit_shared_memory_close(kit_shared_memory_t *mem);
+#ifdef __cplusplus
+}
+#endif
+#endif
#ifdef KIT_IMPLEMENTATION
/*********************************************************************
* *
@@ -2762,157 +2762,6 @@ void thrd_yield(void) {
#endif
/*********************************************************************
* *
- * File: source/kit/shared_memory.posix.c *
- * *
- *********************************************************************/
-#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_SHM_OPEN_FAILED;
- return mem;
- }
- if (mode == KIT_SHARED_MEMORY_CREATE && ftruncate(fd, size) == -1) {
- shm_unlink(mem._name);
- assert(0);
- mem.status = KIT_ERROR_FTRUNCATE_FAILED;
- return mem;
- }
- void *p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
- 0);
- if (p == MAP_FAILED) {
- shm_unlink(mem._name);
- assert(0);
- mem.status = KIT_ERROR_MMAP_FAILED;
- return mem;
- }
- mem.status = KIT_OK;
- mem.size = size;
- mem.bytes = (u8 *) p;
- mem._owned = (mode == KIT_SHARED_MEMORY_CREATE);
- mem._fd = fd;
- return mem;
-}
-kit_status_t kit_shared_memory_close(kit_shared_memory_t *mem) {
- assert(mem != NULL);
- if (mem == NULL)
- return KIT_ERROR_INVALID_ARGUMENT;
- kit_status_t status = KIT_OK;
- if (munmap(mem->bytes, mem->size) != 0)
- status |= KIT_ERROR_MUNMAP_FAILED;
- if (mem->_owned && shm_unlink(mem->_name) != 0)
- status |= KIT_ERROR_UNLINK_FAILED;
- return status;
-}
-#endif
-/*********************************************************************
- * *
- * File: source/kit/shared_memory.win32.c *
- * *
- *********************************************************************/
-#if defined(_WIN32) && !defined(__CYGWIN__)
-# ifndef WIN32_LEAN_AND_MEAN
-# define WIN32_LEAN_AND_MEAN
-# endif
-# ifndef NOMINMAX
-# define NOMINMAX
-# endif
-# include <windows.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);
- char buf[264] = "Global\\";
- assert(size > 0);
- assert(name.size > 0);
- assert(name.size + 8 < sizeof buf);
- assert(name.values != NULL);
- if (name.size <= 0) {
- mem.status = KIT_ERROR_INVALID_NAME;
- return mem;
- }
- if (name.size + 8 >= sizeof buf) {
- 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;
- }
- memcpy(buf + 7, name.values, name.size);
- buf[7 + name.size] = '\0';
- HANDLE h = mode == KIT_SHARED_MEMORY_CREATE
- ? CreateFileMappingA(INVALID_HANDLE_VALUE, NULL,
- PAGE_READWRITE, 0, size, buf)
- : OpenFileMappingA(FILE_MAP_ALL_ACCESS, 0, buf);
- if (h == NULL) {
- mem.status = KIT_ERROR_SHM_OPEN_FAILED;
- return mem;
- }
- void *p = MapViewOfFile(h, FILE_MAP_ALL_ACCESS, 0, 0, size);
- assert(p != NULL);
- if (p == NULL) {
- CloseHandle(h);
- mem.status = KIT_ERROR_MMAP_FAILED;
- return mem;
- }
- mem.status = KIT_OK;
- mem.size = size;
- mem.bytes = (u8 *) p;
- mem._handle = h;
- return mem;
-}
-kit_status_t kit_shared_memory_close(kit_shared_memory_t *mem) {
- assert(mem != NULL);
- i32 status = KIT_OK;
- if (!UnmapViewOfFile(mem->bytes))
- status |= KIT_ERROR_MUNMAP_FAILED;
- if (!CloseHandle(mem->_handle))
- status |= KIT_ERROR_UNLINK_FAILED;
- return status;
-}
-#endif
-/*********************************************************************
- * *
* File: source/kit/dynamic_array.c *
* *
*********************************************************************/
@@ -3841,5 +3690,156 @@ kit_sha256_hash_t kit_sha256(i64 in_size, u8 *in_data) {
#undef EP1
#undef SIG0
#undef SIG1
+/*********************************************************************
+ * *
+ * File: source/kit/shared_memory.posix.c *
+ * *
+ *********************************************************************/
+#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_SHM_OPEN_FAILED;
+ return mem;
+ }
+ if (mode == KIT_SHARED_MEMORY_CREATE && ftruncate(fd, size) == -1) {
+ shm_unlink(mem._name);
+ assert(0);
+ mem.status = KIT_ERROR_FTRUNCATE_FAILED;
+ return mem;
+ }
+ void *p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
+ 0);
+ if (p == MAP_FAILED) {
+ shm_unlink(mem._name);
+ assert(0);
+ mem.status = KIT_ERROR_MMAP_FAILED;
+ return mem;
+ }
+ mem.status = KIT_OK;
+ mem.size = size;
+ mem.bytes = (u8 *) p;
+ mem._owned = (mode == KIT_SHARED_MEMORY_CREATE);
+ mem._fd = fd;
+ return mem;
+}
+kit_status_t kit_shared_memory_close(kit_shared_memory_t *mem) {
+ assert(mem != NULL);
+ if (mem == NULL)
+ return KIT_ERROR_INVALID_ARGUMENT;
+ kit_status_t status = KIT_OK;
+ if (munmap(mem->bytes, mem->size) != 0)
+ status |= KIT_ERROR_MUNMAP_FAILED;
+ if (mem->_owned && shm_unlink(mem->_name) != 0)
+ status |= KIT_ERROR_UNLINK_FAILED;
+ return status;
+}
+#endif
+/*********************************************************************
+ * *
+ * File: source/kit/shared_memory.win32.c *
+ * *
+ *********************************************************************/
+#if defined(_WIN32) && !defined(__CYGWIN__)
+# ifndef WIN32_LEAN_AND_MEAN
+# define WIN32_LEAN_AND_MEAN
+# endif
+# ifndef NOMINMAX
+# define NOMINMAX
+# endif
+# include <windows.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);
+ char buf[264] = "Global\\";
+ assert(size > 0);
+ assert(name.size > 0);
+ assert(name.size + 8 < sizeof buf);
+ assert(name.values != NULL);
+ if (name.size <= 0) {
+ mem.status = KIT_ERROR_INVALID_NAME;
+ return mem;
+ }
+ if (name.size + 8 >= sizeof buf) {
+ 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;
+ }
+ memcpy(buf + 7, name.values, name.size);
+ buf[7 + name.size] = '\0';
+ HANDLE h = mode == KIT_SHARED_MEMORY_CREATE
+ ? CreateFileMappingA(INVALID_HANDLE_VALUE, NULL,
+ PAGE_READWRITE, 0, size, buf)
+ : OpenFileMappingA(FILE_MAP_ALL_ACCESS, 0, buf);
+ if (h == NULL) {
+ mem.status = KIT_ERROR_SHM_OPEN_FAILED;
+ return mem;
+ }
+ void *p = MapViewOfFile(h, FILE_MAP_ALL_ACCESS, 0, 0, size);
+ assert(p != NULL);
+ if (p == NULL) {
+ CloseHandle(h);
+ mem.status = KIT_ERROR_MMAP_FAILED;
+ return mem;
+ }
+ mem.status = KIT_OK;
+ mem.size = size;
+ mem.bytes = (u8 *) p;
+ mem._handle = h;
+ return mem;
+}
+kit_status_t kit_shared_memory_close(kit_shared_memory_t *mem) {
+ assert(mem != NULL);
+ i32 status = KIT_OK;
+ if (!UnmapViewOfFile(mem->bytes))
+ status |= KIT_ERROR_MUNMAP_FAILED;
+ if (!CloseHandle(mem->_handle))
+ status |= KIT_ERROR_UNLINK_FAILED;
+ return status;
+}
+#endif
#endif
#endif