summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2023-09-15 15:45:33 +0200
committerMitya Selivanov <automainint@guattari.tech>2023-09-15 15:45:33 +0200
commit0354e4c4393e373f38d7e47103845680c1210104 (patch)
treeba8a21b17337511374753cccd6b4b364c3179c89
parent65ad8250153c735c8e209ddafa4590257cccac89 (diff)
downloadkit-0354e4c4393e373f38d7e47103845680c1210104.zip
Update single-header
-rw-r--r--gen_inl.c3
-rw-r--r--include/kit.inl.h375
-rw-r--r--source/kit/shared_memory.h1
3 files changed, 291 insertions, 88 deletions
diff --git a/gen_inl.c b/gen_inl.c
index f924a0a..bf9eda8 100644
--- a/gen_inl.c
+++ b/gen_inl.c
@@ -14,6 +14,7 @@ char *HEADERS[] = { "source/kit/types.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",
@@ -34,6 +35,8 @@ 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",
diff --git a/include/kit.inl.h b/include/kit.inl.h
index b83e55c..417001f 100644
--- a/include/kit.inl.h
+++ b/include/kit.inl.h
@@ -62,25 +62,29 @@ typedef double f64;
*********************************************************************/
#ifndef KIT_STATUS_H
#define KIT_STATUS_H
-#ifdef __cplusplus
-extern "C" {
-#endif
enum {
- KIT_OK,
- KIT_ERROR_BAD_ALLOC,
- KIT_ERROR_MKDIR_FAILED,
- KIT_ERROR_RMDIR_FAILED,
- KIT_ERROR_UNLINK_FAILED,
- KIT_ERROR_FILE_ALREADY_EXISTS,
- KIT_ERROR_FILE_DO_NOT_EXIST,
- KIT_ERROR_PATH_TOO_LONG,
- KIT_ERROR_SOCKETS_STARTUP_FAILED,
- KIT_ERROR_SOCKET_CONTROL_FAILED
+ KIT_OK = 0,
+ KIT_ERROR_NOT_IMPLEMENTED = 1,
+ KIT_ERROR_BAD_ALLOC = (1 << 1),
+ KIT_ERROR_INVALID_ARGUMENT = (1 << 2),
+ KIT_ERROR_MKDIR_FAILED = (1 << 3),
+ KIT_ERROR_RMDIR_FAILED = (1 << 4),
+ KIT_ERROR_UNLINK_FAILED = (1 << 5),
+ KIT_ERROR_FILE_ALREADY_EXISTS = (1 << 6),
+ KIT_ERROR_FILE_DO_NOT_EXIST = (1 << 7),
+ KIT_ERROR_PATH_TOO_LONG = (1 << 8),
+ KIT_ERROR_SOCKETS_STARTUP_FAILED = (1 << 9),
+ KIT_ERROR_SOCKET_CONTROL_FAILED = (1 << 10),
+ KIT_ERROR_NAME_TOO_LONG = (1 << 11),
+ KIT_ERROR_INVALID_SIZE = (1 << 12),
+ KIT_ERROR_INVALID_NAME = (1 << 13),
+ KIT_ERROR_SHM_OPEN_FAILED = (1 << 14),
+ KIT_ERROR_SHM_UNLINK_FAILED = (1 << 15),
+ KIT_ERROR_FTRUNCATE_FAILED = (1 << 16),
+ KIT_ERROR_MMAP_FAILED = (1 << 17),
+ KIT_ERROR_MUNMAP_FAILED = (1 << 18),
};
typedef i32 kit_status_t;
-#ifdef __cplusplus
-}
-#endif
#endif
/*********************************************************************
* *
@@ -137,6 +141,9 @@ kit_allocator_t kit_alloc_buffer(i64 size, void *buffer);
# define _GNU_SOURCE
#endif
#include <time.h>
+#ifdef __cplusplus
+extern "C" {
+#endif
#ifndef TIME_UTC
# define TIME_UTC 1
#endif
@@ -144,6 +151,9 @@ kit_allocator_t kit_alloc_buffer(i64 size, void *buffer);
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN 1
# endif
+# ifndef NOMINMAX
+# define NOMINMAX 1
+# endif
# include <windows.h>
# define KIT_TIMESPEC_IMPL_UNIX_EPOCH_IN_TICKS 116444736000000000ull
# define KIT_TIMESPEC_IMPL_TICKS_PER_SECONDS 10000000ull
@@ -163,6 +173,9 @@ static int timespec_get(struct timespec *ts, int base) {
return base;
}
#endif
+#ifdef __cplusplus
+}
+#endif
#endif
/*********************************************************************
* *
@@ -173,14 +186,12 @@ static int timespec_get(struct timespec *ts, int base) {
#define KIT_ATOMIC_H
#ifndef _MSC_VER
# include <stdatomic.h>
-# define KIT_ATOMIC(type_) type_ _Atomic
#else
# include <assert.h>
-# define KIT_ATOMIC(type_) type_ volatile
-# define _Atomic volatile
# ifdef __cplusplus
extern "C" {
# endif
+# define _Atomic volatile
enum {
memory_order_relaxed,
memory_order_consume,
@@ -343,13 +354,13 @@ u64 kit_atomic_fetch_add_explicit_64(u64 volatile *var, u64 value,
memory_order_seq_cst)
# define atomic_fetch_add(var_, value_) \
atomic_fetch_add(var_, value_, memory_order_seq_cst)
+# ifdef __cplusplus
+}
+# endif
#endif
#ifndef KIT_DISABLE_SHORT_NAMES
# define ATOMIC KIT_ATOMIC
#endif
-#ifdef __cplusplus
-}
-#endif
#endif
/*********************************************************************
* *
@@ -471,6 +482,42 @@ 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 *
* *
*********************************************************************/
@@ -507,6 +554,9 @@ i8 kit_ar_compare(i64 left_element_size, i64 left_size,
(left_).values, sizeof((right_).values[0]), \
(right_).size, (right_).values, \
(kit_ar_compare_fn) (compare_))
+#ifdef __cplusplus
+}
+#endif
#ifndef KIT_DISABLE_SHORT_NAMES
# define ar_compare_fn kit_ar_compare_fn
# define ar_equal_bytes kit_ar_equal_bytes
@@ -516,9 +566,6 @@ i8 kit_ar_compare(i64 left_element_size, i64 left_size,
# define AR_EQUAL KIT_AR_EQUAL
# define AR_COMPARE KIT_AR_COMPARE
#endif
-#ifdef __cplusplus
-}
-#endif
#endif
/*********************************************************************
* *
@@ -611,6 +658,9 @@ void kit_da_resize_exact(kit_da_void_t *array, i64 element_size,
(array_).values[i_ - 1] = (array_).values[i_]; \
KIT_DA_RESIZE((array_), (array_).size - 1); \
} while (0)
+#ifdef __cplusplus
+}
+#endif
#ifndef KIT_DISABLE_SHORT_NAMES
# define da_void_t kit_da_void_t
# define da_init kit_da_init
@@ -625,9 +675,6 @@ void kit_da_resize_exact(kit_da_void_t *array, i64 element_size,
# define DA_INSERT KIT_DA_INSERT
# define DA_ERASE KIT_DA_ERASE
#endif
-#ifdef __cplusplus
-}
-#endif
#endif
/*********************************************************************
* *
@@ -641,15 +688,15 @@ void kit_da_resize_exact(kit_da_void_t *array, i64 element_size,
extern "C" {
#endif
typedef KIT_AR(char) kit_str_t;
-#ifdef __GNUC__
+#if defined(__GNUC__) || defined(__clang__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wunused-function"
# pragma GCC diagnostic ignored "-Wunknown-pragmas"
# pragma GCC push_options
# pragma GCC optimize("O3")
#endif
-static kit_str_t kit_str(i64 size, char *static_string) {
- kit_str_t s = { .size = size, .values = static_string };
+static kit_str_t kit_str(i64 size, char const *static_string) {
+ kit_str_t s = { .size = size, .values = (char *) static_string };
return s;
}
// Make a barbarian string for C standard library functions.
@@ -668,7 +715,7 @@ static char *kit_make_bs(kit_str_t s) {
index = (index + 1) % 8;
return result;
}
-#ifdef __GNUC__
+#if defined(__GNUC__) || defined(__clang__)
# pragma GCC pop_options
# pragma GCC diagnostic pop
#endif
@@ -731,14 +778,14 @@ extern "C" {
#define KIT_LOWER_BOUND_REF(return_val, array, value, op) \
KIT_LOWER_BOUND_INL(return_val, (array).size, \
(op) ((array).values + index_, (value)))
+#ifdef __cplusplus
+}
+#endif
#ifndef KIT_DISABLE_SHORT_NAMES
# define LOWER_BOUND_INL KIT_LOWER_BOUND_INL
# define LOWER_BOUND KIT_LOWER_BOUND
# define LOWER_BOUND_REF KIT_LOWER_BOUND_REF
#endif
-#ifdef __cplusplus
-}
-#endif
#endif
/*********************************************************************
* *
@@ -777,14 +824,14 @@ extern "C" {
#define KIT_MOVE_BACK_REF(new_size, array, value, cond) \
KIT_MOVE_BACK_INL(new_size, array, \
(cond) ((array).values + index_, (value)))
+#ifdef __cplusplus
+}
+#endif
#ifndef KIT_DISABLE_SHORT_NAMES
# define MOVE_BACK_INL KIT_MOVE_BACK_INL
# define MOVE_BACK KIT_MOVE_BACK
# define MOVE_BACK_REF KIT_MOVE_BACK_REF
#endif
-#ifdef __cplusplus
-}
-#endif
#endif
/*********************************************************************
* *
@@ -804,7 +851,7 @@ extern "C" {
typedef struct {
u32 v[KIT_BIGINT_SIZE / 4];
} kit_bigint_t;
-#ifdef __GNUC__
+#if defined(__GNUC__) || defined(__clang__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wunused-function"
# pragma GCC diagnostic ignored "-Wunknown-pragmas"
@@ -1223,7 +1270,7 @@ static kit_bigint_t kit_bi_from_base58(kit_str_t base58) {
}
return z;
}
-#ifdef __GNUC__
+#if defined(__GNUC__) || defined(__clang__)
# pragma GCC pop_options
# pragma GCC diagnostic pop
#endif
@@ -1237,6 +1284,9 @@ static kit_bigint_t kit_bi_from_base58(kit_str_t base58) {
kit_bi_from_base32(kit_str(sizeof(static_str_) - 1, (static_str_)))
#define KIT_BASE58(static_str_) \
kit_bi_from_base58(kit_str(sizeof(static_str_) - 1, (static_str_)))
+#ifdef __cplusplus
+}
+#endif
#ifndef KIT_DISABLE_SHORT_NAMES
# define bigint_t kit_bigint_t
# define bi_u32 kit_bi_u32
@@ -1263,9 +1313,6 @@ static kit_bigint_t kit_bi_from_base58(kit_str_t base58) {
# define BASE32 KIT_BASE32
# define BASE58 KIT_BASE58
#endif
-#ifdef __cplusplus
-}
-#endif
#endif
/*********************************************************************
* *
@@ -1291,6 +1338,9 @@ void kit_is_destroy(kit_is_handle_t in);
#define KIT_IS_WRAP_FILE(f) kit_is_wrap_file((f), NULL)
#define KIT_IS_READ(in, destination) \
(in).read((in).state, (destination))
+#ifdef __cplusplus
+}
+#endif
#ifndef KIT_DISABLE_SHORT_NAMES
# define is_read_fn kit_is_read_fn
# define is_handle_t kit_is_handle_t
@@ -1301,9 +1351,6 @@ void kit_is_destroy(kit_is_handle_t in);
# define IS_WRAP_FILE KIT_IS_WRAP_FILE
# define IS_READ KIT_IS_READ
#endif
-#ifdef __cplusplus
-}
-#endif
#endif
/*********************************************************************
* *
@@ -1321,14 +1368,17 @@ typedef struct {
void *internal;
kit_str_builder_t data;
} kit_ib_handle_t;
+typedef i8 (*kit_ib_read_condition_fn)(kit_str_t data);
kit_ib_handle_t kit_ib_wrap(kit_is_handle_t upstream,
kit_allocator_t *alloc);
kit_ib_handle_t kit_ib_read(kit_ib_handle_t buf, i64 size);
-typedef int (*kit_ib_read_condition_fn)(kit_str_t data);
kit_ib_handle_t kit_ib_read_while(kit_ib_handle_t buf,
kit_ib_read_condition_fn condition);
void kit_ib_destroy(kit_ib_handle_t buf);
#define KIT_IB_WRAP(upstream) kit_ib_wrap(upstream, NULL)
+#ifdef __cplusplus
+}
+#endif
#ifndef KIT_DISABLE_SHORT_NAMES
# define ib_handle_t kit_ib_handle_t
# define ib_read_condition_fn kit_ib_read_condition_fn
@@ -1338,9 +1388,6 @@ void kit_ib_destroy(kit_ib_handle_t buf);
# define ib_destroy kit_ib_destroy
# define IB_WRAP KIT_IB_WRAP
#endif
-#ifdef __cplusplus
-}
-#endif
#endif
/*********************************************************************
* *
@@ -1353,11 +1400,6 @@ void kit_ib_destroy(kit_ib_handle_t buf);
#ifdef __cplusplus
extern "C" {
#endif
-#ifdef __GNUC__
-# pragma GCC diagnostic push
-# pragma GCC diagnostic ignored "-Wunused-function"
-# pragma GCC diagnostic ignored "-Wunknown-pragmas"
-#endif
typedef struct {
i32 _;
} kit_af_void;
@@ -1371,6 +1413,11 @@ typedef void (*kit_af_state_machine)(void *self_void_);
typedef struct {
KIT_AF_STATE_DATA;
} kit_af_type_void;
+#if defined(__GNUC__) || defined(__clang__)
+# pragma GCC diagnostic push
+# pragma GCC diagnostic ignored "-Wunused-function"
+# pragma GCC diagnostic ignored "-Wunknown-pragmas"
+#endif
#define KIT_AF_INTERNAL(coro_) (*((kit_af_type_void *) (coro_)))
#ifdef KIT_ENABLE_CUSTOM_ASYNC_FUNCTION_DISPATCH
/* Application should implement this function if custom async
@@ -1386,7 +1433,7 @@ static void kit_async_function_dispatch(void *promise) {
KIT_AF_INTERNAL(promise)._state_machine(promise);
}
#endif
-#ifdef __GNUC__
+#if defined(__GNUC__) || defined(__clang__)
# pragma GCC diagnostic pop
#endif
#define KIT_AF_STATE(ret_type_, name_, ...) \
@@ -1504,6 +1551,9 @@ static void kit_async_function_dispatch(void *promise) {
#define KIT_AF_FINISHED_ALL(return_, promises_) \
KIT_AF_FINISHED_N((return_), (promises_), \
sizeof(promises_) / sizeof((promises_)[0]))
+#ifdef __cplusplus
+}
+#endif
#ifndef KIT_DISABLE_SHORT_NAMES
# define af_void kit_af_void
# define af_state_machine kit_af_state_machine
@@ -1535,9 +1585,6 @@ static void kit_async_function_dispatch(void *promise) {
# define AF_FINISHED_N KIT_AF_FINISHED_N
# define AF_FINISHED_ALL KIT_AF_FINISHED_ALL
#endif
-#ifdef __cplusplus
-}
-#endif
#endif
/*********************************************************************
* *
@@ -1549,6 +1596,21 @@ static void kit_async_function_dispatch(void *promise) {
#ifdef __cplusplus
extern "C" {
#endif
+typedef enum {
+ KIT_PATH_NONE,
+ KIT_PATH_FILE,
+ KIT_PATH_FOLDER
+} kit_path_type_t;
+typedef struct {
+ kit_status_t status;
+ i64 time_modified_sec;
+ i32 time_modified_nsec;
+ i64 size;
+} kit_file_info_t;
+typedef struct {
+ kit_status_t status;
+ KIT_DA(kit_str_builder_t) files;
+} kit_path_list_t;
#if defined(_WIN32) && !defined(__CYGWIN__)
# define KIT_PATH_DELIM_C '\\'
# define KIT_PATH_DELIM "\\"
@@ -1572,26 +1634,14 @@ kit_status_t kit_file_remove(kit_str_t path);
kit_status_t kit_file_remove_folder(kit_str_t path);
kit_status_t kit_file_remove_recursive(kit_str_t path,
kit_allocator_t *alloc);
-typedef enum {
- KIT_PATH_NONE,
- KIT_PATH_FILE,
- KIT_PATH_FOLDER
-} kit_path_type_t;
kit_path_type_t kit_path_type(kit_str_t path);
-typedef struct {
- kit_status_t status;
- i64 time_modified_sec;
- i32 time_modified_nsec;
- i64 size;
-} kit_file_info_t;
kit_file_info_t kit_file_info(kit_str_t path);
-typedef struct {
- kit_status_t status;
- KIT_DA(kit_str_builder_t) files;
-} kit_path_list_t;
kit_path_list_t kit_file_enum_folder(kit_str_t path,
kit_allocator_t *alloc);
void kit_path_list_destroy(kit_path_list_t list);
+#ifdef __cplusplus
+}
+#endif
#ifndef KIT_DISABLE_SHORT_NAMES
# define path_norm kit_path_norm
# define path_join kit_path_join
@@ -1619,9 +1669,6 @@ void kit_path_list_destroy(kit_path_list_t list);
# define PATH_FILE KIT_PATH_FILE
# define PATH_FOLDER KIT_PATH_FOLDER
#endif
-#ifdef __cplusplus
-}
-#endif
#endif
/*********************************************************************
* *
@@ -1645,6 +1692,9 @@ void kit_mt64_init_array(kit_mt64_state_t *state, i64 size,
void kit_mt64_init(kit_mt64_state_t *state, u64 seed);
void kit_mt64_rotate(kit_mt64_state_t *state);
u64 kit_mt64_generate(kit_mt64_state_t *state);
+#ifdef __cplusplus
+}
+#endif
#ifndef KIT_DISABLE_SHORT_NAMES
# define mt64_state_t kit_mt64_state_t
# define mt64_init_array kit_mt64_init_array
@@ -1652,9 +1702,6 @@ u64 kit_mt64_generate(kit_mt64_state_t *state);
# define mt64_rotate kit_mt64_rotate
# define mt64_generate kit_mt64_generate
#endif
-#ifdef __cplusplus
-}
-#endif
#endif
/*********************************************************************
* *
@@ -1667,12 +1714,12 @@ u64 kit_mt64_generate(kit_mt64_state_t *state);
extern "C" {
#endif
void kit_secure_random(i64 size, void *data);
-#ifndef KIT_DISABLE_SHORT_NAMES
-# define secure_random kit_secure_random
-#endif
#ifdef __cplusplus
}
#endif
+#ifndef KIT_DISABLE_SHORT_NAMES
+# define secure_random kit_secure_random
+#endif
#endif
/*********************************************************************
* *
@@ -2391,6 +2438,9 @@ void thrd_yield(void) {
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN 1
# endif
+# ifndef NOMINMAX
+# define NOMINMAX
+# endif
# include <windows.h>
/*
Configuration macro:
@@ -2712,6 +2762,157 @@ 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 *
* *
*********************************************************************/
@@ -3312,8 +3513,8 @@ kit_file_info_t kit_file_info(kit_str_t path) {
if (f != INVALID_HANDLE_VALUE) {
FILETIME ft;
if (GetFileTime(f, NULL, NULL, &ft) != 0) {
- ui64 nsec100 = (((ui64) ft.dwHighDateTime) << 32) |
- (ui64) ft.dwLowDateTime;
+ i64 nsec100 = (((u64) ft.dwHighDateTime) << 32) |
+ (u64) ft.dwLowDateTime;
result.time_modified_sec = (i64) (nsec100 / 10000000);
result.time_modified_nsec = (i32) (100 * (nsec100 % 10000000));
} else {
@@ -3321,7 +3522,7 @@ kit_file_info_t kit_file_info(kit_str_t path) {
}
DWORD high;
DWORD low = GetFileSize(f, &high);
- result.size = (i64) ((((ui64) high) << 32) | (ui64) low);
+ result.size = (i64) ((((u64) high) << 32) | (u64) low);
result.status = KIT_OK;
CloseHandle(f);
return result;
@@ -3495,7 +3696,7 @@ void kit_secure_random(i64 size, void *data) {
if (size <= 0 || data == NULL)
return;
#if defined(_WIN32) && !defined(__CYGWIN__)
- HCRYPTPROV prov = NULL;
+ HCRYPTPROV prov = 0;
if (!CryptAcquireContextW(&prov, NULL, NULL, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT | CRYPT_SILENT) ||
!CryptGenRandom(prov, (DWORD) size, (BYTE *) data) ||
diff --git a/source/kit/shared_memory.h b/source/kit/shared_memory.h
index 811bd0b..fc9a082 100644
--- a/source/kit/shared_memory.h
+++ b/source/kit/shared_memory.h
@@ -2,7 +2,6 @@
#define KIT_SHARED_MEMORY_H
#include "status.h"
-#include "allocator.h"
#include "string_ref.h"
#if !defined(_WIN32) || defined(__CYGWIN__)