Skip to content

Instantly share code, notes, and snippets.

@Deco
Last active August 29, 2015 14:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Deco/208ac923ecc66f42caf2 to your computer and use it in GitHub Desktop.
Save Deco/208ac923ecc66f42caf2 to your computer and use it in GitHub Desktop.
import unittest
type memory* [T=byte] = distinct ptr T
proc `+`*[T](mem: memory[T]; offset: int): memory[T] =
cast[memory[T]](cast[int](mem) + offset*sizeof(T))
proc `-`*[T](mem: memory[T]; offset: int): memory[T] =
cast[memory[T]](cast[int](mem) - offset*sizeof(T))
proc `[]`*[T](mem: memory[T]; offset: int): T =
(cast[ptr T](mem+offset))[]
proc `[]=`*[T](mem: memory[T]; offset: int; value: T) =
(cast[ptr T](mem+offset))[] = value
template memalloc*(t: typedesc; count: int): auto =
cast[memory[t]](alloc(sizeof(t)*count))
template memalloc0*(t: typedesc; count: int): auto =
cast[memory[t]](alloc0(sizeof(t)*count))
proc memeq*[T](a, b: memory[T]; count: int): bool =
equalMem(cast[pointer](a), cast[pointer](b), count*sizeof(T))
proc memcopy*[T](a, b: memory[T]; count: int) =
copyMem(cast[pointer](a), cast[pointer](b), count*sizeof(T))
proc memmove*[T](a, b: memory[T]; count: int) =
moveMem(cast[pointer](a), cast[pointer](b), count*sizeof(T))
proc memzero*[T](a: memory[T]; count: int) =
zeroMem(cast[pointer](a), count*sizeof(T))
suite "memory":
const arbitraryLength = 22
test "memory - memalloc int":
discard memalloc(int, arbitraryLength)
discard memalloc0(int, arbitraryLength)
test "memory - memalloc object":
type SomeObject = object
a, b, c: int
d: array[10, char]
discard memalloc(SomeObject, arbitraryLength)
discard memalloc0(SomeObject, arbitraryLength)
test "memory - `[]`":
var a = memalloc0(int, arbitraryLength)
for i in 0.. <arbitraryLength:
require(a[0] == 0)
test "memory - `[]=`":
var a = memalloc(int, arbitraryLength)
for i in 0.. <arbitraryLength:
a[i] = i*i
for i in 0.. <arbitraryLength:
require(a[i] == i*i)
test "memory - `+`":
var a = memalloc(int, arbitraryLength)
a[11] = 99
var b = a+9
require(b[2] == 99)
test "memory - `-`":
var a = memalloc(int, arbitraryLength)
a[11] = 99
var b = a+9
var c = b-3
require(c[5] == 99)
test "memory - memeq":
var a = memalloc(int, arbitraryLength*2)
var b = memalloc(int, arbitraryLength*2)
for i in 0.. <arbitraryLength:
a[i] = i*i
b[i] = i*i
for i in arbitraryLength.. <arbitraryLength*2:
a[i] = i*i
b[i] = i+999
require(memeq(a, b, arbitraryLength))
require(not memeq(a, b, arbitraryLength+1))
test "memory - memcopy":
var a = memalloc(int, arbitraryLength*2)
var b = memalloc(int, arbitraryLength*2)
for i in 0.. <arbitraryLength*2:
a[i] = i*i
b[i] = 0
memcopy(a, b, arbitraryLength)
require(memeq(a, b, arbitraryLength))
require(not memeq(a, b, arbitraryLength*2))
test "memory - memmove":
var a = memalloc(int, arbitraryLength*3)
for i in 0.. <arbitraryLength*3:
a[i] = i*i
# 123 -> ?13
memmove(a, a+arbitraryLength, arbitraryLength)
for i in arbitraryLength.. <arbitraryLength*3:
a[i] = i*i
test "memory - memzero":
var a = memalloc(int, arbitraryLength)
for i in 0.. <arbitraryLength:
a[i] = i*i
memzero(a, arbitraryLength)
for i in 0.. <arbitraryLength:
require(a[i] == 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment