Консольное приложение (С++) продолжение... н-да.
DoublyLinkedLists.h(заюзал двусвязные списки, хидер для хеш-таблиц куда-то потерял :) )
/* DoublyLinkedLists.h
Coded by Йа(c)
!!! IMPORTANT !!!
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
Doubly-linked lists functionality module. Used for memory leaking checks in kernel mode.
Recommended for using with KrnlMem.h from same folder where you found this file.
Notes:
ListHead -
GetItemsCount - doesn't have to be explained.
InitializeListHead - must be called before any processing.
AddItemOnTopOfList - doesn't have to be explained.
AddItemOnBottomOfList - doesn't have to be explained.
RemoveItemFromList - just remove entry from list, you have to free memory by yourself
GetStructurePtrByItem - By setting OffsetEntryFromBegin = offset of WDListEntry in struct you can retreive pointer to your item
PushItemToList - doesn't have to be explained.
PopItemFromList - doesn't have to be explained.
MoveItemForward - doesn't have to be explained.
MoveItemBackward - doesn't have to be explained.
IsWdListEmpty - doesn't have to be explained.
ClearList - cuz you can't free memory manualy provided parameter EntrySize can free memory to avoid memory leaks.
29.08.10
InsertBeforeCurrent - doesn't have to be explained.
InsertAfterCurrent - doesn't have to be explained.
*/
#pragma once
//#define _USE_KRNL_HEADERS
#ifdef _USE_KRNL_HEADERS
extern "C"
{
#include "ntddk.h"
#include "types.h"
}
#else
//#define CONTAINING_RECORD(address, type, field) ((type *)((PCHAR)(address) - (ULONG_PTR)(&((type *)0)->field)))
#endif
typedef struct _WD_LIST_ENTRY
{
struct _WD_LIST_ENTRY* Flink;
struct _WD_LIST_ENTRY* Blink;
}WD_LIST_ENTRY, *PWD_LIST_ENTRY;
class TWdDoublyLinkedList
{
private:
bool Initialized;
DWORD dwItemsCount;
public:
WD_LIST_ENTRY ListHead;
#ifndef _USE_KRNL_HEADERS
TWdDoublyLinkedList()
{
InitializeListHead();
Initialized = TRUE;
}
#endif
DWORD GetItemsCount()
{
return dwItemsCount;
}
__declspec(property(get=GetItemsCount))DWORD ItemsCount;
void InitializeListHead()
{
ListHead.Flink = &ListHead;
ListHead.Blink = &ListHead;
dwItemsCount = 0;
return;
}
void AddItemOnTopOfList(PWD_LIST_ENTRY pListEntry)
{
PWD_LIST_ENTRY pFirstEntry = ListHead.Flink;
ListHead.Flink = pListEntry;
pListEntry->Blink = &ListHead;
pListEntry->Flink = pFirstEntry;
pFirstEntry->Blink = pListEntry;
dwItemsCount++;
return;
}
void AddItemOnBottomOfList(PWD_LIST_ENTRY pListEntry)
{
PWD_LIST_ENTRY pLastEntry = ListHead.Blink;
ListHead.Blink = pListEntry;
pListEntry->Flink = &ListHead;
pListEntry->Blink = pLastEntry;
pLastEntry->Flink = pListEntry;
dwItemsCount++;
return;
}
void RemoveItemFromList(PWD_LIST_ENTRY pListEntry)
{
PWD_LIST_ENTRY pPrevEntry = pListEntry->Blink;
PWD_LIST_ENTRY pNextEntry = pListEntry->Flink;
pPrevEntry->Flink = pNextEntry;
pNextEntry->Blink = pPrevEntry;
dwItemsCount--;
return;
}
void InsertBeforeCurrent(PWD_LIST_ENTRY pCurrentEntry, PWD_LIST_ENTRY pInsertEntry)
{
PWD_LIST_ENTRY pPrevEntry = pCurrentEntry->Blink;
pInsertEntry->Blink = pPrevEntry;
pInsertEntry->Flink = pCurrentEntry;
pPrevEntry->Flink = pInsertEntry;
pCurrentEntry->Blink = pInsertEntry;
}
void InsertAfterCurrent(PWD_LIST_ENTRY pCurrentEntry, PWD_LIST_ENTRY pInsertEntry)
{
PWD_LIST_ENTRY pNextEntry = pCurrentEntry->Flink;
pInsertEntry->Blink = pCurrentEntry;
pInsertEntry->Flink = pNextEntry;
pNextEntry->Blink = pInsertEntry;
pCurrentEntry->Flink = pInsertEntry;
}
PVOID GetStructurePtrByItem(PWD_LIST_ENTRY pListEntry, DWORD OffsetEntryFromBegin)
{
if(pListEntry == &ListHead)return 0;
return (PVOID)((DWORD)pListEntry - OffsetEntryFromBegin);
}
void PushItemToList(PWD_LIST_ENTRY pListEntry)
{
AddItemOnBottomOfList(pListEntry);
return;
}
PWD_LIST_ENTRY PopItemFromList()
{
PWD_LIST_ENTRY Result = ListHead.Blink;
RemoveItemFromList(Result);
return Result;
}
void MoveItemForward(PWD_LIST_ENTRY pListEntry)
{
PWD_LIST_ENTRY pPrevItem = pListEntry->Blink;
PWD_LIST_ENTRY pNextItem = pListEntry->Flink;
PWD_LIST_ENTRY pNextNextItem = pNextItem->Flink;
pNextNextItem->Blink = pListEntry;
pListEntry->Flink = pNextNextItem;
pListEntry->Blink = pNextItem;
pNextItem->Flink = pListEntry;
pNextItem->Blink = pPrevItem;
pPrevItem->Flink = pNextItem;
return;
}
void MoveItemBackward(PWD_LIST_ENTRY pListEntry)
{
PWD_LIST_ENTRY pPrevItem = pListEntry->Blink;
PWD_LIST_ENTRY pPrevPrevItem = pPrevItem->Blink;
PWD_LIST_ENTRY pNextItem = pListEntry->Flink;
pPrevPrevItem->Flink = pListEntry;
pListEntry->Blink = pPrevPrevItem;
pListEntry->Flink = pPrevItem;
pPrevItem->Blink = pListEntry;
pPrevItem->Flink = pNextItem;
pNextItem->Blink = pPrevItem;
return;
}
bool IsWdListEmpty(void)
{
if((ListHead.Flink == &ListHead) && (ListHead.Blink == &ListHead))
{
return TRUE;
}
else return FALSE;
}
void ClearList(DWORD ListEntryOffset, DWORD EntrySize)
{
PWD_LIST_ENTRY CurrentEntry = ListHead.Flink;
while (!IsWdListEmpty())
{
PVOID MemoryToFree = GetStructurePtrByItem(CurrentEntry, ListEntryOffset);
RemoveItemFromList(CurrentEntry);
CurrentEntry = CurrentEntry->Flink;
#ifdef _USE_KRNL_HEADERS
MmFreeNonCachedMemory(MemoryToFree, EntrySize);
#else
free(MemoryToFree);
#endif
}
dwItemsCount = 0;
}
};
stdafx.h Не смешно. Один гений два дня дожидался пока не перешлю :)
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
#include
#include
#include
// TODO: reference additional headers your program requires here
Все статьи
Part 1
Part 2
Part 3
Part 4