Example #1
0
        internal IntPtr LockHandle(Handle h, byte moveHigh)
        {
#if DEBUG
            DebugUtils.Ping(DebugFlags.HandleSuite, string.Format("Handle: 0x{0}, moveHigh: {1}", h.ToHexString(), moveHigh));
#endif
            HandleEntry item;
            if (handles.TryGetValue(h, out item))
            {
                return(item.Pointer);
            }
            else
            {
                if (SafeNativeMethods.GlobalSize(h.Value).ToInt64() > 0L)
                {
                    IntPtr hPtr = Marshal.ReadIntPtr(h.Value);

                    if (IsValidReadPtr(hPtr) && SafeNativeMethods.GlobalSize(hPtr).ToInt64() > 0L)
                    {
                        return(SafeNativeMethods.GlobalLock(hPtr));
                    }

                    return(SafeNativeMethods.GlobalLock(h.Value));
                }
                if (IsValidReadPtr(h.Value) && IsValidWritePtr(h.Value)) // Pointer to a pointer?
                {
                    return(h.Value);
                }
                return(IntPtr.Zero);
            }
        }
Example #2
0
        internal int GetHandleSize(Handle h)
        {
#if DEBUG
            DebugUtils.Ping(DebugFlags.HandleSuite, string.Format("Handle: 0x{0}", h.ToHexString()));
#endif
            HandleEntry item;
            if (handles.TryGetValue(h, out item))
            {
                return(item.Size);
            }
            else
            {
                if (SafeNativeMethods.GlobalSize(h.Value).ToInt64() > 0L)
                {
                    IntPtr hPtr = Marshal.ReadIntPtr(h.Value);

                    if (IsValidReadPtr(hPtr))
                    {
                        return(SafeNativeMethods.GlobalSize(hPtr).ToInt32());
                    }
                    else
                    {
                        return(SafeNativeMethods.GlobalSize(h.Value).ToInt32());
                    }
                }
                return(0);
            }
        }
Example #3
0
        private unsafe void DisposeHandleImpl(Handle handle)
        {
            if (handle != Handle.Null && IsValidReadPtr(handle.Value))
            {
                HandleEntry item;
                if (handles.TryGetValue(handle, out item))
                {
                    item.Dispose();
                    handles.Remove(handle);
                    OnSuiteHandleDisposed(handle);
                }
                else
                {
                    if (SafeNativeMethods.GlobalSize(handle.Value).ToInt64() > 0L)
                    {
                        IntPtr hPtr = Marshal.ReadIntPtr(handle.Value);

                        if (IsValidReadPtr(hPtr) && SafeNativeMethods.GlobalSize(hPtr).ToInt64() > 0L)
                        {
                            SafeNativeMethods.GlobalFree(hPtr);
                        }

                        SafeNativeMethods.GlobalFree(handle.Value);
                    }
                }
            }
        }
Example #4
0
        private unsafe short SetHandleSize(Handle h, int newSize)
        {
#if DEBUG
            DebugUtils.Ping(DebugFlags.HandleSuite, string.Format("Handle: 0x{0}", h.ToHexString()));
#endif
            if (newSize < 0)
            {
                return(PSError.paramErr);
            }

            if (AllocatedBySuite(h))
            {
                try
                {
                    PSHandle *handle = (PSHandle *)h.Value;
                    IntPtr    ptr    = Memory.ReAlloc(handle->pointer, newSize);

                    handle->pointer = ptr;

                    handles.AddOrUpdate(h, new HandleEntry(h, ptr, newSize));
                }
                catch (OutOfMemoryException)
                {
                    return(PSError.memFullErr);
                }
            }
            else
            {
                if (SafeNativeMethods.GlobalSize(h.Value).ToInt64() > 0L)
                {
                    IntPtr hPtr = Marshal.ReadIntPtr(h.Value);

                    if (IsValidReadPtr(hPtr) && SafeNativeMethods.GlobalSize(hPtr).ToInt64() > 0L)
                    {
                        IntPtr hMem = SafeNativeMethods.GlobalReAlloc(hPtr, new UIntPtr((uint)newSize), NativeConstants.GPTR);
                        if (hMem == IntPtr.Zero)
                        {
                            return(PSError.memFullErr);
                        }
                        Marshal.WriteIntPtr(h.Value, hMem);
                    }
                    else
                    {
                        if (SafeNativeMethods.GlobalReAlloc(h.Value, new UIntPtr((uint)newSize), NativeConstants.GPTR) == IntPtr.Zero)
                        {
                            return(PSError.memFullErr);
                        }
                    }
                }
                else
                {
                    return(PSError.nilHandleErr);
                }
            }

            return(PSError.noErr);
        }
Example #5
0
        internal void UnlockHandle(Handle h)
        {
#if DEBUG
            DebugUtils.Ping(DebugFlags.HandleSuite, string.Format("Handle: 0x{0}", h.ToHexString()));
#endif
            if (!AllocatedBySuite(h))
            {
                if (SafeNativeMethods.GlobalSize(h.Value).ToInt64() > 0L)
                {
                    IntPtr hPtr = Marshal.ReadIntPtr(h.Value);

                    if (IsValidReadPtr(hPtr) && SafeNativeMethods.GlobalSize(hPtr).ToInt64() > 0L)
                    {
                        SafeNativeMethods.GlobalUnlock(hPtr);
                    }
                    else
                    {
                        SafeNativeMethods.GlobalUnlock(h.Value);
                    }
                }
            }
        }