Skip to content

Commit

Permalink
Merge pull request #13 from brenhinkeller/main
Browse files Browse the repository at this point in the history
Add equality comparisons on `MemoryBuffer`s
  • Loading branch information
chriselrod authored Feb 6, 2022
2 parents 1bc34e3 + 797378a commit da7c0a1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ManualMemory"
uuid = "d125e4d3-2237-4719-b19c-fa641b8a4667"
authors = ["chriselrod <[email protected]> and contributors"]
version = "0.1.7"
version = "0.1.8"

[compat]
julia = "1.5"
Expand Down
11 changes: 11 additions & 0 deletions src/ManualMemory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

2 comments on commit da7c0a1

@chriselrod
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/53996

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.8 -m "<description of version>" da7c0a122fbc55a45338a354d4b0c69facd3e670
git push origin v0.1.8

Please sign in to comment.