Patchwork D4118: index: make node tree a Python object

login
register
mail settings
Submitter phabricator
Date Aug. 9, 2018, 12:09 a.m.
Message ID <c8acd19c006bcb0d400ae79f7c827963@localhost.localdomain>
Download mbox | patch
Permalink /patch/33430/
State Not Applicable
Headers show

Comments

phabricator - Aug. 9, 2018, 12:09 a.m.
martinvonz updated this revision to Diff 10082.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D4118?vs=10042&id=10082

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

AFFECTED FILES
  mercurial/cext/parsers.c
  mercurial/cext/revlog.c
  mercurial/policy.py

CHANGE DETAILS




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

Patch

diff --git a/mercurial/policy.py b/mercurial/policy.py
--- a/mercurial/policy.py
+++ b/mercurial/policy.py
@@ -69,7 +69,7 @@ 
     (r'cext', r'bdiff'): 3,
     (r'cext', r'mpatch'): 1,
     (r'cext', r'osutil'): 4,
-    (r'cext', r'parsers'): 7,
+    (r'cext', r'parsers'): 8,
 }
 
 # map import request to other package or module
diff --git a/mercurial/cext/revlog.c b/mercurial/cext/revlog.c
--- a/mercurial/cext/revlog.c
+++ b/mercurial/cext/revlog.c
@@ -42,6 +42,7 @@ 
  * Zero is empty
  */
 typedef struct {
+	PyObject_HEAD
 	indexObject *index;
 	nodetreenode *nodes;
 	unsigned length;     /* # nodes in use */
@@ -1068,6 +1069,7 @@ 
 static int nt_init(nodetree *self, indexObject *index, unsigned capacity)
 {
 	self->index = index;
+	Py_INCREF(index);
 	self->capacity = capacity;
 	self->depth = 0;
 	self->splits = 0;
@@ -1084,6 +1086,15 @@ 
 	return 0;
 }
 
+static int nt_init_py(nodetree *self, PyObject *args)
+{
+	PyObject *index;
+	unsigned capacity;
+	if (!PyArg_ParseTuple(args, "OI", &index, &capacity))
+		return -1;
+	return nt_init(self, (indexObject*)index, capacity);
+}
+
 static int nt_partialmatch(nodetree *self, const char *node,
 			   Py_ssize_t nodelen)
 {
@@ -1136,21 +1147,68 @@ 
 	return -3;
 }
 
+static void nt_dealloc(nodetree *self)
+{
+	Py_XDECREF(self->index);
+	free(self->nodes);
+	self->nodes = NULL;
+	PyObject_Del(self);
+}
+
+static PyTypeObject nodetreeType = {
+	PyVarObject_HEAD_INIT(NULL, 0) /* header */
+	"parsers.nodetree",        /* tp_name */
+	sizeof(nodetree) ,         /* tp_basicsize */
+	0,                         /* tp_itemsize */
+	(destructor)nt_dealloc,    /* tp_dealloc */
+	0,                         /* tp_print */
+	0,                         /* tp_getattr */
+	0,                         /* tp_setattr */
+	0,                         /* tp_compare */
+	0,                         /* tp_repr */
+	0,                         /* tp_as_number */
+	0,                         /* tp_as_sequence */
+	0,                         /* tp_as_mapping */
+	0,                         /* tp_hash */
+	0,                         /* tp_call */
+	0,                         /* tp_str */
+	0,                         /* tp_getattro */
+	0,                         /* tp_setattro */
+	0,                         /* tp_as_buffer */
+	Py_TPFLAGS_DEFAULT,        /* tp_flags */
+	"nodetree",                /* tp_doc */
+	0,                         /* tp_traverse */
+	0,                         /* tp_clear */
+	0,                         /* tp_richcompare */
+	0,                         /* tp_weaklistoffset */
+	0,                         /* tp_iter */
+	0,                         /* tp_iternext */
+	0,                         /* tp_methods */
+	0,                         /* tp_members */
+	0,                         /* tp_getset */
+	0,                         /* tp_base */
+	0,                         /* tp_dict */
+	0,                         /* tp_descr_get */
+	0,                         /* tp_descr_set */
+	0,                         /* tp_dictoffset */
+	(initproc)nt_init_py,      /* tp_init */
+	0,                         /* tp_alloc */
+};
+
 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 index_init_nt");
 			return -1;
 		}
-		self->nt = PyMem_Malloc(sizeof(nodetree));
+		self->nt = PyObject_New(nodetree, &nodetreeType);
 		if (self->nt == NULL) {
-			PyErr_NoMemory();
 			return -1;
 		}
 		unsigned capacity = (self->raw_length < 4 ? 4 : (int)self->raw_length / 2);
 		if (nt_init(self->nt, self, capacity) == -1) {
-			PyMem_Free(self->nt);
+			nt_dealloc(self->nt);
 			self->nt = NULL;
 			return -1;
 		}
@@ -2010,8 +2068,7 @@ 
 		self->offsets = NULL;
 	}
 	if (self->nt != NULL) {
-		free(self->nt->nodes);
-		PyMem_Free(self->nt);
+		nt_dealloc(self->nt);
 	}
 	self->nt = NULL;
 	Py_CLEAR(self->headrevs);
@@ -2035,6 +2092,7 @@ 
 	}
 	Py_XDECREF(self->data);
 	Py_XDECREF(self->added);
+	Py_XDECREF(self->nt);
 	PyObject_Del(self);
 }
 
@@ -2184,6 +2242,12 @@ 
 	Py_INCREF(&indexType);
 	PyModule_AddObject(mod, "index", (PyObject *)&indexType);
 
+	nodetreeType.tp_new = PyType_GenericNew;
+	if (PyType_Ready(&nodetreeType) < 0)
+		return;
+	Py_INCREF(&nodetreeType);
+	PyModule_AddObject(mod, "nodetree", (PyObject *)&nodetreeType);
+
 	nullentry = Py_BuildValue(PY23("iiiiiiis#", "iiiiiiiy#"), 0, 0, 0,
 				  -1, -1, -1, -1, nullid, 20);
 	if (nullentry)
diff --git a/mercurial/cext/parsers.c b/mercurial/cext/parsers.c
--- a/mercurial/cext/parsers.c
+++ b/mercurial/cext/parsers.c
@@ -713,7 +713,7 @@ 
 void manifest_module_init(PyObject *mod);
 void revlog_module_init(PyObject *mod);
 
-static const int version = 7;
+static const int version = 8;
 
 static void module_init(PyObject *mod)
 {