2 * PROJECT: ReactOS Kernel
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: ntoskrnl/mm/ARM3/expool.c
5 * PURPOSE: ARM Memory Manager Executive Pool Manager
6 * PROGRAMMERS: ReactOS Portable Systems Group
9 /* INCLUDES *******************************************************************/
15 #define MODULE_INVOLVED_IN_ARM3
16 #include "../ARM3/miarm.h"
18 #undef ExAllocatePoolWithQuota
19 #undef ExAllocatePoolWithQuotaTag
21 /* GLOBALS ********************************************************************/
23 ULONG ExpNumberOfPagedPools
;
24 POOL_DESCRIPTOR NonPagedPoolDescriptor
;
25 PPOOL_DESCRIPTOR ExpPagedPoolDescriptor
[16 + 1];
26 PPOOL_DESCRIPTOR PoolVector
[2];
28 PKGUARDED_MUTEX ExpPagedPoolMutex
;
30 /* Pool block/header/list access macros */
31 #define POOL_ENTRY(x) (PPOOL_HEADER)((ULONG_PTR)(x) - sizeof(POOL_HEADER))
32 #define POOL_FREE_BLOCK(x) (PLIST_ENTRY)((ULONG_PTR)(x) + sizeof(POOL_HEADER))
33 #define POOL_BLOCK(x, i) (PPOOL_HEADER)((ULONG_PTR)(x) + ((i) * POOL_BLOCK_SIZE))
34 #define POOL_NEXT_BLOCK(x) POOL_BLOCK((x), (x)->BlockSize)
35 #define POOL_PREV_BLOCK(x) POOL_BLOCK((x), -((x)->PreviousSize))
38 * Pool list access debug macros, similar to Arthur's pfnlist.c work.
39 * Microsoft actually implements similar checks in the Windows Server 2003 SP1
40 * pool code, but only for checked builds.
42 * As of Vista, however, an MSDN Blog entry by a Security Team Manager indicates
43 * that these checks are done even on retail builds, due to the increasing
44 * number of kernel-mode attacks which depend on dangling list pointers and other
45 * kinds of list-based attacks.
47 * For now, I will leave these checks on all the time, but later they are likely
48 * to be DBG-only, at least until there are enough kernel-mode security attacks
49 * against ReactOS to warrant the performance hit.
51 * For now, these are not made inline, so we can get good stack traces.
55 ExpDecodePoolLink(IN PLIST_ENTRY Link
)
57 return (PLIST_ENTRY
)((ULONG_PTR
)Link
& ~1);
62 ExpEncodePoolLink(IN PLIST_ENTRY Link
)
64 return (PLIST_ENTRY
)((ULONG_PTR
)Link
| 1);
69 ExpCheckPoolLinks(IN PLIST_ENTRY ListHead
)
71 if ((ExpDecodePoolLink(ExpDecodePoolLink(ListHead
->Flink
)->Blink
) != ListHead
) ||
72 (ExpDecodePoolLink(ExpDecodePoolLink(ListHead
->Blink
)->Flink
) != ListHead
))
74 KeBugCheckEx(BAD_POOL_HEADER
,
77 (ULONG_PTR
)ExpDecodePoolLink(ExpDecodePoolLink(ListHead
->Flink
)->Blink
),
78 (ULONG_PTR
)ExpDecodePoolLink(ExpDecodePoolLink(ListHead
->Blink
)->Flink
));
84 ExpInitializePoolListHead(IN PLIST_ENTRY ListHead
)
86 ListHead
->Flink
= ListHead
->Blink
= ExpEncodePoolLink(ListHead
);
91 ExpIsPoolListEmpty(IN PLIST_ENTRY ListHead
)
93 return (ExpDecodePoolLink(ListHead
->Flink
) == ListHead
);
98 ExpRemovePoolEntryList(IN PLIST_ENTRY Entry
)
100 PLIST_ENTRY Blink
, Flink
;
101 Flink
= ExpDecodePoolLink(Entry
->Flink
);
102 Blink
= ExpDecodePoolLink(Entry
->Blink
);
103 Flink
->Blink
= ExpEncodePoolLink(Blink
);
104 Blink
->Flink
= ExpEncodePoolLink(Flink
);
109 ExpRemovePoolHeadList(IN PLIST_ENTRY ListHead
)
111 PLIST_ENTRY Entry
, Flink
;
112 Entry
= ExpDecodePoolLink(ListHead
->Flink
);
113 Flink
= ExpDecodePoolLink(Entry
->Flink
);
114 ListHead
->Flink
= ExpEncodePoolLink(Flink
);
115 Flink
->Blink
= ExpEncodePoolLink(ListHead
);
121 ExpRemovePoolTailList(IN PLIST_ENTRY ListHead
)
123 PLIST_ENTRY Entry
, Blink
;
124 Entry
= ExpDecodePoolLink(ListHead
->Blink
);
125 Blink
= ExpDecodePoolLink(Entry
->Blink
);
126 ListHead
->Blink
= ExpEncodePoolLink(Blink
);
127 Blink
->Flink
= ExpEncodePoolLink(ListHead
);
133 ExpInsertPoolTailList(IN PLIST_ENTRY ListHead
,
134 IN PLIST_ENTRY Entry
)
137 ExpCheckPoolLinks(ListHead
);
138 Blink
= ExpDecodePoolLink(ListHead
->Blink
);
139 Entry
->Flink
= ExpEncodePoolLink(ListHead
);
140 Entry
->Blink
= ExpEncodePoolLink(Blink
);
141 Blink
->Flink
= ExpEncodePoolLink(Entry
);
142 ListHead
->Blink
= ExpEncodePoolLink(Entry
);
143 ExpCheckPoolLinks(ListHead
);
148 ExpInsertPoolHeadList(IN PLIST_ENTRY ListHead
,
149 IN PLIST_ENTRY Entry
)
152 ExpCheckPoolLinks(ListHead
);
153 Flink
= ExpDecodePoolLink(ListHead
->Flink
);
154 Entry
->Flink
= ExpEncodePoolLink(Flink
);
155 Entry
->Blink
= ExpEncodePoolLink(ListHead
);
156 Flink
->Blink
= ExpEncodePoolLink(Entry
);
157 ListHead
->Flink
= ExpEncodePoolLink(Entry
);
158 ExpCheckPoolLinks(ListHead
);
163 ExpCheckPoolHeader(IN PPOOL_HEADER Entry
)
165 PPOOL_HEADER PreviousEntry
, NextEntry
;
167 /* Is there a block before this one? */
168 if (Entry
->PreviousSize
)
171 PreviousEntry
= POOL_PREV_BLOCK(Entry
);
173 /* The two blocks must be on the same page! */
174 if (PAGE_ALIGN(Entry
) != PAGE_ALIGN(PreviousEntry
))
176 /* Something is awry */
177 KeBugCheckEx(BAD_POOL_HEADER
,
179 (ULONG_PTR
)PreviousEntry
,
184 /* This block should also indicate that it's as large as we think it is */
185 if (PreviousEntry
->BlockSize
!= Entry
->PreviousSize
)
187 /* Otherwise, someone corrupted one of the sizes */
188 KeBugCheckEx(BAD_POOL_HEADER
,
190 (ULONG_PTR
)PreviousEntry
,
195 else if (PAGE_ALIGN(Entry
) != Entry
)
197 /* If there's no block before us, we are the first block, so we should be on a page boundary */
198 KeBugCheckEx(BAD_POOL_HEADER
,
205 /* This block must have a size */
206 if (!Entry
->BlockSize
)
208 /* Someone must've corrupted this field */
209 KeBugCheckEx(BAD_POOL_HEADER
,
216 /* Okay, now get the next block */
217 NextEntry
= POOL_NEXT_BLOCK(Entry
);
219 /* If this is the last block, then we'll be page-aligned, otherwise, check this block */
220 if (PAGE_ALIGN(NextEntry
) != NextEntry
)
222 /* The two blocks must be on the same page! */
223 if (PAGE_ALIGN(Entry
) != PAGE_ALIGN(NextEntry
))
225 /* Something is messed up */
226 KeBugCheckEx(BAD_POOL_HEADER
,
228 (ULONG_PTR
)NextEntry
,
233 /* And this block should think we are as large as we truly are */
234 if (NextEntry
->PreviousSize
!= Entry
->BlockSize
)
236 /* Otherwise, someone corrupted the field */
237 KeBugCheckEx(BAD_POOL_HEADER
,
239 (ULONG_PTR
)NextEntry
,
248 ExpCheckPoolBlocks(IN PVOID Block
)
250 BOOLEAN FoundBlock
= FALSE
;
254 /* Get the first entry for this page, make sure it really is the first */
255 Entry
= PAGE_ALIGN(Block
);
256 ASSERT(Entry
->PreviousSize
== 0);
258 /* Now scan each entry */
261 /* When we actually found our block, remember this */
262 if (Entry
== Block
) FoundBlock
= TRUE
;
264 /* Now validate this block header */
265 ExpCheckPoolHeader(Entry
);
267 /* And go to the next one, keeping track of our size */
268 Size
+= Entry
->BlockSize
;
269 Entry
= POOL_NEXT_BLOCK(Entry
);
271 /* If we hit the last block, stop */
272 if (Size
>= (PAGE_SIZE
/ POOL_BLOCK_SIZE
)) break;
274 /* If we hit the end of the page, stop */
275 if (PAGE_ALIGN(Entry
) == Entry
) break;
278 /* We must've found our block, and we must have hit the end of the page */
279 if ((PAGE_ALIGN(Entry
) != Entry
) || !(FoundBlock
))
281 /* Otherwise, the blocks are messed up */
282 KeBugCheckEx(BAD_POOL_HEADER
, 10, (ULONG_PTR
)Block
, __LINE__
, (ULONG_PTR
)Entry
);
286 /* PRIVATE FUNCTIONS **********************************************************/
291 ExInitializePoolDescriptor(IN PPOOL_DESCRIPTOR PoolDescriptor
,
292 IN POOL_TYPE PoolType
,
297 PLIST_ENTRY NextEntry
, LastEntry
;
300 // Setup the descriptor based on the caller's request
302 PoolDescriptor
->PoolType
= PoolType
;
303 PoolDescriptor
->PoolIndex
= PoolIndex
;
304 PoolDescriptor
->Threshold
= Threshold
;
305 PoolDescriptor
->LockAddress
= PoolLock
;
308 // Initialize accounting data
310 PoolDescriptor
->RunningAllocs
= 0;
311 PoolDescriptor
->RunningDeAllocs
= 0;
312 PoolDescriptor
->TotalPages
= 0;
313 PoolDescriptor
->TotalBytes
= 0;
314 PoolDescriptor
->TotalBigPages
= 0;
317 // Nothing pending for now
319 PoolDescriptor
->PendingFrees
= NULL
;
320 PoolDescriptor
->PendingFreeDepth
= 0;
323 // Loop all the descriptor's allocation lists and initialize them
325 NextEntry
= PoolDescriptor
->ListHeads
;
326 LastEntry
= NextEntry
+ POOL_LISTS_PER_PAGE
;
327 while (NextEntry
< LastEntry
)
329 ExpInitializePoolListHead(NextEntry
);
337 InitializePool(IN POOL_TYPE PoolType
,
340 PPOOL_DESCRIPTOR Descriptor
;
343 // Check what kind of pool this is
345 if (PoolType
== NonPagedPool
)
348 // Initialize the nonpaged pool descriptor
350 PoolVector
[NonPagedPool
] = &NonPagedPoolDescriptor
;
351 ExInitializePoolDescriptor(PoolVector
[NonPagedPool
],
360 // Allocate the pool descriptor
362 Descriptor
= ExAllocatePoolWithTag(NonPagedPool
,
363 sizeof(KGUARDED_MUTEX
) +
364 sizeof(POOL_DESCRIPTOR
),
369 // This is really bad...
371 KeBugCheckEx(MUST_SUCCEED_POOL_EMPTY
,
379 // Setup the vector and guarded mutex for paged pool
381 PoolVector
[PagedPool
] = Descriptor
;
382 ExpPagedPoolMutex
= (PKGUARDED_MUTEX
)(Descriptor
+ 1);
383 KeInitializeGuardedMutex(ExpPagedPoolMutex
);
384 ExInitializePoolDescriptor(Descriptor
,
394 ExLockPool(IN PPOOL_DESCRIPTOR Descriptor
)
397 // Check if this is nonpaged pool
399 if ((Descriptor
->PoolType
& BASE_POOL_TYPE_MASK
) == NonPagedPool
)
402 // Use the queued spin lock
404 return KeAcquireQueuedSpinLock(LockQueueNonPagedPoolLock
);
409 // Use the guarded mutex
411 KeAcquireGuardedMutex(Descriptor
->LockAddress
);
418 ExUnlockPool(IN PPOOL_DESCRIPTOR Descriptor
,
422 // Check if this is nonpaged pool
424 if ((Descriptor
->PoolType
& BASE_POOL_TYPE_MASK
) == NonPagedPool
)
427 // Use the queued spin lock
429 KeReleaseQueuedSpinLock(LockQueueNonPagedPoolLock
, OldIrql
);
434 // Use the guarded mutex
436 KeReleaseGuardedMutex(Descriptor
->LockAddress
);
440 /* PUBLIC FUNCTIONS ***********************************************************/
447 ExAllocatePoolWithTag(IN POOL_TYPE PoolType
,
448 IN SIZE_T NumberOfBytes
,
451 PPOOL_DESCRIPTOR PoolDesc
;
452 PLIST_ENTRY ListHead
;
453 PPOOL_HEADER Entry
, NextEntry
, FragmentEntry
;
458 // Some sanity checks
461 ASSERT(Tag
!= ' GIB');
462 ASSERT(NumberOfBytes
!= 0);
465 // Get the pool type and its corresponding vector for this request
467 PoolType
= PoolType
& BASE_POOL_TYPE_MASK
;
468 PoolDesc
= PoolVector
[PoolType
];
469 ASSERT(PoolDesc
!= NULL
);
472 // Check if this is a big page allocation
474 if (NumberOfBytes
> POOL_MAX_ALLOC
)
477 // Then just return the number of pages requested
479 return MiAllocatePoolPages(PoolType
, NumberOfBytes
);
483 // Should never request 0 bytes from the pool, but since so many drivers do
484 // it, we'll just assume they want 1 byte, based on NT's similar behavior
486 if (!NumberOfBytes
) NumberOfBytes
= 1;
489 // A pool allocation is defined by its data, a linked list to connect it to
490 // the free list (if necessary), and a pool header to store accounting info.
491 // Calculate this size, then convert it into a block size (units of pool
494 // Note that i cannot overflow (past POOL_LISTS_PER_PAGE) because any such
495 // request would've been treated as a POOL_MAX_ALLOC earlier and resulted in
496 // the direct allocation of pages.
498 i
= (NumberOfBytes
+ sizeof(POOL_HEADER
) + (POOL_BLOCK_SIZE
- 1)) / POOL_BLOCK_SIZE
;
501 // Loop in the free lists looking for a block if this size. Start with the
502 // list optimized for this kind of size lookup
504 ListHead
= &PoolDesc
->ListHeads
[i
];
508 // Are there any free entries available on this list?
510 if (!ExpIsPoolListEmpty(ListHead
))
513 // Acquire the pool lock now
515 OldIrql
= ExLockPool(PoolDesc
);
518 // And make sure the list still has entries
520 if (ExpIsPoolListEmpty(ListHead
))
523 // Someone raced us (and won) before we had a chance to acquire
528 ExUnlockPool(PoolDesc
, OldIrql
);
534 // Remove a free entry from the list
535 // Note that due to the way we insert free blocks into multiple lists
536 // there is a guarantee that any block on this list will either be
537 // of the correct size, or perhaps larger.
539 ExpCheckPoolLinks(ListHead
);
540 Entry
= POOL_ENTRY(ExpRemovePoolHeadList(ListHead
));
541 ExpCheckPoolLinks(ListHead
);
542 ExpCheckPoolBlocks(Entry
);
543 ASSERT(Entry
->BlockSize
>= i
);
544 ASSERT(Entry
->PoolType
== 0);
547 // Check if this block is larger that what we need. The block could
548 // not possibly be smaller, due to the reason explained above (and
549 // we would've asserted on a checked build if this was the case).
551 if (Entry
->BlockSize
!= i
)
554 // Is there an entry before this one?
556 if (Entry
->PreviousSize
== 0)
559 // There isn't anyone before us, so take the next block and
560 // turn it into a fragment that contains the leftover data
561 // that we don't need to satisfy the caller's request
563 FragmentEntry
= POOL_BLOCK(Entry
, i
);
564 FragmentEntry
->BlockSize
= Entry
->BlockSize
- i
;
567 // And make it point back to us
569 FragmentEntry
->PreviousSize
= i
;
572 // Now get the block that follows the new fragment and check
573 // if it's still on the same page as us (and not at the end)
575 NextEntry
= POOL_NEXT_BLOCK(FragmentEntry
);
576 if (PAGE_ALIGN(NextEntry
) != NextEntry
)
579 // Adjust this next block to point to our newly created
582 NextEntry
->PreviousSize
= FragmentEntry
->BlockSize
;
588 // There is a free entry before us, which we know is smaller
589 // so we'll make this entry the fragment instead
591 FragmentEntry
= Entry
;
594 // And then we'll remove from it the actual size required.
595 // Now the entry is a leftover free fragment
597 Entry
->BlockSize
-= i
;
600 // Now let's go to the next entry after the fragment (which
601 // used to point to our original free entry) and make it
602 // reference the new fragment entry instead.
604 // This is the entry that will actually end up holding the
607 Entry
= POOL_NEXT_BLOCK(Entry
);
608 Entry
->PreviousSize
= FragmentEntry
->BlockSize
;
611 // And now let's go to the entry after that one and check if
612 // it's still on the same page, and not at the end
614 NextEntry
= POOL_BLOCK(Entry
, i
);
615 if (PAGE_ALIGN(NextEntry
) != NextEntry
)
618 // Make it reference the allocation entry
620 NextEntry
->PreviousSize
= i
;
625 // Now our (allocation) entry is the right size
627 Entry
->BlockSize
= i
;
630 // And the next entry is now the free fragment which contains
631 // the remaining difference between how big the original entry
632 // was, and the actual size the caller needs/requested.
634 FragmentEntry
->PoolType
= 0;
635 BlockSize
= FragmentEntry
->BlockSize
;
638 // Now check if enough free bytes remained for us to have a
639 // "full" entry, which contains enough bytes for a linked list
640 // and thus can be used for allocations (up to 8 bytes...)
642 ExpCheckPoolLinks(&PoolDesc
->ListHeads
[BlockSize
- 1]);
646 // Insert the free entry into the free list for this size
648 ExpInsertPoolTailList(&PoolDesc
->ListHeads
[BlockSize
- 1],
649 POOL_FREE_BLOCK(FragmentEntry
));
650 ExpCheckPoolLinks(POOL_FREE_BLOCK(FragmentEntry
));
655 // We have found an entry for this allocation, so set the pool type
656 // and release the lock since we're done
658 Entry
->PoolType
= PoolType
+ 1;
659 ExpCheckPoolBlocks(Entry
);
660 ExUnlockPool(PoolDesc
, OldIrql
);
663 // Return the pool allocation
665 Entry
->PoolTag
= Tag
;
666 (POOL_FREE_BLOCK(Entry
))->Flink
= NULL
;
667 (POOL_FREE_BLOCK(Entry
))->Blink
= NULL
;
668 return POOL_FREE_BLOCK(Entry
);
670 } while (++ListHead
!= &PoolDesc
->ListHeads
[POOL_LISTS_PER_PAGE
]);
673 // There were no free entries left, so we have to allocate a new fresh page
675 Entry
= MiAllocatePoolPages(PoolType
, PAGE_SIZE
);
676 ASSERT(Entry
!= NULL
);
678 Entry
->BlockSize
= i
;
679 Entry
->PoolType
= PoolType
+ 1;
682 // This page will have two entries -- one for the allocation (which we just
683 // created above), and one for the remaining free bytes, which we're about
684 // to create now. The free bytes are the whole page minus what was allocated
685 // and then converted into units of block headers.
687 BlockSize
= (PAGE_SIZE
/ POOL_BLOCK_SIZE
) - i
;
688 FragmentEntry
= POOL_BLOCK(Entry
, i
);
689 FragmentEntry
->Ulong1
= 0;
690 FragmentEntry
->BlockSize
= BlockSize
;
691 FragmentEntry
->PreviousSize
= i
;
694 // Now check if enough free bytes remained for us to have a "full" entry,
695 // which contains enough bytes for a linked list and thus can be used for
696 // allocations (up to 8 bytes...)
698 if (FragmentEntry
->BlockSize
!= 1)
701 // Excellent -- acquire the pool lock
703 OldIrql
= ExLockPool(PoolDesc
);
706 // And insert the free entry into the free list for this block size
708 ExpCheckPoolLinks(&PoolDesc
->ListHeads
[BlockSize
- 1]);
709 ExpInsertPoolTailList(&PoolDesc
->ListHeads
[BlockSize
- 1],
710 POOL_FREE_BLOCK(FragmentEntry
));
711 ExpCheckPoolLinks(POOL_FREE_BLOCK(FragmentEntry
));
714 // Release the pool lock
716 ExpCheckPoolBlocks(Entry
);
717 ExUnlockPool(PoolDesc
, OldIrql
);
721 // And return the pool allocation
723 ExpCheckPoolBlocks(Entry
);
724 Entry
->PoolTag
= Tag
;
725 return POOL_FREE_BLOCK(Entry
);
733 ExAllocatePool(POOL_TYPE PoolType
,
734 SIZE_T NumberOfBytes
)
737 // Use a default tag of "None"
739 return ExAllocatePoolWithTag(PoolType
, NumberOfBytes
, 'enoN');
747 ExFreePoolWithTag(IN PVOID P
,
750 PPOOL_HEADER Entry
, NextEntry
;
754 PPOOL_DESCRIPTOR PoolDesc
;
755 BOOLEAN Combined
= FALSE
;
758 // Quickly deal with big page allocations
760 if (PAGE_ALIGN(P
) == P
)
767 // Get the entry for this pool allocation
768 // The pointer math here may look wrong or confusing, but it is quite right
774 // Get the size of the entry, and it's pool type, then load the descriptor
775 // for this pool type
777 BlockSize
= Entry
->BlockSize
;
778 PoolType
= (Entry
->PoolType
- 1) & BASE_POOL_TYPE_MASK
;
779 PoolDesc
= PoolVector
[PoolType
];
782 // Get the pointer to the next entry
784 NextEntry
= POOL_BLOCK(Entry
, BlockSize
);
787 // Acquire the pool lock
789 OldIrql
= ExLockPool(PoolDesc
);
792 // Check if the next allocation is at the end of the page
794 ExpCheckPoolBlocks(Entry
);
795 if (PAGE_ALIGN(NextEntry
) != NextEntry
)
798 // We may be able to combine the block if it's free
800 if (NextEntry
->PoolType
== 0)
803 // The next block is free, so we'll do a combine
808 // Make sure there's actual data in the block -- anything smaller
809 // than this means we only have the header, so there's no linked list
812 if ((NextEntry
->BlockSize
!= 1))
815 // The block is at least big enough to have a linked list, so go
816 // ahead and remove it
818 ExpCheckPoolLinks(POOL_FREE_BLOCK(NextEntry
));
819 ExpRemovePoolEntryList(POOL_FREE_BLOCK(NextEntry
));
820 ExpCheckPoolLinks(ExpDecodePoolLink((POOL_FREE_BLOCK(NextEntry
))->Flink
));
821 ExpCheckPoolLinks(ExpDecodePoolLink((POOL_FREE_BLOCK(NextEntry
))->Blink
));
825 // Our entry is now combined with the next entry
827 Entry
->BlockSize
= Entry
->BlockSize
+ NextEntry
->BlockSize
;
832 // Now check if there was a previous entry on the same page as us
834 if (Entry
->PreviousSize
)
837 // Great, grab that entry and check if it's free
839 NextEntry
= POOL_PREV_BLOCK(Entry
);
840 if (NextEntry
->PoolType
== 0)
843 // It is, so we can do a combine
848 // Make sure there's actual data in the block -- anything smaller
849 // than this means we only have the header so there's no linked list
852 if ((NextEntry
->BlockSize
!= 1))
855 // The block is at least big enough to have a linked list, so go
856 // ahead and remove it
858 ExpCheckPoolLinks(POOL_FREE_BLOCK(NextEntry
));
859 ExpRemovePoolEntryList(POOL_FREE_BLOCK(NextEntry
));
860 ExpCheckPoolLinks(ExpDecodePoolLink((POOL_FREE_BLOCK(NextEntry
))->Flink
));
861 ExpCheckPoolLinks(ExpDecodePoolLink((POOL_FREE_BLOCK(NextEntry
))->Blink
));
865 // Combine our original block (which might've already been combined
866 // with the next block), into the previous block
868 NextEntry
->BlockSize
= NextEntry
->BlockSize
+ Entry
->BlockSize
;
871 // And now we'll work with the previous block instead
878 // By now, it may have been possible for our combined blocks to actually
879 // have made up a full page (if there were only 2-3 allocations on the
880 // page, they could've all been combined).
882 if ((PAGE_ALIGN(Entry
) == Entry
) &&
883 (PAGE_ALIGN(POOL_NEXT_BLOCK(Entry
)) == POOL_NEXT_BLOCK(Entry
)))
886 // In this case, release the pool lock, and free the page
888 ExUnlockPool(PoolDesc
, OldIrql
);
889 MiFreePoolPages(Entry
);
894 // Otherwise, we now have a free block (or a combination of 2 or 3)
897 BlockSize
= Entry
->BlockSize
;
898 ASSERT(BlockSize
!= 1);
901 // Check if we actually did combine it with anyone
906 // Get the first combined block (either our original to begin with, or
907 // the one after the original, depending if we combined with the previous)
909 NextEntry
= POOL_NEXT_BLOCK(Entry
);
912 // As long as the next block isn't on a page boundary, have it point
915 if (PAGE_ALIGN(NextEntry
) != NextEntry
) NextEntry
->PreviousSize
= BlockSize
;
919 // Insert this new free block, and release the pool lock
921 ExpInsertPoolHeadList(&PoolDesc
->ListHeads
[BlockSize
- 1], POOL_FREE_BLOCK(Entry
));
922 ExpCheckPoolLinks(POOL_FREE_BLOCK(Entry
));
923 ExUnlockPool(PoolDesc
, OldIrql
);
934 // Just free without checking for the tag
936 ExFreePoolWithTag(P
, 0);
944 ExQueryPoolBlockSize(IN PVOID PoolBlock
,
945 OUT PBOOLEAN QuotaCharged
)
960 ExAllocatePoolWithQuota(IN POOL_TYPE PoolType
,
961 IN SIZE_T NumberOfBytes
)
966 return ExAllocatePoolWithQuotaTag(PoolType
, NumberOfBytes
, 'enoN');
974 ExAllocatePoolWithTagPriority(IN POOL_TYPE PoolType
,
975 IN SIZE_T NumberOfBytes
,
977 IN EX_POOL_PRIORITY Priority
)
983 return ExAllocatePoolWithTag(PoolType
, NumberOfBytes
, Tag
);
991 ExAllocatePoolWithQuotaTag(IN POOL_TYPE PoolType
,
992 IN SIZE_T NumberOfBytes
,
999 return ExAllocatePoolWithTag(PoolType
, NumberOfBytes
, Tag
);