sysresmonitoring/oommonitor/src/oomoutofmemorywatcher.cpp
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Wed, 13 Oct 2010 14:43:04 +0300
branchRCL_3
changeset 82 4610cd70c542
parent 63 c2c61fdca848
permissions -rw-r--r--
Revision: 201039 Kit: 201041

/*
* Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies). 
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:  Main classes for Out of Memory Monitor.
*
*/



#include "oomoutofmemorywatcher.h"
#include "oommemorymonitor.h"
#include "OomTraces.h"

// ======================================================================
// class COutOfMemoryWatcher
// - notifies when free memory crosses preset thresholds
// ======================================================================

// ---------------------------------------------------------
//
// ---------------------------------------------------------
//
COutOfMemoryWatcher* COutOfMemoryWatcher::NewL(CMemoryMonitor& aMonitor, TInt aLowThreshold, TInt aGoodThreshold)
    {
    FUNC_LOG;

    COutOfMemoryWatcher* self = new (ELeave) COutOfMemoryWatcher(aMonitor);
    CleanupStack::PushL(self);
    self->ConstructL(aLowThreshold, aGoodThreshold);
    CleanupStack::Pop(self);
    return self;
    }

// ---------------------------------------------------------
//
// ---------------------------------------------------------
//
COutOfMemoryWatcher::~COutOfMemoryWatcher()
    {
    FUNC_LOG;

    Cancel();
    }

// ---------------------------------------------------------
//
// ---------------------------------------------------------
//
COutOfMemoryWatcher::COutOfMemoryWatcher(CMemoryMonitor& aMonitor)
:   CActive(CActive::EPriorityStandard),
    iLafShutdown(aMonitor)
    {
    FUNC_LOG;

    CActiveScheduler::Add(this);
    }

// ---------------------------------------------------------
//
// ---------------------------------------------------------
//
void COutOfMemoryWatcher::ConstructL(TInt aLowThreshold, TInt aGoodThreshold)
    {
    FUNC_LOG;

    UserSvr::SetMemoryThresholds(aLowThreshold,aGoodThreshold);
    User::LeaveIfError(iChangeNotifier.Create());
    }

void COutOfMemoryWatcher::UpdateThresholds(TInt aLowThreshold, TInt aGoodThreshold)
    {
    FUNC_LOG;

    UserSvr::SetMemoryThresholds(aLowThreshold,aGoodThreshold);
    }

// ---------------------------------------------------------
//
// ---------------------------------------------------------
//
void COutOfMemoryWatcher::Start()
    {
    FUNC_LOG;

    if (!IsActive())
        {
        iChangeNotifier.Logon(iStatus);
        SetActive();
        }
    }

// ---------------------------------------------------------
//
// ---------------------------------------------------------
//
void COutOfMemoryWatcher::DoCancel()
    {
    FUNC_LOG;

    iChangeNotifier.LogonCancel();
    }

// ---------------------------------------------------------
//
// ---------------------------------------------------------
//
void COutOfMemoryWatcher::RunL()
    {
    FUNC_LOG;

    TInt status = iStatus.Int();

    if (status < 0)
        {
        User::Leave(status);
        }

    // Check for memory status change.
    if (status & EChangesFreeMemory)
        {
        iLafShutdown.FreeMemThresholdCrossedL();
        }

    // We are not active until FreeMemThresholdCrossedL returns.
    Start();
    }