Pārlūkot izejas kodu

Implemented checksum() in _speedups.c

Jonas Borgström 15 gadi atpakaļ
vecāks
revīzija
61cbaf8642
2 mainītis faili ar 31 papildinājumiem un 9 dzēšanām
  1. 30 8
      dedupestore/_speedups.c
  2. 1 1
      setup.py

+ 30 - 8
dedupestore/_speedup.c → dedupestore/_speedups.c

@@ -87,28 +87,50 @@ chunkify(PyObject *self, PyObject *args)
     return NULL;
   }
 
-  p->m = 10;
-  p->i = 0;
-  p->fd = fd;
-  p->chunk_size = chunk_size;
-  p->chunks = chunks;
-  return (PyObject *)p;
+    p->m = 10;
+    p->i = 0;
+    p->fd = fd;
+    p->chunk_size = chunk_size;
+    p->chunks = chunks;
+    return (PyObject *)p;
+  }
+
+  static PyObject *
+  checksum(PyObject *self, PyObject *args)
+  {
+    unsigned long int sum = 0, s1, s2;
+    PyObject *data;
+    Py_ssize_t i, len;
+    const char *ptr;
+    if (!PyArg_ParseTuple(args, "O|l", &data, &sum))  return NULL;
+    len = PyString_Size(data);
+    ptr = PyString_AsString(data);
+    s1 = sum & 0xffff;
+    s2 = sum >> 16;
+    printf("Woot %lu\n", sizeof(s1));
+    for(i=0; i < len; i++)
+    {
+        s1 += ptr[i] + 1;
+        s2 += s1;
+    }
+    return PyInt_FromLong(((s2 & 0xffff) << 16) | (s1 & 0xffff));
 }
 
 static PyMethodDef ChunkifierMethods[] = {
     {"chunkify",  chunkify, METH_VARARGS, ""},
+    {"checksum",  checksum, METH_VARARGS, ""},
     {NULL, NULL, 0, NULL}        /* Sentinel */
 };
 
 PyMODINIT_FUNC
-init_chunkifier(void)
+init_speedups(void)
 {
   PyObject* m;
 
   ChunkifyIterType.tp_new = PyType_GenericNew;
   if (PyType_Ready(&ChunkifyIterType) < 0)  return;
 
-  m = Py_InitModule("_chunkifier", ChunkifierMethods);
+  m = Py_InitModule("_speedups", ChunkifierMethods);
 
   Py_INCREF(&ChunkifyIterType);
   PyModule_AddObject(m, "_ChunkifyIter", (PyObject *)&ChunkifyIterType);

+ 1 - 1
setup.py

@@ -8,6 +8,6 @@ setup(name='Dedupestore',
       author='Jonas Borgström',
       author_email='jonas@borgstrom.se',
       packages=['dedupestore'],
-      ext_modules=[Extension('_speedup', ['deduepstore/_speedup.c'])],
+      ext_modules=[Extension('_speedups', ['dedupestore/_speedups.c'])],
      )