initial definition of a MemRange type

This commit is contained in:
Zahary Karadjov 2018-02-16 16:38:25 +02:00
parent 27aa9d9bc2
commit 9815d9708f
5 changed files with 53 additions and 1 deletions

View File

@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Copyright 2018 Status Research & Development GmbH
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.

6
ranges.nim Normal file
View File

@ -0,0 +1,6 @@
import
ranges/memranges
export
memranges

21
ranges.nimble Normal file
View File

@ -0,0 +1,21 @@
mode = ScriptMode.Verbose
packageName = "ranges"
version = "0.0.1"
author = "Status Research & Development GmbH"
description = "Exploration of various implementations of memory range types"
license = "Apache2"
skipDirs = @["tests"]
requires "nim >= 0.17.0"
proc configForTests() =
--hints: off
--debuginfo
--path: "."
--run
task test, "run CPU tests":
configForTests()
setCommand "c", "tests/all.nim"

25
ranges/memranges.nim Normal file
View File

@ -0,0 +1,25 @@
import
ptr_arith
type
MemRange* = object
start: pointer
size: csize
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

0
tests/all.nim Normal file
View File