Skip to content

Commit

Permalink
Fix empty chunk view creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Mashkov committed Dec 4, 2018
1 parent fc783a4 commit de94a64
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
- Added a cpointer constructor to native IoBuffer so that IoBuffer can be used to read AND write on a memory chunk
- Made ByteChannel pass original cause from the owner job
- Fixed reading UTF-8 lines
- Fixed empty chunk view creation
- Utility functions takeWhile* improvements

# 0.1.0
> Published 15 Nov 2018
Expand Down
2 changes: 2 additions & 0 deletions kotlinx-io-js/src/main/kotlin/kotlinx/io/core/IoBufferJS.kt
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,8 @@ actual class IoBuffer internal constructor(
actual fun isExclusivelyOwned(): Boolean = refCount == 1

actual fun makeView(): IoBuffer {
if (this === Empty) return this

val o = origin ?: this
o.acquire()

Expand Down
10 changes: 8 additions & 2 deletions kotlinx-io-jvm/src/main/kotlin/kotlinx/io/core/IoBufferJVM.kt
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,8 @@ actual class IoBuffer private constructor(
* Creates a new view to the same actual buffer with independant read and write positions and gaps
*/
actual fun makeView(): IoBuffer {
if (this === Empty) return this

val newOrigin = origin ?: this
newOrigin.acquire()

Expand Down Expand Up @@ -863,12 +865,16 @@ actual class IoBuffer private constructor(
}

private fun releaseRefCount(): Boolean {
if (this === Empty) throw IllegalArgumentException("Attempted to release empty")
if (this === Empty) {
throw IllegalArgumentException("Attempted to release empty")
}
while (true) {
val value = refCount
val newValue = value - 1

if (value == 0L) throw IllegalStateException("Unable to release: already released")
if (value == 0L) {
throw IllegalStateException("Unable to release: already released")
}
if (RefCount.compareAndSet(this, value, newValue)) {
return newValue == 0L
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,8 @@ actual class IoBuffer internal constructor(
actual fun isExclusivelyOwned(): Boolean = refCount == 1

actual fun makeView(): IoBuffer {
if (this === Empty) return this

val o = origin ?: this
o.acquire()

Expand Down

0 comments on commit de94a64

Please sign in to comment.