Commit c0a713b0 authored by Tim Peters's avatar Tim Peters

Backported the BTree KEY_TYPE_IS_PYOBJECT and VALUE_TYPE_IS_PYOBJECT

macros from Zope3.  These allow converting some runtime tests into
compile-time specialization, and that's going to be more important for
work on speeding "lopsided merges".
parent 012e6b5b
......@@ -12,7 +12,7 @@
****************************************************************************/
#define BTREETEMPLATE_C "$Id: BTreeTemplate.c,v 1.68 2002/06/25 16:23:26 tim_one Exp $\n"
#define BTREETEMPLATE_C "$Id: BTreeTemplate.c,v 1.69 2002/06/27 00:32:54 tim_one Exp $\n"
/* Sanity-check a BTree. This is a private helper for BTree_check. Return:
* -1 Error. If it's an internal inconsistency in the BTree,
......@@ -548,7 +548,9 @@ _BTree_clear(BTree *self)
}
for (i = 1; i < len; i++) {
#ifdef KEY_TYPE_IS_PYOBJECT
DECREF_KEY(self->data[i].key);
#endif
Py_DECREF(self->data[i].child);
}
free(self->data);
......
......@@ -12,7 +12,7 @@
****************************************************************************/
#define BUCKETTEMPLATE_C "$Id: BucketTemplate.c,v 1.45 2002/06/20 15:03:28 tim_one Exp $\n"
#define BUCKETTEMPLATE_C "$Id: BucketTemplate.c,v 1.46 2002/06/27 00:32:54 tim_one Exp $\n"
/* Use BUCKET_SEARCH to find the index at which a key belongs.
* INDEX An int lvalue to hold the index i such that KEY belongs at
......@@ -855,35 +855,41 @@ bucket_byValue(Bucket *self, PyObject *args)
static int
_bucket_clear(Bucket *self)
{
int i;
const int len = self->len;
/* Don't declare i at this level. If neither keys nor values are
* PyObject*, i won't be referenced, and you'll get a nuisance compiler
* wng for declaring it here.
*/
self->len = self->size = 0;
if (self->next)
{
if (self->next) {
Py_DECREF(self->next);
self->next=0;
self->next = NULL;
}
for (i=self->len; --i >= 0; )
{
/* Silence compiler warning about unused variable len for the case
when neither key nor value is an object, i.e. II. */
(void)len;
if (self->keys) {
#ifdef KEY_TYPE_IS_PYOBJECT
int i;
for (i = 0; i < len; ++i)
DECREF_KEY(self->keys[i]);
if (self->values)
{
DECREF_VALUE(self->values[i]);
}
}
self->len=0;
if (self->values)
{
free(self->values);
self->values=0;
}
if (self->keys)
{
#endif
free(self->keys);
self->keys=0;
self->keys = NULL;
}
self->size=0;
if (self->values) {
#ifdef VALUE_TYPE_IS_PYOBJECT
int i;
for (i = 0; i < len; ++i)
DECREF_VALUE(self->values[i]);
#endif
free(self->values);
self->values = NULL;
}
return 0;
}
......
......@@ -35,6 +35,9 @@ Macros for Keys
KEY_TYPE
The C type declaration for keys (e.g., int or PyObject*).
KEY_TYPE_IS_PYOBJECT
Define if KEY_TYPE is a PyObject*, else undef.
KEY_CHECK(K)
Tests whether the PyObject* K can be converted to the (C) key type
(KEY_TYPE). The macro should return a boolean (zero for false,
......@@ -84,6 +87,9 @@ Macros for Values
VALUE_TYPE
The C type declaration for values (e.g., int or PyObject*).
VALUE_TYPE_IS_PYOBJECT
Define if VALUE_TYPE is a PyObject*, else undef.
TEST_VALUE(X, Y)
Like Python's cmp(). Compares X to Y, where X & Y are C values of
type VALUE_TYPE. The macro returns an int, with value
......
......@@ -15,7 +15,7 @@ typedef unsigned char char6[6];
/* Setup template macros */
#define MASTER_ID "$Id: _fsBTree.c,v 1.3 2002/05/31 09:41:07 htrd Exp $\n"
#define MASTER_ID "$Id: _fsBTree.c,v 1.4 2002/06/27 00:32:54 tim_one Exp $\n"
#define PERSISTENT
......@@ -26,8 +26,9 @@ typedef unsigned char char6[6];
/*#include "intkeymacros.h"*/
#define KEYMACROS_H "$Id: _fsBTree.c,v 1.3 2002/05/31 09:41:07 htrd Exp $\n"
#define KEYMACROS_H "$Id: _fsBTree.c,v 1.4 2002/06/27 00:32:54 tim_one Exp $\n"
#define KEY_TYPE char2
#undef KEY_TYPE_IS_PYOBJECT
#define KEY_CHECK(K) (PyString_Check(K) && PyString_GET_SIZE(K)==2)
#define TEST_KEY_SET_OR(V, K, T) if ( ( (V) = ((*(K) < *(T) || (*(K) == *(T) && (K)[1] < (T)[1])) ? -1 : ((*(K) == *(T) && (K)[1] == (T)[1]) ? 0 : 1)) ), 0 )
#define DECREF_KEY(KEY)
......@@ -40,8 +41,9 @@ typedef unsigned char char6[6];
(STATUS)=0; }
/*#include "intvaluemacros.h"*/
#define VALUEMACROS_H "$Id: _fsBTree.c,v 1.3 2002/05/31 09:41:07 htrd Exp $\n"
#define VALUEMACROS_H "$Id: _fsBTree.c,v 1.4 2002/06/27 00:32:54 tim_one Exp $\n"
#define VALUE_TYPE char6
#undef VALUE_TYPE_IS_PYOBJECT
#define TEST_VALUE(K, T) strncmp(K,T,6)
#define DECLARE_VALUE(NAME) VALUE_TYPE NAME
#define DECREF_VALUE(k)
......
#define KEYMACROS_H "$Id: intkeymacros.h,v 1.9 2002/06/25 02:00:55 tim_one Exp $\n"
#define KEYMACROS_H "$Id: intkeymacros.h,v 1.10 2002/06/27 00:32:54 tim_one Exp $\n"
#define KEY_TYPE int
#undef KEY_TYPE_IS_PYOBJECT
#define KEY_CHECK PyInt_Check
#define TEST_KEY_SET_OR(V, K, T) if ( ( (V) = (((K) < (T)) ? -1 : (((K) > (T)) ? 1: 0)) ) , 0 )
#define DECREF_KEY(KEY)
......
#define VALUEMACROS_H "$Id: intvaluemacros.h,v 1.7 2001/04/03 15:02:17 jim Exp $\n"
#define VALUEMACROS_H "$Id: intvaluemacros.h,v 1.8 2002/06/27 00:32:54 tim_one Exp $\n"
#define VALUE_TYPE int
#undef VALUE_TYPE_IS_PYOBJECT
#define TEST_VALUE(K, T) (((K) < (T)) ? -1 : (((K) > (T)) ? 1: 0))
#define VALUE_SAME(VALUE, TARGET) ( (VALUE) == (TARGET) )
#define DECLARE_VALUE(NAME) VALUE_TYPE NAME
......
#define KEYMACROS_H "$Id: objectkeymacros.h,v 1.3 2002/05/31 09:41:07 htrd Exp $\n"
#define KEYMACROS_H "$Id: objectkeymacros.h,v 1.4 2002/06/27 00:32:54 tim_one Exp $\n"
#define KEY_TYPE PyObject *
#define KEY_TYPE_IS_PYOBJECT
#define TEST_KEY_SET_OR(V, KEY, TARGET) if ( ( (V) = PyObject_Compare((KEY),(TARGET)) ), PyErr_Occurred() )
#define INCREF_KEY(k) Py_INCREF(k)
#define DECREF_KEY(KEY) Py_DECREF(KEY)
......
#define VALUEMACROS_H "$Id: objectvaluemacros.h,v 1.3 2001/03/20 13:52:00 jim Exp $\n"
#define VALUEMACROS_H "$Id: objectvaluemacros.h,v 1.4 2002/06/27 00:32:54 tim_one Exp $\n"
#define VALUE_TYPE PyObject *
#define VALUE_TYPE_IS_PYOBJECT
#define TEST_VALUE(VALUE, TARGET) PyObject_Compare((VALUE),(TARGET))
#define DECLARE_VALUE(NAME) VALUE_TYPE NAME
#define INCREF_VALUE(k) Py_INCREF(k)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment