diff options
-rw-r--r-- | gen_inl.c | 41 | ||||
-rw-r--r-- | include/kit.inl.h | 374 |
2 files changed, 203 insertions, 212 deletions
@@ -8,35 +8,24 @@ exit #include <stdlib.h> #include <string.h> -char *HEADERS[] = { "source/kit/types.h", - "source/kit/status.h", - "source/kit/allocator.h", - "source/kit/time.h", - "source/kit/atomic.h", - "source/kit/threads.h", - "source/kit/shared_memory.h", - "source/kit/array_ref.h", - "source/kit/dynamic_array.h", - "source/kit/string_ref.h", - "source/kit/string_builder.h", - "source/kit/lower_bound.h", - "source/kit/move_back.h", - "source/kit/bigint.h", - "source/kit/input_stream.h", - "source/kit/input_buffer.h", - "source/kit/async_function.h", - "source/kit/file.h", - "source/kit/mersenne_twister_64.h", - "source/kit/secure_random.h", - "source/kit/sha256.h", - "source/kit/sockets.h" }; +char *HEADERS[] = { + "source/kit/types.h", "source/kit/status.h", + "source/kit/allocator.h", "source/kit/time.h", + "source/kit/atomic.h", "source/kit/threads.h", + "source/kit/array_ref.h", "source/kit/dynamic_array.h", + "source/kit/string_ref.h", "source/kit/string_builder.h", + "source/kit/lower_bound.h", "source/kit/move_back.h", + "source/kit/bigint.h", "source/kit/input_stream.h", + "source/kit/input_buffer.h", "source/kit/async_function.h", + "source/kit/file.h", "source/kit/mersenne_twister_64.h", + "source/kit/secure_random.h", "source/kit/sha256.h", + "source/kit/sockets.h", "source/kit/shared_memory.h" +}; char *IMPL[] = { "source/kit/allocator.c", "source/kit/atomic.win32.c", "source/kit/threads.posix.c", "source/kit/threads.win32.c", - "source/kit/shared_memory.posix.c", - "source/kit/shared_memory.win32.c", "source/kit/dynamic_array.c", "source/kit/string_builder.c", "source/kit/input_stream.c", @@ -44,7 +33,9 @@ char *IMPL[] = { "source/kit/allocator.c", "source/kit/file.c", "source/kit/mersenne_twister_64.c", "source/kit/secure_random.c", - "source/kit/sha256.c" }; + "source/kit/sha256.c", + "source/kit/shared_memory.posix.c", + "source/kit/shared_memory.win32.c" }; char *repeat(int n, char c) { static char buf[200]; 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 |