Exemplo n.º 1
0
        public static NativeDynamicArray Alloc <T>(Allocator allocator, uint newLength = 0) where T : unmanaged
        {
            unsafe
            {
                var rtnStruc = new NativeDynamicArray();
#if DEBUG && !PROFILE_SVELTO
                rtnStruc.hashType = TypeHash <T> .hash;
#endif
                var sizeOf = MemoryUtilities.SizeOf <T>();

                uint         pointerSize = (uint)MemoryUtilities.SizeOf <UnsafeArray>();
                UnsafeArray *listData    =
                    (UnsafeArray *)MemoryUtilities.Alloc(pointerSize, allocator);

                //clear to nullify the pointers
                MemoryUtilities.MemClear((IntPtr)listData, pointerSize);

                listData->allocator = allocator;
                listData->Realloc <T>((uint)(newLength * sizeOf));

                rtnStruc._list = listData;

                return(rtnStruc);
            }
        }
Exemplo n.º 2
0
        public ref T ReserveEnqueue <T>(out UnsafeArrayIndex index) where T : struct
        {
            unsafe
            {
                BasicTests();

                var sizeOf = MemoryUtilities.SizeOf <T>();
                if (_queue->space - sizeOf < 0)
                {
                    //Todo: NativeBag is very complicated. At the time of writing of this comment I don't remember if the sizeof really needs to be aligned by 4. To check and change this comment
                    _queue->Realloc((uint)((_queue->capacity + MemoryUtilities.Align4((uint)sizeOf)) * 2.0f));
                }

#if ENABLE_THREAD_SAFE_CHECKS
                try
                {
#endif

                return(ref _queue->Reserve <T>(out index));

#if ENABLE_THREAD_SAFE_CHECKS
            }
            finally
            {
                Volatile.Write(ref _threadSentinel, 0);
            }
#endif
            }
        }
Exemplo n.º 3
0
        public ref T ReserveEnqueue <T>(out UnsafeArrayIndex index) where T : struct
        {
            unsafe
            {
                BasicTests();

                var sizeOf = MemoryUtilities.SizeOf <T>();
                if (_queue->space - sizeOf < 0)
                {
                    _queue->Realloc((uint)((_queue->capacity + sizeOf) * 2.0f));
                }

#if DEBUG && !PROFILE_SVELTO
                try
                {
#endif

                return(ref _queue->Reserve <T>(out index));

#if DEBUG && !PROFILE_SVELTO
            }
            finally
            {
                Volatile.Write(ref _threadSentinel, 0);
            }
#endif
            }
        }
Exemplo n.º 4
0
        public static NativeDynamicArray Alloc <T>(Allocator allocator, uint newLength = 0) where T : struct
        {
            unsafe
            {
#if DEBUG && !PROFILE_SVELTO
                var rtnStruc = new NativeDynamicArray {
                    _hashType = TypeHash <T> .hash
                };
#else
                NativeDynamicArray rtnStruc = default;
#endif
                var sizeOf = MemoryUtilities.SizeOf <T>();

                uint         structSize = (uint)MemoryUtilities.SizeOf <UnsafeArray>();
                UnsafeArray *listData   = (UnsafeArray *)MemoryUtilities.Alloc(structSize, allocator);

                //clear to nullify the pointers
                //MemoryUtilities.MemClear((IntPtr) listData, structSize);

                rtnStruc._allocator = allocator;
                listData->Realloc((uint)(newLength * sizeOf), allocator);

                rtnStruc._list = listData;

                return(rtnStruc);
            }
        }
