Mercurial > hg
changeset 29693:b9b9f9a92481
mpatch: split mpatch into two files
author | Maciej Fijalkowski <fijall@gmail.com> |
---|---|
date | Mon, 18 Jul 2016 19:02:30 +0200 |
parents | 6b3a8d034b69 |
children | 55dd12204b8e |
files | mercurial/mpatch.c mercurial/mpatch.h mercurial/mpatch_module.c setup.py |
diffstat | 4 files changed, 181 insertions(+), 125 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/mpatch.c Mon Jul 18 16:25:14 2016 +0200 +++ b/mercurial/mpatch.c Mon Jul 18 19:02:30 2016 +0200 @@ -41,7 +41,7 @@ struct mpatch_frag *base, *head, *tail; }; -static struct mpatch_flist *lalloc(ssize_t size) +struct mpatch_flist *lalloc(ssize_t size) { struct mpatch_flist *a = NULL; @@ -63,7 +63,7 @@ return NULL; } -static void mpatch_lfree(struct mpatch_flist *a) +void mpatch_lfree(struct mpatch_flist *a) { if (a) { free(a->base); @@ -202,7 +202,7 @@ } /* decode a binary patch into a hunk list */ -static struct mpatch_flist *mpatch_decode(const char *bin, ssize_t len) +struct mpatch_flist *mpatch_decode(const char *bin, ssize_t len) { struct mpatch_flist *l; struct mpatch_frag *lt; @@ -238,7 +238,7 @@ } /* calculate the size of resultant text */ -static ssize_t mpatch_calcsize(ssize_t len, struct mpatch_flist *l) +ssize_t mpatch_calcsize(ssize_t len, struct mpatch_flist *l) { ssize_t outlen = 0, last = 0; struct mpatch_frag *f = l->head; @@ -260,7 +260,7 @@ return outlen; } -static int mpatch_apply(char *buf, const char *orig, ssize_t len, +int mpatch_apply(char *buf, const char *orig, ssize_t len, struct mpatch_flist *l) { struct mpatch_frag *f = l->head; @@ -286,7 +286,7 @@ } /* recursively generate a patch of all bins between start and end */ -static struct mpatch_flist *mpatch_fold(PyObject *bins, ssize_t start, +struct mpatch_flist *mpatch_fold(PyObject *bins, ssize_t start, ssize_t end) { ssize_t len, blen; @@ -308,121 +308,3 @@ mpatch_fold(bins, start + len, end)); } -static PyObject * -patches(PyObject *self, PyObject *args) -{ - PyObject *text, *bins, *result; - struct mpatch_flist *patch; - const char *in; - char *out; - Py_ssize_t len, outlen, inlen; - - if (!PyArg_ParseTuple(args, "OO:mpatch", &text, &bins)) - return NULL; - - len = PyList_Size(bins); - if (!len) { - /* nothing to do */ - Py_INCREF(text); - return text; - } - - if (PyObject_AsCharBuffer(text, &in, &inlen)) - return NULL; - - patch = mpatch_fold(bins, 0, len); - if (!patch) - return NULL; - - outlen = mpatch_calcsize(inlen, patch); - if (outlen < 0) { - result = NULL; - goto cleanup; - } - result = PyBytes_FromStringAndSize(NULL, outlen); - if (!result) { - result = NULL; - goto cleanup; - } - out = PyBytes_AsString(result); - if (!mpatch_apply(out, in, inlen, patch)) { - Py_DECREF(result); - result = NULL; - } -cleanup: - mpatch_lfree(patch); - return result; -} - -/* calculate size of a patched file directly */ -static PyObject * -patchedsize(PyObject *self, PyObject *args) -{ - long orig, start, end, len, outlen = 0, last = 0, pos = 0; - Py_ssize_t patchlen; - char *bin; - - if (!PyArg_ParseTuple(args, "ls#", &orig, &bin, &patchlen)) - return NULL; - - while (pos >= 0 && pos < patchlen) { - start = getbe32(bin + pos); - end = getbe32(bin + pos + 4); - len = getbe32(bin + pos + 8); - if (start > end) - break; /* sanity check */ - pos += 12 + len; - outlen += start - last; - last = end; - outlen += len; - } - - if (pos != patchlen) { - if (!PyErr_Occurred()) - PyErr_SetString(mpatch_Error, "patch cannot be decoded"); - return NULL; - } - - outlen += orig - last; - return Py_BuildValue("l", outlen); -} - -static PyMethodDef methods[] = { - {"patches", patches, METH_VARARGS, "apply a series of patches\n"}, - {"patchedsize", patchedsize, METH_VARARGS, "calculed patched size\n"}, - {NULL, NULL} -}; - -#ifdef IS_PY3K -static struct PyModuleDef mpatch_module = { - PyModuleDef_HEAD_INIT, - "mpatch", - mpatch_doc, - -1, - methods -}; - -PyMODINIT_FUNC PyInit_mpatch(void) -{ - PyObject *m; - - m = PyModule_Create(&mpatch_module); - if (m == NULL) - return NULL; - - mpatch_Error = PyErr_NewException("mercurial.mpatch.mpatchError", - NULL, NULL); - Py_INCREF(mpatch_Error); - PyModule_AddObject(m, "mpatchError", mpatch_Error); - - return m; -} -#else -PyMODINIT_FUNC -initmpatch(void) -{ - Py_InitModule3("mpatch", methods, mpatch_doc); - mpatch_Error = PyErr_NewException("mercurial.mpatch.mpatchError", - NULL, NULL); -} -#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mercurial/mpatch.h Mon Jul 18 19:02:30 2016 +0200 @@ -0,0 +1,20 @@ +#ifndef _HG_MPATCH_H_ +#define _HG_MPATCH_H_ + +struct mpatch_frag { + int start, end, len; + const char *data; +}; + +struct mpatch_flist { + struct mpatch_frag *base, *head, *tail; +}; + +int mpatch_decode(const char *bin, ssize_t len, struct mpatch_flist** res); +ssize_t mpatch_calcsize(ssize_t len, struct mpatch_flist *l); +void mpatch_lfree(struct mpatch_flist *a); +int mpatch_apply(char *buf, const char *orig, ssize_t len, + struct mpatch_flist *l); +struct mpatch_flist *mpatch_fold(void *bins, ssize_t start, ssize_t end); + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mercurial/mpatch_module.c Mon Jul 18 19:02:30 2016 +0200 @@ -0,0 +1,153 @@ +/* + mpatch.c - efficient binary patching for Mercurial + + This implements a patch algorithm that's O(m + nlog n) where m is the + size of the output and n is the number of patches. + + Given a list of binary patches, it unpacks each into a hunk list, + then combines the hunk lists with a treewise recursion to form a + single hunk list. This hunk list is then applied to the original + text. + + The text (or binary) fragments are copied directly from their source + Python objects into a preallocated output string to avoid the + allocation of intermediate Python objects. Working memory is about 2x + the total number of hunks. + + Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> + + This software may be used and distributed according to the terms + of the GNU General Public License, incorporated herein by reference. +*/ + +#define PY_SSIZE_T_CLEAN +#include <Python.h> +#include <stdlib.h> +#include <string.h> + +#include "util.h" +#include "bitmanipulation.h" +#include "compat.h" +#include "mpatch.h" + +static char mpatch_doc[] = "Efficient binary patching."; +static PyObject *mpatch_Error; + +static PyObject * +patches(PyObject *self, PyObject *args) +{ + PyObject *text, *bins, *result; + struct mpatch_flist *patch; + const char *in; + char *out; + Py_ssize_t len, outlen, inlen; + + if (!PyArg_ParseTuple(args, "OO:mpatch", &text, &bins)) + return NULL; + + len = PyList_Size(bins); + if (!len) { + /* nothing to do */ + Py_INCREF(text); + return text; + } + + if (PyObject_AsCharBuffer(text, &in, &inlen)) + return NULL; + + patch = mpatch_fold(bins, 0, len); + if (!patch) + return NULL; + + outlen = mpatch_calcsize(inlen, patch); + if (outlen < 0) { + result = NULL; + goto cleanup; + } + result = PyBytes_FromStringAndSize(NULL, outlen); + if (!result) { + result = NULL; + goto cleanup; + } + out = PyBytes_AsString(result); + if (!mpatch_apply(out, in, inlen, patch)) { + Py_DECREF(result); + result = NULL; + } +cleanup: + mpatch_lfree(patch); + return result; +} + +/* calculate size of a patched file directly */ +static PyObject * +patchedsize(PyObject *self, PyObject *args) +{ + long orig, start, end, len, outlen = 0, last = 0, pos = 0; + Py_ssize_t patchlen; + char *bin; + + if (!PyArg_ParseTuple(args, "ls#", &orig, &bin, &patchlen)) + return NULL; + + while (pos >= 0 && pos < patchlen) { + start = getbe32(bin + pos); + end = getbe32(bin + pos + 4); + len = getbe32(bin + pos + 8); + if (start > end) + break; /* sanity check */ + pos += 12 + len; + outlen += start - last; + last = end; + outlen += len; + } + + if (pos != patchlen) { + if (!PyErr_Occurred()) + PyErr_SetString(mpatch_Error, "patch cannot be decoded"); + return NULL; + } + + outlen += orig - last; + return Py_BuildValue("l", outlen); +} + +static PyMethodDef methods[] = { + {"patches", patches, METH_VARARGS, "apply a series of patches\n"}, + {"patchedsize", patchedsize, METH_VARARGS, "calculed patched size\n"}, + {NULL, NULL} +}; + +#ifdef IS_PY3K +static struct PyModuleDef mpatch_module = { + PyModuleDef_HEAD_INIT, + "mpatch", + mpatch_doc, + -1, + methods +}; + +PyMODINIT_FUNC PyInit_mpatch(void) +{ + PyObject *m; + + m = PyModule_Create(&mpatch_module); + if (m == NULL) + return NULL; + + mpatch_Error = PyErr_NewException("mercurial.mpatch.mpatchError", + NULL, NULL); + Py_INCREF(mpatch_Error); + PyModule_AddObject(m, "mpatchError", mpatch_Error); + + return m; +} +#else +PyMODINIT_FUNC +initmpatch(void) +{ + Py_InitModule3("mpatch", methods, mpatch_doc); + mpatch_Error = PyErr_NewException("mercurial.mpatch.mpatchError", + NULL, NULL); +} +#endif
--- a/setup.py Mon Jul 18 16:25:14 2016 +0200 +++ b/setup.py Mon Jul 18 19:02:30 2016 +0200 @@ -561,7 +561,8 @@ depends=common_depends + ['mercurial/bdiff.h']), Extension('mercurial.diffhelpers', ['mercurial/diffhelpers.c'], depends=common_depends), - Extension('mercurial.mpatch', ['mercurial/mpatch.c'], + Extension('mercurial.mpatch', ['mercurial/mpatch.c', + 'mercurial/mpatch_module.c'], depends=common_depends), Extension('mercurial.parsers', ['mercurial/dirs.c', 'mercurial/manifest.c',