diff options
Diffstat (limited to 'libtransport/includes/hicn/transport/portability')
8 files changed, 414 insertions, 62 deletions
diff --git a/libtransport/includes/hicn/transport/portability/CMakeLists.txt b/libtransport/includes/hicn/transport/portability/CMakeLists.txt index 469b11192..d29ec737c 100644 --- a/libtransport/includes/hicn/transport/portability/CMakeLists.txt +++ b/libtransport/includes/hicn/transport/portability/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (c) 2017-2019 Cisco and/or its affiliates. +# Copyright (c) 2021 Cisco and/or its affiliates. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at: @@ -11,11 +11,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -cmake_minimum_required(VERSION 3.5 FATAL_ERROR) - list(APPEND HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/c_portability.h ${CMAKE_CURRENT_SOURCE_DIR}/portability.h + ${CMAKE_CURRENT_SOURCE_DIR}/cpu.h + ${CMAKE_CURRENT_SOURCE_DIR}/cache.h + ${CMAKE_CURRENT_SOURCE_DIR}/endianess.h ) list(APPEND SOURCE_FILES diff --git a/libtransport/includes/hicn/transport/portability/c_portability.h b/libtransport/includes/hicn/transport/portability/c_portability.h index 71e976a81..bc697d8b1 100644 --- a/libtransport/includes/hicn/transport/portability/c_portability.h +++ b/libtransport/includes/hicn/transport/portability/c_portability.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 Cisco and/or its affiliates. * Copyright 2017 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -33,4 +33,12 @@ #define TRANSPORT_ALWAYS_INLINE inline __attribute__((__always_inline__)) #else #define TRANSPORT_ALWAYS_INLINE inline -#endif
\ No newline at end of file +#endif + +// Unused +#ifdef UNUSED +#elif defined(__GNUC__) || defined(__clang__) +#define UNUSED(x) (void)x +#else +#define UNUSED(x) x +#endif diff --git a/libtransport/includes/hicn/transport/portability/cache.h b/libtransport/includes/hicn/transport/portability/cache.h new file mode 100644 index 000000000..ae113c7d6 --- /dev/null +++ b/libtransport/includes/hicn/transport/portability/cache.h @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2022 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include <hicn/transport/portability/platform.h> + +namespace transport { +namespace portability { +namespace cache { + +/** + * @Prefetch utilities + */ + +/* Default cache line size of 64 bytes. */ +#ifndef LOG2_CACHE_LINE_BYTES +static constexpr const std::size_t klog2_cache_line_bytes = 6; +#else +static constexpr const std::size_t klog2_cache_line_bytes = + LOG2_CACHE_LINE_BYTES; +#endif + +/* How much data prefetch instruction prefetches */ +#ifndef LOG2_CACHE_PREFETCH_BYTES +static constexpr const std::size_t klog2_cache_prefetch_bytes = + klog2_cache_line_bytes; +#else +static constexpr const std::size_t klog2_cache_prefetch_bytes = + LOG2_CACHE_PREFETCH_BYTES; +#endif + +/* Default cache line fill buffers. */ +#ifndef N_PREFETCHES +static constexpr const std::size_t kn_prefetches = 16; +#else +static constexpr const std::size_t kn_prefetches = N_PREFETCHES; +#endif + +static constexpr const std::size_t kcache_line_bytes = + (1 << klog2_cache_line_bytes); +static constexpr const std::size_t kcache_prefetch_bytes = + (1 << klog2_cache_prefetch_bytes); + +static constexpr const int READ = 0; +static constexpr const int LOAD = 0; /* alias for read */ +static constexpr const int WRITE = 1; +static constexpr const int STORE = 1; /* alias for write */ + +#if defined(__GNUC__) || defined(__clang__) +// Clang & GCC + +template <int type> +static inline void _prefetch(uint8_t *addr, std::size_t n, std::size_t size) { + if (size > n * kcache_prefetch_bytes) { + __builtin_prefetch(addr + n * kcache_prefetch_bytes, type, + /* locality */ 3); + } +} + +template <typename T, int type> +static inline void prefetch(T *addr, std::size_t size) { + uint8_t *_addr = reinterpret_cast<uint8_t *>(addr); + + _prefetch<type>(_addr, 0, size); + _prefetch<type>(_addr, 1, size); + _prefetch<type>(_addr, 2, size); + _prefetch<type>(_addr, 3, size); +} +#endif + +} // namespace cache +} // namespace portability +} // namespace transport
\ No newline at end of file diff --git a/libtransport/includes/hicn/transport/portability/cpu.h b/libtransport/includes/hicn/transport/portability/cpu.h new file mode 100644 index 000000000..036bf9cd9 --- /dev/null +++ b/libtransport/includes/hicn/transport/portability/cpu.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2022 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#if defined(__aarch64__) && defined(__ARM_NEON) || defined(__i686__) +#define TRANSPORT_HAVE_VEC128 +#endif + +#if defined(__SSE4_2__) && __GNUC__ >= 4 +#define TRANSPORT_HAVE_VEC128 +#endif + +#if defined(__ALTIVEC__) +#define TRANSPORT_HAVE_VEC128 +#endif + +#if defined(__AVX2__) +#define TRANSPORT_HAVE_VEC256 +#if defined(__clang__) && __clang_major__ < 4 +#undef TRANSPORT_HAVE_VEC256 +#endif +#endif + +#if defined(__AVX512BITALG__) +#define TRANSPORT_HAVE_VEC512 +#endif diff --git a/libtransport/includes/hicn/transport/portability/endianess.h b/libtransport/includes/hicn/transport/portability/endianess.h new file mode 100644 index 000000000..c18ac82cf --- /dev/null +++ b/libtransport/includes/hicn/transport/portability/endianess.h @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2022 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include <hicn/transport/errors/errors.h> + +namespace transport { +namespace portability { + +#if (__BYTE_ORDER__) == (__ORDER_LITTLE_ENDIAN__) +static constexpr const bool kIsBigEndian = false; +static constexpr const bool kIsLittleEndian = true; +#else +static constexpr const bool kIsBigEndian = true; +static constexpr const bool kIsLittleEndian = false; +#endif + +template <typename T> +inline T bswap(T value) { + throw errors::RuntimeException("Not implemented"); +} + +template <> +inline int16_t bswap(int16_t value) { + return __builtin_bswap16(value); +} + +template <> +inline int32_t bswap(int32_t value) { + return __builtin_bswap32(value); +} + +template <> +inline int64_t bswap(int64_t value) { + return __builtin_bswap64(value); +} + +template <> +inline uint16_t bswap(uint16_t value) { + return __builtin_bswap16(value); +} + +template <> +inline uint32_t bswap(uint32_t value) { + return __builtin_bswap32(value); +} + +template <> +inline uint64_t bswap(uint64_t value) { + return __builtin_bswap64(value); +} + +template <typename T> +inline T host_to_net(T value) { + if constexpr (kIsLittleEndian) { + return bswap(value); + } + + return value; +} + +template <typename T> +inline T net_to_host(T value) { + if constexpr (kIsLittleEndian) { + return bswap(value); + } + + return value; +} + +} // namespace portability +} // namespace transport
\ No newline at end of file diff --git a/libtransport/includes/hicn/transport/portability/platform.h b/libtransport/includes/hicn/transport/portability/platform.h new file mode 100644 index 000000000..282d27740 --- /dev/null +++ b/libtransport/includes/hicn/transport/portability/platform.h @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2021 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +/* + * Extract the "MACINTOSH" flag from the compiler. + */ +#if defined(__APPLE__) +#define UNIX +#define MACINTOSH +#endif + +/* + * Extract the "SUNOS" flag from the compiler. + */ +#if defined(sun) +#define UNIX +#define SUNOS +#endif + +/* + * Extract the "LINUX" flag from compiler. + */ +#ifdef __linux__ +#define UNIX +#define LINUX +#endif + +/* + * Extract the "ANDROID" flag from compiler. + */ +#ifdef __ANDROID__ +#define UNIX +#define LINUX +#ifndef ANDROID +#define ANDROID +#endif +#endif + +/* + * Extract the "BSD" flag from compiler. + */ +#if defined(BSD) || defined(__FreeBSD__) || defined(__NetBSD__) || \ + defined(__OpenBSD__) +#define OS_BSD +#define UNIX +#endif + +/* + * Extract the "MSDOS" flag from the compiler. + */ +#ifdef __MSDOS__ +#define MSDOS +#undef UNIX +#endif + +/* + * Extract the "WINDOWS" flag from the compiler. + */ +#if defined(_Windows) || defined(__WINDOWS__) || defined(__WIN32__) || \ + defined(WIN32) || defined(__WINNT__) || defined(__NT__) || \ + defined(_WIN32) || defined(_WIN64) +#define WINDOWS +#ifdef _MSC_VER +#define MSV +#if defined(DEBUG) || defined(DEBUGTRACE) +#ifdef NDEBUG +#undef NDEBUG +#endif +#else +#ifndef NDEBUG +#define NDEBUG +#endif +#endif +#else +#undef MSV +#endif +#undef UNIX +#undef MSDOS +#endif + +/* + * Remove the WINDOWS flag when using MACINTOSH. + */ +#ifdef MACINTOSH +#undef WINDOWS +#endif + +/* + * Assume UNIX if not Windows, Macintosh or MSDOS. + */ +#if !defined(WINDOWS) && !defined(MACINTOSH) && !defined(MSDOS) +#define UNIX +#endif diff --git a/libtransport/includes/hicn/transport/portability/portability.h b/libtransport/includes/hicn/transport/portability/portability.h index 1d97a346e..fd6eca4de 100644 --- a/libtransport/includes/hicn/transport/portability/portability.h +++ b/libtransport/includes/hicn/transport/portability/portability.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 Cisco and/or its affiliates. * Copyright 2017 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -22,29 +22,49 @@ #endif #include <hicn/transport/portability/c_portability.h> - #include <string.h> + #include <cstddef> +namespace transport { namespace portability { -constexpr bool little_endian_arch = __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__; -constexpr bool big_endian_arch = !little_endian_arch; - -#if defined(__GNUC__) -#define _TRANSPORT_GNU_DISABLE_WARNING(warning) #warning -#define TRANSPORT_GNU_DISABLE_WARNING(warning) \ - _Pragma(_TRANSPORT_GNU_DISABLE_WARNING(GCC diagnostic ignored warning)) - +// Generalize warning push/pop. +#if defined(__GNUC__) || defined(__clang__) +// Clang & GCC +#define TRANSPORT_PUSH_WARNING _Pragma("GCC diagnostic push") +#define TRANSPORT_POP_WARNING _Pragma("GCC diagnostic pop") +#define TRANSPORT_GNU_DISABLE_WARNING_INTERNAL2(warningName) #warningName +#define TRANSPORT_GNU_DISABLE_WARNING(warningName) \ + _Pragma(TRANSPORT_GNU_DISABLE_WARNING_INTERNAL2( \ + GCC diagnostic ignored warningName)) #ifdef __clang__ -#define TRANSPORT_CLANG_DISABLE_WARNING(warning) \ - TRANSPORT_GNU_DISABLE_WARNING(warning) -#define TRANSPORT_GCC_DISABLE_WARNING(warning) +#define TRANSPORT_CLANG_DISABLE_WARNING(warningName) \ + TRANSPORT_GNU_DISABLE_WARNING(warningName) +#define TRANSPORT_GCC_DISABLE_WARNING(warningName) #else -#define TRANSPORT_CLANG_DISABLE_WARNING(warning) -#define TRANSPORT_GCC_DISABLE_WARNING(warning) \ - TRANSPORT_GNU_DISABLE_WARNING(warning) +#define TRANSPORT_CLANG_DISABLE_WARNING(warningName) +#define TRANSPORT_GCC_DISABLE_WARNING(warningName) \ + TRANSPORT_GNU_DISABLE_WARNING(warningName) #endif +#define TRANSPORT_MSVC_DISABLE_WARNING(warningNumber) +#elif defined(_MSC_VER) +#define TRANSPORT_PUSH_WARNING __pragma(warning(push)) +#define TRANSPORT_POP_WARNING __pragma(warning(pop)) +// Disable the GCC warnings. +#define TRANSPORT_GNU_DISABLE_WARNING(warningName) +#define TRANSPORT_GCC_DISABLE_WARNING(warningName) +#define TRANSPORT_CLANG_DISABLE_WARNING(warningName) +#define TRANSPORT_MSVC_DISABLE_WARNING(warningNumber) \ + __pragma(warning(disable : warningNumber)) +#else +#define TRANSPORT_PUSH_WARNING +#define TRANSPORT_POP_WARNING +#define TRANSPORT_GNU_DISABLE_WARNING(warningName) +#define TRANSPORT_GCC_DISABLE_WARNING(warningName) +#define TRANSPORT_CLANG_DISABLE_WARNING(warningName) +#define TRANSPORT_MSVC_DISABLE_WARNING(warningNumber) #endif } // namespace portability +} // namespace transport
\ No newline at end of file diff --git a/libtransport/includes/hicn/transport/portability/win_portability.h b/libtransport/includes/hicn/transport/portability/win_portability.h index 65d949291..81aa828cd 100644 --- a/libtransport/includes/hicn/transport/portability/win_portability.h +++ b/libtransport/includes/hicn/transport/portability/win_portability.h @@ -1,42 +1,44 @@ -/*
- * Copyright (c) 2017-2019 Cisco and/or its affiliates.
- * Copyright 2017 Facebook, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#pragma once
-#define WIN32_LEAN_AND_MEAN
-#define NOMINMAX
-#include <fcntl.h>
-#include <io.h>
-#include <parc/windows/parc_Utils.h>
-#include <stdint.h>
-#include <stdio.h>
-#include <string.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <time.h>
-#include <windows.h>
-#include <winsock2.h>
-#include <ws2ipdef.h>
-#include <ws2tcpip.h>
-#include <algorithm>
-
-#define __ORDER_LITTLE_ENDIAN__ 0x41424344UL
-#define __ORDER_BIG_ENDIAN__ 0x44434241UL
-#define __BYTE_ORDER__ ('ABCD')
-#undef DELETE
-
-#define HAVE_STRUCT_TIMESPEC
+/* + * Copyright (c) 2021 Cisco and/or its affiliates. + * Copyright 2017 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once +#define WIN32_LEAN_AND_MEAN +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include <fcntl.h> +#include <io.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <time.h> +#include <windows.h> +#include <winsock2.h> +#include <ws2ipdef.h> +#include <ws2tcpip.h> + +#include <algorithm> + +#define __ORDER_LITTLE_ENDIAN__ 0x41424344UL +#define __ORDER_BIG_ENDIAN__ 0x44434241UL +#define __BYTE_ORDER__ ('ABCD') +#undef DELETE + +#define HAVE_STRUCT_TIMESPEC #include <pthread.h>
\ No newline at end of file |