Skip to content

Commit

Permalink
Merge pull request #12 from brenhinkeller/main
Browse files Browse the repository at this point in the history
Add new inner constructor for when you already have an `NTuple` of data
  • Loading branch information
chriselrod authored Feb 4, 2022
2 parents 71ef718 + cd5c22c commit 1bc34e3
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
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.6"
version = "0.1.7"

[compat]
julia = "1.5"
Expand Down
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,35 @@
[![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://JuliaSIMD.github.io/ManualMemory.jl/dev)
[![Build Status](https://github.com/JuliaSIMD/ManualMemory.jl/actions/workflows/CI.yml/badge.svg)](https://github.com/JuliaSIMD/ManualMemory.jl/actions/workflows/CI.yml)
[![Coverage](https://codecov.io/gh/JuliaSIMD/ManualMemory.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/JuliaSIMD/ManualMemory.jl)

Manually managed memory buffers backed by NTuples

### Examples

```julia
julia> using ManualMemory: MemoryBuffer, load, store!, LazyPreserve, preserve, PseudoPtr, Reference

julia> m = MemoryBuffer{4,Float64}(undef)
MemoryBuffer{4, Float64}((2.283825594e-314, 2.2157350003e-314, 2.216358792e-314, 2.08e-322))

julia> store!(pointer(m), 1.23)

julia> load(pointer(m))
1.23
```
Specifying an existing `NTuple` of data:
```julia
julia> s = (1,2,3,4,5);

julia> m = MemoryBuffer(s)
MemoryBuffer{5, Int64}((1, 2, 3, 4, 5))

julia> load(p)
1

julia> load(p+sizeof(Int64))
2

julia> load(p+sizeof(Int64)*2)
3
```
4 changes: 4 additions & 0 deletions src/ManualMemory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ mutable struct MemoryBuffer{N,T}
@assert Base.allocatedinline(T)
new{N,T}()
end
@inline function MemoryBuffer(data::NTuple{N,T}) where {N,T}
@assert Base.allocatedinline(T)
new{N,T}(data)
end
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))
Expand Down
5 changes: 4 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ using Test
store!(pointer(x), 1)
@test load(pointer(x)) === 1 === load(pointer(y))
end

# Test construction with existing data
s = (1,2,3,4,5)
@test MemoryBuffer(s).data === s
end

using ThreadingUtilities
include(joinpath(pkgdir(ThreadingUtilities), "test", "runtests.jl"))

2 comments on commit 1bc34e3

@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/53911

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.7 -m "<description of version>" 1bc34e3f3099659a55041a1a4940b4926aa2f0c8
git push origin v0.1.7

Please sign in to comment.