Types: make SilcUInt32 an unsigned int in most cases.
[silc.git] / lib / silcutil / silctypes.h
index 1e7639ce879a9f255c57a261ef176f7ab12873ee..d7f979766577c666ee2e5f6726ab4f2727bb28a4 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2002 - 2006 Pekka Riikonen
+  Copyright (C) 2002 - 2007 Pekka Riikonen
 
   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
 #ifndef SILCTYPES_H
 #define SILCTYPES_H
 
+/****d* silcutil/SILCTypes/SilcBool
+ *
+ * NAME
+ *
+ *    typedef unigned char SilcBool;
+ *
+ * DESCRIPTION
+ *
+ *    Boolean value, and is always 8-bits.  Represents value 0 or 1.
+ *
+ ***/
+typedef unsigned char SilcBool;
+
+/* The bool macro is deprecated.  Use SilcBool instead. */
+#ifdef SILC_MACOSX
+#define bool _Bool
+#endif
+#ifndef __cplusplus
+#ifndef bool
+#define bool unsigned char
+#endif
+#endif
+
 /****d* silcutil/SILCTypes/TRUE
  *
  * NAME
 #endif
 /***/
 
-/****d* silcutil/SILCTypes/SilcBool
- *
- * NAME
- *
- *    typedef unigned char SilcBool;
- *
- * DESCRIPTION
- *
- *    Boolean value, and is always 8-bits.  Represents value 0 or 1.
- *
- ***/
-typedef unsigned char SilcBool;
-
-/****d* silcutil/SILCTypes/bool
- *
- * NAME
- *
- *    #define SilcBool ...
- *
- * DESCRIPTION
- *
- *    Boolean value, and is 8-bits.  Represents value 0 or 1.  In
- *    C++ code this type is defined by the C++, and this definition is
- *    not used.
- *
- * NOTES
- *
- *    This macro is deprecated.  Use SilcBool instead.
- *
- * SOURCE
- */
-#ifdef SILC_MACOSX
-#define bool _Bool
-#endif
-
-#ifndef __cplusplus
-#ifndef bool
-#define bool unsigned char
-#endif
-#endif
-/***/
-
+/* Our offsetof macro */
 #define silc_offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
 
+/* silc_likely and silc_unlikely GCC branch prediction macros. Use only if
+   you have profiled the code first. */
+#if __GNUC__ >= 3
+#define silc_likely(expr) __builtin_expect(!!(expr), 1)
+#define silc_unlikely(expr) __builtin_expect(!!(expr), 0)
+#else
+#define silc_likely(expr) (expr)
+#define silc_unlikely(expr) (expr)
+#endif /* __GNUC__ >= 3 */
+
 #if SILC_SIZEOF_SHORT > 2
 #error "size of the short must be 2 bytes"
 #endif
@@ -185,14 +177,14 @@ typedef signed short SilcInt16;
  *
  * SOURCE
  */
-#if SILC_SIZEOF_LONG == 4
-typedef unsigned long SilcUInt32;
-typedef signed long SilcInt32;
-#else
 #if SILC_SIZEOF_INT == 4
 typedef unsigned int SilcUInt32;
 typedef signed int SilcInt32;
 #else
+#if SILC_SIZEOF_LONG == 4
+typedef unsigned long SilcUInt32;
+typedef signed long SilcInt32;
+#else
 #if SILC_SIZEOF_LONG_LONG >= 4
 #ifndef WIN32
 typedef unsigned long long SilcUInt32;
@@ -264,6 +256,30 @@ typedef SilcInt32 SilcInt64;
 typedef SilcUInt32 * void *;
 #endif
 
+/****d* silcutil/SILCTypes/SilcSocket
+ *
+ * NAME
+ *
+ *    SilcSocket
+ *
+ * DESCRIPTION
+ *
+ *    Platform specific socket.  On POSIX compliant systems this is simply
+ *    an integer, representing the socket. On other systems it is platform
+ *    specific socket context.  Access it only through routines that can
+ *    handle SilcSocket types, unless you know what you are doing.
+ *
+ * SOURCE
+ */
+#if defined(SILC_UNIX)
+typedef int SilcSocket;
+#elif defined(SILC_WIN32)
+typedef SOCKET SilcSocket;
+#elif defined(SILC_SYMBIAN)
+typedef void * SilcSocket;
+#endif
+/***/
+
 /* Macros */
 
 #define SILC_GET_WORD(cp) ((SilcUInt32)(SilcUInt8)(cp)[0]) << 24       \
