summaryrefslogtreecommitdiffstats
path: root/Objects/obmalloc.c
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2012-12-09 13:28:26 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2012-12-09 13:28:26 (GMT)
commitf9d0b1256fd5a9ae90fa8e8418bd450ec6b7f5f2 (patch)
treefd5ad7ad717440f4586652180cb31a3cf2e52e97 /Objects/obmalloc.c
parentb4b8f234d4407492c4493e3a16bc8263139c7869 (diff)
downloadcpython-f9d0b1256fd5a9ae90fa8e8418bd450ec6b7f5f2.zip
cpython-f9d0b1256fd5a9ae90fa8e8418bd450ec6b7f5f2.tar.gz
cpython-f9d0b1256fd5a9ae90fa8e8418bd450ec6b7f5f2.tar.bz2
Issue #13390: New function :func:`sys.getallocatedblocks()` returns the number of memory blocks currently allocated.
Also, the ``-R`` option to regrtest uses this function to guard against memory allocation leaks.
Diffstat (limited to 'Objects/obmalloc.c')
-rw-r--r--Objects/obmalloc.c21
1 files changed, 20 insertions, 1 deletions
diff --git a/Objects/obmalloc.c b/Objects/obmalloc.c
index 6225ebb..c82c978 100644
--- 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_AllocatedBlocks = 0;
+
+Py_ssize_t
+_Py_GetAllocatedBlocks(void)
+{
+ return _Py_AllocatedBlocks;
+}
+
+
/* 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_AllocatedBlocks++;
+
/*
* 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_AllocatedBlocks--;
+ return result;
+ }
}
/* free */
@@ -978,6 +995,8 @@ PyObject_Free(void *p)
if (p == NULL) /* free(NULL) has no effect */
return;
+ _Py_AllocatedBlocks--;
+
#ifdef WITH_VALGRIND
if (UNLIKELY(running_on_valgrind > 0))
goto redirect;