Skip to content

Commit da7c0a1

Browse files
authored
Merge pull request #13 from brenhinkeller/main
Add equality comparisons on `MemoryBuffer`s
2 parents 1bc34e3 + 797378a commit da7c0a1

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ManualMemory"
22
uuid = "d125e4d3-2237-4719-b19c-fa641b8a4667"
33
authors = ["chriselrod <[email protected]> and contributors"]
4-
version = "0.1.7"
4+
version = "0.1.8"
55

66
[compat]
77
julia = "1.5"

src/ManualMemory.jl

+11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ mutable struct MemoryBuffer{N,T}
1313
end
1414
@inline Base.unsafe_convert(::Type{Ptr{T}}, m::MemoryBuffer) where {T} = Ptr{T}(pointer_from_objref(m))
1515
@inline Base.pointer(m::MemoryBuffer{N,T}) where {N,T} = Ptr{T}(pointer_from_objref(m))
16+
@inline Base.:(==)(::MemoryBuffer, ::MemoryBuffer) = false
17+
@inline function Base.:(==)(a::MemoryBuffer{N,A}, b::MemoryBuffer{N,B}) where {N,A,B}
18+
GC.@preserve a b begin
19+
pa = pointer(a)
20+
pb = pointer(b)
21+
for n in 0:N-1
22+
load(pa + n*offsetsize(A)) == load(pb + n*offsetsize(B)) || return false
23+
end
24+
return true
25+
end
26+
end
1627

1728
"""
1829
PseudoPtr(data, position=firstindex(data))

test/runtests.jl

+7
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ using Test
3535
# Test construction with existing data
3636
s = (1,2,3,4,5)
3737
@test MemoryBuffer(s).data === s
38+
39+
# Test equality and inequality
40+
@test MemoryBuffer((1,2,3,4,5)) == MemoryBuffer((1,2,3,4,5))
41+
@test MemoryBuffer((1,2,3,4,6)) != MemoryBuffer((1,2,3,4,5))
42+
@test MemoryBuffer((0x01,0x02)) == MemoryBuffer((1,2))
43+
@test MemoryBuffer((0x01,0x02)) != MemoryBuffer((0x01,0x02,0x03))
44+
3845
end
3946

4047
using ThreadingUtilities

0 commit comments

Comments
 (0)