@@ -285,8 +301,8 @@ typedef SilcUInt32 * void *;
  */
 #define SILC_GET16_MSB(l, cp)                          \
 do {                                                   \
-       (l) = ((SilcUInt32)(SilcUInt8)(cp)[0] << 8)     \
-           | ((SilcUInt32)(SilcUInt8)(cp)[1]);         \
+  (l) = ((SilcUInt32)(SilcUInt8)(cp)[0] << 8)          \
+    | ((SilcUInt32)(SilcUInt8)(cp)[1]);                        \
 } while(0)
 /***/
 
@@ -304,10 +320,10 @@ do {                                                      \
  */
 #define SILC_GET32_MSB(l, cp)                          \
 do {                                                   \
-       (l) = ((SilcUInt32)(SilcUInt8)(cp)[0]) << 24    \
-           | ((SilcUInt32)(SilcUInt8)(cp)[1] << 16)    \
-           | ((SilcUInt32)(SilcUInt8)(cp)[2] << 8)     \
-           | ((SilcUInt32)(SilcUInt8)(cp)[3]);         \
+  (l) = ((SilcUInt32)(SilcUInt8)(cp)[0]) << 24         \
+    | ((SilcUInt32)(SilcUInt8)(cp)[1] << 16)           \
+    | ((SilcUInt32)(SilcUInt8)(cp)[2] << 8)            \
+    | ((SilcUInt32)(SilcUInt8)(cp)[3]);                        \
 } while(0)
 /***/
 
@@ -325,8 +341,8 @@ do {                                                        \
  */
 #define SILC_GET64_MSB(l, cp)                                  \
 do {                                                           \
-       (l) = ((((SilcUInt64)SILC_GET_WORD((cp))) << 32) |      \
-             ((SilcUInt64)SILC_GET_WORD((cp) + 4)));           \
+  (l) = ((((SilcUInt64)SILC_GET_WORD((cp))) << 32) |           \
+        ((SilcUInt64)SILC_GET_WORD((cp) + 4)));                \
 } while(0)
 /***/
 
@@ -342,11 +358,15 @@ do {                                                              \
  *
  * SOURCE
  */
+#if defined(SILC_I486) && defined(__GNUC__)
+#define SILC_GET16_LSB(l, cp) (l) = (*(SilcUInt16 *)(cp))
+#else
 #define SILC_GET16_LSB(l, cp)                          \
 do {                                                   \
-       (l) = ((SilcUInt32)(SilcUInt8)(cp)[0])          \
-           | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8);    \
+  (l) = ((SilcUInt32)(SilcUInt8)(cp)[0])               \
+    | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8);           \
 } while(0)
+#endif /* SILC_I486 && __GNUC__ */
 /***/
 
 /****d* silcutil/SILCTypes/SILC_GET32_LSB
@@ -361,20 +381,28 @@ do {                                                      \
  *
  * SOURCE
  */
+#if defined(SILC_I486) && defined(__GNUC__)
+#define SILC_GET32_LSB(l, cp) (l) = (*(SilcUInt32 *)(cp))
+#else
 #define SILC_GET32_LSB(l, cp)                          \
 do {                                                   \
-       (l) = ((SilcUInt32)(SilcUInt8)(cp)[0])          \
-           | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8)     \
-           | ((SilcUInt32)(SilcUInt8)(cp)[2] << 16)    \
-           | ((SilcUInt32)(SilcUInt8)(cp)[3] << 24);   \
+  (l) = ((SilcUInt32)(SilcUInt8)(cp)[0])               \
+    | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8)            \
+    | ((SilcUInt32)(SilcUInt8)(cp)[2] << 16)           \
+    | ((SilcUInt32)(SilcUInt8)(cp)[3] << 24);          \
 } while(0)
+#endif /* SILC_I486 && __GNUC__ */
 
 /* Same as upper but XOR the result always. Special purpose macro. */
+#if defined(SILC_I486) && defined(__GNUC__)
+#define SILC_GET32_X_LSB(l, cp) (l) ^= (*(SilcUInt32 *)(cp))
+#else
 #define SILC_GET32_X_LSB(l, cp)                                \
