]> git.ipfire.org Git - thirdparty/xz.git/commitdiff
liblzma: Add "restrict" to a few functions in lz_decoder.h
authorLasse Collin <lasse.collin@tukaani.org>
Tue, 25 Mar 2025 13:18:31 +0000 (15:18 +0200)
committerLasse Collin <lasse.collin@tukaani.org>
Tue, 25 Mar 2025 13:18:31 +0000 (15:18 +0200)
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

index e432c9e56b72341f84c0fa76d346a9c20660193f..ac9334adea7154ccc56f7342063d250fbd566292 100644 (file)
@@ -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;