Advertisement
Blizzardo1

Memory<T> Testing

Jul 18th, 2022
818
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.43 KB | None | 0 0
  1. // This should comply and be a safe way to handle Memory<T>.
  2. // This can actually be used in a class implementation such as:
  3. // public class MemoryBlock<T> {
  4. //    private Memory<T> block;
  5. //    public MemoryBlock<T>(int size) {
  6. //        block = new(new T[size], 0, size);
  7. //    }
  8. // }
  9. // Thus all operations can be wrapped in the class for better memory management.
  10. // It would be better to just use a MemoryPool<T> and create an owner for proper disposal instead of this hack.
  11. namespace MemoryTest
  12. {
  13.     internal static class Program
  14.     {
  15.         private static Memory<byte> _memoryBuffer;
  16.  
  17.         private static void Alloc(int size)
  18.         {
  19.             byte[] bytes = new byte[size];
  20.             _memoryBuffer = new(bytes, 0, size);
  21.         }
  22.        
  23.         private static bool Free()
  24.         {
  25.             if(!_memoryBuffer.IsEmpty)
  26.             {
  27.                 byte[] nbuff = new byte[_memoryBuffer.Length];
  28.                 Memory<byte> destroy = new(nbuff, 0, _memoryBuffer.Length);
  29.                 destroy.CopyTo(_memoryBuffer);
  30.                 _memoryBuffer = _memoryBuffer.Trim<byte>(0);
  31.             }
  32.             Console.WriteLine("Memory Freed");
  33.             return true;
  34.         }
  35.  
  36.         private static byte GetMemoryByte(int location)
  37.         {
  38.             return _memoryBuffer.Span[location];
  39.         }
  40.  
  41.         private static void SetMemory(byte[] buffer)
  42.         {
  43.             var tmp = new Memory<byte>(buffer);
  44.             if(!tmp.TryCopyTo(_memoryBuffer))
  45.             {
  46.                 Console.WriteLine("Could not place memory");
  47.             }
  48.         }
  49.  
  50.         private static void PrintMemory()
  51.         {
  52.             for (int i = 0; i < _memoryBuffer.Length; i++)
  53.             {
  54.                 var b = GetMemoryByte(i);
  55.                 Console.Write((char)b);
  56.             }
  57.             Console.WriteLine();
  58.         }
  59.  
  60.         private static void Main(string[] args)
  61.         {
  62.             string msg = "This is a test.";
  63.            
  64.             Alloc(msg.Length);
  65.             SetMemory(System.Text.Encoding.ASCII.GetBytes(msg));
  66.             PrintMemory();
  67.  
  68.             Free();
  69.             PrintMemory();
  70.            
  71.             msg = "Oh look, another test";
  72.            
  73.             Alloc(msg.Length);
  74.             SetMemory(System.Text.Encoding.ASCII.GetBytes(msg));
  75.             PrintMemory();
  76.            
  77.             Free();
  78.             PrintMemory();
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement