From patchwork Fri Oct 30 12:21:19 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 37298 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id B9606B7C3C for ; Sat, 31 Oct 2009 00:31:37 +1100 (EST) Received: from localhost ([127.0.0.1]:39396 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1N3rZm-00028F-Qm for incoming@patchwork.ozlabs.org; Fri, 30 Oct 2009 09:31:34 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1N3qVt-0001xz-Tn for qemu-devel@nongnu.org; Fri, 30 Oct 2009 08:23:30 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1N3qVj-0001ol-1L for qemu-devel@nongnu.org; Fri, 30 Oct 2009 08:23:23 -0400 Received: from [199.232.76.173] (port=52356 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1N3qVg-0001o8-Ei for qemu-devel@nongnu.org; Fri, 30 Oct 2009 08:23:16 -0400 Received: from mail.valinux.co.jp ([210.128.90.3]:34035) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1N3qVe-0002r7-GB for qemu-devel@nongnu.org; Fri, 30 Oct 2009 08:23:15 -0400 Received: from nm.local.valinux.co.jp (vagw.valinux.co.jp [210.128.90.14]) by mail.valinux.co.jp (Postfix) with ESMTP id 8E02418069; Fri, 30 Oct 2009 21:23:06 +0900 (JST) Received: from yamahata by nm.local.valinux.co.jp with local (Exim 4.69) (envelope-from ) id 1N3qTw-0006dr-20; Fri, 30 Oct 2009 21:21:28 +0900 From: Isaku Yamahata To: qemu-devel@nongnu.org, mst@redhat.com Date: Fri, 30 Oct 2009 21:21:19 +0900 Message-Id: <1256905286-25435-26-git-send-email-yamahata@valinux.co.jp> X-Mailer: git-send-email 1.6.0.2 In-Reply-To: <1256905286-25435-1-git-send-email-yamahata@valinux.co.jp> References: <1256905286-25435-1-git-send-email-yamahata@valinux.co.jp> X-Virus-Scanned: clamav-milter 0.95.2 at va-mail.local.valinux.co.jp X-Virus-Status: Clean X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 3) Cc: yamahata@valinux.co.jp Subject: [Qemu-devel] [PATCH V6 25/32] pci: add helper functions to check ranges overlap. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org add helper function to check ranges overlap suggested by Michael S. Tsirkin . His original suggestion was to use [first, last], however I chosen to use offset, length pair, i.e. [offset, offset + length) because pci configuration space related functions all uses offset and length pair, so it helps to avoid to type constant twice. Cc: Michael S. Tsirkin Signed-off-by: Isaku Yamahata Acked-by: Michael S. Tsirkin --- hw/pci.h | 29 +++++++++++++++++++++++++++++ 1 files changed, 29 insertions(+), 0 deletions(-) diff --git a/hw/pci.h b/hw/pci.h index 8e5d64a..7991bfd 100644 --- a/hw/pci.h +++ b/hw/pci.h @@ -406,4 +406,33 @@ PCIBus *pci_apb_init(target_phys_addr_t special_base, PCIBus *sh_pci_register_bus(pci_set_irq_fn set_irq, pci_map_irq_fn map_irq, void *pic, int devfn_min, int nirq); +/* These are not pci specific. Should move into a separate header. + * Only pci.c uses them, so keep them here for now. + */ + +/* Get last byte of a range from offset + length. + * Undefined for ranges that wrap around 0. */ +static inline uint64_t range_get_last(uint64_t offset, uint64_t len) +{ + return offset + len - 1; +} + +/* Check whether a given range covers a given byte. */ +static inline int range_covers_byte(uint64_t offset, uint64_t len, + uint64_t byte) +{ + return offset <= byte && byte <= range_get_last(offset, len); +} + +/* Check whether 2 given ranges overlap. + * Undefined if ranges that wrap around 0. */ +static inline int ranges_overlap(uint64_t first1, uint64_t len1, + uint64_t first2, uint64_t len2) +{ + uint64_t last1 = range_get_last(first1, len1); + uint64_t last2 = range_get_last(first2, len2); + + return !(last2 < first1 || last1 < first2); +} + #endif