Types: Added SilcCompare generic comparison function
[runtime.git] / lib / silcutil / silctypes.h
index eabb83f032537e6f6906463bcf8d336a55045da8..5d768d819ab70828b844996265ef97ce2befddc3 100644 (file)
@@ -17,7 +17,7 @@
 
 */
 
-/****h* silcutil/SILC Types
+/****h* silcutil/Types and Definitions
  *
  * DESCRIPTION
  *
@@ -44,9 +44,7 @@
 #error "size of the short must be 2 bytes"
 #endif
 
-/******************************* Public API *********************************/
-
-/****d* silcutil/SILCTypes/SilcBool
+/****d* silcutil/SilcBool
  *
  * NAME
  *
@@ -59,7 +57,7 @@
  ***/
 typedef unsigned char SilcBool;
 
-/****d* silcutil/SILCTypes/TRUE
+/****d* silcutil/TRUE
  *
  * NAME
  *
@@ -76,7 +74,7 @@ typedef unsigned char SilcBool;
 #endif
 /***/
 
-/****d* silcutil/SILCTypes/FALSE
+/****d* silcutil/FALSE
  *
  * NAME
  *
@@ -93,7 +91,7 @@ typedef unsigned char SilcBool;
 #endif
 /***/
 
-/****d* silcutil/SILCTypes/SilcUInt8
+/****d* silcutil/SilcUInt8
  *
  * NAME
  *
@@ -108,7 +106,7 @@ typedef unsigned char SilcBool;
 typedef unsigned char SilcUInt8;
 /***/
 
-/****d* silcutil/SILCTypes/SilcInt8
+/****d* silcutil/SilcInt8
  *
  * NAME
  *
@@ -123,7 +121,7 @@ typedef unsigned char SilcUInt8;
 typedef signed char SilcInt8;
 /***/
 
-/****d* silcutil/SILCTypes/SilcUInt16
+/****d* silcutil/SilcUInt16
  *
  * NAME
  *
@@ -138,7 +136,7 @@ typedef signed char SilcInt8;
 typedef unsigned short SilcUInt16;
 /***/
 
-/****d* silcutil/SILCTypes/SilcInt16
+/****d* silcutil/SilcInt16
  *
  * NAME
  *
@@ -153,7 +151,7 @@ typedef unsigned short SilcUInt16;
 typedef signed short SilcInt16;
 /***/
 
-/****d* silcutil/SILCTypes/SilcUInt32
+/****d* silcutil/SilcUInt32
  *
  * NAME
  *
@@ -183,7 +181,7 @@ typedef signed long long SilcInt32;
 #endif
 /***/
 
-/****d* silcutil/SILCTypes/SilcInt32
+/****d* silcutil/SilcInt32
  *
  * NAME
  *
@@ -195,7 +193,7 @@ typedef signed long long SilcInt32;
  *
  ***/
 
-/****d* silcutil/SILCTypes/SilcUInt64
+/****d* silcutil/SilcUInt64
  *
  * NAME
  *
@@ -227,7 +225,7 @@ typedef SilcInt32 SilcInt64;
 #endif
 /***/
 
-/****d* silcutil/SILCTypes/SilcInt64
+/****d* silcutil/SilcInt64
  *
  * NAME
  *
@@ -240,11 +238,37 @@ typedef SilcInt32 SilcInt64;
  *
  ***/
 
+/****d* silcutil/SilcFloat32
+ *
+ * NAME
+ *
+ *    typedef float SilcFloat32;
+ *
+ * DESCRIPTION
+ *
+ *    32-bit floating point number.
+ *
+ ***/
+typedef float SilcFloat32;
+
+/****d* silcutil/SilcFloat64
+ *
+ * NAME
+ *
+ *    typedef double SilcFloat64;
+ *
+ * DESCRIPTION
+ *
+ *    64-bit floating point number.
+ *
+ ***/
+typedef double SilcFloat64;
+
 #if SILC_SIZEOF_VOID_P < 4
 typedef SilcUInt32 * void *;
 #endif
 
-/****d* silcutil/SILCTypes/SilcSocket
+/****d* silcutil/SilcSocket
  *
  * NAME
  *
@@ -255,7 +279,7 @@ typedef SilcUInt32 * void *;
  *    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.
+ *    handle SilcSocket types.
  *
  * SOURCE
  */
@@ -268,7 +292,7 @@ typedef void * SilcSocket;
 #endif
 /***/
 
