private MemoryWrapper AllocMemoryWrapper(HeapMemory allocatedMemory, ArraySegment <byte>?directMemory) { bool pooled; if (!(pooled = _pooledMemoryWrappers.TryDequeue(out MemoryWrapper wrapper))) { int createdMemoryWrappers = Interlocked.Increment(ref _createdMemoryWrappers); if (createdMemoryWrappers >= 1024 && !_hasWarnedAboutMemoryWrapperLeaks) { if (Logging.CurrentLogLevel <= LogLevel.Warning) { Logging.LogWarning("Memory leak suspected. Are you leaking memory to the GC or are your windows too large? Leaking memory to the GC will cause slowdowns. Make sure all memory is deallocated. [MEMORY WRAPPER]"); } _hasWarnedAboutMemoryWrapperLeaks = true; } wrapper = new MemoryWrapper(); } wrapper.IsDead = false; wrapper.AllocatedMemory = allocatedMemory; wrapper.DirectMemory = directMemory; #if RUFFLES_DEBUG // The allocation stacktrace allows us to see where the alloc occured that caused the leak wrapper.allocStacktrace = new StackTrace(true); #endif return(wrapper); }
internal static HeapMemory Alloc(uint size) { uint allocSize = Math.Max(minBufferSize, CalculateMultiple(size, bufferMultiple)); if (_pooledMemory.Count == 0) { _createdPools++; if (_createdPools >= 1024 && !_hasWarnedAboutLeak) { Console.WriteLine("Memory leak detected. Are you leaking memory to the GC or are your windows too large? Leaking memory to the GC will cause slowdowns. Make sure all memory is deallocated."); _hasWarnedAboutLeak = true; } return(new HeapMemory(allocSize)); } HeapMemory memory = _pooledMemory.Dequeue(); memory.EnsureSize(allocSize); memory.isDead = false; memory.VirtualCount = allocSize; memory.VirtualOffset = 0; return(memory); }
internal void DeAlloc(HeapMemory memory) { if (memory.isDead) { throw new MemoryException("Cannot deallocate already dead memory"); } memory.isDead = true; memory.VirtualOffset = 0; memory.VirtualCount = 0; _pooledHeapMemory.Enqueue(memory); }
internal HeapMemory AllocHeapMemory(uint size) { uint allocSize = Math.Max(minHeapMemorySize, CalculateMultiple(size, heapMemorySizeMultiple)); bool pooled; if (!(pooled = _pooledHeapMemory.TryDequeue(out HeapMemory memory))) { int createdHeapMemory = Interlocked.Increment(ref _createdHeapMemory); if (createdHeapMemory >= 1024 && !_hasWarnedAboutHeapMemoryLeaks) { if (Logging.CurrentLogLevel <= LogLevel.Warning) { Logging.LogWarning("Memory leak suspected. Are you leaking memory to the GC or are your windows too large? Leaking memory to the GC will cause slowdowns. Make sure all memory is deallocated. [HEAP MEMORY]"); } _hasWarnedAboutHeapMemoryLeaks = true; } memory = new HeapMemory(allocSize); } memory.EnsureSize(allocSize); memory.IsDead = false; memory.VirtualCount = size; memory.VirtualOffset = 0; if (pooled) { // If we got one from the pool, we need to clear it Array.Clear(memory.Buffer, 0, memory.Buffer.Length); } #if RUFFLES_DEBUG // The allocation stacktrace allows us to see where the alloc occured that caused the leak memory.allocStacktrace = new StackTrace(true); #endif return(memory); }
internal void DeAlloc(HeapMemory memory) { if (memory.IsDead) { throw new MemoryException("Cannot deallocate already dead memory"); } memory.VirtualOffset = 0; memory.VirtualCount = 0; memory.IsDead = true; if (!_pooledHeapMemory.TryEnqueue(memory)) { // Failed to enqueue memory. Queue is full if (Logging.CurrentLogLevel <= LogLevel.Warning) { Logging.LogWarning("Could not return heap memory. The queue is full. The memory will be given to the garbage collector. [HEAP MEMORY]"); } } }
internal MemoryWrapper AllocMemoryWrapper(HeapMemory allocatedMemory) { return(AllocMemoryWrapper(allocatedMemory, null)); }