1234567891011121314151617181920 |
- namespace TBL.CSharp.Base
- {
- public static partial class Extension
- {
- /// <summary>
- /// 缓冲使用委托
- /// </summary>
- public unsafe delegate void OnBufferUse(void* buffer);
-
- /// <summary>
- /// 在栈上开辟一段指定长度的 <see langword="unsafe"/> 缓冲区并立即使用
- /// <para>因为 C# 的 <see langword="stackalloc"/> 作用域问题,使用此方式可以避免栈溢出</para>
- /// </summary>
- public static unsafe void UseStackBuffer(int size, OnBufferUse callback)
- {
- var buffer = stackalloc byte[size];
- callback(buffer);
- }
- }
- }
|