From 6ebd396ff0c39b09cb827bb3c70ee63e9903ea7d Mon Sep 17 00:00:00 2001 From: "C.Brenhin Keller" Date: Sat, 5 Feb 2022 21:55:24 -0500 Subject: [PATCH 1/2] Add `Base.:(==)` implementation from Chris Elrod via Zulip --- src/ManualMemory.jl | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/ManualMemory.jl b/src/ManualMemory.jl index 5ecc335..f774835 100644 --- a/src/ManualMemory.jl +++ b/src/ManualMemory.jl @@ -13,6 +13,17 @@ mutable struct MemoryBuffer{N,T} end @inline Base.unsafe_convert(::Type{Ptr{T}}, m::MemoryBuffer) where {T} = Ptr{T}(pointer_from_objref(m)) @inline Base.pointer(m::MemoryBuffer{N,T}) where {N,T} = Ptr{T}(pointer_from_objref(m)) +@inline Base.:(==)(::MemoryBuffer, ::MemoryBuffer) = false +@inline function Base.:(==)(a::MemoryBuffer{N,A}, b::MemoryBuffer{N,B}) where {N,A,B} + GC.@preserve a b begin + pa = pointer(a) + pb = pointer(b) + for n in 0:N-1 + load(pa + n*offsetsize(A)) == load(pb + n*offsetsize(B)) || return false + end + return true + end +end """ PseudoPtr(data, position=firstindex(data)) From 797378af7c336f1eac05981e9c3864ca66d0ec78 Mon Sep 17 00:00:00 2001 From: "C.Brenhin Keller" Date: Sat, 5 Feb 2022 21:55:52 -0500 Subject: [PATCH 2/2] Add tests for equality and inequality, bump version --- Project.toml | 2 +- test/runtests.jl | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index f194eeb..d2c00ea 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ManualMemory" uuid = "d125e4d3-2237-4719-b19c-fa641b8a4667" authors = ["chriselrod and contributors"] -version = "0.1.7" +version = "0.1.8" [compat] julia = "1.5" diff --git a/test/runtests.jl b/test/runtests.jl index 6854a6c..75ec22e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -35,6 +35,13 @@ using Test # Test construction with existing data s = (1,2,3,4,5) @test MemoryBuffer(s).data === s + + # Test equality and inequality + @test MemoryBuffer((1,2,3,4,5)) == MemoryBuffer((1,2,3,4,5)) + @test MemoryBuffer((1,2,3,4,6)) != MemoryBuffer((1,2,3,4,5)) + @test MemoryBuffer((0x01,0x02)) == MemoryBuffer((1,2)) + @test MemoryBuffer((0x01,0x02)) != MemoryBuffer((0x01,0x02,0x03)) + end using ThreadingUtilities