mercurial/cext/util.h
author Simon Sapin <simon.sapin@octobus.net>
Sun, 03 Oct 2021 13:18:03 +0200
changeset 48221 a32a96079e2d
parent 48138 38488d488ec1
child 48232 f7fd629ffb98
permissions -rw-r--r--
dirstate-v2: initial Python parser The dirstate-v2 file format should be supported even if Rust extensions are not enabled. This changeset adds parsing code that is not used yet. Differential Revision: https://phab.mercurial-scm.org/D11518
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11358
4494fb02d549 util.h: Utility macros for handling different Python APIs.
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
     1
/*
4494fb02d549 util.h: Utility macros for handling different Python APIs.
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
     2
 util.h - utility functions for interfacing with the various python APIs.
4494fb02d549 util.h: Utility macros for handling different Python APIs.
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
     3
4494fb02d549 util.h: Utility macros for handling different Python APIs.
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
     4
 This software may be used and distributed according to the terms of
4494fb02d549 util.h: Utility macros for handling different Python APIs.
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
     5
 the GNU General Public License, incorporated herein by reference.
4494fb02d549 util.h: Utility macros for handling different Python APIs.
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
     6
*/
4494fb02d549 util.h: Utility macros for handling different Python APIs.
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
     7
4494fb02d549 util.h: Utility macros for handling different Python APIs.
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
     8
#ifndef _HG_UTIL_H_
4494fb02d549 util.h: Utility macros for handling different Python APIs.
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
     9
#define _HG_UTIL_H_
4494fb02d549 util.h: Utility macros for handling different Python APIs.
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    10
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents: 25076
diff changeset
    11
#include "compat.h"
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents: 25076
diff changeset
    12
11358
4494fb02d549 util.h: Utility macros for handling different Python APIs.
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    13
#if PY_MAJOR_VERSION >= 3
4494fb02d549 util.h: Utility macros for handling different Python APIs.
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    14
#define IS_PY3K
30112
9b6ff0f940ed parsers: move PyInt aliasing out of util.h
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30101
diff changeset
    15
#endif
11358
4494fb02d549 util.h: Utility macros for handling different Python APIs.
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    16
36618
9a639a33ad1f py3: add PY23() macro to switch string literal depending on python version
Yuya Nishihara <yuya@tcha.org>
parents: 35775
diff changeset
    17
/* helper to switch things like string literal depending on Python version */
9a639a33ad1f py3: add PY23() macro to switch string literal depending on python version
Yuya Nishihara <yuya@tcha.org>
parents: 35775
diff changeset
    18
#ifdef IS_PY3K
9a639a33ad1f py3: add PY23() macro to switch string literal depending on python version
Yuya Nishihara <yuya@tcha.org>
parents: 35775
diff changeset
    19
#define PY23(py2, py3) py3
9a639a33ad1f py3: add PY23() macro to switch string literal depending on python version
Yuya Nishihara <yuya@tcha.org>
parents: 35775
diff changeset
    20
#else
9a639a33ad1f py3: add PY23() macro to switch string literal depending on python version
Yuya Nishihara <yuya@tcha.org>
parents: 35775
diff changeset
    21
#define PY23(py2, py3) py2
9a639a33ad1f py3: add PY23() macro to switch string literal depending on python version
Yuya Nishihara <yuya@tcha.org>
parents: 35775
diff changeset
    22
#endif
9a639a33ad1f py3: add PY23() macro to switch string literal depending on python version
Yuya Nishihara <yuya@tcha.org>
parents: 35775
diff changeset
    23
34635
3455e2e2ce9b util: add clang-format control comment around struct and format macro
Augie Fackler <augie@google.com>
parents: 33758
diff changeset
    24
/* clang-format off */
21809
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 19833
diff changeset
    25
typedef struct {
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 19833
diff changeset
    26
	PyObject_HEAD
47975
ec178161a8d1 dirstate: make dirstate flags char be unsigned
Martin von Zweigbergk <martinvonz@google.com>
parents: 47948
diff changeset
    27
	unsigned char flags;
21809
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 19833
diff changeset
    28
	int mode;
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 19833
diff changeset
    29
	int size;
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 19833
diff changeset
    30
	int mtime;
47539
84391ddf4c78 dirstate-item: rename the class to DirstateItem
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 40598
diff changeset
    31
} dirstateItemObject;
34635
3455e2e2ce9b util: add clang-format control comment around struct and format macro
Augie Fackler <augie@google.com>
parents: 33758
diff changeset
    32
/* clang-format on */
21809
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 19833
diff changeset
    33
47975
ec178161a8d1 dirstate: make dirstate flags char be unsigned
Martin von Zweigbergk <martinvonz@google.com>
parents: 47948
diff changeset
    34
