/** * Method called by {@link MemBuffer} instances when they have consumed * contents of a segment and do not want to hold on to it for local * reuse. */ @Override public synchronized void releaseSegment(S segToRelease) { if (--_bufferOwnedSegmentCount < 0) { // sanity check; not needed in perfect world int count = _bufferOwnedSegmentCount; _bufferOwnedSegmentCount = 0; // "fix" throw new IllegalStateException("Bugger! Corruption Maximus: _bufferOwnedSegmentCount went below 0 (" +count+")"); } // Can we reuse it? if (_reusableSegmentCount < _maxReusableSegments) { _firstReusableSegment = segToRelease.relink(_firstReusableSegment); ++_reusableSegmentCount; } }
/** * Method called by {@link MemBuffer} instances when they have consumed * contents of a segment and do not want to hold on to it for local * reuse. */ @Override public synchronized void releaseSegment(S segToRelease) { if (--_bufferOwnedSegmentCount < 0) { // sanity check; not needed in perfect world int count = _bufferOwnedSegmentCount; _bufferOwnedSegmentCount = 0; // "fix" throw new IllegalStateException("Bugger! Corruption Maximus: _bufferOwnedSegmentCount went below 0 (" +count+")"); } // Can we reuse it? if (_reusableSegmentCount < _maxReusableSegments) { _firstReusableSegment = segToRelease.relink(_firstReusableSegment); ++_reusableSegmentCount; } }
/** * Method that will try to allocate specified number of segments * (and exactly that number; no less). * If this can be done, segments are allocated and prepended into * given segment list; otherwise nothing is allocated and * null is returned. * * @param count Number of segments to allocate * * @return Head of segment list (with newly allocated entries as first * entries) if allocation succeeded; null if not */ @Override public synchronized S allocateSegments(int count, S segmentList) { if (count < 1) { throw new IllegalArgumentException("Must allocate at least one segment (count = "+count+")"); } if (!_canAllocate(count)) { return null; } for (int i = 0; i < count; ++i) { segmentList = _allocateSegment().relink(segmentList); } return segmentList; }
/** * Method that will try to allocate specified number of segments * (and exactly that number; no less). * If this can be done, segments are allocated and prepended into * given segment list; otherwise nothing is allocated and * null is returned. * * @param count Number of segments to allocate * * @return Head of segment list (with newly allocated entries as first * entries) if allocation succeeded; null if not */ @Override public synchronized S allocateSegments(int count, S segmentList) { if (count < 1) { throw new IllegalArgumentException("Must allocate at least one segment (count = "+count+")"); } if (!_canAllocate(count)) { return null; } for (int i = 0; i < count; ++i) { segmentList = _allocateSegment().relink(segmentList); } return segmentList; }
protected MemBufferBase(SegmentAllocator<S> allocator, int minSegmentsToAllocate, int maxSegmentsToAllocate, S initialSegments) { _segmentAllocator = allocator; _segmentSize = allocator.getSegmentSize(); _maxSegmentsForReuse = minSegmentsToAllocate; _maxSegmentsToAllocate = maxSegmentsToAllocate; // all but one of segments goes to the free list _firstFreeSegment = initialSegments.getNext(); // and first one is used as both head and tail initialSegments.relink(null); _head = _tail = initialSegments; // also, better initialize initial segment for writing and reading _head.initForWriting(); _head.initForReading(); _usedSegmentsCount = 1; // Sanity checks? if yes, uncomment this... /* int count = count(_firstFreeSegment); if (count != _freeSegmentCount) { throw new IllegalStateException("Bad initial _freeSegmentCount ("+_freeSegmentCount+"): but only got "+count+" linked"); } */ _freeSegmentCount = minSegmentsToAllocate-1; }
protected MemBufferBase(SegmentAllocator<S> allocator, int minSegmentsToAllocate, int maxSegmentsToAllocate, S initialSegments) { _segmentAllocator = allocator; _segmentSize = allocator.getSegmentSize(); _maxSegmentsForReuse = minSegmentsToAllocate; _maxSegmentsToAllocate = maxSegmentsToAllocate; // all but one of segments goes to the free list _firstFreeSegment = initialSegments.getNext(); // and first one is used as both head and tail initialSegments.relink(null); _head = _tail = initialSegments; // also, better initialize initial segment for writing and reading _head.initForWriting(); _head.initForReading(); _usedSegmentsCount = 1; // Sanity checks? if yes, uncomment this... /* int count = count(_firstFreeSegment); if (count != _freeSegmentCount) { throw new IllegalStateException("Bad initial _freeSegmentCount ("+_freeSegmentCount+"): but only got "+count+" linked"); } */ _freeSegmentCount = minSegmentsToAllocate-1; }
_freeSegmentCount = 1; } else { _firstFreeSegment = old.relink(_firstFreeSegment); ++_freeSegmentCount;
_freeSegmentCount = 1; } else { _firstFreeSegment = old.relink(_firstFreeSegment); ++_freeSegmentCount;