Commits

Armin Rigo  committed b4853af

Reintroduce a way to report the malloc counters.

  • Participants
  • Parent commits b1db1d0
  • Branches kill-someobject

Comments (0)

Files changed (3)

File pypy/translator/c/src/main.h

         pypy_debug_catch_fatal_exception();
     }
 
+    pypy_malloc_counters_results();
+
     return exitcode;
 
  memory_out:

File pypy/translator/c/src/mem.h

 		r = (restype) PyObject_Malloc(size);			\
 		if (r != NULL) {					\
 			memset((void*)r, 0, size);			\
+			COUNT_MALLOC;					\
 		}							\
 	}
 
 
 #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)
 
 
 #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

File pypy/translator/c/test/test_genc.py

                     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
             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)