camerauis/cameraxui/cxengine/src/cxememorymonitorprivate.cpp
branchRCL_3
changeset 54 bac7acad7cb3
parent 53 61bc0f252b2b
child 57 2c87b2808fd7
equal deleted inserted replaced
53:61bc0f252b2b 54:bac7acad7cb3
     1 /*
       
     2 * Copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 #include "cxememorymonitorprivate.h"
       
    19 
       
    20 #ifdef Q_OS_SYMBIAN
       
    21 #include <hal.h>
       
    22 #endif // Q_OS_SYMBIAN
       
    23 
       
    24 #include "cxutils.h"
       
    25 #include "cxenamespace.h"
       
    26 #include "cxefeaturemanager.h"
       
    27 
       
    28 namespace
       
    29 {
       
    30     const int FREEING_RETRIES = 2;
       
    31     const int MONITORING_INTERVAL = 10*1000; // in milliseconds
       
    32 }
       
    33 
       
    34 /*!
       
    35 * Constructor
       
    36 */
       
    37 CxeMemoryMonitorPrivate::CxeMemoryMonitorPrivate(CxeFeatureManager &features)
       
    38     : mFeatures(features)
       
    39 {
       
    40     CX_DEBUG_ENTER_FUNCTION();
       
    41 
       
    42     mFeatures.configuredValues(CxeVariationKeys::FREE_MEMORY_LEVELS, mLevels);
       
    43 
       
    44     CX_DEBUG(("CxeMemoryMonitorPrivate - trigger level: %d bytes", mLevels.value(Cxe::FreeMemoryTrigger)));
       
    45     CX_DEBUG(("CxeMemoryMonitorPrivate - target level:  %d bytes", mLevels.value(Cxe::FreeMemoryTarget)));
       
    46 
       
    47 #ifdef Q_OS_SYMBIAN
       
    48     // Request free memory as defined in Feature Manager.
       
    49     // If we cannot have enough free memory, throw error.
       
    50     bool ok = requestFreeMemory(mLevels.value(Cxe::FreeMemoryTarget));
       
    51     qt_symbian_throwIfError(ok ? KErrNone : KErrNoMemory);
       
    52 #endif // Q_OS_SYMBIAN
       
    53 
       
    54     CX_DEBUG_EXIT_FUNCTION();
       
    55 }
       
    56 
       
    57 /*!
       
    58 * Destructor
       
    59 */
       
    60 CxeMemoryMonitorPrivate::~CxeMemoryMonitorPrivate()
       
    61 {
       
    62     CX_DEBUG_ENTER_FUNCTION();
       
    63     stopMonitoring();
       
    64     CX_DEBUG_EXIT_FUNCTION();
       
    65 }
       
    66 
       
    67 
       
    68 /*!
       
    69 * Get the amount of free memory (RAM).
       
    70 * @return Free RAM in bytes.
       
    71 */
       
    72 int CxeMemoryMonitorPrivate::free() const
       
    73 {
       
    74 #ifdef Q_OS_SYMBIAN
       
    75     int ram(0);
       
    76     HAL::Get(HALData::EMemoryRAMFree, ram);
       
    77     return ram;
       
    78 #else
       
    79     //!@todo: real implementation for desktop etc?
       
    80     return 1024*1024*1024;
       
    81 #endif // Q_OS_SYMBIAN
       
    82 }
       
    83 
       
    84 
       
    85 /*!
       
    86 * Request to free memory (RAM) to achieve asked level.
       
    87 * @param required Required free memory after this call.
       
    88 * @return Was it possible to achieve the required level of free memory.
       
    89 */
       
    90 bool CxeMemoryMonitorPrivate::requestFreeMemory(int required)
       
    91 {
       
    92     CX_DEBUG_ENTER_FUNCTION();
       
    93 
       
    94     if (free() < required) {
       
    95 #ifdef Q_OS_SYMBIAN
       
    96     qt_symbian_throwIfError(mOomMonitor.Connect());
       
    97 
       
    98     int i(0);
       
    99     int status(KErrGeneral);
       
   100     do {
       
   101         status = mOomMonitor.RequestFreeMemory(required);
       
   102     } while (status != KErrNone && ++i < FREEING_RETRIES);
       
   103 
       
   104     mOomMonitor.Close();
       
   105 #endif // Q_OS_SYMBIAN
       
   106     }
       
   107 
       
   108     // If we have the free RAM we were asked for, all good.
       
   109     bool ok = free() >= required;
       
   110 
       
   111     CX_DEBUG_EXIT_FUNCTION();
       
   112     return ok;
       
   113 }
       
   114 
       
   115 /*!
       
   116 * Start monitoring free memory.
       
   117 */
       
   118 void CxeMemoryMonitorPrivate::startMonitoring()
       
   119 {
       
   120     if (!mTimer.isActive()) {
       
   121         connect(&mTimer, SIGNAL(timeout()), this, SLOT(checkMemory()), Qt::UniqueConnection);
       
   122         mTimer.start(MONITORING_INTERVAL);
       
   123     }
       
   124 }
       
   125 
       
   126 /*!
       
   127 * Stop monitoring free memory.
       
   128 */
       
   129 void CxeMemoryMonitorPrivate::stopMonitoring()
       
   130 {
       
   131     mTimer.stop();
       
   132 }
       
   133 
       
   134 /*!
       
   135 * Check free memory. Try to release memory if below trigger level.
       
   136 */
       
   137 void CxeMemoryMonitorPrivate::checkMemory()
       
   138 {
       
   139     CX_DEBUG_ENTER_FUNCTION();
       
   140 
       
   141     if(free() < mLevels.value(Cxe::FreeMemoryTrigger)) {
       
   142         // Memory too low, request more free memory.
       
   143         CX_DEBUG(("[WARNING] CxeMemoryMonitorPrivate - memory low, requesting more.."));
       
   144         requestFreeMemory(mLevels.value(Cxe::FreeMemoryTarget));
       
   145     }
       
   146     CX_DEBUG_EXIT_FUNCTION();
       
   147 }
       
   148 
       
   149 
       
   150 // end of file