mercurial/bitmanipulation.h
author Manuel Jacob <me@manueljacob.de>
Mon, 11 Jul 2022 01:51:20 +0200
branchstable
changeset 49378 094a5fa3cf52
parent 48274 d86908050375
permissions -rw-r--r--
procutil: make stream detection in make_line_buffered more correct and strict In make_line_buffered(), we don’t want to wrap the stream if we know that lines get flushed to the underlying raw stream already. Previously, the heuristic was too optimistic. It assumed that any stream which is not an instance of io.BufferedIOBase doesn’t need wrapping. However, there are buffered streams that aren’t instances of io.BufferedIOBase, like Mercurial’s own winstdout. The new logic is different in two ways: First, only for the check, if unwraps any combination of WriteAllWrapper and winstdout. Second, it skips wrapping the stream only if it is an instance of io.RawIOBase (or already wrapped). If it is an instance of io.BufferedIOBase, it gets wrapped. In any other case, the function raises an exception. This ensures that, if an unknown stream is passed or we add another wrapper in the future, we don’t wrap the stream if it’s already line buffered or not wrap the stream if it’s not line buffered. In fact, this was already helpful during development of this change. Without it, I possibly would have forgot that WriteAllWrapper needs to be ignored for the check, leading to unnecessary wrapping if stdout is unbuffered. The alternative would have been to always wrap unknown streams. However, I don’t think that anyone would benefit from being less strict. We can expect streams from the standard library to be subclassing either io.RawIOBase or io.BufferedIOBase, so running Mercurial in the standard way should not regress by this change. Py2exe might replace sys.stdout and sys.stderr, but that currently breaks Mercurial anyway and also these streams don’t claim to be interactive, so this function is not called for them.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48274
d86908050375 hg: remove reserved identifiers
David Demelier <markand@malikania.fr>
parents: 46707
diff changeset
     1
#ifndef HG_BITMANIPULATION_H
d86908050375 hg: remove reserved identifiers
David Demelier <markand@malikania.fr>
parents: 46707
diff changeset
     2
#define HG_BITMANIPULATION_H
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
     3
32646
b4356d1cf3e4 bitmanipulation: add missing include of string.h
Martin von Zweigbergk <martinvonz@google.com>
parents: 29444
diff changeset
     4
#include <string.h>
b4356d1cf3e4 bitmanipulation: add missing include of string.h
Martin von Zweigbergk <martinvonz@google.com>
parents: 29444
diff changeset
     5
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
     6
#include "compat.h"
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
     7
46707
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
     8
/* Reads a 64 bit integer from big-endian bytes. Assumes that the data is long
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
     9
 enough */
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    10
static inline uint64_t getbe64(const char *c)
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    11
{
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    12
	const unsigned char *d = (const unsigned char *)c;
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    13
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    14
	return ((((uint64_t)d[0]) << 56) | (((uint64_t)d[1]) << 48) |
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    15
	        (((uint64_t)d[2]) << 40) | (((uint64_t)d[3]) << 32) |
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    16
	        (((uint64_t)d[4]) << 24) | (((uint64_t)d[5]) << 16) |
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    17
	        (((uint64_t)d[6]) << 8) | (d[7]));
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    18
}
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    19
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    20
static inline uint32_t getbe32(const char *c)
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    21
{
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    22
	const unsigned char *d = (const unsigned char *)c;
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    23
38303
1fb2510cf8c8 bitmanipulation: fix undefined behavior in bit shift in getbe32
Augie Fackler <augie@google.com>
parents: 34697
diff changeset
    24
	return ((((uint32_t)d[0]) << 24) | (((uint32_t)d[1]) << 16) |
1fb2510cf8c8 bitmanipulation: fix undefined behavior in bit shift in getbe32
Augie Fackler <augie@google.com>
parents: 34697
diff changeset
    25
	        (((uint32_t)d[2]) << 8) | (d[3]));
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    26
}
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    27
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    28
static inline int16_t getbeint16(const char *c)
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    29
{
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    30
	const unsigned char *d = (const unsigned char *)c;
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    31
34697
ce77b0563228 bitmanipulation: reformat with clang-format
Augie Fackler <augie@google.com>
parents: 32646
diff changeset
    32
	return ((d[0] << 8) | (d[1]));
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    33
}
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    34
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    35
static inline uint16_t getbeuint16(const char *c)
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    36
{
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    37
	const unsigned char *d = (const unsigned char *)c;
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    38
34697
ce77b0563228 bitmanipulation: reformat with clang-format
Augie Fackler <augie@google.com>
parents: 32646
diff changeset
    39
	return ((d[0] << 8) | (d[1]));
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    40
}
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    41
46707
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    42
/* Writes a 64 bit integer to bytes in a big-endian format.
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    43
 Assumes that the buffer is long enough */
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    44
static inline void putbe64(uint64_t x, char *c)
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    45
{
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    46
	c[0] = (x >> 56) & 0xff;
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    47
	c[1] = (x >> 48) & 0xff;
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    48
	c[2] = (x >> 40) & 0xff;
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    49
	c[3] = (x >> 32) & 0xff;
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    50
	c[4] = (x >> 24) & 0xff;
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    51
	c[5] = (x >> 16) & 0xff;
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    52
	c[6] = (x >> 8) & 0xff;
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    53
	c[7] = (x)&0xff;
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    54
}
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    55
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    56
static inline void putbe32(uint32_t x, char *c)
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    57
{
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    58
	c[0] = (x >> 24) & 0xff;
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    59
	c[1] = (x >> 16) & 0xff;
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    60
	c[2] = (x >> 8) & 0xff;
34697
ce77b0563228 bitmanipulation: reformat with clang-format
Augie Fackler <augie@google.com>
parents: 32646
diff changeset
    61
	c[3] = (x)&0xff;
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    62
}
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    63
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    64
static inline double getbefloat64(const char *c)
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    65
{
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    66
	const unsigned char *d = (const unsigned char *)c;
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    67
	double ret;
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    68
	int i;
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    69
	uint64_t t = 0;
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    70
	for (i = 0; i < 8; i++) {
34697
ce77b0563228 bitmanipulation: reformat with clang-format
Augie Fackler <augie@google.com>
parents: 32646
diff changeset
    71
		t = (t << 8) + d[i];
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    72
	}
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    73
	memcpy(&ret, &t, sizeof(t));
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    74
	return ret;
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    75
}
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    76
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    77
#endif