From d0de4776f333589a9bf178c02e0f416893cf2084 Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Fri, 18 Aug 2023 12:04:01 -0700 Subject: [PATCH] Large collection fixes --- BeefLibs/corlib/src/Collections/Dictionary.bf | 3 ++- BeefLibs/corlib/src/Collections/HashSet.bf | 2 +- BeefLibs/corlib/src/Collections/List.bf | 3 ++- BeefLibs/corlib/src/IO/FileStream.bf | 22 ++++++++++++++----- BeefLibs/corlib/src/String.bf | 5 +++-- 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/BeefLibs/corlib/src/Collections/Dictionary.bf b/BeefLibs/corlib/src/Collections/Dictionary.bf index e9646c99..e50a9df5 100644 --- a/BeefLibs/corlib/src/Collections/Dictionary.bf +++ b/BeefLibs/corlib/src/Collections/Dictionary.bf @@ -364,6 +364,7 @@ namespace System.Collections #unwarn if (sizeof(int_cosize) == 8) return (int_cosize)(hashCode & 0x7FFFFFFF'FFFFFFFFL); +#unwarn return ((int32)hashCode ^ (int32)((int64)hashCode >> 33)) & 0x7FFFFFFF; } @@ -629,7 +630,7 @@ namespace System.Collections return oldPtr; } - private bool RemoveEntry(int32 hashCode, int_cosize index) + private bool RemoveEntry(int_cosize hashCode, int_cosize index) { if (mBuckets != null) { diff --git a/BeefLibs/corlib/src/Collections/HashSet.bf b/BeefLibs/corlib/src/Collections/HashSet.bf index 8b374174..6412ce14 100644 --- a/BeefLibs/corlib/src/Collections/HashSet.bf +++ b/BeefLibs/corlib/src/Collections/HashSet.bf @@ -1220,7 +1220,7 @@ namespace System.Collections public struct Enumerator : IEnumerator { private HashSet mSet; - private int32 mIndex; + private int_cosize mIndex; #if VERSION_HASHSET private int32 mVersion; #endif diff --git a/BeefLibs/corlib/src/Collections/List.bf b/BeefLibs/corlib/src/Collections/List.bf index 1b04982f..5f6a020e 100644 --- a/BeefLibs/corlib/src/Collections/List.bf +++ b/BeefLibs/corlib/src/Collections/List.bf @@ -252,7 +252,7 @@ namespace System.Collections get { return ref mItems[index.Get(mSize)]; - } + } [Checked] set @@ -330,6 +330,7 @@ namespace System.Collections T* Realloc(int newSize, bool autoFree) { + Runtime.Assert(newSize <= int_cosize.MaxValue); T* oldAlloc = null; if (newSize > 0) { diff --git a/BeefLibs/corlib/src/IO/FileStream.bf b/BeefLibs/corlib/src/IO/FileStream.bf index 933b4ac9..eaa1d0a7 100644 --- a/BeefLibs/corlib/src/IO/FileStream.bf +++ b/BeefLibs/corlib/src/IO/FileStream.bf @@ -280,6 +280,11 @@ namespace System.IO class BufferedFileStream : BufferedStream, IFileStream { + public struct PositionRestorer : this(BufferedFileStream stream, int prevPosition), IDisposable + { + public void Dispose() => stream.Position = prevPosition; + } + protected Platform.BfpFile* mBfpFile; protected int64 mBfpFilePos; FileAccess mFileAccess; @@ -329,11 +334,6 @@ namespace System.IO Delete(); } - protected virtual void Delete() - { - Close(); - } - public this(Platform.BfpFile* handle, FileAccess access, int32 bufferSize, bool isAsync) { mBfpFile = handle; @@ -440,6 +440,18 @@ namespace System.IO return .Ok; } + public PositionRestorer PushPosition(int position) + { + PositionRestorer restorer = .(this, Position); + Position = position; + return restorer; + } + + protected virtual void Delete() + { + Close(); + } + public override Result Seek(int64 pos, SeekKind seekKind = .Absolute) { int64 newPos; diff --git a/BeefLibs/corlib/src/String.bf b/BeefLibs/corlib/src/String.bf index 0c9df151..5151be89 100644 --- a/BeefLibs/corlib/src/String.bf +++ b/BeefLibs/corlib/src/String.bf @@ -756,7 +756,7 @@ namespace System void Realloc(int newSize) { Debug.Assert(AllocSize > 0, "String has been frozen"); - Debug.Assert((uint)newSize <= cSizeFlags); + Runtime.Assert((uint)newSize <= cSizeFlags); char8* newPtr = new:this char8[newSize]* (?); Internal.MemCpy(newPtr, Ptr, mLength); #if VALGRIND @@ -776,8 +776,9 @@ namespace System void Realloc(char8* newPtr, int newSize) { + Runtime.Assert(newSize <= int_cosize.MaxValue); Debug.Assert(AllocSize > 0, "String has been frozen"); - Debug.Assert((uint)newSize <= cSizeFlags); + Runtime.Assert((uint)newSize <= cSizeFlags); Internal.MemCpy(newPtr, Ptr, mLength); if (IsDynAlloc) delete:this mPtrOrBuffer;