Free() public static méthode

public static Free ( void block ) : void
block void
Résultat void
Exemple #1
0
        protected virtual void Dispose(bool disposing)
        {
#if DEBUG
            if (_id == _badDispose)
            {
                Debugger.Break();
            }
#endif
            if (!_disposed)
            {
                _disposed = true;
                Memory.Free();
            }
        }
Exemple #2
0
        public static void Free(UnsafeHashSet *set)
        {
            if (set == null)
            {
                return;
            }

            if (set->_collection.Entries.Dynamic == 1)
            {
                UnsafeHashCollection.Free(&set->_collection);
            }

            *set = default;

            Memory.Free(set);
        }
Exemple #3
0
            private void Dispose(bool disposing)
            {
                if (!disposed)
                {
                    if (disposing)
                    {
                    }

                    if (pointer != IntPtr.Zero)
                    {
                        Memory.Free(pointer);
                        pointer = IntPtr.Zero;
                    }

                    disposed = true;
                }
            }
Exemple #4
0
        public void ResetWriteCopyBufferNull()
        {
            BitStreamer bs = new BitStreamer();

            IntPtr buff = Memory.Alloc(16);

            *(byte *)buff = 212;

            bs.ResetWrite(buff, 16, true);
            Assert.AreEqual(16, bs.ByteLength);
            Assert.AreEqual(16, bs.ByteOffset);

            bs.ResetRead();
            Assert.AreEqual(212, bs.ReadByte());
            Assert.IsTrue(bs.OwnsBuffer);

            Memory.Free(buff);
        }
        public static void Free(UnsafeSortedDictionary *map)
        {
            if (map == null)
            {
                return;
            }

            if (map->_collection.Entries.Dynamic == 1)
            {
                UnsafeBuffer.Free(&map->_collection.Entries);
            }

            // clear memory
            *map = default;

            // free it
            Memory.Free(map);
        }
        public static void Free(UnsafeList *list)
        {
            if (list == null)
            {
                return;
            }

            // free dynamic items separately
            if (list->_items.Dynamic == 1)
            {
                UnsafeBuffer.Free(&list->_items);
            }

            // clear memory
            *list = default;

            // free list
            Memory.Free(list);
        }
        public static void Free(UnsafeStack *stack)
        {
            if (stack == null)
            {
                return;
            }

            // if this is a dynamic sized stack, we need to free the buffer by hand
            if (stack->_items.Dynamic == 1)
            {
                UnsafeBuffer.Free(&stack->_items);
            }

            // clear stack memory just in case
            *stack = default;

            // free stack memory (if this is a fixed size stack, it frees the _items memory also)
            Memory.Free(stack);
        }
Exemple #8
0
        public void ReallocZeroedTest()
        {
            var arr = CreateArray();

            arr = (byte *)Memory.ReallocZeroed((IntPtr)arr, 8, 16);

            // Check the original bytes.
            for (int i = 0; i < 8; i++)
            {
                Assert.AreEqual(i, arr[i]);
            }

            // Verify that the remainder is zeroed.
            for (int i = 8; i < 16; i++)
            {
                Assert.AreEqual(0, arr[i]);
            }

            Memory.Free(arr);
        }
Exemple #9
0
        public static void Free(UnsafeMPMCQueue *queue)
        {
            if (queue == null)
            {
                return;
            }

            // Free all segments
            var segment = queue->_head;

            do
            {
                var next = segment->_nextSegment;
                QueueSegment.Free(segment);
                segment = next;
            }while (segment != null);

            // Clear queue memory (just in case)
            *queue = default;

            // Free queue memory, if this is a fixed queue it frees the items memory at the same time
            Memory.Free(queue);
        }