-/****d* silcutil/SILCTypes/SilcParam
+/****d* silcutil/SilcParam
  *
  * NAME
  *
@@ -313,9 +337,47 @@ typedef SilcUInt32 SilcParam;
 #define SILC_PARAM_REGEX         109
 #define SILC_PARAM_OFFSET_START  110
 #define SILC_PARAM_OFFSET_END    111
-#define SILC_PARAM_APPEND        112
-#define SILC_PARAM_DELETE        113
+#define SILC_PARAM_DELETE        112
 #define SILC_PARAM_ALLOC         0x00010000     /* Allocate, bitmask */
+#define SILC_PARAM_REPLACE       0x00020000    /* Replace, bitmask */
+
+/****d* silcutil/SilcCompareValue
+ *
+ * NAME
+ *
+ *    typedef enum { ... } SilcCompareValue
+ *
+ * DESCRIPTION
+ *
+ *    Values that can be returned by the SilcCompare function.  Note that
+ *    not all routines may respect all of the return values.
+ *
+ * SOURCE
+ */
+typedef enum {
+  SILC_COMPARE_LESS_THAN_EQUAL_TO    = -2,  /* Value 1 <= value 2 */
+  SILC_COMPARE_LESS_THAN             = -1,  /* Value 1 < value 2 */
+  SILC_COMPARE_EQUAL_TO              = 0,   /* Value 1 == value 2 */
+  SILC_COMPARE_GREATER_THAN          = 1,   /* Value 1 > value 2 */
+  SILC_COMPARE_GREATER_THAN_EQUAL_TO = 2,   /* Value 1 >= value 2 */
+  SILC_COMPARE_STOP                  = 3,   /* Stop comparison */
+} SilcCompareValue;
+/***/
+
+/****f* silcutil/SilcCompare
+ *
+ * SYNOPSIS
+ *
+ *    typedef SilcCompareValue (*SilcCompare)(void *value1, void *value2,
+ *                                            void *context);
+ *
+ * DESCRIPTION
+ *
+ *   A comparison function used by many routines in SILC Runtime Toolkit.
+ *
+ ***/
+typedef SilcCompareValue (*SilcCompare)(void *value1, void *value2,
+                                       void *context);
 
 /* Macros */
 
@@ -334,7 +396,33 @@ typedef SilcUInt32 SilcParam;
                    | ((SilcUInt32)(SilcUInt8)(cp)[3])
 #endif /* (SILC_I486 || SILC_X86_64) && __GNUC__ */
 
-/****d* silcutil/SILCTypes/SILC_GET16_MSB
+/****d* silcutil/SILC_MAX
+ *
+ * NAME
+ *
+ *    #define SILC_MAX(a, b)
+ *
+ * DESCRIPTION
+ *
+ *    Return `a' if it is bigger than `b', otherwise return `b'.
+ *
+ ***/
+#define SILC_MAX(a, b) ((a) > (b) ? (a) : (b))
+
+/****d* silcutil/SILC_MIN
+ *
+ * NAME
+ *
+ *    #define SILC_MIN(a, b)
+ *
+ * DESCRIPTION
+ *
+ *    Return `a' if it is smaller than `b', otherwise return `b'.
+ *
+ ***/
+#define SILC_MIN(a, b) ((a) < (b) ? (a) : (b))
+
+/****d* silcutil/SILC_GET16_MSB
  *
  * NAME
  *
@@ -359,7 +447,7 @@ do {                                                        \
 #endif /* (SILC_I386 || SILC_X86_64) && __GNUC__ */
 /***/
 
-/****d* silcutil/SILCTypes/SILC_GET32_MSB
+/****d* silcutil/SILC_GET32_MSB
  *
  * NAME
  *
@@ -404,7 +492,7 @@ do {                                                                        \
     | ((SilcUInt32)(SilcUInt8)(cp)[3]);
 #endif /* (SILC_I486 || SILC_X86_64) && __GNUC__ */
 
-/****d* silcutil/SILCTypes/SILC_GET64_MSB
+/****d* silcutil/SILC_GET64_MSB
  *
  * NAME
  *
@@ -429,7 +517,7 @@ do {                                                                \
 #endif /* SILC_X86_64 && __GNUC__ */
 /***/
 
-/****d* silcutil/SILCTypes/SILC_GET16_LSB
+/****d* silcutil/SILC_GET16_LSB
  *
  * NAME
  *
@@ -452,7 +540,7 @@ do {                                                        \
 #endif /* SILC_I386 || SILC_X86_64 */
 /***/
 
-/****d* silcutil/SILCTypes/SILC_GET32_LSB
+/****d* silcutil/SILC_GET32_LSB
  *
  * NAME
  *
@@ -488,7 +576,7 @@ do {                                                        \
     | ((SilcUInt32)(SilcUInt8)(cp)[3] << 24)
 #endif /* SILC_I386 || SILC_X86_64 */
 
