@@ -1346,30 +1346,23 @@ private int nextAvailableSegmentOffset() { |
1346 | 1346 | return startIndex; |
1347 | 1347 | } |
1348 | 1348 |
|
1349 | | - private int remainingCapacityOnFreeList() { |
1350 | | - final int segmentSize = this.segmentSize; |
1351 | | - int remainingCapacity = externalFreeList.size() * segmentSize; |
1352 | | - IntStack localFreeList = this.localFreeList; |
1353 | | - if (localFreeList != null) { |
1354 | | - assert Thread.currentThread() == ownerThread; |
1355 | | - remainingCapacity += localFreeList.size() * segmentSize; |
1356 | | - } |
1357 | | - return remainingCapacity; |
1358 | | - } |
1359 | | - |
1360 | 1349 | @Override |
1361 | 1350 | public int remainingCapacity() { |
1362 | | - int remainingCapacity = super.remainingCapacity(); |
1363 | | - if (remainingCapacity > segmentSize) { |
1364 | | - return remainingCapacity; |
| 1351 | + int remaining = super.remainingCapacity(); |
| 1352 | + return remaining > segmentSize ? remaining : updateRemainingCapacity(remaining); |
| 1353 | + } |
| 1354 | + |
| 1355 | + private int updateRemainingCapacity(int snapshotted) { |
| 1356 | + int freeSegments = externalFreeList.size(); |
| 1357 | + IntStack localFreeList = this.localFreeList; |
| 1358 | + if (localFreeList != null) { |
| 1359 | + freeSegments += localFreeList.size(); |
1365 | 1360 | } |
1366 | | - int updatedRemainingCapacity = remainingCapacityOnFreeList(); |
1367 | | - if (updatedRemainingCapacity == remainingCapacity) { |
1368 | | - return remainingCapacity; |
| 1361 | + int updated = freeSegments * segmentSize; |
| 1362 | + if (updated != snapshotted) { |
| 1363 | + allocatedBytes = capacity() - updated; |
1369 | 1364 | } |
1370 | | - // update allocatedBytes based on what's available in the free list |
1371 | | - allocatedBytes = capacity() - updatedRemainingCapacity; |
1372 | | - return updatedRemainingCapacity; |
| 1365 | + return updated; |
1373 | 1366 | } |
1374 | 1367 |
|
1375 | 1368 | private void releaseSegmentOffsetIntoFreeList(int startIndex) { |
@@ -1405,12 +1398,7 @@ void releaseSegment(int startIndex, int size) { |
1405 | 1398 | private void handleStateOnExternalReleaseSegment(int localFreeListSize) { |
1406 | 1399 | // localFreeListSize is the number of segments that were in the local free list when |
1407 | 1400 | // markToDeallocate() was called. Check if all segments have been returned. |
1408 | | - int totalFreeSegments = localFreeListSize + externalFreeList.size(); |
1409 | | - if (totalFreeSegments == segments) { |
1410 | | - if (STATE.compareAndSet(this, localFreeListSize, DEALLOCATED)) { |
1411 | | - deallocate(); |
1412 | | - } |
1413 | | - } |
| 1401 | + deallocateIfNeeded(localFreeListSize); |
1414 | 1402 | } |
1415 | 1403 |
|
1416 | 1404 | private void updateStateOnLocalReleaseSegment(int previousLocalSize, IntStack localFreeList) { |
@@ -1420,10 +1408,14 @@ private void updateStateOnLocalReleaseSegment(int previousLocalSize, IntStack lo |
1420 | 1408 | // State was concurrently changed by another external releaseSegment that deallocated. |
1421 | 1409 | return; |
1422 | 1410 | } |
| 1411 | + deallocateIfNeeded(newLocalSize); |
| 1412 | + } |
| 1413 | + |
| 1414 | + private void deallocateIfNeeded(int localSize) { |
1423 | 1415 | // Check if all segments have been returned. |
1424 | | - int totalFreeSegments = newLocalSize + externalFreeList.size(); |
| 1416 | + int totalFreeSegments = localSize + externalFreeList.size(); |
1425 | 1417 | if (totalFreeSegments == segments) { |
1426 | | - if (STATE.compareAndSet(this, newLocalSize, DEALLOCATED)) { |
| 1418 | + if (STATE.compareAndSet(this, localSize, DEALLOCATED)) { |
1427 | 1419 | deallocate(); |
1428 | 1420 | } |
1429 | 1421 | } |
@@ -1434,13 +1426,7 @@ void markToDeallocate() { |
1434 | 1426 | IntStack localFreeList = this.localFreeList; |
1435 | 1427 | int localSize = localFreeList != null ? localFreeList.size() : 0; |
1436 | 1428 | STATE.set(this, localSize); |
1437 | | - // Check if all segments have already been returned. |
1438 | | - int totalFreeSegments = localSize + externalFreeList.size(); |
1439 | | - if (totalFreeSegments == segments) { |
1440 | | - if (STATE.compareAndSet(this, localSize, DEALLOCATED)) { |
1441 | | - deallocate(); |
1442 | | - } |
1443 | | - } |
| 1429 | + deallocateIfNeeded(localSize); |
1444 | 1430 | } |
1445 | 1431 | } |
1446 | 1432 |
|
|