obtain mem ranges from strings and sequences

This commit is contained in:
Zahary Karadjov 2018-02-16 16:58:46 +02:00
parent ee803f088f
commit 9ad57df473
2 changed files with 14 additions and 9 deletions

6
.gitignore vendored
View File

@ -1 +1,7 @@
# ignore all executable files
*
!*.*
!*/
nimcache/

View File

@ -10,16 +10,15 @@ template len*(mr: MemRange): int = mr.size
template `[]`*(mr: MemRange, idx: int): byte = (cast[ptr byte](shift(mr.start, idx)))[]
proc baseAddr*(mr: MemRange): pointer = mr.start
when false:
# XXX: Alternative definition that crashes the compiler. Investigate
type
MemRange* = distinct (pointer, csize)
template len*(mr: MemRange): int = mr[1]
template `[]`*(mr: MemRange, idx: int): byte = (cast[ptr byte](shift(mr[0], idx)))[]
proc baseAddr*(mr: MemRange): pointer = mr[0]
proc makeMemRange*(start: pointer, size: csize): MemRange =
result.start = start
result.size = size
proc toMemRange*(x: string): MemRange =
result.start = x.cstring.pointer
result.size = x.len
proc toMemRange*[T](x: openarray[T]): MemRange =
result.start = cast[pointer](x)
result.size = x.len * T.sizeof