-/****d* silcutil/SILCTypes/SILC_PUT16_MSB
+/****d* silcutil/SILC_PUT16_MSB
  *
  * NAME
  *
@@ -513,7 +601,7 @@ do {                                                \
 #endif /* (SILC_I386 || SILC_X86_64) && __GNUC__ */
 /***/
 
-/****d* silcutil/SILCTypes/SILC_PUT32_MSB
+/****d* silcutil/SILC_PUT32_MSB
  *
  * NAME
  *
@@ -540,7 +628,7 @@ do {                                                \
 #endif /* (SILC_I486 || SILC_X86_64) && __GNUC__ */
 /***/
 
-/****d* silcutil/SILCTypes/SILC_PUT64_MSB
+/****d* silcutil/SILC_PUT64_MSB
  *
  * NAME
  *
@@ -565,7 +653,7 @@ do {                                                                \
 #endif /* SILC_X86_64 && __GNUC__ */
 /***/
 
-/****d* silcutil/SILCTypes/SILC_PUT16_LSB
+/****d* silcutil/SILC_PUT16_LSB
  *
  * NAME
  *
@@ -588,7 +676,7 @@ do  {                                               \
 #endif /* SILC_I386 || SILC_X86_64 */
 /***/
 
-/****d* silcutil/SILCTypes/SILC_PUT32_LSB
+/****d* silcutil/SILC_PUT32_LSB
  *
  * NAME
  *
@@ -613,7 +701,7 @@ do {                                                \
 #endif /* SILC_I386 || SILC_X86_64 */
 /***/
 
-/****d* silcutil/SILCTypes/SILC_SWAB_16
+/****d* silcutil/SILC_SWAB_16
  *
  * NAME
  *
@@ -640,7 +728,7 @@ do {                                                \
 #endif /* (SILC_I386 || SILC_X86_64) && __GNUC__ */
 /***/
 
-/****d* silcutil/SILCTypes/SILC_SWAB_32
+/****d* silcutil/SILC_SWAB_32
  *
  * NAME
  *
@@ -669,7 +757,7 @@ do {                                                \
 #endif /* (SILC_I486 || SILC_X86_64) && __GNUC__ */
 /***/
 
-/****d* silcutil/SILCTypes/SILC_PTR_TO_32
+/****d* silcutil/SILC_PTR_TO_32
  *
  * NAME
  *
@@ -691,7 +779,7 @@ do {                                                \
 #endif
 /***/
 
-/****d* silcutil/SILCTypes/SILC_PTR_TO_64
+/****d* silcutil/SILC_PTR_TO_64
  *
  * NAME
  *
@@ -712,7 +800,7 @@ do {                                                \
 #endif
 /***/
 
-/****d* silcutil/SILCTypes/SILC_32_TO_PTR
+/****d* silcutil/SILC_32_TO_PTR
  *
  * NAME
  *
@@ -733,7 +821,7 @@ do {                                                \
 #endif
 /***/
 
-/****d* silcutil/SILCTypes/SILC_64_TO_PTR
+/****d* silcutil/SILC_64_TO_PTR
  *
  * NAME
  *
@@ -755,7 +843,58 @@ do {                                               \
 #endif
 /***/
 
