Skip to content

Commit

Permalink
Preserve buffers from GC collection when doing unsafe things. (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
maleadt authored Sep 9, 2024
1 parent dfa7d0a commit 2e243bc
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/device.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ end
clGetDeviceInfo(d, prop, 0, C_NULL, sz)
chars = Vector{Cchar}(undef, sz[])
clGetDeviceInfo(d, prop, sz[], chars, C_NULL)
return unsafe_string(pointer(chars))
return GC.@preserve chars unsafe_string(pointer(chars))
end
if s === :profile
return get_string(CL_DEVICE_PROFILE)
Expand Down Expand Up @@ -123,7 +123,7 @@ end
clGetDeviceInfo(d, CL_DEVICE_EXTENSIONS, 0, C_NULL, size)
result = Vector{Cchar}(undef, size[])
clGetDeviceInfo(d, CL_DEVICE_EXTENSIONS, size[], result, C_NULL)
bs = unsafe_string(pointer(result))
bs = GC.@preserve result unsafe_string(pointer(result))
return String[string(s) for s in split(bs)]
end

Expand Down
4 changes: 2 additions & 2 deletions lib/kernel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ function Base.getproperty(k::Kernel, s::Symbol)
clGetKernelInfo(k, CL_KERNEL_FUNCTION_NAME, 0, C_NULL, size)
result = Vector{Cchar}(undef, size[])
clGetKernelInfo(k, CL_KERNEL_FUNCTION_NAME, size[], result, C_NULL)
return unsafe_string(pointer(result))
return GC.@preserve result unsafe_string(pointer(result))
elseif s == :num_args
result = Ref{Cuint}()
clGetKernelInfo(k, CL_KERNEL_NUM_ARGS, sizeof(Cuint), result, C_NULL)
Expand All @@ -360,7 +360,7 @@ function Base.getproperty(k::Kernel, s::Symbol)
if err == CL_SUCCESS && size[] > 1
result = Vector{Cchar}(undef, size[])
clGetKernelInfo(k, CL_KERNEL_ATTRIBUTES, size[], result, C_NULL)
return unsafe_string(pointer(result))
return GC.@preserve result unsafe_string(pointer(result))
else
return ""
end
Expand Down
4 changes: 2 additions & 2 deletions lib/platform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ function Base.getproperty(p::Platform, s::Symbol)
clGetPlatformInfo(p, string_properties[s], 0, C_NULL, size)
result = Vector{Cchar}(undef, size[])
clGetPlatformInfo(p, string_properties[s], size[], result, C_NULL)
return unsafe_string(pointer(result))
return GC.@preserve result unsafe_string(pointer(result))
end

if s == :extensions
size = Ref{Csize_t}()
clGetPlatformInfo(p, CL_PLATFORM_EXTENSIONS, 0, C_NULL, size)
result = Vector{Cchar}(undef, size[])
clGetPlatformInfo(p, CL_PLATFORM_EXTENSIONS, size[], result, C_NULL)
return split(unsafe_string(pointer(result)))
return GC.@preserve result split(unsafe_string(pointer(result)))
end

return getfield(p, s)
Expand Down
4 changes: 2 additions & 2 deletions lib/program.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function Base.getproperty(p::Program, s::Symbol)
src_len[] <= 1 && return nothing
src = Vector{Cchar}(undef, src_len[])
clGetProgramInfo(p, CL_PROGRAM_SOURCE, src_len[], src, C_NULL)
return unsafe_string(pointer(src))
return GC.@preserve src unsafe_string(pointer(src))
elseif s == :binary_sizes
sizes = Vector{Csize_t}(undef, p.num_devices)
clGetProgramInfo(p, CL_PROGRAM_BINARY_SIZES, sizeof(sizes), sizes, C_NULL)
Expand Down Expand Up @@ -180,7 +180,7 @@ function Base.getproperty(p::Program, s::Symbol)
clGetProgramBuildInfo(p, device, CL_PROGRAM_BUILD_LOG, 0, C_NULL, size)
log = Vector{Cchar}(undef, size[])
clGetProgramBuildInfo(p, device, CL_PROGRAM_BUILD_LOG, size[], log, C_NULL)
log_dict[device] = unsafe_string(pointer(log))
log_dict[device] = GC.@preserve log unsafe_string(pointer(log))
end
return log_dict
else
Expand Down
2 changes: 1 addition & 1 deletion test/behaviour.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
cl.launch(kern, str_len, nothing, out_buf)
h = cl.read(out_buf)

@test unsafe_string(pointer(h)) == hello_world_str
@test hello_world_str == GC.@preserve h unsafe_string(pointer(h))
end

@testset "Low Level API Test" begin
Expand Down

0 comments on commit 2e243bc

Please sign in to comment.