From bc14e4c94e788d42eeab984298391fc0ca46f969 Mon Sep 17 00:00:00 2001 From: Lasse Collin Date: Tue, 25 Mar 2025 15:18:31 +0200 Subject: [PATCH] liblzma: Add "restrict" to a few functions in lz_decoder.h This doesn't make any difference in practice because compilers can already see that writing through the dict->buf pointer cannot modify the contents of *dict itself: The LZMA decoder makes a local copy of the lzma_dict structure, and even if it didn't, the pointer to lzma_dict in the LZMA decoder is already "restrict". It's nice to add "restrict" anyway. uint8_t is typically unsigned char which can alias anything. Without the above conditions or "restrict", compilers could need to assume that writing through dict->buf might modify *dict. This would matter in dict_repeat() because the loops refer to dict->buf and dict->pos instead of making local copies of those members for the duration of the loops. If compilers had to assume that writing through dict->buf can affect *dict, then compilers would need to emit code that reloads dict->buf and dict->pos after every write through dict->buf. --- src/liblzma/lz/lz_decoder.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/liblzma/lz/lz_decoder.h b/src/liblzma/lz/lz_decoder.h index e432c9e5..ac9334ad 100644 --- a/src/liblzma/lz/lz_decoder.h +++ b/src/liblzma/lz/lz_decoder.h @@ -161,7 +161,8 @@ dict_is_distance_valid(const lzma_dict *const dict, const size_t distance) /// Repeat *len bytes at distance. static inline bool -dict_repeat(lzma_dict *dict, uint32_t distance, uint32_t *len) +dict_repeat(lzma_dict *restrict dict, + uint32_t distance, uint32_t *restrict len) { // Don't write past the end of the dictionary. const size_t dict_avail = dict->limit - dict->pos; @@ -195,7 +196,7 @@ dict_repeat(lzma_dict *dict, uint32_t distance, uint32_t *len) static inline void -dict_put(lzma_dict *dict, uint8_t byte) +dict_put(lzma_dict *restrict dict, uint8_t byte) { dict->buf[dict->pos++] = byte; @@ -207,7 +208,7 @@ dict_put(lzma_dict *dict, uint8_t byte) /// Puts one byte into the dictionary. Returns true if the dictionary was /// already full and the byte couldn't be added. static inline bool -dict_put_safe(lzma_dict *dict, uint8_t byte) +dict_put_safe(lzma_dict *restrict dict, uint8_t byte) { if (unlikely(dict->pos == dict->limit)) return true;