-/****d* silcutil/SILCTypes/silc_rol
+/****d* silcutil/SILC_ASSERT
+ *
+ * NAME
+ *
+ *    #define SILC_ASSERT(experssion)
+ *
+ * DESCRIPTION
+ *
+ *    Assert macro that prints error message to stderr and calls abort()
+ *    if the `expression' is false (ie. compares equal to zero).  If
+ *    SILC_DEBUG is not defined this macro has no effect.
+ *
+ * SOURCE
+ */
+#if defined(SILC_DEBUG)
+#define SILC_ASSERT(expr) assert((expr));
+#else
+#define SILC_ASSERT(expr) do { } while(0)
+#endif /* SILC_DEBUG */
+/***/
+
+/****d* silcutil/SILC_VERIFY
+ *
+ * NAME
+ *
+ *    #define SILC_VERIFY(experssion)
+ *
+ * DESCRIPTION
+ *
+ *    Verification macro that prints error message to stderr and calls
+ *    abort() if the `expression' is false (ie. compares equal to zero)
+ *    on debug builds (SILC_DEBUG defined), and prints error message to
+ *    stderr on release builds (SILC_DEBUG undefined) but does not abort().
+ *    This macro is always compiled even if debugging (SILC_DEBUG) is not
+ *    defined.
+ *
+ * SOURCE
+ */
+#if defined(SILC_DEBUG)
+#define SILC_VERIFY(expr) assert((expr));
+#else
+#define SILC_VERIFY(expr)                                              \
+  if (silc_unlikely(!(expr))) {                                                \
+    SILC_LOG_ERROR(("SILC_VERIFY %s:%s:%d",                            \
+                   __FILE__, __FUNCTION__, __LINE__));                 \
+    silc_set_errno_reason_nofail(SILC_ERR_ASSERT, "SILC_VERIFY %s:%s:%d", \
+                                __FILE__, __FUNCTION__, __LINE__);     \
+  }
+#endif /* SILC_DEBUG */
+/***/
+
+/****d* silcutil/silc_rol
  *
  * NAME
  *
@@ -772,14 +911,38 @@ static inline SilcUInt32 silc_rol(SilcUInt32 val, int num)
 {
 #if (defined(SILC_I386) || defined(SILC_X86_64)) && defined(__GNUC__)
   asm volatile ("roll %%cl, %0"
-               : "=q" (val) : "0" (val), "c" (num));
+               : "=r" (val) : "0" (val), "c" (num));
   return val;
 #else
   return ((val << (SilcUInt32)num) | (val >> (32 - (SilcUInt32)num)));
 #endif /* (SILC_I486 || SILC_X86_64) && __GNUC__ */
 }
 
-/****d* silcutil/SILCTypes/silc_ror
+/****d* silcutil/silc_rolc
+ *
+ * NAME
+ *
+ *    static inline SilcUInt32 silc_rolc(SilcUInt32 val, const int num);
+ *
+ * DESCRIPTION
+ *
+ *    Rotate 32-bit integer's bits to left `num' times.  Bits pushed to the
+ *    left will appear from the right side of the integer, thus rotating.
+ *    Returns the rotated value.
+ *
+ ***/
+static inline SilcUInt32 silc_rolc(SilcUInt32 val, const int num)
+{
+#if (defined(SILC_I386) || defined(SILC_X86_64)) && defined(__GNUC__)
+  asm volatile ("roll %2, %0"
+               : "=r" (val) : "0" (val), "I" (num));
+  return val;
+#else
+  return ((val << (SilcUInt32)num) | (val >> (32 - (SilcUInt32)num)));
+#endif /* (SILC_I486 || SILC_X86_64) && __GNUC__ */
+}
+
+/****d* silcutil/silc_ror
  *
  * NAME
  *
@@ -796,14 +959,38 @@ static inline SilcUInt32 silc_ror(SilcUInt32 val, int num)
 {
 #if (defined(SILC_I386) || defined(SILC_X86_64)) && defined(__GNUC__)
   asm volatile ("rorl %%cl, %0"
-               : "=q" (val) : "0" (val), "c" (num));
+               : "=r" (val) : "0" (val), "c" (num));
+  return val;
+#else
+  return ((val >> (SilcUInt32)num) | (val << (32 - (SilcUInt32)num)));
+#endif /* (SILC_I486 || SILC_X86_64) && __GNUC__ */
+}
+
+/****d* silcutil/silc_rorc
+ *
+ * NAME
+ *
+ *    static inline SilcUInt32 silc_ror(SilcUInt32 val, const int num);
+ *
+ * DESCRIPTION
+ *
+ *    Rotate 32-bit integer's bits to right `num' times.  Bits pushed to the
+ *    right will appear from the left side of the integer, thus rotating.
+ *    Returns the rotated value.
+ *
+ ***/
+static inline SilcUInt32 silc_rorc(SilcUInt32 val, const int num)
+{
+#if (defined(SILC_I386) || defined(SILC_X86_64)) && defined(__GNUC__)
+  asm volatile ("rorl %2, %0"
+               : "=r" (val) : "0" (val), "I" (num));
   return val;
 #else
   return ((val >> (SilcUInt32)num) | (val << (32 - (SilcUInt32)num)));
 #endif /* (SILC_I486 || SILC_X86_64) && __GNUC__ */
 }
 
