changeset 38941:c0b6a7c78a21

index: split up nt_init() in two I'd like to make nt_init() take a pointer to a nodetree to initialize, but it currently also allocates the nodetree. This patch prepares for that change by making nt_init() be about initializing an existing node tree and by creating a new index_init_nt() that creates the nodetree. Differential Revision: https://phab.mercurial-scm.org/D4114
author Martin von Zweigbergk <martinvonz@google.com>
date Wed, 18 Jul 2018 12:03:31 -0700
parents b6fb71a0a005
children 3e74c01102af
files mercurial/cext/revlog.c
diffstat 1 files changed, 27 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/cext/revlog.c	Wed Jul 18 23:20:01 2018 -0700
+++ b/mercurial/cext/revlog.c	Wed Jul 18 12:03:31 2018 -0700
@@ -1095,11 +1095,30 @@
 	return nt_insert(self, node, -2);
 }
 
-static int nt_init(indexObject *self)
+static int nt_init(nodetree *self, indexObject *index, unsigned capacity)
+{
+	self->index = index;
+	self->capacity = capacity;
+	self->depth = 0;
+	self->splits = 0;
+	self->nodes = calloc(self->capacity, sizeof(nodetreenode));
+	if (self->nodes == NULL) {
+		PyErr_NoMemory();
+		return -1;
+	}
+	self->length = 1;
+	if (nt_insert(self, nullid, -1) == -1) {
+		free(self->nodes);
+		return -1;
+	}
+	return 0;
+}
+
+static int index_init_nt(indexObject *self)
 {
 	if (self->nt == NULL) {
 		if ((size_t)self->raw_length > INT_MAX / sizeof(nodetreenode)) {
-			PyErr_SetString(PyExc_ValueError, "overflow in nt_init");
+			PyErr_SetString(PyExc_ValueError, "overflow in index_init_nt");
 			return -1;
 		}
 		self->nt = PyMem_Malloc(sizeof(nodetree));
@@ -1107,29 +1126,15 @@
 			PyErr_NoMemory();
 			return -1;
 		}
-		self->nt->capacity = self->raw_length < 4
-			? 4 : (int)self->raw_length / 2;
-
-		self->nt->nodes = calloc(self->nt->capacity, sizeof(nodetreenode));
-		if (self->nt->nodes == NULL) {
+		unsigned capacity = (self->raw_length < 4 ? 4 : (int)self->raw_length / 2);
+		if (nt_init(self->nt, self, capacity) == -1) {
 			PyMem_Free(self->nt);
 			self->nt = NULL;
-			PyErr_NoMemory();
 			return -1;
 		}
 		self->ntrev = (int)index_length(self);
 		self->ntlookups = 1;
 		self->ntmisses = 0;
-		self->nt->depth = 0;
-		self->nt->splits = 0;
-		self->nt->length = 1;
-		self->nt->index = self;
-		if (nt_insert(self->nt, nullid, -1) == -1) {
-			free(self->nt->nodes);
-			PyMem_Free(self->nt);
-			self->nt = NULL;
-			return -1;
-		}
 	}
 	return 0;
 }
@@ -1146,7 +1151,7 @@
 {
 	int rev;
 
-	if (nt_init(self) == -1)
+	if (index_init_nt(self) == -1)
 		return -3;
 
 	self->ntlookups++;
@@ -1341,7 +1346,7 @@
 		Py_RETURN_NONE;
 	}
 
-	if (nt_init(self) == -1)
+	if (index_init_nt(self) == -1)
 		return NULL;
 	if (nt_populate(self) == -1)
 		return NULL;
@@ -1376,7 +1381,7 @@
 		return NULL;
 
 	self->ntlookups++;
-	if (nt_init(self) == -1)
+	if (index_init_nt(self) == -1)
 		return NULL;
 	if (nt_populate(self) == -1)
 		return NULL;
@@ -1921,7 +1926,7 @@
 		return -1;
 	}
 
-	if (nt_init(self) == -1)
+	if (index_init_nt(self) == -1)
 		return -1;
 	return nt_insert(self->nt, node, (int)rev);
 }