diff options
author | Mitya Selivanov <automainint@guattari.tech> | 2023-12-29 06:21:33 +0100 |
---|---|---|
committer | Mitya Selivanov <automainint@guattari.tech> | 2023-12-29 06:21:33 +0100 |
commit | 2d6c8fec45b23a8a28668ecf3ef281139ab778a7 (patch) | |
tree | 75d2a8538992129a83c0c2b83688289443d697e5 /source/kit/shared_memory.win32.c | |
parent | 820b171245f2f14766f3accdb0246a4e2c0d596a (diff) | |
download | saw-2d6c8fec45b23a8a28668ecf3ef281139ab778a7.zip |
refactor dependencies; include dependencies source code
Diffstat (limited to 'source/kit/shared_memory.win32.c')
-rw-r--r-- | source/kit/shared_memory.win32.c | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/source/kit/shared_memory.win32.c b/source/kit/shared_memory.win32.c new file mode 100644 index 0000000..34bb276 --- /dev/null +++ b/source/kit/shared_memory.win32.c @@ -0,0 +1,89 @@ +#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 < 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, + (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; +} + +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_UNMAP_FAILED; + if (!CloseHandle(mem->_handle)) + status |= KIT_ERROR_UNLINK_FAILED; + + return status; +} + +kit_status_t kit_shared_memory_clean(kit_str_t name) { + // Do nothing. + // + + return KIT_OK; +} +#endif |