Exemplo n.º 5
0
        public ref T ReserveEnqueue <T>(out UnsafeArrayIndex index) where T : struct
        {
            unsafe
            {
                BasicTests();

                var sizeOf = MemoryUtilities.SizeOf <T>();
                if (_queue->availableSpace - sizeOf < 0)
                {
                    _queue->Realloc((_queue->capacity + (uint)sizeOf) << 1);
                }

#if ENABLE_THREAD_SAFE_CHECKS
                try
                {
#endif

                return(ref _queue->Reserve <T>(out index));

#if ENABLE_THREAD_SAFE_CHECKS
            }
            finally
            {
                Volatile.Write(ref _threadSentinel, 0);
            }
#endif
            }
        }
        public void Alloc(uint newCapacity)
        {
            DBC.Common.Check.Require(buffer == null || buffer.ToNativeArray(out _) == IntPtr.Zero, "can't alloc an already allocated buffer");

            var    realBuffer = MemoryUtilities.Alloc((uint)(newCapacity * MemoryUtilities.SizeOf <T>()), Allocator.Persistent);
            NB <T> b          = new NB <T>(realBuffer, newCapacity);

            buffer          = b;
            this.realBuffer = b;
        }
Exemplo n.º 7
0
        public ref T Get <T>(uint index) where T : struct
        {
            unsafe {
#if DEBUG && !PROFILE_SVELTO
                uint sizeOf = (uint)MemoryUtilities.SizeOf <T>();
                if (index + sizeOf > _writeIndex)
                {
                    throw new Exception("no reading authorized");
                }
#endif
                return(ref Unsafe.AsRef <T>(Unsafe.Add <T>(ptr, (int)index)));
            }
        }
Exemplo n.º 8
0
        public SharedSveltoDictionaryNative(uint size, Allocator nativeAllocator)
        {
            var dictionary =
                new SveltoDictionary <TKey, TValue, NativeStrategy <SveltoDictionaryNode <TKey> >, NativeStrategy <TValue>,
                                      NativeStrategy <int> >(
                    size, nativeAllocator);
            int structSize = MemoryUtilities.SizeOf <SveltoDictionary <TKey, TValue, NativeStrategy <SveltoDictionaryNode <TKey> >, NativeStrategy <TValue>,
                                                                       NativeStrategy <int> > >();

            _sharedDictionary = MemoryUtilities.Alloc((uint)structSize, Allocator.Persistent);

            _dictionary = dictionary;
        }
Exemplo n.º 9
0
        public NativeBag(Allocator allocator)
        {
            unsafe
            {
                var sizeOf   = MemoryUtilities.SizeOf <UnsafeBlob>();
                var listData = (UnsafeBlob *)MemoryUtilities.Alloc((uint)sizeOf, allocator);

                //clear to nullify the pointers
                MemoryUtilities.MemClear((IntPtr)listData, (uint)sizeOf);
                listData->allocator = allocator;
                _queue = listData;
            }
        }
        public void Alloc(uint newCapacity, Allocator nativeAllocator)
        {
            _nativeAllocator = nativeAllocator;

            Check.Require(this._realBuffer.ToNativeArray(out _) == IntPtr.Zero
                          , "can't alloc an already allocated buffer");

            var realBuffer =
                MemoryUtilities.Alloc((uint)(newCapacity * MemoryUtilities.SizeOf <T>()), _nativeAllocator);
            NB <T> b = new NB <T>(realBuffer, newCapacity);

            _buffer          = IntPtr.Zero;
            this._realBuffer = b;
        }
Exemplo n.º 11
0
        public NativeBag(Allocator allocator)
        {
            unsafe {
                var sizeOf   = MemoryUtilities.SizeOf <UnsafeBlob>();
                var listData = (UnsafeBlob *)MemoryUtilities.Alloc((uint)sizeOf, allocator);

                //clear to nullify the pointers
                //MemoryUtilities.MemClear((IntPtr) listData, (uint) sizeOf);
                listData->allocator = allocator;
                _queue = listData;
#if ENABLE_THREAD_SAFE_CHECKS
                _threadSentinel = 0;
#endif
            }
        }
        public void Resize(uint newCapacity)
        {
            Check.Require(newCapacity > 0, "Resize requires a size greater than 0");
            Check.Require(newCapacity > capacity, "can't resize to a smaller size");

            var pointer = _realBuffer.ToNativeArray(out _);
            var sizeOf  = MemoryUtilities.SizeOf <T>();

            pointer = MemoryUtilities.Realloc(pointer, (uint)(capacity * sizeOf), (uint)(newCapacity * sizeOf)
                                              , Allocator.Persistent);
            NB <T> b = new NB <T>(pointer, newCapacity);

            _buffer     = IntPtr.Zero;
            _realBuffer = b;
        }
