diff options
author | Mitya Selivanov <automainint@guattari.tech> | 2024-07-14 21:12:37 +0200 |
---|---|---|
committer | Mitya Selivanov <automainint@guattari.tech> | 2024-07-14 21:12:37 +0200 |
commit | 30740ca4131d1f574718262451b4410207dc8d4e (patch) | |
tree | fc88b16a216079397ad85b9c6b1a1c1c5712a814 /kit/shared_memory.win32.c | |
parent | 5e3c99bb1cf1d03ea006300121265571f5008fd2 (diff) | |
download | saw-30740ca4131d1f574718262451b4410207dc8d4e.zip |
Reworking the build system
Diffstat (limited to 'kit/shared_memory.win32.c')
-rw-r--r-- | kit/shared_memory.win32.c | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/kit/shared_memory.win32.c b/kit/shared_memory.win32.c new file mode 100644 index 0000000..f1d24d0 --- /dev/null +++ b/kit/shared_memory.win32.c @@ -0,0 +1,91 @@ +#include "shared_memory.h" + +#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 < (i64) sizeof buf); + assert(name.values != NULL); + + if (name.size <= 0) { + mem.status = KIT_ERROR_INVALID_NAME; + return mem; + } + + if (name.size + 8 >= (i64) 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, + (DWORD) (size >> 32), (DWORD) size, buf) + : OpenFileMappingA(FILE_MAP_ALL_ACCESS, 0, buf); + + if (h == INVALID_HANDLE_VALUE) { + mem.status = KIT_ERROR_OPEN_FAILED; + return mem; + } + + void *p = MapViewOfFile(h, FILE_MAP_ALL_ACCESS, 0, 0, + (SIZE_T) size); + + if (p == NULL) { + CloseHandle(h); + mem.status = KIT_ERROR_MAP_FAILED; + return mem; + } + + mem.status = KIT_OK; + mem.size = size; + mem.bytes = (u8 *) p; + mem._handle = h; + return mem; +} + +s32 kit_shared_memory_close(kit_shared_memory_t *mem) { + assert(mem != NULL); + + i32 status = KIT_OK; + + if (!UnmapViewOfFile(mem->bytes)) + status |= KIT_ERROR_UNMAP_FAILED; + if (!CloseHandle(mem->_handle)) + status |= KIT_ERROR_UNLINK_FAILED; + + return status; +} + +s32 kit_shared_memory_clean(kit_str_t name) { + // Do nothing. + // + + (void) name; + + return KIT_OK; +} +#endif |