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
23 #define ASSERT(x) if (!(x)) {DbgPrint("Assertion "#x" failed at %s:%d\n", __FILE__,__LINE__); KeBugCheck(0); }
27 #define R_MUTEX FAST_MUTEX
28 #define R_ACQUIRE_MUTEX(pool) /*DPRINT1("Acquiring PPool Mutex\n");*/ ExAcquireFastMutex(&pool->Mutex)
29 #define R_RELEASE_MUTEX(pool) /*DPRINT1("Releasing PPool Mutex\n");*/ ExReleaseFastMutex(&pool->Mutex)
30 #define R_PRINT_ADDRESS(addr) KeRosPrintAddress(addr)
31 #define R_PANIC() KeBugCheck(0)
32 #define R_DEBUG DbgPrint
33 #define R_GET_STACK_FRAMES(ptr,cnt) KeRosGetStackFrames(ptr,cnt)
37 /* GLOBALS *******************************************************************/
39 PVOID MmPagedPoolBase
;
40 ULONG MmPagedPoolSize
;
41 ULONG MmTotalPagedPoolQuota
= 0; // TODO FIXME commented out until we use it
42 static PR_POOL MmPagedPool
= NULL
;
44 /* FUNCTIONS *****************************************************************/
47 MmInitializePagedPool(VOID
)
50 * We are still at a high IRQL level at this point so explicitly commit
51 * the first page of the paged pool before writing the first block header.
53 MmCommitPagedPoolAddress ( (PVOID
)MmPagedPoolBase
, FALSE
);
55 MmPagedPool
= RPoolInit ( MmPagedPoolBase
,
61 ExInitializeFastMutex(&MmPagedPool
->Mutex
);
64 /**********************************************************************
66 * ExAllocatePagedPoolWithTag@12
75 ExAllocatePagedPoolWithTag (IN POOL_TYPE PoolType
,
76 IN ULONG NumberOfBytes
,
81 if ( NumberOfBytes
>= PAGE_SIZE
)
83 else if ( PoolType
& CACHE_ALIGNED_POOL_MASK
)
88 ASSERT_IRQL(APC_LEVEL
);
90 return RPoolAlloc ( MmPagedPool
, NumberOfBytes
, Tag
, align
);
94 ExFreePagedPool(IN PVOID Block
)
96 ASSERT_IRQL(APC_LEVEL
);
97 RPoolFree ( MmPagedPool
, Block
);
101 #ifdef PPOOL_UMODE_TEST
103 PVOID
TestAlloc ( ULONG Bytes
)
107 //printf ( "Allocating block: " ); RPoolStats ( MmPagedPool );
108 //RPoolRedZoneCheck ( MmPagedPool, __FILE__, __LINE__ );
110 ret
= ExAllocatePagedPoolWithTag ( PagedPool
, Bytes
, 0 );
112 //printf ( "Block %x allocated: ", ret ); RPoolStats ( MmPagedPool );
113 //RPoolRedZoneCheck ( MmPagedPool, __FILE__, __LINE__ );
118 void TestFree ( PVOID ptr
)
120 //printf ( "Freeing block %x: ", ptr ); RPoolStats ( MmPagedPool );
121 //RPoolRedZoneCheck ( MmPagedPool, __FILE__, __LINE__ );
122 ExFreePagedPool(ptr
);
123 //printf ( "Block %x freed: ", ptr ); RPoolStats ( MmPagedPool );
124 //RPoolRedZoneCheck ( MmPagedPool, __FILE__, __LINE__ );
131 char* keepers
[COUNT
];
133 int AllocSize
[] = { 15, 31, 63, 127, 255, 511, 1023, 2047 };
134 const int ALLOCS
= sizeof(AllocSize
) / sizeof(0[AllocSize
]);
137 MmPagedPoolSize
= 1*1024*1024;
138 MmPagedPoolBase
= malloc ( MmPagedPoolSize
);
139 MmInitializePagedPool();
141 dwStart
= GetTickCount();
143 printf ( "test #1 phase #1\n" );
144 for ( i
= 0; i
< COUNT
; i
++ )
146 //printf ( "keeper %i) ", i );
147 keepers
[i
] = TestAlloc ( AllocSize
[i
%ALLOCS
] );
148 if ( !keepers
[i
] ) printf ( "allocation failed\n" );
149 //printf ( "trash %i) ", i );
150 trash
[i
] = TestAlloc ( AllocSize
[i
%ALLOCS
] );
151 if ( !trash
[i
] ) printf ( "allocation failed\n" );
154 printf ( "test #1 phase #2\n" );
155 for ( i
= 0; i
< COUNT
; i
++ )
159 //printf ( "%i) ", i );
160 TestFree ( trash
[i
] );
163 printf ( "test #1 phase #3\n" );
164 for ( i
= 0; i
< 4; i
++ )
166 //printf ( "%i) ", i );
167 keepers
[i
] = TestAlloc ( 4096 );
168 if ( !keepers
[i
] ) printf ( "allocation failed\n" );
171 printf ( "test #1 phase #4\n" );
172 for ( i
= 0; i
< 4; i
++ )
174 //printf ( "%i) ", i );
175 TestFree ( keepers
[i
] );
178 printf ( "test #1 phase #5\n" );
180 for ( i
= 0; i
< COUNT
; i
++ )
182 //printf ( "%i) ", i );
183 trash
[i
] = TestAlloc ( rand()%1024+1 );
184 if ( !trash
[i
] ) printf ( "allocation failed\n" );
186 printf ( "test #1 phase #6\n" );
187 for ( i
= 0; i
< 10000; i
++ )
189 TestFree ( trash
[i
%COUNT
] );
190 trash
[i
%COUNT
] = TestAlloc ( rand()%1024+1 );
191 if ( !trash
[i
%COUNT
] ) printf ( "allocation failed\n" );
193 printf ( "test #1 phase #7\n" );
195 for ( i
= 0; i
< COUNT
; i
++ )
199 TestFree ( trash
[i
] );
203 printf ( "test #1 phase #8 ( freed %i of %i trash )\n", j
, COUNT
);
204 if ( !TestAlloc ( 2048 ) )
205 printf ( "Couldn't allocate 2048 bytes after freeing up a whole bunch of blocks\n" );
207 free ( MmPagedPoolBase
);
209 printf ( "test time: %lu\n", GetTickCount() - dwStart
);
211 printf ( "test #2\n" );
213 MmPagedPoolSize
= 1024;
214 MmPagedPoolBase
= malloc ( MmPagedPoolSize
);
215 MmInitializePagedPool();
218 i
= RPoolLargestAllocPossible ( MmPagedPool
, 0 );
219 if ( !TestAlloc ( i
) )
221 printf ( "allocating last available block failed\n" );
224 free ( MmPagedPoolBase
);
226 printf ( "done!\n" );
229 #endif//PPOOL_UMODE_TEST