~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_groupcompress_pyx.pyx

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-03-10 15:10:10 UTC
  • mfrom: (5712.2.1 trunk)
  • Revision ID: pqm@pqm.ubuntu.com-20110310151010-da17g4uls3dn7hut
(vila) Tweak release instructions (Vincent Ladeuil)

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
 
    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
 
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
63
52
    void free_delta_index(delta_index *index) 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
 
53
    void *create_delta(delta_index *indexes,
 
54
             void *buf, unsigned long bufsize,
 
55
             unsigned long *delta_size, unsigned long max_delta_size) nogil
69
56
    unsigned long get_delta_hdr_size(unsigned char **datap,
70
57
                                     unsigned char *top) nogil
71
58
    unsigned long sizeof_delta_index(delta_index *index)
99
86
    return DeltaIndex(source)
100
87
 
101
88
 
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
 
 
116
89
cdef class DeltaIndex:
117
90
 
118
91
    # We need Pyrex 0.9.8+ to understand a 'list' definition, and this object
174
147
        cdef char *c_delta
175
148
        cdef Py_ssize_t c_delta_size
176
149
        cdef delta_index *index
177
 
        cdef delta_result res
178
150
        cdef unsigned int source_location
179
151
        cdef source_info *src
180
152
        cdef unsigned int num_indexes
193
165
        src.size = c_delta_size
194
166
        src.agg_offset = self._source_offset + unadded_bytes
195
167
        with nogil:
196
 
            res = create_delta_index_from_delta(src, self._index, &index)
197
 
        if res != DELTA_OK:
198
 
            raise _translate_delta_failure(res)
 
168
            index = create_delta_index_from_delta(src, self._index)
199
169
        self._source_offset = src.agg_offset + src.size
200
 
        if index != self._index:
 
170
        if index != NULL:
201
171
            free_delta_index(self._index)
202
172
            self._index = index
203
173
 
211
181
        cdef char *c_source
212
182
        cdef Py_ssize_t c_source_size
213
183
        cdef delta_index *index
214
 
        cdef delta_result res
215
184
        cdef unsigned int source_location
216
185
        cdef source_info *src
217
186
        cdef unsigned int num_indexes
237
206
        # We delay creating the index on the first insert
238
207
        if source_location != 0:
239
208
            with nogil:
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:
 
209
                index = create_delta_index(src, self._index)
 
210
            if index != NULL:
244
211
                free_delta_index(self._index)
245
212
                self._index = index
246
213
 
247
214
    cdef _populate_first_index(self):
248
215
        cdef delta_index *index
249
 
        cdef delta_result res
250
216
        if len(self._sources) != 1 or self._index != NULL:
251
217
            raise AssertionError('_populate_first_index should only be'
252
218
                ' called when we have a single source and no index yet')
253
219
 
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
 
220
        # We know that self._index is already NULL, so whatever
 
221
        # create_delta_index returns is fine
256
222
        with nogil:
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
 
223
            self._index = create_delta_index(&self._source_infos[0], NULL)
 
224
        assert self._index != NULL
261
225
 
262
226
    cdef _expand_sources(self):
263
227
        raise RuntimeError('if we move self._source_infos, then we need to'
274
238
        cdef void * delta
275
239
        cdef unsigned long delta_size
276
240
        cdef unsigned long c_max_delta_size
277
 
        cdef delta_result res
278
241
 
279
242
        if self._index == NULL:
280
243
            if len(self._sources) == 0:
293
256
        #       allocate the bytes into the final string
294
257
        c_max_delta_size = max_delta_size
295
258
        with nogil:
296
 
            res = create_delta(self._index, target, target_size,
297
 
                               &delta_size, c_max_delta_size, &delta)
 
259
            delta = create_delta(self._index,
 
260
                                 target, target_size,
 
261
                                 &delta_size, c_max_delta_size)
298
262
        result = None
299
 
        if res == DELTA_OK:
 
263
        if delta:
300
264
            result = PyString_FromStringAndSize(<char *>delta, delta_size)
301
265
            free(delta)
302
 
        elif res != DELTA_SIZE_TOO_BIG:
303
 
            raise _translate_delta_failure(res)
304
266
        return result
305
267
 
306
268
 
407
369
                # Copy instruction
408
370
                data = _decode_copy_instruction(data, cmd, &cp_off, &cp_size)
409
371
                if (cp_off + cp_size < cp_size or
410
 
                    cp_off + cp_size > <unsigned int>source_size or
411
 
                    cp_size > <unsigned int>size):
 
372
                    cp_off + cp_size > source_size or
 
373
                    cp_size > size):
412
374
                    failed = 1
413
375
                    break
414
376
                memcpy(out, source + cp_off, cp_size)