Exemple #10
0
        private unsafe IntPtr PSBufferNew(uint *requestedSize, uint minimumSize)
        {
            IntPtr ptr = IntPtr.Zero;

            try
            {
                if (requestedSize != null && *requestedSize > minimumSize)
                {
                    uint allocatedSize = 0;
                    uint size          = *requestedSize;
                    while (size > minimumSize)
                    {
                        // Allocate the largest buffer we can that is greater than the specified minimum size.
                        ptr = Memory.Allocate(size, MemoryAllocationFlags.ReturnZeroOnOutOfMemory);
                        if (ptr != IntPtr.Zero)
                        {
                            buffers.Add(ptr, new BufferEntry(ptr, size));
                            allocatedSize = size;
                            break;
                        }

                        size /= 2;
                    }

                    if (ptr == IntPtr.Zero)
                    {
                        // If we cannot allocate a buffer larger than the minimum size
                        // attempt to allocate a buffer at the minimum size.

                        ptr = Memory.Allocate(minimumSize, MemoryAllocationFlags.ReturnZeroOnOutOfMemory);
                        if (ptr != IntPtr.Zero)
                        {
                            buffers.Add(ptr, new BufferEntry(ptr, minimumSize));
                            allocatedSize = minimumSize;
                        }
                    }

                    // The requested size pointer is used as an output parameter to return the actual number of bytes allocated.
                    *requestedSize = allocatedSize;
                }
                else
                {
                    ptr = Memory.Allocate(minimumSize, MemoryAllocationFlags.ReturnZeroOnOutOfMemory);
                    if (ptr != IntPtr.Zero)
                    {
                        buffers.Add(ptr, new BufferEntry(ptr, minimumSize));
                    }
                }
            }
            catch (OutOfMemoryException)
            {
                // Free the buffer memory if the framework throws an OutOfMemoryException when adding to the buffers list.
                if (ptr != IntPtr.Zero)
                {
                    Memory.Free(ptr);
                    ptr = IntPtr.Zero;
                }
            }

            return(ptr);
        }
Exemple #11
0
        public void PluginMemoryHandlerTest()
        {
            // Arrange
            // ensure delegates are not garbage collected from managed code
            var malloc              = new MemoryMalloc(Malloc);
            var free                = new MemoryFree(Free);
            var realloc             = new MemoryRealloc(Realloc);
            var nonContextualMalloc = new MemoryNonContextualMalloc(NonContextualMalloc);
            var nonContextualFree   = new MemoryNonContextualFree(NonContextualFree);

            PluginMemoryHandler memoryHandler = new PluginMemoryHandler
            {
                Base = new PluginBase
                {
                    Magic           = Cms.PluginMagicNumber,
                    ExpectedVersion = (uint)Cms.EncodedCMMVersion,  // >= 2.8
                    Type            = PluginType.MemoryHandler,
                    Next            = IntPtr.Zero
                },
                Malloc              = Marshal.GetFunctionPointerForDelegate(malloc),
                Free                = Marshal.GetFunctionPointerForDelegate(free),
                Realloc             = Marshal.GetFunctionPointerForDelegate(realloc),
                MallocZero          = IntPtr.Zero, // optional
                Calloc              = IntPtr.Zero, // optional
                Duplicate           = IntPtr.Zero, // optional
                NonContextualMalloc = Marshal.GetFunctionPointerForDelegate(nonContextualMalloc),
                NonContextualFree   = Marshal.GetFunctionPointerForDelegate(nonContextualFree)
            };

            int    rawsize             = Marshal.SizeOf(memoryHandler);
            IntPtr memoryHandlerPlugin = Marshal.AllocHGlobal(rawsize);

            Marshal.StructureToPtr(memoryHandler, memoryHandlerPlugin, false);

            // Act
            try
            {
                using (var context = Context.Create(memoryHandlerPlugin, IntPtr.Zero))
                {
                    // Assert
                    IntPtr mallocPtr = Memory.Malloc(context, 0x200);
                    Assert.AreNotEqual(IntPtr.Zero, mallocPtr);

                    IntPtr reallocPtr = Memory.Realloc(context, mallocPtr, 0x300);
                    Assert.AreNotEqual(IntPtr.Zero, mallocPtr);
                    Memory.Free(context, reallocPtr);
                }
            }
            finally
            {
                Marshal.DestroyStructure(memoryHandlerPlugin, typeof(PluginMemoryHandler));
                Marshal.FreeHGlobal(memoryHandlerPlugin);
            }

            // allocates memory
            IntPtr Malloc(IntPtr contextID, uint size)
            {
                TestContext.WriteLine($"Malloc(contextId: {contextID}, size: {size})");
                try
                {
                    return(Marshal.AllocHGlobal((int)size));
                }
                catch (Exception)
                {
                    return(IntPtr.Zero);
                }
            }

            // frees memory
            void Free(IntPtr contextID, IntPtr ptr)
            {
                TestContext.WriteLine($"Free(contextId: {contextID}, ptr: 0x{ptr:X})");
                Marshal.FreeHGlobal(ptr);
            }

            // reallocates memory
            IntPtr Realloc(IntPtr contextID, IntPtr ptr, uint newSize)
            {
                TestContext.WriteLine($"Realloc(contextId: {contextID}, ptr: 0x{ptr:X}, newSize: {newSize})");
                try
                {
                    return(Marshal.ReAllocHGlobal(ptr, (IntPtr)newSize));
                }
                catch (Exception)
                {
                    return(IntPtr.Zero);
                }
            }

            // allocates non-contextual memory
            IntPtr NonContextualMalloc(IntPtr userData, uint size)
            {
                TestContext.WriteLine($"NonContextualMalloc(userData: {userData}, size: {size})");
                try
                {
                    return(Marshal.AllocHGlobal((int)size));
                }
                catch (Exception)
                {
                    return(IntPtr.Zero);
                }
            }

            // frees non-contextual memory
            void NonContextualFree(IntPtr userData, IntPtr ptr)
            {
                TestContext.WriteLine($"NonContextualFree(userData: {userData}, ptr: 0x{ptr:X})");
                Marshal.FreeHGlobal(ptr);
            }
        }
