From 93acc859af7aadb80a42aace75a4e2860acb17cd Mon Sep 17 00:00:00 2001 From: Chloe M Date: Mon, 13 Jul 2026 20:19:09 -0400 Subject: [PATCH] ptos: pool: Add freeing of allocated memory Signed-off-by: Chloe M --- service/ptos/ex/pool.c | 35 +++++++++++++++++++++++++++++++++++ service/ptos/head/ex/pool.h | 8 ++++++++ 2 files changed, 43 insertions(+) diff --git a/service/ptos/ex/pool.c b/service/ptos/ex/pool.c index c83fb2a..9b3e801 100644 --- a/service/ptos/ex/pool.c +++ b/service/ptos/ex/pool.c @@ -268,6 +268,41 @@ ExAllocatePoolWithTag(POOL_TYPE Type, USIZE ByteCount, ULONG Tag) return Base; } +VOID +ExFreePoolWithTag(VOID *Ptr, ULONG Tag) +{ + POOL_HEADER *Header; + POOL_PAGE *Page; + USHORT BitOff, BitMax; + + if (Ptr == NULL) { + return; + } + + Header = PTR_NOFFSET(Ptr, sizeof(POOL_HEADER)); + if (Header->Tag != Tag) { + KeBugCheck( + BUGCHECK_MISC, + "tag does not match pool header\n" + ); + } + + Page = Header->Page; + if (Page == NULL) { + KeBugCheck( + BUGCHECK_MISC, + "attempted free on malformed header\n" + ); + } + + BitMax = Header->SizeBytes / Header->Gran; + BitMax *= 8; + + for (BitOff = Header->BitBase; BitOff < BitMax; ++BitOff) { + CLRBIT(&Page->Bitmap, BitOff); + } +} + VOID ExPoolRegionInit(POOL_REGION *Region) { diff --git a/service/ptos/head/ex/pool.h b/service/ptos/head/ex/pool.h index 88a350b..3c02eb1 100644 --- a/service/ptos/head/ex/pool.h +++ b/service/ptos/head/ex/pool.h @@ -89,6 +89,14 @@ typedef struct { */ VOID ExPoolRegionInit(POOL_REGION *Region); +/* + * Free a pool with an associated tag + * + * @Ptr: Base of memory to free + * @Tag: Tag associated with allocated memory + */ +VOID ExFreePoolWithTag(VOID *Ptr, ULONG Tag); + /* * Allocate a memory pool of a specific type and assign a tag * to it for verifying frees among other debugging needs.