[PATCH 03 of 11] parsers: use Python memory allocator in reachableroots2()

Gregory Szorc gregory.szorc at gmail.com
Thu Mar 9 16:59:11 EST 2017


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1489089934 28800
#      Thu Mar 09 12:05:34 2017 -0800
# Node ID 32baf8818dd3c3d9a4b76f5bd277a682f1cb2781
# Parent  4482def4ff468c6d0d5ee7f6270d8976bd264774
parsers: use Python memory allocator in reachableroots2()

For the same reasons we've been doing this in other commits.

Python 2 doesn't have an allocator API that zeros memory. So we
do this manually with a memset().

diff --git a/mercurial/parsers.c b/mercurial/parsers.c
--- a/mercurial/parsers.c
+++ b/mercurial/parsers.c
@@ -1210,18 +1210,20 @@ static PyObject *reachableroots2(indexOb
 		goto bail;
 
 	/* Initialize internal datastructures */
-	tovisit = (int *)malloc((len + 1) * sizeof(int));
+	tovisit = (int *)PyMem_Malloc((len + 1) * sizeof(int));
 	if (tovisit == NULL) {
 		PyErr_NoMemory();
 		goto bail;
 	}
 
-	revstates = (char *)calloc(len + 1, 1);
+	revstates = (char *)PyMem_Malloc(len + 1);
 	if (revstates == NULL) {
 		PyErr_NoMemory();
 		goto bail;
 	}
 
+	memset(revstates, 0, len + 1);
+
 	l = PyList_GET_SIZE(roots);
 	for (i = 0; i < l; i++) {
 		revnum = PyInt_AsLong(PyList_GET_ITEM(roots, i));
@@ -1312,13 +1314,13 @@ static PyObject *reachableroots2(indexOb
 		}
 	}
 
-	free(revstates);
-	free(tovisit);
+	PyMem_Free(revstates);
+	PyMem_Free(tovisit);
 	return reachable;
 bail:
 	Py_XDECREF(reachable);
-	free(revstates);
-	free(tovisit);
+	PyMem_Free(revstates);
+	PyMem_Free(tovisit);
 	return NULL;
 }
 


More information about the Mercurial-devel mailing list