Exemple #12
0
        public void PluginTagTypeTest()
        {
            // Arrange
            const TagSignature     SigInt     = (TagSignature)0x74747448;     // 'tttH'
            const TagTypeSignature SigIntType = (TagTypeSignature)0x74747448; // 'tttH'

            PluginTag tag = new PluginTag
            {
                Base = new PluginBase
                {
                    Magic           = Cms.PluginMagicNumber,
                    ExpectedVersion = (uint)Cms.EncodedCMMVersion,    // >= 2.8
                    Type            = PluginType.Tag,
                    Next            = IntPtr.Zero
                },
                Signature  = SigInt,
                Descriptor = new TagDescriptor
                {
                    ElemCount       = 1,
                    nSupportedTypes = 1,
                    SupportedTypes  = new TagTypeSignature[TagDescriptor.MAX_TYPES_IN_LCMS_PLUGIN],
                    Decider         = IntPtr.Zero
                }
            };

            tag.Descriptor.SupportedTypes[0] = SigIntType;

            int    rawsize   = Marshal.SizeOf(tag);
            IntPtr tagPlugin = Marshal.AllocHGlobal(rawsize);

            Marshal.StructureToPtr(tag, tagPlugin, false);

            // ensure delegates are not garbage collected from managed code
            var read      = new TagTypeRead(Read);
            var write     = new TagTypeWrite(Write);
            var duplicate = new TagTypeDuplicate(Duplicate);
            var free      = new TagTypeFree(Free);

            PluginTagType tagType = new PluginTagType
            {
                Base = new PluginBase
                {
                    Magic           = Cms.PluginMagicNumber,
                    ExpectedVersion = (uint)Cms.EncodedCMMVersion,    // >= 2.8
                    Type            = PluginType.TagType,
                    Next            = tagPlugin
                },
                Handler = new TagTypeHandler
                {
                    Signature = SigIntType,
                    Read      = Marshal.GetFunctionPointerForDelegate(read),
                    Write     = Marshal.GetFunctionPointerForDelegate(write),
                    Duplicate = Marshal.GetFunctionPointerForDelegate(duplicate),
                    Free      = Marshal.GetFunctionPointerForDelegate(free)
                }
            };

            rawsize = Marshal.SizeOf(tagType);
            IntPtr tagTypePlugin = Marshal.AllocHGlobal(rawsize);

            Marshal.StructureToPtr(tagType, tagTypePlugin, false);

            // Act
            try
            {
                using (var context = Context.Create(tagTypePlugin, IntPtr.Zero))
                    using (var profile = Profile.CreatePlaceholder(context))
                    {
                        var errorHandler = new ErrorHandler(HandleError);
                        context.SetErrorHandler(errorHandler);

                        uint expected = 1234;
                        bool written  = profile.WriteTag(SigInt, expected);
                        Assert.IsTrue(written);

                        profile.Save(null, out uint bytesNeeded);
                        Assert.AreNotEqual(0, bytesNeeded);
                        byte[] profileMemory = new byte[bytesNeeded];

                        bool saved = profile.Save(profileMemory, out uint bytesWritten);
                        Assert.IsTrue(saved);
                        // close original profile to flush caches
                        profile.Close();

                        // re-open profile from memory
                        using (var profile2 = Profile.Open(context, profileMemory))
                        {
                            IntPtr data = profile2.ReadTag(SigInt);
                            uint[] u    = new uint[1];
                            Marshal.Copy(data, (int[])(object)u, 0, 1);
                            uint actual = u[0];

                            // Assert
                            Assert.AreEqual(expected, actual);
                        }
                    }
            }
            finally
            {
                Marshal.DestroyStructure(tagPlugin, typeof(PluginTag));
                Marshal.FreeHGlobal(tagPlugin);

                Marshal.DestroyStructure(tagTypePlugin, typeof(PluginTagType));
                Marshal.FreeHGlobal(tagTypePlugin);
            }

            // allocates unmanaged memory for a single 'uint' and reads from i/o handler into it
            IntPtr Read(in TagTypeHandler self, IntPtr io, out uint nItems, uint tagSize)
            {
                TestContext.WriteLine($"Read(self: {self}, io: 0x{io:X}, out nItems, tagSize: {tagSize})");

                using (var context = Context.FromHandle(self.ContextID))
                    using (var iohandler = IOHandler.FromHandle(io))
                    {
                        nItems = 1;
                        IntPtr ptr = Memory.Malloc(context, sizeof(uint));
                        if (ptr == IntPtr.Zero)
                        {
                            return(IntPtr.Zero);
                        }

                        // unsafe, but faster...
                        unsafe
                        {
                            if (!iohandler.Read(ref *(uint *)ptr))
                            {
                                return(IntPtr.Zero);
                            }
                        }

                        // - or -
                        // verifiable, but slower...

                        //uint[] arr = new uint[1];
                        //if (!iohandler.Read(ref arr[0])) return IntPtr.Zero;
                        //Marshal.Copy((int[])(object)arr, 0, ptr, 1);

                        return(ptr);
                    }
            }

            // uses the i/o handler to write a single 'uint' read from unmanaged memory 'ptr'
            int Write(in TagTypeHandler self, IntPtr io, IntPtr ptr, uint nItems)
            {
                TestContext.WriteLine($"Write(self: {self}, io: 0x{io:X}, ptr: 0x{ptr:X}, nItems: {nItems})");

                using (var iohandler = IOHandler.FromHandle(io))
                {
                    // unsafe, but faster...
                    unsafe
                    {
                        return(iohandler.Write(*(uint *)ptr) ? 1 : 0);
                    }

                    // - or -
                    // verifiable, but slower...

                    //uint[] arr = new uint[1];
                    //Marshal.Copy(ptr, (int[])(object)arr, 0, 1);
                    //return iohandler.Write(arr[0]) ? 1 : 0;
                }
            }

            // duplicates the unmanaged memory 'ptr' into a new block of size 'n x sizeof(uint)'
            IntPtr Duplicate(in TagTypeHandler self, IntPtr ptr, uint n)
            {
                TestContext.WriteLine($"Duplicate(self: {self}, ptr: 0x{ptr:X}, n: {n})");

                using (var context = Context.FromHandle(self.ContextID))
                {
                    return(Memory.Duplicate(context, ptr, n * sizeof(uint)));
                }
            }

            // frees the unmanaged memory 'ptr'
            void Free(in TagTypeHandler self, IntPtr ptr)
            {
                TestContext.WriteLine($"Free(self: {self}, ptr: 0x{ptr:X})");

                using (var context = Context.FromHandle(self.ContextID))
                {
                    Memory.Free(context, ptr);
                }
            }

            void HandleError(IntPtr contextID, int errorCode, string errorText)
            {
                TestContext.WriteLine($"Error!!! contextID: {contextID}, errorCode: {errorCode}, errorText: '{errorText}'");
            }
        }