Rev 118: Change the internals of _loader a fair amount. in http://bazaar.launchpad.net/~meliae-dev/meliae/trunk

John Arbash Meinel john at arbash-meinel.com
Wed Dec 23 17:13:31 GMT 2009


At http://bazaar.launchpad.net/~meliae-dev/meliae/trunk

------------------------------------------------------------
revno: 118
revision-id: john at arbash-meinel.com-20091223171312-7rhhmmdb2p7sldqt
parent: john at arbash-meinel.com-20091223165505-vj8z50nodcw8cnd4
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: trunk
timestamp: Wed 2009-12-23 11:13:12 -0600
message:
  Change the internals of _loader a fair amount.
  
  Rather than trying to pack things into C long types, we go ahead
  and keep the reference to them. Should be interesting.
-------------- next part --------------
=== modified file 'meliae/_loader.pyx'
--- a/meliae/_loader.pyx	2009-12-23 16:55:05 +0000
+++ b/meliae/_loader.pyx	2009-12-23 17:13:12 +0000
@@ -23,6 +23,8 @@
 
     PyObject *PyDict_GetItem(object d, object key)
     int PyDict_SetItem(object d, object key, object val) except -1
+    void Py_INCREF(PyObject*)
+    void Py_DECREF(PyObject*)
 
 
 cdef object _set_default(object d, object val):
@@ -41,60 +43,58 @@
     return val
 
 
-cdef object _ref_list_to_list(long *ref_list):
+cdef object _ref_list_to_list(PyObject **ref_list):
     """Convert the notation of [len, items, ...] into [items].
 
     :param ref_list: A pointer to NULL, or to a list of longs. The list should
         start with the count of items
     """
-    cdef long i
+    cdef long i, size
     # TODO: Always return a tuple, we already know the width, and this prevents
     #       double malloc()
 
     if ref_list == NULL:
         return ()
+    size = <long>(ref_list[0])
     refs = []
-    for i from 1 <= i <= ref_list[0]:
-        refs.append(ref_list[i])
+    for i from 1 <= i <= size:
+        refs.append(<object>(ref_list[i]))
     return refs
 
 
-cdef long *_list_to_ref_list(object refs) except? NULL:
-    cdef long i, num_refs, *ref_list
-    cdef unsigned long temp
+cdef PyObject **_list_to_ref_list(object refs) except? NULL:
+    cdef long i, num_refs
+    cdef PyObject **ref_list
 
     num_refs = len(refs)
     if num_refs == 0:
         return NULL
-    ref_list = <long*>PyMem_Malloc(sizeof(long)*(num_refs+1))
-    ref_list[0] = num_refs
+    ref_list = <PyObject**>PyMem_Malloc(sizeof(PyObject*)*(num_refs+1))
+    ref_list[0] = <PyObject*>(num_refs)
     i = 1
     for ref in refs:
-        # refs often come in as unsigned integers, internally, we just track
-        # them as ints. Note that we don't support processing a 64-bit dump
-        # on 32-bit platforms. We *could* but it isn't really worth the memory
-        # overhead (yet).
-        temp = ref
-        ref_list[i] = <long>temp
+        # TODO: we *should* incref here, but I'm cheating
+        ref_list[i] = <PyObject*>ref
+        Py_INCREF(ref_list[i])
         i = i + 1
     return ref_list
 
 
-cdef object _format_list(long *ref_list):
+cdef object _format_list(PyObject **ref_list):
     cdef long i, num_refs, max_refs
 
     if ref_list == NULL:
         return ''
-    num_refs = ref_list[0]
+    num_refs = <long>ref_list[0]
     max_refs = num_refs
     if max_refs > 10:
         max_refs = 10
     ref_str = ['[']
     for i from 0 <= i < max_refs:
         if i == 0:
-            ref_str.append('%d' % ref_list[i+1])
+            ref_str.append('%d' % (<object>ref_list[i+1]))
         else:
-            ref_str.append(', %d' % ref_list[i+1])
+            ref_str.append(', %d' % (<object>ref_list[i+1]))
     if num_refs > 10:
         ref_str.append(', ...]')
     else:
@@ -130,17 +130,17 @@
     cdef readonly object type_str # pointer to a PyString, this is expected to be shared
                                   # with many other instances, but longer than 4 bytes
     cdef public long size
-    cdef long *_ref_list # An array of addresses that this object
-                         # referenced. May be NULL if len() == 0
-                         # If not null, the first item is the length of the
-                         # list
+    cdef PyObject **_ref_list # An array of addresses that this object
+                              # referenced. May be NULL if len() == 0
+                              # If not null, the first item is the length of the
+                              # list
     cdef readonly int length # Object length (ob_size), aka len(object)
     cdef public object value    # May be None, a PyString or a PyInt
     cdef readonly object name     # Name of this object (only valid for
                                   # modules, etc)
-    cdef long *_referrer_list # An array of addresses that refer to this,
-                              # if not null, the first item indicates the
-                              # length of the list
+    cdef PyObject **_referrer_list # An array of addresses that refer to this,
+                                   # if not null, the first item indicates the
+                                   # length of the list
 
     cdef public unsigned long total_size # Size of everything referenced from
                                          # this object
@@ -176,7 +176,7 @@
         def __get__(self):
             if self._ref_list == NULL:
                 return 0
-            return self._ref_list[0]
+            return <long>self._ref_list[0]
 
     property referrers:
         """The list of objects that reference this object.
@@ -197,13 +197,20 @@
         def __get__(self):
             if self._referrer_list == NULL:
                 return 0
-            return self._referrer_list[0]
+            return <long>self._referrer_list[0]
 
     def __dealloc__(self):
+        cdef long i, size
         if self._ref_list != NULL:
+            size = <long>self._ref_list[0]
+            for i from 1 <= i < size:
+                Py_DECREF(self._ref_list[i])
             PyMem_Free(self._ref_list)
             self._ref_list = NULL
         if self._referrer_list != NULL:
+            size = <long>self._referrer_list[0]
+            for i from 1 <= i < size:
+                Py_DECREF(self._referrer_list[i])
             PyMem_Free(self._referrer_list)
             self._referrer_list = NULL
 
@@ -219,13 +226,14 @@
             ref_space = ''
             ref_str = ''
         else:
-            num_refs = self._ref_list[0]
+            num_refs = <long>self._ref_list[0]
             ref_str = _format_list(self._ref_list)
             ref_space = ' '
         if self._referrer_list == NULL:
             referrer_str = ''
         else:
-            referrer_str = ', %d referrers %s' % (self._referrer_list[0],
+            referrer_str = ', %d referrers %s' % (
+                <long>self._referrer_list[0],
                 _format_list(self._referrer_list))
         if self.value is None:
             value_str = ''



More information about the bazaar-commits mailing list