Beispiel #1
0
        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 !;
Beispiel #2
0
        public Status FreePool <T>(T[] arr)
        {
#if DEBUG
            AllocateCount--;
#endif
            return((Status)RawCalliHelper.StdCall(_FreePool, Unsafe.As <T[], IntPtr>(ref arr)));
        }
Beispiel #3
0
 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));
 }
Beispiel #4
0
 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));
 }
Beispiel #5
0
        public Status FreePool(IntPtr buf)
        {
#if DEBUG
            AllocateCount--;
#endif
            return((Status)RawCalliHelper.StdCall(_FreePool, buf));
        }
Beispiel #6
0
        // 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));
        }
Beispiel #7
0
        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(&regArgStorage);
                RuntimeImports.RhRegisterForGCReporting(&regByRefStorage);

                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(&regByRefStorage);
                RuntimeImports.RhUnregisterForGCReporting(&regArgStorage);
            }

            return(ref ret);
        }
Beispiel #8
0
        private static unsafe void RunFunctionWithConservativelyReportedBufferInternal <T>(int cbBuffer, IntPtr pfnTargetToInvoke, ref T context, ref RuntimeImports.ConservativelyReportedRegionDesc regionDesc)
        {
            fixed(RuntimeImports.ConservativelyReportedRegionDesc *pRegionDesc = &regionDesc)
            {
                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);
            }
        }
Beispiel #9
0
        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);
        }
Beispiel #10
0
 // 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));
 }
Beispiel #11
0
 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));
 }
Beispiel #12
0
 public unsafe Status SetPosition(ulong pos) {
     fixed(FileProtocol *_this = &this)
     return((Status)RawCalliHelper.StdCall(_SetPosition, _this, pos));
 }
Beispiel #13
0
 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));
 }
Beispiel #14
0
 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));
 }
Beispiel #15
0
 // 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));
 }
Beispiel #16
0
 public unsafe Status SetCursorPosition(ulong column, ulong row) {
     fixed(SimpleTextOutputProtocol *_this = &this)
     return((Status)RawCalliHelper.StdCall(_SetCursorPosition, _this, column, row));
 }
Beispiel #17
0
 public unsafe Status EnableCursor(bool visible) {
     fixed(SimpleTextOutputProtocol *_this = &this)
     return((Status)RawCalliHelper.StdCall(_EnableCursor, _this, visible));
 }
Beispiel #18
0
 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));
 }
Beispiel #19
0
 public unsafe Status Close() {
     fixed(FileProtocol *_this = &this)
     return((Status)RawCalliHelper.StdCall(_Close, _this));
 }
Beispiel #20
0
 public unsafe Status Reset(bool ExtendedVerification) {
     fixed(SimpleTextOutputProtocol *_this = &this)
     return((Status)RawCalliHelper.StdCall(_Reset, _this, &ExtendedVerification));
 }
Beispiel #21
0
 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));
 }
Beispiel #22
0
 public unsafe Status TestString(ReadonlyNativeString str) {
     fixed(SimpleTextOutputProtocol *_this = &this)
     return((Status)RawCalliHelper.StdCall(_TestString, _this, str));
 }
Beispiel #23
0
 public unsafe Status OpenVolume(out ReadonlyNativeReference <FileProtocol> root) {
     fixed(SimpleFileSystemProtocol *_this = &this)
     fixed(ReadonlyNativeReference <FileProtocol> *_root = &root)
     return((Status)RawCalliHelper.StdCall(_OpenVolume, _this, _root));
 }
Beispiel #24
0
 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));
 }
Beispiel #25
0
 public unsafe Status SetMode(uint mode) {
     fixed(GraphicsOutputProtocol *_this = &this)
     return((Status)RawCalliHelper.StdCall(_SetMode, _this, mode));
 }
Beispiel #26
0
 public unsafe Status SetMode(ulong mode) {
     fixed(SimpleTextOutputProtocol *_this = &this)
     return((Status)RawCalliHelper.StdCall(_SetMode, _this, mode));
 }
Beispiel #27
0
 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));
 }
Beispiel #28
0
 public unsafe Status SetAttribute(ulong attribute) {
     fixed(SimpleTextOutputProtocol *_this = &this)
     return((Status)RawCalliHelper.StdCall(_SetAttribute, _this, attribute));
 }
Beispiel #29
0
 public void OutputString(void *handle, char *str)
 {
     RawCalliHelper.StdCall(_outputString, (byte *)handle, str);
 }
Beispiel #30
0
 public unsafe Status ClearScreen() {
     fixed(SimpleTextOutputProtocol *_this = &this)
     return((Status)RawCalliHelper.StdCall(_ClearScreen, _this));
 }