1 #ifndef _HELPER_H
2 #define _HELPER_H
4 #ifndef ROUND_UP
5 #define ROUND_UP(n, align) \
6 ROUND_DOWN(((ULONG)n) + (align) - 1, (align))
7 #endif
9 #ifndef ROUND_DOWN
10 #define ROUND_DOWN(n, align) \
11 (((ULONG)n) & ~((align) - 1l))
12 #endif
14 #ifndef ROUNDUP
15 #define ROUNDUP ROUND_UP
16 #endif
18 #ifndef ROUNDDOWN
19 #define ROUNDDOWN ROUND_DOWN
20 #endif
22 #ifndef PAGE_ROUND_DOWN
23 #define PAGE_ROUND_DOWN(x) (((ULONG)x)&(~(PAGE_SIZE-1)))
24 #endif
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
30 #define ABS_VALUE(V) (((V) < 0) ? -(V) : (V))
32 #define RtlRosMin3(X,Y,Z) (((X) < (Y)) ? RtlRosMin(X,Z) : RtlRosMin(Y,Z))
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
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))
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))
57 #define OPTHDROFFSET(a) ((LPVOID)((BYTE *)a + \
59 sizeof (IMAGE_NT_SIGNATURE) + \
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
67 #define NTSTAT_FACILITY_SHIFT 16
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 \
88 {\
89 if (CONTAINING_RECORD(current, Type, ListEntryField)->SortField >\
90 (NewEntry)->SortField)\
91 {\
92 break;\
93 }\
95 }\
96 \
97 InsertTailList(current, &((NewEntry)->ListEntryField));\
98 }
100 #define InsertDescendingListFIFO(ListHead, NewEntry, Type, ListEntryField, SortField)\
101 {\
102 PLIST_ENTRY current;\
103 \
106 {\
107 if (CONTAINING_RECORD(current, Type, ListEntryField)->SortField <\
108 (NewEntry)->SortField)\
109 {\
110 break;\
111 }\
113 }\
114 \
115 InsertTailList(current, &((NewEntry)->ListEntryField));\
116 }
118 #define InsertAscendingList(ListHead, NewEntry, Type, ListEntryField, SortField)\
119 {\
120 PLIST_ENTRY current;\
121 \
124 {\
125 if (CONTAINING_RECORD(current, Type, ListEntryField)->SortField >=\
126 (NewEntry)->SortField)\
127 {\
128 break;\
129 }\
131 }\
132 \
133 InsertTailList(current, &((NewEntry)->ListEntryField));\
134 }
136 #define InsertDescendingList(ListHead, NewEntry, Type, ListEntryField, SortField)\
137 {\
138 PLIST_ENTRY current;\
139 \