- Add DDK alignment macros and move/rename the ones in the NDK for user-mode only...
[reactos.git] / reactos / include / reactos / helper.h
index 959c48f..c7d933e 100644 (file)
@@ -1,31 +1,66 @@
 #ifndef _HELPER_H
 #define _HELPER_H
 
-/* FIXME: clean this mess up and move to NDK */
-#define ROUNDUP(a,b)   ((((a)+(b)-1)/(b))*(b))
-#define ROUNDDOWN(a,b) (((a)/(b))*(b))
-#define ROUND_UP ROUNDUP
-#define ROUND_DOWN ROUNDDOWN
+#ifndef ROUND_UP
+#define ROUND_UP(n, align) \
+    ROUND_DOWN(((ULONG)n) + (align) - 1, (align))
+#endif
+
+#ifndef ROUND_DOWN
+#define ROUND_DOWN(n, align) \
+    (((ULONG)n) & ~((align) - 1l))
+#endif
+
+#ifndef ROUNDUP
+#define ROUNDUP ROUND_UP
+#endif
+
+#ifndef ROUNDDOWN
+#define ROUNDDOWN ROUND_DOWN
+#endif
+
+#ifndef PAGE_ROUND_DOWN
 #define PAGE_ROUND_DOWN(x) (((ULONG)x)&(~(PAGE_SIZE-1)))
+#endif
+
+#ifndef PAGE_ROUND_UP
 #define PAGE_ROUND_UP(x) ( (((ULONG)x)%PAGE_SIZE) ? ((((ULONG)x)&(~(PAGE_SIZE-1)))+PAGE_SIZE) : ((ULONG)x) )
+#endif
+
 #define ABS_VALUE(V) (((V) < 0) ? -(V) : (V))
-#define RtlRosMin(X,Y) (((X) < (Y))? (X) : (Y))
+
 #define RtlRosMin3(X,Y,Z) (((X) < (Y)) ? RtlRosMin(X,Z) : RtlRosMin(Y,Z))
+
+#ifndef KEBUGCHECK
 #define KEBUGCHECKEX(a,b,c,d,e) DbgPrint("KeBugCheckEx at %s:%i\n",__FILE__,__LINE__), KeBugCheckEx(a,b,c,d,e)
 #define KEBUGCHECK(a) DbgPrint("KeBugCheck at %s:%i\n",__FILE__,__LINE__), KeBugCheck(a)
-#define EXPORTED __declspec(dllexport)
-#define IMPORTED __declspec(dllimport)
-#define LIST_FOR_EACH(entry, head) \
-   for(entry = (head)->Flink; entry != (head); entry = entry->Flink)
-#define LIST_FOR_EACH_SAFE(tmp_entry, head, ptr, type, field) \
-   for ((tmp_entry)=(head)->Flink; (tmp_entry)!=(head) && \
-        ((ptr) = CONTAINING_RECORD(tmp_entry,type,field)) && \
-        ((tmp_entry) = (tmp_entry)->Flink); )
+#endif
+
+/* iterate through the list using a list entry. 
+ * elem is set to NULL if the list is run thru without breaking out or if list is empty.
+ */
+#define LIST_FOR_EACH(elem, list, type, field) \
+    for ((elem) = CONTAINING_RECORD((list)->Flink, type, field); \
+         &(elem)->field != (list) || (elem = NULL); \
+         (elem) = CONTAINING_RECORD((elem)->field.Flink, type, field))
+         
+/* iterate through the list using a list entry, with safety against removal 
+ * elem is set to NULL if the list is run thru without breaking out or if list is empty.
+ */
+#define LIST_FOR_EACH_SAFE(cursor, cursor2, list, type, field) \
+    for ((cursor) = CONTAINING_RECORD((list)->Flink, type, field), \
+         (cursor2) = CONTAINING_RECORD((cursor)->field.Flink, type, field); \
+         &(cursor)->field != (list) || (cursor = NULL); \
+         (cursor) = (cursor2), \
+         (cursor2) = CONTAINING_RECORD((cursor)->field.Flink, type, field))
+         
 #define OPTHDROFFSET(a) ((LPVOID)((BYTE *)a                 +  \
                         ((PIMAGE_DOS_HEADER)a)->e_lfanew    +  \
                         sizeof (IMAGE_NT_SIGNATURE)                 +  \
                         sizeof (IMAGE_FILE_HEADER)))
+#ifndef TAG
 #define TAG(A, B, C, D) (ULONG)(((A)<<0) + ((B)<<8) + ((C)<<16) + ((D)<<24))
+#endif
 #define RVA(m, b) ((PVOID)((ULONG_PTR)(b) + (ULONG_PTR)(m)))
 #define NTSTAT_SEVERITY_SHIFT 30
 #define NTSTAT_SEVERITY_MASK  0x00000003
@@ -44,7 +79,7 @@
 #define HOURS_TO_100NS(hours) (((LONGLONG)(hours)) * MINUTES_TO_100NS(60))
 #define UNICODIZE1(x) L##x
 #define UNICODIZE(x) UNICODIZE1(x)
-#define InsertAscendingListFIFO(ListHead, Type, ListEntryField, NewEntry, SortField)\
+#define InsertAscendingListFIFO(ListHead, NewEntry, Type, ListEntryField, SortField)\
 {\
   PLIST_ENTRY current;\
 \
@@ -62,7 +97,7 @@
   InsertTailList(current, &((NewEntry)->ListEntryField));\
 }
 
-#define InsertDescendingListFIFO(ListHead, Type, ListEntryField, NewEntry, SortField)\
+#define InsertDescendingListFIFO(ListHead, NewEntry, Type, ListEntryField, SortField)\
 {\
   PLIST_ENTRY current;\
 \
   InsertTailList(current, &((NewEntry)->ListEntryField));\
 }
 
-#define InsertAscendingList(ListHead, Type, ListEntryField, NewEntry, SortField)\
+#define InsertAscendingList(ListHead, NewEntry, Type, ListEntryField, SortField)\
 {\
   PLIST_ENTRY current;\
 \
   InsertTailList(current, &((NewEntry)->ListEntryField));\
 }
 
-#define InsertDescendingList(ListHead, Type, ListEntryField, NewEntry, SortField)\
+#define InsertDescendingList(ListHead, NewEntry, Type, ListEntryField, SortField)\
 {\
   PLIST_ENTRY current;\
 \