@@ -180,4 +180,67 @@ private static partial byte proc_handler_call_native(
180180 nint calldata ) ;
181181
182182 #endregion
183+
184+ #region Memory (for calldata management)
185+
186+ /// <summary>
187+ /// Allocates zeroed memory using OBS's allocator.
188+ /// </summary>
189+ [ LibraryImport ( Lib , EntryPoint = "bzalloc" ) ]
190+ [ UnmanagedCallConv ( CallConvs = [ typeof ( CallConvCdecl ) ] ) ]
191+ internal static partial nint bzalloc ( nuint size ) ;
192+
193+ /// <summary>
194+ /// Frees memory allocated by OBS's allocator.
195+ /// </summary>
196+ [ LibraryImport ( Lib , EntryPoint = "bfree" ) ]
197+ [ UnmanagedCallConv ( CallConvs = [ typeof ( CallConvCdecl ) ] ) ]
198+ internal static partial void bfree ( nint ptr ) ;
199+
200+ #endregion
201+
202+ #region Calldata Management
203+
204+ /// <summary>
205+ /// Size of the calldata structure (4 fields on x64: pointer + 2 size_t + bool with padding).
206+ /// </summary>
207+ internal static nuint CalldataSize => ( nuint ) ( nint . Size * 4 ) ;
208+
209+ /// <summary>
210+ /// Creates and initializes an empty calldata structure.
211+ /// </summary>
212+ internal static nint calldata_create ( )
213+ {
214+ var cd = bzalloc ( CalldataSize ) ;
215+ // bzalloc already zeros the memory, which is equivalent to calldata_init
216+ return cd ;
217+ }
218+
219+ /// <summary>
220+ /// Destroys a calldata structure and frees its memory.
221+ /// </summary>
222+ internal static void calldata_destroy ( nint calldata )
223+ {
224+ if ( calldata == nint . Zero ) return ;
225+
226+ // Free the internal stack if allocated (first field is the stack pointer)
227+ unsafe
228+ {
229+ var stackPtr = * ( nint * ) calldata ;
230+ // Check if not fixed (4th field, which is a bool - need to check offset)
231+ // For simplicity, we'll just free the stack pointer if it's non-zero
232+ // The 'fixed' flag is at offset: sizeof(nint) + sizeof(nuint) + sizeof(nuint)
233+ var fixedOffset = nint . Size + nint . Size + nint . Size ;
234+ var isFixed = * ( byte * ) ( calldata + fixedOffset ) != 0 ;
235+
236+ if ( stackPtr != nint . Zero && ! isFixed )
237+ {
238+ bfree ( stackPtr ) ;
239+ }
240+ }
241+
242+ bfree ( calldata ) ;
243+ }
244+
245+ #endregion
183246}
0 commit comments