Skip to content

Instantly share code, notes, and snippets.

@nkint
Created January 28, 2020 10:34
Show Gist options
  • Save nkint/47afb897d35a3a1917a8ca0cc8f5c9e3 to your computer and use it in GitHub Desktop.
Save nkint/47afb897d35a3a1917a8ca0cc8f5c9e3 to your computer and use it in GitHub Desktop.
umbrella malloc
import { Type, typedArray } from '@thi.ng/api'
import { MemPool } from '@thi.ng/malloc'
import { Vec } from '@thi.ng/vectors'
import { computeNewData } from './buffers'
const { numSliceRows, numSliceColumns, resolution } = globalState.deref()
const poolSize = numSliceRows * numSliceColumns * (resolution + 3)
const POOL = new MemPool({ size: poolSize })
console.log('poolSize of', { poolSize })
const DB = new Atom({ points: { addr: 0, size: 0 } }) // addr=0 here means unallocated
// allocate a chunk of memory large enough for 1000 floats
// return as typed array
const points = POOL.mallocAs(Type.F32, 10)
points.set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
// only store block info in atom
DB.resetIn('points', { addr: points.byteOffset, size: points.length, type: Type.F32 })
// then when you need to deal with the point array
// const { type, addr, size } = DB.deref().points
// const points2 = typedArray(type, POOL.buf, addr, size)
// // Float32Array [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
// // if you need to resize the array at some point...
// POOL.reallocArray(points2, newSize)
// // or...
// POOL.realloc(DB.deref().points.addr, newSize * 4) // 4 = sizeof(float)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment