From 2f289c64f625962d945ec3bee80f36bc5c61ee35 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Thu, 3 Dec 2020 14:24:40 +0100 Subject: [PATCH] p9y --- CMake/_Include.cmake | 5 ++ include/libmemcached-1.0/configure.h.in | 2 + include/libmemcached-1.0/memcached.h | 28 +----- include/libmemcached-1.0/platform.h | 52 +++++++---- src/CMakeLists.txt | 1 + src/bin/CMakeLists.txt | 1 - src/bin/common/CMakeLists.txt | 4 +- src/bin/common/options.hpp | 7 +- src/bin/memcapable.cc | 16 ++-- src/bin/memcp.cc | 5 +- src/libmemcached/CMakeLists.txt | 3 +- src/libmemcached/behavior.cc | 2 + src/libmemcached/common.h | 8 +- src/libmemcached/connect.cc | 25 +----- src/libmemcached/flush_buffers.cc | 1 + src/libmemcached/get.cc | 1 + src/libmemcached/hash.cc | 8 +- src/libmemcached/hosts.cc | 6 +- src/libmemcached/instance.cc | 1 + src/libmemcached/io.cc | 5 +- src/libmemcached/parse.cc | 10 +-- src/libmemcached/poll.h | 56 ------------ src/libmemcached/purge.cc | 1 + src/libmemcached/response.cc | 2 +- src/libmemcached/sasl.cc | 2 + src/libmemcached/server.cc | 1 + src/libmemcached/server.hpp | 4 +- src/libmemcached/socket.hpp | 54 ------------ src/libmemcached/windows.hpp | 101 ---------------------- src/libmemcachedprotocol/CMakeLists.txt | 1 + src/libmemcachedprotocol/binary_handler.c | 1 + src/libmemcachedprotocol/common.h | 1 - src/libmemcachedprotocol/handler.c | 5 +- src/p9y/CMakeLists.txt | 36 ++++++++ src/{win32 => p9y}/getopt.c | 19 ++-- src/{win32/getopt.h => p9y/getopt.hpp} | 14 ++- src/p9y/gettimeofday.cpp | 23 +++++ src/p9y/gettimeofday.hpp | 12 +++ src/p9y/index.hpp | 15 ++++ src/p9y/libgen.c | 10 +++ src/p9y/libgen.hpp | 20 +++++ src/p9y/p9y.cpp | 1 + src/{libmemcached/poll.cc => p9y/poll.c} | 29 +------ src/p9y/poll.hpp | 44 ++++++++++ src/p9y/random.hpp | 16 ++++ src/p9y/realpath.c | 7 ++ src/p9y/realpath.hpp | 21 +++++ src/p9y/socket.c | 50 +++++++++++ src/p9y/socket.hpp | 81 +++++++++++++++++ test/lib/common.hpp | 1 + 50 files changed, 452 insertions(+), 367 deletions(-) delete mode 100644 src/libmemcached/poll.h delete mode 100644 src/libmemcached/socket.hpp delete mode 100644 src/libmemcached/windows.hpp create mode 100644 src/p9y/CMakeLists.txt rename src/{win32 => p9y}/getopt.c (98%) rename src/{win32/getopt.h => p9y/getopt.hpp} (87%) create mode 100644 src/p9y/gettimeofday.cpp create mode 100644 src/p9y/gettimeofday.hpp create mode 100644 src/p9y/index.hpp create mode 100644 src/p9y/libgen.c create mode 100644 src/p9y/libgen.hpp create mode 100644 src/p9y/p9y.cpp rename src/{libmemcached/poll.cc => p9y/poll.c} (50%) create mode 100644 src/p9y/poll.hpp create mode 100644 src/p9y/random.hpp create mode 100644 src/p9y/realpath.c create mode 100644 src/p9y/realpath.hpp create mode 100644 src/p9y/socket.c create mode 100644 src/p9y/socket.hpp diff --git a/CMake/_Include.cmake b/CMake/_Include.cmake index 6f9a092b..a419d8df 100644 --- a/CMake/_Include.cmake +++ b/CMake/_Include.cmake @@ -136,12 +136,17 @@ check_type("struct msghdr" sys/socket.h) check_cxx_symbol(abi::__cxa_demangle cxxabi.h) check_symbol(fcntl fcntl.h) +check_symbol(gettimeofday sys/time.h) check_symbol(htonll arpa/inet.h) +check_symbol(index strings.h) check_symbol(MSG_DONTWAIT sys/socket.h) check_symbol(MSG_MORE sys/socket.h) check_symbol(MSG_NOSIGNAL sys/socket.h) check_symbol(SO_RCVTIMEO sys/socket.h) check_symbol(SO_SNDTIMEO sys/socket.h) +check_symbol(rand stdlib.h) +check_symbol(random stdlib.h) +check_symbol(realpath stdlib.h) check_symbol(sendmsg sys/socket.h) check_symbol(setenv stdlib.h) check_symbol(strerror_r string.h) diff --git a/include/libmemcached-1.0/configure.h.in b/include/libmemcached-1.0/configure.h.in index fcda4200..dfa55eba 100644 --- a/include/libmemcached-1.0/configure.h.in +++ b/include/libmemcached-1.0/configure.h.in @@ -18,6 +18,8 @@ #cmakedefine01 LIBMEMCACHED_ENABLE_DEPRECATED #cmakedefine01 LIBMEMCACHED_WITH_SASL_SUPPORT +#cmakedefine HAVE_VISIBILITY 1 + #cmakedefine HAVE_NETDB_H 1 #cmakedefine HAVE_IN_PORT_T 1 diff --git a/include/libmemcached-1.0/memcached.h b/include/libmemcached-1.0/memcached.h index 6f87e438..e3eac4e2 100644 --- a/include/libmemcached-1.0/memcached.h +++ b/include/libmemcached-1.0/memcached.h @@ -15,34 +15,8 @@ #pragma once -/* This seems to be required for older compilers @note - * http://stackoverflow.com/questions/8132399/how-to-printf-uint64-t */ -#ifndef __STDC_FORMAT_MACROS -# define __STDC_FORMAT_MACROS -#endif - -#ifdef __cplusplus -# include -# include -# include -#else -# include -# include -# include -# include -#endif - -#include - -#ifndef HAVE_PID_T -typedef int pid_t; -#endif -#ifndef HAVE_SSIZE_T -typedef long int ssize_t; -#endif - -#include "libmemcached-1.0/visibility.h" #include "libmemcached-1.0/configure.h" +#include "libmemcached-1.0/visibility.h" #include "libmemcached-1.0/platform.h" #include "libmemcached-1.0/limits.h" diff --git a/include/libmemcached-1.0/platform.h b/include/libmemcached-1.0/platform.h index a3ccd3d7..1ab1bc07 100644 --- a/include/libmemcached-1.0/platform.h +++ b/include/libmemcached-1.0/platform.h @@ -17,25 +17,45 @@ #include "libmemcached-1.0/configure.h" -#if defined(_WIN32) -# include -# include +/* This seems to be required for older compilers @note + * http://stackoverflow.com/questions/8132399/how-to-printf-uint64-t */ +#ifndef __STDC_FORMAT_MACROS +# define __STDC_FORMAT_MACROS +#endif + +#ifdef __cplusplus +# include +# include +# include +#else +# include +# include +# include +# include +#endif + +#include -# ifndef HAVE_IN_PORT_T +#if defined HAVE_NETDB_H +# include +#endif + +#if !defined HAVE_IN_PORT_T typedef int in_port_t; -# define HAVE_IN_PORT_T 1 -# endif +#endif -typedef SOCKET memcached_socket_t; +#if !defined HAVE_PID_T +typedef int pid_t; +#endif -#else -# include -# include -# include -# include -# include -# include +#ifndef HAVE_SSIZE_T +typedef long int ssize_t; +#endif +#if defined _WIN32 +# include +# include +typedef SOCKET memcached_socket_t; +#else typedef int memcached_socket_t; - -#endif /* _WIN32 */ +#endif // _WIN32 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3eaa0219..87176a23 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,3 +1,4 @@ +add_subdirectory(p9y) add_subdirectory(bin) add_subdirectory(libhashkit) diff --git a/src/bin/CMakeLists.txt b/src/bin/CMakeLists.txt index 187b9cef..7b4c3498 100644 --- a/src/bin/CMakeLists.txt +++ b/src/bin/CMakeLists.txt @@ -19,6 +19,5 @@ target_sources(memcapable PRIVATE ../libmemcached/byteorder.cc) # extra libs -target_link_libraries(memcapable PRIVATE Threads::Threads) target_link_libraries(memping PRIVATE libmemcachedutil) target_link_libraries(memslap PRIVATE Threads::Threads) diff --git a/src/bin/common/CMakeLists.txt b/src/bin/common/CMakeLists.txt index 84443e26..0c77c392 100644 --- a/src/bin/common/CMakeLists.txt +++ b/src/bin/common/CMakeLists.txt @@ -1,9 +1,7 @@ add_library(libclient_common STATIC options.cpp checks.hpp random.hpp time.hpp) -if(NOT HAVE_GETOPT_H) - target_sources(libclient_common PRIVATE ../../win32/getopt.c) -endif() add_library(client_common ALIAS libclient_common) set_target_properties(libclient_common PROPERTIES CXX_STANDARD ${CXX_STANDARD}) +target_link_libraries(libclient_common PRIVATE p9y) target_link_libraries(libclient_common PUBLIC libmemcached) target_include_directories(libclient_common PUBLIC . diff --git a/src/bin/common/options.hpp b/src/bin/common/options.hpp index 980ca38d..65018eff 100644 --- a/src/bin/common/options.hpp +++ b/src/bin/common/options.hpp @@ -24,12 +24,7 @@ #include #include "libmemcached/common.h" - -#ifdef HAVE_GETOPT_H -# include -#elif defined _MSC_VER -# include "win32/getopt.h" -#endif +#include "p9y/getopt.hpp" class client_options { public: diff --git a/src/bin/memcapable.cc b/src/bin/memcapable.cc index 1b9af02a..54e9c224 100644 --- a/src/bin/memcapable.cc +++ b/src/bin/memcapable.cc @@ -32,13 +32,11 @@ # include #endif -#ifndef HAVE_GETOPT_H -# include "../../win32/getopt.h" -#endif +#include "p9y/getopt.hpp" +#include "p9y/socket.hpp" +#include "p9y/poll.hpp" #include "libmemcached-1.0/memcached.h" -#include "libmemcached/poll.h" -#include "libmemcached/socket.hpp" #include "libmemcachedprotocol-0.0/binary.h" #include "libmemcached/byteorder.h" @@ -132,14 +130,14 @@ static memcached_socket_t set_noblock(void) { int flags = fcntl(sock, F_GETFL, 0); if (flags == -1) { perror("Failed to get socket flags"); - memcached_close_socket(sock); + closesocket(sock); return INVALID_SOCKET; } if ((flags & O_NONBLOCK) != O_NONBLOCK) { if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1) { perror("Failed to set socket to nonblocking mode"); - memcached_close_socket(sock); + closesocket(sock); return INVALID_SOCKET; } } @@ -183,8 +181,8 @@ static ssize_t timeout_io_op(memcached_socket_t fd, short direction, const char } else { ret = recv(fd, const_cast(buf), len, 0); } - - if (ret == SOCKET_ERROR && get_socket_errno() == EWOULDBLOCK) { + int local_errno = get_socket_errno(); + if (ret == SOCKET_ERROR && local_errno == EWOULDBLOCK || (EAGAIN != EWOULDBLOCK && local_errno == EAGAIN)) { struct pollfd fds; memset(&fds, 0, sizeof(struct pollfd)); fds.events = direction; diff --git a/src/bin/memcp.cc b/src/bin/memcp.cc index 7ad86787..7396057e 100644 --- a/src/bin/memcp.cc +++ b/src/bin/memcp.cc @@ -21,13 +21,12 @@ #include "common/options.hpp" #include "common/checks.hpp" +#include "p9y/libgen.hpp" +#include "p9y/realpath.hpp" #include #include #include -#if HAVE_LIBGEN_H -# include -#endif #include #include diff --git a/src/libmemcached/CMakeLists.txt b/src/libmemcached/CMakeLists.txt index 15947215..0287fcc9 100644 --- a/src/libmemcached/CMakeLists.txt +++ b/src/libmemcached/CMakeLists.txt @@ -57,7 +57,6 @@ set(LIBMEMCACHED_SOURCES namespace.cc options.cc parse.cc - poll.cc purge.cc quit.cc response.cc @@ -95,6 +94,7 @@ set_target_properties(libmemcached PROPERTIES SOVERSION ${LIBMEMCACHED_SO_VERSION}) target_compile_definitions(libmemcached PRIVATE -DBUILDING_LIBMEMCACHED) target_link_libraries(libmemcached PUBLIC libhashkit Threads::Threads ${CMAKE_DL_LIBS}) +target_link_libraries(libmemcached PRIVATE p9y) if(MSVC) target_link_libraries(libmemcached PUBLIC wsock32 ws2_32) endif() @@ -138,6 +138,7 @@ add_library(memcachedinternal ALIAS libmemcachedinternal) set_target_properties(libmemcachedinternal PROPERTIES CXX_STANDARD ${CXX_STANDARD} LIBRARY_OUTPUT_NAME memcachedinternal) target_compile_definitions(libmemcachedinternal PRIVATE -DBUILDING_LIBMEMCACHEDINTERNAL) target_link_libraries(libmemcachedinternal PUBLIC libhashkit Threads::Threads ${CMAKE_DL_LIBS}) +target_link_libraries(libmemcachedinternal PRIVATE p9y) if(MSVC) target_link_libraries(libmemcached PUBLIC wsock32 ws2_32) endif() diff --git a/src/libmemcached/behavior.cc b/src/libmemcached/behavior.cc index 576ec883..d0d6c6f2 100644 --- a/src/libmemcached/behavior.cc +++ b/src/libmemcached/behavior.cc @@ -16,6 +16,8 @@ #include "libmemcached/common.h" #include "libmemcached/options.hpp" #include "libmemcached/virtual_bucket.h" +#include "p9y/random.hpp" +#include "p9y/socket.hpp" #include #include diff --git a/src/libmemcached/common.h b/src/libmemcached/common.h index eea5319b..defb3b0d 100644 --- a/src/libmemcached/common.h +++ b/src/libmemcached/common.h @@ -65,17 +65,11 @@ # include #endif -#if defined(_WIN32) -# include "libmemcached/windows.hpp" -#endif - #include "libmemcached-1.0/memcached.h" #include "libmemcached/watchpoint.h" #include "libmemcached/is.h" typedef struct memcached_st Memcached; -#include "libmemcached/poll.h" - #ifdef __cplusplus memcached_instance_st *memcached_instance_fetch(memcached_st *ptr, uint32_t server_key); #endif @@ -90,7 +84,6 @@ memcached_instance_st *memcached_instance_fetch(memcached_st *ptr, uint32_t serv # include "libmemcached/io.hpp" # include "libmemcached/udp.hpp" # include "libmemcached/do.hpp" -# include "libmemcached/socket.hpp" # include "libmemcached/connect.hpp" # include "libmemcached/allocators.hpp" # include "libmemcached/hash.hpp" @@ -151,6 +144,7 @@ extern "C" { memcached_return_t run_distribution(memcached_st *ptr); #ifdef __cplusplus +# include "p9y/poll.hpp" static inline void memcached_server_response_increment(memcached_instance_st *instance) { instance->events(POLLIN); instance->cursor_active_++; diff --git a/src/libmemcached/connect.cc b/src/libmemcached/connect.cc index ff7772ca..913e48be 100644 --- a/src/libmemcached/connect.cc +++ b/src/libmemcached/connect.cc @@ -14,32 +14,11 @@ */ #include "libmemcached/common.h" +#include "p9y/socket.hpp" +#include "p9y/poll.hpp" #include -#ifndef SOCK_CLOEXEC -# define SOCK_CLOEXEC 0 -#endif - -#ifndef SOCK_NONBLOCK -# define SOCK_NONBLOCK 0 -#endif - -#ifndef FD_CLOEXEC -# define FD_CLOEXEC 0 -#endif - -#ifndef SO_NOSIGPIPE -# define SO_NOSIGPIPE 0 -#endif - -#ifndef TCP_NODELAY -# define TCP_NODELAY 0 -#endif - -#ifndef TCP_KEEPIDLE -# define TCP_KEEPIDLE 0 -#endif static memcached_return_t connect_poll(memcached_instance_st *server, const int connection_error) { struct pollfd fds[1]; diff --git a/src/libmemcached/flush_buffers.cc b/src/libmemcached/flush_buffers.cc index 512fa2b7..6c58e69c 100644 --- a/src/libmemcached/flush_buffers.cc +++ b/src/libmemcached/flush_buffers.cc @@ -14,6 +14,7 @@ */ #include "libmemcached/common.h" +#include "p9y/socket.hpp" memcached_return_t memcached_flush_buffers(memcached_st *shell) { Memcached *memc = memcached2Memcached(shell); diff --git a/src/libmemcached/get.cc b/src/libmemcached/get.cc index 4dd88061..d625adce 100644 --- a/src/libmemcached/get.cc +++ b/src/libmemcached/get.cc @@ -14,6 +14,7 @@ */ #include "libmemcached/common.h" +#include "p9y/random.hpp" char *memcached_get(memcached_st *ptr, const char *key, size_t key_length, size_t *value_length, uint32_t *flags, memcached_return_t *error) { diff --git a/src/libmemcached/hash.cc b/src/libmemcached/hash.cc index cb1ca464..3e147f87 100644 --- a/src/libmemcached/hash.cc +++ b/src/libmemcached/hash.cc @@ -14,13 +14,9 @@ */ #include "libmemcached/common.h" - -#if HAVE_SYS_TIME_H -# include -#endif -#include - #include "libmemcached/virtual_bucket.h" +#include "p9y/gettimeofday.hpp" +#include "p9y/random.hpp" uint32_t memcached_generate_hash_value(const char *key, size_t key_length, memcached_hash_t hash_algorithm) { diff --git a/src/libmemcached/hosts.cc b/src/libmemcached/hosts.cc index 39247287..8f121502 100644 --- a/src/libmemcached/hosts.cc +++ b/src/libmemcached/hosts.cc @@ -15,12 +15,10 @@ #include "libmemcached/common.h" #include "libmemcached/assert.hpp" +#include "p9y/gettimeofday.hpp" +#include "p9y/random.hpp" #include -#if HAVE_SYS_TIME_H -# include -#endif -#include /* Protoypes (static) */ static memcached_return_t update_continuum(Memcached *ptr); diff --git a/src/libmemcached/instance.cc b/src/libmemcached/instance.cc index 266eccc7..972f6b6f 100644 --- a/src/libmemcached/instance.cc +++ b/src/libmemcached/instance.cc @@ -14,6 +14,7 @@ */ #include "libmemcached/common.h" +#include "p9y/socket.hpp" static inline void _server_init(memcached_instance_st *self, Memcached *root, const memcached_string_t &hostname, in_port_t port, uint32_t weight, diff --git a/src/libmemcached/io.cc b/src/libmemcached/io.cc index 27b5ecbd..94aca617 100644 --- a/src/libmemcached/io.cc +++ b/src/libmemcached/io.cc @@ -15,9 +15,8 @@ #include "libmemcached/common.h" -#ifdef HAVE_SYS_SOCKET_H -# include -#endif +#include "p9y/socket.hpp" +#include "p9y/poll.hpp" void initialize_binary_request(memcached_instance_st *server, protocol_binary_request_header &header) { diff --git a/src/libmemcached/parse.cc b/src/libmemcached/parse.cc index 7130cf46..12431222 100644 --- a/src/libmemcached/parse.cc +++ b/src/libmemcached/parse.cc @@ -26,8 +26,8 @@ memcached_server_list_st memcached_servers_parse(const char *server_strings) { end_ptr = server_strings + strlen(server_strings); - for (begin_ptr = server_strings, string = (char *) index(server_strings, ','); - begin_ptr != end_ptr; string = (char *) index(begin_ptr, ',')) + for (begin_ptr = server_strings, string = (char *) strchr(server_strings, ','); + begin_ptr != end_ptr; string = (char *) strchr(begin_ptr, ',')) { char buffer[HUGE_STRING_LEN]; char *ptr, *ptr2; @@ -44,7 +44,7 @@ memcached_server_list_st memcached_servers_parse(const char *server_strings) { begin_ptr = end_ptr; } - ptr = index(buffer, ':'); + ptr = strchr(buffer, ':'); in_port_t port = 0; if (ptr) { @@ -59,9 +59,9 @@ memcached_server_list_st memcached_servers_parse(const char *server_strings) { return NULL; } - ptr2 = index(ptr, ' '); + ptr2 = strchr(ptr, ' '); if (!ptr2) - ptr2 = index(ptr, ':'); + ptr2 = strchr(ptr, ':'); if (ptr2) { ptr2++; diff --git a/src/libmemcached/poll.h b/src/libmemcached/poll.h deleted file mode 100644 index 8c0ad1ff..00000000 --- a/src/libmemcached/poll.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - +--------------------------------------------------------------------+ - | libmemcached - C/C++ Client Library for memcached | - +--------------------------------------------------------------------+ - | Redistribution and use in source and binary forms, with or without | - | modification, are permitted under the terms of the BSD license. | - | You should have received a copy of the license in a bundled file | - | named LICENSE; in case you did not receive a copy you can review | - | the terms online at: https://opensource.org/licenses/BSD-3-Clause | - +--------------------------------------------------------------------+ - | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | - | Copyright (c) 2020 Michael Wallner | - +--------------------------------------------------------------------+ -*/ - -#pragma once - -#if defined HAVE_SYS_POLL_H -# include -#elif defined HAVE_POLL_H -# include -#elif defined _WIN32 -# include "windows.hpp" -# define poll WSAPoll -typedef int nfds_t; -#elif !defined _MSC_VER - -# ifdef __cplusplus -extern "C" { -# endif - -typedef struct pollfd { -# if defined(_WIN32) - SOCKET fd; -# else - int fd; -# endif - short events; - short revents; -} pollfd_t; - -typedef int nfds_t; - -# define POLLIN 0x0001 -# define POLLOUT 0x0004 -# define POLLERR 0x0008 -# define POLLHUP 0x010 /* Hung up. */ -# define POLLNVAL 0x020 /* Invalid polling request. */ - -int poll(struct pollfd fds[], nfds_t nfds, int tmo); - -# ifdef __cplusplus -} -# endif - -#endif // defined(_WIN32) diff --git a/src/libmemcached/purge.cc b/src/libmemcached/purge.cc index 4437d761..d4b3146d 100644 --- a/src/libmemcached/purge.cc +++ b/src/libmemcached/purge.cc @@ -14,6 +14,7 @@ */ #include "libmemcached/common.h" +#include "p9y/socket.hpp" #define memcached_set_purging(__object, __value) ((__object)->state.is_purging = (__value)) diff --git a/src/libmemcached/response.cc b/src/libmemcached/response.cc index 5f0f0204..13ffefeb 100644 --- a/src/libmemcached/response.cc +++ b/src/libmemcached/response.cc @@ -197,7 +197,7 @@ static memcached_return_t textual_read_one_response(memcached_instance_st *insta and buffer[5] == 'O' and buffer[6] == 'N') /* VERSION */ { /* Find the space, and then move one past it to copy version */ - char *response_ptr = index(buffer, ' '); + char *response_ptr = strchr(buffer, ' '); char *endptr; errno = 0; diff --git a/src/libmemcached/sasl.cc b/src/libmemcached/sasl.cc index 9c8ad52f..4fa981d2 100644 --- a/src/libmemcached/sasl.cc +++ b/src/libmemcached/sasl.cc @@ -14,6 +14,8 @@ */ #include "libmemcached/common.h" +#include "p9y/socket.hpp" + #include #include diff --git a/src/libmemcached/server.cc b/src/libmemcached/server.cc index e038196a..28d063ec 100644 --- a/src/libmemcached/server.cc +++ b/src/libmemcached/server.cc @@ -14,6 +14,7 @@ */ #include "libmemcached/common.h" +#include "p9y/socket.hpp" static inline void _server_init(memcached_server_st *self, Memcached *root, const memcached_string_t &hostname, in_port_t port, uint32_t weight, diff --git a/src/libmemcached/server.hpp b/src/libmemcached/server.hpp index 017242bd..86f6ffb9 100644 --- a/src/libmemcached/server.hpp +++ b/src/libmemcached/server.hpp @@ -15,9 +15,7 @@ #pragma once -#ifdef HAVE_SYS_TIME_H -# include -#endif +#include "p9y/gettimeofday.hpp" #include diff --git a/src/libmemcached/socket.hpp b/src/libmemcached/socket.hpp deleted file mode 100644 index ed7810f7..00000000 --- a/src/libmemcached/socket.hpp +++ /dev/null @@ -1,54 +0,0 @@ -/* - +--------------------------------------------------------------------+ - | libmemcached - C/C++ Client Library for memcached | - +--------------------------------------------------------------------+ - | Redistribution and use in source and binary forms, with or without | - | modification, are permitted under the terms of the BSD license. | - | You should have received a copy of the license in a bundled file | - | named LICENSE; in case you did not receive a copy you can review | - | the terms online at: https://opensource.org/licenses/BSD-3-Clause | - +--------------------------------------------------------------------+ - | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | - | Copyright (c) 2020 Michael Wallner | - +--------------------------------------------------------------------+ -*/ - -#pragma once - -/* To hide the platform differences between MS Windows and Unix, I am - * going to use the Microsoft way and #define the Microsoft-specific - * functions to the unix way. Microsoft use a separate subsystem for sockets, - * but Unix normally just use a filedescriptor on the same functions. It is - * a lot easier to map back to the unix way with macros than going the other - * way without side effect ;-) - */ -#if defined(WIN32) || defined(__MINGW32__) -# include "win32/wrappers.h" -# include "windows.hpp" -# define get_socket_errno() translate_windows_error() -#else -# include -# define INVALID_SOCKET -1 -# define SOCKET_ERROR -1 -# define closesocket(a) close(a) -# define get_socket_errno() errno -#endif - -#ifdef __cplusplus -static inline void memcached_close_socket(memcached_socket_t &socket_fd) { - closesocket(socket_fd); - socket_fd = INVALID_SOCKET; -} -#endif - -#ifndef HAVE_MSG_NOSIGNAL -# define MSG_NOSIGNAL 0 -#endif - -#ifndef HAVE_MSG_DONTWAIT -# define MSG_DONTWAIT 0 -#endif - -#ifndef HAVE_MSG_MORE -# define MSG_MORE 0 -#endif diff --git a/src/libmemcached/windows.hpp b/src/libmemcached/windows.hpp deleted file mode 100644 index b867a6d4..00000000 --- a/src/libmemcached/windows.hpp +++ /dev/null @@ -1,101 +0,0 @@ -/* - +--------------------------------------------------------------------+ - | libmemcached - C/C++ Client Library for memcached | - +--------------------------------------------------------------------+ - | Redistribution and use in source and binary forms, with or without | - | modification, are permitted under the terms of the BSD license. | - | You should have received a copy of the license in a bundled file | - | named LICENSE; in case you did not receive a copy you can review | - | the terms online at: https://opensource.org/licenses/BSD-3-Clause | - +--------------------------------------------------------------------+ - | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | - | Copyright (c) 2020 Michael Wallner | - +--------------------------------------------------------------------+ -*/ - -#pragma once - -#ifdef __cplusplus -# include -#else -# include -#endif - -#ifndef WIN32_LEAN_AND_MEAN -# define WIN32_LEAN_AND_MEAN -#endif - -#if defined(HAVE_WINSOCK2_H) && HAVE_WINSOCK2_H -# include -#endif - -#if defined(HAVE_WS2TCPIP_H) && HAVE_WS2TCPIP_H -# include -#endif - -#if defined(HAVE_IO_H) && HAVE_IO_H -# include -#endif - -struct sockaddr_un { - short int sun_family; - char sun_path[108]; -}; - -static inline int translate_windows_error() { - int local_errno = WSAGetLastError(); - - switch (local_errno) { - case WSAEINVAL: - local_errno = EINPROGRESS; - break; - case WSAEALREADY: - case WSAEWOULDBLOCK: - local_errno = EAGAIN; - break; - - case WSAECONNREFUSED: - local_errno = ECONNREFUSED; - break; - - case WSAENETUNREACH: - local_errno = ENETUNREACH; - break; - - case WSAETIMEDOUT: - local_errno = ETIMEDOUT; - break; - - case WSAECONNRESET: - local_errno = ECONNRESET; - break; - - case WSAEADDRINUSE: - local_errno = EADDRINUSE; - break; - - case WSAEOPNOTSUPP: - local_errno = EOPNOTSUPP; - break; - - case WSAENOPROTOOPT: - local_errno = ENOPROTOOPT; - break; - - default: - break; - } - - return local_errno; -} - -static inline char *basename(const char *filename) { - static char base[MAX_PATH * 2], ext[MAX_PATH], *ptr; - (void) _splitpath_s(filename, NULL, 0, NULL, 0, base, MAX_PATH, ext, MAX_PATH); - strcat_s(base, MAX_PATH * 2 - 1, ext); - return base; -} - -static inline char *realpath(const char *path, char real[MAX_PATH]) { - return _fullpath(real, path, MAX_PATH); -} \ No newline at end of file diff --git a/src/libmemcachedprotocol/CMakeLists.txt b/src/libmemcachedprotocol/CMakeLists.txt index 61fbed15..43d59de1 100644 --- a/src/libmemcachedprotocol/CMakeLists.txt +++ b/src/libmemcachedprotocol/CMakeLists.txt @@ -21,6 +21,7 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL AppleClang) LINK_FLAGS "-Wl,-undefined,dynamic_lookup" ) endif() +target_link_libraries(libmemcachedprotocol PRIVATE p9y) target_link_libraries(libmemcachedprotocol PUBLIC Threads::Threads) if(MSVC) target_link_libraries(libmemcachedprotocol PUBLIC wsock32 ws2_32) diff --git a/src/libmemcachedprotocol/binary_handler.c b/src/libmemcachedprotocol/binary_handler.c index ada4c425..a1b072d0 100644 --- a/src/libmemcachedprotocol/binary_handler.c +++ b/src/libmemcachedprotocol/binary_handler.c @@ -14,6 +14,7 @@ */ #include "libmemcachedprotocol/common.h" +#include "p9y/socket.hpp" #include #include diff --git a/src/libmemcachedprotocol/common.h b/src/libmemcachedprotocol/common.h index ee27ab4a..39f0464e 100644 --- a/src/libmemcachedprotocol/common.h +++ b/src/libmemcachedprotocol/common.h @@ -21,7 +21,6 @@ #include "libmemcachedprotocol-0.0/handler.h" #include "libmemcachedprotocol/cache.h" #include "libmemcached/byteorder.h" -#include "libmemcached/socket.hpp" /* * I don't really need the following two functions as function pointers diff --git a/src/libmemcachedprotocol/handler.c b/src/libmemcachedprotocol/handler.c index b8484072..535795ca 100644 --- a/src/libmemcachedprotocol/handler.c +++ b/src/libmemcachedprotocol/handler.c @@ -26,10 +26,7 @@ #include #include -#include -#ifdef HAVE_SYS_SOCKET_H -# include -#endif +#include "p9y/socket.hpp" /* ** ********************************************************************** diff --git a/src/p9y/CMakeLists.txt b/src/p9y/CMakeLists.txt new file mode 100644 index 00000000..1734f0b7 --- /dev/null +++ b/src/p9y/CMakeLists.txt @@ -0,0 +1,36 @@ +add_library(p9y STATIC + getopt.hpp + libgen.hpp + realpath.hpp + socket.hpp + poll.hpp + random.hpp + index.hpp + p9y.cpp + ) + +target_include_directories(p9y PRIVATE + . + ${CMAKE_SOURCE_DIR}/include + ${CMAKE_BINARY_DIR} + ${CMAKE_BINARY_DIR}/include + ) + +if(NOT HAVE_GETOPT_H) + target_sources(p9y PRIVATE getopt.c) +endif() +if(NOT HAVE_LIBGEN_H) + target_sources(p9y PRIVATE libgen.c) +endif() +if(NOT HAVE_REALPATH) + target_sources(p9y PRIVATE realpath.c) +endif() +if(WIN32) + target_sources(p9y PRIVATE socket.c) +endif() +if(NOT HAVE_POLL_H AND NOT HAVE_SYS_POLL_H AND NOT WIN32) + target_sources(p9y PRIVATE poll.c) +endif() +if(NOT HAVE_GETTIMEOFDAY) + target_sources(p9y PRIVATE gettimeofday.cpp) +endif() diff --git a/src/win32/getopt.c b/src/p9y/getopt.c similarity index 98% rename from src/win32/getopt.c rename to src/p9y/getopt.c index dd8cd704..40434941 100644 --- a/src/win32/getopt.c +++ b/src/p9y/getopt.c @@ -3,9 +3,9 @@ * DISCLAIMER * This file is part of the mingw-w64 runtime package. * - * The mingw-w64 runtime package and its code is distributed in the hope that it - * will be useful but WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESSED OR - * IMPLIED ARE HEREBY DISCLAIMED. This includes but is not limited to + * The mingw-w64 runtime package and its code is distributed in the hope that it + * will be useful but WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESSED OR + * IMPLIED ARE HEREBY DISCLAIMED. This includes but is not limited to * warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ /* @@ -58,14 +58,21 @@ #define __GETOPT_H__ -/* All the headers include this file. */ -#include +#ifndef WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN +#endif + +#ifdef _WIN32 +# include +# include +#endif + +#include #include #include #include #include #include -#include #ifdef __cplusplus diff --git a/src/win32/getopt.h b/src/p9y/getopt.hpp similarity index 87% rename from src/win32/getopt.h rename to src/p9y/getopt.hpp index 8f0835b7..f449d601 100644 --- a/src/win32/getopt.h +++ b/src/p9y/getopt.hpp @@ -1,6 +1,14 @@ #pragma once +#include "mem_config.h" + +#ifdef HAVE_GETOPT_H +# include +#else + +# ifdef __cplusplus extern "C" { +# endif extern int opterr; /* if error message should be printed */ extern int optind; /* index into parent argv vector */ @@ -27,4 +35,8 @@ int getopt_long(int nargc, char * const *nargv, const char *options, int getopt_long_only(int nargc, char * const *nargv, const char *options, const struct option *long_options, int *idx); -} \ No newline at end of file +# ifdef __cplusplus +} +# endif + +#endif // HAVE_GETOPT_H diff --git a/src/p9y/gettimeofday.cpp b/src/p9y/gettimeofday.cpp new file mode 100644 index 00000000..7d002ca8 --- /dev/null +++ b/src/p9y/gettimeofday.cpp @@ -0,0 +1,23 @@ +#include "gettimeofday.hpp" + +#include + +#if !defined HAVE_GETTIMEOFDAY +int gettimeofday(struct timeval* tp, struct timezone*) { + using clock = std::chrono::system_clock; + auto as_sec = [] (auto d) { + return std::chrono::duration_cast(d); + }; + auto as_usec = [] (auto d) { + return std::chrono::duration_cast(d); + }; + + auto now = clock::now().time_since_epoch(); + auto sec = as_sec(now); + auto usec = as_usec(now - sec); + + tp->tv_sec = sec.count(); + tp->tv_usec = usec.count(); + return 0; +} +#endif diff --git a/src/p9y/gettimeofday.hpp b/src/p9y/gettimeofday.hpp new file mode 100644 index 00000000..02b8d6d9 --- /dev/null +++ b/src/p9y/gettimeofday.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include "libmemcached-1.0/platform.h" + +#include +#if defined HAVE_SYS_TIME_H +# include +#endif + +#if !defined HAVE_GETTIMEOFDAY +int gettimeofday(struct timeval* tp, struct timezone* tzp); +#endif diff --git a/src/p9y/index.hpp b/src/p9y/index.hpp new file mode 100644 index 00000000..d193bf67 --- /dev/null +++ b/src/p9y/index.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include "mem_config.h" + +#if defined HAVE_STRINGS_H +# include +#elif defined __cplusplus +# include +#else +# include +#endif + +#if !defined HAVE_INDEX +# define index strchr +#endif diff --git a/src/p9y/libgen.c b/src/p9y/libgen.c new file mode 100644 index 00000000..70696eb7 --- /dev/null +++ b/src/p9y/libgen.c @@ -0,0 +1,10 @@ +#include "libgen.hpp" + +#if defined _WIN32 +char *basename(const char *filename) { + static char base[_MAX_PATH * 2], ext[_MAX_PATH], *ptr; + (void) _splitpath_s(filename, NULL, 0, NULL, 0, base, _MAX_PATH, ext, _MAX_PATH); + strcat_s(base, _MAX_PATH * 2 - 1, ext); + return base; +} +#endif diff --git a/src/p9y/libgen.hpp b/src/p9y/libgen.hpp new file mode 100644 index 00000000..82976d7e --- /dev/null +++ b/src/p9y/libgen.hpp @@ -0,0 +1,20 @@ + +#include "mem_config.h" + +#if defined __cplusplus +# include +#else +# include +#endif + +#if defined HAVE_LIBGEN_H +# include +#elif defined _WIN32 +# if defined __cplusplus +extern "C" { +# endif +char *basename(const char *filename); +# if defined __cplusplus +} +# endif +#endif // HAVE_LIBGEN_H diff --git a/src/p9y/p9y.cpp b/src/p9y/p9y.cpp new file mode 100644 index 00000000..35ee8f01 --- /dev/null +++ b/src/p9y/p9y.cpp @@ -0,0 +1 @@ +// empty stub diff --git a/src/libmemcached/poll.cc b/src/p9y/poll.c similarity index 50% rename from src/libmemcached/poll.cc rename to src/p9y/poll.c index 5914aa47..b745c8be 100644 --- a/src/libmemcached/poll.cc +++ b/src/p9y/poll.c @@ -1,29 +1,6 @@ -/* - +--------------------------------------------------------------------+ - | libmemcached - C/C++ Client Library for memcached | - +--------------------------------------------------------------------+ - | Redistribution and use in source and binary forms, with or without | - | modification, are permitted under the terms of the BSD license. | - | You should have received a copy of the license in a bundled file | - | named LICENSE; in case you did not receive a copy you can review | - | the terms online at: https://opensource.org/licenses/BSD-3-Clause | - +--------------------------------------------------------------------+ - | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | - | Copyright (c) 2020 Michael Wallner | - +--------------------------------------------------------------------+ -*/ +#include "poll.hpp" -#include "libmemcached/common.h" - -#if defined(_WIN32) -# include "libmemcached/poll.h" -# if HAVE_SYS_TIME_H -# include -# endif -# include -# if HAVE_STRINGS_H -# include -# endif +#if defined P9Y_NEED_POLL int poll(struct pollfd fds[], nfds_t nfds, int tmo) { fd_set readfds, writefds, errorfds; @@ -76,4 +53,4 @@ int poll(struct pollfd fds[], nfds_t nfds, int tmo) { return ret; } -#endif // defined(_WIN32) +#endif // P9Y_NEED_POLL diff --git a/src/p9y/poll.hpp b/src/p9y/poll.hpp new file mode 100644 index 00000000..6a242618 --- /dev/null +++ b/src/p9y/poll.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include "libmemcached-1.0/platform.h" + +#if defined HAVE_SYS_POLL_H +# include +#elif defined HAVE_POLL_H +# include +#elif defined _WIN32 +# define poll WSAPoll +typedef int nfds_t; +#else + +# define P9Y_NEED_POLL + +# ifdef __cplusplus +extern "C" { +# endif + +typedef struct pollfd { +# if defined(_WIN32) + SOCKET fd; +# else + int fd; +# endif + short events; + short revents; +} pollfd_t; + +typedef int nfds_t; + +# define POLLIN 0x0001 +# define POLLOUT 0x0004 +# define POLLERR 0x0008 +# define POLLHUP 0x010 /* Hung up. */ +# define POLLNVAL 0x020 /* Invalid polling request. */ + +int poll(struct pollfd fds[], nfds_t nfds, int tmo); + +# ifdef __cplusplus +} +# endif + +#endif diff --git a/src/p9y/random.hpp b/src/p9y/random.hpp new file mode 100644 index 00000000..213d2adb --- /dev/null +++ b/src/p9y/random.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include "mem_config.h" + +#if defined __cplusplus +# include +#else +# include +#endif + +#if !defined HAVE_RANDOM +# if defined HAVE_RAND +# define random rand +# define srandom srand +# endif +#endif diff --git a/src/p9y/realpath.c b/src/p9y/realpath.c new file mode 100644 index 00000000..cf6430a2 --- /dev/null +++ b/src/p9y/realpath.c @@ -0,0 +1,7 @@ +#include "realpath.hpp" + +#if defined _WIN32 +char *realpath(const char *path, char real[_MAX_PATH]) { + return _fullpath(real, path, _MAX_PATH); +} +#endif diff --git a/src/p9y/realpath.hpp b/src/p9y/realpath.hpp new file mode 100644 index 00000000..ed63cfac --- /dev/null +++ b/src/p9y/realpath.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "mem_config.h" + +#if defined __cplusplus +# include +#else +# include +#endif + +#if !defined HAVE_REALPATH +# if defined _WIN32 +# if defined __cplusplus +extern "C" { +# endif +char *realpath(const char *path, char real[_MAX_PATH]); +# if defined __cplusplus +} +# endif +# endif // _WIN32 +#endif // HAVE_REALPATH diff --git a/src/p9y/socket.c b/src/p9y/socket.c new file mode 100644 index 00000000..1398e1d8 --- /dev/null +++ b/src/p9y/socket.c @@ -0,0 +1,50 @@ +#include "socket.hpp" + +#if defined _WIN32 +int get_socket_errno() { + int local_errno = WSAGetLastError(); + + switch (local_errno) { + case WSAEINVAL: + local_errno = EINPROGRESS; + break; + case WSAEALREADY: + case WSAEWOULDBLOCK: + local_errno = EAGAIN; + break; + + case WSAECONNREFUSED: + local_errno = ECONNREFUSED; + break; + + case WSAENETUNREACH: + local_errno = ENETUNREACH; + break; + + case WSAETIMEDOUT: + local_errno = ETIMEDOUT; + break; + + case WSAECONNRESET: + local_errno = ECONNRESET; + break; + + case WSAEADDRINUSE: + local_errno = EADDRINUSE; + break; + + case WSAEOPNOTSUPP: + local_errno = EOPNOTSUPP; + break; + + case WSAENOPROTOOPT: + local_errno = ENOPROTOOPT; + break; + + default: + break; + } + + return local_errno; +} +#endif // _WIN32 diff --git a/src/p9y/socket.hpp b/src/p9y/socket.hpp new file mode 100644 index 00000000..339b5995 --- /dev/null +++ b/src/p9y/socket.hpp @@ -0,0 +1,81 @@ +#pragma once + +#include "libmemcached-1.0/platform.h" + +#if defined __cplusplus +# include +#else +# include +#endif + +#if defined HAVE_ARPA_INET_H +# include +#endif + +#if defined HAVE_SYS_SOCKET_H +# include +#endif + +#if defined HAVE_SYS_UN_H +# include +#endif + +#if defined HAVE_UNISTD_H +# include +#endif + +#if defined __cplusplus +extern "C" { +#endif + +# if defined _WIN32 +int get_socket_errno(); +# define SHUT_WR SD_SEND +# define SHUT_RD SD_RECEIVE +# define SHUT_RDWR SD_BOTH +# else +# define closesocket close +# define get_socket_errno() errno +# define INVALID_SOCKET (-1) +# define SOCKET_ERROR (-1) +# endif + +#if defined __cplusplus +} +#endif + +#if !defined SOCK_CLOEXEC +# define SOCK_CLOEXEC 0 +#endif + +#if !defined SOCK_NONBLOCK +# define SOCK_NONBLOCK 0 +#endif + +#if !defined FD_CLOEXEC +# define FD_CLOEXEC 0 +#endif + +#if !defined SO_NOSIGPIPE +# define SO_NOSIGPIPE 0 +#endif + +#if !defined TCP_NODELAY +# define TCP_NODELAY 0 +#endif + +#if !defined TCP_KEEPIDLE +# define TCP_KEEPIDLE 0 +#endif + +#if !defined EAI_SYSTEM +# define EAI_SYSTEM (-1) +#endif + +#if !defined HAVE_MSG_NOSIGNAL +# define MSG_NOSIGNAL 0 +#endif + +#if !defined HAVE_MSG_MORE +# define MSG_MORE 0 +#endif diff --git a/test/lib/common.hpp b/test/lib/common.hpp index e522f9cc..083efe0d 100644 --- a/test/lib/common.hpp +++ b/test/lib/common.hpp @@ -16,6 +16,7 @@ #pragma once #include +#include #include #include #include -- 2.30.2