+ if( indx >= 0 )
+ giLastPossibleFree = indx;
+ LOG("indx = %i", indx);
+ #else
+ c = giLastPossibleFree % 32;
+ b = (giLastPossibleFree / 32) % 32;
+ a = giLastPossibleFree / 1024;
+
+ LOG("a=%i,b=%i,c=%i", a, b, c);
+ for( ; gaSuperBitmap[a] == -1 && a >= 0; a-- );
+ if(a < 0) {
+ Mutex_Release( &glPhysAlloc );
+ Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p) - %lli/%lli used",
+ __builtin_return_address(0), giPhysAlloc, giPageCount);
+ LEAVE('i', 0);
+ return 0;
+ }
+ for( ; gaSuperBitmap[a] & (1<<b); b-- );
+ for( ; gaPageBitmap[a*32+b] & (1<<c); c-- );
+ LOG("a=%i,b=%i,c=%i", a, b, c);
+ indx = (a << 10) | (b << 5) | c;
+ if( indx >= 0 )
+ giLastPossibleFree = indx;
+ #endif
+
+ if( indx < 0 ) {
+ Mutex_Release( &glPhysAlloc );
+ Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p) - %lli/%lli used (indx = %x)",
+ __builtin_return_address(0), giPhysAlloc, giPageCount, indx);
+ Log_Debug("PMem", "giLastPossibleFree = %lli", giLastPossibleFree);
+ LEAVE('i', 0);
+ return 0;
+ }
+
+ if( indx > 0xFFFFF ) {
+ Panic("The fuck? Too many pages! (indx = 0x%x)", indx);
+ }
+
+ if( indx >= giPageCount ) {
+ Mutex_Release( &glPhysAlloc );
+ Log_Error("PMem", "MM_AllocPhys - indx(%i) > giPageCount(%i)", indx, giPageCount);
+ LEAVE('i', 0);
+ return 0;
+ }
+
+ // Mark page used
+ if( MM_GetPhysAddr( (tVAddr)&gaPageReferences[indx] ) )
+ gaPageReferences[indx] = 1;
+ gaPageBitmap[ indx>>5 ] |= 1 << (indx&31);
+
+ giPhysAlloc ++;
+
+ // Get address
+ ret = indx << 12;
+
+ // Mark used block
+ if(gaPageBitmap[ indx>>5 ] == -1) {
+ gaSuperBitmap[indx>>10] |= 1 << ((indx>>5)&31);
+ }
+
+ // Release Spinlock
+ Mutex_Release( &glPhysAlloc );
+
+ LEAVE('X', ret);
+ #if TRACE_ALLOCS
+ if( now() > 4000 ) {
+ Log_Debug("PMem", "MM_AllocPhys: RETURN %P (%i free)", ret, giPageCount-giPhysAlloc);
+ Proc_PrintBacktrace();
+ }
+ #endif
+ return ret;