Example #1
0
        IC_PyBaseObject_Dealloc(IntPtr objPtr)
        {
            IntPtr       objType = CPyMarshal.ReadPtrField(objPtr, typeof(PyObject), "ob_type");
            dgt_void_ptr freeDgt = (dgt_void_ptr)
                                   CPyMarshal.ReadFunctionPtrField(
                objType, typeof(PyTypeObject), "tp_free", typeof(dgt_void_ptr));

            freeDgt(objPtr);
        }
        IC_PySlice_Dealloc(IntPtr slicePtr)
        {
            this.DecRef(CPyMarshal.ReadPtrField(slicePtr, typeof(PySliceObject), "start"));
            this.DecRef(CPyMarshal.ReadPtrField(slicePtr, typeof(PySliceObject), "stop"));
            this.DecRef(CPyMarshal.ReadPtrField(slicePtr, typeof(PySliceObject), "step"));

            dgt_void_ptr freeDgt = (dgt_void_ptr)
                                   CPyMarshal.ReadFunctionPtrField(
                this.PySlice_Type, typeof(PyTypeObject), "tp_free", typeof(dgt_void_ptr));

            freeDgt(slicePtr);
        }
Example #3
0
        IC_PyMethod_Dealloc(IntPtr objPtr)
        {
            this.DecRef(CPyMarshal.ReadPtrField(objPtr, typeof(PyMethodObject), "im_func"));
            this.DecRef(CPyMarshal.ReadPtrField(objPtr, typeof(PyMethodObject), "im_self"));
            this.DecRef(CPyMarshal.ReadPtrField(objPtr, typeof(PyMethodObject), "im_class"));

            IntPtr       objType = CPyMarshal.ReadPtrField(objPtr, typeof(PyObject), "ob_type");
            dgt_void_ptr freeDgt = (dgt_void_ptr)
                                   CPyMarshal.ReadFunctionPtrField(
                objType, typeof(PyTypeObject), "tp_free", typeof(dgt_void_ptr));

            freeDgt(objPtr);
        }
Example #4
0
        IC_file_dealloc(IntPtr ptr)
        {
            if (this.FILEs.ContainsKey(ptr))
            {
                Unmanaged.fclose(this.FILEs[ptr]);
                this.FILEs.Remove(ptr);
            }
            IntPtr       _type   = CPyMarshal.ReadPtrField(ptr, typeof(PyObject), "ob_type");
            dgt_void_ptr freeDgt = (dgt_void_ptr)
                                   CPyMarshal.ReadFunctionPtrField(
                _type, typeof(PyTypeObject), "tp_free", typeof(dgt_void_ptr));

            freeDgt(ptr);
        }
Example #5
0
        IC_PyInstance_Dealloc(IntPtr objPtr)
        {
            IntPtr dictPtr = CPyMarshal.ReadPtrField(objPtr, typeof(PyInstanceObject), "in_dict");

            if (dictPtr != IntPtr.Zero)
            {
                this.DecRef(dictPtr);
            }

            IntPtr       objType = CPyMarshal.ReadPtrField(objPtr, typeof(PyObject), "ob_type");
            dgt_void_ptr freeDgt = (dgt_void_ptr)
                                   CPyMarshal.ReadFunctionPtrField(
                objType, typeof(PyTypeObject), "tp_free", typeof(dgt_void_ptr));

            freeDgt(objPtr);
        }
