+ gaPageReferences[ indx ] = 1;
+ gaPageBitmap[ indx>>5 ] |= 1 << (indx&31);
+
+
+ // Get address
+ ret = indx << 12;
+ giLastPossibleFree = indx;
+
+ // Mark used block
+ if(gaPageBitmap[ indx>>5 ] == -1)
+ gaSuperBitmap[indx>>10] |= 1 << ((indx>>5)&31);
+
+ // Release Spinlock
+ RELEASE( &giPhysAlloc );
+
+ LEAVE('X', ret);
+ //Log("MM_AllocPhys: RETURN 0x%x", ret);
+ return ret;
+}
+
+/**
+ * \fn tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
+ * \brief Allocate a range of physical pages
+ * \param Pages Number of pages to allocate
+ * \param MaxBits Maximum number of address bits to use
+ */
+tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
+{
+ int a, b;
+ int i, idx, sidx;
+ tPAddr ret;
+
+ ENTER("iPages iMaxBits", Pages, MaxBits);
+
+ // Sanity Checks
+ if(MaxBits < 0) {
+ LEAVE('i', 0);
+ return 0;
+ }
+ if(MaxBits > PHYS_BITS) MaxBits = PHYS_BITS;
+
+ // Lock
+ LOCK( &giPhysAlloc );
+
+ // Set up search state
+ if( giLastPossibleFree > ((tPAddr)1 << (MaxBits-12)) ) {
+ sidx = (tPAddr)1 << (MaxBits-12);
+ }
+ else {
+ sidx = giLastPossibleFree;
+ }
+ idx = sidx / 32;
+ sidx %= 32;
+ b = idx % 32;
+ a = idx / 32;
+
+ #if 0
+ LOG("a=%i, b=%i, idx=%i, sidx=%i", a, b, idx, sidx);
+
+ // Find free page
+ for( ; gaSuperBitmap[a] == -1 && a --; ) b = 31;
+ if(a < 0) {
+ RELEASE( &giPhysAlloc );
+ Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
+ LEAVE('i', 0);
+ return 0;
+ }
+ LOG("a = %i", a);
+ for( ; gaSuperBitmap[a] & (1 << b); b-- ) sidx = 31;
+ LOG("b = %i", b);
+ idx = a * 32 + b;
+ for( ; gaPageBitmap[idx] & (1 << sidx); sidx-- )
+ LOG("gaPageBitmap[%i] = 0x%08x", idx, gaPageBitmap[idx]);
+
+ LOG("idx = %i, sidx = %i", idx, sidx);
+ #else
+
+ #endif
+
+ // Check if the gap is large enough
+ while( idx >= 0 )
+ {
+ // Find a free page
+ for( ; ; )
+ {
+ // Bulk Skip
+ if( gaPageBitmap[idx] == -1 ) {
+ idx --;
+ sidx = 31;
+ continue;
+ }
+
+ if( gaPageBitmap[idx] & (1 << sidx) ) {
+ sidx --;
+ if(sidx < 0) { sidx = 31; idx --; }
+ if(idx < 0) break;
+ continue;
+ }
+ break;
+ }
+ if( idx < 0 ) break;
+
+ // Check if it is a free range
+ for( i = 0; i < Pages; i++ )
+ {
+ // Used page? break
+ if( gaPageBitmap[idx] & (1 << sidx) )
+ break;
+
+ sidx --;
+ if(sidx < 0) { sidx = 31; idx --; }
+ if(idx < 0) break;
+ }
+
+ if( i == Pages )
+ break;
+ }
+
+ // Check if an address was found
+ if( idx < 0 ) {
+ RELEASE( &giPhysAlloc );
+ Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
+ LEAVE('i', 0);
+ return 0;
+ }
+
+ // Mark pages used
+ for( i = 0; i < Pages; i++ )
+ {
+ if(gaPageReferences)
+ gaPageReferences[idx*32+sidx] = 1;
+ gaPageBitmap[ idx ] |= 1 << sidx;
+ sidx ++;
+ if(sidx == 32) { sidx = 0; idx ++; }
+ }