-       (l) ^= ((SilcUInt32)(SilcUInt8)(cp)[0])         \
-           | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8)     \
-           | ((SilcUInt32)(SilcUInt8)(cp)[2] << 16)    \
-           | ((SilcUInt32)(SilcUInt8)(cp)[3] << 24)
+  (l) ^= ((SilcUInt32)(SilcUInt8)(cp)[0])              \
+    | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8)            \
+    | ((SilcUInt32)(SilcUInt8)(cp)[2] << 16)           \
+    | ((SilcUInt32)(SilcUInt8)(cp)[3] << 24)
+#endif /* SILC_I486 && __GNUC__ */
 /***/
 
 /****d* silcutil/SILCTypes/SILC_PUT16_MSB
@@ -391,8 +419,8 @@ do {                                                        \
  */
 #define SILC_PUT16_MSB(l, cp)                  \
 do {                                           \
-       (cp)[0] = (SilcUInt8)((l) >> 8);        \
-       (cp)[1] = (SilcUInt8)(l);               \
+  (cp)[0] = (SilcUInt8)((l) >> 8);             \
+  (cp)[1] = (SilcUInt8)(l);                    \
 } while(0)
 /***/
 
@@ -410,10 +438,10 @@ do {                                              \
  */
 #define SILC_PUT32_MSB(l, cp)                  \
 do {                                           \
-       (cp)[0] = (SilcUInt8)((l) >> 24);       \
-       (cp)[1] = (SilcUInt8)((l) >> 16);       \
-       (cp)[2] = (SilcUInt8)((l) >> 8);        \
-       (cp)[3] = (SilcUInt8)(l);               \
+  (cp)[0] = (SilcUInt8)((l) >> 24);            \
+  (cp)[1] = (SilcUInt8)((l) >> 16);            \
+  (cp)[2] = (SilcUInt8)((l) >> 8);             \
+  (cp)[3] = (SilcUInt8)(l);                    \
 } while(0)
 /***/
 
@@ -448,11 +476,15 @@ do {                                                              \
  *
  * SOURCE
  */
+#if defined(SILC_I486) && defined(__GNUC__)
+#define SILC_PUT16_LSB(l, cp) (*(SilcUInt16 *)(cp)) = (l)
+#else
 #define SILC_PUT16_LSB(l, cp)                  \
 do  {                                          \
-       (cp)[0] = (SilcUInt8)(l);               \
-       (cp)[1] = (SilcUInt8)((l) >> 8);        \
+  (cp)[0] = (SilcUInt8)(l);                    \
+  (cp)[1] = (SilcUInt8)((l) >> 8);             \
 } while(0)
+#endif /* SILC_I486 && __GNUC__ */
 /***/
 
 /****d* silcutil/SILCTypes/SILC_PUT32_LSB
@@ -467,13 +499,17 @@ do  {                                             \
  *
  * SOURCE
  */
+#if defined(SILC_I486) && defined(__GNUC__)
+#define SILC_PUT32_LSB(l, cp) (*(SilcUInt32 *)(cp)) = (l)
+#else
 #define SILC_PUT32_LSB(l, cp)                  \
 do {                                           \
-       (cp)[0] = (SilcUInt8)(l);               \
-       (cp)[1] = (SilcUInt8)((l) >> 8);        \
-       (cp)[2] = (SilcUInt8)((l) >> 16);       \
-       (cp)[3] = (SilcUInt8)((l) >> 24);       \
+  (cp)[0] = (SilcUInt8)(l);                    \
+  (cp)[1] = (SilcUInt8)((l) >> 8);             \
+  (cp)[2] = (SilcUInt8)((l) >> 16);            \
+  (cp)[3] = (SilcUInt8)((l) >> 24);            \
 } while(0)
+#endif /* SILC_I486 && __GNUC__ */
 /***/
 
 /****d* silcutil/SILCTypes/SILC_SWAB_16
@@ -598,4 +634,11 @@ do {                                               \
 #endif
 /***/
 
+typedef char __check_size1[sizeof(SilcInt8)   ==       1 ? 1 : -1];
+typedef char __check_size2[sizeof(SilcUInt8)  ==       1 ? 1 : -1];
+typedef char __check_size3[sizeof(SilcInt16)  ==       2 ? 1 : -1];
+typedef char __check_size4[sizeof(SilcUInt16) ==       2 ? 1 : -1];
+typedef char __check_size5[sizeof(SilcInt32)  ==       4 ? 1 : -1];
+typedef char __check_size6[sizeof(SilcUInt32) ==       4 ? 1 : -1];
+
 #endif /* SILCTYPES_H */