1 /*++ 2 3 Copyright (c) Microsoft Corporation 4 5 Module Name: 6 7 FxCollection.hpp 8 9 Abstract: 10 11 This module implements a simple collection class to operate on 12 objects derived from FxObject. 13 14 Author: 15 16 17 18 Environment: 19 20 Both kernel and user mode 21 22 Revision History: 23 24 --*/ 25 26 #ifndef _FXCOLLECTION_HPP_ 27 #define _FXCOLLECTION_HPP_ 28 29 class FxCollectionEntry : public FxStump { 30 31 friend FxCollection; 32 friend FxCollectionInternal; 33 34 protected: 35 FxCollectionEntry( 36 VOID 37 ) 38 { 39 } 40 41 public: 42 FxObject *m_Object; 43 44 LIST_ENTRY m_ListEntry; 45 46 public: 47 FxCollectionEntry* 48 Next( 49 VOID 50 ) 51 { 52 return CONTAINING_RECORD(m_ListEntry.Flink, FxCollectionEntry, m_ListEntry); 53 } 54 }; 55 56 struct FxCollectionInternal { 57 protected: 58 ULONG m_Count; 59 60 LIST_ENTRY m_ListHead; 61 62 public: 63 FxCollectionInternal( 64 VOID 65 ); 66 67 ~FxCollectionInternal( 68 VOID 69 ); 70 71 _Must_inspect_result_ 72 FxCollectionEntry* 73 FindEntry( 74 __in ULONG Index 75 ); 76 77 _Must_inspect_result_ 78 FxCollectionEntry* 79 FindEntryByObject( 80 __in FxObject* Object 81 ); 82 83 ULONG 84 Count( 85 VOID 86 ); 87 88 BOOLEAN 89 Add( 90 __in PFX_DRIVER_GLOBALS FxDriverGlobals, 91 __in FxObject *Item 92 ); 93 94 _Must_inspect_result_ 95 FxObject * 96 GetItem( 97 __in ULONG Index 98 ); 99 100 _Must_inspect_result_ 101 FxObject* 102 GetFirstItem( 103 VOID 104 ); 105 106 _Must_inspect_result_ 107 FxObject* 108 GetLastItem( 109 VOID 110 ); 111 112 NTSTATUS 113 Remove( 114 __in ULONG Index 115 ); 116 117 VOID 118 CleanupEntry( 119 __in FxCollectionEntry* Entry 120 ); 121 122 VOID 123 CleanupEntryObject( 124 __in FxObject* Object 125 ) 126 { 127 Object->RELEASE(this); 128 } 129 130 NTSTATUS 131 RemoveEntry( 132 __in FxCollectionEntry* Entry 133 ); 134 135 _Must_inspect_result_ 136 NTSTATUS 137 RemoveItem( 138 __in FxObject* Item 139 ); 140 141 _Must_inspect_result_ 142 FxCollectionEntry* 143 Start( 144 VOID 145 ) 146 { 147 return CONTAINING_RECORD(m_ListHead.Flink, FxCollectionEntry, m_ListEntry); 148 } 149 150 _Must_inspect_result_ 151 FxCollectionEntry* 152 End( 153 VOID 154 ) 155 { 156 return CONTAINING_RECORD(&m_ListHead, FxCollectionEntry, m_ListEntry); 157 } 158 159 VOID 160 Clear( 161 VOID 162 ); 163 164 protected: 165 _Must_inspect_result_ 166 FxCollectionEntry* 167 AllocateEntry( 168 __in PFX_DRIVER_GLOBALS FxDriverGlobals 169 ) 170 { 171 return new(FxDriverGlobals) FxCollectionEntry(); 172 } 173 174 VOID 175 AddEntry( 176 __in FxCollectionEntry *Node, 177 __in FxObject* Item 178 ) 179 { 180 Node->m_Object = Item; 181 182 // 183 // Refcount the item we are adding to the list. 184 // 185 Item->ADDREF(this); 186 187 // 188 // Increment the number of items in the collection. 189 // 190 m_Count++; 191 } 192 }; 193 194 class FxCollection : public FxNonPagedObject, public FxCollectionInternal { 195 196 public: 197 FxCollection( 198 __in PFX_DRIVER_GLOBALS FxDriverGlobals 199 ); 200 201 ~FxCollection( 202 VOID 203 ); 204 205 BOOLEAN 206 Add( 207 __in FxObject *Item 208 ) 209 { 210 return FxCollectionInternal::Add(GetDriverGlobals(), Item); 211 } 212 213 VOID 214 StealCollection( 215 __in FxCollection* Collection 216 ); 217 218 protected: 219 FxCollection( 220 __in PFX_DRIVER_GLOBALS FxDriverGlobals, 221 __in WDFTYPE Type, 222 __in USHORT Size 223 ); 224 225 }; 226 227 #endif // _FXCOLLECTION_HPP_ 228