1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-09 15:50:40 -08:00

Prefer _WIDTH macros to sizeof in pdumper.c

This is a bit clearer, and should work better on hypothetical
platforms where integers have holes in their representation.
* src/pdumper.c: Since the code no longer uses CHAR_BIT,
don’t worry about whether it equals 8.
(DUMP_OFF_WIDTH): New macro.
(EMACS_RELOC_LENGTH_BITS, DUMP_RELOC_OFFSET_BITS): Use it.
(DUMP_BITSET_WORD_WIDTH): New macro.
(dump_bitsets_init, dump_bitset__bit_slot)
(dump_bitset_bit_set_p, dump_bitset__set_bit_value): Use it.
This commit is contained in:
Paul Eggert 2023-05-14 18:51:24 -07:00
parent 9f8a5989b6
commit ebf5e4ca1c

View file

@ -103,7 +103,6 @@ verify (sizeof (intptr_t) == sizeof (ptrdiff_t));
verify (sizeof (void (*) (void)) == sizeof (void *)); verify (sizeof (void (*) (void)) == sizeof (void *));
verify (sizeof (ptrdiff_t) <= sizeof (Lisp_Object)); verify (sizeof (ptrdiff_t) <= sizeof (Lisp_Object));
verify (sizeof (ptrdiff_t) <= sizeof (EMACS_INT)); verify (sizeof (ptrdiff_t) <= sizeof (EMACS_INT));
verify (CHAR_BIT == 8);
static size_t static size_t
divide_round_up (size_t x, size_t y) divide_round_up (size_t x, size_t y)
@ -133,6 +132,7 @@ static int nr_remembered_data = 0;
typedef int_least32_t dump_off; typedef int_least32_t dump_off;
#define DUMP_OFF_MIN INT_LEAST32_MIN #define DUMP_OFF_MIN INT_LEAST32_MIN
#define DUMP_OFF_MAX INT_LEAST32_MAX #define DUMP_OFF_MAX INT_LEAST32_MAX
#define DUMP_OFF_WIDTH INT_LEAST32_WIDTH
#define PRIdDUMP_OFF PRIdLEAST32 #define PRIdDUMP_OFF PRIdLEAST32
enum { EMACS_INT_XDIGITS = (EMACS_INT_WIDTH + 3) / 4 }; enum { EMACS_INT_XDIGITS = (EMACS_INT_WIDTH + 3) / 4 };
@ -222,8 +222,7 @@ enum emacs_reloc_type
enum enum
{ {
EMACS_RELOC_TYPE_BITS = 3, EMACS_RELOC_TYPE_BITS = 3,
EMACS_RELOC_LENGTH_BITS = (sizeof (dump_off) * CHAR_BIT EMACS_RELOC_LENGTH_BITS = DUMP_OFF_WIDTH - EMACS_RELOC_TYPE_BITS
- EMACS_RELOC_TYPE_BITS)
}; };
struct emacs_reloc struct emacs_reloc
@ -273,7 +272,7 @@ enum
dump. Always suitable for heap objects; may be more aligned. */ dump. Always suitable for heap objects; may be more aligned. */
DUMP_ALIGNMENT = max (GCALIGNMENT, DUMP_RELOCATION_ALIGNMENT), DUMP_ALIGNMENT = max (GCALIGNMENT, DUMP_RELOCATION_ALIGNMENT),
DUMP_RELOC_OFFSET_BITS = sizeof (dump_off) * CHAR_BIT - DUMP_RELOC_TYPE_BITS DUMP_RELOC_OFFSET_BITS = DUMP_OFF_WIDTH - DUMP_RELOC_TYPE_BITS
}; };
verify (RELOC_DUMP_TO_EMACS_LV + 8 < (1 << DUMP_RELOC_TYPE_BITS)); verify (RELOC_DUMP_TO_EMACS_LV + 8 < (1 << DUMP_RELOC_TYPE_BITS));
@ -4997,6 +4996,7 @@ dump_mmap_contiguous (struct dump_memory_map *maps, int nr_maps)
} }
typedef uint_fast32_t dump_bitset_word; typedef uint_fast32_t dump_bitset_word;
#define DUMP_BITSET_WORD_WIDTH UINT_FAST32_WIDTH
struct dump_bitset struct dump_bitset
{ {
@ -5007,9 +5007,9 @@ struct dump_bitset
static bool static bool
dump_bitsets_init (struct dump_bitset bitset[2], size_t number_bits) dump_bitsets_init (struct dump_bitset bitset[2], size_t number_bits)
{ {
int xword_size = sizeof (bitset[0].bits[0]); int xword_size = sizeof (dump_bitset_word);
int bits_per_word = xword_size * CHAR_BIT; ptrdiff_t words_needed = divide_round_up (number_bits,
ptrdiff_t words_needed = divide_round_up (number_bits, bits_per_word); DUMP_BITSET_WORD_WIDTH);
dump_bitset_word *bits = calloc (words_needed, 2 * xword_size); dump_bitset_word *bits = calloc (words_needed, 2 * xword_size);
if (!bits) if (!bits)
return false; return false;
@ -5024,9 +5024,7 @@ static dump_bitset_word *
dump_bitset__bit_slot (const struct dump_bitset *bitset, dump_bitset__bit_slot (const struct dump_bitset *bitset,
size_t bit_number) size_t bit_number)
{ {
int xword_size = sizeof (bitset->bits[0]); ptrdiff_t word_number = bit_number / DUMP_BITSET_WORD_WIDTH;
int bits_per_word = xword_size * CHAR_BIT;
ptrdiff_t word_number = bit_number / bits_per_word;
eassert (word_number < bitset->number_words); eassert (word_number < bitset->number_words);
return &bitset->bits[word_number]; return &bitset->bits[word_number];
} }
@ -5035,10 +5033,8 @@ static bool
dump_bitset_bit_set_p (const struct dump_bitset *bitset, dump_bitset_bit_set_p (const struct dump_bitset *bitset,
size_t bit_number) size_t bit_number)
{ {
unsigned xword_size = sizeof (bitset->bits[0]);
unsigned bits_per_word = xword_size * CHAR_BIT;
dump_bitset_word bit = 1; dump_bitset_word bit = 1;
bit <<= bit_number % bits_per_word; bit <<= bit_number % DUMP_BITSET_WORD_WIDTH;
return *dump_bitset__bit_slot (bitset, bit_number) & bit; return *dump_bitset__bit_slot (bitset, bit_number) & bit;
} }
@ -5047,11 +5043,9 @@ dump_bitset__set_bit_value (struct dump_bitset *bitset,
size_t bit_number, size_t bit_number,
bool bit_is_set) bool bit_is_set)
{ {
int xword_size = sizeof (bitset->bits[0]);
int bits_per_word = xword_size * CHAR_BIT;
dump_bitset_word *slot = dump_bitset__bit_slot (bitset, bit_number); dump_bitset_word *slot = dump_bitset__bit_slot (bitset, bit_number);
dump_bitset_word bit = 1; dump_bitset_word bit = 1;
bit <<= bit_number % bits_per_word; bit <<= bit_number % DUMP_BITSET_WORD_WIDTH;
if (bit_is_set) if (bit_is_set)
*slot = *slot | bit; *slot = *slot | bit;
else else