From 5dba52a199b28621197942565c0163712bba43a1 Mon Sep 17 00:00:00 2001 From: Julius Goryavsky Date: Fri, 7 Jun 2019 16:12:28 +0200 Subject: [PATCH] Removing of cross-platform code and dependencies from external includes (common.h, rotate.h, netconv.h) --- tempesta_fw/http2/bits.h | 48 +- tempesta_fw/http2/buffers.c | 9 +- tempesta_fw/http2/buffers.h | 5 +- tempesta_fw/http2/common.h | 614 ------------------ tempesta_fw/http2/errors.h | 2 +- tempesta_fw/http2/hash.c | 158 +---- tempesta_fw/http2/hash.h | 3 +- tempesta_fw/http2/hgen/Makefile | 2 +- tempesta_fw/http2/hgen/hgen.c | 2 +- tempesta_fw/http2/hindex.c | 12 +- tempesta_fw/http2/hindex.h | 3 +- tempesta_fw/http2/hpack.h | 3 +- tempesta_fw/http2/hpack_decoder.c | 36 +- tempesta_fw/http2/hpack_encoder.c | 11 +- tempesta_fw/http2/hpack_helpers.h | 93 +-- tempesta_fw/http2/hstatic.h | 3 +- tempesta_fw/http2/http2.h | 3 +- tempesta_fw/http2/http2_api.h | 2 + tempesta_fw/http2/huffman.c | 444 +++---------- tempesta_fw/http2/huffman.h | 3 +- tempesta_fw/http2/netconv.h | 406 ------------ tempesta_fw/http2/rotate.h | 262 -------- tempesta_fw/http2/subs.c | 16 +- tempesta_fw/http2/subs.h | 21 +- tempesta_fw/http2/t/Makefile | 2 +- tempesta_fw/http2/t/compare_test.c | 1 - tempesta_fw/http2/t/fragments_test.c | 2 +- tempesta_fw/http2/t/hgentest.c | 2 +- tempesta_fw/http2/t/hpack_test.c | 2 +- tempesta_fw/http2/t/huffman_test.c | 2 +- tempesta_fw/http2/t/ngx_http_v2_huff_decode.c | 10 +- 31 files changed, 235 insertions(+), 1947 deletions(-) delete mode 100644 tempesta_fw/http2/common.h delete mode 100644 tempesta_fw/http2/netconv.h delete mode 100644 tempesta_fw/http2/rotate.h diff --git a/tempesta_fw/http2/bits.h b/tempesta_fw/http2/bits.h index 78b183750..5c26917bc 100644 --- a/tempesta_fw/http2/bits.h +++ b/tempesta_fw/http2/bits.h @@ -28,15 +28,13 @@ #ifndef BITS_H #define BITS_H -#include "common.h" +#include #define Bit_Sub - #define Bit_High(x, n) (x) >= (1U << n) #ifdef __GNUC__ -#if Platform_64bit - #define Bit_CRC(crc, x) \ (__extension__ ({ \ register uintptr_t __r = crc; \ @@ -57,56 +55,19 @@ #else -#define Bit_CRC(crc, x) \ - (__extension__ ({ \ - register uintptr_t __r = crc; \ - __asm__( \ - "crc32 %2,%0" : "=r" (__r) : "0" (__r), "rm" ((uintptr_t) x) : "cc" \ - ); \ - __r; \ - })) - -#define Bit_FastLog2(x) \ - (__extension__ ({ \ - register uintptr_t __r; \ - __asm__( \ - "bsr %1,%0" : "=r" (__r) : "rm" ((uintptr_t) x) : "cc" \ - ); \ - __r; \ - })) - -#endif - -#else - -common_inline unsigned int +static __inline__ unsigned int Bit_FastLog2(uintptr_t value) { unsigned int x = (unsigned int)value; unsigned int n = 0; + unsigned int c; -#ifdef Platform_64bit if (Bit_High(value, 32)) { x = (unsigned int)(value >> 32); n = 32; } if (Bit_High(x, 16)) x = x >> 16, n += 16; -#else - if (Bit_High(x, 16)) - x = x >> 16, n = 16; -#endif -#ifndef Branch_Free - if (x >= 256) - x = x >> 8, n += 8; - if (x >= 16) - x = x >> 4, n += 4; - if (x >= 4) - x = x >> 2, n += 2; - n = n + (x >> 1); -#else - unsigned int c; - x = (x | 1) >> n; n = n + 15; c = ((x - 0x0100) >> 16) & 8; @@ -116,13 +77,12 @@ Bit_FastLog2(uintptr_t value) n = n - c; x = x >> (12 Bit_Sub c); n = n - ((x + 2) >> (x >> 1)); -#endif return n; } #endif -common_inline uintptr_t +static __inline__ uintptr_t Bit_UpPowerOfTwo(uintptr_t x) { if (likely(x > 2)) { diff --git a/tempesta_fw/http2/buffers.c b/tempesta_fw/http2/buffers.c index f40abe312..a46b28f2d 100644 --- a/tempesta_fw/http2/buffers.c +++ b/tempesta_fw/http2/buffers.c @@ -24,7 +24,6 @@ #include #include #include -#include "common.h" #include "../pool.h" #include "../str.h" #include "hash.h" @@ -422,7 +421,7 @@ void buffer_new(HTTP2Output * __restrict p, TfwPool * __restrict pool, unsigned int alignment) { - unsigned int align = Word_Size - 1; + unsigned int align = sizeof(uintptr_t) - 1; if (alignment) { align = ~alignment & (alignment - 1); @@ -1202,11 +1201,7 @@ buffer_str_cmp(const TfwStr * __restrict x, const TfwStr * __restrict y) Final: delta = (cx >> 1) - (cy >> 1); if (delta) { -#if Fast_Capacity < Bit_Capacity - return (delta >> (Bit_Capacity - Fast_Capacity)) | 1; -#else - return delta; -#endif + return (delta >> 32) | 1; } return (int)(cx - cy); } diff --git a/tempesta_fw/http2/buffers.h b/tempesta_fw/http2/buffers.h index 9ff62b212..a959c9cb8 100644 --- a/tempesta_fw/http2/buffers.h +++ b/tempesta_fw/http2/buffers.h @@ -23,7 +23,8 @@ #ifndef HTTP2_BUFFERS_H #define HTTP2_BUFFERS_H -#include "common.h" +#include + #include "../pool.h" #include "../str.h" #include "errors.h" @@ -322,7 +323,7 @@ void buffer_str_print(const TfwStr * __restrict str); /* Free memory occupied by the TfwStr descriptors, */ /* which may be allocated for compound strings: */ -common_inline void +static __inline__ void buffer_str_free(TfwPool * __restrict pool, TfwStr * __restrict str) { if (!TFW_STR_PLAIN(str)) { diff --git a/tempesta_fw/http2/common.h b/tempesta_fw/http2/common.h deleted file mode 100644 index a1aa549e2..000000000 --- a/tempesta_fw/http2/common.h +++ /dev/null @@ -1,614 +0,0 @@ -/** - * Tempesta FW - * - * Conversion between little and big endian numbers. - * - * Copyright (C) 2017 Tempesta Technologies, Inc. - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, - * or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with - * this program; if not, write to the Free Software Foundation, Inc., 59 - * Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * Copyright (C) Julius Goryavsky. Original code of this module - * is granted by the author for unrestricted use in the Tempesta FW - * and for distribution under GNU General Public License without - * any restrictions. - */ - -#ifndef COMMON_H -#define COMMON_H - -/* - * Platform_x86: must be specified to enable x86-specific optimizations. - */ -#define Platform_x86 - -/* - * Platform_32bit: 32-bit platform. - * Platform_64bit: 64-bit platform. - */ -#define Platform_64bit - -/* - * Platform_Little: little-endian platform. - * Platform_Big: big-endian platform. - */ -#define Platform_Little - -/* - * Platform_Alignment: plafrom requires aligned access to the memory. - */ -/* #define Platform_Alignment */ - -/* - * Hardware_Predicates: hardware support for boolean predicates. - */ -#define Hardware_Predicates - -/* - * Hardware_Conditional: hardware support for conditional move. - */ -#define Hardware_Conditional - -/* - * Hardware_Carry: hardware support carry flag. - */ -#define Hardware_Carry - -/* - * Hardware_Carry: hardware rotate. - */ -#define Hardware_Rotate - -/* - * Hardware_ShiftAdd: mask of available shifts in the hardware - * shift-add command (LEA on x86): - */ -#define Hardware_ShiftAdd 15 - -/* - * Hardware_Swap: hardware swap registers. - */ -#define Hardware_Swap - -/* - * Hardware_Remainder: hardware remainder of integer division. - */ -#define Hardware_Remainder - -/* - * Hardware_MultiplyLong: hardware multiply long. - */ -#define Hardware_MultiplyLong - -/* - * Hardware_Log2: hardware integer logarithm. - */ -#define Hardware_Log2 - -/* - * Hardware_TrailZero: hardware counting of trailing zeros. - */ -#define Hardware_TrailZero - -/* - * Hardware_LeadZero: hardware counting of leading zeros. - */ -#define Hardware_LeadZero - -/* - * Hardware_Population: hardware population count. - */ -#define Hardware_Population - -/* - * Hardware_CRC32: hardware CRC-32 calculation. - */ -#define Hardware_CRC32 - -/* - * Hardware_AES: hardware AES rounds. - */ -#define Hardware_AES - -/* - * Hardware_AndNot: hardware and-not. - */ -#define Hardware_AndNot - -/* - * Hardware_BarrelShifter: processor have full-functional - * barrel shifter available on instruction level. - */ -/* #define Hardware_BarrelShifter */ - -#ifndef Hardware_ShiftAdd - #ifdef Hardware_BarrelShifter - #ifdef Platform_32bit - #define Hardware_ShiftAdd 0xFFFFFFFE - #else - #define Hardware_ShiftAdd 0xFFFFFFFFFFFFFFFE - #endif - #else - #define Hardware_ShiftAdd 0 - #endif -#endif - -#define High_Bit32 0x80000000 - -#ifdef Platform_32bit - #define Bit_Capacity 32 - #define Bit_Capacity_Log 5 - #define Word_Size 4 - #define High_Bit 0x80000000 -#else - #define Bit_Capacity 64 - #define Bit_Capacity_Log 6 - #define Word_Size 8 - #define High_Bit 0x8000000000000000 -#endif - -/* - * Maximal available length in the shift instructions: - */ -#define Shift_Length (Bit_Capacity - 1) - -/* - * Integer logarithm-related definitions, - * which may be derived from main specification: - */ -#ifdef Hardware_LeadZero - #define Hardware_FastLog2 -#elif defined(Hardware_Log2) - #define Hardware_FastLog2 - #ifndef Hardware_LeadOnes - #define Hardware_Log2Only - #endif -#elif defined(Hardware_LeadOnes) - #define Hardware_FastLog2 - #define Hardware_LeadOnesOnly -#endif - -#if defined(Fast_Log2) || \ - defined(Hardware_Population) || \ - defined(Hardware_LeadZeroBytes) - #define Hardware_QuickLog2 -#endif - -/* - * Macros to use LEA on the x86, barrel shifter and other - * hardware-specific instructions istead of "|" if suitable: - */ - -#if Hardware_ShiftAdd & 1 - #define Bit_Add(x, y) ((x) + (y)) -#else - #define Bit_Add(x, y) ((x) | (y)) -#endif - -#if Hardware_ShiftAdd & 2 - #define Bit_Join1(x, y) (((x) << 1) + (y)) -#else - #define Bit_Join1(x, y) (((x) << 1) | (y)) -#endif - -#if Hardware_ShiftAdd & 4 - #define Bit_Join2(x, y) (((x) << 2) + (y)) -#else - #define Bit_Join2(x, y) (((x) << 2) | (y)) -#endif - -#if Hardware_ShiftAdd & 8 - #define Bit_Join3(x, y) (((x) << 3) + (y)) -#else - #define Bit_Join3(x, y) (((x) << 3) | (y)) -#endif - -#if Hardware_ShiftAdd & 16 - #define Bit_Join4(x, y) (((x) << 4) + (y)) -#else - #define Bit_Join4(x, y) (((x) << 4) | (y)) -#endif - -#if Hardware_ShiftAdd & 256 - #define Bit_Join8(x, y) (((x) << 8) + (y)) -#else - #define Bit_Join8(x, y) (((x) << 8) | (y)) -#endif - -#if Hardware_ShiftAdd & ~0x11E - #define Bit_Shift(x, y, z) (((x) << (y)) + (z)) -#else - #define Bit_Shift(x, y, z) (((x) << (y)) | (z)) -#endif - -#define Bit_Join Bit_Shift - -/* - * Optimal values for current hardware: - */ -#define Multiply_Throughput 1 -#define Multiply_Latency64 4 -#define Divide_Latency64 95 -#define Divide_Latency32 22 - -#ifdef Platform_32bit - #define Multiply_Latency Multiply_Latency32 - #define Divide_Latency Divide_Latency32 -#else - #define Multiply_Latency Multiply_Latency64 - #ifndef Multiply_Latency32 - #define Multiply_Latency32 Multiply_Latency64 - #endif - #define Divide_Latency Divide_Latency64 - #ifndef Divide_Latency32 - #define Divide_Latency32 Divide_Latency64 - #endif -#endif - -#ifndef Multiply_Latency16 - #define Multiply_Latency16 Multiply_Latency32 -#endif - -#ifndef Multiply_Latency8 - #define Multiply_Latency8 Multiply_Latency16 -#endif - -#ifndef Divide_Latency16 - #define Divide_Latency16 Divide_Latency32 -#endif - -#ifndef Divide_Latency8 - #define Divide_Latency8 Divide_Latency16 -#endif - -#ifndef Multiply_Throughput - #define Multiply_Throughput Multiply_Latency -#endif - -#ifndef Multiply_Add_Latency - #ifdef Hardware_MultiplyAdd - #define Multiply_Add_Latency Multiply_Latency - #else - #define Multiply_Add_Latency (Multiply_Latency + 1) - #endif -#endif - -#ifndef Multiply_Chain_Latency - #define Multiply_Chain_Latency Multiply_Add_Latency -#endif - -#ifndef Multiply_High_Latency - #ifdef Hardware_MultiplyHigh - #define Multiply_High_Latency Multiply_Latency - #elif defined(Hardware_MultiplyLong) - #define Multiply_High_Latency Multiply_Long_Latency - #endif -#endif - -#ifndef Multiply_Long_Latency - #ifdef Hardware_MultiplyLong - #define Multiply_Long_Latency Multiply_Latency - #elif defined(Hardware_MultiplyHigh) - #define Multiply_Long_Latency \ - (Multiply_Throughput + Multiply_High_Latency) - #endif -#endif - -/* - * Some compiler-specific settings: - */ -#ifdef __GNUC__ - #ifndef __cplusplus - #define common_inline static __inline__ - #endif - #if defined(__EMX__) && ! defined(__STRICT_ANSI__) - #define Compiler_Rotate - #endif - #ifndef __CHAR_UNSIGNED__ - #define Compiler_SignedChar - #endif - #define Internal_int64 - typedef long long int64; - typedef unsigned long long uint64; - #define Attribute_Const __attribute__((const)) - #define Attribute_NoReturn __attribute__((noreturn)) - #if __GNUC__ >= 3 - #define Attribute_Allocate __attribute__((malloc)) - #define Attribute_Pure __attribute__((pure)) - #define Attribute_Clean __attribute__((pure)) - #define Attribute_Printf(x, y) __attribute__((format(printf, x, y))) - #define Attribute_Align(x) __attribute__((aligned(x))) - #endif - #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4) - #define Compiler_GCC44 - #endif - #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) - #define Compiler_GCC43 - #endif - #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) - #define Compiler_GCC34 - #endif - #if __GNUC__ >= 4 - #define Compiler_Restrict - #define likely(x) __builtin_expect(x, 1) - #define unlikely(x) __builtin_expect(x, 0) - #endif -#elif defined(_MSC_VER) - #pragma once - #pragma warning(disable: 4711 4464) - #pragma warning(disable: 4055 4100 4127 4242 4244 4514 4710 4820) - #ifndef _CRT_SECURE_NO_DEPRECATE - #define _CRT_SECURE_NO_DEPRECATE - #endif - #ifdef Platform_Emulator - #pragma warning(disable: 4244) - #endif - #define common_cdecl __cdecl - #ifndef _POSIX_ - #define Compiler_Rotate - #define Compiler_Rotate64 - #endif - #ifndef __cplusplus - #define common_inline static __inline - #endif - #ifndef __CHAR_UNSIGNED__ - #define Compiler_SignedChar - #endif - #define Internal_int64 - typedef __int64 int64; - typedef unsigned __int64 uint64; - #if _MSC_VER >= 1400 - #if _INTEGRAL_MAX_BITS >= 128 - #define Internal_int128 - typedef __int128 int128; - typedef unsigned __int128 uint128; - #endif - #define Attribute_Allocate __declspec(restrict) - #define Attribute_NoAlias __declspec(noalias) - #define Attribute_Clean __declspec(noalias) - #define Compiler_Restrict - #endif - #define Attribute_Align(x) __declspec(align(x)) - #define Attribute_NoReturn __declspec(noreturn) -#endif - -#ifndef common_cdecl -#define common_cdecl -#endif - -#ifndef NULL -#ifdef __cplusplus -#define NULL 0 -#else -#define NULL (void *) 0 -#endif -#endif - -#ifdef __cplusplus - #define common_inline inline -#endif - -#define local_function common_inline - -#ifndef Compiler_Restrict - #define __restrict -#endif - -#ifdef _MSC_VER - #define __restrict_fixed -#else - #define __restrict_fixed __restrict -#endif - -#if ! defined(__INT_MAX__) || \ - (__INT_MAX__ == 0x7FFFFFFF) - #define Platform_Int32bit -#endif - -#ifdef Platform_64bit - #if ! defined(__LONG_MAX__) || \ - (__LONG_MAX__ != 0x7FFFFFFF) - #define Platform_Long64bit - #endif -#endif - -#ifdef Platform_64bit - #ifndef Internal_int64 - #define Internal_int64 - typedef long int64; - typedef unsigned long uint64; - #endif -#endif - -#ifdef Platform_Int32bit - typedef int int32; - typedef unsigned int uint32; -#else - typedef __int32 int32; - typedef unsigned __int32 uint32; -#endif - -typedef short int16; -typedef signed char int8; -typedef unsigned short uint16; -typedef unsigned char uint8; - -#ifdef Platform_32bit - typedef int wide; - typedef unsigned int uwide; -#else - typedef int64 wide; - typedef uint64 uwide; -#endif - -#ifdef Platform_x86 - typedef int fast; - typedef unsigned int ufast; - #define Fast_Capacity 32 -#else - typedef wide fast; - typedef uwide ufast; - #define Fast_Capacity Bit_Capacity -#endif - -#if defined(_BSD_SOURCE) || defined(_SVID_SOURCE) - #define Platform_SysTypes -#endif - -#ifdef Platform_SysTypes - #ifndef _SYS_TYPES_H - #include - #endif - typedef unsigned char uchar; -#else - typedef unsigned char uchar; - typedef unsigned short ushort; - typedef unsigned int uint; - typedef unsigned long ulong; -#endif - -#ifndef __RPCNDR_H__ - typedef unsigned char byte; -#endif - -#ifndef __cplusplus - #ifndef bool - typedef unsigned char bool; - #endif - #define true 1 - #define false 0 -#endif - -/* - * Common definitions for compiler-specific optimization attributes: - */ - -#ifndef Attribute_Allocate - #define Attribute_Allocate -#endif - -#ifndef Attribute_NoAlias - #define Attribute_NoAlias -#endif - -#ifndef Attribute_Pure - #define Attribute_Pure -#endif - -#ifndef Attribute_Clean - #define Attribute_Clean -#endif - -#ifndef Attribute_OutOnly - #define Attribute_OutOnly -#endif - -#ifndef Attribute_Const - #define Attribute_Const Attribute_Clean -#endif - -#ifndef Attribute_Printf - #define Attribute_Printf(x, y) -#endif - -#ifdef Attribute_Align - #define Compiler_Align -#else - #define Attribute_Align(x) -#endif - -#ifndef Attribute_NoReturn - #define Attribute_NoReturn -#endif - -#ifndef likely - #define likely(x) (x) -#endif - -#ifndef unlikely - #define unlikely(x) (x) -#endif - -/* - * Remove aliasing effects from the pointer, because ANSI-compliant - * compiler should assume that any pointer to a union, which containing - * "char", potentially overlap with any other pointers in the program: - */ -typedef union { - char c; - void *p; -} AntiAliasType; - -#define AntiAliasLink(x) ((AntiAliasType *) (x))->p - -/* Double-wide integer types: */ - -#ifdef Platform_32bit - #ifdef Internal_int64 - #define Internal_dwide - typedef int64 dwide; - typedef uint64 udwide; - #endif -#else - #ifdef Internal_int128 - #define Internal_dwide - typedef int128 dwide; - typedef uint128 udwide; - #endif -#endif - -#ifdef Internal_int64 - #define MakeConst64(x, y) (((uint64) (x) << 32) | (y)) -#else - #define MakeConst64 MakeConstDWide -#endif - -#ifdef Internal_dwide - #define MakeConstDWide(x, y) (((udwide) (x) << Bit_Capacity) | (y)) -#else -/* - * Software implementation of double-wide integes. - * For example, GCC does not support 128-bit integers: - */ - #ifdef Platform_Big - typedef struct { - wide High; - uwide Low; - } dwide; - typedef struct { - uwide High; - uwide Low; - } udwide; - #define MakeConstDWide(x, y) {x, y} - #else - typedef struct { - uwide Low; - wide High; - } dwide; - typedef struct { - uwide Low; - uwide High; - } udwide; - #define MakeConstDWide(x, y) {y, x} - #endif - #ifdef Platform_32bit - typedef dwide int64; - typedef udwide uint64; - #else - typedef dwide int128; - typedef udwide uint128; - #endif -#endif - -#endif diff --git a/tempesta_fw/http2/errors.h b/tempesta_fw/http2/errors.h index d71045375..9a3b63261 100644 --- a/tempesta_fw/http2/errors.h +++ b/tempesta_fw/http2/errors.h @@ -1,7 +1,7 @@ /** * Tempesta FW * - * Conversion between little and big endian numbers. + * HTTP/2 error codes * * Copyright (C) 2017 Tempesta Technologies, Inc. * diff --git a/tempesta_fw/http2/hash.c b/tempesta_fw/http2/hash.c index 00e2e8941..726852693 100644 --- a/tempesta_fw/http2/hash.c +++ b/tempesta_fw/http2/hash.c @@ -27,13 +27,24 @@ #include #include -#include "common.h" +#include #include "../pool.h" #include "bits.h" #include "subs.h" -#include "rotate.h" #include "hash.h" +static __inline__ uintptr_t +Rotate_Left(const uintptr_t x, const unsigned int Shift) +{ + if (Shift) { + return (x << Shift) | (x >> (64 - Shift)); + } else { + return x; + } +} + +#define Bit_Shift(x, y, z) (((x) << (y)) | (z)) + typedef struct Hash_Entry { struct Hash_Entry *Next; const void *Name; @@ -85,7 +96,7 @@ static void Hash_Compress(Hash * __restrict const ht, const uintptr_t c); /* Some helper routines: */ /* ------------------------------------------- */ -local_function uintptr_t +static __inline__ uintptr_t HF(const uintptr_t k) { #ifdef __GNUC__ @@ -604,76 +615,20 @@ Byte_Hash_Chain(const void *__restrict const x, const uintptr_t Length, l = sizeof(uintptr_t) - l; if (n >= l) { n = n - l; -#ifdef Platform_Big - if (l & 1) { - h = *t++; - } -#ifdef Platform_32bit - if (l >= 2) { -#else - if (l & 2) { -#endif - h = Bit_Shift(h, 16, *(uint16_t *) t); - t = t + 2; - } -#ifdef Platform_64bit - if (l >= 4) { - h = Bit_Shift(h, 32, *(uint32_t *) t); - t = t + 4; - } -#endif -#else -#ifdef Platform_32bit - if (l & 1) { - h = *t++ << 24; - } - if (l >= 2) { -#if defined(Compiler_Rotate) && ! defined(_MSC_VER) - h = Rotate_Left(h | *(uint16_t *) t, 16); -#else - h = Bit_Shift(*(uint16_t *) t, 16, h >> 16); -#endif - t = t + 2; - } -#else if (l & 1) { h = (uintptr_t) * t++ << 56; } if (l & 2) { -#if defined(Compiler_Rotate64) - h = Rotate_Left(h | *(uint16_t *) t, 48); -#else h = Bit_Shift((uintptr_t) * (uint16_t *) t, 48, h >> 16); -#endif t = t + 2; } if (l >= 4) { -#if defined(Compiler_Rotate64) - h = Rotate_Left(h | *(uint32_t *) t, 32); -#else h = Bit_Shift((uintptr_t) * (uint32_t *) t, 32, h >> 32); -#endif t = t + 4; } -#endif -#endif } else { -#ifdef Platform_32bit -#ifdef Platform_Little - h = *t++; - if (n == 2) { - h = Bit_Join8(*t, h); - } -#else - h = *t++ << 24; - if (n == 2) { - h = Bit_Shift(*t, 16, h); - } -#endif -#else -#ifdef Platform_Little unsigned int s = (l & 1) << 3; if (s) { @@ -703,48 +658,11 @@ Byte_Hash_Chain(const void *__restrict const x, const uintptr_t Length, } } h = Bit_Shift((uintptr_t) * t, s, h); -#else - unsigned int s = 64; - - if (l & 1) { - h = (uintptr_t) * t << 56; - if (--n == 0) { - goto L0; - } - t = t + 1; - s = 56; - } - if (n >= 2) { - s = s - 16; - h = Bit_Shift((uintptr_t) * (uint16_t *) t, s, - h); - n = n - 2; - if (n == 0) { - goto L0; - } - t = t + 2; - if (n >= 2) { - s = s - 16; - h = Bit_Shift((uintptr_t) * - (uint16_t *) t, s, h); - if (n == 2) { - goto L0; - } - t = t + 2; - } - } - h = Bit_Shift((uintptr_t) * t, s - 8, h); -#endif L0: -#endif -#ifdef Platform_Little - return Rotate_Left(h, (Shift & (Word_Size - 1)) * 8); -#else - return Rotate_Right(h, (Shift & (Word_Size - 1)) * 8); -#endif + return Rotate_Left(h, (Shift & (sizeof(uintptr_t) - 1)) * 8); } } - l = (l + Shift) & (Word_Size - 1); + l = (l + Shift) & (sizeof(uintptr_t) - 1); m = n / sizeof(uintptr_t); if (m) { do { @@ -755,8 +673,6 @@ Byte_Hash_Chain(const void *__restrict const x, const uintptr_t Length, if (n & (sizeof(uintptr_t) - 1)) { unsigned int s; -#ifdef Platform_Little -#ifdef Platform_64bit s = (n & 4) << 3; if (s) { h = h ^ *(uint32_t *) t; @@ -770,48 +686,6 @@ Byte_Hash_Chain(const void *__restrict const x, const uintptr_t Length, if (n & 1) { h = h ^ ((uintptr_t) * t << s); } -#else - s = n & 2; - if (s) { - h = h ^ *(uint16_t *) t; - t = t + 2; - } - if (n & 1) { - h = h ^ (*t << (s << 3)); - } -#endif -#else -#ifdef Platform_64bit - s = 64; - if (n & 4) { - h = h ^ ((uintptr_t) * (uint32_t *) t << 32); - t = t + 4; - s = 32; - } - if (n & 2) { - s = s - 16; - h = h ^ ((uintptr_t) * (uint16_t *) t << s); - t = t + 2; - } - if (n & 1) { - h = h ^ ((uintptr_t) * t << (s - 8)); - } -#else - s = 24; - if (n & 2) { - h = h ^ (*(uint16_t *) t << 16); - t = t + 2; - s = 8; - } - if (n & 1) { - h = h ^ (*t << s); - } -#endif -#endif } -#ifdef Platform_Little return Rotate_Left(h, l * 8); -#else - return Rotate_Right(h, l * 8); -#endif } diff --git a/tempesta_fw/http2/hash.h b/tempesta_fw/http2/hash.h index 9a61040bf..38176bc9f 100644 --- a/tempesta_fw/http2/hash.h +++ b/tempesta_fw/http2/hash.h @@ -28,7 +28,8 @@ #ifndef HASH_H #define HASH_H -#include "common.h" +#include + #include "../pool.h" typedef struct Hash Hash; diff --git a/tempesta_fw/http2/hgen/Makefile b/tempesta_fw/http2/hgen/Makefile index 3a6fb817f..29256ed14 100644 --- a/tempesta_fw/http2/hgen/Makefile +++ b/tempesta_fw/http2/hgen/Makefile @@ -1,6 +1,6 @@ options = -Wall -Wextra -Wstrict-overflow=5 -fno-strict-overflow -O3 -hfiles = ../common.h hfcode.h +hfiles = hfcode.h all: hfstate.h diff --git a/tempesta_fw/http2/hgen/hgen.c b/tempesta_fw/http2/hgen/hgen.c index aafc15636..50229a124 100644 --- a/tempesta_fw/http2/hgen/hgen.c +++ b/tempesta_fw/http2/hgen/hgen.c @@ -22,7 +22,7 @@ #include #include -#include "../common.h" +#include #include "hfcode.h" diff --git a/tempesta_fw/http2/hindex.c b/tempesta_fw/http2/hindex.c index 38c9ab53d..5c095f373 100644 --- a/tempesta_fw/http2/hindex.c +++ b/tempesta_fw/http2/hindex.c @@ -31,7 +31,7 @@ #include #include #include -#include "common.h" + #include "../pool.h" #include "../str.h" #include "bits.h" @@ -152,7 +152,7 @@ hpack_hash_add(Hash * const __restrict ht, return prev; } else if (p->arena == HPack_Arena_User) { const unsigned int length = p->len; - uchar *__restrict data = hpack_mem_allocate(length); + unsigned char *__restrict data = hpack_mem_allocate(length); if (data) { const TfwStr *__restrict source = p->ptr; @@ -404,7 +404,7 @@ hpack_copy_data(HTTP2Index * __restrict ip, if (name->arena == HPack_Arena_User) { const uintptr_t length = name->len; - uchar *__restrict data = hpack_mem_allocate(length); + unsigned char *__restrict data = hpack_mem_allocate(length); if (data) { HPackStr *__restrict np = Sub_Allocate(ip->sub); @@ -428,7 +428,7 @@ hpack_copy_data(HTTP2Index * __restrict ip, } if (value && value->arena == HPack_Arena_User) { const uintptr_t length = value->len; - uchar *__restrict data = hpack_mem_allocate(length); + unsigned char *__restrict data = hpack_mem_allocate(length); if (data) { HPackStr *__restrict vp = Sub_Allocate(ip->sub); @@ -582,11 +582,11 @@ hpack_add_and_prune(HTTP2Index * __restrict ip, block = (block >> 1) - wrap; if (block) { - memcpy(entries, (uchar *) previous + wrap, + memcpy(entries, (unsigned char *) previous + wrap, block); } if (wrap) { - memcpy((uchar *) entries + block, previous, + memcpy((unsigned char *) entries + block, previous, wrap); } tfw_pool_free(pool, previous, block); diff --git a/tempesta_fw/http2/hindex.h b/tempesta_fw/http2/hindex.h index e7d41dd06..6c9189c95 100644 --- a/tempesta_fw/http2/hindex.h +++ b/tempesta_fw/http2/hindex.h @@ -24,7 +24,8 @@ #ifndef HPACK_INDEX_H #define HPACK_INDEX_H -#include "common.h" +#include + #include "../pool.h" #include "../str.h" #include "subs.h" diff --git a/tempesta_fw/http2/hpack.h b/tempesta_fw/http2/hpack.h index b7ed38b5d..c5947fcc6 100644 --- a/tempesta_fw/http2/hpack.h +++ b/tempesta_fw/http2/hpack.h @@ -23,7 +23,8 @@ #ifndef HPACK_H #define HPACK_H -#include "common.h" +#include + #include "../pool.h" #include "../str.h" #include "../http.h" diff --git a/tempesta_fw/http2/hpack_decoder.c b/tempesta_fw/http2/hpack_decoder.c index 71b94fac6..d0b46dc85 100644 --- a/tempesta_fw/http2/hpack_decoder.c +++ b/tempesta_fw/http2/hpack_decoder.c @@ -24,7 +24,6 @@ #include #include #include -#include "common.h" #include "../pool.h" #include "../str.h" #include "../http.h" @@ -74,7 +73,7 @@ hpack_decode(HPack * __restrict hp, *rc = 0; if (n) { uintptr_t m; - const uchar *__restrict src = buffer_get(source, &m); + const unsigned char *__restrict src = buffer_get(source, &m); unsigned int state = hp->state; do { @@ -144,7 +143,7 @@ hpack_decode(HPack * __restrict hp, default: /* case HPack_State_Ready: */ { - uchar c; + unsigned char c; field = tfw_pool_alloc(pool, @@ -240,7 +239,7 @@ hpack_decode(HPack * __restrict hp, } case HPack_State_Name: { - uchar c; + unsigned char c; DPUTS("Decode header name length..."); if (unlikely(m == 0)) { @@ -325,7 +324,7 @@ hpack_decode(HPack * __restrict hp, } case HPack_State_Value: { - uchar c; + unsigned char c; Get_Value: DPUTS("Decode header value length..."); @@ -361,7 +360,7 @@ hpack_decode(HPack * __restrict hp, } case HPack_State_Value_Text: { - uchar *dst; + unsigned char *dst; unsigned int hrc; length = field->value.len; @@ -428,27 +427,12 @@ hpack_decode(HPack * __restrict hp, DPRINTF ("Add header by dictionary index: %" PRIuPTR "\n", index); -#if Fast_Capacity > 32 - if ((index >> 32) == 0) { - hrc = - hpack_add_index(hp-> - dynamic, - field, - index, - state, - buffer); - } else { - hrc = - Err_HTTP2_IntegerOveflow; - } -#else hrc = hpack_add_index(hp->dynamic, field, index, state, buffer); -#endif } else { DPUTS ("Add header with name and value..."); @@ -558,11 +542,6 @@ hpack_free_list(HTTP2Output * __restrict buffer, HTTP2Field * __restrict p) unsigned int hpack_set_max_window(HPack * __restrict hp, unsigned int max_window) { -#if Fast_Capacity > 32 - if (unlikely(max_window > (uint32_t) - 1)) { - return Err_HPack_InvalidTableSize; - } -#endif if (max_window == 0) { max_window = (uint32_t) - 1; } @@ -592,11 +571,6 @@ hpack_new(unsigned int max_window, unsigned char is_encoder, { HTTP2Index *__restrict dynamic; -#if Fast_Capacity > 32 - if (unlikely(max_window > (uint32_t) - 1)) { - return NULL; - } -#endif if (max_window == 0) { max_window = (uint32_t) - 1; } diff --git a/tempesta_fw/http2/hpack_encoder.c b/tempesta_fw/http2/hpack_encoder.c index 7af870efe..1541724ee 100644 --- a/tempesta_fw/http2/hpack_encoder.c +++ b/tempesta_fw/http2/hpack_encoder.c @@ -24,7 +24,6 @@ #include #include #include -#include "common.h" #include "../pool.h" #include "../str.h" #include "../http.h" @@ -46,9 +45,9 @@ #define DPUTS(...) #endif -static uchar * +static unsigned char * write_index(HTTP2Output * __restrict out, - uchar * __restrict dst, + unsigned char * __restrict dst, unsigned int *__restrict k_new, unsigned int index, unsigned int max, unsigned int mask, unsigned int *__restrict rc) @@ -80,9 +79,9 @@ write_index(HTTP2Output * __restrict out, return NULL; } -static uchar * +static unsigned char * write_string(HTTP2Output * __restrict out, - uchar * __restrict dst, + unsigned char * __restrict dst, unsigned int *__restrict k_new, const HPackStr * __restrict x, unsigned int *__restrict rc) { @@ -149,7 +148,7 @@ hpack_encode(HPack * __restrict hp, HTTP2Index *const __restrict ip = hp->dynamic; unsigned int rc; unsigned int k; - uchar *__restrict dst = buffer_open(out, &k, 0); + unsigned char *__restrict dst = buffer_open(out, &k, 0); do { uintptr_t value_len; diff --git a/tempesta_fw/http2/hpack_helpers.h b/tempesta_fw/http2/hpack_helpers.h index 3919f8d52..e1030d849 100644 --- a/tempesta_fw/http2/hpack_helpers.h +++ b/tempesta_fw/http2/hpack_helpers.h @@ -23,29 +23,66 @@ #ifndef HPACK_HELPERS_H #define HPACK_HELPERS_H -#include "common.h" -#include "netconv.h" +#include +#include +#include -#ifdef Platform_Alignment +/* + * Macros to use LEA on the x86, barrel shifter and other + * hardware-specific instructions istead of "|" if suitable: + */ +#define Bit_Add(x, y) ((x) | (y)) +#define Bit_Join1(x, y) (((x) << 1) + (y)) +#define Bit_Join2(x, y) (((x) << 2) + (y)) +#define Bit_Join3(x, y) (((x) << 3) + (y)) +#define Bit_Join4(x, y) (((x) << 4) | (y)) +#define Bit_Join8(x, y) (((x) << 8) | (y)) +#define Bit_Shift(x, y, z) (((x) << (y)) | (z)) +#define Bit_Join Bit_Shift -#define GET_INT32_FLAT(x) \ - x = Bit_Join(src[0], 24, \ - Bit_Join(src[1], 16, \ - Bit_Join8(src[2], src[3]))) +#define Big16 htons +#define Big32 htonl -#define GET_INT16_FLAT(x) \ - x = Bit_Join8(src[0], src[1]) +#ifdef __GNUC__ + +#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) + +#define Big64(x) (uint64_t) __builtin_bswap64(x) #else +#define Big64(x) \ + (__extension__ ({ \ + register uint64_t __r = x; \ + __asm__( \ + "bswapq %0" : "=r" (__r) : "0" (__r) \ + ); \ + __r; \ + })) + +#endif + +#else + +static __inline__ uint64_t +Big64 (const uint64_t x) +{ + uint64_t y; + y = Bit_Join8(x & 0x00FF00FF00FF00FF, (x >> 8) & 0x00FF00FF00FF00FF); + y = Bit_Shift(y & 0x0000FFFF0000FFFF, 16, (y >> 16) & 0x0000FFFF0000FFFF); + return return (y << 32) | (y >> (64 - 32)); +} + +#endif + +#define BigWide Big64 + #define GET_INT32_FLAT(x) \ x = Big32(* (uint32_t_t *) src) #define GET_INT16_FLAT(x) \ x = Big16(* (uint16_t_t *) src) -#endif - #define GET_INT32(x) \ do { \ n -= 4; \ @@ -94,34 +131,6 @@ do { \ } \ } while (0) -#ifdef Platform_Alignment - -#define PUT_INT32_FLAT(x) \ -do { \ - dst[0] = (x) >> 24; \ - dst[1] = (unsigned char) ((x) >> 16); \ - dst[2] = (unsigned char) ((x) >> 8); \ - dst[3] = (unsigned char) (x); \ - dst += 4; \ -} while (0) - -#define PUT_INT24_FLAT(x) \ -do { \ - dst[0] = (x) >> 16; \ - dst[1] = (unsigned char) ((x) >> 8); \ - dst[2] = (unsigned char) (x); \ - dst += 3; \ -} while (0) - -#define PUT_INT16_FLAT(x) \ -do { \ - dst[0] = (x) >> 8; \ - dst[2] = (unsigned char) (x); \ - dst += 2; \ -} while (0) - -#else - #define PUT_INT32_FLAT(x) \ do { \ * (uint32_t_t *) dst = x; \ @@ -141,10 +150,8 @@ do { \ dst += 2; \ } while (0) -#endif - -#define HPACK_LIMIT (Bit_Capacity / 7) * 7 -#define HPACK_LAST ((1 << (Bit_Capacity % 7)) - 1) +#define HPACK_LIMIT (64 / 7) * 7 +#define HPACK_LAST ((1 << (64 % 7)) - 1) /* Flexible integer decoding as specified */ /* in the HPACK RFC-7541: */ diff --git a/tempesta_fw/http2/hstatic.h b/tempesta_fw/http2/hstatic.h index 6ac81cb80..36e0e8c79 100644 --- a/tempesta_fw/http2/hstatic.h +++ b/tempesta_fw/http2/hstatic.h @@ -23,7 +23,8 @@ #ifndef HPACK_STATIC_H #define HPACK_STATIC_H -#include "common.h" +#include + #include "../str.h" #include "hpack.h" #include "hindex.h" diff --git a/tempesta_fw/http2/http2.h b/tempesta_fw/http2/http2.h index 664e06b38..e64dfdeeb 100644 --- a/tempesta_fw/http2/http2.h +++ b/tempesta_fw/http2/http2.h @@ -23,7 +23,8 @@ #ifndef HTTP2_H #define HTTP2_H -#include "common.h" +#include + #include "../pool.h" #include "../str.h" #include "../http.h" diff --git a/tempesta_fw/http2/http2_api.h b/tempesta_fw/http2/http2_api.h index 528ea19f3..175cdc1d3 100644 --- a/tempesta_fw/http2/http2_api.h +++ b/tempesta_fw/http2/http2_api.h @@ -23,6 +23,8 @@ #ifndef HTTP2_API_H #define HTTP2_API_H +#include + #include "../pool.h" #include "../str.h" #include "../http.h" diff --git a/tempesta_fw/http2/huffman.c b/tempesta_fw/http2/huffman.c index c5ef21901..9fb91fdf2 100644 --- a/tempesta_fw/http2/huffman.c +++ b/tempesta_fw/http2/huffman.c @@ -21,11 +21,11 @@ */ #include -#include "common.h" -#include "netconv.h" +#include #include "errors.h" #include "buffers.h" #include "huffman.h" +#include "hpack_helpers.h" /* * Huffman decoder state machine: @@ -150,10 +150,10 @@ huffman_decode_tail_s(unsigned int c, #define GET_UWIDE(tail) \ do { \ - current = Bit_Capacity - tail; \ + current = 64 - tail; \ c = BigWide(* (uintptr_t *) src); \ - src += Word_Size; \ - n -= Word_Size; \ + src += sizeof(uintptr_t); \ + n -= sizeof(uintptr_t); \ } while (0) #define GET_UINT(bits) \ @@ -213,29 +213,27 @@ do { \ } \ } while (0) -#ifdef Platform_64bit - #define GET_CASCADE() \ if ((uintptr_t) src & 2) { \ - if (space <= (Bit_Capacity - 8) - 16) { \ + if (space <= (64 - 8) - 16) { \ space += 16; \ c = Bit_Join(c, 16, Big16(* (uint16_t *) src)); \ src += 2; \ L1: \ - if (space <= (Bit_Capacity - 8) - 32) { \ + if (space <= (64 - 8) - 32) { \ space += 32; \ c = Bit_Join(c, 32, \ Big32(* (uint32_t *) src)); \ src += 4; \ } \ - if (space <= (Bit_Capacity - 8) - 16) { \ + if (space <= (64 - 8) - 16) { \ space += 16; \ c = Bit_Join(c, 16, \ Big16(* (uint16_t *) src)); \ src += 2; \ } \ } \ - if (space <= (Bit_Capacity - 8) - 8) { \ + if (space <= (64 - 8) - 8) { \ c = Bit_Join8(c, * src++); \ } \ } \ @@ -244,17 +242,17 @@ L1: \ #define GET_OCTETS(tail) \ do { \ unsigned int space = 0; \ - if (n < Word_Size - 1) { \ - space = (Bit_Capacity - 8) - n * 8; \ - n = Word_Size - 1; \ + if (n < sizeof(uintptr_t) - 1) { \ + space = (64 - 8) - n * 8; \ + n = sizeof(uintptr_t) - 1; \ current -= space; \ } \ if ((uintptr_t) src & 1) { \ c = Bit_Join8(c, * src++); \ space += 8; \ } \ - current += (Word_Size - 1) * 8; \ - n -= Word_Size - 1; \ + current += (sizeof(uintptr_t) - 1) * 8; \ + n -= sizeof(uintptr_t) - 1; \ GET_CASCADE(); \ } while (0) @@ -266,26 +264,26 @@ do { \ space += 8; \ } \ if ((uintptr_t) src & 2) { \ - if (space <= Bit_Capacity - 16) { \ + if (space <= 64 - 16) { \ space += 16; \ c = Bit_Join(c, 16, \ Big16(* (uint16_t *) src)); \ src += 2; \ L0: \ - if (space <= Bit_Capacity - 32) { \ + if (space <= 64 - 32) { \ space += 32; \ c = Bit_Join(c, 32, \ Big32(* (uint32_t *) src)); \ src += 4; \ } \ - if (space <= Bit_Capacity - 16) { \ + if (space <= 64 - 16) { \ space += 16; \ c = Bit_Join(c, 16, \ Big16(* (uint16_t *) src)); \ src += 2; \ } \ } \ - if (space <= Bit_Capacity - 8) { \ + if (space <= 64 - 8) { \ c = Bit_Join8(c, * src++); \ } \ } \ @@ -297,70 +295,33 @@ L0: \ Bit_Join((uintptr_t) * (uint32_t *) (src + 4), 32, \ * (uint32_t *) src) \ ); \ - src += Word_Size; \ + src += sizeof(uintptr_t); \ } \ } while (0) #define GET_FIRST(tail) \ do { \ - current = Bit_Capacity - HT_NBITS; \ - if (((uintptr_t) src & (Word_Size - 1)) == 0) { \ - if (n < Word_Size) { \ + unsigned int space = 0; \ + current = 64 - HT_NBITS; \ + if (((uintptr_t) src & (sizeof(uintptr_t) - 1)) == 0) { \ + if (n < sizeof(uintptr_t)) { \ goto Z0; \ } \ c = BigWide(* (uintptr_t *) src); \ - src += Word_Size; \ + src += sizeof(uintptr_t); \ } \ else { \ - unsigned int space = 0; \ - if (n < Word_Size) { \ + if (n < sizeof(uintptr_t)) { \ Z0: \ - space = Bit_Capacity - n * 8; \ - n = Word_Size; \ + space = 64 - n * 8; \ + n = sizeof(uintptr_t); \ current -= space; \ } \ } \ GET_FIRST_CASCADE(); \ - n -= Word_Size; \ + n -= sizeof(uintptr_t); \ } while (0) -#else - -#ifdef Platform_Alignment - -#define GET_OCTETS(tail) \ - if (current != -tail || \ - (uintptr_t) src & (Word_Size - 1) || \ - n < Word_Size) \ - GET_OCTETS_BY_8(tail); \ - else \ - GET_UWIDE(tail) - -#define GET_FIRST(tail) \ - if (((uintptr_t) src & (Word_Size - 1)) == 0 && \ - n >= Word_Size) \ - GET_UWIDE(tail); \ - else \ - GET_FIRST_BY_8(tail) - -#else - -#define GET_OCTETS(tail) \ - if (current != -tail || n < Word_Size) \ - GET_OCTETS_BY_8(tail); \ - else \ - GET_UWIDE(tail) - -#define GET_FIRST(tail) \ - if (n >= Word_Size) \ - GET_UWIDE(tail); \ - else \ - GET_FIRST_BY_8(tail) - -#endif - -#endif - unsigned int huffman_decode(const char *__restrict source, char *__restrict dst, uintptr_t n) { @@ -547,11 +508,11 @@ huffman_decode_tail_s_f(unsigned int c, #define GET_UWIDE_FR(tail) \ do { \ - current = Bit_Capacity - tail; \ + current = 64 - tail; \ c = BigWide(* (uintptr_t *) src); \ - src += Word_Size; \ - n -= Word_Size; \ - m -= Word_Size; \ + src += sizeof(uintptr_t); \ + n -= sizeof(uintptr_t); \ + m -= sizeof(uintptr_t); \ } while (0) #define GET_UINT_FR(bits) \ @@ -619,19 +580,17 @@ do { \ } \ } while (0) -#ifdef Platform_64bit - #define GET_OCTETS_FR(tail) \ do { \ - space = m; \ + unsigned int space = m; \ if (m > n) { \ space = n; \ } \ - if (space >= Word_Size - 1) { \ + if (space >= sizeof(uintptr_t) - 1) { \ space = 0; \ } \ else { \ - space = Word_Size - 1 - space; \ + space = sizeof(uintptr_t) - 1 - space; \ n += space; \ m += space; \ space <<= 3; \ @@ -641,33 +600,34 @@ do { \ c = Bit_Join8(c, * src++); \ space += 8; \ } \ - current += (Word_Size - 1) * 8; \ - n -= Word_Size - 1; \ - m -= Word_Size - 1; \ + current += (sizeof(uintptr_t) - 1) * 8;\ + n -= sizeof(uintptr_t) - 1; \ + m -= sizeof(uintptr_t) - 1; \ GET_CASCADE(); \ } while (0) -#define GET_FIRST(tail) \ +#define GET_FIRST_FR(tail) \ do { \ unsigned int space = m; \ if (m > n) { \ space = n; \ } \ - current = Bit_Capacity - HT_NBITS; \ - if (((uintptr_t) src & (Word_Size - 1)) == 0) { \ - if (space < Word_Size) { \ + current = 64 - HT_NBITS; \ + if (((uintptr_t) src & (sizeof(uintptr_t) - 1)) == 0) { \ + if (space < sizeof(uintptr_t)) { \ goto Z0; \ } \ c = BigWide(* (uintptr_t *) src); \ - src += Word_Size; \ + src += sizeof(uintptr_t); \ } \ else { \ - if (space >= Word_Size) { \ + if (space >= sizeof(uintptr_t)) { \ space = 0; \ } \ else { \ Z0: \ - space = Word_Size - space; \ + space = sizeof(uintptr_t) - \ + space; \ n += space; \ m += space; \ space <<= 3; \ @@ -675,48 +635,10 @@ Z0: \ } \ } \ GET_FIRST_CASCADE(); \ - n -= Word_Size; \ - m -= Word_Size; \ + n -= sizeof(uintptr_t); \ + m -= sizeof(uintptr_t); \ } while (0) -#else - -#ifdef Platform_Alignment - -#define GET_OCTETS_FR(tail) \ - if (current != -tail || \ - (uintptr_t) src & (Word_Size - 1) || \ - n < Word_Size || m < Word_Size) \ - GET_OCTETS_BY_8_FR(tail); \ - else \ - GET_UWIDE_FR(tail) - -#define GET_FIRST_FR(tail) \ - if (((uintptr_t) src & (Word_Size - 1)) == 0 && \ - n >= Word_Size && m >= Word_Size) \ - GET_UWIDE_FR(tail); \ - else \ - GET_FIRST_BY_8_FR(tail) - -#else - -#define GET_OCTETS_FR(tail) \ - if (current != -tail || n < Word_Size \ - || m < Word_Size) \ - GET_OCTETS_BY_8_FR(tail); \ - else \ - GET_UWIDE_FR(tail) - -#define GET_FIRST_FR(tail) \ - if (n >= Word_Size && m >= Word_Size) \ - GET_UWIDE_FR(tail); \ - else \ - GET_FIRST_BY_8_FR(tail) - -#endif - -#endif - unsigned int huffman_decode_fragments(HTTP2Input * __restrict source, HTTP2Output * __restrict out, uintptr_t n) @@ -828,8 +750,6 @@ huffman_decode_fragments(HTTP2Input * __restrict source, dst[2] = (char) (aux >> 8); \ dst[3] = (char) aux -#ifdef Platform_64bit - #define Write8Big() \ dst[0] = (char) (aux >> 56); \ dst[1] = (char) (aux >> 48); \ @@ -840,24 +760,6 @@ huffman_decode_fragments(HTTP2Input * __restrict source, dst[6] = (char) (aux >> 8); \ dst[7] = (char) aux -#endif - -#ifdef Platform_Big - -#define Write2() \ - dst[0] = (char) (aux >> 7); \ - dst[1] = (char) aux - -#define Write4 Write4Big - -#ifdef Platfrom_64bit - -#define Write8 Write8Big - -#endif - -#else - #define Write2() \ dst[0] = (char) aux; \ dst[1] = (char) (aux >> 7) @@ -867,8 +769,6 @@ huffman_decode_fragments(HTTP2Input * __restrict source, dst[2] = (char) (aux >> 16); \ dst[3] = (char) (aux >> 24) -#ifdef Platform_64bit - #define Write8() \ Write4(); \ dst[4] = (char) (aux >> 32); \ @@ -876,15 +776,7 @@ huffman_decode_fragments(HTTP2Input * __restrict source, dst[6] = (char) (aux >> 48); \ dst[7] = (char) (aux >> 56) -#endif - -#endif - -#ifdef Platfrom_64bit #define WriteAux Write8Big -#else -#define WriteAux Write4Big -#endif uintptr_t huffman_encode(const char *__restrict source, char *__restrict dst, uintptr_t n) @@ -899,7 +791,7 @@ huffman_encode(const char *__restrict source, char *__restrict dst, uintptr_t n) do { const unsigned int s = *src++; - const unsigned int d = Bit_Capacity - current; + const unsigned int d = 64 - current; const unsigned int c = ht_encode[s]; const unsigned int l = ht_length[s]; @@ -907,21 +799,11 @@ huffman_encode(const char *__restrict source, char *__restrict dst, uintptr_t n) if (l <= d) { aux = Bit_Join(aux, l, c); } else { - current -= Bit_Capacity; + current -= 64; aux = Bit_Join(aux, d, c >> current); -#ifdef Platform_Alignment - if (((uintptr_t) dst & (Word_Size - 1)) == 0) { -#endif -#ifdef Platform_Little - aux = SwapBytes(aux); -#endif - *(uintptr_t *) dst = aux; -#ifdef Platform_Alignment - } else { - WriteAux(); - } -#endif - dst += Word_Size; + aux = Big64(aux); + *(uintptr_t *) dst = aux; + dst += sizeof(uintptr_t); aux = c; } } while (--n); @@ -934,53 +816,21 @@ huffman_encode(const char *__restrict source, char *__restrict dst, uintptr_t n) aux = Bit_Join(aux, d, HT_EOS_HIGH >> tail); current += d; } - aux <<= Bit_Capacity - current; -#ifdef Platform_Little - aux = SwapBytes(aux); -#endif -#ifdef Platform_64bit - if (current == Bit_Capacity) { -#ifdef Platform_Alignment - if (((uintptr_t) dst & (Word_Size - 1)) == 0) { -#endif - *(uintptr_t *) dst = aux; -#ifdef Platform_Alignment - } else { - Write8(); - } -#endif - dst += Word_Size; + aux <<= 64 - current; + aux = Bug64(aux); + if (current == 64) { + *(uintptr_t *) dst = aux; + dst += sizeof(uintptr_t); goto Exit; } -#endif if (current > 31) { -#ifdef Platform_Alignment - if (((uintptr_t) dst & 3) == 0) { -#endif - *(uint32_t *) dst = (uint32_t) aux; -#ifdef Platform_Alignment - } else { - Write4(); - } -#endif + *(uint32_t *) dst = (uint32_t) aux; dst += 4; -#ifdef Platform_32bit - goto Exit; -#else aux >>= 32; current -= 32; -#endif } if (current > 15) { -#ifdef Platform_Alignment - if (((uintptr_t) dst & 1) == 0) { -#endif - *(uint16_t *) dst = (uint16_t) aux; -#ifdef Platform_Alignment - } else { - Write2(); - } -#endif + *(uint16_t *) dst = (uint16_t) aux; dst += 2; aux >>= 16; current -= 16; @@ -994,8 +844,6 @@ huffman_encode(const char *__restrict source, char *__restrict dst, uintptr_t n) return dst - dst_saved; } -#ifdef Platform_Little - #define WriteBytes(n) \ do { \ unsigned int __n = n; \ @@ -1007,21 +855,6 @@ do { \ } while (--__n); \ } while (0) -#else - -#define WriteBytes(n) \ -do { \ - unsigned int __n = n; \ - do { \ - CheckByte_goto(out, Bug); \ - * dst++ = (char) (aux >> (((n) - 1) * 8)); \ - aux <<= 8; \ - k--; \ - } while (--__n); \ -} while (0) - -#endif - unsigned char * huffman_encode_fragments(HTTP2Output * __restrict out, unsigned char *__restrict dst, @@ -1052,7 +885,7 @@ huffman_encode_fragments(HTTP2Output * __restrict out, } { const unsigned int s = *src++; - const unsigned int d = Bit_Capacity - current; + const unsigned int d = 64 - current; const unsigned int c = ht_encode[s]; const unsigned int l = ht_length[s]; @@ -1060,26 +893,14 @@ huffman_encode_fragments(HTTP2Output * __restrict out, if (l <= d) { aux = Bit_Join(aux, l, c); } else { - current -= Bit_Capacity; + current -= 64; aux = Bit_Join(aux, d, c >> current); - if (k >= Word_Size) { -#ifdef Platform_Alignment - if (((uintptr_t) dst & - (Word_Size - 1)) == 0) { -#endif -#ifdef Platform_Little - aux = SwapBytes(aux); -#endif - *(uintptr_t *) dst = - aux; -#ifdef Platform_Alignment - } else { - WriteAux(); - } -#endif - dst += Word_Size; + if (k >= sizeof(uintptr_t)) { + aux = Big64(aux); + *(uintptr_t *) dst = aux; + dst += sizeof(uintptr_t); } else { - WriteBytes(Word_Size); + WriteBytes(sizeof(uintptr_t)); } aux = c; } @@ -1095,65 +916,30 @@ huffman_encode_fragments(HTTP2Output * __restrict out, aux = Bit_Join(aux, d, HT_EOS_HIGH >> tail); current += d; } - aux <<= Bit_Capacity - current; -#ifdef Platform_Little - aux = SwapBytes(aux); -#endif -#ifdef Platform_64bit - if (current == Bit_Capacity) { - if (k >= Word_Size) { -#ifdef Platform_Alignment - if (((uintptr_t) dst & (Word_Size - 1)) - == 0) { -#endif - *(uintptr_t *) dst = aux; -#ifdef Platform_Alignment - } else { - Write8(); - } -#endif - dst += Word_Size; + aux <<= 64 - current; + aux = Big64(aux); + if (current == 64) { + if (k >= sizeof(uintptr_t)) { + *(uintptr_t *) dst = aux; + dst += sizeof(uintptr_t); } else { - WriteBytes(Word_Size); + WriteBytes(sizeof(uintptr_t)); } goto Exit; } -#endif if (current > 31) { if (k >= 4) { -#ifdef Platform_Alignment - if (((uintptr_t) dst & 3) == 0) { -#endif - *(uint32_t *) dst = - (uint32_t) aux; -#ifdef Platform_Alignment - } else { - Write4(); - } -#endif + *(uint32_t *) dst = (uint32_t) aux; dst += 4; } else { WriteBytes(4); } -#ifdef Platform_32bit - goto Exit; -#else aux >>= 32; current -= 32; -#endif } if (current > 15) { if (k >= 2) { -#ifdef Platform_Alignment - if (((uintptr_t) dst & 1) == 0) { -#endif - *(uint16_t *) dst = - (uint16_t) aux; -#ifdef Platform_Alignment - } else { - Write2(); - } -#endif + *(uint16_t *) dst = (uint16_t) aux; dst += 2; } else { WriteBytes(2); @@ -1191,7 +977,7 @@ huffman_encode_plain(HTTP2Output * __restrict out, do { const unsigned int s = *src++; - const unsigned int d = Bit_Capacity - current; + const unsigned int d = 64 - current; const unsigned int c = ht_encode[s]; const unsigned int l = ht_length[s]; @@ -1199,25 +985,14 @@ huffman_encode_plain(HTTP2Output * __restrict out, if (l <= d) { aux = Bit_Join(aux, l, c); } else { - current -= Bit_Capacity; + current -= 64; aux = Bit_Join(aux, d, c >> current); - if (k >= Word_Size) { -#ifdef Platform_Alignment - if (((uintptr_t) dst & (Word_Size - 1)) - == 0) { -#endif -#ifdef Platform_Little - aux = SwapBytes(aux); -#endif - *(uintptr_t *) dst = aux; -#ifdef Platform_Alignment - } else { - WriteAux(); - } -#endif - dst += Word_Size; + if (k >= sizeof(uintptr_t)) { + aux = Big64(aux); + *(uintptr_t *) dst = aux; + dst += sizeof(uintptr_t); } else { - WriteBytes(Word_Size); + WriteBytes(sizeof(uintptr_t)); } aux = c; } @@ -1231,65 +1006,30 @@ huffman_encode_plain(HTTP2Output * __restrict out, aux = Bit_Join(aux, d, HT_EOS_HIGH >> tail); current += d; } - aux <<= Bit_Capacity - current; -#ifdef Platform_Little - aux = SwapBytes(aux); -#endif -#ifdef Platform_64bit - if (current == Bit_Capacity) { - if (k >= Word_Size) { -#ifdef Platform_Alignment - if (((uintptr_t) dst & (Word_Size - 1)) - == 0) { -#endif - *(uintptr_t *) dst = aux; -#ifdef Platform_Alignment - } else { - Write8(); - } -#endif - dst += Word_Size; + aux <<= 64 - current; + aux = Big64(aux); + if (current == 64) { + if (k >= sizeof(uintptr_t)) { + *(uintptr_t *) dst = aux; + dst += sizeof(uintptr_t); } else { - WriteBytes(Word_Size); + WriteBytes(sizeof(uintptr_t)); } goto Exit; } -#endif if (current > 31) { if (k >= 4) { -#ifdef Platform_Alignment - if (((uintptr_t) dst & 3) == 0) { -#endif - *(uint32_t *) dst = - (uint32_t) aux; -#ifdef Platform_Alignment - } else { - Write4(); - } -#endif + *(uint32_t *) dst = (uint32_t) aux; dst += 4; } else { WriteBytes(4); } -#ifdef Platform_32bit - goto Exit; -#else aux >>= 32; current -= 32; -#endif } if (current > 15) { if (k >= 2) { -#ifdef Platform_Alignment - if (((uintptr_t) dst & 1) == 0) { -#endif - *(uint16_t *) dst = - (uint16_t) aux; -#ifdef Platform_Alignment - } else { - Write2(); - } -#endif + *(uint16_t *) dst = (uint16_t) aux; dst += 2; } else { WriteBytes(2); diff --git a/tempesta_fw/http2/huffman.h b/tempesta_fw/http2/huffman.h index 383940b95..ced48ef38 100644 --- a/tempesta_fw/http2/huffman.h +++ b/tempesta_fw/http2/huffman.h @@ -23,7 +23,8 @@ #ifndef HPACK_HUFFMAN_H #define HPACK_HUFFMAN_H -#include "common.h" +#include + #include "errors.h" #include "buffers.h" diff --git a/tempesta_fw/http2/netconv.h b/tempesta_fw/http2/netconv.h deleted file mode 100644 index cee9b9534..000000000 --- a/tempesta_fw/http2/netconv.h +++ /dev/null @@ -1,406 +0,0 @@ -/** - * Tempesta FW - * - * Conversion between little and big endian numbers. - * - * Copyright (C) 2017 Tempesta Technologies, Inc. - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, - * or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with - * this program; if not, write to the Free Software Foundation, Inc., 59 - * Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * Copyright (C) Julius Goryavsky. Original code of this module - * is granted by the author for unrestricted use in the Tempesta FW - * and for distribution under GNU General Public License without - * any restrictions. - */ - -#ifndef COMMON_NETCONV_H -#define COMMON_NETCONV_H - -#include "common.h" -#include "rotate.h" - -#define SwapBytes16_Defined -#define SwapBytes32_Defined -#define SwapBytes64_Defined - -#ifdef _MSC_VER - #ifndef __INTRIN_H_ - #pragma warning(push, 4) - #pragma warning(disable: 4255 4668) - #include - #pragma warning(pop) - #endif -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -#ifdef Platform_64bit - -#ifdef __GNUC__ - #define SwapBytes16F(x) \ - (__extension__ ({ \ - register uint __r = (uint16) x; \ - __asm__( \ - "xchgb %b0,%h0" : "=Q" (__r) : "0" (__r) \ - ); \ - __r; \ - })) - #ifdef Compiler_GCC43 - #define SwapBytes32F(x) (uint32) __builtin_bswap32(x) - #define SwapBytes64F(x) (uint64) __builtin_bswap64(x) - #else - #define SwapBytes32F(x) \ - (__extension__ ({ \ - register uint __r = x; \ - __asm__( \ - "bswapl %0" : "=R" (__r) : "0" (__r) \ - ); \ - __r; \ - })) - #define SwapBytes64F(x) \ - (__extension__ ({ \ - register uwide __r = x; \ - __asm__( \ - "bswapq %0" : "=r" (__r) : "0" (__r) \ - ); \ - __r; \ - })) - #endif - #define SwapBytesDWide_Defined - #define SwapBytesDWide(x) \ - (__extension__ ({ \ - uint128 __x = x; \ - uint128 __r; \ - __asm__( \ - "bswapq %0\n\t" \ - "bswapq %1" : \ - "=&r" (__r.High), "=&r" (__r.Low) : \ - "0" (__x.Low), "1" (__x.High) \ - ); \ - __r; \ - })) -#elif defined(_MSC_VER) - #define SwapBytes16F(x) _byteswap_ushort(x) - #define SwapBytes32F(x) _byteswap_ulong(x) - #define SwapBytes64F(x) _byteswap_uint64(x) -#else - #error Unsupported compiler... -#endif - -#else - -#ifdef _MSC_VER - #define SwapBytes16F(x) _byteswap_ushort(x) - #define SwapBytes32F(x) _byteswap_ulong(x) - #define SwapBytes64F(x) _byteswap_uint64(x) -#elif defined(__GNUC__) - #define SwapBytes16F(x) \ - (__extension__ ({ \ - register uint __r = (uint16) x; \ - __asm__( \ - "xchgb %b0,%h0" : "=Q" (__r) : "0" (__r) \ - ); \ - __r; \ - })) - #ifdef Compiler_GCC43 - #define SwapBytes32F(x) (uint32) __builtin_bswap32(x) - #define SwapBytes64F(x) (uint64) __builtin_bswap64(x) - #else - #define SwapBytes32F(x) \ - (__extension__ ({ \ - register uint __r = x; \ - __asm__( \ - "bswap %0" : "=r" (__r) : "0" (__r) \ - ); \ - __r; \ - })) - #define SwapBytes64F(x) \ - (__extension__ ({ \ - register uint64 __r; \ - register uint __x; \ - register uint __y; \ - __asm__( \ - "bswap %0\n\t" \ - "bswap %1" : \ - "=a" (__x), "=d" (__y) : "A" (x) \ - ); \ - __asm__("" : "=A" (__r) : "d" (__x), "a" (__y)); \ - __r; \ - })) - #endif -#else - #error Unsupported compiler... -#endif - -#endif - -#ifdef __cplusplus -} -#endif - -#ifdef __GNUC__ - #define SwapBytes16C(x) \ - (uint16) (((uint16) (x) >> 8) | \ - ((uint16) (x) << 8)) - #define SwapBytes32C(x) \ - (((uint32) (x) << 24) | \ - (((uint32) (x) & 0x0000FF00U) << 8) | \ - (((uint32) (x) & 0x00FF0000U) >> 8) | \ - ((uint32) (x) >> 24)) - #define SwapBytes64C(x) \ - (((uint64) (x) << 56) | \ - (((uint64) (x) & 0x000000000000FF00ULL) << 40) | \ - (((uint64) (x) & 0x0000000000FF0000ULL) << 24) | \ - (((uint64) (x) & 0x00000000FF000000ULL) << 8) | \ - (((uint64) (x) & 0x000000FF00000000ULL) >> 8) | \ - (((uint64) (x) & 0x0000FF0000000000ULL) >> 24) | \ - (((uint64) (x) & 0x00FF000000000000ULL) >> 40) | \ - ((uint64) (x) >> 56)) - #define SwapBytes16(x) \ - (__builtin_constant_p((uint16) (x)) ? \ - SwapBytes16C(x) : SwapBytes16F(x)) - #define SwapBytes32(x) \ - (__builtin_constant_p((uint32) (x)) ? \ - SwapBytes32C(x) : SwapBytes32F(x)) - #define SwapBytes64(x) \ - (__builtin_constant_p((uint64) (x)) ? \ - SwapBytes64C(x) : SwapBytes64F(x)) - #ifndef SwapBytesDWide_Defined - #if defined(Platform_64bit) || defined(Hardware_Swap) - #define SwapBytesDWide_Defined - #ifdef Internal_dwide - #define SwapBytesDWide(x) \ - (__extension__ ({ \ - udwide __x = x; \ - ((udwide) SwapBytes((uwide) __x) << Bit_Capacity) | \ - SwapBytes((uwide) (__x >> Bit_Capacity)); \ - })) - #else - #define SwapBytesDWide(x) \ - (__extension__ ({ \ - udwide __x = x; \ - udwide __r; \ - __r.Low = SwapBytes(__x.High); \ - __r.High = SwapBytes(__x.Low); \ - __r; \ - })) - #endif - #endif - #endif -#else - #define SwapBytes16 SwapBytes16F - #define SwapBytes32 SwapBytes32F - #define SwapBytes64 SwapBytes64F -#endif - -#ifdef Platform_32bit - #define SwapBytes SwapBytes32 - #ifndef SwapBytes64_Defined - #define SwapBytes64_Defined - #define SwapBytes64F SwapBytesDWide - #else - #ifndef SwapBytesDWide_Defined - #define SwapBytesDWide_Defined - #define SwapBytesDWide SwapBytes64 - #endif - #endif -#else - #define SwapBytes SwapBytes64 - #define SwapBytes128 SwapBytesDWide -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -#ifndef SwapBytes16_Defined - Attribute_Const common_inline ufast - SwapBytes16F (const ufast x) - { - return Bit_Join8(x & 0xFF, (x & 0xFF00) >> 8); - } -#endif - -#ifndef SwapBytes32_Defined - -#ifndef Compiler_Rotate - Attribute_Const common_inline ufast - SwapBytes32F (const uint32 x) - { - return Bit_Shift(x, 24, - Bit_Join8(x & 0xFF00, - ((x >> 8) & 0xFF00) | - (x >> 24))); - } -#else - Attribute_Const common_inline ufast - SwapBytes32F (const ufast x) - { - ufast y = Bit_Join8(x & 0x00FF00FF, (x >> 8) & 0x00FF00FF); - return Rotate32_Left(y, 16); - } -#endif - -#endif - -#ifdef Platform_64bit - -#ifndef SwapBytes64_Defined - -Attribute_Const common_inline uint64 -SwapBytes64F (const uint64 x) -{ - uint64 y; - y = Bit_Join8(x & 0x00FF00FF00FF00FF, (x >> 8) & 0x00FF00FF00FF00FF); - y = Bit_Shift(y & 0x0000FFFF0000FFFF, 16, (y >> 16) & 0x0000FFFF0000FFFF); - #ifdef Compiler_Rotate64 - return Rotate_Left(y, 32); - #else - return Bit_Shift(y, 32, y >> 32); - #endif -} - -#endif - -#endif - -#ifndef SwapBytesDWide_Defined - -#if defined(Platform_64bit) || defined(Hardware_Swap) - -#define SwapBytesDWide_Defined - -Attribute_Const common_inline udwide -SwapBytesDWide (const udwide x) -{ - #ifdef Internal_dwide - return ((udwide) SwapBytes((uwide) x) << Bit_Capacity) | - SwapBytes((uwide) (x >> Bit_Capacity)); - #else - udwide r; - r.Low = SwapBytes(x.High); - r.High = SwapBytes(x.Low); - return r; - #endif -} - -#endif - -#endif - -#ifdef Platform_32bit - -#ifndef SwapBytesDWide_Defined - -Attribute_Const common_inline -uint64 SwapBytesDWide (const uint64 x) -{ - #ifdef Internal_int64 - uint a = (uint) (x >> 32); - uint b = (uint) x; - #else - uint64 r; - uint a = x.Low; - uint b = x.High; - #endif - #ifndef Compiler_Rotate - a = Bit_Shift(a, 24, - Bit_Join8(a & 0xFF00, - ((a >> 8) & 0xFF00) | - (a >> 24))); - b = Bit_Shift(b, 24, - Bit_Join8(b & 0xFF00, - ((b >> 8) & 0xFF00) | - (b >> 24))); - #else - a = Bit_Join8(a & 0x00FF00FF, (a >> 8) & 0x00FF00FF); - b = Bit_Join8(b & 0x00FF00FF, (b >> 8) & 0x00FF00FF); - a = Rotate32_Left(a, 16); - b = Rotate32_Left(b, 16); - #endif - #ifdef Internal_int64 - return ((uint64) b << 32) | a; - #else - r.Low = b; - r.High = a; - return r; - #endif -} - -#endif - -#endif - -#ifdef __cplusplus -} -#endif - -#ifdef Platform_Little - #define Little16(x) (x) - #define Little32(x) (x) - #define Little64(x) (x) - #define LittleShort(x) (x) - #define LittleInt(x) (x) - #define LittleWide(x) (x) - #define LittleDWide(x) (x) - #define Big16 SwapBytes16 - #define Big32 SwapBytes32 - #define Big64 SwapBytes64 - #define BigShort SwapBytes16 - #ifdef Platform_Int32bit - #define BigInt SwapBytes32 - #else - #define BigInt SwapBytes64 - #endif - #define BigWide SwapBytes - #define BigDWide SwapBytesDWide - #ifdef Platform_64bit - #define Little128(x) (x) - #define Big128 SwapBytes128 - #endif -#else - #define Big16(x) (x) - #define Big32(x) (x) - #define Big64(x) (x) - #define BigShort(x) (x) - #define BigInt(x) (x) - #define BigWide(x) (x) - #define BigDWide(x) (x) - #define Little16 SwapBytes16 - #define Little32 SwapBytes32 - #define Little64 SwapBytes64 - #define LittleShort SwapBytes16 - #ifdef Platform_Int32bit - #define LittleInt SwapBytes32 - #else - #define LittleInt SwapBytes64 - #endif - #define LittleWide SwapBytes - #define LittleDWide SwapBytesDWide - #ifdef Platform_64bit - #define Big128(x) (x) - #define Little128 SwapBytes128 - #endif -#endif - -#define NetShort Big16 -#define NetInt Big32 -#define NetLong Big64 -#define NetWide BigWide -#define NetDWide BigDWide - -#endif diff --git a/tempesta_fw/http2/rotate.h b/tempesta_fw/http2/rotate.h deleted file mode 100644 index 21b2ad645..000000000 --- a/tempesta_fw/http2/rotate.h +++ /dev/null @@ -1,262 +0,0 @@ -/** - * Tempesta FW - * - * Integer shifts with rotation. - * - * Copyright (C) 2017 Tempesta Technologies, Inc. - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, - * or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with - * this program; if not, write to the Free Software Foundation, Inc., 59 - * Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * Copyright (C) Julius Goryavsky. Original code of this module - * is granted by the author for unrestricted use in the Tempesta FW - * and for distribution under GNU General Public License without - * any restrictions. - */ - -#ifndef COMMON_ROTATE_H -#define COMMON_ROTATE_H - -#include "common.h" - -#ifdef Compiler_Rotate -#ifdef _MSC_VER -#ifndef __INTRIN_H_ -#pragma warning(push, 4) -#pragma warning(disable: 4255 4668) -#include -#pragma warning(pop) -#endif -#else -#include -#endif -#define Rotate32_Defined -#define Rotate32_Left _rotl -#define Rotate32_Right _rotr -#ifdef Compiler_Rotate64 -#define Rotate64_Defined -#ifdef _MSC_VER -#define Rotate64_Left _rotl64 -#define Rotate64_Right _rotr64 -#else -#define Rotate64_Left _lrotl -#define Rotate64_Right _lrotr -#endif -#endif -#endif - -#ifdef Platform_32bit -#define Rotate_Defined -#define Rotate_Left Rotate32_Left -#define Rotate_Right Rotate32_Right -#ifdef Rotate64_Defined -#define Rotate_Double_Defined -#define Rotate_Double_Left Rotate64_Left -#define Rotate_Double_Right Rotate64_Right -#else -#define Rotate64_Left Rotate_Double_Left -#define Rotate64_Right Rotate_Double_Right -#endif -#else -#ifdef Rotate64_Defined -#define Rotate_Defined -#define Rotate_Left Rotate64_Left -#define Rotate_Right Rotate64_Right -#else -#define Rotate64_Left Rotate_Left -#define Rotate64_Right Rotate_Right -#endif -#define Rotate128_Left Rotate_Double_Left -#define Rotate128_Right Rotate_Double_Right -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -#ifndef Rotate32_Defined - - Attribute_Const common_inline ufast - Rotate32_Left(const uint32 x, const ufast Shift) { -#if Shift_Length < 32 - if (Shift) { -#endif -#if defined(Hardware_Rotate) || Hardware_ShiftAdd <= 1 - return (uint32) (x << Shift) | ((uwide) x >> - (32 - Shift)); -#else - return (uint32) (x << Shift) + - ((uwide) x >> (32 - Shift)); -#endif -#if Shift_Length < 32 - } else { - return x; - } -#endif - } - - Attribute_Const common_inline ufast - Rotate32_Right(const uint32 x, const ufast Shift) { -#if Shift_Length < 32 - if (Shift) { -#endif -#if defined(Hardware_Rotate) || Hardware_ShiftAdd <= 1 - return (x >> Shift) | (uint32) ((uwide) x << - (32 - Shift)); -#else - return (x >> Shift) + - (uint32) ((uwide) x << (32 - Shift)); -#endif -#if Shift_Length < 32 - } else { - return x; - } -#endif - } - -#endif - -#ifndef Rotate_Defined - - Attribute_Const common_inline uwide - Rotate_Left(const uwide x, const ufast Shift) { -#if Shift_Length < Bit_Capacity - if (Shift) { -#endif -#if defined(Hardware_Rotate) || Hardware_ShiftAdd <= 1 - return (x << Shift) | (x >> (Bit_Capacity - Shift)); -#else - return (x << Shift) + (x >> (Bit_Capacity - Shift)); -#endif -#if Shift_Length < Bit_Capacity - } else { - return x; - } -#endif - } - - Attribute_Const common_inline uwide - Rotate_Right(const uwide x, const ufast Shift) { -#if Shift_Length < Bit_Capacity - if (Shift) { -#endif -#if defined(Hardware_Rotate) || Hardware_ShiftAdd <= 1 - return (x >> Shift) | (x << (Bit_Capacity - Shift)); -#else - return (x >> Shift) + (x << (Bit_Capacity - Shift)); -#endif -#if Shift_Length < Bit_Capacity - } else { - return x; - } -#endif - } - -#endif - -#ifndef Rotate_Double_Defined - -#ifdef Internal_dwide - -#define Rotate_Double_Defined - - Attribute_Const common_inline udwide - Rotate_Double_Left(const udwide x, const ufast Shift) { -#if Shift_Length < Bit_Capacity - if (Shift) { -#endif - return (x << Shift) | (x >> (2 * Bit_Capacity - Shift)); -#if Shift_Length < Bit_Capacity - } else { - return x; - } -#endif - } - - Attribute_Const common_inline udwide - Rotate_Double_Right(const udwide x, const ufast Shift) { -#if Shift_Length < Bit_Capacity - if (Shift) { -#endif - return (x << (2 * Bit_Capacity - Shift)) | (x >> Shift); -#if Shift_Length < Bit_Capacity - } else { - return x; - } -#endif - } - -#else - - Attribute_Const common_inline udwide - Rotate_Double_Left(const udwide x, const ufast Shift) { - udwide r = x; - int Offset = Bit_Capacity - Shift; - - if (Offset > 0) { -#if Shift_Length < Bit_Capacity - if (Shift) { -#endif - r.Low = - Bit_Shift(r.Low, Shift, r.High >> Offset); - r.High = - Bit_Shift(r.High, Shift, r.Low >> Offset); -#if Shift_Length < Bit_Capacity - } -#endif - } else { - const uint Bias = Offset + Bit_Capacity; - - Offset = -Offset; - r.Low = Bit_Shift(r.High, Offset, r.Low >> Bias); - r.High = Bit_Shift(r.Low, Offset, r.High >> Bias); - } - return r; - } - - Attribute_Const common_inline udwide - Rotate_Double_Right(const udwide x, const ufast Shift) { - udwide r = x; - int Offset = Bit_Capacity - Shift; - - if (Offset > 0) { -#if Shift_Length < Bit_Capacity - if (Shift) { -#endif - r.Low = - Bit_Shift(r.High, Offset, r.Low >> Shift); - r.High = - Bit_Shift(r.Low, Offset, r.High >> Shift); -#if Shift_Length < Bit_Capacity - } -#endif - } else { - const uint Bias = Offset + Bit_Capacity; - - Offset = -Offset; - r.Low = Bit_Shift(r.Low, Bias, r.High >> Offset); - r.High = Bit_Shift(r.High, Bias, r.Low >> Offset); - } - return r; - } - -#endif - -#endif - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/tempesta_fw/http2/subs.c b/tempesta_fw/http2/subs.c index 172b0d3a8..63f9d969e 100644 --- a/tempesta_fw/http2/subs.c +++ b/tempesta_fw/http2/subs.c @@ -26,7 +26,7 @@ */ #include -#include "common.h" +#include #include "subs.h" #ifndef offsetof @@ -35,12 +35,12 @@ #define Bit_UMul32(x, y) (uint32_t) ((x) * (y)) -#define Sub_Shift(n) \ - Shift = (offsetof(Sub, Data) + (Word_Size - 1)) & \ - ~(Word_Size - 1); \ - if ((n & (2 * Word_Size - 1)) == 0) { \ - Shift = (offsetof(Sub, Data) + (2 * Word_Size - 1)) & \ - ~(2 * Word_Size - 1); \ +#define Sub_Shift(n) \ + Shift = (offsetof(Sub, Data) + (sizeof(uintptr_t) - 1)) & \ + ~(sizeof(uintptr_t) - 1); \ + if ((n & (2 * sizeof(uintptr_t) - 1)) == 0) { \ + Shift = (offsetof(Sub, Data) + (2 * sizeof(uintptr_t) - 1)) & \ + ~(2 * sizeof(uintptr_t) - 1); \ } static void Sub_Chunk(Sub * const Object, void *const x, unsigned int n, @@ -54,7 +54,7 @@ Sub_New_Internal(const char *const Name, const int Length, { if (Length >= 1) { const unsigned int n = - (Length + (Word_Size - 1)) & ~(Word_Size - 1); + (Length + (sizeof(uintptr_t) - 1)) & ~(sizeof(uintptr_t) - 1); if ((Quant | Initial) >= 0) { unsigned int Allocated; Sub *Object; diff --git a/tempesta_fw/http2/subs.h b/tempesta_fw/http2/subs.h index d358cc2f0..4d225dcc6 100644 --- a/tempesta_fw/http2/subs.h +++ b/tempesta_fw/http2/subs.h @@ -28,9 +28,22 @@ #ifndef SUBS_H #define SUBS_H -#include "common.h" +#include + #include "../pool.h" +/* + * Remove aliasing effects from the pointer, because ANSI-compliant + * compiler should assume that any pointer to a union, which containing + * "char", potentially overlap with any other pointers in the program: + */ +typedef union { + char c; + void *p; +} AntiAliasType; + +#define AntiAliasLink(x) ((AntiAliasType *) (x))->p + typedef struct Sub Sub; typedef void *Sub_Allocate_Function(Sub * const Object); @@ -73,7 +86,7 @@ void *Sub_Allocate_List(Sub * const Object, const int Count, void *const Last); void Sub_Free_List(Sub * const Object, void *const First_Element); unsigned int Sub_Query_Length(const Sub * const Object); -common_inline void * +static __inline__ void * Sub_Allocate(Sub * const Object) { void **const Block = Object->Next.p; @@ -86,14 +99,14 @@ Sub_Allocate(Sub * const Object) } } -common_inline void +static __inline__ void Sub_Free(Sub * const Object, void *const Block) { AntiAliasLink(Block) = Object->Next.p; Object->Next.p = (void * *)Block; } -common_inline void +static __inline__ void Sub_Free_Chain(Sub * const Object, void *const p, void *const q) { AntiAliasLink(q) = Object->Next.p; diff --git a/tempesta_fw/http2/t/Makefile b/tempesta_fw/http2/t/Makefile index 8cae8b94c..fd8e8bb6e 100644 --- a/tempesta_fw/http2/t/Makefile +++ b/tempesta_fw/http2/t/Makefile @@ -7,7 +7,7 @@ options = -Wall -Wextra -Wstrict-overflow=5 -Wno-unused-parameter \ -ftree-loop-im -fivopts -fira-hoist-pressure \ -fbranch-target-load-optimize -hfiles = ../common.h ../bits.h ../netconv.h ../rotate.h +hfiles = ../bits.h hfiles += ../subs.h ../hash.h hfiles += ../buffers.h ../errors.h ../hfstate.h ../huffman.h diff --git a/tempesta_fw/http2/t/compare_test.c b/tempesta_fw/http2/t/compare_test.c index c35a1bca2..508f8f061 100644 --- a/tempesta_fw/http2/t/compare_test.c +++ b/tempesta_fw/http2/t/compare_test.c @@ -26,7 +26,6 @@ #include #include -#include "../common.h" #include "../../pool.h" #include "../../str.h" #include "../buffers.h" diff --git a/tempesta_fw/http2/t/fragments_test.c b/tempesta_fw/http2/t/fragments_test.c index bd0f7b1fd..87dd9be8a 100644 --- a/tempesta_fw/http2/t/fragments_test.c +++ b/tempesta_fw/http2/t/fragments_test.c @@ -24,9 +24,9 @@ #include #include #include +#include #include -#include "../common.h" #include "../../pool.h" #include "../../str.h" #include "../buffers.h" diff --git a/tempesta_fw/http2/t/hgentest.c b/tempesta_fw/http2/t/hgentest.c index db0cbec05..ecea32519 100644 --- a/tempesta_fw/http2/t/hgentest.c +++ b/tempesta_fw/http2/t/hgentest.c @@ -22,7 +22,7 @@ #include #include -#include "../common.h" +#include #include "../huffman.h" #include "../hgen/hfcode.h" diff --git a/tempesta_fw/http2/t/hpack_test.c b/tempesta_fw/http2/t/hpack_test.c index aa8932433..02729116f 100644 --- a/tempesta_fw/http2/t/hpack_test.c +++ b/tempesta_fw/http2/t/hpack_test.c @@ -25,8 +25,8 @@ #include #include #include +#include -#include "../common.h" #include "../../pool.h" #include "../../str.h" #include "../buffers.h" diff --git a/tempesta_fw/http2/t/huffman_test.c b/tempesta_fw/http2/t/huffman_test.c index b78d63a51..6b32b2079 100644 --- a/tempesta_fw/http2/t/huffman_test.c +++ b/tempesta_fw/http2/t/huffman_test.c @@ -25,8 +25,8 @@ #include #include #include +#include -#include "../common.h" #include "../huffman.h" typedef struct { diff --git a/tempesta_fw/http2/t/ngx_http_v2_huff_decode.c b/tempesta_fw/http2/t/ngx_http_v2_huff_decode.c index c9ea90c26..4e110ec0c 100644 --- a/tempesta_fw/http2/t/ngx_http_v2_huff_decode.c +++ b/tempesta_fw/http2/t/ngx_http_v2_huff_decode.c @@ -5,16 +5,16 @@ #include #include -#include "../common.h" +#include -typedef uchar u_char; -typedef fast ngx_int_t; -typedef ufast ngx_uint_t; +typedef unsigned char u_char; +typedef int ngx_int_t; +typedef unsigned int ngx_uint_t; #define NGX_OK 0 #define NGX_ERROR 1 -#define ngx_inline common_inline +#define ngx_inline static __inline__ typedef struct { u_char next;