static const unsigned char dirstate_flag_wc_tracked = 1;
ec178161a8d1 dirstate: make dirstate flags char be unsigned
Martin von Zweigbergk <martinvonz@google.com>
parents: 47948
diff changeset
    35
static const unsigned char dirstate_flag_p1_tracked = 1 << 1;
48138
38488d488ec1 dirstate-item: change the internal storage and constructor value
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48025
diff changeset
    36
static const unsigned char dirstate_flag_p2_info = 1 << 2;
38488d488ec1 dirstate-item: change the internal storage and constructor value
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48025
diff changeset
    37
static const unsigned char dirstate_flag_has_meaningful_data = 1 << 3;
38488d488ec1 dirstate-item: change the internal storage and constructor value
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48025
diff changeset
    38
static const unsigned char dirstate_flag_has_meaningful_mtime = 1 << 4;
48221
a32a96079e2d dirstate-v2: initial Python parser
Simon Sapin <simon.sapin@octobus.net>
parents: 48138
diff changeset
    39
static const unsigned char dirstate_flag_mode_exec_perm = 1 << 5;
a32a96079e2d dirstate-v2: initial Python parser
Simon Sapin <simon.sapin@octobus.net>
parents: 48138
diff changeset
    40
static const unsigned char dirstate_flag_mode_is_symlink = 1 << 6;
47948
83f0e93ec34b dirstate-item: move the C implementation to the same logic
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 47539
diff changeset
    41
47539
84391ddf4c78 dirstate-item: rename the class to DirstateItem
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 40598
diff changeset
    42
extern PyTypeObject dirstateItemType;
84391ddf4c78 dirstate-item: rename the class to DirstateItem
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 40598
diff changeset
    43
#define dirstate_tuple_check(op) (Py_TYPE(op) == &dirstateItemType)
21809
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 19833
diff changeset
    44
35775
440e8fce29e7 cext: define MIN macro only if it is not yet defined
André Sintzoff <andre.sintzoff@gmail.com>
parents: 34635
diff changeset
    45
#ifndef MIN
34635
3455e2e2ce9b util: add clang-format control comment around struct and format macro
Augie Fackler <augie@google.com>
parents: 33758
diff changeset
    46
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
35775
440e8fce29e7 cext: define MIN macro only if it is not yet defined
André Sintzoff <andre.sintzoff@gmail.com>
parents: 34635
diff changeset
    47
#endif
24442
98042b0e19f9 manifest: move C bool polyfill into util.h
Matt Mackall <mpm@selenic.com>
parents: 24016
diff changeset
    48
/* VC9 doesn't include bool and lacks stdbool.h based on my searching */
24829
8e9f8d2a2c0c util: fix the check for non-C99 compilers (issue4605)
Kevin Bullock <kbullock@ringworld.org>
parents: 24823
diff changeset
    49
#if defined(_MSC_VER) || __STDC_VERSION__ < 199901L
24442
98042b0e19f9 manifest: move C bool polyfill into util.h
Matt Mackall <mpm@selenic.com>
parents: 24016
diff changeset
    50
#define true 1
98042b0e19f9 manifest: move C bool polyfill into util.h
Matt Mackall <mpm@selenic.com>
parents: 24016
diff changeset
    51
#define false 0
98042b0e19f9 manifest: move C bool polyfill into util.h
Matt Mackall <mpm@selenic.com>
parents: 24016
diff changeset
    52
typedef unsigned char bool;
98042b0e19f9 manifest: move C bool polyfill into util.h
Matt Mackall <mpm@selenic.com>
parents: 24016
diff changeset
    53
#else
98042b0e19f9 manifest: move C bool polyfill into util.h
Matt Mackall <mpm@selenic.com>
parents: 24016
diff changeset
    54
#include <stdbool.h>
98042b0e19f9 manifest: move C bool polyfill into util.h
Matt Mackall <mpm@selenic.com>
parents: 24016
diff changeset
    55
#endif
98042b0e19f9 manifest: move C bool polyfill into util.h
Matt Mackall <mpm@selenic.com>
parents: 24016
diff changeset
    56
33756
5866ba5e9c48 cext: move _dict_new_presized() to header
Yuya Nishihara <yuya@tcha.org>
parents: 32386
diff changeset
    57
