1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-26 03:28:02 +02:00

Added ZeroGap to AllowAppend

This commit is contained in:
Brian Fiete 2025-01-27 09:17:45 -08:00
parent ce42dc4fbe
commit 161d9dc540
19 changed files with 334 additions and 88 deletions

View file

@ -372,7 +372,7 @@ namespace System
[AttributeUsage(.Constructor)]
public struct AllowAppendAttribute : Attribute
{
public bool ZeroGap;
}
[AttributeUsage(.Class | .Struct)]

View file

@ -1,5 +1,6 @@
using System.Threading;
using System.Diagnostics;
using System.Collections;
namespace System
{
@ -105,6 +106,8 @@ namespace System
{
String emitStr = scope .();
HashSet<String> foundSigs = scope .();
for (var methodInfo in typeof(T).GetMethods(.Public | .DeclaredOnly))
{
if (methodInfo.IsStatic)
@ -112,11 +115,15 @@ namespace System
if (!methodInfo.IsConstructor)
continue;
var sig = methodInfo.GetMethodSig(.. new .());
if (!foundSigs.Add(sig))
continue;
emitStr.AppendF("public static RefCounted<T> Create(");
methodInfo.GetParamsDecl(emitStr);
emitStr.AppendF(")\n");
emitStr.AppendF("{{\n");
emitStr.AppendF("\treturn new [Friend] RefCountedAppend<T>(");
emitStr.AppendF("\treturn new [Friend]RefCountedAppend<T>(");
methodInfo.GetArgsList(emitStr);
emitStr.AppendF(");\n}}\n");
}
@ -207,7 +214,16 @@ namespace System
if (!methodInfo.IsConstructor)
continue;
emitStr.AppendF("[AllowAppend]\nprotected this(");
if (methodInfo.AllowAppendKind == .Yes)
emitStr.AppendF("[AllowAppend]\n");
if (methodInfo.AllowAppendKind == .ZeroGap)
emitStr.AppendF("[AllowAppend(ZeroGap=true)]\n");
if (methodInfo.CheckedKind == .Checked)
emitStr.AppendF("[Checked]\n");
if (methodInfo.CheckedKind == .Unchecked)
emitStr.AppendF("[Unchecked]\n");
emitStr.AppendF("protected this(");
methodInfo.GetParamsDecl(emitStr);
emitStr.AppendF(")\n");
emitStr.AppendF("{{\n");

View file

@ -530,11 +530,8 @@ namespace System.Reflection
}
}
public Result<T*> GetValueReference<T>(Object target)
Result<T*> DoGetValueReference<T>(Object target)
{
if (FieldType != typeof(T))
return .Err;
void* targetDataAddr;
if (target == null)
{
@ -556,6 +553,21 @@ namespace System.Reflection
}
}
public Result<T*> GetValueReference<T>(Object target)
{
if (FieldType != typeof(T))
return .Err;
return DoGetValueReference<T>(target);
}
public Result<T> GetValue<T>(Object target)
{
if ((FieldType != typeof(T)) && (!FieldType.IsSubtypeOf(typeof(T))))
return .Err;
T* result = Try!(DoGetValueReference<T>(target));
return *result;
}
public Result<Variant> GetValueReference(Object target)
{
void* targetDataAddr;

View file

@ -82,6 +82,13 @@ namespace System.Reflection
Name == "~this" :
mData.mMethodData.mName === "~this";
public AllowAppendKind AllowAppendKind => Compiler.IsComptime ?
Type.[Friend]Comptime_Method_GetInfo(mData.mComptimeMethodInstance).mMethodFlags.AllowAppendKind :
mData.mMethodData.[Friend]mFlags.AllowAppendKind;
public CheckedKind CheckedKind => Compiler.IsComptime ?
Type.[Friend]Comptime_Method_GetInfo(mData.mComptimeMethodInstance).mMethodFlags.CheckedKind :
mData.mMethodData.[Friend]mFlags.CheckedKind;
public Type ReturnType => Compiler.IsComptime ?
Type.[Friend]GetType((.)Type.[Friend]Comptime_Method_GetInfo(mData.mComptimeMethodInstance).mReturnTypeId) :
Type.[Friend]GetType(mData.mMethodData.mReturnType);

View file

@ -74,42 +74,86 @@ namespace System
const uint32 cStrPtrFlag = 0x40000000;
#endif
[AllowAppend]
[AllowAppend(ZeroGap=true)]
public this(int count) // 0
{
int bufferSize = (count == 0) ? 0 : (count - 1) & ~(sizeof(char8*) - 1);
#unwarn
char8* addlPtr = append char8[bufferSize]*(?);
Init(bufferSize);
mAllocSizeAndFlags = (uint_strsize)bufferSize + (int_strsize)sizeof(char8*);
mLength = 0;
}
[AllowAppend]
public this(int count) // 0
{
int bufferSize = count;
#unwarn
char8* addlPtr = append char8[bufferSize]*(?);
mPtrOrBuffer = addlPtr;
mAllocSizeAndFlags = (uint_strsize)bufferSize | cStrPtrFlag;
mLength = 0;
}
[AllowAppend(ZeroGap=true)]
public this()
{
let bufferSize = 16 - sizeof(char8*);
#unwarn
char8* addlPtr = append char8[bufferSize]*(?);
Init(bufferSize);
mAllocSizeAndFlags = (uint_strsize)bufferSize + (int_strsize)sizeof(char8*);
mLength = 0;
}
[AllowAppend]
public this()
{
let bufferSize = 8;
#unwarn
char8* addlPtr = append char8[bufferSize]*(?);
mPtrOrBuffer = addlPtr;
mAllocSizeAndFlags = (uint_strsize)bufferSize | cStrPtrFlag;
mLength = 0;
}
[AllowAppend(ZeroGap=true)]
public this(String str)
{
let count = str.mLength;
/*int a = __appendIdx;
int b = offsetof(String, mPtrOrBuffer);
if (__appendIdx == offsetof(String, mPtrOrBuffer))
{
}*/
int bufferSize = (count == 0) ? 0 : (count - 1) & ~(sizeof(char8*) - 1);
#unwarn
char8* addlPtr = append char8[bufferSize]*(?);
Init(bufferSize);
Internal.MemCpy(Ptr, str.Ptr, count);
mLength = count;
mAllocSizeAndFlags = (uint_strsize)bufferSize + (int_strsize)sizeof(char8*);
}
[AllowAppend]
public this(String str)
{
let count = str.mLength;
/*int a = __appendIdx;
int b = offsetof(String, mPtrOrBuffer);
if (__appendIdx == offsetof(String, mPtrOrBuffer))
{
}*/
int bufferSize = count;
#unwarn
char8* addlPtr = append char8[bufferSize]*(?);
mPtrOrBuffer = addlPtr;
Internal.MemCpy(Ptr, str.Ptr, count);
mLength = count;
mAllocSizeAndFlags = (uint_strsize)bufferSize | cStrPtrFlag;
}
[AllowAppend(ZeroGap=true)]
public this(String str, int offset)
{
Debug.Assert((uint)offset <= (uint)str.Length);
@ -126,7 +170,7 @@ namespace System
mLength = (int_strsize)count;
}
[AllowAppend]
[AllowAppend(ZeroGap=true)]
public this(String str, int offset, int count)
{
Debug.Assert((uint)offset <= (uint)str.Length);
@ -144,7 +188,7 @@ namespace System
mLength = (int_strsize)count;
}
[AllowAppend]
[AllowAppend(ZeroGap=true)]
public this(char8 c, int count)
{
int bufferSize = (count == 0) ? 0 : (count - 1) & ~(sizeof(char8*) - 1);
@ -158,7 +202,7 @@ namespace System
mLength = (int_strsize)count;
}
[AllowAppend]
[AllowAppend(ZeroGap=true)]
public this(char8* char8Ptr)
{
let count = Internal.CStrLen(char8Ptr);
@ -173,7 +217,7 @@ namespace System
mLength = count;
}
[AllowAppend]
[AllowAppend(ZeroGap=true)]
public this(char8* char8Ptr, int count)
{
int bufferSize = (count == 0) ? 0 : (count - 1) & ~(sizeof(char8*) - 1);
@ -187,7 +231,7 @@ namespace System
mLength = (int_strsize)count;
}
[AllowAppend]
[AllowAppend(ZeroGap=true)]
public this(char16* char16Ptr)
{
let count = UTF16.GetLengthAsUTF8(char16Ptr);
@ -200,7 +244,7 @@ namespace System
UTF16.Decode(char16Ptr, this);
}
[AllowAppend]
[AllowAppend(ZeroGap=true)]
public this(Span<char16> chars)
{
let count = UTF16.GetLengthAsUTF8(chars);
@ -213,14 +257,13 @@ namespace System
UTF16.Decode(chars, this);
}
[AllowAppend]
[AllowAppend(ZeroGap=true)]
public this(StringView strView)
{
let count = strView.Length;
int bufferSize = (count == 0) ? 0 : (count - 1) & ~(sizeof(char8*) - 1);
#unwarn
char8* addlPtr = append char8[bufferSize]*(?);
Init(bufferSize);
let ptr = Ptr;
Internal.MemCpy(ptr, strView.Ptr, strView.Length);
mAllocSizeAndFlags = (uint_strsize)bufferSize + (int_strsize)sizeof(char8*);
@ -228,6 +271,20 @@ namespace System
}
[AllowAppend]
public this(StringView strView)
{
let count = strView.Length;
int bufferSize = count;
#unwarn
char8* addlPtr = append char8[bufferSize]*(?);
mPtrOrBuffer = addlPtr;
let ptr = Ptr;
Internal.MemCpy(ptr, strView.Ptr, strView.Length);
mAllocSizeAndFlags = (uint_strsize)bufferSize | cStrPtrFlag;
mLength = (int_strsize)strView.Length;
}
[AllowAppend(ZeroGap=true)]
public this(StringView strView, CreateFlags flags)
{
let count = strView.Length + (flags.HasFlag(.NullTerminate) ? 1 : 0);
@ -243,7 +300,7 @@ namespace System
mLength = (int32)strView.Length;
}
[AllowAppend]
[AllowAppend(ZeroGap=true)]
public this(StringView strView, int offset)
{
Debug.Assert((uint)offset <= (uint)strView.Length);
@ -261,7 +318,7 @@ namespace System
mLength = (int_strsize)count;
}
[AllowAppend]
[AllowAppend(ZeroGap=true)]
public this(StringView strView, int offset, int count)
{
Debug.Assert((uint)offset <= (uint)strView.Length);
@ -279,7 +336,7 @@ namespace System
mLength = (int_strsize)count;
}
[AllowAppend]
[AllowAppend(ZeroGap=true)]
public this(char8[] chars, int offset, int count)
{
int bufferSize = (count == 0) ? 0 : (count - 1) & ~(sizeof(char8*) - 1);
@ -670,7 +727,7 @@ namespace System
public static implicit operator Span<char8>(String str)
{
if (str == null)
return .(null, 0);
return .((char8*)null, 0);
return .(str.Ptr, str.Length);
}

View file

@ -1742,9 +1742,9 @@ namespace System.Reflection
Appended = 0x1000,
}
public enum MethodFlags : uint16
public enum MethodFlags : uint32
{
MethodAccessMask = 0x0007,
case MethodAccessMask = 0x0007,
Protected = 0x0003,
Public = 0x0006,
@ -1762,5 +1762,26 @@ namespace System.Reflection
ThisCall = 0x3000, // Purposely resuing StdCall|FastCall
Mutating = 0x4000,
Constructor = 0x8000,
AppendBit0 = 0x10000,
AppendBit1 = 0x20000,
CheckedBit0 = 0x40000,
CheckedBit1 = 0x80000;
public AllowAppendKind AllowAppendKind => (.)(int32)(this / AppendBit0);
public CheckedKind CheckedKind => (.)(int32)(this / CheckedBit0);
}
public enum AllowAppendKind
{
No,
Yes,
ZeroGap
}
public enum CheckedKind
{
NotSet,
Checked,
Unchecked
}
}