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 *****************************************************************/
49 MmInitializePagedPool(VOID
)
52 * We are still at a high IRQL level at this point so explicitly commit
53 * the first page of the paged pool before writing the first block header.
55 MmCommitPagedPoolAddress ( (PVOID
)MmPagedPoolBase
, FALSE
);
57 MmPagedPool
= RPoolInit ( MmPagedPoolBase
,
63 ExInitializeFastMutex(&MmPagedPool
->Mutex
);
66 /**********************************************************************
68 * ExAllocatePagedPoolWithTag@12
77 ExAllocatePagedPoolWithTag (IN POOL_TYPE PoolType
,
78 IN ULONG NumberOfBytes
,
83 if ( NumberOfBytes
>= PAGE_SIZE
)
85 else if ( PoolType
& CACHE_ALIGNED_POOL_MASK
)
90 ASSERT_IRQL(APC_LEVEL
);
92 return RPoolAlloc ( MmPagedPool
, NumberOfBytes
, Tag
, align
);
96 ExFreePagedPool(IN PVOID Block
)
98 ASSERT_IRQL(APC_LEVEL
);
99 RPoolFree ( MmPagedPool
, Block
);
103 #ifdef PPOOL_UMODE_TEST
105 PVOID
TestAlloc ( ULONG Bytes
)
109 //printf ( "Allocating block: " ); RPoolStats ( MmPagedPool );
110 //RPoolRedZoneCheck ( MmPagedPool, __FILE__, __LINE__ );
112 ret
= ExAllocatePagedPoolWithTag ( PagedPool
, Bytes
, 0 );
114 //printf ( "Block %x allocated: ", ret ); RPoolStats ( MmPagedPool );
115 //RPoolRedZoneCheck ( MmPagedPool, __FILE__, __LINE__ );
120 void TestFree ( PVOID ptr
)
122 //printf ( "Freeing block %x: ", ptr ); RPoolStats ( MmPagedPool );
123 //RPoolRedZoneCheck ( MmPagedPool, __FILE__, __LINE__ );
124 ExFreePagedPool(ptr
);
125 //printf ( "Block %x freed: ", ptr ); RPoolStats ( MmPagedPool );
126 //RPoolRedZoneCheck ( MmPagedPool, __FILE__, __LINE__ );
133 char* keepers
[COUNT
];
135 int AllocSize
[] = { 15, 31, 63, 127, 255, 511, 1023, 2047 };
136 const int ALLOCS
= sizeof(AllocSize
) / sizeof(0[AllocSize
]);
139 MmPagedPoolSize
= 1*1024*1024;
140 MmPagedPoolBase
= malloc ( MmPagedPoolSize
);
141 MmInitializePagedPool();
143 dwStart
= GetTickCount();
145 printf ( "test #1 phase #1\n" );
146 for ( i
= 0; i
< COUNT
; i
++ )
148 //printf ( "keeper %i) ", i );
149 keepers
[i
] = TestAlloc ( AllocSize
[i
%ALLOCS
] );
150 if ( !keepers
[i
] ) printf ( "allocation failed\n" );
151 //printf ( "trash %i) ", i );
152 trash
[i
] = TestAlloc ( AllocSize
[i
%ALLOCS
] );
153 if ( !trash
[i
] ) printf ( "allocation failed\n" );
156 printf ( "test #1 phase #2\n" );
157 for ( i
= 0; i
< COUNT
; i
++ )
161 //printf ( "%i) ", i );
162 TestFree ( trash
[i
] );
165 printf ( "test #1 phase #3\n" );
166 for ( i
= 0; i
< 4; i
++ )
168 //printf ( "%i) ", i );
169 keepers
[i
] = TestAlloc ( 4096 );
170 if ( !keepers
[i
] ) printf ( "allocation failed\n" );
173 printf ( "test #1 phase #4\n" );
174 for ( i
= 0; i
< 4; i
++ )
176 //printf ( "%i) ", i );
177 TestFree ( keepers
[i
] );
180 printf ( "test #1 phase #5\n" );
182 for ( i
= 0; i
< COUNT
; i
++ )
184 //printf ( "%i) ", i );
185 trash
[i
] = TestAlloc ( rand()%1024+1 );
186 if ( !trash
[i
] ) printf ( "allocation failed\n" );
188 printf ( "test #1 phase #6\n" );
189 for ( i
= 0; i
< 10000; i
++ )
191 TestFree ( trash
[i
%COUNT
] );
192 trash
[i
%COUNT
] = TestAlloc ( rand()%1024+1 );
193 if ( !trash
[i
%COUNT
] ) printf ( "allocation failed\n" );
195 printf ( "test #1 phase #7\n" );
197 for ( i
= 0; i
< COUNT
; i
++ )
201 TestFree ( trash
[i
] );
205 printf ( "test #1 phase #8 ( freed %i of %i trash )\n", j
, COUNT
);
206 if ( !TestAlloc ( 2048 ) )
207 printf ( "Couldn't allocate 2048 bytes after freeing up a whole bunch of blocks\n" );
209 free ( MmPagedPoolBase
);
211 printf ( "test time: %lu\n", GetTickCount() - dwStart
);
213 printf ( "test #2\n" );
215 MmPagedPoolSize
= 1024;
216 MmPagedPoolBase
= malloc ( MmPagedPoolSize
);
217 MmInitializePagedPool();
220 i
= RPoolLargestAllocPossible ( MmPagedPool
, 0 );
221 if ( !TestAlloc ( i
) )
223 printf ( "allocating last available block failed\n" );
226 free ( MmPagedPoolBase
);
228 printf ( "done!\n" );
231 #endif//PPOOL_UMODE_TEST