/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/_patiencediff_c.c

  • Committer: Jelmer Vernooij
  • Date: 2018-07-13 00:24:33 UTC
  • mfrom: (7037 work)
  • mto: This revision was merged to the branch mainline in revision 7039.
  • Revision ID: jelmer@jelmer.uk-20180713002433-dr601ols49ge07fr
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
#define SENTINEL -1
47
47
 
48
48
 
49
 
/* malloc returns NULL on some platforms if you try to allocate nothing,
50
 
 * causing <https://bugs.launchpad.net/bzr/+bug/511267> and
51
 
 * <https://bugs.launchpad.net/bzr/+bug/331095>.  On glibc it passes, but
52
 
 * let's make it fail to aid testing. */
53
 
#define guarded_malloc(x) ( (x) ? malloc(x) : NULL )
54
 
 
55
49
enum {
56
50
    OP_EQUAL = 0,
57
51
    OP_INSERT,
151
145
compare_lines(struct line *a, struct line *b)
152
146
{
153
147
    return ((a->hash != b->hash)
154
 
            || PyObject_Compare(a->data, b->data));
 
148
            || PyObject_RichCompareBool(a->data, b->data, Py_EQ) == 0);
155
149
}
156
150
 
157
151
 
190
184
        hsize *= 2;
191
185
 
192
186
    /* can't be 0 */
193
 
    hashtable = (struct bucket *) guarded_malloc(sizeof(struct bucket) * hsize);
 
187
    hashtable = (struct bucket *)malloc(sizeof(struct bucket) * hsize);
