diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2019-01-07 13:15:30 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2019-01-16 07:48:47 +0100 |
commit | b3da5c6698d4ec39d62062d23f1da79f9c359cdf (patch) | |
tree | 8b74effb405c964653e47a773fb33f7c08c070d0 /compat | |
parent | fd3eb4515639e000b899544827b5fd4ff9473937 (diff) |
Revert "compat/shm, proto/wine: remove duplication"
This reverts commit bab093ebbe392927a92ef201fe60344d5c1191dd.
Diffstat (limited to 'compat')
-rw-r--r-- | compat/shm.c | 150 | ||||
-rw-r--r-- | compat/shm.cpp | 128 | ||||
-rw-r--r-- | compat/shm.h | 87 | ||||
-rw-r--r-- | compat/shm.hpp | 34 |
4 files changed, 142 insertions, 257 deletions
diff --git a/compat/shm.c b/compat/shm.c deleted file mode 100644 index 95263a08..00000000 --- a/compat/shm.c +++ /dev/null @@ -1,150 +0,0 @@ -#define BUILD_SHM -#include "shm.h" - -#ifdef SHM_WIN32 - -#include <windows.h> - -SHM_FUN(void, init, const char* shm_name, const char* mutex_name, int map_size) -{ - if (mutex_name != NULL) - { - self->mutex = CreateMutexA(NULL, false, mutex_name); - - if (!self->mutex) - goto fail; - } - - self->mapped_file = CreateFileMappingA( - INVALID_HANDLE_VALUE, - NULL, - PAGE_READWRITE, - 0, - (unsigned)map_size, - shm_name); - - if (!self->mapped_file) - goto fail; - - self->mem = MapViewOfFile(self->mapped_file, - FILE_MAP_WRITE, - 0, - 0, - (unsigned) map_size); - - if (!self->mem) - goto fail; - - return; - -fail: - SHM_FUN_NAME(free)(self); -} - -SHM_FUN0(void, free) -{ - if (self->mem) - (void) UnmapViewOfFile(self->mem); - - if (self->mapped_file) - (void) CloseHandle(self->mapped_file); - - if (self->mutex) - (void) CloseHandle(self->mutex); - - self->mem = NULL; - self->mapped_file = NULL; - self->mutex = NULL; -} - -SHM_FUN0(void, lock) -{ - if (self->mutex) - (void)(WaitForSingleObject(self->mutex, INFINITE) == WAIT_OBJECT_0); -} - -SHM_FUN0(void, unlock) -{ - (void) ReleaseMutex(self->mutex); -} - -SHM_FUN0(bool, success) -{ - return self->mem != NULL; -} - -#else - -#include <stdio.h> -#include <string.h> -#include <sys/file.h> -#include <sys/mman.h> -#include <fcntl.h> -#include <limits.h> -#include <unistd.h> -#include <sys/types.h> -#include <alloca.h> - -//#pragma GCC diagnostic ignored "-Wunused-result" - -SHM_FUN(void, init, const char *shm_name, const char* mutex_name, int map_size) -{ - char* filename = alloca(strlen(shm_name)+2); - (void)mutex_name; - - self->mem = (void*)-1; - self->fd = -1; - self->size = 0; - - if (map_size <= 0) - goto fail; - - self->size = map_size; - strcpy(filename, "/"); - strcat(filename, shm_name); - self->fd = shm_open(filename, O_RDWR | O_CREAT, 0600); - (void)ftruncate(self->fd, (off_t)map_size); - self->mem = mmap(NULL, (size_t)map_size, PROT_READ|PROT_WRITE, MAP_SHARED, self->fd, (off_t)0); - - if (self->mem == (void*)-1) - goto fail; - - return; - -fail: - SHM_FUN_NAME(free)(self); -} - -SHM_FUN0(void, free) -{ - if (self->mem != (void*)-1) - (void)munmap(self->mem, self->size); - if (self->fd != -1) - (void)close(self->fd); - - self->mem = (void*)-1; - self->fd = -1; - self->size = 0; -} - -SHM_FUN0(void, lock) -{ - flock(self->fd, LOCK_EX); -} - -SHM_FUN0(void, unlock) -{ - flock(self->fd, LOCK_UN); -} - -SHM_FUN0(bool, success) -{ - return self->mem != (void*) -1; -} - -#endif - -SHM_FUN0(void*, ptr) -{ - return self->mem; -} diff --git a/compat/shm.cpp b/compat/shm.cpp index 265d8ff8..1f863190 100644 --- a/compat/shm.cpp +++ b/compat/shm.cpp @@ -5,37 +5,133 @@ * copyright notice and this permission notice appear in all copies. */ -#define BUILD_SHM -#include "shm.hpp" +#include "shm.h" -SHMXX_TYPE_NAME& SHMXX_TYPE_NAME::operator=(SHMXX_TYPE_NAME&&) noexcept = default; +#if defined _WIN32 -// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) -SHMXX_TYPE_NAME::SHMXX_TYPE_NAME(const char* shm_name, const char* mutex_name, int map_size) +#include <cstring> +#include <cstdio> + +#include <accctrl.h> +#include <aclapi.h> + +#ifdef QT_CORE_LIB +# include <QDebug> +# define warn(str, ...) (qDebug() << "shm:" str ": " << __VA_ARGS__) +#else +# define warn(str, ...) (void)0 +#endif + +shm_wrapper::shm_wrapper(const char* shm_name, const char* mutex_name, int map_size) { - SHM_FUN_NAME(init)(&impl, shm_name, mutex_name, map_size); + if (mutex_name == nullptr) + mutex = nullptr; + else + { + mutex = CreateMutexA(nullptr, false, mutex_name); + + if (!mutex) + { + warn("CreateMutexA", (int) GetLastError()); + return; + } + } + + mapped_file = CreateFileMappingA( + INVALID_HANDLE_VALUE, + nullptr, + PAGE_READWRITE, + 0, + map_size, + shm_name); + + if (!mapped_file) + { + warn("CreateFileMappingA", (int) GetLastError()); + + return; + } + + mem = MapViewOfFile(mapped_file, + FILE_MAP_WRITE, + 0, + 0, + map_size); + + if (!mem) + warn("MapViewOfFile:", (int) GetLastError()); } -SHMXX_TYPE_NAME::~SHMXX_TYPE_NAME() +shm_wrapper::~shm_wrapper() { - SHM_FUN_NAME(free)(&impl); + if (mem && !UnmapViewOfFile(mem)) + goto fail; + + if (mapped_file && !CloseHandle(mapped_file)) + goto fail; + + if (mutex && !CloseHandle(mutex)) + goto fail; + + return; + +fail: + warn("failed to close mapping", (int) GetLastError()); } -bool SHMXX_TYPE_NAME::success() noexcept +bool shm_wrapper::lock() { - return SHM_FUN_NAME(success)(&impl); + if (mutex) + return WaitForSingleObject(mutex, INFINITE) == WAIT_OBJECT_0; + else + return false; } -void SHMXX_TYPE_NAME::lock() noexcept + +bool shm_wrapper::unlock() +{ + if (mutex) + return ReleaseMutex(mutex); + else + return false; +} +#else + +#include <limits.h> + +#pragma GCC diagnostic ignored "-Wunused-result" +shm_wrapper::shm_wrapper(const char *shm_name, const char* /*mutex_name*/, int map_size) : size(map_size) +{ + char filename[PATH_MAX+2] {}; + strcpy(filename, "/"); + strcat(filename, shm_name); + fd = shm_open(filename, O_RDWR | O_CREAT, 0600); + (void) ftruncate(fd, map_size); + mem = mmap(NULL, map_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, (off_t)0); +} + +shm_wrapper::~shm_wrapper() +{ + (void) munmap(mem, size); + (void) close(fd); +} + +bool shm_wrapper::lock() { - SHM_FUN_NAME(lock)(&impl); + return flock(fd, LOCK_EX) == 0; } -void SHMXX_TYPE_NAME::unlock() noexcept +bool shm_wrapper::unlock() { - SHM_FUN_NAME(unlock)(&impl); + return flock(fd, LOCK_UN) == 0; } +#endif -void* SHMXX_TYPE_NAME::ptr() noexcept +bool shm_wrapper::success() { - return SHM_FUN_NAME(ptr)(&impl); +#ifndef _WIN32 + return mem != (void*) -1; +#else + return mem != nullptr; +#endif } + diff --git a/compat/shm.h b/compat/shm.h index 856b9c8c..814ce90c 100644 --- a/compat/shm.h +++ b/compat/shm.h @@ -1,68 +1,41 @@ -#ifndef SHM_HEADER_GUARD -#define SHM_HEADER_GUARD +/* Copyright (c) 2013 Stanislaw Halik <sthalik@misaki.pl> -#include "macros1.h" + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + */ +#pragma once -#ifndef SHM_WIN32_INIT -# ifdef _WIN32 -# define SHM_WIN32 -# else -# undef SHM_WIN32 -# endif +#if defined(_WIN32) +#include <windows.h> #else -# if SHM_WIN32_INIT -# define SHM_WIN32 -# else -# undef SHM_WIN32 -# endif +#include <stdio.h> +#include <string.h> +#include <sys/file.h> +#include <sys/mman.h> +#include <fcntl.h> +#include <limits.h> +#include <unistd.h> +#include <sys/types.h> #endif -#ifndef SHM_TYPE_NAME -# define SHM_TYPE_NAME shm_mem_impl -#endif - -#ifndef SHM_FUN_PREFIX -# define SHM_FUN_PREFIX shm_mem_impl_ -#endif - -#ifndef SHM_EXPORT -# define SHM_EXPORT -#endif - -#ifndef __cplusplus -# define SHM_EXTERN -# include <stdbool.h> -struct SHM_TYPE_NAME; -typedef struct SHM_TYPE_NAME SHM_TYPE_NAME; -#else -# define SHM_EXTERN extern "C" -#endif +#include "macros.hpp" +#include "export.hpp" -struct SHM_TYPE_NAME { +class OTR_COMPAT_EXPORT shm_wrapper final +{ void* mem; -#ifdef SHM_WIN32 - void* mutex; - void* mapped_file; +#if defined(_WIN32) + HANDLE mutex, mapped_file; #else int fd, size; #endif -}; - -#define SHM_FUN_NAME(f) PP_CAT(SHM_FUN_PREFIX, f) -#define SHM_FUN_(r, f)SHM_EXTERN SHM_EXPORT r SHM_FUN_NAME(f) -#define SHM_FUN(r, f, ...) SHM_FUN_(r, f)(SHM_TYPE_NAME* self, __VA_ARGS__) -#define SHM_FUN0(r, f) SHM_FUN_(r, f)(SHM_TYPE_NAME* self) - -SHM_FUN(void, init, const char* shm_name, const char* mutex_name, int map_size); -SHM_FUN0(void, free); -SHM_FUN0(void, lock); -SHM_FUN0(void, unlock); -SHM_FUN0(void*,ptr); -SHM_FUN0(bool, success); -#ifndef BUILD_SHM -# undef SHM_FUN -# undef SHM_FUN_NAME -#endif - -#endif // SHM_HEADER_GUARD +public: + cc_noinline shm_wrapper(const char *shm_name, const char *mutex_name, int map_size); + cc_noinline ~shm_wrapper(); + cc_noinline bool lock(); + cc_noinline bool unlock(); + cc_noinline bool success(); + inline void* ptr() { return mem; } +}; diff --git a/compat/shm.hpp b/compat/shm.hpp deleted file mode 100644 index 428764e4..00000000 --- a/compat/shm.hpp +++ /dev/null @@ -1,34 +0,0 @@ -/* Copyright (c) 2013 Stanislaw Halik <sthalik@misaki.pl> - - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - */ -#ifndef SHMXX_HEADER_GUARD -#define SHMXX_HEADER_GUARD - -#include "export.hpp" -#include "shm.h" - -#ifndef SHMXX_TYPE_NAME -# define SHMXX_TYPE_NAME mem -#endif - -class OTR_COMPAT_EXPORT SHMXX_TYPE_NAME final -{ - SHM_TYPE_NAME impl; - -public: - SHMXX_TYPE_NAME(const char* shm_name, const char* mutex_name, int map_size); - ~SHMXX_TYPE_NAME(); - - bool success() noexcept; - void* ptr() noexcept; - void lock() noexcept; - void unlock() noexcept; - - SHMXX_TYPE_NAME& operator=(const SHMXX_TYPE_NAME&) = delete; - SHMXX_TYPE_NAME& operator=(SHMXX_TYPE_NAME&&) noexcept; -}; - -#endif // SHMXX_HEADER_GUARD |