From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44687) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UulFV-0000tc-4I for qemu-devel@nongnu.org; Thu, 04 Jul 2013 11:15:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UulFT-0005KM-GQ for qemu-devel@nongnu.org; Thu, 04 Jul 2013 11:15:08 -0400 Received: from mail-we0-x22d.google.com ([2a00:1450:400c:c03::22d]:37878) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UulFT-0005Jx-9F for qemu-devel@nongnu.org; Thu, 04 Jul 2013 11:15:07 -0400 Received: by mail-we0-f173.google.com with SMTP id x54so1239049wes.32 for ; Thu, 04 Jul 2013 08:15:06 -0700 (PDT) Received: from playground.station (net-37-117-148-210.cust.dsl.vodafone.it. [37.117.148.210]) by mx.google.com with ESMTPSA id d8sm4212546wiz.0.2013.07.04.08.15.04 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Thu, 04 Jul 2013 08:15:05 -0700 (PDT) Sender: Paolo Bonzini From: Paolo Bonzini Date: Thu, 4 Jul 2013 17:13:18 +0200 Message-Id: <1372950842-32422-23-git-send-email-pbonzini@redhat.com> In-Reply-To: <1372950842-32422-1-git-send-email-pbonzini@redhat.com> References: <1372950842-32422-1-git-send-email-pbonzini@redhat.com> Subject: [Qemu-devel] [PATCH 22/66] memory: add ref/unref List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Reviewed-by: Jan Kiszka Signed-off-by: Paolo Bonzini --- include/exec/memory.h | 30 ++++++++++++++++++++++++++++++ memory.c | 14 ++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/include/exec/memory.h b/include/exec/memory.h index 2f3e8e4..d2466a7 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -247,6 +247,36 @@ void memory_region_init(MemoryRegion *mr, struct Object *owner, const char *name, uint64_t size); + +/** + * memory_region_ref: Add 1 to a memory region's reference count + * + * Whenever memory regions are accessed outside the BQL, they need to be + * preserved against hot-unplug. MemoryRegions actually do not have their + * own reference count; they piggyback on a QOM object, their "owner". + * This function adds a reference to the owner. + * + * All MemoryRegions must have an owner if they can disappear, even if the + * device they belong to operates exclusively under the BQL. This is because + * the region could be returned at any time by memory_region_find, and this + * is usually under guest control. + * + * @mr: the #MemoryRegion + */ +void memory_region_ref(MemoryRegion *mr); + +/** + * memory_region_unref: Remove 1 to a memory region's reference count + * + * Whenever memory regions are accessed outside the BQL, they need to be + * preserved against hot-unplug. MemoryRegions actually do not have their + * own reference count; they piggyback on a QOM object, their "owner". + * This function removes a reference to the owner and possibly destroys it. + * + * @mr: the #MemoryRegion + */ +void memory_region_unref(MemoryRegion *mr); + /** * memory_region_init_io: Initialize an I/O memory region. * diff --git a/memory.c b/memory.c index f85774a..e04d519 100644 --- a/memory.c +++ b/memory.c @@ -1019,6 +1019,20 @@ Object *memory_region_owner(MemoryRegion *mr) return mr->owner; } +void memory_region_ref(MemoryRegion *mr) +{ + if (mr && mr->owner) { + object_ref(mr->owner); + } +} + +void memory_region_unref(MemoryRegion *mr) +{ + if (mr && mr->owner) { + object_unref(mr->owner); + } +} + uint64_t memory_region_size(MemoryRegion *mr) { if (int128_eq(mr->size, int128_2_64())) { -- 1.8.1.4