- Add DDK alignment macros and move/rename the ones in the NDK for user-mode only...
[reactos.git] / reactos / include / reactos / helper.h
1 #ifndef _HELPER_H
2 #define _HELPER_H
3
4 #ifndef ROUND_UP
5 #define ROUND_UP(n, align) \
6 ROUND_DOWN(((ULONG)n) + (align) - 1, (align))
7 #endif
8
9 #ifndef ROUND_DOWN
10 #define ROUND_DOWN(n, align) \
11 (((ULONG)n) & ~((align) - 1l))
12 #endif
13
14 #ifndef ROUNDUP
15 #define ROUNDUP ROUND_UP
16 #endif
17
18 #ifndef ROUNDDOWN
19 #define ROUNDDOWN ROUND_DOWN
20 #endif
21
22 #ifndef PAGE_ROUND_DOWN
23 #define PAGE_ROUND_DOWN(x) (((ULONG)x)&(~(PAGE_SIZE-1)))
24 #endif
25
26 #ifndef PAGE_ROUND_UP
27 #define PAGE_ROUND_UP(x) ( (((ULONG)x)%PAGE_SIZE) ? ((((ULONG)x)&(~(PAGE_SIZE-1)))+PAGE_SIZE) : ((ULONG)x) )
28 #endif
29
30 #define ABS_VALUE(V) (((V) < 0) ? -(V) : (V))
31
32 #define RtlRosMin3(X,Y,Z) (((X) < (Y)) ? RtlRosMin(X,Z) : RtlRosMin(Y,Z))
33
34 #ifndef KEBUGCHECK
35 #define KEBUGCHECKEX(a,b,c,d,e) DbgPrint("KeBugCheckEx at %s:%i\n",__FILE__,__LINE__), KeBugCheckEx(a,b,c,d,e)
36 #define KEBUGCHECK(a) DbgPrint("KeBugCheck at %s:%i\n",__FILE__,__LINE__), KeBugCheck(a)
37 #endif
38
39 /* iterate through the list using a list entry.
40 * elem is set to NULL if the list is run thru without breaking out or if list is empty.
41 */
42 #define LIST_FOR_EACH(elem, list, type, field) \
43 for ((elem) = CONTAINING_RECORD((list)->Flink, type, field); \
44 &(elem)->field != (list) || (elem = NULL); \
45 (elem) = CONTAINING_RECORD((elem)->field.Flink, type, field))
46
47 /* iterate through the list using a list entry, with safety against removal
48 * elem is set to NULL if the list is run thru without breaking out or if list is empty.
49 */
50 #define LIST_FOR_EACH_SAFE(cursor, cursor2, list, type, field) \
51 for ((cursor) = CONTAINING_RECORD((list)->Flink, type, field), \
52 (cursor2) = CONTAINING_RECORD((cursor)->field.Flink, type, field); \
53 &(cursor)->field != (list) || (cursor = NULL); \
54 (cursor) = (cursor2), \
55 (cursor2) = CONTAINING_RECORD((cursor)->field.Flink, type, field))
56
57 #define OPTHDROFFSET(a) ((LPVOID)((BYTE *)a + \
58 ((PIMAGE_DOS_HEADER)a)->e_lfanew + \
59 sizeof (IMAGE_NT_SIGNATURE) + \
60 sizeof (IMAGE_FILE_HEADER)))
61 #ifndef TAG
62 #define TAG(A, B, C, D) (ULONG)(((A)<<0) + ((B)<<8) + ((C)<<16) + ((D)<<24))
63 #endif
64 #define RVA(m, b) ((PVOID)((ULONG_PTR)(b) + (ULONG_PTR)(m)))
65 #define NTSTAT_SEVERITY_SHIFT 30
66 #define NTSTAT_SEVERITY_MASK 0x00000003
67 #define NTSTAT_FACILITY_SHIFT 16
68 #define NTSTAT_FACILITY_MASK 0x00000FFF
69 #define NTSTAT_CUSTOMER_MASK 0x20000000
70 #define NT_SEVERITY(StatCode) (((StatCode) >> NTSTAT_SEVERITY_SHIFT) & NTSTAT_SEVERITY_MASK)
71 #define NT_FACILITY(StatCode) (((StatCode) >> NTSTAT_FACILITY_SHIFT) & NTSTAT_FACILITY_MASK)
72 #define NT_CUSTOMER(StatCode) ((StatCode) & NTSTAT_CUSTOMER_MASK)
73 #define RELATIVE_TIME(wait) (-(wait))
74 #define NANOS_TO_100NS(nanos) (((LONGLONG)(nanos)) / 100)
75 #define MICROS_TO_100NS(micros) (((LONGLONG)(micros)) * NANOS_TO_100NS(1000))
76 #define MILLIS_TO_100NS(milli) (((LONGLONG)(milli)) * MICROS_TO_100NS(1000))
77 #define SECONDS_TO_100NS(seconds) (((LONGLONG)(seconds)) * MILLIS_TO_100NS(1000))
78 #define MINUTES_TO_100NS(minutes) (((LONGLONG)(minutes)) * SECONDS_TO_100NS(60))
79 #define HOURS_TO_100NS(hours) (((LONGLONG)(hours)) * MINUTES_TO_100NS(60))
80 #define UNICODIZE1(x) L##x
81 #define UNICODIZE(x) UNICODIZE1(x)
82 #define InsertAscendingListFIFO(ListHead, NewEntry, Type, ListEntryField, SortField)\
83 {\
84 PLIST_ENTRY current;\
85 \
86 current = (ListHead)->Flink;\
87 while (current != (ListHead))\
88 {\
89 if (CONTAINING_RECORD(current, Type, ListEntryField)->SortField >\
90 (NewEntry)->SortField)\
91 {\
92 break;\
93 }\
94 current = current->Flink;\
95 }\
96 \
97 InsertTailList(current, &((NewEntry)->ListEntryField));\
98 }
99
100 #define InsertDescendingListFIFO(ListHead, NewEntry, Type, ListEntryField, SortField)\
101 {\
102 PLIST_ENTRY current;\
103 \
104 current = (ListHead)->Flink;\
105 while (current != (ListHead))\
106 {\
107 if (CONTAINING_RECORD(current, Type, ListEntryField)->SortField <\
108 (NewEntry)->SortField)\
109 {\
110 break;\
111 }\
112 current = current->Flink;\
113 }\
114 \
115 InsertTailList(current, &((NewEntry)->ListEntryField));\
116 }
117
118 #define InsertAscendingList(ListHead, NewEntry, Type, ListEntryField, SortField)\
119 {\
120 PLIST_ENTRY current;\
121 \
122 current = (ListHead)->Flink;\
123 while (current != (ListHead))\
124 {\
125 if (CONTAINING_RECORD(current, Type, ListEntryField)->SortField >=\
126 (NewEntry)->SortField)\
127 {\
128 break;\
129 }\
130 current = current->Flink;\
131 }\
132 \
133 InsertTailList(current, &((NewEntry)->ListEntryField));\
134 }
135
136 #define InsertDescendingList(ListHead, NewEntry, Type, ListEntryField, SortField)\
137 {\
138 PLIST_ENTRY current;\
139 \
140 current = (ListHead)->Flink;\
141 while (current != (ListHead))\
142 {\
143 if (CONTAINING_RECORD(current, Type, ListEntryField)->SortField <=\
144 (NewEntry)->SortField)\
145 {\
146 break;\
147 }\
148 current = current->Flink;\
149 }\
150 \
151 InsertTailList(current, &((NewEntry)->ListEntryField));\
152 }
153
154 #endif