GAudio.GATDataAllocator.GetDataContainer C# (CSharp) Method

GetDataContainer() public method

Finds and virtually allocates a GATManagedData instance before returning it as a GATData reference. First, the algorithm looks at the free chunks bin of appropriate size. If the bin doesn't hold any chunk, it will check if there is enough unfragmented space. If there isn't, it will look at bins holding larger chunks and fragment one if found. Finally, it will attempt defragmenting and run again, before logging an out of memory error.
public GetDataContainer ( int size ) : GATData
size int /// Size of the chunk to virtually allocate. ///
return GATData
        public GATData GetDataContainer( int size )
        {
            GATManagedData chunk = null;
            int binIndex;
            int binSize;

            binIndex = GetBinIndexForSize( size );
            binSize  = _binWidth + binIndex * _binWidth;

            if( _freeChunksBins[ binIndex ].Count != 0 )
            {
                chunk = _freeChunksBins[ binIndex ].Pop ();
            }
            else
            {
                if( _unallocatedCursor.MaxSize >= binSize )
                {
                    chunk = _unallocatedCursor;
                    chunk.allocatedSize = size;
                    _unallocatedCursor = GetOrMakeChunk();
                    _unallocatedCursor.AllocateFree( chunk.MemOffset + binSize, _endCursor );
                    chunk.next = _unallocatedCursor;
                }
                else
                {
                    if( TryFragmentBins( binIndex + 1, binSize, ref chunk ) == false )
                    {
                        Defragment();

                        if( _freeChunksBins[ binIndex ].Count != 0 )
                        {
                            chunk = _freeChunksBins[ binIndex ].Pop ();
                        }
                        else
                        {
                            if( _unallocatedCursor.MaxSize >= binSize )
                            {
                                chunk = _unallocatedCursor;
                                chunk.allocatedSize = size;
                                _unallocatedCursor = GetOrMakeChunk();
                                _unallocatedCursor.AllocateFree( chunk.MemOffset + binSize, _endCursor );
                                chunk.next = _unallocatedCursor;
                            }
                            else if( TryFragmentBins( binIndex + 1, binSize, ref chunk ) == false )
                            {
                                throw new GATException( "Out of memory!" );
                            }
                        }
                    }
                }
            }

            chunk.allocatedSize = size;

            return chunk;
        }

Usage Example

Beispiel #1
0
 /// <summary>
 /// Convenience method to request a chunk of virtual memory from the default
 /// GATDataAllocator instance.
 /// </summary>
 public static GATData GetDataContainer(int length)
 {
     return(__allocator.GetDataContainer(length));
 }