@@ -4,7 +4,7 @@ namespace Yggdrasil;
44
55internal static class Flat
66{
7- private readonly static IntPtr _libHandle ;
7+ private static IntPtr _libHandle ;
88
99 static Flat ( )
1010 {
@@ -17,11 +17,6 @@ static Flat()
1717 built_in_strategies = Marshal . GetDelegateForFunctionPointer < BuiltInStrategiesDelegate > ( NativeLibLoader . LoadFunctionPointer ( _libHandle , "flat_built_in_strategies" ) ) ;
1818 get_metrics = Marshal . GetDelegateForFunctionPointer < GetMetricsDelegate > ( NativeLibLoader . LoadFunctionPointer ( _libHandle , "flat_get_metrics" ) ) ;
1919 define_counter = Marshal . GetDelegateForFunctionPointer < DefineCounterDelegate > ( NativeLibLoader . LoadFunctionPointer ( _libHandle , "flat_define_counter" ) ) ;
20- inc_counter = Marshal . GetDelegateForFunctionPointer < IncCounterDelegate > ( NativeLibLoader . LoadFunctionPointer ( _libHandle , "flat_inc_counter" ) ) ;
21- define_gauge = Marshal . GetDelegateForFunctionPointer < DefineGaugeDelegate > ( NativeLibLoader . LoadFunctionPointer ( _libHandle , "flat_define_gauge" ) ) ;
22- set_gauge = Marshal . GetDelegateForFunctionPointer < SetGaugeDelegate > ( NativeLibLoader . LoadFunctionPointer ( _libHandle , "flat_set_gauge" ) ) ;
23- define_histogram = Marshal . GetDelegateForFunctionPointer < DefineHistogramDelegate > ( NativeLibLoader . LoadFunctionPointer ( _libHandle , "flat_define_histogram" ) ) ;
24- observe_histogram = Marshal . GetDelegateForFunctionPointer < ObserveHistogramDelegate > ( NativeLibLoader . LoadFunctionPointer ( _libHandle , "flat_observe_histogram" ) ) ;
2520 }
2621
2722 [ UnmanagedFunctionPointer ( CallingConvention . Cdecl ) ]
@@ -33,42 +28,29 @@ static Flat()
3328 private delegate Buf CheckEnabledDelegate ( IntPtr enginePtr , IntPtr messagePtr , nuint messageLen ) ;
3429
3530 [ UnmanagedFunctionPointer ( CallingConvention . Cdecl ) ]
36- private delegate Buf CheckVariantDelegate ( IntPtr enginePtr , IntPtr messagePtr , nuint messageLen ) ;
31+ public delegate Buf CheckVariantDelegate ( IntPtr enginePtr , IntPtr messagePtr , nuint messageLen ) ;
3732
3833 [ UnmanagedFunctionPointer ( CallingConvention . Cdecl ) ]
39- private delegate Buf ListKnownTogglesDelegate ( IntPtr enginePtr ) ;
34+ public delegate Buf ListKnownTogglesDelegate ( IntPtr enginePtr ) ;
4035
4136 [ UnmanagedFunctionPointer ( CallingConvention . Cdecl ) ]
42- private delegate Buf BuiltInStrategiesDelegate ( ) ;
37+ public delegate Buf BuiltInStrategiesDelegate ( ) ;
4338
4439 [ UnmanagedFunctionPointer ( CallingConvention . Cdecl ) ]
45- private delegate Buf GetMetricsDelegate ( IntPtr enginePtr ) ;
40+ public delegate Buf GetMetricsDelegate ( IntPtr enginePtr ) ;
4641
4742 [ UnmanagedFunctionPointer ( CallingConvention . Cdecl ) ]
48- private delegate Buf DefineCounterDelegate ( IntPtr enginePtr , IntPtr messagePtr , nuint messageLen ) ;
49- [ UnmanagedFunctionPointer ( CallingConvention . Cdecl ) ]
50- private delegate Buf IncCounterDelegate ( IntPtr enginePtr , IntPtr messagePtr , nuint messageLen ) ;
51- [ UnmanagedFunctionPointer ( CallingConvention . Cdecl ) ]
52- private delegate Buf DefineGaugeDelegate ( IntPtr enginePtr , IntPtr messagePtr , nuint messageLen ) ;
53- [ UnmanagedFunctionPointer ( CallingConvention . Cdecl ) ]
54- private delegate Buf SetGaugeDelegate ( IntPtr enginePtr , IntPtr messagePtr , nuint messageLen ) ;
55- [ UnmanagedFunctionPointer ( CallingConvention . Cdecl ) ]
56- private delegate Buf DefineHistogramDelegate ( IntPtr enginePtr , IntPtr messagePtr , nuint messageLen ) ;
57- [ UnmanagedFunctionPointer ( CallingConvention . Cdecl ) ]
58- private delegate Buf ObserveHistogramDelegate ( IntPtr enginePtr , IntPtr messagePtr , nuint messageLen ) ;
43+ public delegate Buf DefineCounterDelegate ( IntPtr enginePtr , IntPtr messagePtr , nuint messageLen ) ;
44+
5945 private static readonly TakeStateDelegate take_state ;
6046 private static readonly FreeBufferDelegate free_buffer ;
6147 private static readonly CheckEnabledDelegate check_enabled ;
6248 private static readonly CheckVariantDelegate check_variant ;
6349 private static readonly ListKnownTogglesDelegate list_known_toggles ;
6450 private static readonly BuiltInStrategiesDelegate built_in_strategies ;
6551 private static readonly GetMetricsDelegate get_metrics ;
52+
6653 private static readonly DefineCounterDelegate define_counter ;
67- private static readonly IncCounterDelegate inc_counter ;
68- private static readonly DefineGaugeDelegate define_gauge ;
69- private static readonly SetGaugeDelegate set_gauge ;
70- private static readonly DefineHistogramDelegate define_histogram ;
71- private static readonly ObserveHistogramDelegate observe_histogram ;
7254
7355 public static Buf TakeState ( IntPtr ptr , string json )
7456 {
@@ -77,14 +59,33 @@ public static Buf TakeState(IntPtr ptr, string json)
7759
7860 public static Buf CheckEnabled ( IntPtr ptr , byte [ ] message )
7961 {
80- return CallWithPinnedBytes ( message , ( msgPtr , len ) => check_enabled ( ptr , msgPtr , len ) ) ;
62+ nuint len = ( nuint ) message . Length ;
63+ GCHandle handle = GCHandle . Alloc ( message , GCHandleType . Pinned ) ;
64+ try
65+ {
66+ IntPtr msgPtr = handle . AddrOfPinnedObject ( ) ;
67+ return check_enabled ( ptr , msgPtr , len ) ;
68+ }
69+ finally
70+ {
71+ handle . Free ( ) ;
72+ }
8173 }
8274
8375 public static Buf CheckVariant ( IntPtr ptr , byte [ ] message )
8476 {
85- return CallWithPinnedBytes ( message , ( msgPtr , len ) => check_variant ( ptr , msgPtr , len ) ) ;
77+ nuint len = ( nuint ) message . Length ;
78+ GCHandle handle = GCHandle . Alloc ( message , GCHandleType . Pinned ) ;
79+ try
80+ {
81+ IntPtr msgPtr = handle . AddrOfPinnedObject ( ) ;
82+ return check_variant ( ptr , msgPtr , len ) ;
83+ }
84+ finally
85+ {
86+ handle . Free ( ) ;
87+ }
8688 }
87-
8889 public static Buf ListKnownToggles ( IntPtr ptr )
8990 {
9091 return list_known_toggles ( ptr ) ;
@@ -102,43 +103,12 @@ public static Buf GetMetrics(IntPtr ptr)
102103
103104 public static Buf DefineCounter ( IntPtr ptr , byte [ ] message )
104105 {
105- return CallWithPinnedBytes ( message , ( msgPtr , len ) => define_counter ( ptr , msgPtr , len ) ) ;
106- }
107-
108- public static Buf IncCounter ( IntPtr ptr , byte [ ] message )
109- {
110- return CallWithPinnedBytes ( message , ( msgPtr , len ) => inc_counter ( ptr , msgPtr , len ) ) ;
111- }
112-
113- public static Buf DefineGauge ( IntPtr ptr , byte [ ] message )
114- {
115- return CallWithPinnedBytes ( message , ( msgPtr , len ) => define_gauge ( ptr , msgPtr , len ) ) ;
116- }
117-
118- public static Buf SetGauge ( IntPtr ptr , byte [ ] message )
119- {
120- return CallWithPinnedBytes ( message , ( msgPtr , len ) => set_gauge ( ptr , msgPtr , len ) ) ;
121- }
122-
123- public static Buf DefineHistogram ( IntPtr ptr , byte [ ] message )
124- {
125- return CallWithPinnedBytes ( message , ( msgPtr , len ) => define_histogram ( ptr , msgPtr , len ) ) ;
126- }
127-
128- public static Buf ObserveHistogram ( IntPtr ptr , byte [ ] message )
129- {
130- return CallWithPinnedBytes ( message , ( msgPtr , len ) => observe_histogram ( ptr , msgPtr , len ) ) ;
131- }
132-
133- private static Buf CallWithPinnedBytes ( byte [ ] message , Func < IntPtr , nuint , Buf > invoker )
134- {
135- if ( message is null ) throw new ArgumentNullException ( nameof ( message ) ) ;
136-
137- var len = ( nuint ) message . Length ;
138- var handle = GCHandle . Alloc ( message , GCHandleType . Pinned ) ;
106+ nuint len = ( nuint ) message . Length ;
107+ GCHandle handle = GCHandle . Alloc ( message , GCHandleType . Pinned ) ;
139108 try
140109 {
141- return invoker ( handle . AddrOfPinnedObject ( ) , len ) ;
110+ IntPtr msgPtr = handle . AddrOfPinnedObject ( ) ;
111+ return define_counter ( ptr , msgPtr , len ) ;
142112 }
143113 finally
144114 {
0 commit comments