Mercurial > public > mercurial-scm > hg
annotate mercurial/mpatch.c @ 495:e94cebc60d96
Pull from TAH
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Pull from TAH
manifest hash: 08b55e07198b8a4272753895fe3727d9f52ebb75
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)
iD8DBQFCwO5YywK+sNU5EO8RArUtAJ9gsYYNbZH22COOz9B7ppwgCDxM8ACfYWcl
n9Dim3Z7qepGUXvRDEeFZ9c=
=JMQx
-----END PGP SIGNATURE-----
author | mpm@selenic.com |
---|---|
date | Mon, 27 Jun 2005 22:29:44 -0800 |
parents | 098d1f039c18 aa3d592df9b9 |
children | 7f3fc8fd427e |
rev | line source |
---|---|
72 | 1 /* |
2 mpatch.c - efficient binary patching for Mercurial | |
3 | |
4 This implements a patch algorithm that's O(m + nlog n) where m is the | |
5 size of the output and n is the number of patches. | |
6 | |
7 Given a list of binary patches, it unpacks each into a hunk list, | |
8 then combines the hunk lists with a treewise recursion to form a | |
9 single hunk list. This hunk list is then applied to the original | |
10 text. | |
11 | |
12 The text (or binary) fragments are copied directly from their source | |
13 Python objects into a preallocated output string to avoid the | |
14 allocation of intermediate Python objects. Working memory is about 2x | |
15 the total number of hunks. | |
16 | |
17 Copyright 2005 Matt Mackall <mpm@selenic.com> | |
18 | |
19 This software may be used and distributed according to the terms | |
20 of the GNU General Public License, incorporated herein by reference. | |
21 */ | |
22 | |
23 #include <Python.h> | |
24 #include <stdlib.h> | |
25 #include <string.h> | |
410
7c678976df3e
Make mpatch.c compilable under the other `OS'
mpm@selenic.com
parents:
384
diff
changeset
|
26 #ifdef _WIN32 |
411
9e9f7ab43ce2
Add 'other OS' bits to bdiff.c / style cleanups
mpm@selenic.com
parents:
410
diff
changeset
|
27 static uint32_t ntohl(uint32_t x) |
9e9f7ab43ce2
Add 'other OS' bits to bdiff.c / style cleanups
mpm@selenic.com
parents:
410
diff
changeset
|
28 { |
9e9f7ab43ce2
Add 'other OS' bits to bdiff.c / style cleanups
mpm@selenic.com
parents:
410
diff
changeset
|
29 return ((x & 0x000000ffUL) << 24) | |
9e9f7ab43ce2
Add 'other OS' bits to bdiff.c / style cleanups
mpm@selenic.com
parents:
410
diff
changeset
|
30 ((x & 0x0000ff00UL) << 8) | |
9e9f7ab43ce2
Add 'other OS' bits to bdiff.c / style cleanups
mpm@selenic.com
parents:
410
diff
changeset
|
31 ((x & 0x00ff0000UL) >> 8) | |
9e9f7ab43ce2
Add 'other OS' bits to bdiff.c / style cleanups
mpm@selenic.com
parents:
410
diff
changeset
|
32 ((x & 0xff000000UL) >> 24); |
410
7c678976df3e
Make mpatch.c compilable under the other `OS'
mpm@selenic.com
parents:
384
diff
changeset
|
33 } |
7c678976df3e
Make mpatch.c compilable under the other `OS'
mpm@selenic.com
parents:
384
diff
changeset
|
34 #else |
472 | 35 #include <netinet/in.h> |
410
7c678976df3e
Make mpatch.c compilable under the other `OS'
mpm@selenic.com
parents:
384
diff
changeset
|
36 #endif |
72 | 37 |
38 static char mpatch_doc[] = "Efficient binary patching."; | |
39 | |
40 struct frag { | |
41 int start, end, len; | |
42 char *data; | |
43 }; | |
44 | |
45 struct flist { | |
46 struct frag *base, *head, *tail; | |
47 }; | |
48 | |
49 static struct flist *lalloc(int size) | |
50 { | |
128 | 51 struct flist *a = NULL; |
72 | 52 |
53 a = malloc(sizeof(struct flist)); | |
128 | 54 if (a) { |
55 a->base = malloc(sizeof(struct frag) * size); | |
282 | 56 if (!a->base) { |
128 | 57 free(a); |
282 | 58 a = NULL; |
59 } else | |
128 | 60 a->head = a->tail = a->base; |
61 } | |
72 | 62 return a; |
63 } | |
64 | |
65 static void lfree(struct flist *a) | |
66 { | |
128 | 67 if (a) { |
68 free(a->base); | |
69 free(a); | |
70 } | |
72 | 71 } |
72 | |
73 static int lsize(struct flist *a) | |
74 { | |
75 return a->tail - a->head; | |
76 } | |
77 | |
78 /* move hunks in source that are less cut to dest, compensating | |
79 for changes in offset. the last hunk may be split if necessary. | |
80 */ | |
81 static int gather(struct flist *dest, struct flist *src, int cut, int offset) | |
82 { | |
83 struct frag *d = dest->tail, *s = src->head; | |
84 int postend, c, l; | |
85 | |
86 while (s != src->tail) { | |
87 if (s->start + offset >= cut) | |
82 | 88 break; /* we've gone far enough */ |
72 | 89 |
90 postend = offset + s->start + s->len; | |
91 if (postend <= cut) { | |
92 /* save this hunk */ | |
93 offset += s->start + s->len - s->end; | |
94 *d++ = *s++; | |
95 } | |
96 else { | |
97 /* break up this hunk */ | |
98 c = cut - offset; | |
99 if (s->end < c) | |
100 c = s->end; | |
101 l = cut - offset - s->start; | |
102 if (s->len < l) | |
103 l = s->len; | |
104 | |
105 offset += s->start + l - c; | |
106 | |
107 d->start = s->start; | |
108 d->end = c; | |
109 d->len = l; | |
110 d->data = s->data; | |
111 d++; | |
112 s->start = c; | |
113 s->len = s->len - l; | |
114 s->data = s->data + l; | |
115 | |
82 | 116 break; |
72 | 117 } |
118 } | |
119 | |
120 dest->tail = d; | |
121 src->head = s; | |
122 return offset; | |
123 } | |
124 | |
125 /* like gather, but with no output list */ | |
126 static int discard(struct flist *src, int cut, int offset) | |
127 { | |
128 struct frag *s = src->head; | |
129 int postend, c, l; | |
130 | |
131 while (s != src->tail) { | |
132 if (s->start + offset >= cut) | |
82 | 133 break; |
72 | 134 |
135 postend = offset + s->start + s->len; | |
136 if (postend <= cut) { | |
137 offset += s->start + s->len - s->end; | |
138 s++; | |
139 } | |
140 else { | |
141 c = cut - offset; | |
142 if (s->end < c) | |
143 c = s->end; | |
144 l = cut - offset - s->start; | |
145 if (s->len < l) | |
146 l = s->len; | |
147 | |
148 offset += s->start + l - c; | |
149 s->start = c; | |
150 s->len = s->len - l; | |
151 s->data = s->data + l; | |
152 | |
82 | 153 break; |
72 | 154 } |
155 } | |
156 | |
157 src->head = s; | |
158 return offset; | |
159 } | |
160 | |
161 /* combine hunk lists a and b, while adjusting b for offset changes in a/ | |
162 this deletes a and b and returns the resultant list. */ | |
163 static struct flist *combine(struct flist *a, struct flist *b) | |
164 { | |
128 | 165 struct flist *c = NULL; |
166 struct frag *bh, *ct; | |
72 | 167 int offset = 0, post; |
168 | |
128 | 169 if (a && b) |
170 c = lalloc((lsize(a) + lsize(b)) * 2); | |
171 | |
172 if (c) { | |
72 | 173 |
128 | 174 for (bh = b->head; bh != b->tail; bh++) { |
175 /* save old hunks */ | |
176 offset = gather(c, a, bh->start, offset); | |
72 | 177 |
128 | 178 /* discard replaced hunks */ |
179 post = discard(a, bh->end, offset); | |
72 | 180 |
128 | 181 /* insert new hunk */ |
182 ct = c->tail; | |
183 ct->start = bh->start - offset; | |
184 ct->end = bh->end - post; | |
185 ct->len = bh->len; | |
186 ct->data = bh->data; | |
187 c->tail++; | |
188 offset = post; | |
189 } | |
190 | |
191 /* hold on to tail from a */ | |
192 memcpy(c->tail, a->head, sizeof(struct frag) * lsize(a)); | |
193 c->tail += lsize(a); | |
72 | 194 } |
195 | |
196 lfree(a); | |
197 lfree(b); | |
198 return c; | |
199 } | |
200 | |
201 /* decode a binary patch into a hunk list */ | |
202 static struct flist *decode(char *bin, int len) | |
203 { | |
204 struct flist *l; | |
205 struct frag *lt; | |
206 char *end = bin + len; | |
384
a29decbf7475
mpatch: attempt to handle unpack alignment issues on Solaris
mpm@selenic.com
parents:
282
diff
changeset
|
207 char decode[12]; /* for dealing with alignment issues */ |
72 | 208 |
209 /* assume worst case size, we won't have many of these lists */ | |
210 l = lalloc(len / 12); | |
211 lt = l->tail; | |
212 | |
213 while (bin < end) { | |
384
a29decbf7475
mpatch: attempt to handle unpack alignment issues on Solaris
mpm@selenic.com
parents:
282
diff
changeset
|
214 memcpy(decode, bin, 12); |
a29decbf7475
mpatch: attempt to handle unpack alignment issues on Solaris
mpm@selenic.com
parents:
282
diff
changeset
|
215 lt->start = ntohl(*(uint32_t *)decode); |
a29decbf7475
mpatch: attempt to handle unpack alignment issues on Solaris
mpm@selenic.com
parents:
282
diff
changeset
|
216 lt->end = ntohl(*(uint32_t *)(decode + 4)); |
a29decbf7475
mpatch: attempt to handle unpack alignment issues on Solaris
mpm@selenic.com
parents:
282
diff
changeset
|
217 lt->len = ntohl(*(uint32_t *)(decode + 8)); |
72 | 218 lt->data = bin + 12; |
219 bin += 12 + lt->len; | |
220 lt++; | |
221 } | |
222 | |
223 l->tail = lt; | |
224 return l; | |
225 } | |
226 | |
227 /* calculate the size of resultant text */ | |
228 static int calcsize(int len, struct flist *l) | |
229 { | |
230 int outlen = 0, last = 0; | |
231 struct frag *f = l->head; | |
232 | |
233 while (f != l->tail) { | |
234 outlen += f->start - last; | |
235 last = f->end; | |
236 outlen += f->len; | |
237 f++; | |
238 } | |
239 | |
240 outlen += len - last; | |
241 return outlen; | |
242 } | |
243 | |
244 static void apply(char *buf, char *orig, int len, struct flist *l) | |
245 { | |
246 struct frag *f = l->head; | |
247 int last = 0; | |
248 char *p = buf; | |
249 | |
250 while (f != l->tail) { | |
251 memcpy(p, orig + last, f->start - last); | |
252 p += f->start - last; | |
253 memcpy(p, f->data, f->len); | |
254 last = f->end; | |
255 p += f->len; | |
256 f++; | |
257 } | |
258 memcpy(p, orig + last, len - last); | |
259 } | |
260 | |
261 /* recursively generate a patch of all bins between start and end */ | |
262 static struct flist *fold(PyObject *bins, int start, int end) | |
263 { | |
264 int len; | |
265 | |
266 if (start + 1 == end) { | |
267 /* trivial case, output a decoded list */ | |
268 PyObject *tmp = PyList_GetItem(bins, start); | |
128 | 269 if (!tmp) |
270 return NULL; | |
72 | 271 return decode(PyString_AsString(tmp), PyString_Size(tmp)); |
272 } | |
273 | |
274 /* divide and conquer, memory management is elsewhere */ | |
275 len = (end - start) / 2; | |
276 return combine(fold(bins, start, start + len), | |
277 fold(bins, start + len, end)); | |
278 } | |
279 | |
280 static PyObject * | |
281 patches(PyObject *self, PyObject *args) | |
282 { | |
283 PyObject *text, *bins, *result; | |
284 struct flist *patch; | |
285 char *in, *out; | |
286 int len, outlen; | |
287 | |
128 | 288 if (!PyArg_ParseTuple(args, "SO:mpatch", &text, &bins)) |
72 | 289 return NULL; |
290 | |
291 len = PyList_Size(bins); | |
292 if (!len) { | |
293 /* nothing to do */ | |
294 Py_INCREF(text); | |
295 return text; | |
296 } | |
297 | |
298 patch = fold(bins, 0, len); | |
128 | 299 if (!patch) |
300 return PyErr_NoMemory(); | |
301 | |
72 | 302 outlen = calcsize(PyString_Size(text), patch); |
303 result = PyString_FromStringAndSize(NULL, outlen); | |
128 | 304 if (result) { |
305 in = PyString_AsString(text); | |
306 out = PyString_AsString(result); | |
307 apply(out, in, PyString_Size(text), patch); | |
308 } | |
309 | |
72 | 310 lfree(patch); |
311 return result; | |
312 } | |
313 | |
314 static PyMethodDef methods[] = { | |
315 {"patches", patches, METH_VARARGS, "apply a series of patches\n"}, | |
316 {NULL, NULL} | |
317 }; | |
318 | |
319 PyMODINIT_FUNC | |
320 initmpatch(void) | |
321 { | |
322 Py_InitModule3("mpatch", methods, mpatch_doc); | |
323 } | |
324 |