obtain mem ranges from strings and sequences
This commit is contained in:
parent
ee803f088f
commit
9ad57df473
|
@ -1 +1,7 @@
|
|||
# ignore all executable files
|
||||
*
|
||||
!*.*
|
||||
!*/
|
||||
|
||||
nimcache/
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue