VirtualBoxBase.cpp revision 26d2a42f095ded346df2e41cc4837cb426b4753a
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync/* $Id$ */
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync/** @file
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync *
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * VirtualBox COM base classes implementation
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync */
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync/*
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync *
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * available from http://www.virtualbox.org. This file is free software;
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * you can redistribute it and/or modify it under the terms of the GNU
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * General Public License (GPL) as published by the Free Software
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync *
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * additional information or have any questions.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync */
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync#if !defined (VBOX_WITH_XPCOM)
f84cd77241a1c4b9106a92280611c659243e10d1vboxsync#include <windows.h>
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync#include <dbghelp.h>
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync#else /* !defined (VBOX_WITH_XPCOM) */
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync/// @todo remove when VirtualBoxErrorInfo goes away from here
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync#include <nsIServiceManager.h>
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync#include <nsIExceptionService.h>
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync#endif /* !defined (VBOX_WITH_XPCOM) */
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#include "VirtualBoxBase.h"
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#include "VirtualBoxErrorInfoImpl.h"
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#include "Logging.h"
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#include <iprt/semaphore.h>
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#include <iprt/asm.h>
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync// VirtualBoxBaseProto methods
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync////////////////////////////////////////////////////////////////////////////////
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsyncVirtualBoxBaseProto::VirtualBoxBaseProto()
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync{
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync mState = NotReady;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync mStateChangeThread = NIL_RTTHREAD;
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync mCallers = 0;
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync mZeroCallersSem = NIL_RTSEMEVENT;
6420f75ffc86ab6494eb5e95418f0c95e71e8068vboxsync mInitUninitSem = NIL_RTSEMEVENTMULTI;
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync mInitUninitWaiters = 0;
6420f75ffc86ab6494eb5e95418f0c95e71e8068vboxsync mObjectLock = NULL;
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync}
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsyncVirtualBoxBaseProto::~VirtualBoxBaseProto()
6420f75ffc86ab6494eb5e95418f0c95e71e8068vboxsync{
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync if (mObjectLock)
6420f75ffc86ab6494eb5e95418f0c95e71e8068vboxsync delete mObjectLock;
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync Assert (mInitUninitWaiters == 0);
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync Assert (mInitUninitSem == NIL_RTSEMEVENTMULTI);
9ad5e3912962c3dbccc1afc4e7d62890fe906814vboxsync if (mZeroCallersSem != NIL_RTSEMEVENT)
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync RTSemEventDestroy (mZeroCallersSem);
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync mCallers = 0;
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync mStateChangeThread = NIL_RTTHREAD;
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync mState = NotReady;
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync}
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync// util::Lockable interface
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsyncRWLockHandle *VirtualBoxBaseProto::lockHandle() const
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync{
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync /* lazy initialization */
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync if (RT_UNLIKELY(!mObjectLock))
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync {
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync AssertCompile (sizeof (RWLockHandle *) == sizeof (void *));
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync RWLockHandle *objLock = new RWLockHandle;
9ad5e3912962c3dbccc1afc4e7d62890fe906814vboxsync if (!ASMAtomicCmpXchgPtr ((void * volatile *) &mObjectLock, objLock, NULL))
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync {
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync delete objLock;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync objLock = (RWLockHandle *) ASMAtomicReadPtr ((void * volatile *) &mObjectLock);
d32c860c64e340970271b4113a6a67cad64460b4vboxsync }
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync return objLock;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync }
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync return mObjectLock;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync}
28d7c24dda3ad9c1c47a3f77454193b1a48da852vboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync/**
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * Increments the number of calls to this object by one.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync *
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * After this method succeeds, it is guaranted that the object will remain
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * in the Ready (or in the Limited) state at least until #releaseCaller() is
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * called.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync *
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * This method is intended to mark the beginning of sections of code within
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * methods of COM objects that depend on the readiness (Ready) state. The
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * Ready state is a primary "ready to serve" state. Usually all code that
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * works with component's data depends on it. On practice, this means that
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * almost every public method, setter or getter of the object should add
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * itself as an object's caller at the very beginning, to protect from an
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * unexpected uninitialization that may happen on a different thread.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync *
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * Besides the Ready state denoting that the object is fully functional,
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * there is a special Limited state. The Limited state means that the object
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * is still functional, but its functionality is limited to some degree, so
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * not all operations are possible. The @a aLimited argument to this method
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * determines whether the caller represents this limited functionality or
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * not.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync *
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * This method succeeeds (and increments the number of callers) only if the
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * current object's state is Ready. Otherwise, it will return E_ACCESSDENIED
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * to indicate that the object is not operational. There are two exceptions
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * from this rule:
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * <ol>
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * <li>If the @a aLimited argument is |true|, then this method will also
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * succeeed if the object's state is Limited (or Ready, of course).
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * </li>
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * <li>If this method is called from the same thread that placed
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * the object to InInit or InUninit state (i.e. either from within the
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * AutoInitSpan or AutoUninitSpan scope), it will succeed as well (but
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * will not increase the number of callers).
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * </li>
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * </ol>
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync *
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * Normally, calling addCaller() never blocks. However, if this method is
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * called by a thread created from within the AutoInitSpan scope and this
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * scope is still active (i.e. the object state is InInit), it will block
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * until the AutoInitSpan destructor signals that it has finished
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * initialization.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync *
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync * Also, addCaller() will block if the object is probing uninitialization on
9ad5e3912962c3dbccc1afc4e7d62890fe906814vboxsync * another thread with AutoMayUninitSpan (i.e. the object state is MayUninit).
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync * And again, the block will last until the AutoMayUninitSpan destructor signals
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * that it has finished probing and the object is either ready again or will
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * uninitialize shortly (so that addCaller() will fail).
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync *
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * When this method returns a failure, the caller must not use the object
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * and should return the failed result code to its own caller.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync *
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @param aState Where to store the current object's state (can be
50fdc90dae026b2086f85b0f028aa63dd6bbe14evboxsync * used in overriden methods to determine the cause of
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * the failure).
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @param aLimited |true| to add a limited caller.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync *
50fdc90dae026b2086f85b0f028aa63dd6bbe14evboxsync * @return S_OK on success or E_ACCESSDENIED on failure.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync *
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @note It is preferrable to use the #addLimitedCaller() rather than
50fdc90dae026b2086f85b0f028aa63dd6bbe14evboxsync * calling this method with @a aLimited = |true|, for better
50fdc90dae026b2086f85b0f028aa63dd6bbe14evboxsync * self-descriptiveness.
e8ac7dce6d625856c57792a6af738e2fe2667264vboxsync *
50fdc90dae026b2086f85b0f028aa63dd6bbe14evboxsync * @sa #addLimitedCaller()
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @sa #releaseCaller()
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync */
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsyncHRESULT VirtualBoxBaseProto::addCaller (State *aState /* = NULL */,
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync bool aLimited /* = false */)
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync{
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync AutoWriteLock stateLock (mStateLock);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync HRESULT rc = E_ACCESSDENIED;
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync if (mState == Ready || (aLimited && mState == Limited))
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync {
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync /* if Ready or allows Limited, increase the number of callers */
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync ++ mCallers;
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync rc = S_OK;
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync }
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync else
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync if (mState == InInit || mState == MayUninit || mState == InUninit)
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync {
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync if (mStateChangeThread == RTThreadSelf())
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync {
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync /* Called from the same thread that is doing AutoInitSpan or
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync * AutoUninitSpan or AutoMayUninitSpan, just succeed */
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync rc = S_OK;
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync }
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync else if (mState == InInit || mState == MayUninit)
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync {
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync /* One of the two:
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync *
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync * 1) addCaller() is called by a "child" thread while the "parent"
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync * thread is still doing AutoInitSpan/AutoReinitSpan, so wait for
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync * the state to become either Ready/Limited or InitFailed (in
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync * case of init failure).
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync *
* 2) addCaller() is called while another thread is in
* AutoMayUninitSpan, so wait for the state to become either
* Ready or WillUninit.
*
* Note that in either case we increase the number of callers anyway
* -- to prevent AutoUninitSpan from early completion if we are
* still not scheduled to pick up the posted semaphore when uninit()
* is called.
*/
++ mCallers;
/* lazy semaphore creation */
if (mInitUninitSem == NIL_RTSEMEVENTMULTI)
{
RTSemEventMultiCreate (&mInitUninitSem);
Assert (mInitUninitWaiters == 0);
}
++ mInitUninitWaiters;
LogFlowThisFunc ((mState == InInit ?
"Waiting for AutoInitSpan/AutoReinitSpan to "
"finish...\n" :
"Waiting for AutoMayUninitSpan to finish...\n"));
stateLock.leave();
RTSemEventMultiWait (mInitUninitSem, RT_INDEFINITE_WAIT);
stateLock.enter();
if (-- mInitUninitWaiters == 0)
{
/* destroy the semaphore since no more necessary */
RTSemEventMultiDestroy (mInitUninitSem);
mInitUninitSem = NIL_RTSEMEVENTMULTI;
}
if (mState == Ready || (aLimited && mState == Limited))
rc = S_OK;
else
{
Assert (mCallers != 0);
-- mCallers;
if (mCallers == 0 && mState == InUninit)
{
/* inform AutoUninitSpan ctor there are no more callers */
RTSemEventSignal (mZeroCallersSem);
}
}
}
}
if (aState)
*aState = mState;
return rc;
}
/**
* Decreases the number of calls to this object by one.
*
* Must be called after every #addCaller() or #addLimitedCaller() when
* protecting the object from uninitialization is no more necessary.
*/
void VirtualBoxBaseProto::releaseCaller()
{
AutoWriteLock stateLock (mStateLock);
if (mState == Ready || mState == Limited)
{
/* if Ready or Limited, decrease the number of callers */
AssertMsgReturn (mCallers != 0, ("mCallers is ZERO!"), (void) 0);
-- mCallers;
return;
}
if (mState == InInit || mState == MayUninit || mState == InUninit)
{
if (mStateChangeThread == RTThreadSelf())
{
/* Called from the same thread that is doing AutoInitSpan or
* AutoUninitSpan, just succeed */
return;
}
if (mState == InUninit)
{
/* the caller is being released after AutoUninitSpan has begun */
AssertMsgReturn (mCallers != 0, ("mCallers is ZERO!"), (void) 0);
-- mCallers;
if (mCallers == 0)
{
/* inform the AutoUninitSpan ctor there are no more callers */
RTSemEventSignal (mZeroCallersSem);
}
return;
}
}
AssertMsgFailed (("mState = %d!", mState));
}
// VirtualBoxBaseProto::AutoInitSpan methods
////////////////////////////////////////////////////////////////////////////////
/**
* Creates a smart initialization span object that places the object to
* InInit state.
*
* Please see the AutoInitSpan class description for more info.
*
* @param aObj |this| pointer of the managed VirtualBoxBase object whose
* init() method is being called.
* @param aResult Default initialization result.
*/
VirtualBoxBaseProto::AutoInitSpan::
AutoInitSpan (VirtualBoxBaseProto *aObj, Result aResult /* = Failed */)
: mObj (aObj), mResult (aResult), mOk (false)
{
Assert (aObj);
AutoWriteLock stateLock (mObj->mStateLock);
mOk = mObj->mState == NotReady;
AssertReturnVoid (mOk);
mObj->setState (InInit);
}
/**
* Places the managed VirtualBoxBase object to Ready/Limited state if the
* initialization succeeded or partly succeeded, or places it to InitFailed
* state and calls the object's uninit() method.
*
* Please see the AutoInitSpan class description for more info.
*/
VirtualBoxBaseProto::AutoInitSpan::~AutoInitSpan()
{
/* if the state was other than NotReady, do nothing */
if (!mOk)
return;
AutoWriteLock stateLock (mObj->mStateLock);
Assert (mObj->mState == InInit);
if (mObj->mCallers > 0)
{
Assert (mObj->mInitUninitWaiters > 0);
/* We have some pending addCaller() calls on other threads (created
* during InInit), signal that InInit is finished and they may go on. */
RTSemEventMultiSignal (mObj->mInitUninitSem);
}
if (mResult == Succeeded)
{
mObj->setState (Ready);
}
else
if (mResult == Limited)
{
mObj->setState (VirtualBoxBaseProto::Limited);
}
else
{
mObj->setState (InitFailed);
/* leave the lock to prevent nesting when uninit() is called */
stateLock.leave();
/* call uninit() to let the object uninit itself after failed init() */
mObj->uninit();
/* Note: the object may no longer exist here (for example, it can call
* the destructor in uninit()) */
}
}
// VirtualBoxBaseProto::AutoReinitSpan methods
////////////////////////////////////////////////////////////////////////////////
/**
* Creates a smart re-initialization span object and places the object to
* InInit state.
*
* Please see the AutoInitSpan class description for more info.
*
* @param aObj |this| pointer of the managed VirtualBoxBase object whose
* re-initialization method is being called.
*/
VirtualBoxBaseProto::AutoReinitSpan::
AutoReinitSpan (VirtualBoxBaseProto *aObj)
: mObj (aObj), mSucceeded (false), mOk (false)
{
Assert (aObj);
AutoWriteLock stateLock (mObj->mStateLock);
mOk = mObj->mState == Limited;
AssertReturnVoid (mOk);
mObj->setState (InInit);
}
/**
* Places the managed VirtualBoxBase object to Ready state if the
* re-initialization succeeded (i.e. #setSucceeded() has been called) or back to
* Limited state otherwise.
*
* Please see the AutoInitSpan class description for more info.
*/
VirtualBoxBaseProto::AutoReinitSpan::~AutoReinitSpan()
{
/* if the state was other than Limited, do nothing */
if (!mOk)
return;
AutoWriteLock stateLock (mObj->mStateLock);
Assert (mObj->mState == InInit);
if (mObj->mCallers > 0 && mObj->mInitUninitWaiters > 0)
{
/* We have some pending addCaller() calls on other threads (created
* during InInit), signal that InInit is finished and they may go on. */
RTSemEventMultiSignal (mObj->mInitUninitSem);
}
if (mSucceeded)
{
mObj->setState (Ready);
}
else
{
mObj->setState (Limited);
}
}
// VirtualBoxBaseProto::AutoUninitSpan methods
////////////////////////////////////////////////////////////////////////////////
/**
* Creates a smart uninitialization span object and places this object to
* InUninit state.
*
* Please see the AutoInitSpan class description for more info.
*
* @note This method blocks the current thread execution until the number of
* callers of the managed VirtualBoxBase object drops to zero!
*
* @param aObj |this| pointer of the VirtualBoxBase object whose uninit()
* method is being called.
*/
VirtualBoxBaseProto::AutoUninitSpan::AutoUninitSpan (VirtualBoxBaseProto *aObj)
: mObj (aObj), mInitFailed (false), mUninitDone (false)
{
Assert (aObj);
AutoWriteLock stateLock (mObj->mStateLock);
Assert (mObj->mState != InInit);
/* Set mUninitDone to |true| if this object is already uninitialized
* (NotReady) or if another AutoUninitSpan is currently active on some
* other thread (InUninit). */
mUninitDone = mObj->mState == NotReady ||
mObj->mState == InUninit;
if (mObj->mState == InitFailed)
{
/* we've been called by init() on failure */
mInitFailed = true;
}
else
{
if (mUninitDone)
{
/* do nothing if already uninitialized */
if (mObj->mState == NotReady)
return;
/* otherwise, wait until another thread finishes uninitialization.
* This is necessary to make sure that when this method returns, the
* object is NotReady and therefore can be deleted (for example).
* In particular, this is used by
* VirtualBoxBaseWithTypedChildrenNEXT::uninitDependentChildren(). */
/* lazy semaphore creation */
if (mObj->mInitUninitSem == NIL_RTSEMEVENTMULTI)
{
RTSemEventMultiCreate (&mObj->mInitUninitSem);
Assert (mObj->mInitUninitWaiters == 0);
}
++ mObj->mInitUninitWaiters;
LogFlowFunc (("{%p}: Waiting for AutoUninitSpan to finish...\n",
mObj));
stateLock.leave();
RTSemEventMultiWait (mObj->mInitUninitSem, RT_INDEFINITE_WAIT);
stateLock.enter();
if (-- mObj->mInitUninitWaiters == 0)
{
/* destroy the semaphore since no more necessary */
RTSemEventMultiDestroy (mObj->mInitUninitSem);
mObj->mInitUninitSem = NIL_RTSEMEVENTMULTI;
}
return;
}
}
/* go to InUninit to prevent from adding new callers */
mObj->setState (InUninit);
/* wait for already existing callers to drop to zero */
if (mObj->mCallers > 0)
{
/* lazy creation */
Assert (mObj->mZeroCallersSem == NIL_RTSEMEVENT);
RTSemEventCreate (&mObj->mZeroCallersSem);
/* wait until remaining callers release the object */
LogFlowFunc (("{%p}: Waiting for callers (%d) to drop to zero...\n",
mObj, mObj->mCallers));
stateLock.leave();
RTSemEventWait (mObj->mZeroCallersSem, RT_INDEFINITE_WAIT);
}
}
/**
* Places the managed VirtualBoxBase object to the NotReady state.
*/
VirtualBoxBaseProto::AutoUninitSpan::~AutoUninitSpan()
{
/* do nothing if already uninitialized */
if (mUninitDone)
return;
AutoWriteLock stateLock (mObj->mStateLock);
Assert (mObj->mState == InUninit);
mObj->setState (NotReady);
}
// VirtualBoxBaseProto::AutoMayUninitSpan methods
////////////////////////////////////////////////////////////////////////////////
/**
* Creates a smart initialization span object that places the object to
* MayUninit state.
*
* Please see the AutoMayUninitSpan class description for more info.
*
* @param aObj |this| pointer of the managed VirtualBoxBase object whose
* uninit() method to be probably called.
*/
VirtualBoxBaseProto::AutoMayUninitSpan::
AutoMayUninitSpan (VirtualBoxBaseProto *aObj)
: mObj (aObj), mRC (E_FAIL), mAlreadyInProgress (false)
, mAcceptUninit (false)
{
Assert (aObj);
AutoWriteLock stateLock (mObj->mStateLock);
AssertReturnVoid (mObj->mState != InInit &&
mObj->mState != InUninit);
switch (mObj->mState)
{
case Ready:
break;
case MayUninit:
/* Nothing to be done if already in MayUninit. */
mAlreadyInProgress = true;
mRC = S_OK;
return;
default:
/* Abuse mObj->addCaller() to get the extended error info possibly
* set by reimplementations of addCaller() and return it to the
* caller. Note that this abuse is supposed to be safe because we
* should've filtered out all states where addCaller() would do
* something else but set error info. */
mRC = mObj->addCaller();
Assert (FAILED (mRC));
return;
}
/* go to MayUninit to cause new callers to wait until we finish */
mObj->setState (MayUninit);
mRC = S_OK;
/* wait for already existing callers to drop to zero */
if (mObj->mCallers > 0)
{
/* lazy creation */
Assert (mObj->mZeroCallersSem == NIL_RTSEMEVENT);
RTSemEventCreate (&mObj->mZeroCallersSem);
/* wait until remaining callers release the object */
LogFlowFunc (("{%p}: Waiting for callers (%d) to drop to zero...\n",
mObj, mObj->mCallers));
stateLock.leave();
RTSemEventWait (mObj->mZeroCallersSem, RT_INDEFINITE_WAIT);
}
}
/**
* Places the managed VirtualBoxBase object back to Ready state if
* #acceptUninit() was not called, or places it to WillUninit state and calls
* the object's uninit() method.
*
* Please see the AutoMayUninitSpan class description for more info.
*/
VirtualBoxBaseProto::AutoMayUninitSpan::~AutoMayUninitSpan()
{
/* if we did nothing in the constructor, do nothing here */
if (mAlreadyInProgress || FAILED (mRC))
return;
AutoWriteLock stateLock (mObj->mStateLock);
Assert (mObj->mState == MayUninit);
if (mObj->mCallers > 0)
{
Assert (mObj->mInitUninitWaiters > 0);
/* We have some pending addCaller() calls on other threads made after
* going to during MayUnit, signal that MayUnit is finished and they may
* go on. */
RTSemEventMultiSignal (mObj->mInitUninitSem);
}
if (!mAcceptUninit)
{
mObj->setState (Ready);
}
else
{
mObj->setState (WillUninit);
/* leave the lock to prevent nesting when uninit() is called */
stateLock.leave();
/* call uninit() to let the object uninit itself */
mObj->uninit();
/* Note: the object may no longer exist here (for example, it can call
* the destructor in uninit()) */
}
}
// VirtualBoxBase methods
////////////////////////////////////////////////////////////////////////////////
/**
* Translates the given text string according to the currently installed
* translation table and current context. The current context is determined
* by the context parameter. Additionally, a comment to the source text
* string text can be given. This comment (which is NULL by default)
* is helpful in sutuations where it is necessary to distinguish between
* two or more semantically different roles of the same source text in the
* same context.
*
* @param context the context of the translation (can be NULL
* to indicate the global context)
* @param sourceText the string to translate
* @param comment the comment to the string (NULL means no comment)
*
* @return
* the translated version of the source string in UTF-8 encoding,
* or the source string itself if the translation is not found
* in the given context.
*/
// static
const char *VirtualBoxBase::translate (const char *context, const char *sourceText,
const char *comment)
{
#if 0
Log(("VirtualBoxBase::translate:\n"
" context={%s}\n"
" sourceT={%s}\n"
" comment={%s}\n",
context, sourceText, comment));
#endif
/// @todo (dmik) incorporate Qt translation file parsing and lookup
return sourceText;
}
// VirtualBoxSupportTranslationBase methods
////////////////////////////////////////////////////////////////////////////////
/**
* Modifies the given argument so that it will contain only a class name
* (null-terminated). The argument must point to a <b>non-constant</b>
* string containing a valid value, as it is generated by the
* __PRETTY_FUNCTION__ built-in macro of the GCC compiler, or by the
* __FUNCTION__ macro of any other compiler.
*
* The function assumes that the macro is used within the member of the
* class derived from the VirtualBoxSupportTranslation<> template.
*
* @param prettyFunctionName string to modify
* @return
* true on success and false otherwise
*/
bool VirtualBoxSupportTranslationBase::cutClassNameFrom__PRETTY_FUNCTION__ (char *fn)
{
Assert (fn);
if (!fn)
return false;
#if defined (__GNUC__)
// the format is like:
// VirtualBoxSupportTranslation<C>::VirtualBoxSupportTranslation() [with C = VirtualBox]
#define START " = "
#define END "]"
#elif defined (_MSC_VER)
// the format is like:
// VirtualBoxSupportTranslation<class VirtualBox>::__ctor
#define START "<class "
#define END ">::"
#endif
char *start = strstr (fn, START);
Assert (start);
if (start)
{
start += sizeof (START) - 1;
char *end = strstr (start, END);
Assert (end && (end > start));
if (end && (end > start))
{
size_t len = end - start;
memmove (fn, start, len);
fn [len] = 0;
return true;
}
}
#undef END
#undef START
return false;
}
// VirtualBoxSupportErrorInfoImplBase methods
////////////////////////////////////////////////////////////////////////////////
RTTLS VirtualBoxSupportErrorInfoImplBase::MultiResult::sCounter = NIL_RTTLS;
void VirtualBoxSupportErrorInfoImplBase::MultiResult::init()
{
if (sCounter == NIL_RTTLS)
{
sCounter = RTTlsAlloc();
AssertReturnVoid (sCounter != NIL_RTTLS);
}
uintptr_t counter = (uintptr_t) RTTlsGet (sCounter);
++ counter;
RTTlsSet (sCounter, (void *) counter);
}
VirtualBoxSupportErrorInfoImplBase::MultiResult::~MultiResult()
{
uintptr_t counter = (uintptr_t) RTTlsGet (sCounter);
AssertReturnVoid (counter != 0);
-- counter;
RTTlsSet (sCounter, (void *) counter);
}
/**
* Sets error info for the current thread. This is an internal function that
* gets eventually called by all public variants. If @a aWarning is
* @c true, then the highest (31) bit in the @a aResultCode value which
* indicates the error severity is reset to zero to make sure the receiver will
* recognize that the created error info object represents a warning rather
* than an error.
*/
/* static */
HRESULT VirtualBoxSupportErrorInfoImplBase::setErrorInternal (
HRESULT aResultCode, const GUID &aIID,
const Bstr &aComponent, const Bstr &aText,
bool aWarning, bool aLogIt)
{
/* whether multi-error mode is turned on */
bool preserve = ((uintptr_t) RTTlsGet (MultiResult::sCounter)) > 0;
if (aLogIt)
LogRel (("ERROR [COM]: aRC=%Rhrc (%#08x) aIID={%RTuuid} aComponent={%ls} aText={%ls} "
"aWarning=%RTbool, preserve=%RTbool\n",
aResultCode, aResultCode, &aIID, aComponent.raw(), aText.raw(), aWarning,
preserve));
/* these are mandatory, others -- not */
AssertReturn ((!aWarning && FAILED (aResultCode)) ||
(aWarning && aResultCode != S_OK),
E_FAIL);
AssertReturn (!aText.isEmpty(), E_FAIL);
/* reset the error severity bit if it's a warning */
if (aWarning)
aResultCode &= ~0x80000000;
HRESULT rc = S_OK;
do
{
ComObjPtr <VirtualBoxErrorInfo> info;
rc = info.createObject();
CheckComRCBreakRC (rc);
#if !defined (VBOX_WITH_XPCOM)
ComPtr <IVirtualBoxErrorInfo> curInfo;
if (preserve)
{
/* get the current error info if any */
ComPtr <IErrorInfo> err;
rc = ::GetErrorInfo (0, err.asOutParam());
CheckComRCBreakRC (rc);
rc = err.queryInterfaceTo (curInfo.asOutParam());
if (FAILED (rc))
{
/* create a IVirtualBoxErrorInfo wrapper for the native
* IErrorInfo object */
ComObjPtr <VirtualBoxErrorInfo> wrapper;
rc = wrapper.createObject();
if (SUCCEEDED (rc))
{
rc = wrapper->init (err);
if (SUCCEEDED (rc))
curInfo = wrapper;
}
}
}
/* On failure, curInfo will stay null */
Assert (SUCCEEDED (rc) || curInfo.isNull());
/* set the current error info and preserve the previous one if any */
rc = info->init (aResultCode, aIID, aComponent, aText, curInfo);
CheckComRCBreakRC (rc);
ComPtr <IErrorInfo> err;
rc = info.queryInterfaceTo (err.asOutParam());
if (SUCCEEDED (rc))
rc = ::SetErrorInfo (0, err);
#else // !defined (VBOX_WITH_XPCOM)
nsCOMPtr <nsIExceptionService> es;
es = do_GetService (NS_EXCEPTIONSERVICE_CONTRACTID, &rc);
if (NS_SUCCEEDED (rc))
{
nsCOMPtr <nsIExceptionManager> em;
rc = es->GetCurrentExceptionManager (getter_AddRefs (em));
CheckComRCBreakRC (rc);
ComPtr <IVirtualBoxErrorInfo> curInfo;
if (preserve)
{
/* get the current error info if any */
ComPtr <nsIException> ex;
rc = em->GetCurrentException (ex.asOutParam());
CheckComRCBreakRC (rc);
rc = ex.queryInterfaceTo (curInfo.asOutParam());
if (FAILED (rc))
{
/* create a IVirtualBoxErrorInfo wrapper for the native
* nsIException object */
ComObjPtr <VirtualBoxErrorInfo> wrapper;
rc = wrapper.createObject();
if (SUCCEEDED (rc))
{
rc = wrapper->init (ex);
if (SUCCEEDED (rc))
curInfo = wrapper;
}
}
}
/* On failure, curInfo will stay null */
Assert (SUCCEEDED (rc) || curInfo.isNull());
/* set the current error info and preserve the previous one if any */
rc = info->init (aResultCode, aIID, aComponent, aText, curInfo);
CheckComRCBreakRC (rc);
ComPtr <nsIException> ex;
rc = info.queryInterfaceTo (ex.asOutParam());
if (SUCCEEDED (rc))
rc = em->SetCurrentException (ex);
}
else if (rc == NS_ERROR_UNEXPECTED)
{
/*
* It is possible that setError() is being called by the object
* after the XPCOM shutdown sequence has been initiated
* (for example, when XPCOM releases all instances it internally
* references, which can cause object's FinalConstruct() and then
* uninit()). In this case, do_GetService() above will return
* NS_ERROR_UNEXPECTED and it doesn't actually make sense to
* set the exception (nobody will be able to read it).
*/
LogWarningFunc (("Will not set an exception because "
"nsIExceptionService is not available "
"(NS_ERROR_UNEXPECTED). "
"XPCOM is being shutdown?\n"));
rc = NS_OK;
}
#endif // !defined (VBOX_WITH_XPCOM)
}
while (0);
AssertComRC (rc);
return SUCCEEDED (rc) ? aResultCode : rc;
}
// VirtualBoxBaseWithChildren methods
////////////////////////////////////////////////////////////////////////////////
/**
* Uninitializes all dependent children registered with #addDependentChild().
*
* @note
* This method will call uninit() methods of children. If these methods
* access the parent object, uninitDependentChildren() must be called
* either at the beginning of the parent uninitialization sequence (when
* it is still operational) or after setReady(false) is called to
* indicate the parent is out of action.
*/
void VirtualBoxBaseWithChildren::uninitDependentChildren()
{
/// @todo (r=dmik) see todo in VirtualBoxBase.h, in
// template <class C> void removeDependentChild (C *child)
LogFlowThisFuncEnter();
AutoWriteLock alock (this);
AutoWriteLock mapLock (mMapLock);
LogFlowThisFunc (("count=%d...\n", mDependentChildren.size()));
if (mDependentChildren.size())
{
/* We keep the lock until we have enumerated all children.
* Those ones that will try to call #removeDependentChild() from
* a different thread will have to wait */
Assert (mUninitDoneSem == NIL_RTSEMEVENT);
int vrc = RTSemEventCreate (&mUninitDoneSem);
AssertRC (vrc);
Assert (mChildrenLeft == 0);
mChildrenLeft = (unsigned)mDependentChildren.size();
for (DependentChildren::iterator it = mDependentChildren.begin();
it != mDependentChildren.end(); ++ it)
{
VirtualBoxBase *child = (*it).second;
Assert (child);
if (child)
child->uninit();
}
mDependentChildren.clear();
}
/* Wait until all children started uninitializing on their own
* (and therefore are waiting for some parent's method or for
* #removeDependentChild() to return) are finished uninitialization */
if (mUninitDoneSem != NIL_RTSEMEVENT)
{
/* let stuck children run */
mapLock.leave();
alock.leave();
LogFlowThisFunc (("Waiting for uninitialization of all children...\n"));
RTSemEventWait (mUninitDoneSem, RT_INDEFINITE_WAIT);
alock.enter();
mapLock.enter();
RTSemEventDestroy (mUninitDoneSem);
mUninitDoneSem = NIL_RTSEMEVENT;
Assert (mChildrenLeft == 0);
}
LogFlowThisFuncLeave();
}
/**
* Returns a pointer to the dependent child corresponding to the given
* interface pointer (used as a key in the map) or NULL if the interface
* pointer doesn't correspond to any child registered using
* #addDependentChild().
*
* @param unk
* Pointer to map to the dependent child object (it is ComPtr <IUnknown>
* rather than IUnknown *, to guarantee IUnknown * identity)
* @return
* Pointer to the dependent child object
*/
VirtualBoxBase *VirtualBoxBaseWithChildren::getDependentChild (
const ComPtr <IUnknown> &unk)
{
AssertReturn (!!unk, NULL);
AutoWriteLock alock (mMapLock);
if (mUninitDoneSem != NIL_RTSEMEVENT)
return NULL;
DependentChildren::const_iterator it = mDependentChildren.find (unk);
if (it == mDependentChildren.end())
return NULL;
return (*it).second;
}
/** Helper for addDependentChild() template method */
void VirtualBoxBaseWithChildren::addDependentChild (
const ComPtr <IUnknown> &unk, VirtualBoxBase *child)
{
AssertReturn (!!unk && child, (void) 0);
AutoWriteLock alock (mMapLock);
if (mUninitDoneSem != NIL_RTSEMEVENT)
{
// for this very unlikely case, we have to increase the number of
// children left, for symmetry with #removeDependentChild()
++ mChildrenLeft;
return;
}
std::pair <DependentChildren::iterator, bool> result =
mDependentChildren.insert (DependentChildren::value_type (unk, child));
AssertMsg (result.second, ("Failed to insert a child to the map\n"));
}
/** Helper for removeDependentChild() template method */
void VirtualBoxBaseWithChildren::removeDependentChild (const ComPtr <IUnknown> &unk)
{
/// @todo (r=dmik) see todo in VirtualBoxBase.h, in
// template <class C> void removeDependentChild (C *child)
AssertReturn (!!unk, (void) 0);
AutoWriteLock alock (mMapLock);
if (mUninitDoneSem != NIL_RTSEMEVENT)
{
// uninitDependentChildren() is in action, just increase the number
// of children left and signal a semaphore when it reaches zero
Assert (mChildrenLeft != 0);
-- mChildrenLeft;
if (mChildrenLeft == 0)
{
int vrc = RTSemEventSignal (mUninitDoneSem);
AssertRC (vrc);
}
return;
}
DependentChildren::size_type result = mDependentChildren.erase (unk);
AssertMsg (result == 1, ("Failed to remove a child from the map\n"));
NOREF (result);
}
// VirtualBoxBaseWithChildrenNEXT methods
////////////////////////////////////////////////////////////////////////////////
/**
* Uninitializes all dependent children registered on this object with
* #addDependentChild().
*
* Must be called from within the VirtualBoxBaseProto::AutoUninitSpan (i.e.
* typically from this object's uninit() method) to uninitialize children
* before this object goes out of service and becomes unusable.
*
* Note that this method will call uninit() methods of child objects. If
* these methods need to call the parent object during uninitialization,
* #uninitDependentChildren() must be called before the relevant part of the
* parent is uninitialized: usually at the begnning of the parent
* uninitialization sequence.
*
* Keep in mind that the uninitialized child objects may be no longer available
* (i.e. may be deleted) after this method returns.
*
* @note Locks #childrenLock() for writing.
*
* @note May lock something else through the called children.
*/
void VirtualBoxBaseWithChildrenNEXT::uninitDependentChildren()
{
AutoCaller autoCaller (this);
/* sanity */
AssertReturnVoid (autoCaller.state() == InUninit ||
autoCaller.state() == InInit);
AutoWriteLock chLock (childrenLock());
size_t count = mDependentChildren.size();
while (count != 0)
{
/* strongly reference the weak child from the map to make sure it won't
* be deleted while we've released the lock */
DependentChildren::iterator it = mDependentChildren.begin();
ComPtr <IUnknown> unk = it->first;
Assert (!unk.isNull());
VirtualBoxBase *child = it->second;
/* release the lock to let children stuck in removeDependentChild() go
* on (otherwise we'll deadlock in uninit() */
chLock.leave();
/* Note that if child->uninit() happens to be called on another
* thread right before us and is not yet finished, the second
* uninit() call will wait until the first one has done so
* (thanks to AutoUninitSpan). */
Assert (child);
if (child)
child->uninit();
chLock.enter();
/* uninit() is guaranteed to be done here so the child must be already
* deleted from the list by removeDependentChild() called from there.
* Do some checks to avoid endless loops when the user is forgetful */
-- count;
Assert (count == mDependentChildren.size());
if (count != mDependentChildren.size())
mDependentChildren.erase (it);
Assert (count == mDependentChildren.size());
}
}
/**
* Returns a pointer to the dependent child (registered using
* #addDependentChild()) corresponding to the given interface pointer or NULL if
* the given pointer is unrelated.
*
* The relation is checked by using the given interface pointer as a key in the
* map of dependent children.
*
* Note that ComPtr <IUnknown> is used as an argument instead of IUnknown * in
* order to guarantee IUnknown identity and disambiguation by doing
* QueryInterface (IUnknown) rather than a regular C cast.
*
* @param aUnk Pointer to map to the dependent child object.
* @return Pointer to the dependent VirtualBoxBase child object.
*
* @note Locks #childrenLock() for reading.
*/
VirtualBoxBaseNEXT *
VirtualBoxBaseWithChildrenNEXT::getDependentChild (const ComPtr <IUnknown> &aUnk)
{
AssertReturn (!aUnk.isNull(), NULL);
AutoCaller autoCaller (this);
/* return NULL if uninitDependentChildren() is in action */
if (autoCaller.state() == InUninit)
return NULL;
AutoReadLock alock (childrenLock());
DependentChildren::const_iterator it = mDependentChildren.find (aUnk);
if (it == mDependentChildren.end())
return NULL;
return (*it).second;
}
/** Helper for addDependentChild(). */
void VirtualBoxBaseWithChildrenNEXT::doAddDependentChild (
IUnknown *aUnk, VirtualBoxBaseNEXT *aChild)
{
AssertReturnVoid (aUnk != NULL);
AssertReturnVoid (aChild != NULL);
AutoCaller autoCaller (this);
/* sanity */
AssertReturnVoid (autoCaller.state() == InInit ||
autoCaller.state() == Ready ||
autoCaller.state() == Limited);
AutoWriteLock alock (childrenLock());
std::pair <DependentChildren::iterator, bool> result =
mDependentChildren.insert (DependentChildren::value_type (aUnk, aChild));
AssertMsg (result.second, ("Failed to insert child %p to the map\n", aUnk));
}
/** Helper for removeDependentChild(). */
void VirtualBoxBaseWithChildrenNEXT::doRemoveDependentChild (IUnknown *aUnk)
{
AssertReturnVoid (aUnk);
AutoCaller autoCaller (this);
/* sanity */
AssertReturnVoid (autoCaller.state() == InUninit ||
autoCaller.state() == InInit ||
autoCaller.state() == Ready ||
autoCaller.state() == Limited);
AutoWriteLock alock (childrenLock());
DependentChildren::size_type result = mDependentChildren.erase (aUnk);
AssertMsg (result == 1, ("Failed to remove child %p from the map\n", aUnk));
NOREF (result);
}
// Settings API additions
////////////////////////////////////////////////////////////////////////////////
#if defined VBOX_MAIN_SETTINGS_ADDONS
namespace settings
{
template<> stdx::char_auto_ptr
ToString <com::Bstr> (const com::Bstr &aValue, unsigned int aExtra)
{
stdx::char_auto_ptr result;
if (aValue.raw() == NULL)
throw ENoValue();
/* The only way to cause RTUtf16ToUtf8Ex return a number of bytes needed
* w/o allocating the result buffer itself is to provide that both cch
* and *ppsz are not NULL. */
char dummy [1];
char *dummy2 = dummy;
size_t strLen = 1;
int vrc = RTUtf16ToUtf8Ex (aValue.raw(), RTSTR_MAX,
&dummy2, strLen, &strLen);
if (RT_SUCCESS (vrc))
{
/* the string only contains '\0' :) */
result.reset (new char [1]);
result.get() [0] = '\0';
return result;
}
if (vrc == VERR_BUFFER_OVERFLOW)
{
result.reset (new char [strLen + 1]);
char *buf = result.get();
vrc = RTUtf16ToUtf8Ex (aValue.raw(), RTSTR_MAX, &buf, strLen + 1, NULL);
}
if (RT_FAILURE (vrc))
throw LogicError (RT_SRC_POS);
return result;
}
template<> com::Guid FromString <com::Guid> (const char *aValue)
{
if (aValue == NULL)
throw ENoValue();
/* For settings, the format is always {XXX...XXX} */
char buf [RTUUID_STR_LENGTH];
if (aValue == NULL || *aValue != '{' ||
strlen (aValue) != RTUUID_STR_LENGTH + 1 ||
aValue [RTUUID_STR_LENGTH] != '}')
throw ENoConversion (FmtStr ("'%s' is not Guid", aValue));
/* strip { and } */
memcpy (buf, aValue + 1, RTUUID_STR_LENGTH - 1);
buf [RTUUID_STR_LENGTH - 1] = '\0';
/* we don't use Guid (const char *) because we want to throw
* ENoConversion on format error */
RTUUID uuid;
int vrc = RTUuidFromStr (&uuid, buf);
if (RT_FAILURE (vrc))
throw ENoConversion (FmtStr ("'%s' is not Guid (%Rrc)", aValue, vrc));
return com::Guid (uuid);
}
template<> stdx::char_auto_ptr
ToString <com::Guid> (const com::Guid &aValue, unsigned int aExtra)
{
/* For settings, the format is always {XXX...XXX} */
stdx::char_auto_ptr result (new char [RTUUID_STR_LENGTH + 2]);
int vrc = RTUuidToStr (aValue.raw(), result.get() + 1, RTUUID_STR_LENGTH);
if (RT_FAILURE (vrc))
throw LogicError (RT_SRC_POS);
result.get() [0] = '{';
result.get() [RTUUID_STR_LENGTH] = '}';
result.get() [RTUUID_STR_LENGTH + 1] = '\0';
return result;
}
} /* namespace settings */
#endif /* VBOX_MAIN_SETTINGS_ADDONS */