equal
deleted
inserted
replaced
11 #include <Python.h> |
11 #include <Python.h> |
12 #include <assert.h> |
12 #include <assert.h> |
13 #include <ctype.h> |
13 #include <ctype.h> |
14 #include <limits.h> |
14 #include <limits.h> |
15 #include <stddef.h> |
15 #include <stddef.h> |
|
16 #include <stdint.h> |
16 #include <stdlib.h> |
17 #include <stdlib.h> |
17 #include <string.h> |
18 #include <string.h> |
18 |
19 |
19 #include "bitmanipulation.h" |
20 #include "bitmanipulation.h" |
20 #include "charencode.h" |
21 #include "charencode.h" |
53 */ |
54 */ |
54 typedef struct { |
55 typedef struct { |
55 indexObject *index; |
56 indexObject *index; |
56 nodetreenode *nodes; |
57 nodetreenode *nodes; |
57 Py_ssize_t nodelen; |
58 Py_ssize_t nodelen; |
58 unsigned length; /* # nodes in use */ |
59 size_t length; /* # nodes in use */ |
59 unsigned capacity; /* # nodes allocated */ |
60 size_t capacity; /* # nodes allocated */ |
60 int depth; /* maximum depth of tree */ |
61 int depth; /* maximum depth of tree */ |
61 int splits; /* # splits performed */ |
62 int splits; /* # splits performed */ |
62 } nodetree; |
63 } nodetree; |
63 |
64 |
64 typedef struct { |
65 typedef struct { |
65 PyObject_HEAD /* ; */ |
66 PyObject_HEAD /* ; */ |
66 nodetree nt; |
67 nodetree nt; |
1534 } |
1535 } |
1535 |
1536 |
1536 static int nt_new(nodetree *self) |
1537 static int nt_new(nodetree *self) |
1537 { |
1538 { |
1538 if (self->length == self->capacity) { |
1539 if (self->length == self->capacity) { |
1539 unsigned newcapacity; |
1540 size_t newcapacity; |
1540 nodetreenode *newnodes; |
1541 nodetreenode *newnodes; |
1541 newcapacity = self->capacity * 2; |
1542 newcapacity = self->capacity * 2; |
1542 if (newcapacity >= INT_MAX / sizeof(nodetreenode)) { |
1543 if (newcapacity >= SIZE_MAX / sizeof(nodetreenode)) { |
1543 PyErr_SetString(PyExc_MemoryError, |
1544 PyErr_SetString(PyExc_MemoryError, |
1544 "overflow in nt_new"); |
1545 "overflow in nt_new"); |
1545 return -1; |
1546 return -1; |
1546 } |
1547 } |
1547 newnodes = |
1548 newnodes = |
1641 * terms of nodetree nodes. */ |
1642 * terms of nodetree nodes. */ |
1642 self->capacity = (capacity < 4 ? 4 : capacity / 2); |
1643 self->capacity = (capacity < 4 ? 4 : capacity / 2); |
1643 self->nodelen = index->nodelen; |
1644 self->nodelen = index->nodelen; |
1644 self->depth = 0; |
1645 self->depth = 0; |
1645 self->splits = 0; |
1646 self->splits = 0; |
1646 if ((size_t)self->capacity > INT_MAX / sizeof(nodetreenode)) { |
1647 if (self->capacity > SIZE_MAX / sizeof(nodetreenode)) { |
1647 PyErr_SetString(PyExc_ValueError, "overflow in init_nt"); |
1648 PyErr_SetString(PyExc_ValueError, "overflow in init_nt"); |
1648 return -1; |
1649 return -1; |
1649 } |
1650 } |
1650 self->nodes = calloc(self->capacity, sizeof(nodetreenode)); |
1651 self->nodes = calloc(self->capacity, sizeof(nodetreenode)); |
1651 if (self->nodes == NULL) { |
1652 if (self->nodes == NULL) { |