Example #6
0
        DecRef(IntPtr ptr)
        {
            this.AttemptToMap(ptr);

            if (!this.HasPtr(ptr))
            {
                throw new KeyNotFoundException(String.Format(
                                                   "DecRef: missing key in pointer map: {0}", ptr.ToString("x")));
            }

            int count = CPyMarshal.ReadIntField(ptr, typeof(PyObject), "ob_refcnt");

            if (count == 0)
            {
                throw new BadRefCountException("Trying to DecRef an object with ref count 0");
            }
            else if (count == 1)
            {
                IntPtr typePtr = CPyMarshal.ReadPtrField(ptr, typeof(PyObject), "ob_type");
                if (typePtr == IntPtr.Zero)
                {
                    throw new CannotInterpretException(String.Format(
                                                           "Cannot destroy object at {0}: null type", ptr.ToString("x")));
                }

                if (CPyMarshal.ReadPtrField(typePtr, typeof(PyTypeObject), "tp_dealloc") == IntPtr.Zero)
                {
                    throw new CannotInterpretException(String.Format(
                                                           "Cannot destroy object at {0} with type at {1}: no dealloc function", ptr.ToString("x"), typePtr.ToString("x")));
                }

                dgt_void_ptr deallocDgt = (dgt_void_ptr)CPyMarshal.ReadFunctionPtrField(
                    typePtr, typeof(PyTypeObject), "tp_dealloc", typeof(dgt_void_ptr));
                deallocDgt(ptr);
            }
            else
            {
                CPyMarshal.WriteIntField(ptr, typeof(PyObject), "ob_refcnt", count - 1);
                if (this.map.HasPtr(ptr))
                {
                    this.map.UpdateStrength(ptr);
                }
            }
        }
Example #7
0
        DumpPtr(IntPtr ptr)
        {
            if (!this.allocator.Contains(ptr))
            {
                // we don't own this memory; not our problem.
                return;
            }

            try
            {
                IntPtr typePtr = CPyMarshal.ReadPtrField(ptr, typeof(PyObject), "ob_type");
                if (typePtr == IntPtr.Zero)
                {
                    // no type
                    return;
                }

                if (CPyMarshal.ReadPtrField(typePtr, typeof(PyTypeObject), "tp_dealloc") == IntPtr.Zero)
                {
                    // no dealloc function
                    return;
                }

                dgt_void_ptr dealloc = (dgt_void_ptr)
                                       CPyMarshal.ReadFunctionPtrField(
                    typePtr, typeof(PyTypeObject), "tp_dealloc", typeof(dgt_void_ptr));
                dealloc(ptr);
            }

            // not really surprised to see these errors...
            catch (BadMappingException) {}
            catch (AccessViolationException) {}
            catch (NullReferenceException) {}

            // may be worth mentioning other errors though
            catch (Exception e)
            {
                if (this.logErrors)
                {
                    Console.WriteLine("unexpected error during DumpPtr:\n{0}", e);
                }
            }
        }
        IC_PyTuple_Dealloc(IntPtr tuplePtr)
        {
            int    length   = CPyMarshal.ReadIntField(tuplePtr, typeof(PyTupleObject), "ob_size");
            IntPtr itemsPtr = CPyMarshal.Offset(
                tuplePtr, Marshal.OffsetOf(typeof(PyTupleObject), "ob_item"));

            for (int i = 0; i < length; i++)
            {
                IntPtr itemPtr = CPyMarshal.ReadPtr(
                    CPyMarshal.Offset(
                        itemsPtr, i * CPyMarshal.PtrSize));
                if (itemPtr != IntPtr.Zero)
                {
                    this.DecRef(itemPtr);
                }
            }
            dgt_void_ptr freeDgt = (dgt_void_ptr)
                                   CPyMarshal.ReadFunctionPtrField(
                this.PyTuple_Type, typeof(PyTypeObject), "tp_free", typeof(dgt_void_ptr));

            freeDgt(tuplePtr);
        }
Example #9
0
        IC_PyList_Dealloc(IntPtr listPtr)
        {
            PyListObject listStruct = (PyListObject)Marshal.PtrToStructure(listPtr, typeof(PyListObject));

            if (listStruct.ob_item != IntPtr.Zero)
            {
                IntPtr itemsPtr = listStruct.ob_item;
                for (int i = 0; i < listStruct.ob_size; i++)
                {
                    IntPtr itemPtr = CPyMarshal.ReadPtr(itemsPtr);
                    if (itemPtr != IntPtr.Zero)
                    {
                        this.DecRef(itemPtr);
                    }
                    itemsPtr = CPyMarshal.Offset(itemsPtr, CPyMarshal.PtrSize);
                }
                this.allocator.Free(listStruct.ob_item);
            }
            dgt_void_ptr freeDgt = (dgt_void_ptr)
                                   CPyMarshal.ReadFunctionPtrField(
                this.PyList_Type, typeof(PyTypeObject), "tp_free", typeof(dgt_void_ptr));

            freeDgt(listPtr);
        }