brenhinkeller / StaticTools.jl

Enabling StaticCompiler.jl-based compilation of (some) Julia code to standalone native binaries by avoiding GC allocations and llvmcall-ing all the things!
MIT License
167 stars 12 forks source link

Implement Base.unsafe_wrap(MallocArray, ...) #19

Closed mkitti closed 2 years ago

mkitti commented 2 years ago

This pull request implements Base.unsafe_wrap for MallocArray. This provides a hook to make it easier to compose with other packages since Base.unsafe_wrap is a generic method to construct an AbstractArray from a pointer.

brenhinkeller commented 2 years ago

Looks like it compiles!

julia> function foo()
           ptr = Ptr{Int}(malloc(25*sizeof(Int)))
           a = Base.unsafe_wrap(MallocArray, ptr, (5,5))
           a .= 5
           printf(a)
       end
foo (generic function with 1 method)

julia> compile_executable(foo, (), "./")
ld: warning: object file (./foo.o) was built for newer OSX version (12.0) than being linked (10.13)
"/Users/cbkeller/foo"

shell> ./foo
5   5   5   5   5
5   5   5   5   5
5   5   5   5   5
5   5   5   5   5
5   5   5   5   5
brenhinkeller commented 2 years ago

This seems like a totally reasonable thing to add, thanks for the PR!