Exemplo n.º 13
0
        public void Alloc(uint newCapacity, Allocator nativeAllocator)
        {
#if DEBUG && !PROFILE_SVELTO
            if (!(this._realBuffer.ToNativeArray(out _) == IntPtr.Zero))
            {
                throw new PreconditionException("can't alloc an already allocated buffer");
            }
#endif
            _nativeAllocator = nativeAllocator;

            var realBuffer =
                MemoryUtilities.Alloc((uint)(newCapacity * MemoryUtilities.SizeOf <T>()), _nativeAllocator);
            NB <T> b = new NB <T>(realBuffer, newCapacity);
            _buffer     = default;
            _realBuffer = b;
        }
Exemplo n.º 14
0
        public ThreadSafeNativeBag(Allocator allocator, uint capacity)
        {
            unsafe
            {
                var sizeOf   = MemoryUtilities.SizeOf <UnsafeBlob>();
                var listData = (UnsafeBlob *)MemoryUtilities.Alloc((uint)sizeOf, allocator);

                //clear to nullify the pointers
                //MemoryUtilities.MemClear((IntPtr) listData, (uint) sizeOf);
                listData->allocator = allocator;
                _queue = = tData;
                _queue->Realloc(capacity);
            }

            _writingGuard = 0;
        }
Exemplo n.º 15
0
        public NativeBag(Allocator allocator)
        {
            unsafe
            {
                var sizeOf   = MemoryUtilities.SizeOf <UnsafeBlob>();
                var listData = (UnsafeBlob *)MemoryUtilities.Alloc((uint)sizeOf, allocator);

                //clear to nullify the pointers
                MemoryUtilities.MemClear((IntPtr)listData, (uint)sizeOf);
                listData->allocator = allocator;
                _queue = listData;
#if DEBUG && !PROFILE_SVELTO
                _threadSentinel = 0;
#endif
            }
        }
Exemplo n.º 16
0
        public int Capacity <T>() where T : struct
        {
            unsafe {
#if DEBUG && !PROFILE_SVELTO
                if (_list == null)
                {
                    throw new Exception("NativeDynamicArray: null-access");
                }
                if (_hashType != TypeHash <T> .hash)
                {
                    throw new Exception("NativeDynamicArray: not expected type used");
                }
#endif
                return(_list->capacity / MemoryUtilities.SizeOf <T>());
            }
        }
Exemplo n.º 17
0
        public int Count <T>() where T : struct
        {
            unsafe
            {
#if ENABLE_DEBUG_CHECKS
                if (_list == null)
                {
                    throw new Exception("NativeDynamicArray: null-access");
                }
                if (_hashType != TypeHash <T> .hash)
                {
                    throw new Exception($"NativeDynamicArray: not expected type used");
                }
#endif
                return(_list->count / MemoryUtilities.SizeOf <T>());
            }
        }
Exemplo n.º 18
0
        public uint Count <T>() where T : unmanaged
        {
            unsafe
            {
#if DEBUG && !PROFILE_SVELTO
                if (_list == null)
                {
                    throw new Exception("NativeDynamicArray: null-access");
                }
                if (hashType != TypeHash <T> .hash)
                {
                    throw new Exception("NativeDynamicArray: not excepted type used");
                }
#endif
                return((uint)(_list->count / MemoryUtilities.SizeOf <T>()));
            }
        }
