~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_groupcompress_pyx.pyx

  • Committer: Jelmer Vernooij
  • Date: 2011-05-10 07:46:15 UTC
  • mfrom: (5844 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5845.
  • Revision ID: jelmer@samba.org-20110510074615-eptod049ndjxc4i7
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
        unsigned long agg_offset
47
47
    struct delta_index:
48
48
        pass
49
 
    delta_index * create_delta_index(source_info *src, delta_index *old) nogil
50
 
    delta_index * create_delta_index_from_delta(source_info *delta,
51
 
                                                delta_index *old) nogil
 
49
    ctypedef enum delta_result:
 
50
        DELTA_OK
 
51
        DELTA_OUT_OF_MEMORY
 
52
        DELTA_INDEX_NEEDED
 
53
        DELTA_SOURCE_EMPTY
 
54
        DELTA_SOURCE_BAD
 
55
        DELTA_BUFFER_EMPTY
 
56
        DELTA_SIZE_TOO_BIG
 
57
    delta_result create_delta_index(source_info *src,
 
58
                                    delta_index *old,
 
59
                                    delta_index **fresh) nogil
 
60
    delta_result create_delta_index_from_delta(source_info *delta,
 
61
                                               delta_index *old,
 
62
                                               delta_index **fresh) nogil
52
63
    void free_delta_index(delta_index *index) nogil
53
 
    void *create_delta(delta_index *indexes,
54
 
             void *buf, unsigned long bufsize,
55
 
             unsigned long *delta_size, unsigned long max_delta_size) nogil
 
64
    delta_result create_delta(delta_index *indexes,
 
65
                              void *buf, unsigned long bufsize,
 
66
                              unsigned long *delta_size,
 
67
                              unsigned long max_delta_size,
 
68
                              void **delta_data) nogil
56
69
    unsigned long get_delta_hdr_size(unsigned char **datap,
57
70
                                     unsigned char *top) nogil
58
71
    unsigned long sizeof_delta_index(delta_index *index)
86
99
    return DeltaIndex(source)
87
100
 
88
101
 
 
102
cdef object _translate_delta_failure(delta_result result):
 
103
    if result == DELTA_OUT_OF_MEMORY:
 
104
        return MemoryError("Delta function failed to allocate memory")
 
105
    elif result == DELTA_INDEX_NEEDED:
 
106
        return ValueError("Delta function requires delta_index param")
 
107
    elif result == DELTA_SOURCE_EMPTY:
 
108
        return ValueError("Delta function given empty source_info param")
 
109
    elif result == DELTA_SOURCE_BAD:
 
110
        return RuntimeError("Delta function given invalid source_info param")
 
111
    elif result == DELTA_BUFFER_EMPTY:
 
112
        return ValueError("Delta function given empty buffer params")
 
113
    return AssertionError("Unrecognised delta result code: %d" % result)
 
114
 
 
115
 
89
116
cdef class DeltaIndex:
90
117
 
91
118
    # We need Pyrex 0.9.8+ to understand a 'list' definition, and this object
147
174
        cdef char *c_delta
148
175
        cdef Py_ssize_t c_delta_size
149
176
        cdef delta_index *index
 
177
        cdef delta_result res
150
178
        cdef unsigned int source_location
151
179
        cdef source_info *src
152
180
        cdef unsigned int num_indexes
165
193
        src.size = c_delta_size
166
194
        src.agg_offset = self._source_offset + unadded_bytes
167
195
        with nogil:
168
 
            index = create_delta_index_from_delta(src, self._index)
 
196
            res = create_delta_index_from_delta(src, self._index, &index)
 
197
        if res != DELTA_OK:
 
198
            raise _translate_delta_failure(res)
169
199
        self._source_offset = src.agg_offset + src.size
170
 
        if index != NULL:
 
200
        if index != self._index:
171
201
            free_delta_index(self._index)
172
202
            self._index = index
173
203
 
181
211
        cdef char *c_source
182
212
        cdef Py_ssize_t c_source_size
183
213
        cdef delta_index *index
 
214
        cdef delta_result res
184
215
        cdef unsigned int source_location
185
216
        cdef source_info *src
186
217
        cdef unsigned int num_indexes
206
237
        # We delay creating the index on the first insert
207
238
        if source_location != 0:
208
239
            with nogil:
209
 
                index = create_delta_index(src, self._index)
210
 
            if index != NULL:
 
240
                res = create_delta_index(src, self._index, &index)
 
241
            if res != DELTA_OK:
 
242
                raise _translate_delta_failure(res)
 
243
            if index != self._index:
211
244
                free_delta_index(self._index)
212
245
                self._index = index
213
246
 
214
247
    cdef _populate_first_index(self):
215
248
        cdef delta_index *index
 
249
        cdef delta_result res
216
250
        if len(self._sources) != 1 or self._index != NULL:
217
251
            raise AssertionError('_populate_first_index should only be'
218
252
                ' called when we have a single source and no index yet')
219
253
 
220
 
        # We know that self._index is already NULL, so whatever
221
 
        # create_delta_index returns is fine
 
254
        # We know that self._index is already NULL, so create_delta_index
 
255
        # will always create a new index unless there's a malloc failure
222
256
        with nogil:
223
 
            self._index = create_delta_index(&self._source_infos[0], NULL)
224
 
        assert self._index != NULL
 
257
            res = create_delta_index(&self._source_infos[0], NULL, &index)
 
258
        if res != DELTA_OK:
 
259
            raise _translate_delta_failure(res)
 
260
        self._index = index
225
261
 
226
262
    cdef _expand_sources(self):
227
263
        raise RuntimeError('if we move self._source_infos, then we need to'
238
274
        cdef void * delta
239
275
        cdef unsigned long delta_size
240
276
        cdef unsigned long c_max_delta_size
 
277
        cdef delta_result res
241
278
 
242
279
        if self._index == NULL:
243
280
            if len(self._sources) == 0:
256
293
        #       allocate the bytes into the final string
257
294
        c_max_delta_size = max_delta_size
258
295
        with nogil:
259
 
            delta = create_delta(self._index,
260
 
                                 target, target_size,
261
 
                                 &delta_size, c_max_delta_size)
 
296
            res = create_delta(self._index, target, target_size,
 
297
                               &delta_size, c_max_delta_size, &delta)
262
298
        result = None
263
 
        if delta:
 
299
        if res == DELTA_OK:
264
300
            result = PyString_FromStringAndSize(<char *>delta, delta_size)
265
301
            free(delta)
 
302
        elif res != DELTA_SIZE_TOO_BIG:
 
303
            raise _translate_delta_failure(res)
266
304
        return result
267
305
 
268
306
 
369
407
                # Copy instruction
370
408
                data = _decode_copy_instruction(data, cmd, &cp_off, &cp_size)
371
409
                if (cp_off + cp_size < cp_size or
372
 
                    cp_off + cp_size > source_size or
373
 
                    cp_size > size):
 
410
                    cp_off + cp_size > <unsigned int>source_size or
 
411
                    cp_size > <unsigned int>size):
374
412
                    failed = 1
375
413
                    break
376
414
                memcpy(out, source + cp_off, cp_size)