/*
- * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
+ * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
*
* This file is part of libFirm.
*
* @file
* @date 31.05.2005
* @author Sebastian Hack
- * @brief Some utility macros.
+ * @brief Miscellaneous utility macros.
*/
#ifndef FIRM_ADT_UTIL_H
#define FIRM_ADT_UTIL_H
-#include "firm_config.h"
-
-/**
- * Get the offset of a member of a struct.
- * @param type The type of the struct member is in.
- * @param member The name of the member.
- * @return The offset of member in type in bytes.
- */
-#define offset_of(type, member) \
- ((char *) &(((type *) 0)->member) - (char *) 0)
+#include <stddef.h>
/**
* Make pointer to the struct from a pointer to a member of that struct.
* @param member The name of the member.
* @return A pointer to the struct member is in.
*/
-#define container_of(ptr, type, member) \
- ((type *) ((char *) (ptr) - offset_of(type, member)))
+#define firm_container_of(ptr, type, member) \
+ ((type *) ((char *) (ptr) - offsetof(type, member)))
/**
- * Get the number of elements of a static array.
- * @param arr The static array.
- * @return The number of elements in that array.
+ * Returns size of a static array. Warning: This returns invalid values for
+ * dynamically allocated arrays.
+ *
+ * @param a static array
*/
-#define array_size(arr) \
- (sizeof(arr) / sizeof((arr)[0]))
+#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
+
+#undef MIN
+#undef MAX
+#define MAX(x, y) ((x) > (y) ? (x) : (y))
+#define MIN(x, y) ((x) < (y) ? (x) : (y))
/**
- * Asserts that the constant expression x is not zero at compiletime. name has
- * to be a unique identifier.
- *
- * @note This uses the fact, that double case labels are not allowed.
+ * Three valued compare as demanded by e.g. qsort(3)
+ * @param c A number.
+ * @param d Another number.
+ * @return 0 if c == d, -1 if c < d, 1 if c > d.
*/
-#define COMPILETIME_ASSERT(x, name) \
- static __attribute__((unused)) void compiletime_assert_##name (int h) { \
- switch(h) { case 0: case (x): ; } \
- }
+#define QSORT_CMP(c, d) (((c) > (d)) - ((c) < (d)))
-#ifdef __GNUC__
/**
- * Indicates to the compiler that the value of x is very likely 1
- * @note Only use this in speed critical code and when you are sure x is often 1
+ * convert an integer into pointer
*/
-#define LIKELY(x) __builtin_expect((x), 1)
+#define INT_TO_PTR(v) ((void *)((char *)0 + (v)))
/**
- * Indicates to the compiler that it's very likely that x is 0
- * @note Only use this in speed critical code and when you are sure x is often 0
+ * convert a pointer into an integer
*/
-#define UNLIKELY(x) __builtin_expect((x), 0)
-#else
-#define LIKELY(x) x
-#define UNLIKELY(x) x
-#endif
+#define PTR_TO_INT(v) (((char *)(v) - (char *)0))
#endif