Skip to content

Commit 58f3b1c

Browse files
authored
Add Julia allocator (#30)
* Add Julia allocator * fix for 1.9 * remove bad ref
1 parent 6346f1a commit 58f3b1c

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "AWSCRT"
22
uuid = "df31ea59-17a4-4ebd-9d69-4f45266dc2c7"
3-
version = "0.5.0"
3+
version = "0.5.1"
44

55
[deps]
66
CountDownLatches = "621fb831-fdad-4fff-93ac-1af7b7ed19e3"

src/AWSCRT.jl

+12
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ const _C_ON_SUBSCRIBE_COMPLETE = Ref{Ptr{Cvoid}}(C_NULL)
3535
const _C_ON_UNSUBSCRIBE_COMPLETE = Ref{Ptr{Cvoid}}(C_NULL)
3636
const _C_ON_RESUBSCRIBE_COMPLETE = Ref{Ptr{Cvoid}}(C_NULL)
3737
const _C_ON_PUBLISH_COMPLETE = Ref{Ptr{Cvoid}}(C_NULL)
38+
const _C_AWS_MALLOC = Ref{Ptr{Cvoid}}(C_NULL)
39+
const _C_AWS_FREE = Ref{Ptr{Cvoid}}(C_NULL)
40+
const _C_AWS_REALLOC = Ref{Ptr{Cvoid}}(C_NULL)
41+
const _C_AWS_CALLOC = Ref{Ptr{Cvoid}}(C_NULL)
3842

3943
function _release(; include_mem_tracer = isempty(get(ENV, "AWS_CRT_MEMORY_TRACING", "")))
4044
aws_thread_set_managed_join_timeout_ns(5e8) # 0.5 seconds
@@ -61,6 +65,8 @@ const advanced_use_note =
6165
"default so that you can bring your own native data if you need to. However, you are then responsible for the " *
6266
"memory management of that data."
6367

68+
include("allocator.jl")
69+
6470
include("AWSIO.jl")
6571
export EventLoopGroup
6672
export get_or_create_default_event_loop_group
@@ -139,6 +145,10 @@ function __init__()
139145
)
140146
_C_ON_PUBLISH_COMPLETE[] =
141147
@cfunction(_c_on_publish_complete, Cvoid, (Ptr{aws_mqtt_client_connection}, Cuint, Cint, Ptr{Cvoid}))
148+
_C_AWS_MALLOC[] = @cfunction(_c_aws_malloc, Ptr{Cvoid}, (Ptr{aws_allocator}, Csize_t))
149+
_C_AWS_FREE[] = @cfunction(_c_aws_free, Cvoid, (Ptr{aws_allocator}, Ptr{Cvoid}))
150+
_C_AWS_REALLOC[] = @cfunction(_c_aws_realloc, Ptr{Cvoid}, (Ptr{aws_allocator}, Ptr{Cvoid}, Csize_t, Csize_t))
151+
_C_AWS_CALLOC[] = @cfunction(_c_aws_calloc, Ptr{Cvoid}, (Ptr{aws_allocator}, Csize_t, Csize_t))
142152

143153
_AWSCRT_ALLOCATOR[] = let level = get(ENV, "AWS_CRT_MEMORY_TRACING", "")
144154
if !isempty(level)
@@ -152,6 +162,8 @@ function __init__()
152162
end
153163
frames_per_stack = parse(Int, strip(get(ENV, "AWS_CRT_MEMORY_TRACING_FRAMES_PER_STACK", "0")))
154164
aws_mem_tracer_new(aws_default_allocator(), C_NULL, level, frames_per_stack)
165+
elseif get(ENV, "AWS_CRT_USE_JL_ALLOCATOR", "") == "true"
166+
new_jl_allocator()
155167
else
156168
aws_default_allocator()
157169
end

src/allocator.jl

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function _c_aws_malloc(allocator::Ptr{aws_allocator}, size::Csize_t)::Ptr{Cvoid}
2+
return @ccall jl_malloc(size::Csize_t)::Ptr{Cvoid}
3+
end
4+
5+
function _c_aws_free(allocator::Ptr{aws_allocator}, ptr::Ptr{Cvoid})::Cvoid
6+
return @ccall jl_free(ptr::Ptr{Cvoid})::Cvoid
7+
end
8+
9+
function _c_aws_realloc(
10+
allocator::Ptr{aws_allocator},
11+
ptr::Ptr{Cvoid},
12+
old_size::Csize_t,
13+
new_size::Csize_t,
14+
)::Ptr{Cvoid}
15+
return @ccall jl_realloc(ptr::Ptr{Cvoid}, new_size::Csize_t)::Ptr{Cvoid}
16+
end
17+
18+
function _c_aws_calloc(allocator::Ptr{aws_allocator}, num::Csize_t, size::Csize_t)::Ptr{Cvoid}
19+
return @ccall jl_calloc(num::Csize_t, size::Csize_t)::Ptr{Cvoid}
20+
end
21+
22+
"""
23+
new_jl_allocator()
24+
25+
Returns an `aws_allocator` which integrates with Julia's GC to track memory allocated by native code.
26+
"""
27+
function new_jl_allocator()
28+
return aws_allocator(_C_AWS_MALLOC[], _C_AWS_FREE[], _C_AWS_REALLOC[], _C_AWS_CALLOC[], C_NULL)
29+
end

0 commit comments

Comments
 (0)