From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46753) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b3fVs-0008KX-9q for qemu-devel@nongnu.org; Fri, 20 May 2016 04:10:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b3fVr-0001rH-9N for qemu-devel@nongnu.org; Fri, 20 May 2016 04:10:28 -0400 Date: Fri, 20 May 2016 10:10:17 +0200 From: Kevin Wolf Message-ID: <20160520081017.GC4861@noname.redhat.com> References: <1463671329-22655-1-git-send-email-kwolf@redhat.com> <1463671329-22655-22-git-send-email-kwolf@redhat.com> <6e34bd03-65d6-ceab-be3b-0e24450ed006@redhat.com> <20160520080549.GB4861@noname.redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20160520080549.GB4861@noname.redhat.com> Subject: Re: [Qemu-devel] [PULL 21/31] block: Avoid bs->blk in bdrv_next() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Paolo Bonzini Cc: qemu-block@nongnu.org, qemu-devel@nongnu.org Am 20.05.2016 um 10:05 hat Kevin Wolf geschrieben: > Am 20.05.2016 um 09:54 hat Paolo Bonzini geschrieben: > > > +/* Iterates over all top-level BlockDriverStates, i.e. BDSs that are owned by > > > + * the monitor or attached to a BlockBackend */ > > > +BdrvNextIterator *bdrv_next(BdrvNextIterator *it, BlockDriverState **bs) > > > +{ > > > + if (!it) { > > > + it = g_new(BdrvNextIterator, 1); > > > > Hmm, who frees it? (Especially if the caller exits the loop > > prematurely, which means you cannot just free it before returning NULL). > > I think it's better to: > > > > - allocate the iterator on the stack and make bdrv_next return a BDS * > > > > - and add a bdrv_first function that does this: > > > > > + *it = (BdrvNextIterator) { > > > + .phase = BDRV_NEXT_BACKEND_ROOTS, > > > + }; > > > > and then returns bdrv_next(it); > > > > - if desirable add a macro that abstracts the calls to bdrv_first and > > bdrv_next. > > Already posted a fix. I chose to keep the interface and free the > BdrvNextIterator inside bdrv_next(), when we return NULL after the last > element. Oops, should have actually read your email... You're right about callers that prematurely exit the loop, of course. I still don't really like first/next interfaces, though. Perhaps start the iteration with bs == NULL instead of it == NULL? Kevin