[pypy-svn] r51410 - in pypy/dist/pypy/module/_rawffi: . test

fijal at codespeak.net fijal at codespeak.net
Tue Feb 12 14:52:18 CET 2008


Author: fijal
Date: Tue Feb 12 14:52:15 2008
New Revision: 51410

Added:
   pypy/dist/pypy/module/_rawffi/test/test_tracker.py   (contents, props changed)
   pypy/dist/pypy/module/_rawffi/tracker.py   (contents, props changed)
Modified:
   pypy/dist/pypy/module/_rawffi/__init__.py
   pypy/dist/pypy/module/_rawffi/interp_rawffi.py
Log:
Some support for object allocation tracing.


Modified: pypy/dist/pypy/module/_rawffi/__init__.py
==============================================================================
--- pypy/dist/pypy/module/_rawffi/__init__.py	(original)
+++ pypy/dist/pypy/module/_rawffi/__init__.py	Tue Feb 12 14:52:15 2008
@@ -5,6 +5,7 @@
 from pypy.interpreter.mixedmodule import MixedModule
 from pypy.module._rawffi.interp_rawffi import W_CDLL
 from pypy.rpython.lltypesystem import lltype, rffi
+from pypy.module._rawffi.tracker import Tracker
 
 class Module(MixedModule):
     applevelname = '_rawffi'
@@ -21,6 +22,7 @@
         'charp2string'       : 'interp_rawffi.charp2string',
         'charp2rawstring'    : 'interp_rawffi.charp2rawstring',
         'CallbackPtr'        : 'callback.W_CallbackPtr',
+        '_num_of_allocated_objects' : 'tracker.num_of_allocated_objects',
     }
 
     appleveldefs = {

Modified: pypy/dist/pypy/module/_rawffi/interp_rawffi.py
==============================================================================
--- pypy/dist/pypy/module/_rawffi/interp_rawffi.py	(original)
+++ pypy/dist/pypy/module/_rawffi/interp_rawffi.py	Tue Feb 12 14:52:15 2008
@@ -11,6 +11,7 @@
 
 from pypy.tool.sourcetools import func_with_new_name
 from pypy.rlib.rarithmetic import intmask, r_uint, r_singlefloat
+from pypy.module._rawffi.tracker import tracker
 
 def _signed_type_for(TYPE):
     sz = rffi.sizeof(TYPE)
@@ -195,6 +196,9 @@
         else:
             self.ll_buffer = lltype.malloc(rffi.VOIDP.TO, size, flavor='raw',
                                            zero=True)
+            if tracker.DO_TRACING:
+                ll_buf = rffi.cast(rffi.UINT, self.ll_buffer)
+                tracker.trace_allocation(ll_buf, self)
 
     def getbuffer(space, self):
         return space.wrap(rffi.cast(lltype.Unsigned, self.ll_buffer))
@@ -210,6 +214,9 @@
     def free(self, space):
         if not self.ll_buffer:
             raise segfault_exception(space, "freeing NULL pointer")
+        if tracker.DO_TRACING:
+            ll_buf = rffi.cast(rffi.UINT, self.ll_buffer)
+            tracker.trace_free(ll_buf)
         lltype.free(self.ll_buffer, flavor='raw')
         self.ll_buffer = lltype.nullptr(rffi.VOIDP.TO)
     free.unwrap_spec = ['self', ObjSpace]

Added: pypy/dist/pypy/module/_rawffi/test/test_tracker.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/_rawffi/test/test_tracker.py	Tue Feb 12 14:52:15 2008
@@ -0,0 +1,29 @@
+
+from pypy.conftest import gettestobjspace
+from pypy.module._rawffi.tracker import Tracker
+
+class AppTestTracker:
+    def setup_class(cls):
+        Tracker.DO_TRACING = True
+        space = gettestobjspace(usemodules=('_rawffi','struct'))
+        cls.space = space
+
+    def test_array(self):
+        import _rawffi
+        assert _rawffi._num_of_allocated_objects() == 0
+        a = _rawffi.Array('c')(3)
+        assert _rawffi._num_of_allocated_objects() == 1
+        a.free()
+        assert _rawffi._num_of_allocated_objects() == 0
+
+    def test_structure(self):
+        import _rawffi
+        assert _rawffi._num_of_allocated_objects() == 0
+        s = _rawffi.Structure([('a', 'i'), ('b', 'i')])()
+        assert _rawffi._num_of_allocated_objects() == 1
+        s.free()
+        assert _rawffi._num_of_allocated_objects() == 0
+
+    def teardown_class(cls):
+        Tracker.DO_TRACING = False
+

Added: pypy/dist/pypy/module/_rawffi/tracker.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/_rawffi/tracker.py	Tue Feb 12 14:52:15 2008
@@ -0,0 +1,29 @@
+
+""" The file that keeps track about freed/kept-alive objects allocated
+by _rawffi. Used for debugging ctypes
+"""
+from pypy.interpreter.baseobjspace import W_Root, ObjSpace, Wrappable, \
+     Arguments
+
+class Tracker(object):
+    DO_TRACING = False
+
+    def __init__(self):
+        self.alloced = {}
+
+    def trace_allocation(self, address, obj):
+        self.alloced[address] = obj
+
+    def trace_free(self, address):
+        del self.alloced[address]
+
+# single, global, static object to keep all tracker info
+tracker = Tracker()
+
+def num_of_allocated_objects(space):
+    return space.wrap(len(tracker.alloced))
+num_of_allocated_objects.unwrap_spec = [ObjSpace]
+
+def print_alloced_objects(space):
+    xxx
+    # eventually inspect and print what's left from applevel



More information about the Pypy-commit mailing list