3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/mm/ppool.c
6 * PURPOSE: Implements the paged pool
8 * PROGRAMMERS: David Welch (welch@mcmail.com)
12 /* INCLUDES *****************************************************************/
14 #ifdef PPOOL_UMODE_TEST
15 #include "ppool_umode.h"
16 #else//PPOOL_UMODE_TEST
19 #include <internal/debug.h>
20 #endif//PPOOL_UMODE_TEST
22 #if defined (ALLOC_PRAGMA)
23 #pragma alloc_text(INIT, MmInitializePagedPool)
27 #define ASSERT(x) if (!(x)) {DbgPrint("Assertion "#x" failed at %s:%d\n", __FILE__,__LINE__); KeBugCheck(0); }
31 #define R_MUTEX FAST_MUTEX
32 #define R_ACQUIRE_MUTEX(pool) /*DPRINT1("Acquiring PPool Mutex\n");*/ ExAcquireFastMutex(&pool->Mutex)
33 #define R_RELEASE_MUTEX(pool) /*DPRINT1("Releasing PPool Mutex\n");*/ ExReleaseFastMutex(&pool->Mutex)
34 #define R_PRINT_ADDRESS(addr) KeRosPrintAddress(addr)
35 #define R_PANIC() KeBugCheck(0)
36 #define R_DEBUG DbgPrint
37 #define R_GET_STACK_FRAMES(ptr,cnt) KeRosGetStackFrames(ptr,cnt)
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(APC_LEVEL
);
96 return RPoolAlloc ( MmPagedPool
, NumberOfBytes
, Tag
, align
);
100 ExFreePagedPool(IN PVOID Block
)
102 ASSERT_IRQL(APC_LEVEL
);
103 RPoolFree ( MmPagedPool
, Block
);
107 #ifdef PPOOL_UMODE_TEST
109 PVOID
TestAlloc ( ULONG Bytes
)
113 //printf ( "Allocating block: " ); RPoolStats ( MmPagedPool );
114 //RPoolRedZoneCheck ( MmPagedPool, __FILE__, __LINE__ );
116 ret
= ExAllocatePagedPoolWithTag ( PagedPool
, Bytes
, 0 );
118 //printf ( "Block %x allocated: ", ret ); RPoolStats ( MmPagedPool );
119 //RPoolRedZoneCheck ( MmPagedPool, __FILE__, __LINE__ );
124 void TestFree ( PVOID ptr
)
126 //printf ( "Freeing block %x: ", ptr ); RPoolStats ( MmPagedPool );
127 //RPoolRedZoneCheck ( MmPagedPool, __FILE__, __LINE__ );
128 ExFreePagedPool(ptr
);
129 //printf ( "Block %x freed: ", ptr ); RPoolStats ( MmPagedPool );
130 //RPoolRedZoneCheck ( MmPagedPool, __FILE__, __LINE__ );
137 char* keepers
[COUNT
];
139 int AllocSize
[] = { 15, 31, 63, 127, 255, 511, 1023, 2047 };
140 const int ALLOCS
= sizeof(AllocSize
) / sizeof(0[AllocSize
]);
143 MmPagedPoolSize
= 1*1024*1024;
144 MmPagedPoolBase
= malloc ( MmPagedPoolSize
);
145 MmInitializePagedPool();
147 dwStart
= GetTickCount();
149 printf ( "test #1 phase #1\n" );
150 for ( i
= 0; i
< COUNT
; i
++ )
152 //printf ( "keeper %i) ", i );
153 keepers
[i
] = TestAlloc ( AllocSize
[i
%ALLOCS
] );
154 if ( !keepers
[i
] ) printf ( "allocation failed\n" );
155 //printf ( "trash %i) ", i );
156 trash
[i
] = TestAlloc ( AllocSize
[i
%ALLOCS
] );
157 if ( !trash
[i
] ) printf ( "allocation failed\n" );
160 printf ( "test #1 phase #2\n" );
161 for ( i
= 0; i
< COUNT
; i
++ )
165 //printf ( "%i) ", i );
166 TestFree ( trash
[i
] );
169 printf ( "test #1 phase #3\n" );
170 for ( i
= 0; i
< 4; i
++ )
172 //printf ( "%i) ", i );
173 keepers
[i
] = TestAlloc ( 4096 );
174 if ( !keepers
[i
] ) printf ( "allocation failed\n" );
177 printf ( "test #1 phase #4\n" );
178 for ( i
= 0; i
< 4; i
++ )
180 //printf ( "%i) ", i );
181 TestFree ( keepers
[i
] );
184 printf ( "test #1 phase #5\n" );
186 for ( i
= 0; i
< COUNT
; i
++ )
188 //printf ( "%i) ", i );
189 trash
[i
] = TestAlloc ( rand()%1024+1 );
190 if ( !trash
[i
] ) printf ( "allocation failed\n" );
192 printf ( "test #1 phase #6\n" );
193 for ( i
= 0; i
< 10000; i
++ )
195 TestFree ( trash
[i
%COUNT
] );
196 trash
[i
%COUNT
] = TestAlloc ( rand()%1024+1 );
197 if ( !trash
[i
%COUNT
] ) printf ( "allocation failed\n" );
199 printf ( "test #1 phase #7\n" );
201 for ( i
= 0; i
< COUNT
; i
++ )
205 TestFree ( trash
[i
] );
209 printf ( "test #1 phase #8 ( freed %i of %i trash )\n", j
, COUNT
);
210 if ( !TestAlloc ( 2048 ) )
211 printf ( "Couldn't allocate 2048 bytes after freeing up a whole bunch of blocks\n" );
213 free ( MmPagedPoolBase
);
215 printf ( "test time: %lu\n", GetTickCount() - dwStart
);
217 printf ( "test #2\n" );
219 MmPagedPoolSize
= 1024;
220 MmPagedPoolBase
= malloc ( MmPagedPoolSize
);
221 MmInitializePagedPool();
224 i
= RPoolLargestAllocPossible ( MmPagedPool
, 0 );
225 if ( !TestAlloc ( i
) )
227 printf ( "allocating last available block failed\n" );
230 free ( MmPagedPoolBase
);
232 printf ( "done!\n" );
235 #endif//PPOOL_UMODE_TEST