Show More
@@ -1,54 +1,61 b'' | |||||
1 | /* |
|
1 | /* | |
2 | util.h - utility functions for interfacing with the various python APIs. |
|
2 | util.h - utility functions for interfacing with the various python APIs. | |
3 |
|
3 | |||
4 | This software may be used and distributed according to the terms of |
|
4 | This software may be used and distributed according to the terms of | |
5 | the GNU General Public License, incorporated herein by reference. |
|
5 | the GNU General Public License, incorporated herein by reference. | |
6 | */ |
|
6 | */ | |
7 |
|
7 | |||
8 | #ifndef _HG_UTIL_H_ |
|
8 | #ifndef _HG_UTIL_H_ | |
9 | #define _HG_UTIL_H_ |
|
9 | #define _HG_UTIL_H_ | |
10 |
|
10 | |||
11 | #include "compat.h" |
|
11 | #include "compat.h" | |
12 |
|
12 | |||
13 | #if PY_MAJOR_VERSION >= 3 |
|
13 | #if PY_MAJOR_VERSION >= 3 | |
14 | #define IS_PY3K |
|
14 | #define IS_PY3K | |
15 | #endif |
|
15 | #endif | |
16 |
|
16 | |||
|
17 | /* helper to switch things like string literal depending on Python version */ | |||
|
18 | #ifdef IS_PY3K | |||
|
19 | #define PY23(py2, py3) py3 | |||
|
20 | #else | |||
|
21 | #define PY23(py2, py3) py2 | |||
|
22 | #endif | |||
|
23 | ||||
17 | /* clang-format off */ |
|
24 | /* clang-format off */ | |
18 | typedef struct { |
|
25 | typedef struct { | |
19 | PyObject_HEAD |
|
26 | PyObject_HEAD | |
20 | char state; |
|
27 | char state; | |
21 | int mode; |
|
28 | int mode; | |
22 | int size; |
|
29 | int size; | |
23 | int mtime; |
|
30 | int mtime; | |
24 | } dirstateTupleObject; |
|
31 | } dirstateTupleObject; | |
25 | /* clang-format on */ |
|
32 | /* clang-format on */ | |
26 |
|
33 | |||
27 | extern PyTypeObject dirstateTupleType; |
|
34 | extern PyTypeObject dirstateTupleType; | |
28 | #define dirstate_tuple_check(op) (Py_TYPE(op) == &dirstateTupleType) |
|
35 | #define dirstate_tuple_check(op) (Py_TYPE(op) == &dirstateTupleType) | |
29 |
|
36 | |||
30 | #ifndef MIN |
|
37 | #ifndef MIN | |
31 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) |
|
38 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) | |
32 | #endif |
|
39 | #endif | |
33 | /* VC9 doesn't include bool and lacks stdbool.h based on my searching */ |
|
40 | /* VC9 doesn't include bool and lacks stdbool.h based on my searching */ | |
34 | #if defined(_MSC_VER) || __STDC_VERSION__ < 199901L |
|
41 | #if defined(_MSC_VER) || __STDC_VERSION__ < 199901L | |
35 | #define true 1 |
|
42 | #define true 1 | |
36 | #define false 0 |
|
43 | #define false 0 | |
37 | typedef unsigned char bool; |
|
44 | typedef unsigned char bool; | |
38 | #else |
|
45 | #else | |
39 | #include <stdbool.h> |
|
46 | #include <stdbool.h> | |
40 | #endif |
|
47 | #endif | |
41 |
|
48 | |||
42 | static inline PyObject *_dict_new_presized(Py_ssize_t expected_size) |
|
49 | static inline PyObject *_dict_new_presized(Py_ssize_t expected_size) | |
43 | { |
|
50 | { | |
44 | /* _PyDict_NewPresized expects a minused parameter, but it actually |
|
51 | /* _PyDict_NewPresized expects a minused parameter, but it actually | |
45 | creates a dictionary that's the nearest power of two bigger than the |
|
52 | creates a dictionary that's the nearest power of two bigger than the | |
46 | parameter. For example, with the initial minused = 1000, the |
|
53 | parameter. For example, with the initial minused = 1000, the | |
47 | dictionary created has size 1024. Of course in a lot of cases that |
|
54 | dictionary created has size 1024. Of course in a lot of cases that | |
48 | can be greater than the maximum load factor Python's dict object |
|
55 | can be greater than the maximum load factor Python's dict object | |
49 | expects (= 2/3), so as soon as we cross the threshold we'll resize |
|
56 | expects (= 2/3), so as soon as we cross the threshold we'll resize | |
50 | anyway. So create a dictionary that's at least 3/2 the size. */ |
|
57 | anyway. So create a dictionary that's at least 3/2 the size. */ | |
51 | return _PyDict_NewPresized(((1 + expected_size) / 2) * 3); |
|
58 | return _PyDict_NewPresized(((1 + expected_size) / 2) * 3); | |
52 | } |
|
59 | } | |
53 |
|
60 | |||
54 | #endif /* _HG_UTIL_H_ */ |
|
61 | #endif /* _HG_UTIL_H_ */ |
General Comments 0
You need to be logged in to leave comments.
Login now