-
Notifications
You must be signed in to change notification settings - Fork 28
/
memory.jl
44 lines (35 loc) · 862 Bytes
/
memory.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Memory
# ======
"""
A contiguous memory.
This type works like a `Vector` method.
"""
struct Memory
ptr::Ptr{UInt8}
size::UInt
end
function Memory(data::ByteData)
return Memory(pointer(data), sizeof(data))
end
@inline function Base.length(mem::Memory)
return mem.size
end
@inline function Base.firstindex(mem::Memory)
return 1
end
@inline function Base.lastindex(mem::Memory)
return Int(mem.size)
end
@inline function Base.checkbounds(mem::Memory, i::Integer)
if !(1 ≤ i ≤ lastindex(mem))
throw(BoundsError(mem, i))
end
end
@inline function Base.getindex(mem::Memory, i::Integer)
@boundscheck checkbounds(mem, i)
return unsafe_load(mem.ptr, i)
end
@inline function Base.setindex!(mem::Memory, val::UInt8, i::Integer)
@boundscheck checkbounds(mem, i)
return unsafe_store!(mem.ptr, val, i)
end