Skip to content

Commit 1f7601d

Browse files
authored
Merge pull request #175 from maleadt/tb/dispose_messages
Properly dispose of messages to avoid memory leaks.
2 parents 671ba03 + 3070046 commit 1f7601d

File tree

10 files changed

+26
-20
lines changed

10 files changed

+26
-20
lines changed

src/LLVM.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ end
8585

8686
## source code includes
8787

88-
include("util/types.jl")
88+
include("util.jl")
8989

9090
include("base.jl")
9191

src/core/context.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ end
5050
reftype(::Type{DiagnosticInfo}) = API.LLVMDiagnosticInfoRef
5151

5252
severity(di::DiagnosticInfo) = API.LLVMGetDiagInfoSeverity(ref(di))
53-
message(di::DiagnosticInfo) = unsafe_string(API.LLVMGetDiagInfoDescription(ref(di)))
53+
message(di::DiagnosticInfo) = unsafe_message(API.LLVMGetDiagInfoDescription(ref(di)))
5454

5555

5656
## handlers

src/core/module.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function Module(f::Core.Function, args...)
3131
end
3232

3333
function Base.show(io::IO, mod::Module)
34-
output = unsafe_string(API.LLVMPrintModuleToString(ref(mod)))
34+
output = unsafe_message(API.LLVMPrintModuleToString(ref(mod)))
3535
print(io, output)
3636
end
3737

src/core/type.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ issized(typ::LLVMType) =
2929
context(typ::LLVMType) = Context(API.LLVMGetTypeContext(ref(typ)))
3030

3131
function Base.show(io::IO, typ::LLVMType)
32-
output = unsafe_string(API.LLVMPrintTypeToString(ref(typ)))
32+
output = unsafe_message(API.LLVMPrintTypeToString(ref(typ)))
3333
print(io, output)
3434
end
3535

src/core/value.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ name(val::Value) = unsafe_string(API.LLVMGetValueName(ref(val)))
3939
name!(val::Value, name::String) = API.LLVMSetValueName(ref(val), name)
4040

4141
function Base.show(io::IO, val::Value)
42-
output = unsafe_string(API.LLVMPrintValueToString(ref(val)))
42+
output = unsafe_message(API.LLVMPrintValueToString(ref(val)))
4343
print(io, output)
4444
end
4545

src/datalayout.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ end
2525
dispose(data::DataLayout) = API.LLVMDisposeTargetData(ref(data))
2626

2727
Base.convert(::Type{String}, data::DataLayout) =
28-
unsafe_string(API.LLVMCopyStringRepOfTargetData(ref(data)))
28+
unsafe_message(API.LLVMCopyStringRepOfTargetData(ref(data)))
2929

3030
function Base.show(io::IO, data::DataLayout)
3131
@printf(io, "DataLayout(%s)", convert(String, data))

src/target.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ function Target(triple::String)
1515
status = convert(Core.Bool, API.LLVMGetTargetFromTriple(triple, out_ref, out_error))
1616

1717
if status
18-
error = unsafe_string(out_error[])
19-
API.LLVMDisposeMessage(out_error[])
18+
error = unsafe_message(out_error[])
2019
throw(LLVMException(error))
2120
end
2221

src/targetmachine.jl

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,23 @@ function TargetMachine(f::Core.Function, args...)
2828
end
2929

3030
target(tm::TargetMachine) = Target(API.LLVMGetTargetMachineTarget(ref(tm)))
31-
triple(tm::TargetMachine) = unsafe_string(API.LLVMGetTargetMachineTriple(ref(tm)))
32-
triple() = unsafe_string(API.LLVMGetDefaultTargetTriple())
33-
cpu(tm::TargetMachine) = unsafe_string(API.LLVMGetTargetMachineCPU(ref(tm)))
34-
features(tm::TargetMachine) = unsafe_string(API.LLVMGetTargetMachineFeatureString(ref(tm)))
31+
triple(tm::TargetMachine) = unsafe_message(API.LLVMGetTargetMachineTriple(ref(tm)))
32+
triple() = unsafe_message(API.LLVMGetDefaultTargetTriple())
33+
cpu(tm::TargetMachine) = unsafe_message(API.LLVMGetTargetMachineCPU(ref(tm)))
34+
features(tm::TargetMachine) = unsafe_message(API.LLVMGetTargetMachineFeatureString(ref(tm)))
3535

3636
asm_verbosity!(tm::TargetMachine, verbose::Core.Bool) =
3737
API.LLVMSetTargetMachineAsmVerbosity(ref(tm), convert(Bool, verbose))
3838

3939
function emit(tm::TargetMachine, mod::Module, filetype::API.LLVMCodeGenFileType)
4040
out_error = Ref{Cstring}()
4141
out_membuf = Ref{API.LLVMMemoryBufferRef}()
42-
status = convert(Core.Bool,
42+
status = convert(Core.Bool,
4343
API.LLVMTargetMachineEmitToMemoryBuffer(ref(tm), ref(mod), filetype,
4444
out_error, out_membuf))
4545

4646
if status
47-
error = unsafe_string(out_error[])
48-
API.LLVMDisposeMessage(out_error[])
47+
error = unsafe_message(out_error[])
4948
throw(LLVMException(error))
5049
end
5150

@@ -54,12 +53,11 @@ end
5453

5554
function emit(tm::TargetMachine, mod::Module, filetype::API.LLVMCodeGenFileType, path::String)
5655
out_error = Ref{Cstring}()
57-
status = convert(Core.Bool,
56+
status = convert(Core.Bool,
5857
API.LLVMTargetMachineEmitToFile(ref(tm), ref(mod), path, filetype, out_error))
5958

6059
if status
61-
error = unsafe_string(out_error[])
62-
API.LLVMDisposeMessage(out_error[])
60+
error = unsafe_message(out_error[])
6361
throw(LLVMException(error))
6462
end
6563

src/util/types.jl renamed to src/util.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
# Defining types in the LLVM type hierarchy
1+
# utilities
2+
3+
function unsafe_message(ptr)
4+
str = unsafe_string(ptr)
5+
API.LLVMDisposeMessage(ptr)
6+
str
7+
end
8+
9+
10+
## defining types in the LLVM type hierarchy
211

312
# llvm.org/docs/doxygen/html/group__LLVMCSupportTypes.html
413

test/support.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ close(out.in)
1919

2020
end
2121

22-
end
22+
end

0 commit comments

Comments
 (0)