FileMon中的Hash表算法代码

     这个hash表算法,不是很复杂。不过对于学习还是不错的选择。现在把代码贴下:

 

    这里定义了每个hash项的结构,以及hash桶的计算。

 

<textarea cols="87" rows="16" name="code" class="cpp">// // Structure for the fileobject/name hash table // typedef struct _nameentry { PFILE_OBJECT FileObject; struct _nameentry *Next; CHAR FullPathName[]; } HASH_ENTRY, *PHASH_ENTRY; // // Number of hash buckets in the hash table // #define NUMHASH 0x100 // // Hash function. Basically chops the low few bits of the file object // #if defined(_IA64_) #define HASHOBJECT(_fileobject) (((ULONG_PTR)_fileobject)&gt;&gt;5)%NUMHASH #else #define HASHOBJECT(_fileobject) (((ULONG)_fileobject)&gt;&gt;5)%NUMHASH #endif </textarea>

 

下面这部分为hash清空,删除的代码。

 <textarea cols="88" rows="20" name="code" class="cpp">//---------------------------------------------------------------------- // H A S H T A B L E M A N A G E M E N T //---------------------------------------------------------------------- //---------------------------------------------------------------------- // // FilemonHashCleanup // // Called when we are unloading to free any memory that we have // in our possession. // //---------------------------------------------------------------------- VOID FilemonHashCleanup( VOID ) { PHASH_ENTRY hashEntry, nextEntry; ULONG i; KeEnterCriticalRegion(); ExAcquireResourceExclusiveLite( &amp;HashResource, TRUE ); // // Free the hash table entries // for( i = 0; i &lt; NUMHASH; i++ ) { hashEntry = HashTable[i]; while( hashEntry ) { nextEntry = hashEntry-&gt;Next; ExFreePool( hashEntry ); hashEntry = nextEntry; } HashTable[i] = NULL; } ExReleaseResourceLite( &amp;HashResource ); KeLeaveCriticalRegion(); } //---------------------------------------------------------------------- // // FilemonFreeHashEntry // // When we see a file close, we can free the string we had associated // with the fileobject being closed since we know it won't be used // again. // //---------------------------------------------------------------------- VOID FilemonFreeHashEntry( PFILE_OBJECT fileObject ) { PHASH_ENTRY hashEntry, prevEntry; KeEnterCriticalRegion(); ExAcquireResourceExclusiveLite( &amp;HashResource, TRUE ); // // Look-up the entry // hashEntry = HashTable[ HASHOBJECT( fileObject ) ]; prevEntry = NULL; while( hashEntry &amp;&amp; hashEntry-&gt;FileObject != fileObject ) { prevEntry = hashEntry; hashEntry = hashEntry-&gt;Next; } // // If we fall of the hash list without finding what we're looking // for, just return. // if( !hashEntry ) { ExReleaseResourceLite( &amp;HashResource ); KeLeaveCriticalRegion(); return; } // // Got it! Remove it from the list // if( prevEntry ) { prevEntry-&gt;Next = hashEntry-&gt;Next; } else { HashTable[ HASHOBJECT( fileObject )] = hashEntry-&gt;Next; } // // Free the entry's memory // ExFreePool( hashEntry ); ExReleaseResourceLite( &amp;HashResource ); KeLeaveCriticalRegion(); }</textarea>

 

 

下面这段代码是在函数中向hash表添加项的过程。大致的意思就是首先找到桶的位置,然后在这个桶下的列表中添加就行了。

 

 

<textarea cols="87" rows="13" name="code" class="cpp"> PHASH_ENTRY hashEntry, newEntry; // // Allocate a hash entry // newEntry = ExAllocatePool( NonPagedPool, sizeof(HASH_ENTRY ) + strlen( fullPathName ) + 1); // // If no memory for a new entry, oh well. // if( newEntry ) { // // Fill in the new entry // newEntry-&gt;FileObject = fileObject; strcpy( newEntry-&gt;FullPathName, fullPathName ); // // Put it in the hash table // KeEnterCriticalRegion(); ExAcquireResourceExclusiveLite( &amp;HashResource, TRUE ); newEntry-&gt;Next = HashTable[ HASHOBJECT(fileObject) ]; HashTable[ HASHOBJECT(fileObject) ] = newEntry; ExReleaseResourceLite( &amp;HashResource ); KeLeaveCriticalRegion(); }</textarea>

 

下面是hash表的定义,以及初始化过程:

 

<textarea cols="87" rows="15" name="code" class="cpp"> // // Hash table for keeping names around. This is necessary because // at any time the name information in the fileobjects that we // see can be deallocated and reused. If we want to print accurate // names, we need to keep them around ourselves. // PHASH_ENTRY HashTable[NUMHASH]; // // Initialize the name hash table // for(i = 0; i &lt; NUMHASH; i++ ) HashTable[i] = NULL;</textarea>

你可能感兴趣的:(算法,struct,list,table,null,structure)