-/****d* silcutil/SILCTypes/silc_rol64
+/****d* silcutil/silc_rol64
  *
  * NAME
  *
@@ -820,14 +1007,38 @@ static inline SilcUInt64 silc_rol64(SilcUInt64 val, int num)
 {
 #if defined(SILC_X86_64) && defined(__GNUC__)
   asm volatile ("rolq %%cl, %0"
-               : "=q" (val) : "0" (val), "c" (num));
+               : "=r" (val) : "0" (val), "c" (num));
+  return val;
+#else
+  return ((val << (SilcUInt64)num) | (val >> (64 - (SilcUInt64)num)));
+#endif /* SILC_X86_64 && __GNUC__ */
+}
+
+/****d* silcutil/silc_rolc64
+ *
+ * NAME
+ *
+ *    static inline SilcUInt64 silc_rolc64(SilcUInt64 val, const int num);
+ *
+ * DESCRIPTION
+ *
+ *    Rotate 64-bit integer's bits to left `num' times.  Bits pushed to the
+ *    left will appear from the right side of the integer, thus rotating.
+ *    Returns the rotated value.
+ *
+ ***/
+static inline SilcUInt64 silc_rolc64(SilcUInt64 val, const int num)
+{
+#if defined(SILC_X86_64) && defined(__GNUC__)
+  asm volatile ("rolq %2, %0"
+               : "=r" (val) : "0" (val), "J" (num));
   return val;
 #else
   return ((val << (SilcUInt64)num) | (val >> (64 - (SilcUInt64)num)));
 #endif /* SILC_X86_64 && __GNUC__ */
 }
 
-/****d* silcutil/SILCTypes/silc_ror64
+/****d* silcutil/silc_ror64
  *
  * NAME
  *
@@ -844,14 +1055,38 @@ static inline SilcUInt64 silc_ror64(SilcUInt64 val, int num)
 {
 #if defined(SILC_X86_64) && defined(__GNUC__)
   asm volatile ("rorq %%cl, %0"
-               : "=q" (val) : "0" (val), "c" (num));
+               : "=r" (val) : "0" (val), "c" (num));
+  return val;
+#else
+  return ((val >> (SilcUInt64)num) | (val << (64 - (SilcUInt64)num)));
+#endif /* SILC_X86_64 && __GNUC__ */
+}
+
+/****d* silcutil/silc_rorc64
+ *
+ * NAME
+ *
+ *    static inline SilcUInt64 silc_rorc64(SilcUInt64 val, const int num);
+ *
+ * DESCRIPTION
+ *
+ *    Rotate 64-bit integer's bits to right `num' times.  Bits pushed to the
+ *    right will appear from the left side of the integer, thus rotating.
+ *    Returns the rotated value.
+ *
+ ***/
+static inline SilcUInt64 silc_rorc64(SilcUInt64 val, const int num)
+{
+#if defined(SILC_X86_64) && defined(__GNUC__)
+  asm volatile ("rorq %2, %0"
+               : "=r" (val) : "0" (val), "J" (num));
   return val;
 #else
   return ((val >> (SilcUInt64)num) | (val << (64 - (SilcUInt64)num)));
 #endif /* SILC_X86_64 && __GNUC__ */
 }
 
-/****d* silcutil/SILCTypes/silc_offsetof
+/****d* silcutil/silc_offsetof
  *
  * NAME
  *
@@ -864,7 +1099,7 @@ static inline SilcUInt64 silc_ror64(SilcUInt64 val, int num)
  ***/
 #define silc_offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
 
-/****d* silcutil/SILCTypes/silc_attribute
+/****d* silcutil/silc_attribute
  *
  * NAME
  *
@@ -882,12 +1117,12 @@ static inline SilcUInt64 silc_ror64(SilcUInt64 val, int num)
  *
  ***/
 #if defined(__GNUC__)
-#define silc_attribute(attrlist) __attribute__(attrlist)
+#define silc_attribute(attrlist) __attribute__ (attrlist)
 #else
 #define silc_attribute(attrlist)
 #endif /* __GNUC__ */
 
-/****d* silcutil/SILCTypes/silc_likely
+/****d* silcutil/silc_likely
  *
  * NAME
  *
@@ -902,7 +1137,7 @@ static inline SilcUInt64 silc_ror64(SilcUInt64 val, int num)
  *
  ***/
 
-/****d* silcutil/SILCTypes/silc_unlikely
+/****d* silcutil/silc_unlikely
  *
  * NAME
  *
@@ -928,7 +1163,7 @@ static inline SilcUInt64 silc_ror64(SilcUInt64 val, int num)
    reading or writing if you think that the data will be needed soon after
    prefetching. */
 
-/****d* silcutil/SILCTypes/silc_prefetch
+/****d* silcutil/silc_prefetch
  *
  * NAME
  *
@@ -960,7 +1195,7 @@ void silc_prefetch(void *addr, int rw, int locality)
 #endif /* __GNUC__ */
 }
 
-/****d* silcutil/SILCTypes/silc_prefetch_block
+/****d* silcutil/silc_prefetch_block
  *
  * NAME
  *