From: Willy Tarreau Date: Fri, 26 Mar 2021 16:28:47 +0000 (+0100) Subject: MINOR: compat: add short aliases for a few very commonly used types X-Git-Tag: v2.4-dev14~3 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=be362fd992fb3d4d1709b58ad1698f20ce82aee7;p=thirdparty%2Fhaproxy.git MINOR: compat: add short aliases for a few very commonly used types Very often we use "int" where negative numbers are not needed (and can further cause trouble) just because it's painful to type "unsigned int" or "unsigned", or ugly to use in function arguments. Similarly sometimes chars would absolutely need to be signed but nobody types "signed char". Let's add a few aliases for such types and make them part of the standard internal API so that over time we can get used to them and get rid of horrible definitions. A comment also reminds some commonly available types and their properties regarding other types. --- diff --git a/include/haproxy/compat.h b/include/haproxy/compat.h index 0422b90760..0c1d9c9be4 100644 --- a/include/haproxy/compat.h +++ b/include/haproxy/compat.h @@ -34,6 +34,33 @@ #include #include + +/* These are a few short names for commonly used types whose size and sometimes + * signedness depends on the architecture. Be careful not to rely on a few + * common but wrong assumptions: + * - char is not always signed (ARM, AARCH64, PPC) + * - long is not always large enough for a pointer (Windows) + * These types are needed with the standard C API (string.h, printf, syscalls). + * + * When a fixed size is needed (protocol interoperability), better use the + * standard types provided by stdint.h: + * - size_t : unsigned int of default word size, large enough for any + * object in memory + * - ssize_t : signed int of default word size, used by some syscalls + * - uintptr_t : an unsigned int large enough to store any pointer + * - ptrdiff_t : a signed int large enough to hold a distance between 2 ptrs + * - int_t : a signed int of bits (8,16,32,64 work everywhere) + * - uint_t : an unsigned int of bits + */ +typedef signed char schar; +typedef unsigned char uchar; +typedef unsigned short ushort; +typedef unsigned int uint; +typedef unsigned long ulong; +typedef unsigned long long ullong; +typedef long long llong; + + /* set any optional field in a struct to this type to save ifdefs. Its address * will still be valid but it will not reserve any room nor require any * initialization.