diff mbox series

[09/13] system/memory_mapping: make range overlap check more readable

Message ID 20240722040742.11513-10-yaoxt.fnst@fujitsu.com (mailing list archive)
State New, archived
Headers show
Series make range overlap check more readable | expand

Commit Message

Xingtao Yao (Fujitsu) July 22, 2024, 4:07 a.m. UTC
use ranges_overlap() instead of open-coding the overlap check to improve
the readability of the code.

Signed-off-by: Yao Xingtao <yaoxt.fnst@fujitsu.com>
---
 system/memory_mapping.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Philippe Mathieu-Daudé July 22, 2024, 7:21 a.m. UTC | #1
On 22/7/24 06:07, Yao Xingtao wrote:
> use ranges_overlap() instead of open-coding the overlap check to improve
> the readability of the code.
> 
> Signed-off-by: Yao Xingtao <yaoxt.fnst@fujitsu.com>
> ---
>   system/memory_mapping.c | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/system/memory_mapping.c b/system/memory_mapping.c
> index 6f884c5b90c9..ca2390eb8044 100644
> --- a/system/memory_mapping.c
> +++ b/system/memory_mapping.c
> @@ -12,6 +12,7 @@
>    */
>   
>   #include "qemu/osdep.h"
> +#include "qemu/range.h"
>   #include "qapi/error.h"
>   
>   #include "sysemu/memory_mapping.h"
> @@ -353,8 +354,7 @@ void memory_mapping_filter(MemoryMappingList *list, int64_t begin,
>       MemoryMapping *cur, *next;
>   
>       QTAILQ_FOREACH_SAFE(cur, &list->head, next, next) {
> -        if (cur->phys_addr >= begin + length ||
> -            cur->phys_addr + cur->length <= begin) {
> +        if (!ranges_overlap(cur->phys_addr, cur->length, begin, length)) {

Maybe this one is easier to read the other way around:

      if (!ranges_overlap(begin, length, cur->phys_addr, cur->length)) {

Anyhow,

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Tested-by: Philippe Mathieu-Daudé <philmd@linaro.org>
David Hildenbrand July 22, 2024, 9:13 a.m. UTC | #2
On 22.07.24 06:07, Yao Xingtao wrote:
> use ranges_overlap() instead of open-coding the overlap check to improve
> the readability of the code.
> 
> Signed-off-by: Yao Xingtao <yaoxt.fnst@fujitsu.com>
> ---
>   system/memory_mapping.c | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/system/memory_mapping.c b/system/memory_mapping.c
> index 6f884c5b90c9..ca2390eb8044 100644
> --- a/system/memory_mapping.c
> +++ b/system/memory_mapping.c
> @@ -12,6 +12,7 @@
>    */
>   
>   #include "qemu/osdep.h"
> +#include "qemu/range.h"
>   #include "qapi/error.h"
>   
>   #include "sysemu/memory_mapping.h"
> @@ -353,8 +354,7 @@ void memory_mapping_filter(MemoryMappingList *list, int64_t begin,
>       MemoryMapping *cur, *next;
>   
>       QTAILQ_FOREACH_SAFE(cur, &list->head, next, next) {
> -        if (cur->phys_addr >= begin + length ||
> -            cur->phys_addr + cur->length <= begin) {
> +        if (!ranges_overlap(cur->phys_addr, cur->length, begin, length)) {
>               QTAILQ_REMOVE(&list->head, cur, next);
>               g_free(cur);
>               list->num--;

Reviewed-by: David Hildenbrand <david@redhat.com>
diff mbox series

Patch

diff --git a/system/memory_mapping.c b/system/memory_mapping.c
index 6f884c5b90c9..ca2390eb8044 100644
--- a/system/memory_mapping.c
+++ b/system/memory_mapping.c
@@ -12,6 +12,7 @@ 
  */
 
 #include "qemu/osdep.h"
+#include "qemu/range.h"
 #include "qapi/error.h"
 
 #include "sysemu/memory_mapping.h"
@@ -353,8 +354,7 @@  void memory_mapping_filter(MemoryMappingList *list, int64_t begin,
     MemoryMapping *cur, *next;
 
     QTAILQ_FOREACH_SAFE(cur, &list->head, next, next) {
-        if (cur->phys_addr >= begin + length ||
-            cur->phys_addr + cur->length <= begin) {
+        if (!ranges_overlap(cur->phys_addr, cur->length, begin, length)) {
             QTAILQ_REMOVE(&list->head, cur, next);
             g_free(cur);
             list->num--;