[Zodb-checkins] CVS: StandaloneZODB/bsddb3Storage/bsddb3Storage - _helper.c:1.1.2.1

Barry Warsaw barry@wooz.org
Wed, 17 Apr 2002 16:22:24 -0400


Update of /cvs-repository/StandaloneZODB/bsddb3Storage/bsddb3Storage
In directory cvs.zope.org:/tmp/cvs-serv20525/bsddb3Storage

Added Files:
      Tag: bsddb3Storage-picklelog-branch
	_helper.c 
Log Message:
Simple helper C extension which speeds up the calculation of reference
counting increments.  Provides one function, incr() which takes an
8-byte string (representing a 64-bit unsigned integer) and a Python
integer, adds them together and return the results converted back into
an 8-byte string.


=== Added File StandaloneZODB/bsddb3Storage/bsddb3Storage/_helper.c ===
#include <Python.h>

static PyObject*
helper_incr(PyObject* self, PyObject* args)
{
    PyObject *pylong, *incr, *sum;
    char *s, x[8];
    int len, res;

    if (!PyArg_ParseTuple(args, "s#O:incr", &s, &len, &incr))
        return NULL;

    assert(len == 8);

    /* there seems to be no direct route from byte array to long long, so
     * first convert it to a PyLongObject*, then convert /that/ thing to a
     * long long
     */
    pylong = _PyLong_FromByteArray(s, len,
                                   0 /* big endian */, 0 /* unsigned */);
    
    if (!pylong)
        return NULL;

    sum = PyNumber_Add(pylong, incr);
    if (!sum)
        return NULL;

    res = _PyLong_AsByteArray((PyLongObject*)sum, x, 8,
                              0 /* big endian */, 0 /* unsigned */);
    if (res < 0)
        return NULL;

    return PyString_FromStringAndSize(x, 8);
}


static PyMethodDef helper_methods[] = {
    {"incr", helper_incr, METH_VARARGS},
    {NULL, NULL}                             /* sentinel */
};


DL_EXPORT(void)
init_helper(void)
{
    (void)Py_InitModule("_helper", helper_methods);
}