D4114: index: split up nt_init() in two

martinvonz (Martin von Zweigbergk) phabricator at mercurial-scm.org
Mon Aug 6 09:25:14 EDT 2018


martinvonz updated this revision to Diff 9979.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D4114?vs=9917&id=9979

REVISION DETAIL
  https://phab.mercurial-scm.org/D4114

AFFECTED FILES
  mercurial/cext/revlog.c

CHANGE DETAILS

diff --git a/mercurial/cext/revlog.c b/mercurial/cext/revlog.c
--- a/mercurial/cext/revlog.c
+++ b/mercurial/cext/revlog.c
@@ -1092,41 +1092,46 @@
 	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) {
 		self->nt = PyMem_Malloc(sizeof(nodetree));
 		if (self->nt == NULL) {
 			PyErr_NoMemory();
 			return -1;
 		}
 		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->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) {
 			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);
-			free(self->nt);
-			self->nt = NULL;
-			return -1;
-		}
 	}
 	return 0;
 }
@@ -1143,7 +1148,7 @@
 {
 	int rev;
 
-	if (nt_init(self) == -1)
+	if (index_init_nt(self) == -1)
 		return -3;
 
 	self->ntlookups++;
@@ -1338,7 +1343,7 @@
 		Py_RETURN_NONE;
 	}
 
-	if (nt_init(self) == -1)
+	if (index_init_nt(self) == -1)
 		return NULL;
 	if (nt_populate(self) == -1)
 		return NULL;
@@ -1373,7 +1378,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;
@@ -1918,7 +1923,7 @@
 		return -1;
 	}
 
-	if (nt_init(self) == -1)
+	if (index_init_nt(self) == -1)
 		return -1;
 	return nt_insert(self->nt, node, (int)rev);
 }



To: martinvonz, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list