@@ -2,7 +2,7 @@ package uno.buffer
22
33
44import glm_.BYTES
5- import glm_.set
5+ import glm_.buffer.*
66import org.lwjgl.PointerBuffer
77import org.lwjgl.system.MemoryUtil
88import uno.kotlin.Quadruple
@@ -13,15 +13,6 @@ import java.nio.*
1313 * Created by elect on 05/03/17.
1414 */
1515
16- fun floatBufferBig (capacity : Int ): FloatBuffer = MemoryUtil .memCallocFloat(capacity)
17- fun doubleBufferBig (capacity : Int ): DoubleBuffer = MemoryUtil .memCallocDouble(capacity)
18-
19- fun bufferBig (capacity : Int ): ByteBuffer = MemoryUtil .memCalloc(capacity)
20- fun shortBufferBig (capacity : Int ): ShortBuffer = MemoryUtil .memCallocShort(capacity)
21- fun intBufferBig (capacity : Int ): IntBuffer = MemoryUtil .memCallocInt(capacity)
22- fun longBufferBig (capacity : Int ): LongBuffer = MemoryUtil .memCallocLong(capacity)
23-
24- fun charBufferBig (capacity : Int ): CharBuffer = TODO ()
2516
2617fun pointerBufferBig (capacity : Int ): PointerBuffer = MemoryUtil .memCallocPointer(capacity)
2718fun pointerBufferBig (capacity : IntBuffer ): PointerBuffer = MemoryUtil .memCallocPointer(capacity[0 ])
@@ -68,60 +59,51 @@ fun doubleBuffersBig(sizeA: Int, sizeB: Int, sizeC: Int) = Triple(doubleBufferBi
6859fun doubleBuffersBig (sizeA : Int , sizeB : Int , sizeC : Int , sizeD : Int ) = Quadruple (doubleBufferBig(sizeA), doubleBufferBig(sizeB), doubleBufferBig(sizeC), doubleBufferBig(sizeD))
6960fun doubleBuffersBig (sizeA : Int , sizeB : Int , sizeC : Int , sizeD : Int , sizeE : Int ) = Quintuple (doubleBufferBig(sizeA), doubleBufferBig(sizeB), doubleBufferBig(sizeC), doubleBufferBig(sizeD), doubleBufferBig(sizeE))
7061
71-
72- fun ByteBuffer.destroy () = MemoryUtil .memFree(this ) // TODO rename?
73- fun ShortBuffer.destroy () = MemoryUtil .memFree(this )
74- fun IntBuffer.destroy () = MemoryUtil .memFree(this )
75- fun LongBuffer.destroy () = MemoryUtil .memFree(this )
76- fun FloatBuffer.destroy () = MemoryUtil .memFree(this )
77- fun DoubleBuffer.destroy () = MemoryUtil .memFree(this )
78- fun CharBuffer.destroy () = MemoryUtil .memFree(this )
79- fun PointerBuffer.destroy () = MemoryUtil .memFree(this )
80-
81- fun destroyBuf (vararg buffers : Buffer ) {
62+ fun free (vararg buffers : Buffer ) {
8263 for (i in 0 until buffers.size)
8364 MemoryUtil .memFree(buffers[i])
8465}
8566
86- fun <R > ByteBuffer.use (block : (ByteBuffer ) -> R ) = block(this ).also { destroy() }
87- fun <R > ShortBuffer.use (block : (ShortBuffer ) -> R ) = block(this ).also { destroy() }
88- fun <R > IntBuffer.use (block : (IntBuffer ) -> R ) = block(this ).also { destroy() }
89- fun <R > LongBuffer.use (block : (LongBuffer ) -> R ) = block(this ).also { destroy() }
90- fun <R > FloatBuffer.use (block : (FloatBuffer ) -> R ) = block(this ).also { destroy() }
91- fun <R > DoubleBuffer.use (block : (DoubleBuffer ) -> R ) = block(this ).also { destroy() }
92- fun <R > CharBuffer.use (block : (CharBuffer ) -> R ) = block(this ).also { destroy() }
93- fun <R > PointerBuffer.use (block : (PointerBuffer ) -> R ) = block(this ).also { destroy() }
94-
95- @Suppress(" UNCHECKED_CAST" )
96- fun withBuffer (list : List <* >, block : ByteBuffer .() -> Unit ) {
97- when (list.elementAt(0 )) {
98- is Byte -> bufferBig(list.size).apply {
99- val l = list as List <Byte >
100- for (i in l.indices) set(i, l[i])
101- }
102- is Short -> bufferBig(list.size * Short .BYTES ).apply {
103- val l = list as List <Short >
104- for (i in l.indices) putShort(i * Short .BYTES , l[i])
105- }
106- is Int -> bufferBig(list.size * Int .BYTES ).apply {
107- val l = list as List <Int >
108- for (i in l.indices) putInt(i * Int .BYTES , l[i])
109- }
110- is Long -> bufferBig(list.size * Long .BYTES ).apply {
111- val l = list as List <Long >
112- for (i in l.indices) putLong(i * Long .BYTES , l[i])
113- }
114- is Float -> bufferBig(list.size * Float .BYTES ).apply {
115- val l = list as List <Float >
116- for (i in l.indices) putFloat(i * Float .BYTES , l[i])
117- }
118- is Double -> bufferBig(list.size * Double .BYTES ).apply {
119- val l = list as List <Double >
120- for (i in l.indices) putDouble(i * Double .BYTES , l[i])
121- }
122- else -> throw Error (" unsupported type" )
123- }.run {
124- block()
125- destroy()
126- }
127- }
67+ fun <R > ByteBuffer.use (block : (ByteBuffer ) -> R ) = block(this ).also { free() }
68+ fun <R > ShortBuffer.use (block : (ShortBuffer ) -> R ) = block(this ).also { free() }
69+ fun <R > IntBuffer.use (block : (IntBuffer ) -> R ) = block(this ).also { free() }
70+ fun <R > LongBuffer.use (block : (LongBuffer ) -> R ) = block(this ).also { free() }
71+ fun <R > FloatBuffer.use (block : (FloatBuffer ) -> R ) = block(this ).also { free() }
72+ fun <R > DoubleBuffer.use (block : (DoubleBuffer ) -> R ) = block(this ).also { free() }
73+ fun <R > CharBuffer.use (block : (CharBuffer ) -> R ) = block(this ).also { free() }
74+ fun <R > PointerBuffer.use (block : (PointerBuffer ) -> R ) = block(this ).also { free() }
75+ //
76+ // @Suppress("UNCHECKED_CAST")
77+ // fun withBuffer(list: List<*>, block: ByteBuffer.() -> Unit) {
78+ // when (list.elementAt(0)) {
79+ // is Byte -> bufferBig(list.size).apply {
80+ // val l = list as List<Byte>
81+ // for (i in l.indices)
82+ // put(i, l[i])
83+ // }
84+ // is Short -> bufferBig(list.size * Short.BYTES).apply {
85+ // val l = list as List<Short>
86+ // for (i in l.indices) putShort(i * Short.BYTES, l[i])
87+ // }
88+ // is Int -> bufferBig(list.size * Int.BYTES).apply {
89+ // val l = list as List<Int>
90+ // for (i in l.indices) putInt(i * Int.BYTES, l[i])
91+ // }
92+ // is Long -> bufferBig(list.size * Long.BYTES).apply {
93+ // val l = list as List<Long>
94+ // for (i in l.indices) putLong(i * Long.BYTES, l[i])
95+ // }
96+ // is Float -> bufferBig(list.size * Float.BYTES).apply {
97+ // val l = list as List<Float>
98+ // for (i in l.indices) putFloat(i * Float.BYTES, l[i])
99+ // }
100+ // is Double -> bufferBig(list.size * Double.BYTES).apply {
101+ // val l = list as List<Double>
102+ // for (i in l.indices) putDouble(i * Double.BYTES, l[i])
103+ // }
104+ // else -> throw Error("unsupported type")
105+ // }.run {
106+ // block()
107+ // free()
108+ // }
109+ // }
0 commit comments