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>()); } }
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 } }
public void Dispose() { #if DEBUG if (_data == null) { throw new Exception("using invalid AtomicNativeBags"); } #endif for (int i = 0; i < _threadsCount; i++) { GetBuffer(i).Dispose(); } MemoryUtilities.Free((IntPtr)_data, _allocator); _data = null; }
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; }
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; }
public void ShiftRight(uint index, uint count) { DBC.Common.Check.Require(index < capacity, "out of bounds index"); DBC.Common.Check.Require(count < capacity, "out of bounds count"); if (count == index) { return; } DBC.Common.Check.Require(count > index, "wrong parameters used"); var array = _realBuffer.ToNativeArray(out _); MemoryUtilities.Memmove <T>(array, index, index + 1, count - index); }
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 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>()); } }
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>())); } }
internal Consumer(string name, uint capacity) : this() { unsafe { #if DEBUG && !PROFILE_SVELTO _name = name; #endif _ringBuffer = new RingBuffer <ValueTuple <T, EGID> >((int)capacity, #if DEBUG && !PROFILE_SVELTO _name #else string.Empty #endif ); mustBeDisposed = MemoryUtilities.Alloc(sizeof(bool), Allocator.Persistent); *(bool *)mustBeDisposed = false; } }
public Item(GameSave gameSave, int typeIdOffset, int offset, int dataLength, int itemBuffsOffset, int itemBuffsLength, int itemGemsOffset, int itemGemsLength) { (_gameSave, TypeIdOffset, ItemOffset) = (gameSave, typeIdOffset, offset); _levelShiftOffset = (byte)(8 * gameSave.Body[TypeIdOffset + 10]); ItemBytes = _gameSave.Body.AsSpan(offset, dataLength).ToArray(); ItemSockets = new ItemSockets(gameSave, itemGemsOffset, itemGemsLength); ItemBuffs = new ItemBuffMemory(gameSave, itemBuffsOffset, itemBuffsLength); PlayerBuffs = new List <Buff>(BuffCount); for (int i = 0; i < PlayerBuffs.Capacity; i++) { PlayerBuffs.Add(Amalur.GetBuff(MemoryUtilities.Read <uint>(ItemBytes, Offsets.FirstBuff + i * 8))); } if (HasCustomName) { ItemName = Encoding.UTF8.GetString(ItemBytes, Offsets.Name, NameLength); } }
private static long GetNumBytesUsed(int numInts) { // force garbage collection GC.Collect(); var begin = MemoryUtilities.NumBytesUsed(true); var ints = new int[numInts]; ints[0] = 123; var end = MemoryUtilities.NumBytesUsed(true); // force the array to be in memory until we have our measurements Assert.Equal(123, ints[0]); return(end - begin); }
public void AllocateMore(uint newSize, uint numberOfItemsToCopy, bool clear = true) { #if DEBUG && !PROFILE_SVELTO if (newSize <= capacity) { throw new DBC.Common.PreconditionException("can't alloc more to a smaller or equal size"); } if (numberOfItemsToCopy > capacity) { throw new DBC.Common.PreconditionException("out of bounds number of elements to copy"); } #endif var pointer = _realBuffer.ToNativeArray(out _); pointer = MemoryUtilities.Realloc <T>(pointer, newSize, _nativeAllocator, numberOfItemsToCopy, true, clear); NB <T> b = new NB <T>(pointer, newSize); _realBuffer = b; _invalidHandle = true; }
public static IntPtr Create() { unsafe { //allocate the pointer to the dictionary IntPtr dic = MemoryUtilities .Alloc <SveltoDictionary <long, Sentinel, SentinelNativeStrategy <SveltoDictionaryNode <long> >, SentinelNativeStrategy <Sentinel>, SentinelNativeStrategy <int> > >(1, Allocator.Persistent); //allocate the dictionary itself Unsafe.AsRef <SveltoDictionary <long, Sentinel, SentinelNativeStrategy <SveltoDictionaryNode <long> >, SentinelNativeStrategy <Sentinel>, SentinelNativeStrategy <int> > >((void *)dic) = new SveltoDictionary <long, Sentinel, SentinelNativeStrategy <SveltoDictionaryNode <long> >, SentinelNativeStrategy <Sentinel>, SentinelNativeStrategy <int> >(1, Allocator.Persistent); return(dic); } }
public void Alloc(uint newCapacity, Allocator allocator, bool clear) { #if DEBUG && !PROFILE_SVELTO if (!(this._realBuffer.ToNativeArray(out _) == IntPtr.Zero)) { throw new DBC.Common.PreconditionException("can't alloc an already allocated buffer"); } if (allocator != Allocator.Persistent && allocator != Allocator.Temp && allocator != Allocator.TempJob) { throw new Exception("invalid allocator used for native strategy"); } #endif _nativeAllocator = allocator; IntPtr realBuffer = MemoryUtilities.Alloc <T>(newCapacity, _nativeAllocator, clear); NB <T> b = new NB <T>(realBuffer, newCapacity); _invalidHandle = true; _realBuffer = b; }
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)); } }
internal CoreEffectMemory(Span <byte> buffer) { ReadOnlySpan <byte> bytes = Amalur.Bytes; ReadOnlySpan <byte> coreEffectSequence = new byte[] { 0x84, 0x60, 0x28, 0x00, 0x00 }; coreEffectSequence.CopyTo(buffer.Slice(8)); ItemIndex = bytes.IndexOf(buffer); ReadOnlySpan <byte> span = bytes.Slice(ItemIndex); int count = span[Offsets.EffectCount]; DataLength = Offsets.FirstEffect + (count * 24) + 8; Bytes = span.Slice(0, DataLength).ToArray(); var firstDisplayEffect = Offsets.FirstEffect + (count * 16) + 8; for (int i = 0; i < count; i++) { List.Add(MemoryUtilities.Read <uint>(span, firstDisplayEffect + i * 8)); } }
public void TestResize() { unsafe { var ptr = MemoryUtilities.Alloc(10, Allocator.Persistent); Unsafe.Write((void *)ptr, new Test() { a = 3 , b = 1 }); Unsafe.Write((void *)(ptr + 8), (short)-10); ptr = MemoryUtilities.Realloc(ptr, 10, Allocator.Persistent, 16); var test = Unsafe.Read <Test>((void *)ptr); Assert.That(test.a == 3); Assert.That(test.b == 1); Assert.That(Unsafe.Read <short>((void *)(ptr + 8)) == -10); MemoryUtilities.Free(ptr, Allocator.Persistent); } }
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; }
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; }
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 Stash(GameSave gameSave, int offset) { (_gameSave, _offset) = (gameSave, offset); Items.Capacity = Count; Span <byte> data = _gameSave.Body.AsSpan(_offset, DataLength); if (Items.Capacity > 0) { var indices = GetAllIndices(data, gameSave.IsRemaster); for (int i = 0; i < indices.Count - 1; i++) { if (Amalur.ItemDefinitions.ContainsKey(MemoryUtilities.Read <uint>(_gameSave.Body, _offset + indices[i]))) { var itemStart = indices[i]; var gems = Array.Empty <Gem>(); if (_gameSave.Body[_offset + indices[i + 1] - 1] != 0xFF) { var gemList = new List <Gem>(); var ix = _offset + indices[i + 1] - 4; uint handle; while ((handle = MemoryUtilities.Read <uint>(_gameSave.Body, ix)) > 4u) { ix -= 4; } for (uint j = 0; j < handle; j++) { i++; if (Amalur.GemDefinitions.ContainsKey(MemoryUtilities.Read <uint>(_gameSave.Body, _offset + indices[i]))) { gemList.Add(new Gem(_gameSave, _offset + indices[i])); } } gems = gemList.ToArray(); } var item = CreateStashItem(gameSave, _offset + itemStart, (i + 1 == indices.Count ? DataLength : indices[i + 1]) - itemStart, gems); Items.Add(item); } } // ok we might read this twice, who cares. if (Amalur.ItemDefinitions.ContainsKey(MemoryUtilities.Read <uint>(_gameSave.Body, _offset + indices[^ 1])))
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; }
public async Task <string> CallTask(string url, string token, string data, string cmdType) { Console.WriteLine(url + " - " + data); Uri serviceAccountTokenUri = new Uri(url); string requestJson = data; using (var webClient = new WebClient()) { try { ConfigureRequestHeaders(webClient, token); string response = ""; if (cmdType == "POST") { response = await webClient.UploadStringTaskAsync(serviceAccountTokenUri, requestJson); } else { response = await webClient.DownloadStringTaskAsync(serviceAccountTokenUri); } //_logger.LogDebug($"Response:\n{response}"); return(response); } catch (Exception ex) { //_logger.LogCritical(exception.ToString()); } finally { MemoryUtilities.CleanupWebClient(webClient); } } return(null); }
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 UnsafeArray *listData = (UnsafeArray *)MemoryUtilities.Alloc <UnsafeArray>(1, allocator); //clear to nullify the pointers //MemoryUtilities.MemClear((IntPtr) listData, structSize); rtnStruc._allocator = allocator; listData->Realloc <T>(newLength, allocator); rtnStruc._list = listData; return(rtnStruc); } }
public static NativeDynamicArray Alloc <T>(Allocator allocator, uint newLength = 0) where T : struct { 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((uint)(newLength * sizeOf)); rtnStruc._list = listData; return(rtnStruc); } }
public StashItem(GameSave gameSave, int offset, int dataLength, Gem[] gems) { ItemOffset = offset; Bytes = gameSave.Body.AsSpan(offset, dataLength).ToArray(); PlayerBuffs.Capacity = BuffCount; var firstBuff = Offsets.FirstBuff; for (int i = 0; i < PlayerBuffs.Capacity; i++) { PlayerBuffs.Add(Amalur.GetBuff(MemoryUtilities.Read <uint>(Bytes, firstBuff + (i * 8)))); } if (HasCustomName) { ItemName = Encoding.UTF8.GetString(Bytes, Offsets.Name, NameLength); } Gems = gems; // socket section is either FF // or 20 02, followed by int32 count, and int32 handle per gem. int socketsStart = gems.Length == 0 ? Bytes.Length - 1 : gems[0].ItemOffset - offset - (4 * (1 + gems.Length)) - 2; ItemBuffs = Bytes[Offsets.HasItemBuffs] == 0x14 ? new ItemBuffMemory(this, socketsStart) : Definition.ItemBuffs; }
public void CreateIndex() { var index = new JasixIndex(); IndexHeader(index); string lastLine = IndexPositions(index); IndexGenes(lastLine, index); index.Write(_writeStream); Console.WriteLine(); long peakMemoryUsageBytes = MemoryUtilities.GetPeakMemoryUsage(); var wallTimeSpan = _benchmark.GetElapsedTime(); Console.WriteLine(); if (peakMemoryUsageBytes > 0) { Console.WriteLine("Peak memory usage: {0}", MemoryUtilities.ToHumanReadable(peakMemoryUsageBytes)); } Console.WriteLine("Time: {0}", Benchmark.ToHumanReadable(wallTimeSpan)); }
/// <summary> /// executes the command-line workflow /// </summary> public void Execute(string[] args) { var bench = new Benchmark(); try { List <string> unsupportedOps = null; if (args == null || args.Length == 0) { SetExitCode(ExitCodes.MissingCommandLineOption); _showHelpMenu = true; } else { try { unsupportedOps = _commandLineOps.Parse(args); if (unsupportedOps.Count > 0) { SetExitCode(ExitCodes.UnknownCommandLineOption); _showHelpMenu = true; } } catch (OptionException oe) { _errorBuilder.AppendFormat("{0}ERROR: {1}\n", _errorSpacer, oe.Message); SetExitCode(ExitCodes.UnknownCommandLineOption); _showHelpMenu = true; } } if (_showVersion) { Console.WriteLine("{0} {1}", _versionProvider.GetProgramVersion(), _versionProvider.GetDataVersion()); SetExitCode(ExitCodes.Success); } else { if (!Console.IsOutputRedirected) { CommandLineUtilities.DisplayBanner(_programAuthors); } if (_showHelpMenu) { Help.Show(_commandLineOps, _commandLineExample, _programDescription); CommandLineUtilities.ShowUnsupportedOptions(unsupportedOps); Console.WriteLine(); Console.WriteLine(_versionProvider.GetDataVersion()); Console.WriteLine(); // print the errors if any were found if (FoundParsingErrors()) { return; } } else { ValidateCommandLine(); // print the errors if any were found if (FoundParsingErrors()) { return; } ProgramExecution(); } } } catch (Exception e) { ExitCode = ExitCodeUtilities.ShowException(e); } _peakMemoryUsageBytes = MemoryUtilities.GetPeakMemoryUsage(); _wallTimeSpan = bench.GetElapsedTime(); if (!_showVersion && !_showHelpMenu && !Console.IsOutputRedirected) { Console.WriteLine(); if (_peakMemoryUsageBytes > 0) { Console.WriteLine("Peak memory usage: {0}", MemoryUtilities.ToHumanReadable(_peakMemoryUsageBytes)); } Console.WriteLine("Time: {0}", Benchmark.ToHumanReadable(_wallTimeSpan)); } }
public void Clear() { MemoryUtilities.MemClear(_ptr, (uint)(_capacity * MemoryUtilities.SizeOf <T>())); }