From 0e6e166ca64879bb6cab6316be2383aaa24619ad Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Fri, 22 Nov 2024 11:26:06 +0300 Subject: [PATCH] Removing initial idea with blobs --- src/vm/util/blob.cs | 90 --------------------------------------------- tests/test_blobs.cs | 19 ++-------- 2 files changed, 4 insertions(+), 105 deletions(-) delete mode 100644 src/vm/util/blob.cs diff --git a/src/vm/util/blob.cs b/src/vm/util/blob.cs deleted file mode 100644 index 16e18742..00000000 --- a/src/vm/util/blob.cs +++ /dev/null @@ -1,90 +0,0 @@ -using System; -using System.Runtime.InteropServices; -using System.Runtime.CompilerServices; - -namespace bhl { - -public class Blob : IValRefcounted where T : unmanaged -{ - internal Pool> pool; - - //NOTE: -1 means it's in released state, - // public only for quick inspection - internal int _refs; - - public int refs => _refs; - - internal byte[] data; - - static int Size = Marshal.SizeOf(); - - static class PoolHolder where T1 : unmanaged - { - public static System.Threading.ThreadLocal>> pool = - new System.Threading.ThreadLocal>>(() => - { - return new Pool>(); - }); - } - - internal Blob() - {} - - static public Blob New(ref T val) - { - var pool = PoolHolder.pool.Value; - - Blob blob = null; - if(pool.stack.Count == 0) - { - ++pool.miss; - blob = new Blob(); - blob.data = new byte[Size]; - } - else - { - ++pool.hits; - blob = pool.stack.Pop(); - } - - Unsafe.As(ref blob.data[0]) = val; - - blob._refs = 1; - blob.pool = pool; - - return blob; - } - - public ref T Value => ref Unsafe.As(ref data[0]); - - public void Retain() - { - if(_refs == -1) - throw new Exception("Invalid state(-1)"); - ++_refs; - } - - public void Release() - { - if(_refs == -1) - throw new Exception("Invalid state(-1)"); - if(_refs == 0) - throw new Exception("Double free(0)"); - - --_refs; - if(_refs == 0) - Del(this); - } - - static void Del(Blob blob) - { - if(blob._refs != 0) - throw new Exception("Freeing invalid object, refs " + blob._refs); - - blob._refs = -1; - - blob.pool.stack.Push(blob); - } -} - -} diff --git a/tests/test_blobs.cs b/tests/test_blobs.cs index 105af191..7d008556 100644 --- a/tests/test_blobs.cs +++ b/tests/test_blobs.cs @@ -13,20 +13,9 @@ public struct StructBlob [Fact] public void TestBlob() { - var val = new StructBlob(); - val.x = 1; - val.y = 10; - val.z = 100; - - var blob = Blob.New(ref val); - - AssertEqual(1, blob.Value.x); - AssertEqual(10, blob.Value.y); - AssertEqual(100, blob.Value.z); - - blob.Value.y = 30; - AssertEqual(30, blob.Value.y); - - blob.Release(); + //var val = new StructBlob(); + //val.x = 1; + //val.y = 10; + //val.z = 100; } }