Use memory wrappers instead of ExAllocatePool/ExFreePool directly
[reactos.git] / reactos / ntoskrnl / config / cmalloc.c
index 355ad6d..37da97e 100644 (file)
@@ -11,7 +11,6 @@
 #include <ntoskrnl.h>
 #define NDEBUG
 #include <debug.h>
-#include "cm.h"
 
 /* GLOBALS *******************************************************************/
 
@@ -56,14 +55,14 @@ CmpFreeKeyControlBlock(IN PCM_KEY_CONTROL_BLOCK Kcb)
     PAGED_CODE();
     
     /* Sanity checks */
-    ASSERT(IsListEmpty(&(Kcb->KeyBodyListHead)) == TRUE);
+    ASSERT(IsListEmpty(&Kcb->KeyBodyListHead) == TRUE);
     for (i = 0; i < 4; i++) ASSERT(Kcb->KeyBodyArray[i] == NULL);
     
     /* Check if it wasn't privately allocated */
     if (!Kcb->PrivateAlloc)
     {
         /* Free it from the pool */
-        ExFreePool(Kcb);
+        CmpFree(Kcb, 0);
         return;
     }
     
@@ -71,9 +70,8 @@ CmpFreeKeyControlBlock(IN PCM_KEY_CONTROL_BLOCK Kcb)
     KeAcquireGuardedMutex(&CmpAllocBucketLock);
     
     /* Sanity check on lock ownership */
-    ASSERT((GET_HASH_ENTRY(CmpCacheTable, Kcb->ConvKey).Owner ==
-            KeGetCurrentThread()) ||
-           (CmpTestRegistryLockExclusive() == TRUE));
+    //ASSERT((CmpIsKcbLockedExclusive(Kcb) == TRUE) ||
+    //       (CmpTestRegistryLockExclusive() == TRUE));
     
     /* Add us to the free list */
     InsertTailList(&CmpFreeKCBListHead, &Kcb->FreeListEntry);
@@ -88,19 +86,19 @@ CmpFreeKeyControlBlock(IN PCM_KEY_CONTROL_BLOCK Kcb)
     if (++AllocPage->FreeCount == CM_KCBS_PER_PAGE)
     {
         /* Loop all the entries */
-        for (i = CM_KCBS_PER_PAGE; i; i--)
+        for (i = 0; i < CM_KCBS_PER_PAGE; i++)
         {
             /* Get the KCB */
             Kcb = (PVOID)((ULONG_PTR)AllocPage +
                           FIELD_OFFSET(CM_ALLOC_PAGE, AllocPage) +
-                          (i - 1) * sizeof(CM_KEY_CONTROL_BLOCK));
+                          i * sizeof(CM_KEY_CONTROL_BLOCK));
             
             /* Remove the entry */ 
             RemoveEntryList(&Kcb->FreeListEntry);
         }
         
         /* Free the page */
-        ExFreePool(AllocPage);
+        CmpFree(AllocPage, 0);
     }
     
     /* Release the lock */
@@ -116,7 +114,7 @@ CmpAllocateKeyControlBlock(VOID)
     PCM_ALLOC_PAGE AllocPage;
     ULONG i;
     PAGED_CODE();
-    
+
     /* Check if private allocations are initialized */
     if (CmpAllocInited)
     {
@@ -153,7 +151,7 @@ SearchKcbList:
         }
         
         /* Allocate an allocation page */
-        AllocPage = ExAllocatePoolWithTag(PagedPool, PAGE_SIZE, TAG_CM);
+        AllocPage = CmpAllocate(PAGE_SIZE, TRUE, TAG_CM);
         if (AllocPage)
         {
             /* Set default entries */
@@ -178,11 +176,11 @@ SearchKcbList:
             goto SearchKcbList;
         }
     }
-    
+
     /* Allocate a KCB only */
-    CurrentKcb = ExAllocatePoolWithTag(PagedPool,
-                                       sizeof(CM_KEY_CONTROL_BLOCK),
-                                       TAG_CM);
+    CurrentKcb = CmpAllocate(sizeof(CM_KEY_CONTROL_BLOCK),
+                             TRUE,
+                             TAG_CM);
     if (CurrentKcb)
     {
         /* Set it up */
@@ -209,31 +207,31 @@ CmpAllocateDelayItem(VOID)
     
     /* Look for an item on the free list */
 SearchList:
-        if (!IsListEmpty(&CmpFreeDelayItemsListHead))
-        {
-            /* Get the current entry in the list */
-            NextEntry = RemoveHeadList(&CmpFreeDelayItemsListHead);
-            
-            /* Grab the item */
-            Entry = CONTAINING_RECORD(NextEntry, CM_DELAY_ALLOC, ListEntry);
-            
-            /* Clear the list */
-            Entry->ListEntry.Flink = Entry->ListEntry.Blink = NULL;
-            
-            /* Grab the alloc page */
-            AllocPage = (PCM_ALLOC_PAGE)((ULONG_PTR)Entry & 0xFFFFF000);
-            
-            /* Decrease free entries */
-            ASSERT(AllocPage->FreeCount != 0);
-            AllocPage->FreeCount--;
-            
-            /* Release the lock */
-            KeReleaseGuardedMutex(&CmpDelayAllocBucketLock);
-            return Entry;
-        }
+    if (!IsListEmpty(&CmpFreeDelayItemsListHead))
+    {
+        /* Get the current entry in the list */
+        NextEntry = RemoveHeadList(&CmpFreeDelayItemsListHead);
+        
+        /* Grab the item */
+        Entry = CONTAINING_RECORD(NextEntry, CM_DELAY_ALLOC, ListEntry);
+        
+        /* Clear the list */
+        Entry->ListEntry.Flink = Entry->ListEntry.Blink = NULL;
+        
+        /* Grab the alloc page */
+        AllocPage = (PCM_ALLOC_PAGE)((ULONG_PTR)Entry & 0xFFFFF000);
+        
+        /* Decrease free entries */
+        ASSERT(AllocPage->FreeCount != 0);
+        AllocPage->FreeCount--;
+        
+        /* Release the lock */
+        KeReleaseGuardedMutex(&CmpDelayAllocBucketLock);
+        return Entry;
+    }
     
     /* Allocate an allocation page */
-    AllocPage = ExAllocatePoolWithTag(PagedPool, PAGE_SIZE, TAG_CM);
+    AllocPage = CmpAllocate(PAGE_SIZE, TRUE, TAG_CM);
     if (AllocPage)
     {
         /* Set default entries */
@@ -297,7 +295,7 @@ CmpFreeDelayItem(PVOID Entry)
         }
         
         /* Now free the page */
-        ExFreePool(AllocPage);
+        CmpFree(AllocPage, 0);
     }
     
     /* Release the lock */