public static unsafe T CreateInstance <[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] T>() { // Grab the pointer to the default constructor of the type. If T doesn't have a default // constructor, the intrinsic returns a marker pointer that we check for. IntPtr defaultConstructor = DefaultConstructorOf <T>(); // Check if we got the marker back. // // TODO: might want to disambiguate the different cases for abstract class, interface, etc. if (defaultConstructor == (IntPtr)(delegate * < Guid >) & MissingConstructorMethod) { throw new MissingMethodException(SR.Format(SR.MissingConstructor_Name, typeof(T))); } T t; try { // Call the default constructor on the allocated instance. if (RuntimeHelpers.IsReference <T>()) { // Grab a pointer to the optimized allocator for the type and call it. IntPtr allocator = AllocatorOf <T>(); t = RawCalliHelper.Call <T>(allocator, EETypePtr.EETypePtrOf <T>().RawValue); RawCalliHelper.Call(defaultConstructor, t); // Debugger goo so that stepping in works. Only affects debug info generation. // The call gets optimized away. DebugAnnotations.PreviousCallContainsDebuggerStepInCode(); } else { t = default !;
public Status FreePool <T>(T[] arr) { #if DEBUG AllocateCount--; #endif return((Status)RawCalliHelper.StdCall(_FreePool, Unsafe.As <T[], IntPtr>(ref arr))); }
public unsafe Status GetInfo(ref Guid type, ref ulong bufSize, out FileInfo buf) { fixed(FileProtocol *_this = &this) fixed(Guid * _type = &type) fixed(ulong *_bufSize = &bufSize) fixed(FileInfo * _buf = &buf) return((Status)RawCalliHelper.StdCall(_GetInfo, _this, _type, _bufSize, _buf)); }
public unsafe Status GetMemoryMap(ref ulong memMapSize, IntPtr memMap, out ulong mapKey, out ulong descSize, out uint descVer) { fixed(ulong *_memMapSize = &memMapSize) fixed(ulong *_mapKey = &mapKey) fixed(ulong *_descSize = &descSize) fixed(uint *_descVer = &descVer) return((Status)RawCalliHelper.StdCall(_GetMemoryMap, _memMapSize, memMap, _mapKey, _descSize, _descVer)); }
public Status FreePool(IntPtr buf) { #if DEBUG AllocateCount--; #endif return((Status)RawCalliHelper.StdCall(_FreePool, buf)); }
// TODO: Figure out how to get rid of this pointer public unsafe Status AllocatePool(MemoryType type, ulong size, IntPtr *buf) { #if DEBUG AllocateCount++; #endif return((Status)RawCalliHelper.StdCall(_AllocatePool, type, size, buf)); }
private unsafe ref byte InvokeWithManyArguments( IntPtr methodToCall, ref byte thisArg, ref byte ret, object?[] parameters, BinderBundle binderBundle, bool wrapInTargetInvocationException) { int argCount = _argumentCount; // We don't check a max stack size since we are invoking a method which // naturally requires a stack size that is dependent on the arg count\size. IntPtr *pStorage = stackalloc IntPtr[2 * argCount]; NativeMemory.Clear(pStorage, (nuint)(2 * argCount) * (nuint)sizeof(IntPtr)); ByReference *pByRefStorage = (ByReference *)(pStorage + argCount); RuntimeImports.GCFrameRegistration regArgStorage = new(pStorage, (uint)argCount, areByRefs : false); RuntimeImports.GCFrameRegistration regByRefStorage = new(pByRefStorage, (uint)argCount, areByRefs : true); try { RuntimeImports.RhRegisterForGCReporting(®ArgStorage); RuntimeImports.RhRegisterForGCReporting(®ByRefStorage); CheckArguments(ref Unsafe.As <IntPtr, object>(ref *pStorage), pByRefStorage, parameters, binderBundle); try { ret = ref RawCalliHelper.Call(InvokeThunk, (void *)methodToCall, ref thisArg, ref ret, pByRefStorage); DebugAnnotations.PreviousCallContainsDebuggerStepInCode(); } catch (Exception e) when(wrapInTargetInvocationException) { throw new TargetInvocationException(e); } finally { if (_needsCopyBack) { CopyBack(ref Unsafe.As <IntPtr, object>(ref *pStorage), parameters); } } } finally { RuntimeImports.RhUnregisterForGCReporting(®ByRefStorage); RuntimeImports.RhUnregisterForGCReporting(®ArgStorage); } return(ref ret); }
private static unsafe void RunFunctionWithConservativelyReportedBufferInternal <T>(int cbBuffer, IntPtr pfnTargetToInvoke, ref T context, ref RuntimeImports.ConservativelyReportedRegionDesc regionDesc) { fixed(RuntimeImports.ConservativelyReportedRegionDesc *pRegionDesc = ®ionDesc) { int cbBufferAligned = (cbBuffer + (sizeof(IntPtr) - 1)) & ~(sizeof(IntPtr) - 1); // The conservative region must be IntPtr aligned, and a multiple of IntPtr in size void *region = stackalloc IntPtr[cbBufferAligned / sizeof(IntPtr)]; RuntimeImports.RhInitializeConservativeReportingRegion(pRegionDesc, region, cbBufferAligned); RawCalliHelper.Call <T>(pfnTargetToInvoke, region, ref context); RuntimeImports.RhDisableConservativeReportingRegion(pRegionDesc); } }
public static T CreateInstance <T>() { T t = default(T); bool missingDefaultConstructor = false; EETypePtr eetype = EETypePtr.EETypePtrOf <T>(); if (!RuntimeHelpers.IsReference <T>()) { // Early out for valuetypes since we don't support default constructors anyway. // This lets codegens that expand IsReference<T> optimize away the rest of this code. } else if (eetype.ComponentSize != 0) { // ComponentSize > 0 indicates an array-like type (e.g. string, array, etc). // Allocating this using the normal allocator would result in silent heap corruption. missingDefaultConstructor = true; } else if (eetype.IsInterface) { // Do not attempt to allocate interface types either missingDefaultConstructor = true; } else { bool oldValueOfMissingDefaultCtorMarkerBool = s_createInstanceMissingDefaultConstructor; try { t = (T)(RuntimeImports.RhNewObject(eetype)); // Run the default constructor. If the default constructor was missing, codegen // will expand DefaultConstructorOf to ClassWithMissingConstructor::.ctor // and we detect that later. IntPtr defaultConstructor = DefaultConstructorOf <T>(); RawCalliHelper.Call(defaultConstructor, t); DebugAnnotations.PreviousCallContainsDebuggerStepInCode(); } catch (Exception e) { throw new TargetInvocationException(e); } if (s_createInstanceMissingDefaultConstructor != oldValueOfMissingDefaultCtorMarkerBool) { missingDefaultConstructor = true; // We didn't call the real .ctor (because there wasn't one), but we still allocated // an uninitialized object. If it has a finalizer, it would run - prevent that. GC.SuppressFinalize(t); } } if (missingDefaultConstructor) { throw new MissingMethodException(SR.Format(SR.MissingConstructor_Name, typeof(T))); } return(t); }
// Change these 2 functions to use byte[] insteadd of NativeArray<byte>, and maybe string instead of ReadonlyNativeString public unsafe Status GetDriverName(NativeArray <byte> language, out ReadonlyNativeString name) { fixed(ComponentName2Protocol *_this = &this) fixed(ReadonlyNativeString * _name = &name) return((Status)RawCalliHelper.StdCall(_GetDriverName, _this, language, _name)); }
public unsafe Status QueryMode(uint modeNumber, out ulong sizeOfInfo, out ReadonlyNativeReference <GraphicsOutputModeInformation> info) { fixed(GraphicsOutputProtocol *_this = &this) fixed(ulong *_sizeOfInfo = &sizeOfInfo) fixed(ReadonlyNativeReference <GraphicsOutputModeInformation> *_info = &info) return((Status)RawCalliHelper.StdCall(_QueryMode, _this, modeNumber, _sizeOfInfo, _info)); }
public unsafe Status SetPosition(ulong pos) { fixed(FileProtocol *_this = &this) return((Status)RawCalliHelper.StdCall(_SetPosition, _this, pos)); }
public unsafe Status Open(out ReadonlyNativeReference <FileProtocol> newHandle, string filename, FileMode mode, FileAttribute attr) { fixed(FileProtocol *_this = &this) fixed(ReadonlyNativeReference <FileProtocol> *_newHandle = &newHandle) fixed(char *f = &filename._firstChar) return((Status)RawCalliHelper.StdCall(_Open, _this, _newHandle, f, mode, attr)); }
public unsafe Status HandleProtocol(Handle handle, ref Guid protocol, out IntPtr iface) { fixed(Guid *_protocol = &protocol) fixed(IntPtr * _iface = &iface) return((Status)RawCalliHelper.StdCall(_HandleProtocol, handle, _protocol, _iface)); }
// TODO: Get rid of the out Handle* and use an out Handle[] or out NativeArray<Handle> instead public unsafe Status LocateHandleBuffer(LocateSearchType searchType, ref Guid protocol, IntPtr searchKey, ref ulong numHandles, out NativeArray <Handle> buffer) { fixed(Guid *_protocol = &protocol) fixed(ulong *_numHandles = &numHandles) fixed(NativeArray <Handle> *_buffer = &buffer) return((Status)RawCalliHelper.StdCall(_LocateHandleBuffer, searchType, _protocol, searchKey, _numHandles, _buffer)); }
public unsafe Status SetCursorPosition(ulong column, ulong row) { fixed(SimpleTextOutputProtocol *_this = &this) return((Status)RawCalliHelper.StdCall(_SetCursorPosition, _this, column, row)); }
public unsafe Status EnableCursor(bool visible) { fixed(SimpleTextOutputProtocol *_this = &this) return((Status)RawCalliHelper.StdCall(_EnableCursor, _this, visible)); }
public unsafe Status CloseProtocol(Handle handle, ref Guid protocol, Handle agent, Handle controller) { fixed(Guid *pProt = &protocol) return((Status)RawCalliHelper.StdCall(_CloseProtocol, handle, pProt, agent, controller)); }
public unsafe Status Close() { fixed(FileProtocol *_this = &this) return((Status)RawCalliHelper.StdCall(_Close, _this)); }
public unsafe Status Reset(bool ExtendedVerification) { fixed(SimpleTextOutputProtocol *_this = &this) return((Status)RawCalliHelper.StdCall(_Reset, _this, &ExtendedVerification)); }
public unsafe Status Read(ref ulong bufSize, IntPtr buf) { fixed(FileProtocol *_this = &this) fixed(ulong *_bufSize = &bufSize) return((Status)RawCalliHelper.StdCall(_Read, _this, _bufSize, buf)); }
public unsafe Status TestString(ReadonlyNativeString str) { fixed(SimpleTextOutputProtocol *_this = &this) return((Status)RawCalliHelper.StdCall(_TestString, _this, str)); }
public unsafe Status OpenVolume(out ReadonlyNativeReference <FileProtocol> root) { fixed(SimpleFileSystemProtocol *_this = &this) fixed(ReadonlyNativeReference <FileProtocol> *_root = &root) return((Status)RawCalliHelper.StdCall(_OpenVolume, _this, _root)); }
public unsafe Status QueryMode(ulong mode, out ulong columns, out ulong rows) { fixed(SimpleTextOutputProtocol *_this = &this) fixed(ulong *_columns = &columns, _rows = &rows) return((Status)RawCalliHelper.StdCall(_QueryMode, _this, mode, _columns, _rows)); }
public unsafe Status SetMode(uint mode) { fixed(GraphicsOutputProtocol *_this = &this) return((Status)RawCalliHelper.StdCall(_SetMode, _this, mode)); }
public unsafe Status SetMode(ulong mode) { fixed(SimpleTextOutputProtocol *_this = &this) return((Status)RawCalliHelper.StdCall(_SetMode, _this, mode)); }
public unsafe Status GetControllerName(Handle controller, Handle child, NativeArray <byte> language, out ReadonlyNativeString name) { fixed(ComponentName2Protocol *_this = &this) fixed(ReadonlyNativeString * _name = &name) return((Status)RawCalliHelper.StdCall(_GetControllerName, _this, controller, child, language, _name)); }
public unsafe Status SetAttribute(ulong attribute) { fixed(SimpleTextOutputProtocol *_this = &this) return((Status)RawCalliHelper.StdCall(_SetAttribute, _this, attribute)); }
public void OutputString(void *handle, char *str) { RawCalliHelper.StdCall(_outputString, (byte *)handle, str); }
public unsafe Status ClearScreen() { fixed(SimpleTextOutputProtocol *_this = &this) return((Status)RawCalliHelper.StdCall(_ClearScreen, _this)); }