194
188
    if (hashtable == NULL) {
195
189
        PyErr_NoMemory();
196
190
        return 0;
467
461
    last_a_pos = alo - 1;
468
462
    last_b_pos = blo - 1;
469
463
 
470
 
    lcs = (struct matching_line *)guarded_malloc(sizeof(struct matching_line) * (bhi - blo));
 
464
    lcs = (struct matching_line *)malloc(sizeof(struct matching_line) * (bhi - blo));
471
465
    if (lcs == NULL)
472
466
        return 0;
473
467
 
628
622
        goto error;
629
623
 
630
624
    if (bsize > 0) {
631
 
        matches = (struct matching_line *)guarded_malloc(sizeof(struct matching_line) * bsize);
 
625
        matches = (struct matching_line *)malloc(sizeof(struct matching_line) * bsize);
632
626
        if (matches == NULL)
633
627
            goto error;
634
628
 
635
 
        backpointers = (Py_ssize_t *)guarded_malloc(sizeof(Py_ssize_t) * bsize * 4);
 
629
        backpointers = (Py_ssize_t *)malloc(sizeof(Py_ssize_t) * bsize * 4);
636
630
        if (backpointers == NULL)
637
631
            goto error;
638
632
    }
694
688
    matches.count = 0;
695
689
 
696
690
    if (bsize > 0) {
697
 
        matches.matches = (struct matching_block *)guarded_malloc(sizeof(struct matching_block) * bsize);
 
691
        matches.matches = (struct matching_block *)malloc(sizeof(struct matching_block) * bsize);
698
692
        if (matches.matches == NULL)
699
693
            goto error;
700
694
 
701
 
        backpointers = (Py_ssize_t *)guarded_malloc(sizeof(Py_ssize_t) * bsize * 4);
 
695
        backpointers = (Py_ssize_t *)malloc(sizeof(Py_ssize_t) * bsize * 4);
702
696
        if (backpointers == NULL)
703
697
            goto error;
704
698
    } else {
767
761
        }
768
762
 
769
763
        if (self->bsize > 0) {
770
 
            self->backpointers = (Py_ssize_t *)guarded_malloc(sizeof(Py_ssize_t) * self->bsize * 4);
 
764
            self->backpointers = (Py_ssize_t *)malloc(sizeof(Py_ssize_t) * self->bsize * 4);
771
765
            if (self->backpointers == NULL) {
772
766
                Py_DECREF(self);
773
767
                PyErr_NoMemory();
790
784
    free(self->hashtable.table);
791
785
    delete_lines(self->b, self->bsize);
792
786
    delete_lines(self->a, self->asize);
793
 
    self->ob_type->tp_free((PyObject *)self);
 
787
    ((PyObject *)self)->ob_type->tp_free((PyObject *)self);
794
788
}
795
789
 
796
790
 
819
813
    matches.count = 0;
820
814
    if (self->bsize > 0) {
821
815
        matches.matches = (struct matching_block *)
822
 
            guarded_malloc(sizeof(struct matching_block) * self->bsize);
 
816
            malloc(sizeof(struct matching_block) * self->bsize);
823
817
        if (matches.matches == NULL)
824
818
            return PyErr_NoMemory();
825
819
    } else
900
894
    struct matching_blocks matches;
901
895
 
902
896
    matches.count = 0;
903
 
    matches.matches = (struct matching_block *)guarded_malloc(sizeof(struct matching_block) * (self->bsize + 1));
 
897
    matches.matches = (struct matching_block *)malloc(sizeof(struct matching_block) * (self->bsize + 1));
904
898
    if (matches.matches == NULL)
905
899
        return PyErr_NoMemory();
906
900
 
1005
999
        return NULL;
1006
1000
 
1007
1001
    matches.count = 0;
1008
 
    matches.matches = (struct matching_block *)guarded_malloc(sizeof(struct matching_block) * (self->bsize + 1));
 
1002
    matches.matches = (struct matching_block *)malloc(sizeof(struct matching_block) * (self->bsize + 1));
1009
1003
    if (matches.matches == NULL)
1010
1004
        return PyErr_NoMemory();
1011
1005
 
1023
1017
    matches.count++;
1024
1018
 
1025
1019
    ncodes = 0;
1026
 
    codes = (struct opcode *)guarded_malloc(sizeof(struct opcode) * matches.count * 2);
 
1020
    codes = (struct opcode *)malloc(sizeof(struct opcode) * matches.count * 2);
1027
1021
    if (codes == NULL) {
1028
1022
        free(matches.matches);
1029
1023
        return PyErr_NoMemory();
1170
1164
 
1171
1165
static PyTypeObject PatienceSequenceMatcherType = {
1172
1166
    PyObject_HEAD_INIT(NULL)
1173
 
    0,                                           /* ob_size */
1174
 
    "PatienceSequenceMatcher",                   /* tp_name */
1175
 
    sizeof(PatienceSequenceMatcher),             /* tp_basicsize */
1176
 
    0,                                           /* tp_itemsize */
1177
 
    (destructor)PatienceSequenceMatcher_dealloc, /* tp_dealloc */
1178
 
    0,                                           /* tp_print */
1179
 
    0,                                           /* tp_getattr */
1180
 
    0,                                           /* tp_setattr */
1181
 
    0,                                           /* tp_compare */
1182
 
    0,                                           /* tp_repr */
1183
 
    0,                                           /* tp_as_number */
1184
 
    0,                                           /* tp_as_sequence */
1185
 
    0,                                           /* tp_as_mapping */
1186
 
    0,                                           /* tp_hash */
1187
 
    0,                                           /* tp_call */
1188
 
    0,                                           /* tp_str */
1189
 
    0,                                           /* tp_getattro */
1190
 
    0,                                           /* tp_setattro */
1191
 
    0,                                           /* tp_as_buffer */
1192
 
    Py_TPFLAGS_DEFAULT,                          /* tp_flags*/
1193
 
    PatienceSequenceMatcher_doc,                 /* tp_doc */
1194
 
    0,                                           /* tp_traverse */
1195
 
    0,                                           /* tp_clear */
1196
 
    0,                                           /* tp_richcompare */
1197
 
    0,                                           /* tp_weaklistoffset */
1198
 
    0,                                           /* tp_iter */
1199
 
    0,                                           /* tp_iternext */
1200
 
    PatienceSequenceMatcher_methods,             /* tp_methods */
1201
 
    0,                                           /* tp_members */
1202
 
    0,                                           /* tp_getset */
1203
 
    0,                                           /* tp_base */
1204
 
    0,                                           /* tp_dict */
1205
 
    0,                                           /* tp_descr_get */
1206
 
    0,                                           /* tp_descr_set */
1207
 
    0,                                           /* tp_dictoffset */
1208
 
    0,                                           /* tp_init */
1209
 
    0,                                           /* tp_alloc */
1210
 
    PatienceSequenceMatcher_new,                 /* tp_new */
 
1167
    .tp_name = "PatienceSequenceMatcher",
 
1168
    .tp_basicsize = sizeof(PatienceSequenceMatcher),
 
1169
    .tp_dealloc = (destructor)PatienceSequenceMatcher_dealloc,
 
1170
    .tp_flags = Py_TPFLAGS_DEFAULT,
 
1171
    .tp_doc = PatienceSequenceMatcher_doc,
 
1172
    .tp_methods = PatienceSequenceMatcher_methods,
 
1173
    .tp_new = PatienceSequenceMatcher_new,
1211
1174
};
1212
1175
 
1213
1176
 
1217
1180
    {NULL, NULL}
1218
1181
};
1219
1182
 
1220
 
 
1221
 
PyMODINIT_FUNC
1222
 
init_patiencediff_c(void)
 
1183
PYMOD_INIT_FUNC(_patiencediff_c)
1223
1184
{
1224
1185
    PyObject* m;
1225
1186
 
1226
1187
    if (PyType_Ready(&PatienceSequenceMatcherType) < 0)
1227
 
        return;
1228
 
 
1229
 
    m = Py_InitModule3("_patiencediff_c", cpatiencediff_methods,
1230
 
                       "C implementation of PatienceSequenceMatcher");
1231
 
    if (m == NULL)
1232
 
      return;
1233
 
 
 
1188
        return PYMOD_ERROR;
 
1189
 
 
1190
    PYMOD_CREATE(m, "_patiencediff_c",
 
1191
                 "C implementation of PatienceSequenceMatcher",
 
1192
                 cpatiencediff_methods);
 
1193
    if (m == NULL) {
 
1194
        return PYMOD_ERROR;
 
1195
    }
1234
1196
    Py_INCREF(&PatienceSequenceMatcherType);
1235
1197
    PyModule_AddObject(m, "PatienceSequenceMatcher_c",
1236
1198
                       (PyObject *)&PatienceSequenceMatcherType);
 
1199
    return PYMOD_SUCCESS(m);
1237
1200
}
1238
1201
 
1239
1202