Managed vs Unmanaged code. Round 1. (Part 3) (не холивара для, а справедливости ради)

Nov 02, 2010 21:35

Консольное приложение (С++) продолжение... н-да.

DoublyLinkedLists.h(заюзал двусвязные списки, хидер для хеш-таблиц куда-то потерял :) )


  1. /* DoublyLinkedLists.h

  2. Coded by Йа(c)

  3.  

  4. !!! IMPORTANT !!!

  5. This module not ready for use in kernel. Code not checked, used for testing only. Use it on your own risk. Preferable use native macros from ntddk.h

  6.  

  7. Doubly-linked lists functionality module. Used for memory leaking checks in kernel mode.

  8. Recommended for using with KrnlMem.h from same folder where you found this file.

  9.  

  10. Notes:

  11. ListHead -

  12.  

  13. GetItemsCount - doesn't have to be explained.

  14. InitializeListHead - must be called before any processing.

  15. AddItemOnTopOfList - doesn't have to be explained.

  16. AddItemOnBottomOfList - doesn't have to be explained.

  17. RemoveItemFromList - just remove entry from list, you have to free memory by yourself

  18. GetStructurePtrByItem - By setting OffsetEntryFromBegin = offset of WDListEntry in struct you can retreive pointer to your item

  19. PushItemToList - doesn't have to be explained.

  20. PopItemFromList - doesn't have to be explained.

  21. MoveItemForward - doesn't have to be explained.

  22. MoveItemBackward - doesn't have to be explained.

  23. IsWdListEmpty - doesn't have to be explained.

  24. ClearList - cuz you can't free memory manualy provided parameter EntrySize can free memory to avoid memory leaks.

  25. 29.08.10

  26. InsertBeforeCurrent - doesn't have to be explained.

  27. InsertAfterCurrent - doesn't have to be explained.

  28. */

  29. #pragma once

  30.  

  31. //#define _USE_KRNL_HEADERS

  32.  

  33.  

  34. #ifdef _USE_KRNL_HEADERS

  35. extern "C"

  36. {

  37. #include "ntddk.h"

  38. #include "types.h"

  39. }

  40. #else

  41. //#define CONTAINING_RECORD(address, type, field) ((type *)((PCHAR)(address) - (ULONG_PTR)(&((type *)0)->field)))

  42. #endif

  43.  

  44. typedef struct _WD_LIST_ENTRY

  45. {

  46. struct _WD_LIST_ENTRY* Flink;

  47. struct _WD_LIST_ENTRY* Blink;

  48. }WD_LIST_ENTRY, *PWD_LIST_ENTRY;

  49.  

  50. class TWdDoublyLinkedList

  51. {

  52. private:

  53. bool Initialized;

  54. DWORD dwItemsCount;

  55. public:

  56. WD_LIST_ENTRY ListHead;

  57. #ifndef _USE_KRNL_HEADERS

  58. TWdDoublyLinkedList()

  59. {

  60. InitializeListHead();

  61. Initialized = TRUE;

  62. }

  63. #endif

  64. DWORD GetItemsCount()

  65. {

  66. return dwItemsCount;

  67. }

  68. __declspec(property(get=GetItemsCount))DWORD ItemsCount;

  69.  

  70. void InitializeListHead()

  71. {

  72. ListHead.Flink = &ListHead;

  73. ListHead.Blink = &ListHead;

  74.  

  75. dwItemsCount = 0;

  76. return;

  77. }

  78. void AddItemOnTopOfList(PWD_LIST_ENTRY pListEntry)

  79. {

  80. PWD_LIST_ENTRY pFirstEntry = ListHead.Flink;

  81. ListHead.Flink = pListEntry;

  82. pListEntry->Blink = &ListHead;

  83. pListEntry->Flink = pFirstEntry;

  84. pFirstEntry->Blink = pListEntry;

  85.  

  86. dwItemsCount++;

  87. return;

  88. }

  89. void AddItemOnBottomOfList(PWD_LIST_ENTRY pListEntry)

  90. {

  91. PWD_LIST_ENTRY pLastEntry = ListHead.Blink;

  92. ListHead.Blink = pListEntry;

  93. pListEntry->Flink = &ListHead;

  94. pListEntry->Blink = pLastEntry;

  95. pLastEntry->Flink = pListEntry;

  96.  

  97. dwItemsCount++;

  98. return;

  99. }

  100. void RemoveItemFromList(PWD_LIST_ENTRY pListEntry)

  101. {

  102. PWD_LIST_ENTRY pPrevEntry = pListEntry->Blink;

  103. PWD_LIST_ENTRY pNextEntry = pListEntry->Flink;

  104. pPrevEntry->Flink = pNextEntry;

  105. pNextEntry->Blink = pPrevEntry;

  106.  

  107. dwItemsCount--;

  108. return;

  109. }

  110. void InsertBeforeCurrent(PWD_LIST_ENTRY pCurrentEntry, PWD_LIST_ENTRY pInsertEntry)

  111. {

  112. PWD_LIST_ENTRY pPrevEntry = pCurrentEntry->Blink;

  113. pInsertEntry->Blink = pPrevEntry;

  114. pInsertEntry->Flink = pCurrentEntry;

  115. pPrevEntry->Flink = pInsertEntry;

  116. pCurrentEntry->Blink = pInsertEntry;

  117. }

  118. void InsertAfterCurrent(PWD_LIST_ENTRY pCurrentEntry, PWD_LIST_ENTRY pInsertEntry)

  119. {

  120. PWD_LIST_ENTRY pNextEntry = pCurrentEntry->Flink;

  121. pInsertEntry->Blink = pCurrentEntry;

  122. pInsertEntry->Flink = pNextEntry;

  123. pNextEntry->Blink = pInsertEntry;

  124. pCurrentEntry->Flink = pInsertEntry;

  125. }

  126. PVOID GetStructurePtrByItem(PWD_LIST_ENTRY pListEntry, DWORD OffsetEntryFromBegin)

  127. {

  128. if(pListEntry == &ListHead)return 0;

  129. return (PVOID)((DWORD)pListEntry - OffsetEntryFromBegin);

  130. }

  131. void PushItemToList(PWD_LIST_ENTRY pListEntry)

  132. {

  133. AddItemOnBottomOfList(pListEntry);

  134. return;

  135. }

  136. PWD_LIST_ENTRY PopItemFromList()

  137. {

  138. PWD_LIST_ENTRY Result = ListHead.Blink;

  139. RemoveItemFromList(Result);

  140. return Result;

  141. }

  142. void MoveItemForward(PWD_LIST_ENTRY pListEntry)

  143. {

  144. PWD_LIST_ENTRY pPrevItem = pListEntry->Blink;

  145. PWD_LIST_ENTRY pNextItem = pListEntry->Flink;

  146. PWD_LIST_ENTRY pNextNextItem = pNextItem->Flink;

  147. pNextNextItem->Blink = pListEntry;

  148. pListEntry->Flink = pNextNextItem;

  149. pListEntry->Blink = pNextItem;

  150. pNextItem->Flink = pListEntry;

  151. pNextItem->Blink = pPrevItem;

  152. pPrevItem->Flink = pNextItem;

  153. return;

  154. }

  155. void MoveItemBackward(PWD_LIST_ENTRY pListEntry)

  156. {

  157. PWD_LIST_ENTRY pPrevItem = pListEntry->Blink;

  158. PWD_LIST_ENTRY pPrevPrevItem = pPrevItem->Blink;

  159. PWD_LIST_ENTRY pNextItem = pListEntry->Flink;

  160. pPrevPrevItem->Flink = pListEntry;

  161. pListEntry->Blink = pPrevPrevItem;

  162. pListEntry->Flink = pPrevItem;

  163. pPrevItem->Blink = pListEntry;

  164. pPrevItem->Flink = pNextItem;

  165. pNextItem->Blink = pPrevItem;

  166. return;

  167. }

  168. bool IsWdListEmpty(void)

  169. {

  170. if((ListHead.Flink == &ListHead) && (ListHead.Blink == &ListHead))

  171. {

  172. return TRUE;

  173. }

  174. else return FALSE;

  175. }

  176. void ClearList(DWORD ListEntryOffset, DWORD EntrySize)

  177. {

  178. PWD_LIST_ENTRY CurrentEntry = ListHead.Flink;

  179. while (!IsWdListEmpty())

  180. {

  181. PVOID MemoryToFree = GetStructurePtrByItem(CurrentEntry, ListEntryOffset);

  182. RemoveItemFromList(CurrentEntry);

  183. CurrentEntry = CurrentEntry->Flink;

  184. #ifdef _USE_KRNL_HEADERS

  185. MmFreeNonCachedMemory(MemoryToFree, EntrySize);

  186. #else

  187. free(MemoryToFree);

  188. #endif

  189. }

  190. dwItemsCount = 0;

  191. }

  192. };
stdafx.h Не смешно. Один гений два дня дожидался пока не перешлю :)


  1. // stdafx.h : include file for standard system include files,

  2. // or project specific include files that are used frequently, but

  3. // are changed infrequently

  4. //

  5.  

  6. #pragma once

  7.  

  8. #include "targetver.h"

  9.  

  10. #include

  11. #include

  12. #include

  13.  

  14.  

  15. // TODO: reference additional headers your program requires here

  16.  
Все статьи
Part 1 Part 2 Part 3 Part 4

it, c#, c++, С++ vs С#

Previous post Next post
Up