Skip to content

Commit 1bc34e3

Browse files
authored
Merge pull request #12 from brenhinkeller/main
Add new inner constructor for when you already have an `NTuple` of data
2 parents 71ef718 + cd5c22c commit 1bc34e3

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
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.6"
4+
version = "0.1.7"
55

66
[compat]
77
julia = "1.5"

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,35 @@
44
[![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://JuliaSIMD.github.io/ManualMemory.jl/dev)
55
[![Build Status](https://github.com/JuliaSIMD/ManualMemory.jl/actions/workflows/CI.yml/badge.svg)](https://github.com/JuliaSIMD/ManualMemory.jl/actions/workflows/CI.yml)
66
[![Coverage](https://codecov.io/gh/JuliaSIMD/ManualMemory.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/JuliaSIMD/ManualMemory.jl)
7+
8+
Manually managed memory buffers backed by NTuples
9+
10+
### Examples
11+
12+
```julia
13+
julia> using ManualMemory: MemoryBuffer, load, store!, LazyPreserve, preserve, PseudoPtr, Reference
14+
15+
julia> m = MemoryBuffer{4,Float64}(undef)
16+
MemoryBuffer{4, Float64}((2.283825594e-314, 2.2157350003e-314, 2.216358792e-314, 2.08e-322))
17+
18+
julia> store!(pointer(m), 1.23)
19+
20+
julia> load(pointer(m))
21+
1.23
22+
```
23+
Specifying an existing `NTuple` of data:
24+
```julia
25+
julia> s = (1,2,3,4,5);
26+
27+
julia> m = MemoryBuffer(s)
28+
MemoryBuffer{5, Int64}((1, 2, 3, 4, 5))
29+
30+
julia> load(p)
31+
1
32+
33+
julia> load(p+sizeof(Int64))
34+
2
35+
36+
julia> load(p+sizeof(Int64)*2)
37+
3
38+
```

src/ManualMemory.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ mutable struct MemoryBuffer{N,T}
66
@assert Base.allocatedinline(T)
77
new{N,T}()
88
end
9+
@inline function MemoryBuffer(data::NTuple{N,T}) where {N,T}
10+
@assert Base.allocatedinline(T)
11+
new{N,T}(data)
12+
end
913
end
1014
@inline Base.unsafe_convert(::Type{Ptr{T}}, m::MemoryBuffer) where {T} = Ptr{T}(pointer_from_objref(m))
1115
@inline Base.pointer(m::MemoryBuffer{N,T}) where {N,T} = Ptr{T}(pointer_from_objref(m))

test/runtests.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ using Test
3131
store!(pointer(x), 1)
3232
@test load(pointer(x)) === 1 === load(pointer(y))
3333
end
34+
35+
# Test construction with existing data
36+
s = (1,2,3,4,5)
37+
@test MemoryBuffer(s).data === s
3438
end
3539

3640
using ThreadingUtilities
3741
include(joinpath(pkgdir(ThreadingUtilities), "test", "runtests.jl"))
38-

0 commit comments

Comments
 (0)