2023-09-18 07:21:50 +00:00
|
|
|
## Can be shared safely between threads
|
|
|
|
type SharedSeq*[T] = tuple[data: ptr UncheckedArray[T], len: int]
|
2023-07-31 07:52:04 +00:00
|
|
|
|
|
|
|
proc alloc*(str: cstring): cstring =
|
|
|
|
# Byte allocation from the given address.
|
|
|
|
# There should be the corresponding manual deallocation with deallocShared !
|
|
|
|
let ret = cast[cstring](allocShared(len(str) + 1))
|
|
|
|
copyMem(ret, str, len(str) + 1)
|
|
|
|
return ret
|
2023-09-18 07:21:50 +00:00
|
|
|
|
|
|
|
proc alloc*(str: string): cstring =
|
|
|
|
## Byte allocation from the given address.
|
|
|
|
## There should be the corresponding manual deallocation with deallocShared !
|
|
|
|
var ret = cast[cstring](allocShared(str.len + 1))
|
|
|
|
let s = cast[seq[char]](str)
|
2024-03-15 23:08:47 +00:00
|
|
|
for i in 0 ..< str.len:
|
2023-09-18 07:21:50 +00:00
|
|
|
ret[i] = s[i]
|
|
|
|
ret[str.len] = '\0'
|
|
|
|
return ret
|
|
|
|
|
|
|
|
proc allocSharedSeq*[T](s: seq[T]): SharedSeq[T] =
|
2024-03-01 00:58:35 +00:00
|
|
|
let data = allocShared(sizeof(T) * s.len)
|
|
|
|
if s.len != 0:
|
|
|
|
copyMem(data, unsafeAddr s[0], s.len)
|
2023-09-18 07:21:50 +00:00
|
|
|
return (cast[ptr UncheckedArray[T]](data), s.len)
|
|
|
|
|
|
|
|
proc deallocSharedSeq*[T](s: var SharedSeq[T]) =
|
|
|
|
deallocShared(s.data)
|
|
|
|
s.len = 0
|
|
|
|
|
|
|
|
proc toSeq*[T](s: SharedSeq[T]): seq[T] =
|
|
|
|
## Creates a seq[T] from a SharedSeq[T]. No explicit dealloc is required
|
|
|
|
## as req[T] is a GC managed type.
|
|
|
|
var ret = newSeq[T]()
|
2024-03-15 23:08:47 +00:00
|
|
|
for i in 0 ..< s.len:
|
2023-09-18 07:21:50 +00:00
|
|
|
ret.add(s.data[i])
|
|
|
|
return ret
|