11using Microsoft . CodeAnalysis ;
22using System ;
3+ using System . IO ;
34using System . Text ;
45
56namespace StackMemoryCollections
@@ -348,23 +349,35 @@ public void ReducingCapacity(in nuint reducingCount)
348349 throw new Exception(""Failed to reduce available memory, stack moved further"");
349350 }}
350351
351- if(_head > _tail && Capacity - _head + 1 < reducingCount )
352+ if(Size == 0 )
352353 {{
353- _stackMemoryS->ShiftLeft((byte*)_start + _head, (long)(reducingCount * { sizeOf } ));
354+ _head = 0;
355+ _tail = 0;
356+ }}
357+ else
358+ if(_head > _tail)
359+ {{
360+ _stackMemoryS->ShiftLeft((byte*)(_start + _head), (byte*)(_start + (Capacity)), (long)(reducingCount * { sizeOf } ));
354361 _head -= reducingCount;{ incrementVersion }
355362 }}
356363 else
357364 if ((_head < _tail && Capacity - _tail + 1 < reducingCount) ||
358365 (_head == _tail && Capacity - _tail + 1 < reducingCount)
359366 )
360367 {{
361- _stackMemoryS->ShiftLeft((byte*)_start + _tail, (long)(reducingCount * { sizeOf } ));
362- _tail -= reducingCount;{ incrementVersion }
363- }}
364- else
365- {{
366- _stackMemoryS->FreeMemory(reducingCount * { sizeOf } );
368+ var freeCountToEnd = Capacity - (_head + 1);
369+ if(freeCountToEnd != 0 && freeCountToEnd <= reducingCount)
370+ {{
371+ //do nothing
372+ }}
373+ else
374+ {{
375+ _stackMemoryS->ShiftLeft((byte*)(_start + _head), (byte*)(_start + (_tail + 1)), (long)((Capacity - freeCountToEnd - ((_tail + 1) - _head)) * { sizeOf } ));
376+ _tail = 0;{ incrementVersion }
377+ }}
367378 }}
379+
380+ _stackMemoryS->FreeMemory(reducingCount * { sizeOf } );
368381 }}
369382 else if (_stackMemoryC != null)
370383 {{
@@ -373,23 +386,35 @@ public void ReducingCapacity(in nuint reducingCount)
373386 throw new Exception(""Failed to reduce available memory, stack moved further"");
374387 }}
375388
376- if (_head > _tail && Capacity - _head + 1 < reducingCount )
389+ if(Size == 0 )
377390 {{
378- _stackMemoryC.ShiftLeft((byte*)_start + _head, (long)(reducingCount * { sizeOf } ));
391+ _head = 0;
392+ _tail = 0;
393+ }}
394+ else
395+ if(_head > _tail)
396+ {{
397+ _stackMemoryC.ShiftLeft((byte*)(_start + _head), (byte*)(_start + (Capacity * { sizeOf } )), (long)(reducingCount * { sizeOf } ));
379398 _head -= reducingCount;{ incrementVersion }
380399 }}
381400 else
382401 if ((_head < _tail && Capacity - _tail + 1 < reducingCount) ||
383402 (_head == _tail && Capacity - _tail + 1 < reducingCount)
384403 )
385404 {{
386- _stackMemoryC.ShiftLeft((byte*)_start + _tail, (long)(reducingCount * { sizeOf } ));
387- _tail -= reducingCount;{ incrementVersion }
388- }}
389- else
390- {{
391- _stackMemoryC.FreeMemory(reducingCount * { sizeOf } );
392- }}
405+ var freeCountToEnd = Capacity - (_head + 1);
406+ if(freeCountToEnd != 0 && freeCountToEnd <= reducingCount)
407+ {{
408+ //do nothing
409+ }}
410+ else
411+ {{
412+ _stackMemoryC.ShiftLeft((byte*)(_start + _head), (byte*)(_start + (_tail + 1)), (long)((Capacity - freeCountToEnd - ((_tail + 1) - _head)) * { sizeOf } ));
413+ _tail = 0;{ incrementVersion }
414+ }}
415+ }}
416+
417+ _stackMemoryC.FreeMemory(reducingCount * { sizeOf } );
393418 }}
394419 }}
395420
@@ -468,7 +493,7 @@ public void ExpandCapacity(in nuint expandCount)
468493 _stackMemoryS->AllocateMemory(expandCount * { sizeOf } );
469494 if(Size != 0 && _head != 0 && _head > _tail)
470495 {{
471- _stackMemoryS->ShiftRight((byte*)_start + _head, (byte*)_start + (( _head + (Capacity - _head)) * { sizeOf } ), (long)(expandCount * { sizeOf } ));{ incrementVersion }
496+ _stackMemoryS->ShiftRight((byte*)( _start + _head) , (byte*)( _start + (_head + (Capacity - _head))), (long)(expandCount * { sizeOf } ));{ incrementVersion }
472497 _head += expandCount;
473498 }}
474499 }}
@@ -482,7 +507,7 @@ public void ExpandCapacity(in nuint expandCount)
482507 _stackMemoryC.AllocateMemory(expandCount * { sizeOf } );
483508 if (Size != 0 && _head != 0 && _head > _tail)
484509 {{
485- _stackMemoryC.ShiftRight((byte*)_start + _head, (byte*)_start + (( _head + (Capacity - _head)) * { sizeOf } ), (long)(expandCount * { sizeOf } ));{ incrementVersion }
510+ _stackMemoryC.ShiftRight((byte*)( _start + _head) , (byte*)( _start + (_head + (Capacity - _head))), (long)(expandCount * { sizeOf } ));{ incrementVersion }
486511 _head += expandCount;
487512 }}
488513 }}
@@ -567,7 +592,7 @@ public bool TryExpandCapacity(in nuint expandCount)
567592
568593 if(Size != 0 && _head != 0 && _head > _tail)
569594 {{
570- _stackMemoryS->ShiftRight((byte*)_start + _head, (byte*)_start + (( _head + (Capacity - _head)) * { sizeOf } ), (long)(expandCount * { sizeOf } ));{ incrementVersion }
595+ _stackMemoryS->ShiftRight((byte*)( _start + _head) , (byte*)( _start + (_head + (Capacity - _head))), (long)(expandCount * { sizeOf } ));{ incrementVersion }
571596 _head += expandCount;
572597 }}
573598 }}
@@ -585,7 +610,7 @@ public bool TryExpandCapacity(in nuint expandCount)
585610
586611 if (Size != 0 && _head != 0 && _head > _tail)
587612 {{
588- _stackMemoryC.ShiftRight((byte*)_start + _head, (byte*)_start + (( _head + (Capacity - _head)) * { sizeOf } ), (long)(expandCount * { sizeOf } ));{ incrementVersion }
613+ _stackMemoryC.ShiftRight((byte*)( _start + _head) , (byte*)( _start + (_head + (Capacity - _head))), (long)(expandCount * { sizeOf } ));{ incrementVersion }
589614 _head += expandCount;
590615 }}
591616 }}
0 commit comments