2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/mm/ppool.c
5 * PURPOSE: Implements the paged pool
7 * PROGRAMMERS: David Welch (welch@mcmail.com)
11 /* INCLUDES *****************************************************************/
17 #if defined (ALLOC_PRAGMA)
18 #pragma alloc_text(INIT, MmInitializePagedPool)
22 #define ASSERT(x) if (!(x)) {DbgPrint("Assertion "#x" failed at %s:%d\n", __FILE__,__LINE__); KeBugCheck(0); }
26 #define R_MUTEX FAST_MUTEX
27 #define R_ACQUIRE_MUTEX(pool) /*DPRINT1("Acquiring PPool Mutex\n");*/ ExAcquireFastMutex(&pool->Mutex)
28 #define R_RELEASE_MUTEX(pool) /*DPRINT1("Releasing PPool Mutex\n");*/ ExReleaseFastMutex(&pool->Mutex)
29 #define R_PRINT_ADDRESS(addr) KeRosPrintAddress(addr)
30 #define R_PANIC() KeBugCheck(0)
31 #define R_DEBUG DbgPrint
34 #define R_GET_STACK_FRAMES(ptr,cnt)
36 #define R_GET_STACK_FRAMES(ptr,cnt) RtlWalkFrameChain((PVOID*)ptr,cnt, 0)
41 /* GLOBALS *******************************************************************/
43 PVOID MmPagedPoolBase
;
44 ULONG MmPagedPoolSize
;
45 ULONG MmTotalPagedPoolQuota
= 0; // TODO FIXME commented out until we use it
46 static PR_POOL MmPagedPool
= NULL
;
48 /* FUNCTIONS *****************************************************************/
53 MmInitializePagedPool(VOID
)
56 * We are still at a high IRQL level at this point so explicitly commit
57 * the first page of the paged pool before writing the first block header.
59 MmCommitPagedPoolAddress ( (PVOID
)MmPagedPoolBase
, FALSE
);
61 MmPagedPool
= RPoolInit ( MmPagedPoolBase
,
67 ExInitializeFastMutex(&MmPagedPool
->Mutex
);
70 /**********************************************************************
72 * ExAllocatePagedPoolWithTag@12
81 ExAllocatePagedPoolWithTag (IN POOL_TYPE PoolType
,
82 IN ULONG NumberOfBytes
,
87 if ( NumberOfBytes
>= PAGE_SIZE
)
89 else if ( PoolType
& CACHE_ALIGNED_POOL_MASK
)
94 ASSERT_IRQL_LESS_OR_EQUAL(APC_LEVEL
);
96 return RPoolAlloc ( MmPagedPool
, NumberOfBytes
, Tag
, align
);
100 ExFreePagedPool(IN PVOID Block
)
102 ASSERT_IRQL_LESS_OR_EQUAL(APC_LEVEL
);
103 RPoolFree ( MmPagedPool
, Block
);
107 EiGetPagedPoolTag(IN PVOID Block
)
109 return RBodyToHdr(Block
)->Tag
;
113 #ifdef PPOOL_UMODE_TEST
115 PVOID
TestAlloc ( ULONG Bytes
)
119 //printf ( "Allocating block: " ); RPoolStats ( MmPagedPool );
120 //RPoolRedZoneCheck ( MmPagedPool, __FILE__, __LINE__ );
122 ret
= ExAllocatePagedPoolWithTag ( PagedPool
, Bytes
, 0 );
124 //printf ( "Block %x allocated: ", ret ); RPoolStats ( MmPagedPool );
125 //RPoolRedZoneCheck ( MmPagedPool, __FILE__, __LINE__ );
130 void TestFree ( PVOID ptr
)
132 //printf ( "Freeing block %x: ", ptr ); RPoolStats ( MmPagedPool );
133 //RPoolRedZoneCheck ( MmPagedPool, __FILE__, __LINE__ );
134 ExFreePagedPool(ptr
);
135 //printf ( "Block %x freed: ", ptr ); RPoolStats ( MmPagedPool );
136 //RPoolRedZoneCheck ( MmPagedPool, __FILE__, __LINE__ );
143 char* keepers
[COUNT
];
145 int AllocSize
[] = { 15, 31, 63, 127, 255, 511, 1023, 2047 };
146 const int ALLOCS
= sizeof(AllocSize
) / sizeof(0[AllocSize
]);
149 MmPagedPoolSize
= 1*1024*1024;
150 MmPagedPoolBase
= malloc ( MmPagedPoolSize
);
151 MmInitializePagedPool();
153 dwStart
= GetTickCount();
155 printf ( "test #1 phase #1\n" );
156 for ( i
= 0; i
< COUNT
; i
++ )
158 //printf ( "keeper %i) ", i );
159 keepers
[i
] = TestAlloc ( AllocSize
[i
%ALLOCS
] );
160 if ( !keepers
[i
] ) printf ( "allocation failed\n" );
161 //printf ( "trash %i) ", i );
162 trash
[i
] = TestAlloc ( AllocSize
[i
%ALLOCS
] );
163 if ( !trash
[i
] ) printf ( "allocation failed\n" );
166 printf ( "test #1 phase #2\n" );
167 for ( i
= 0; i
< COUNT
; i
++ )
171 //printf ( "%i) ", i );
172 TestFree ( trash
[i
] );
175 printf ( "test #1 phase #3\n" );
176 for ( i
= 0; i
< 4; i
++ )
178 //printf ( "%i) ", i );
179 keepers
[i
] = TestAlloc ( 4096 );
180 if ( !keepers
[i
] ) printf ( "allocation failed\n" );
183 printf ( "test #1 phase #4\n" );
184 for ( i
= 0; i
< 4; i
++ )
186 //printf ( "%i) ", i );
187 TestFree ( keepers
[i
] );
190 printf ( "test #1 phase #5\n" );
192 for ( i
= 0; i
< COUNT
; i
++ )
194 //printf ( "%i) ", i );
195 trash
[i
] = TestAlloc ( rand()%1024+1 );
196 if ( !trash
[i
] ) printf ( "allocation failed\n" );
198 printf ( "test #1 phase #6\n" );
199 for ( i
= 0; i
< 10000; i
++ )
201 TestFree ( trash
[i
%COUNT
] );
202 trash
[i
%COUNT
] = TestAlloc ( rand()%1024+1 );
203 if ( !trash
[i
%COUNT
] ) printf ( "allocation failed\n" );
205 printf ( "test #1 phase #7\n" );
207 for ( i
= 0; i
< COUNT
; i
++ )
211 TestFree ( trash
[i
] );
215 printf ( "test #1 phase #8 ( freed %i of %i trash )\n", j
, COUNT
);
216 if ( !TestAlloc ( 2048 ) )
217 printf ( "Couldn't allocate 2048 bytes after freeing up a whole bunch of blocks\n" );
219 free ( MmPagedPoolBase
);
221 printf ( "test time: %lu\n", GetTickCount() - dwStart
);
223 printf ( "test #2\n" );
225 MmPagedPoolSize
= 1024;
226 MmPagedPoolBase
= malloc ( MmPagedPoolSize
);
227 MmInitializePagedPool();
230 i
= RPoolLargestAllocPossible ( MmPagedPool
, 0 );
231 if ( !TestAlloc ( i
) )
233 printf ( "allocating last available block failed\n" );
236 free ( MmPagedPoolBase
);
238 printf ( "done!\n" );
241 #endif//PPOOL_UMODE_TEST