-
Notifications
You must be signed in to change notification settings - Fork 844
Cleanup memory pool code add some tests #344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
78897fb to
c5a1edc
Compare
| // process with Debug.Assert | ||
| if (!_slab.IsActive) | ||
| { | ||
| MemoryPoolThrowHelper.ThrowInvalidOperationException_BlockReturnedToDisposedPool(); |
There was a problem hiding this comment.
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...
de3b063 to
693c900
Compare
| if (!Slab.IsActive) MemoryPoolThrowHelper.ThrowObjectDisposedException(MemoryPoolThrowHelper.ExceptionArgument.MemoryPoolBlock); | ||
| if (!_slab.IsActive) | ||
| { | ||
| MemoryPoolThrowHelper.ThrowObjectDisposedException(MemoryPoolThrowHelper.ExceptionArgument.MemoryPoolBlock); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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; } |
There was a problem hiding this comment.
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;
There was a problem hiding this comment.
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++; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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);
// ...2607233 to
479e510
Compare
479e510 to
0042084
Compare
|
🆙📅 |
654f7a9 to
d2393cc
Compare
d2393cc to
b42b573
Compare
|
🆙📅 |
This PR adds more descriptive error messages to existing cases and adds two more checks in debug mode:
It also adds one check in release mode:
And removes one check in release mode:
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.