1- using .. LLVM
2- using .. LLVM. Interop
3-
4- abstract type RTMap{Name,MT,K,V,ME,F} end
5- abstract type AbstractHashMap{Name,MT,K,V,ME,F} <: RTMap{Name,MT,K,V,ME,F} end
6- abstract type AbstractArrayMap{Name,MT,K,V,ME,F} <: RTMap{Name,MT,K,V,ME,F} end
7-
8- struct HashMap{Name,MT,K,V,ME,F} <: AbstractHashMap{Name,MT,K,V,ME,F} end
9- maptype_to_jltype (:: Val{API.BPF_MAP_TYPE_HASH} ) = HashMap
10- struct ArrayMap{Name,MT,K,V,ME,F} <: AbstractArrayMap{Name,MT,K,V,ME,F} end
11- maptype_to_jltype (:: Val{API.BPF_MAP_TYPE_ARRAY} ) = ArrayMap
12-
13- function RTMap (; name, maptype, keytype, valuetype, maxentries= 1 , flags= 0 )
14- jltype = maptype_to_jltype (Val (maptype))
15- jltype {Symbol(name), maptype, keytype, valuetype, maxentries, flags} ()
16- end
17-
181function map_lookup_elem (map:: RTMap{Name,MT,K,V,ME,F} , key:: K ) where {Name,MT,K,V,ME,F}
192 keyref = Ref {K} (key)
203 GC. @preserve keyref begin
@@ -37,19 +20,6 @@ function map_delete_elem(map::RTMap{Name,MT,K,V,ME,F}, key::K) where {Name,MT,K,
3720 _map_delete_elem (map, Base. unsafe_convert (Ptr{K}, keyref))
3821 end
3922end
40- function _genmap! (mod:: LLVM.Module , :: Type{<:RTMap{Name,MT,K,V,ME,F}} , ctx) where {Name,MT,K,V,ME,F}
41- T_i32 = LLVM. Int32Type (ctx)
42- T_map = LLVM. StructType ([T_i32, T_i32, T_i32, T_i32, T_i32], ctx)
43- name = string (Name)
44- gv = GlobalVariable (mod, T_map, name)
45- section! (gv, " maps" )
46- alignment! (gv, 4 )
47- vec = Any[Int32 (MT),Int32 (sizeof (K)),Int32 (sizeof (V)),Int32 (ME),Int32 (F)]
48- init = ConstantStruct ([ConstantInt (v, ctx) for v in vec], ctx)
49- initializer! (gv, init)
50- linkage! (gv, LLVM. API. LLVMLinkOnceODRLinkage)
51- return gv
52- end
5323@generated function _map_lookup_elem (map:: RTMap{Name,MT,K,V,ME,F} , key:: Ptr{K} ) where {Name,MT,K,V,ME,F}
5424 JuliaContext () do ctx
5525 T_keyp = LLVM. PointerType (convert (LLVMType, K, ctx))
@@ -217,6 +187,17 @@ function Base.haskey(map::AbstractArrayMap{Name,MT,K,V,ME,F}, idx) where {Name,M
217187 end
218188end
219189
190+ function Base. get (map:: RTMap{Name,MT,K,V,ME,F} , k:: K , v:: V ) where {Name,MT,K,V,ME,F}
191+ map_v = map[k]
192+ if map_v != = nothing
193+ return map_v
194+ else
195+ return v
196+ end
197+ end
198+ @inline Base. get (map:: RTMap{Name,MT,K,V,ME,F} , k, v) where {Name,MT,K,V,ME,F} =
199+ get (map, bpfconvert (K, k), bpfconvert (V, v))
200+
220201# # Perf
221202
222203#= TODO
0 commit comments