Ejemplo n.º 1
0
        private unsafe short SetHandleSize(IntPtr 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.ToPointer();
                    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).ToInt64() > 0L)
                {
                    IntPtr hPtr = Marshal.ReadIntPtr(h);

                    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, hMem);
                    }
                    else
                    {
                        if (SafeNativeMethods.GlobalReAlloc(h, new UIntPtr((uint)newSize), NativeConstants.GPTR) == IntPtr.Zero)
                        {
                            return(PSError.memFullErr);
                        }
                    }
                }
                else
                {
                    return(PSError.nilHandleErr);
                }
            }

            return(PSError.noErr);
        }
Ejemplo n.º 2
0
        internal unsafe IntPtr NewHandle(int size)
        {
            if (size < 0)
            {
                return(IntPtr.Zero);
            }

            IntPtr handle = IntPtr.Zero;

            try
            {
                // The Photoshop API 'Handle' is an indirect pointer.
                // As some plug-ins may dereference the pointer instead of calling HandleLockProc we recreate that implementation.
                handle = Memory.Allocate(PSHandle.SizeOf, true);

                PSHandle *hand = (PSHandle *)handle.ToPointer();

                hand->pointer = Memory.Allocate(size, true);

                handles.Add(handle, new HandleEntry(handle, hand->pointer, size));
#if DEBUG
                string message = string.Format("Handle: 0x{0}, pointer: 0x{1}, size: {2}", handle.ToHexString(), hand->pointer.ToHexString(), size);
                DebugUtils.Ping(DebugFlags.HandleSuite, message);
#endif
            }
            catch (OutOfMemoryException)
            {
                if (handle != IntPtr.Zero)
                {
                    // Free the handle pointer memory if it has been allocated.
                    // This would occur if the framework throws an OutOfMemoryException when adding to the handles dictionary.
                    PSHandle *hand = (PSHandle *)handle.ToPointer();
                    if (hand->pointer != IntPtr.Zero)
                    {
                        Memory.Free(hand->pointer);
                        hand->pointer = IntPtr.Zero;
                    }

                    Memory.Free(handle);
                    handle = IntPtr.Zero;
                }

                return(IntPtr.Zero);
            }

            return(handle);
        }