]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: compat: add short aliases for a few very commonly used types
authorWilly Tarreau <w@1wt.eu>
Fri, 26 Mar 2021 16:28:47 +0000 (17:28 +0100)
committerWilly Tarreau <w@1wt.eu>
Fri, 26 Mar 2021 16:54:15 +0000 (17:54 +0100)
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.

include/haproxy/compat.h

index 0422b907602211c30589e9a4b04a0ae05fc7628b..0c1d9c9be4408f47530d71943c6c09a4c28b177c 100644 (file)
 #include <netinet/in.h>
 #include <netinet/tcp.h>
 
+
+/* 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<size>_t : a signed int of <size> bits (8,16,32,64 work everywhere)
+ *   - uint<size>_t : an unsigned int of <size> 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.