Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(29982)

Unified Diff: Objects/obmalloc.c

Issue 13390: Hunt memory allocations in addition to reference leaks
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Lib/test/test_sys.py ('k') | Python/pythonrun.c » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
--- a/Objects/obmalloc.c
+++ b/Objects/obmalloc.c
@@ -525,6 +525,15 @@ static size_t ntimes_arena_allocated = 0
/* High water mark (max value ever seen) for narenas_currently_allocated. */
static size_t narenas_highwater = 0;
+static Py_ssize_t _Py_AllocedBlocks = 0;
+
+Py_ssize_t
+_Py_GetAllocedBlocks(void)
+{
+ return _Py_AllocedBlocks;
+}
+
+
/* Allocate a new arena. If we run out of memory, return NULL. Else
* allocate a new arena, and return the address of an arena_object
* describing the new arena. It's expected that the caller will set
@@ -785,6 +794,8 @@ PyObject_Malloc(size_t nbytes)
if (nbytes > PY_SSIZE_T_MAX)
return NULL;
+ _Py_AllocedBlocks++;
+
/*
* This implicitly redirects malloc(0).
*/
@@ -901,6 +912,7 @@ PyObject_Malloc(size_t nbytes)
* and free list are already initialized.
*/
bp = pool->freeblock;
+ assert(bp != NULL);
pool->freeblock = *(block **)bp;
UNLOCK();
return (void *)bp;
@@ -958,7 +970,12 @@ redirect:
*/
if (nbytes == 0)
nbytes = 1;
- return (void *)malloc(nbytes);
+ {
+ void *result = malloc(nbytes);
+ if (!result)
+ _Py_AllocedBlocks--;
+ return result;
+ }
}
/* free */
@@ -978,6 +995,8 @@ PyObject_Free(void *p)
if (p == NULL) /* free(NULL) has no effect */
return;
+ _Py_AllocedBlocks--;
+
#ifdef WITH_VALGRIND
if (UNLIKELY(running_on_valgrind > 0))
goto redirect;
« no previous file with comments | « Lib/test/test_sys.py ('k') | Python/pythonrun.c » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+