FRAMEWORK/MemChain.hpp
//---------------------------------------------------------------------------
// No wasting space for a Chunk size - you responsible for knowing it (It would require 16 bytes to keep SIMD alignment)  // Perfect for 'operator new'
// TODO: Optimize, if possible?
// It is big but avoids excess overwritings in CPU cache and 'IF' duplicates
// ???: Check if Ptr is in current block and that Ptr and Size are 'sizeof(SEmpty)' aligned?
// Blocks can allocate more pages sequentially, but can`t release them (To avoid last item Alloc-Free-Alloc cycle overhead)
//
No wasting space for a Chunk size - you responsible for knowing it (It would require 16 bytes to keep SIMD alignment)  // Perfect for 'operator new'
TODO: Optimize, if possible.
It is big but avoids excess overwritings in CPU cache and 'IF' duplicates
???: Check if Ptr is in current block and that Ptr and Size are 'sizeof(SEmpty)' aligned.
Blocks can allocate more pages sequentially, but can`t release them (To avoid last item Alloc-Free-Alloc cycle overhead)
No wasting space for a Chunk size - you responsible for knowing it ( It would require 16 bytes to keep SIMD alignment ) // Perfect for ' operator new ' TODO : Optimize if possible . It is big but avoids excess overwritings in CPU cache and ' IF ' duplicates ??? : Check if Ptr is in current block and that Ptr and Size are ' sizeof ( SEmpty ) ' aligned . Blocks can allocate more pages sequentially but can ` t release them ( To avoid last item Alloc - Free - Alloc cycle overhead )
bool Free(void* Ptr, SIZE_T Size)    
{
 SIZE_T Offs = (UINT8*)Ptr - (UINT8*)this;         
 SIZE_T FLen = AlignFrwrdPow2(Size, BAlignment);  
 if(!this->Empty)    
  {
   SEmpty* Curr = (SEmpty*)Ptr;
   Curr->Next   = 0;
   Curr->Size   = FLen;  
   this->Empty  = Offs;
   return true;
  }
 SIZE_T EmOffs = this->Empty;  
 if(Offs < EmOffs)      
  {
   SEmpty* ENxt = (SEmpty*)Ptr;
   if((Offs + FLen) == this->Empty)      
    {
     SEmpty* ELst = (SEmpty*)((UINT8*)this + this->Empty);
     ENxt->Next = ELst->Next;
     ENxt->Size = FLen + ELst->Size;    
    }
     else
      {
       ENxt->Next = this->Empty;
       ENxt->Size = FLen;  
      }
   this->Empty = Offs;
   return true;
  }
 for(;;)
  {
   SEmpty* Curr = (SEmpty*)((UINT8*)this + EmOffs);
   if(!Curr->Next)   
    {
     if((EmOffs + Curr->Size) == Offs)Curr->Size += FLen;     
       else
        {
         SEmpty* ENxt = (SEmpty*)Ptr;
         ENxt->Next   = 0;
         ENxt->Size   = FLen;  
         Curr->Next   = Offs;
        }
     return true;
    }
   if(Offs < Curr->Next)  
    {
     if((EmOffs + Curr->Size) == Offs)  
      {      
       if((Offs + FLen) == Curr->Next)   
        {
         SEmpty* ENxt = (SEmpty*)((UINT8*)this + Curr->Next);
         Curr->Size  += FLen + ENxt->Size;
         Curr->Next   = ENxt->Next;
         return true;
        }
       Curr->Size += FLen;
       return true;
      }
     if((Offs + FLen) == Curr->Next)       - merge with it    // Not right after Curr, it is some gap there
      {
       SEmpty* ENxt = (SEmpty*)((UINT8*)this + Curr->Next);
       SEmpty* ENew = (SEmpty*)Ptr;
       ENew->Next   = ENxt->Next;
       ENew->Size   = FLen + ENxt->Size;  
       Curr->Next   = Offs;
       return true;
      }
     SEmpty* ENxt = (SEmpty*)Ptr;     
     ENxt->Next   = Curr->Next;
     ENxt->Size   = FLen;  
     Curr->Next   = Offs;
     return true;
    }
   EmOffs = Curr->Next;
  }         
 return false;
}
bool Free ( void * Ptr , SIZE_T Size ) { SIZE_T Offs = ( UINT8 * ) Ptr - ( UINT8 * ) this ; SIZE_T FLen = AlignFrwrdPow2 ( Size , BAlignment ) ; if ( ! this -> Empty ) { SEmpty * Curr = ( SEmpty * ) Ptr ; Curr -> Next = 0 ; Curr -> Size = FLen ; this -> Empty = Offs ; return true ; } SIZE_T EmOffs = this -> Empty ; if ( Offs < EmOffs ) { SEmpty * ENxt = ( SEmpty * ) Ptr ; if ( ( Offs + FLen ) == this -> Empty ) { SEmpty * ELst = ( SEmpty * ) ( ( UINT8 * ) this + this -> Empty ) ; ENxt -> Next = ELst -> Next ; ENxt -> Size = FLen + ELst -> Size ; } else { ENxt -> Next = this -> Empty ; ENxt -> Size = FLen ; } this -> Empty = Offs ; return true ; } for ( ; ; ) { SEmpty * Curr = ( SEmpty * ) ( ( UINT8 * ) this + EmOffs ) ; if ( ! Curr -> Next ) { if ( ( EmOffs + Curr -> Size ) == Offs ) Curr -> Size += FLen ; else { SEmpty * ENxt = ( SEmpty * ) Ptr ; ENxt -> Next = 0 ; ENxt -> Size = FLen ; Curr -> Next = Offs ; } return true ; } if ( Offs < Curr -> Next ) { if ( ( EmOffs + Curr -> Size ) == Offs ) { if ( ( Offs + FLen ) == Curr -> Next ) { SEmpty * ENxt = ( SEmpty * ) ( ( UINT8 * ) this + Curr -> Next ) ; Curr -> Size += FLen + ENxt -> Size ; Curr -> Next = ENxt -> Next ; return true ; } Curr -> Size += FLen ; return true ; } if ( ( Offs + FLen ) == Curr -> Next ) { SEmpty * ENxt = ( SEmpty * ) ( ( UINT8 * ) this + Curr -> Next ) ; SEmpty * ENew = ( SEmpty * ) Ptr ; ENew -> Next = ENxt -> Next ; ENew -> Size = FLen + ENxt -> Size ; Curr -> Next = Offs ; return true ; } SEmpty * ENxt = ( SEmpty * ) Ptr ; ENxt -> Next = Curr -> Next ; ENxt -> Size = FLen ; Curr -> Next = Offs ; return true ; } EmOffs = Curr -> Next ; } return false ; }
No wasting space for a Chunk size - you responsible for knowing it (It would require 16 bytes to keep SIMD alignment)  // Perfect for 'operator new'
TODO: Optimize, if possible?
No wasting space for a Chunk size - you responsible for knowing it ( It would require 16 bytes to keep SIMD alignment ) // Perfect for ' operator new ' TODO : Optimize if possible?
// Ptr must be in current block   // Check that PTR and SIZE are 'sizeof(SEmpty)' aligned // Headers also aligned to this value (Min sizeof(SEmpty))  // Init a first Empty // Curr->Size is not SIZE_T // Initial offset // Replace 'this->Empty' // Only first record, others will go with '(Offs < Curr->Next)' // Right before Next // Combine with Prev // Curr->Size is not SIZE_T // Add Last // Right after Curr - enlarge it // Curr->Size is not SIZE_T // Between Curr And Next // Right after Curr - enlarge it // Chunk fits right in the gap between Curr and Next - Merge them // Right before Next - merge with it    // Not right after Curr, it is some gap there // Curr->Size is not SIZE_T // Insert between Curr and Next // Curr->Size is not SIZE_T