static inline PyObject *_dict_new_presized(Py_ssize_t expected_size)
5866ba5e9c48 cext: move _dict_new_presized() to header
Yuya Nishihara <yuya@tcha.org>
parents: 32386
diff changeset
    58
{
5866ba5e9c48 cext: move _dict_new_presized() to header
Yuya Nishihara <yuya@tcha.org>
parents: 32386
diff changeset
    59
	/* _PyDict_NewPresized expects a minused parameter, but it actually
5866ba5e9c48 cext: move _dict_new_presized() to header
Yuya Nishihara <yuya@tcha.org>
parents: 32386
diff changeset
    60
	   creates a dictionary that's the nearest power of two bigger than the
5866ba5e9c48 cext: move _dict_new_presized() to header
Yuya Nishihara <yuya@tcha.org>
parents: 32386
diff changeset
    61
	   parameter. For example, with the initial minused = 1000, the
5866ba5e9c48 cext: move _dict_new_presized() to header
Yuya Nishihara <yuya@tcha.org>
parents: 32386
diff changeset
    62
	   dictionary created has size 1024. Of course in a lot of cases that
5866ba5e9c48 cext: move _dict_new_presized() to header
Yuya Nishihara <yuya@tcha.org>
parents: 32386
diff changeset
    63
	   can be greater than the maximum load factor Python's dict object
5866ba5e9c48 cext: move _dict_new_presized() to header
Yuya Nishihara <yuya@tcha.org>
parents: 32386
diff changeset
    64
	   expects (= 2/3), so as soon as we cross the threshold we'll resize
5866ba5e9c48 cext: move _dict_new_presized() to header
Yuya Nishihara <yuya@tcha.org>
parents: 32386
diff changeset
    65
	   anyway. So create a dictionary that's at least 3/2 the size. */
5866ba5e9c48 cext: move _dict_new_presized() to header
Yuya Nishihara <yuya@tcha.org>
parents: 32386
diff changeset
    66
	return _PyDict_NewPresized(((1 + expected_size) / 2) * 3);
5866ba5e9c48 cext: move _dict_new_presized() to header
Yuya Nishihara <yuya@tcha.org>
parents: 32386
diff changeset
    67
}
5866ba5e9c48 cext: move _dict_new_presized() to header
Yuya Nishihara <yuya@tcha.org>
parents: 32386
diff changeset
    68
40598
fa33196088c4 revlog: replace PyInt_AS_LONG with a more portable helper function
Augie Fackler <augie@google.com>
parents: 36618
diff changeset
    69
/* Convert a PyInt or PyLong to a long. Returns false if there is an
fa33196088c4 revlog: replace PyInt_AS_LONG with a more portable helper function
Augie Fackler <augie@google.com>
parents: 36618
diff changeset
    70
   error, in which case an exception will already have been set. */
fa33196088c4 revlog: replace PyInt_AS_LONG with a more portable helper function
Augie Fackler <augie@google.com>
parents: 36618
diff changeset
    71
static inline bool pylong_to_long(PyObject *pylong, long *out)
fa33196088c4 revlog: replace PyInt_AS_LONG with a more portable helper function
Augie Fackler <augie@google.com>
parents: 36618
diff changeset
    72
{
fa33196088c4 revlog: replace PyInt_AS_LONG with a more portable helper function
Augie Fackler <augie@google.com>
parents: 36618
diff changeset
    73
	*out = PyLong_AsLong(pylong);
fa33196088c4 revlog: replace PyInt_AS_LONG with a more portable helper function
Augie Fackler <augie@google.com>
parents: 36618
diff changeset
    74
	/* Fast path to avoid hitting PyErr_Occurred if the value was obviously
fa33196088c4 revlog: replace PyInt_AS_LONG with a more portable helper function
Augie Fackler <augie@google.com>
parents: 36618
diff changeset
    75
	 * not an error. */
fa33196088c4 revlog: replace PyInt_AS_LONG with a more portable helper function
Augie Fackler <augie@google.com>
parents: 36618
diff changeset
    76
	if (*out != -1) {
fa33196088c4 revlog: replace PyInt_AS_LONG with a more portable helper function
Augie Fackler <augie@google.com>
parents: 36618
diff changeset
    77
		return true;
fa33196088c4 revlog: replace PyInt_AS_LONG with a more portable helper function
Augie Fackler <augie@google.com>
parents: 36618
diff changeset
    78
	}
fa33196088c4 revlog: replace PyInt_AS_LONG with a more portable helper function
Augie Fackler <augie@google.com>
parents: 36618
diff changeset
    79
	return PyErr_Occurred() == NULL;
fa33196088c4 revlog: replace PyInt_AS_LONG with a more portable helper function
Augie Fackler <augie@google.com>
parents: 36618
diff changeset
    80
}
fa33196088c4 revlog: replace PyInt_AS_LONG with a more portable helper function
Augie Fackler <augie@google.com>
parents: 36618
diff changeset
    81
11358
4494fb02d549 util.h: Utility macros for handling different Python APIs.
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    82
#endif /* _HG_UTIL_H_ */