Exemplo n.º 19
0
        public ref T ReserveEnqueue <T>(out UnsafeArrayIndex index) where T : struct
        {
            unsafe
            {
#if DEBUG && !PROFILE_SVELTO
                if (_queue == null)
                {
                    throw new Exception("SimpleNativeArray: null-access");
                }
#endif
                var sizeOf = MemoryUtilities.SizeOf <T>();
                if (_queue->space - sizeOf < 0)
                {
                    _queue->Realloc((uint)((_queue->capacity + sizeOf) * 2.0f));
                }

                return(ref _queue->Reserve <T>(out index));
            }
        }
Exemplo n.º 20
0
        public void Resize(uint newCapacity)
        {
#if DEBUG && !PROFILE_SVELTO
            if (!(newCapacity > 0))
            {
                throw new PreconditionException("Resize requires a size greater than 0");
            }
            if (!(newCapacity > capacity))
            {
                throw new PreconditionException("can't resize to a smaller size");
            }
#endif
            var pointer = _realBuffer.ToNativeArray(out _);
            var sizeOf  = MemoryUtilities.SizeOf <T>();
            pointer = MemoryUtilities.Realloc(pointer, (uint)(capacity * sizeOf), (uint)(newCapacity * sizeOf)
                                              , Allocator.Persistent);
            NB <T> b = new NB <T>(pointer, newCapacity);
            _buffer     = IntPtr.Zero;
            _realBuffer = b;
        }
Exemplo n.º 21
0
        public void Resize(uint newCapacity, bool copyContent = true)
        {
#if DEBUG && !PROFILE_SVELTO
            if (!(newCapacity > 0))
            {
                throw new PreconditionException("Resize requires a size greater or equal to 0");
            }
            if (!(newCapacity > capacity))
            {
                throw new PreconditionException("can't resize to a smaller size");
            }
#endif
            var pointer = _realBuffer.ToNativeArray(out _);
            var sizeOf  = MemoryUtilities.SizeOf <T>();
            pointer = MemoryUtilities.Realloc(pointer, (uint)(capacity * sizeOf), (uint)(newCapacity * sizeOf)
                                              , _nativeAllocator, copyContent);
            NB <T> b = new NB <T>(pointer, newCapacity);
            _realBuffer    = b;
            _invalidHandle = true;
        }
Exemplo n.º 22
0
        public ref T ReserveEnqueue <T>
            (out UnsafeArrayIndex index)
            where T : struct //should be unmanaged, but it's not due to Svelto.ECS constraints.
        {
            unsafe
            {
                BasicTests();

                var sizeOf = MemoryUtilities.SizeOf <T>();

                using (_threadSentinel.TestThreadSafety())
                {
                    if (_queue->availableSpace - sizeOf < 0)
                    {
                        _queue->Grow <T>();
                    }

                    return(ref _queue->Reserve <T>(out index));
                }
            }
        }
        public AtomicNativeBags(Allocator allocator)
        {
            _allocator    = allocator;
            _threadsCount = JobsUtility.MaxJobThreadCount + 1;

            var bufferSize     = MemoryUtilities.SizeOf <NativeBag>();
            var bufferCount    = _threadsCount;
            var allocationSize = bufferSize * bufferCount;

            var ptr = (byte *)MemoryUtilities.Alloc((uint)allocationSize, allocator);

            MemoryUtilities.MemClear((IntPtr)ptr, (uint)allocationSize);

            for (int i = 0; i < bufferCount; i++)
            {
                var bufferPtr = (NativeBag *)(ptr + bufferSize * i);
                var buffer    = new NativeBag(allocator);
                MemoryUtilities.CopyStructureToPtr(ref buffer, (IntPtr)bufferPtr);
            }

            _data = (NativeBag *)ptr;
        }
Exemplo n.º 24
0
 public void Clear()
 {
     MemoryUtilities.MemClear(_ptr, (uint)(_capacity * MemoryUtilities.SizeOf <T>()));
 }