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