exec: use char* for pointer arithmetic
PATCH 3/3 exec: use char* for pointer arithmetic
void* pointer arithmetic is not in the
C standard. This change allows using
the QEMU headers with a C++ compiler.
Google-Bug-Id: 331190993
--- include/exec/memory.h | 8 ++++---- include/exec/memory_ldst_cached.h.inc | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-)
include/exec/memory.h +4 -4
@@ -2839,7 +2839,7 @@ static inline uint8_t address_space_ldub_cached(MemoryRegionCache *cache, {assert (addr < cache->len);if (likely (cache->ptr)) { -return ldub_p (cache->ptr + addr); +return ldub_p ((char *)cache->ptr + addr); }else {return address_space_ldub_cached_slow (cache, addr, attrs, result); }
@@ -2850,7 +2850,7 @@ static inline void address_space_stb_cached(MemoryRegionCache *cache, {assert (addr < cache->len);if (likely (cache->ptr)) { -stb_p (cache->ptr + addr, val); +stb_p ((char *)cache->ptr + addr, val); }else {address_space_stb_cached_slow (cache, addr, val, attrs, result); }
@@ -3123,7 +3123,7 @@ address_space_read_cached(MemoryRegionCache *cache, hwaddr addr,assert (addr < cache->len && len <= cache->len - addr);fuzz_dma_read_cb (cache->xlat + addr, len, cache->mrs.mr);if (likely (cache->ptr)) { -memcpy (buf, cache->ptr + addr, len); +memcpy (buf, (char *)cache->ptr + addr, len);return MEMTX_OK ; }else {return address_space_read_cached_slow (cache, addr, buf, len);
@@ -3144,7 +3144,7 @@ address_space_write_cached(MemoryRegionCache *cache, hwaddr addr, {assert (addr < cache->len && len <= cache->len - addr);if (likely (cache->ptr)) { -memcpy (cache->ptr + addr, buf, len); +memcpy ((char *)cache->ptr + addr, buf, len);return MEMTX_OK ; }else {return address_space_write_cached_slow (cache, addr, buf, len);
include/exec/memory_ldst_cached.h.inc +6 -6
@@ -30,7 +30,7 @@ static inline uint16_t ADDRESS_SPACE_LD_CACHED(uw)(MemoryRegionCache *cache, assert(addr < cache->len && 2 <= cache->len - addr); fuzz_dma_read_cb(cache->xlat + addr, 2, cache->mrs.mr); if (likely(cache->ptr)) { - return LD_P(uw)(cache->ptr + addr); + return LD_P(uw)((char*)cache->ptr + addr); } else { return ADDRESS_SPACE_LD_CACHED_SLOW(uw)(cache, addr, attrs, result); }
@@ -42,7 +42,7 @@ static inline uint32_t ADDRESS_SPACE_LD_CACHED(l)(MemoryRegionCache *cache, assert(addr < cache->len && 4 <= cache->len - addr); fuzz_dma_read_cb(cache->xlat + addr, 4, cache->mrs.mr); if (likely(cache->ptr)) { - return LD_P(l)(cache->ptr + addr); + return LD_P(l)((char*)cache->ptr + addr); } else { return ADDRESS_SPACE_LD_CACHED_SLOW(l)(cache, addr, attrs, result); }
@@ -54,7 +54,7 @@ static inline uint64_t ADDRESS_SPACE_LD_CACHED(q)(MemoryRegionCache *cache, assert(addr < cache->len && 8 <= cache->len - addr); fuzz_dma_read_cb(cache->xlat + addr, 8, cache->mrs.mr); if (likely(cache->ptr)) { - return LD_P(q)(cache->ptr + addr); + return LD_P(q)((char*)cache->ptr + addr); } else { return ADDRESS_SPACE_LD_CACHED_SLOW(q)(cache, addr, attrs, result); }
@@ -76,7 +76,7 @@ static inline void ADDRESS_SPACE_ST_CACHED(w)(MemoryRegionCache *cache, { assert(addr < cache->len && 2 <= cache->len - addr); if (likely(cache->ptr)) { - ST_P(w)(cache->ptr + addr, val); + ST_P(w)((char*)cache->ptr + addr, val); } else { ADDRESS_SPACE_ST_CACHED_SLOW(w)(cache, addr, val, attrs, result); }
@@ -87,7 +87,7 @@ static inline void ADDRESS_SPACE_ST_CACHED(l)(MemoryRegionCache *cache, { assert(addr < cache->len && 4 <= cache->len - addr); if (likely(cache->ptr)) { - ST_P(l)(cache->ptr + addr, val); + ST_P(l)((char*)cache->ptr + addr, val); } else { ADDRESS_SPACE_ST_CACHED_SLOW(l)(cache, addr, val, attrs, result); }
@@ -98,7 +98,7 @@ static inline void ADDRESS_SPACE_ST_CACHED(q)(MemoryRegionCache *cache, { assert(addr < cache->len && 8 <= cache->len - addr); if (likely(cache->ptr)) { - ST_P(q)(cache->ptr + addr, val); + ST_P(q)((char*)cache->ptr + addr, val); } else { ADDRESS_SPACE_ST_CACHED_SLOW(q)(cache, addr, val, attrs, result); }
Signature · 1 line
2.45.2.627.g7a2c4fd464-goog
include/exec/memory.h static inline uint8_t address_space_ldub_cached(MemoryRegionCache *cache, · line 2842
Quoted 6 lines · Roman Kiryanov
include/exec/memory.h +1
-1@@ -2839,7 +2839,7 @@ static inline uint8_t address_space_ldub_cached(MemoryRegionCache *cache, {assert (addr < cache->len);if (likely (cache->ptr)) { -return ldub_p (cache->ptr + addr); +return ldub_p ((char *)cache->ptr + addr);
We require "char *" with a space.
With all of those fixed,
r~
PS: I'm annoyed that standards never adopted arithmetic on void *.
Hi Richard,
Quoted 1 line · Richard Henderson
We require "char *" with a space.
thank you for looking into this. I sent v2 for this one.
Quoted 10 lines · Richard Henderson
include/exec/memory.h static inline uint8_t address_space_ldub_cached(MemoryRegionCache *cache, · line 2842
Quoted 6 lines · Roman Kiryanov
include/exec/memory.h +1
-1@@ -2839,7 +2839,7 @@ static inline uint8_t address_space_ldub_cached(MemoryRegionCache *cache, {assert (addr < cache->len);if (likely (cache->ptr)) { -return ldub_p (cache->ptr + addr); +return ldub_p ((char *)cache->ptr + addr);We require "char *" with a space.
Quoted 4 lines
With all of those fixed,
Reviewed-by Richard Henderson <[email protected]>PS: I'm annoyed that standards never adopted arithmetic on void *.
NB, QEMU is explicitly *NOT* targetting the C standard, we are targetting the C dialect supported by GCC and CLang only. IOW, if they have well defined behaviour for arithmetic on void *, then we are free to use it.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Quoted 14 lines · Daniel P. Berrangé
Quoted 13 lines · Richard Henderson
include/exec/memory.h static inline uint8_t address_space_ldub_cached(MemoryRegionCache *cache, · line 2842
Quoted 6 lines · Roman Kiryanov
include/exec/memory.h +1
-1@@ -2839,7 +2839,7 @@ static inline uint8_t address_space_ldub_cached(MemoryRegionCache *cache, {assert (addr < cache->len);if (likely (cache->ptr)) { -return ldub_p (cache->ptr + addr); +return ldub_p ((char *)cache->ptr + addr);We require "char *" with a space.
With all of those fixed,
Reviewed-by Richard Henderson <[email protected]>
Quoted 6 lines
Quoted 1 line
PS: I'm annoyed that standards never adopted arithmetic on void *.
NB, QEMU is explicitly *NOT* targetting the C standard, we are targetting the C dialect supported by GCC and CLang only. IOW, if they have well defined behaviour for arithmetic on void *, then we are free to use it.
It looks like GNU C does support it:
https://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html
Quoted 3 lines
With regards,
Daniel
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro
Hi Daniel and Alex,
Quoted 8 lines · Alex Bennée
Quoted 4 lines · Daniel P. Berrangé
NB, QEMU is explicitly *NOT* targetting the C standard, we are targetting the C dialect supported by GCC and CLang only. IOW, if they have well defined behaviour for arithmetic on void *, then we are free to use it.
It looks like GNU C does support it:
GCC does support void* pointer arithmetic as an extension. But if you decide to use other compilers, you might not have the same luck. We (and maybe other developers) would like to use the QEMU headers with a C++ compiler where this extension is not available.
Regards,
Roman.
Quoted 11 lines · Roman Kiryanov
Hi Daniel and Alex,
Quoted 7 lines · Alex Bennée
Quoted 4 lines · Daniel P. Berrangé
NB, QEMU is explicitly *NOT* targetting the C standard, we are targetting the C dialect supported by GCC and CLang only. IOW, if they have well defined behaviour for arithmetic on void *, then we are free to use it.
Quoted 6 lines
Quoted 1 line
It looks like GNU C does support it:
GCC does support void* pointer arithmetic as an extension. But if you decide to use other compilers, you might not have the same luck. We (and maybe other developers) would like to use the QEMU headers with a C++ compiler where this extension is not available.
I think this is the point where I would say "you're making the code worse for upstream and the only benefit is to your out-of-tree downstream code". If you want to build QEMU, use one of the compilers that QEMU supports. There are lots and lots of places where we assume the GCC-or-clang feature set over and above plain C.
thanks
-- PMM
Hi Peter, thank you for looking.
Quoted 4 lines · Peter Maydell
I think this is the point where I would say "you're making the code worse for upstream and the only benefit is to your out-of-tree downstream code". If you want to build QEMU, use one of the compilers that QEMU supports.
I think there is a value in letting other developers (not just us) to build their code with QEMU. I understand your concern and sent an updated version of the patch, which is only two lines long.
Quoted 2 lines
There are lots and lots of places where we
assume the GCC-or-clang feature set over and above plain C.
I am not changing this part.
Quoted 7 lines · Roman Kiryanov
void* pointer arithmetic is not in the
C standard. This change allows using
the QEMU headers with a C++ compiler.Google-Bug-Id: 331190993
Change-Id I5a064853429f627c17a9213910811dea4ced6174Signed-off-by Roman Kiryanov <[email protected]>
Would it work instead to declare MemoryRegionCache's ptr field as char*?
Thanks,
Paolo
Quoted 10 lines · Paolo Bonzini
Quoted 7 lines · Roman Kiryanov
void* pointer arithmetic is not in the
C standard. This change allows using
the QEMU headers with a C++ compiler.Google-Bug-Id: 331190993
Change-Id I5a064853429f627c17a9213910811dea4ced6174Signed-off-by Roman Kiryanov <[email protected]>Would it work instead to declare MemoryRegionCache's ptr field as char*?
I prefer to use char* only where there are strings. For unstructured data such as MemoryRegionCache, void* is more appropriate.
r~
Quoted 7 lines · Richard Henderson
Quoted 5 lines · Paolo Bonzini
Quoted 4 lines · Roman Kiryanov
void* pointer arithmetic is not in the
C standard. This change allows using
the QEMU headers with a C++ compiler.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Google-Bug-Id: 331190993
Change-Id I5a064853429f627c17a9213910811dea4ced6174Signed-off-by Roman Kiryanov <[email protected]>Would it work instead to declare MemoryRegionCache's ptr field as char*?
I prefer to use char* only where there are strings. For unstructured data such as MemoryRegionCache, void* is more appropriate.
Or uint8_t*... I agree about char*, but unless casts are needed, I find uint8_t and void pointers to be more or less interchangeable.
The problem is that casts are a bit uglier and (while unlikely in this particular case) more subject to bit rot.
Paolo
Quoted 10 lines · Paolo Bonzini
Quoted 4 lines
Quoted 1 line
Would it work instead to declare MemoryRegionCache's ptr field as char*?
I prefer to use char* only where there are strings. For unstructured data such as MemoryRegionCache, void* is more appropriate.
Or uint8_t*... I agree about char*, but unless casts are needed, I find
uint8_tand void pointers to be more or less interchangeable.The problem is that casts are a bit uglier and (while unlikely in this particular case) more subject to bit rot.
Will typedef char *MemoryRegionCachePtr; or making the ptr field pointing to struct MemoryRegionCacheData { char unused; } work better?