summaryrefslogtreecommitdiffstats
path: root/Python/hamt.c
diff options
context:
space:
mode:
Diffstat (limited to 'Python/hamt.c')
-rw-r--r--Python/hamt.c141
1 files changed, 47 insertions, 94 deletions
diff --git a/Python/hamt.c b/Python/hamt.c
index 3aa31c6..d94d070 100644
--- a/Python/hamt.c
+++ b/Python/hamt.c
@@ -525,8 +525,7 @@ hamt_node_bitmap_new(Py_ssize_t size)
assert(size % 2 == 0);
if (size == 0 && _empty_bitmap_node != NULL) {
- Py_INCREF(_empty_bitmap_node);
- return (PyHamtNode *)_empty_bitmap_node;
+ return (PyHamtNode *)Py_NewRef(_empty_bitmap_node);
}
/* No freelist; allocate a new bitmap node */
@@ -550,8 +549,7 @@ hamt_node_bitmap_new(Py_ssize_t size)
/* Since bitmap nodes are immutable, we can cache the instance
for size=0 and reuse it whenever we need an empty bitmap node.
*/
- _empty_bitmap_node = node;
- Py_INCREF(_empty_bitmap_node);
+ _empty_bitmap_node = (PyHamtNode_Bitmap*)Py_NewRef(node);
}
return (PyHamtNode *)node;
@@ -577,8 +575,7 @@ hamt_node_bitmap_clone(PyHamtNode_Bitmap *node)
}
for (i = 0; i < Py_SIZE(node); i++) {
- Py_XINCREF(node->b_array[i]);
- clone->b_array[i] = node->b_array[i];
+ clone->b_array[i] = Py_XNewRef(node->b_array[i]);
}
clone->b_bitmap = node->b_bitmap;
@@ -603,14 +600,12 @@ hamt_node_bitmap_clone_without(PyHamtNode_Bitmap *o, uint32_t bit)
uint32_t i;
for (i = 0; i < key_idx; i++) {
- Py_XINCREF(o->b_array[i]);
- new->b_array[i] = o->b_array[i];
+ new->b_array[i] = Py_XNewRef(o->b_array[i]);
}
assert(Py_SIZE(o) >= 0 && Py_SIZE(o) <= 32);
for (i = val_idx + 1; i < (uint32_t)Py_SIZE(o); i++) {
- Py_XINCREF(o->b_array[i]);
- new->b_array[i - 2] = o->b_array[i];
+ new->b_array[i - 2] = Py_XNewRef(o->b_array[i]);
}
new->b_bitmap = o->b_bitmap & ~bit;
@@ -643,15 +638,11 @@ hamt_node_new_bitmap_or_collision(uint32_t shift,
return NULL;
}
- Py_INCREF(key1);
- n->c_array[0] = key1;
- Py_INCREF(val1);
- n->c_array[1] = val1;
+ n->c_array[0] = Py_NewRef(key1);
+ n->c_array[1] = Py_NewRef(val1);
- Py_INCREF(key2);
- n->c_array[2] = key2;
- Py_INCREF(val2);
- n->c_array[3] = val2;
+ n->c_array[2] = Py_NewRef(key2);
+ n->c_array[3] = Py_NewRef(val2);
return (PyHamtNode *)n;
}
@@ -736,8 +727,7 @@ hamt_node_bitmap_assoc(PyHamtNode_Bitmap *self,
if (val_or_node == (PyObject *)sub_node) {
Py_DECREF(sub_node);
- Py_INCREF(self);
- return (PyHamtNode *)self;
+ return (PyHamtNode *)Py_NewRef(self);
}
PyHamtNode_Bitmap *ret = hamt_node_bitmap_clone(self);
@@ -759,8 +749,7 @@ hamt_node_bitmap_assoc(PyHamtNode_Bitmap *self,
if (comp_err == 1) { /* key == key_or_null */
if (val == val_or_node) {
/* we already have the same key/val pair; return self. */
- Py_INCREF(self);
- return (PyHamtNode *)self;
+ return (PyHamtNode *)Py_NewRef(self);
}
/* We're setting a new value for the key we had before.
@@ -769,8 +758,7 @@ hamt_node_bitmap_assoc(PyHamtNode_Bitmap *self,
if (ret == NULL) {
return NULL;
}
- Py_INCREF(val);
- Py_SETREF(ret->b_array[val_idx], val);
+ Py_SETREF(ret->b_array[val_idx], Py_NewRef(val));
return (PyHamtNode *)ret;
}
@@ -923,22 +911,18 @@ hamt_node_bitmap_assoc(PyHamtNode_Bitmap *self,
/* Copy all keys/values that will be before the new key/value
we are adding. */
for (i = 0; i < key_idx; i++) {
- Py_XINCREF(self->b_array[i]);
- new_node->b_array[i] = self->b_array[i];
+ new_node->b_array[i] = Py_XNewRef(self->b_array[i]);
}
/* Set the new key/value to the new Bitmap node. */
- Py_INCREF(key);
- new_node->b_array[key_idx] = key;
- Py_INCREF(val);
- new_node->b_array[val_idx] = val;
+ new_node->b_array[key_idx] = Py_NewRef(key);
+ new_node->b_array[val_idx] = Py_NewRef(val);
/* Copy all keys/values that will be after the new key/value
we are adding. */
assert(Py_SIZE(self) >= 0 && Py_SIZE(self) <= 32);
for (i = key_idx; i < (uint32_t)Py_SIZE(self); i++) {
- Py_XINCREF(self->b_array[i]);
- new_node->b_array[i + 2] = self->b_array[i];
+ new_node->b_array[i + 2] = Py_XNewRef(self->b_array[i]);
}
new_node->b_bitmap = self->b_bitmap | bit;
@@ -1019,10 +1003,8 @@ hamt_node_bitmap_without(PyHamtNode_Bitmap *self,
PyObject *key = sub_tree->b_array[0];
PyObject *val = sub_tree->b_array[1];
- Py_INCREF(key);
- Py_XSETREF(clone->b_array[key_idx], key);
- Py_INCREF(val);
- Py_SETREF(clone->b_array[val_idx], val);
+ Py_XSETREF(clone->b_array[key_idx], Py_NewRef(key));
+ Py_SETREF(clone->b_array[val_idx], Py_NewRef(val));
Py_DECREF(sub_tree);
@@ -1343,14 +1325,11 @@ hamt_node_collision_assoc(PyHamtNode_Collision *self,
}
for (i = 0; i < Py_SIZE(self); i++) {
- Py_INCREF(self->c_array[i]);
- new_node->c_array[i] = self->c_array[i];
+ new_node->c_array[i] = Py_NewRef(self->c_array[i]);
}
- Py_INCREF(key);
- new_node->c_array[i] = key;
- Py_INCREF(val);
- new_node->c_array[i + 1] = val;
+ new_node->c_array[i] = Py_NewRef(key);
+ new_node->c_array[i + 1] = Py_NewRef(val);
*added_leaf = 1;
return (PyHamtNode *)new_node;
@@ -1364,8 +1343,7 @@ hamt_node_collision_assoc(PyHamtNode_Collision *self,
if (self->c_array[val_idx] == val) {
/* We're setting a key/value pair that's already set. */
- Py_INCREF(self);
- return (PyHamtNode *)self;
+ return (PyHamtNode *)Py_NewRef(self);
}
/* We need to replace old value for the key
@@ -1378,14 +1356,12 @@ hamt_node_collision_assoc(PyHamtNode_Collision *self,
/* Copy all elements of the old node to the new one. */
for (i = 0; i < Py_SIZE(self); i++) {
- Py_INCREF(self->c_array[i]);
- new_node->c_array[i] = self->c_array[i];
+ new_node->c_array[i] = Py_NewRef(self->c_array[i]);
}
/* Replace the old value with the new value for the our key. */
Py_DECREF(new_node->c_array[val_idx]);
- Py_INCREF(val);
- new_node->c_array[val_idx] = val;
+ new_node->c_array[val_idx] = Py_NewRef(val);
return (PyHamtNode *)new_node;
@@ -1410,8 +1386,7 @@ hamt_node_collision_assoc(PyHamtNode_Collision *self,
return NULL;
}
new_node->b_bitmap = hamt_bitpos(self->c_hash, shift);
- Py_INCREF(self);
- new_node->b_array[1] = (PyObject*) self;
+ new_node->b_array[1] = Py_NewRef(self);
assoc_res = hamt_node_bitmap_assoc(
new_node, shift, hash, key, val, added_leaf);
@@ -1473,17 +1448,13 @@ hamt_node_collision_without(PyHamtNode_Collision *self,
}
if (key_idx == 0) {
- Py_INCREF(self->c_array[2]);
- node->b_array[0] = self->c_array[2];
- Py_INCREF(self->c_array[3]);
- node->b_array[1] = self->c_array[3];
+ node->b_array[0] = Py_NewRef(self->c_array[2]);
+ node->b_array[1] = Py_NewRef(self->c_array[3]);
}
else {
assert(key_idx == 2);
- Py_INCREF(self->c_array[0]);
- node->b_array[0] = self->c_array[0];
- Py_INCREF(self->c_array[1]);
- node->b_array[1] = self->c_array[1];
+ node->b_array[0] = Py_NewRef(self->c_array[0]);
+ node->b_array[1] = Py_NewRef(self->c_array[1]);
}
node->b_bitmap = hamt_bitpos(hash, shift);
@@ -1504,12 +1475,10 @@ hamt_node_collision_without(PyHamtNode_Collision *self,
/* Copy all other keys from `self` to `new` */
Py_ssize_t i;
for (i = 0; i < key_idx; i++) {
- Py_INCREF(self->c_array[i]);
- new->c_array[i] = self->c_array[i];
+ new->c_array[i] = Py_NewRef(self->c_array[i]);
}
for (i = key_idx + 2; i < Py_SIZE(self); i++) {
- Py_INCREF(self->c_array[i]);
- new->c_array[i - 2] = self->c_array[i];
+ new->c_array[i - 2] = Py_NewRef(self->c_array[i]);
}
*new_node = (PyHamtNode*)new;
@@ -1661,8 +1630,7 @@ hamt_node_array_clone(PyHamtNode_Array *node)
/* Copy all elements from the current Array node to the new one. */
for (i = 0; i < HAMT_ARRAY_NODE_SIZE; i++) {
- Py_XINCREF(node->a_array[i]);
- clone->a_array[i] = node->a_array[i];
+ clone->a_array[i] = (PyHamtNode*)Py_XNewRef(node->a_array[i]);
}
VALIDATE_ARRAY_NODE(clone)
@@ -1719,8 +1687,7 @@ hamt_node_array_assoc(PyHamtNode_Array *self,
/* Copy all elements from the current Array node to the
new one. */
for (i = 0; i < HAMT_ARRAY_NODE_SIZE; i++) {
- Py_XINCREF(self->a_array[i]);
- new_node->a_array[i] = self->a_array[i];
+ new_node->a_array[i] = (PyHamtNode*)Py_XNewRef(self->a_array[i]);
}
assert(new_node->a_array[idx] == NULL);
@@ -1868,15 +1835,12 @@ hamt_node_array_without(PyHamtNode_Array *self,
PyObject *key = child->b_array[0];
PyObject *val = child->b_array[1];
- Py_INCREF(key);
- new->b_array[new_i] = key;
- Py_INCREF(val);
- new->b_array[new_i + 1] = val;
+ new->b_array[new_i] = Py_NewRef(key);
+ new->b_array[new_i + 1] = Py_NewRef(val);
}
else {
new->b_array[new_i] = NULL;
- Py_INCREF(node);
- new->b_array[new_i + 1] = (PyObject*)node;
+ new->b_array[new_i + 1] = Py_NewRef(node);
}
}
else {
@@ -1894,8 +1858,7 @@ hamt_node_array_without(PyHamtNode_Array *self,
/* Just copy the node into our new Bitmap */
new->b_array[new_i] = NULL;
- Py_INCREF(node);
- new->b_array[new_i + 1] = (PyObject*)node;
+ new->b_array[new_i + 1] = Py_NewRef(node);
}
new_i += 2;
@@ -2311,8 +2274,7 @@ _PyHamt_Assoc(PyHamtObject *o, PyObject *key, PyObject *val)
if (new_root == o->h_root) {
Py_DECREF(new_root);
- Py_INCREF(o);
- return o;
+ return (PyHamtObject*)Py_NewRef(o);
}
new_o = hamt_alloc();
@@ -2348,8 +2310,7 @@ _PyHamt_Without(PyHamtObject *o, PyObject *key)
case W_EMPTY:
return _PyHamt_New();
case W_NOT_FOUND:
- Py_INCREF(o);
- return o;
+ return (PyHamtObject*)Py_NewRef(o);
case W_NEWNODE: {
assert(new_root != NULL);
@@ -2476,8 +2437,7 @@ _PyHamt_New(void)
if (_empty_hamt != NULL) {
/* HAMT is an immutable object so we can easily cache an
empty instance. */
- Py_INCREF(_empty_hamt);
- return _empty_hamt;
+ return (PyHamtObject*)Py_NewRef(_empty_hamt);
}
PyHamtObject *o = hamt_alloc();
@@ -2494,8 +2454,7 @@ _PyHamt_New(void)
o->h_count = 0;
if (_empty_hamt == NULL) {
- Py_INCREF(o);
- _empty_hamt = o;
+ _empty_hamt = (PyHamtObject*)Py_NewRef(o);
}
return o;
@@ -2591,8 +2550,7 @@ hamt_baseiter_new(PyTypeObject *type, binaryfunc yield, PyHamtObject *o)
return NULL;
}
- Py_INCREF(o);
- it->hi_obj = o;
+ it->hi_obj = (PyHamtObject*)Py_NewRef(o);
it->hi_yield = yield;
hamt_iterator_init(&it->hi_iter, o->h_root);
@@ -2648,8 +2606,7 @@ PyTypeObject _PyHamtKeys_Type = {
static PyObject *
hamt_iter_yield_keys(PyObject *key, PyObject *val)
{
- Py_INCREF(key);
- return key;
+ return Py_NewRef(key);
}
PyObject *
@@ -2672,8 +2629,7 @@ PyTypeObject _PyHamtValues_Type = {
static PyObject *
hamt_iter_yield_values(PyObject *key, PyObject *val)
{
- Py_INCREF(val);
- return val;
+ return Py_NewRef(val);
}
PyObject *
@@ -2766,8 +2722,7 @@ hamt_tp_subscript(PyHamtObject *self, PyObject *key)
case F_ERROR:
return NULL;
case F_FOUND:
- Py_INCREF(val);
- return val;
+ return Py_NewRef(val);
case F_NOT_FOUND:
PyErr_SetObject(PyExc_KeyError, key);
return NULL;
@@ -2817,14 +2772,12 @@ hamt_py_get(PyHamtObject *self, PyObject *args)
case F_ERROR:
return NULL;
case F_FOUND:
- Py_INCREF(val);
- return val;
+ return Py_NewRef(val);
case F_NOT_FOUND:
if (def == NULL) {
Py_RETURN_NONE;
}
- Py_INCREF(def);
- return def;
+ return Py_NewRef(def);
default:
Py_UNREACHABLE();
}