Author: Armin Rigo <[email protected]>
Branch: kill-someobject
Changeset: r57947:b4853aff2f42
Date: 2012-10-09 12:52 +0200
http://bitbucket.org/pypy/pypy/changeset/b4853aff2f42/

Log:    Reintroduce a way to report the malloc counters.

diff --git a/pypy/translator/c/src/main.h b/pypy/translator/c/src/main.h
--- a/pypy/translator/c/src/main.h
+++ b/pypy/translator/c/src/main.h
@@ -68,6 +68,8 @@
         pypy_debug_catch_fatal_exception();
     }
 
+    pypy_malloc_counters_results();
+
     return exitcode;
 
  memory_out:
diff --git a/pypy/translator/c/src/mem.h b/pypy/translator/c/src/mem.h
--- a/pypy/translator/c/src/mem.h
+++ b/pypy/translator/c/src/mem.h
@@ -103,6 +103,7 @@
                r = (restype) PyObject_Malloc(size);                    \
                if (r != NULL) {                                        \
                        memset((void*)r, 0, size);                      \
+                       COUNT_MALLOC;                                   \
                }                                                       \
        }
 
@@ -110,11 +111,14 @@
 
 #define OP_RAW_MALLOC(size, r, restype)  {                             \
                r = (restype) PyObject_Malloc(size);                    \
+               if (r != NULL) {                                        \
+                       COUNT_MALLOC;                                   \
+               }                                                       \
        }
 
 #endif
 
-#define OP_RAW_FREE(p, r) PyObject_Free(p);
+#define OP_RAW_FREE(p, r) PyObject_Free(p); COUNT_FREE;
 
 #define OP_RAW_MEMCLEAR(p, size, r) memset((void*)p, 0, size)
 
@@ -135,6 +139,31 @@
 
 #define OP_FREE(p)     OP_RAW_FREE(p, do_not_use)
 
+/*------------------------------------------------------------*/
+#ifndef COUNT_OP_MALLOCS
+/*------------------------------------------------------------*/
+
+#define COUNT_MALLOC   /* nothing */
+#define COUNT_FREE     /* nothing */
+
+#define pypy_malloc_counters_results()  /* nothing */
+
+/*------------------------------------------------------------*/
+#else /*COUNT_OP_MALLOCS*/
+/*------------------------------------------------------------*/
+
+static int count_mallocs=0, count_frees=0;
+
+#define COUNT_MALLOC   count_mallocs++
+#define COUNT_FREE     count_frees++
+
+#define pypy_malloc_counters_results()  \
+    printf("MALLOC COUNTERS: %d %d\n", count_mallocs, count_frees)
+
+/*------------------------------------------------------------*/
+#endif /*COUNT_OP_MALLOCS*/
+/*------------------------------------------------------------*/
+
 /* for Boehm GC */
 
 #ifdef USING_BOEHM_GC
diff --git a/pypy/translator/c/test/test_genc.py 
b/pypy/translator/c/test/test_genc.py
--- a/pypy/translator/c/test/test_genc.py
+++ b/pypy/translator/c/test/test_genc.py
@@ -71,6 +71,7 @@
                     policy=annotatorpolicy, thread=thread)
     if not backendopt:
         t.disable(["backendopt_lltype"])
+    t.driver.config.translation.countmallocs = True
     t.annotate()
     t.compile_c()
     ll_res = graphof(t.context, fn).getreturnvar().concretetype
@@ -79,15 +80,31 @@
             t.view()
     except AttributeError:
         pass
-    def f(*args):
+
+    def f(*args, **kwds):
+        if 'expected_extra_mallocs' in kwds:
+            expected_extra_mallocs = kwds.pop('expected_extra_mallocs')
+        else:
+            expected_extra_mallocs = 0
+        assert not kwds
         assert len(args) == len(argtypes)
         for arg, argtype in zip(args, argtypes):
             assert isinstance(arg, argtype)
         stdout = t.driver.cbuilder.cmdexec(" ".join([llrepr(arg) for arg in 
args]))
         print stdout
-        assert stdout.endswith(' ;\n')
+        stdout, lastline, empty = stdout.rsplit('\n', 2)
+        assert empty == ''
+        assert lastline.startswith('MALLOC COUNTERS: ')
+        mallocs, frees = map(int, lastline.split()[2:])
+        assert stdout.endswith(' ;')
         pos = stdout.rindex('THE RESULT IS: ')
-        res = stdout[pos + len('THE RESULT IS: '):-3]
+        res = stdout[pos + len('THE RESULT IS: '):-2]
+        #
+        if isinstance(expected_extra_mallocs, int):
+            assert mallocs - frees == expected_extra_mallocs
+        else:
+            assert mallocs - frees in expected_extra_mallocs
+        #
         if ll_res in [lltype.Signed, lltype.Unsigned, lltype.SignedLongLong,
                       lltype.UnsignedLongLong]:
             return int(res)
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to