Skip to content

Conversation

@pakrym
Copy link

@pakrym pakrym commented Apr 24, 2018

This PR adds more descriptive error messages to existing cases and adds two more checks in debug mode:

  1. Double dispose of pool
  2. Disposing pool with active blocks.

It also adds one check in release mode:

  1. Renting from a disposed pool. Current behaviour there is highly unpredictable and hard to correctly handle.

And removes one check in release mode:

  1. Disposing (returning) block to a disposed pool.

This PR doesn't address the unpinning while in use issue raised by @benaadams but I don't want to mix up cleanup/diagnostics and functional fixes.

@pakrym pakrym requested review from davidfowl and halter73 April 24, 2018 23:39
@pakrym pakrym force-pushed the pakrym/pool-cleanup branch from 78897fb to c5a1edc Compare April 24, 2018 23:41
// process with Debug.Assert
if (!_slab.IsActive)
{
MemoryPoolThrowHelper.ThrowInvalidOperationException_BlockReturnedToDisposedPool();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kill this exception in Release mode? Its still there...

@pakrym pakrym force-pushed the pakrym/pool-cleanup branch from de3b063 to 693c900 Compare April 25, 2018 15:26
if (!Slab.IsActive) MemoryPoolThrowHelper.ThrowObjectDisposedException(MemoryPoolThrowHelper.ExceptionArgument.MemoryPoolBlock);
if (!_slab.IsActive)
{
MemoryPoolThrowHelper.ThrowObjectDisposedException(MemoryPoolThrowHelper.ExceptionArgument.MemoryPoolBlock);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to have an _isDisposed field in MemoryPoolBlock itself that was set in Dispose and unset in Lease? Then there'd be a different exception for using the block after it's returned to the pool and using the block before it's returned to the pool but after the pool is disposed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought about it too, will do.

public bool IsActive => _isActive;

public IntPtr NativePointer => _nativePointer;
public bool IsActive { get; private set; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this ever different than public bool IsActive => !_isDisposed;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope.

block.IsLeased = true;
#endif
Return(block);
blocksAllocated++;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to suggest how to calculate the blocksAllocated by a single call to AllocateSlab in a single expression, but I realized the for loop condition is messed up.

I'm pretty sure offset + _blockSize < blockAllocationLength should be either offset + _blockSize <= _slabLength or offset - firstOffset + _blockSize <= blockAllocationLength instead.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We create additional segment after the loop. It takes the last _blockSize of memory.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhhh. I think it's technically right then but still a little hard to follow. I think it would be a lot easier to grok (if only because less bitwise operations) if written as follows:

// ...

var maxPenultimateOffset = _slabLength - (_blockSize * 2);
var offset = firstOffset;
for (;
    offset <= maxPenultimateOffset;
    offset += _blockSize)
{
    var block = new MemoryPoolBlock(
        this,
        slab,
        offset,
        _blockSize);
    
    // ...

@pakrym pakrym force-pushed the pakrym/pool-cleanup branch from 2607233 to 479e510 Compare April 25, 2018 20:44
@pakrym pakrym force-pushed the pakrym/pool-cleanup branch from 479e510 to 0042084 Compare April 25, 2018 20:51
@pakrym
Copy link
Author

pakrym commented Apr 25, 2018

🆙📅

@pakrym pakrym force-pushed the pakrym/pool-cleanup branch from 654f7a9 to d2393cc Compare April 27, 2018 18:37
@pakrym pakrym force-pushed the pakrym/pool-cleanup branch from d2393cc to b42b573 Compare April 27, 2018 18:42
@pakrym
Copy link
Author

pakrym commented Apr 27, 2018

🆙📅

@pakrym pakrym merged commit 1dfc638 into dev Apr 30, 2018
@natemcmaster natemcmaster deleted the pakrym/pool-cleanup branch October 25, 2018 00:25
@ghost ghost locked as resolved and limited conversation to collaborators May 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants