AppMemMgr.c
上传用户:lqx1163
上传日期:2014-08-13
资源大小:9183k
文件大小:62k
源码类别:

MTK

开发平台:

C/C++

  1. /*****************************************************************************
  2. *  Copyright Statement:
  3. *  --------------------
  4. *  This software is protected by Copyright and the information contained
  5. *  herein is confidential. The software may not be copied and the information
  6. *  contained herein may not be used or disclosed except with the written
  7. *  permission of MediaTek Inc. (C) 2005
  8. *
  9. *  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  10. *  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  11. *  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
  12. *  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  13. *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  14. *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  15. *  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  16. *  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  17. *  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
  18. *  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
  19. *  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
  20. *  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
  21. *
  22. *  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
  23. *  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  24. *  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  25. *  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
  26. *  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. 
  27. *
  28. *  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
  29. *  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
  30. *  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
  31. *  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
  32. *  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
  33. *
  34. *****************************************************************************/
  35. /*****************************************************************************
  36.  *
  37.  * Filename:
  38.  * ---------
  39.  *  AppMemMgr.c
  40.  *
  41.  * Project:
  42.  * --------
  43.  *  MAUI
  44.  *
  45.  * Description:
  46.  * ------------
  47.  *  App-Based Shared Memory Manager
  48.  *
  49.  *  App-based shared memory is allocated before entering an application, and 
  50.  *  released when exiting an application. 
  51.  *
  52.  * Author:
  53.  * -------
  54.  * -------
  55.  *                      
  56.  *============================================================================
  57.  *             HISTORY
  58.  * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  59.  *------------------------------------------------------------------------------
  60.  * removed!
  61.  *
  62.  * removed!
  63.  * removed!
  64.  * removed!
  65.  *
  66.  * removed!
  67.  * removed!
  68.  * removed!
  69.  *
  70.  * removed!
  71.  * removed!
  72.  * removed!
  73.  *
  74.  * removed!
  75.  * removed!
  76.  * removed!
  77.  *
  78.  *------------------------------------------------------------------------------
  79.  * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  80.  *============================================================================
  81.  ****************************************************************************/
  82. /***************************************************************************** 
  83.  * Include
  84.  *****************************************************************************/
  85. #include "MMI_features.h"
  86. #include "stdC.h"
  87. #include "L4Dr1.h"
  88. #include "GlobalDefs.h"
  89. #include "DebugInitDef.h"
  90. #include "AppMemMgrGprot.h"
  91. #include "CommonScreensEnum.h"
  92. #include "CommonScreens.h"
  93. #include "SettingProfile.h"         /* for tone ID */
  94. #include "wgui_categories_fmgr.h"   /* Category 212 */
  95. #ifndef MMI_ON_WIN32
  96. #include "mmiapi_func.h"
  97. #endif
  98. #include "kal_non_specific_general_types.h"
  99. #include "app_mem.h"
  100. /***************************************************************************** 
  101.  * Define
  102.  *****************************************************************************/
  103. /***************************************************************************** 
  104.  * Typedef 
  105.  *****************************************************************************/
  106. /* States of mmi_frm_appmem_stop_confirm_screen_yes() */
  107. typedef enum
  108. {
  109.     /* Normal case */
  110.     MMI_FRM_APPMEM_STOP_STATE_IGNORE,
  111.     /* Inside mmi_frm_appmem_stop_confirm_screen_yes() */
  112.     MMI_FRM_APPMEM_STOP_STATE_WAITING,
  113.     /* Stop operation finished inside mmi_frm_appmem_stop_confirm_screen_yes() */
  114.     MMI_FRM_APPMEM_STOP_STATE_FINISHED
  115. } mmi_frm_appmem_stop_state_enum;
  116. /* Context of the prompt screen */
  117. typedef struct 
  118. {
  119.     /* Properties of the application requesting to free up more memory */
  120.     MMI_ID_TYPE app_name;
  121.     MMI_ID_TYPE app_icon;
  122.     /* Requested memory size */
  123.     U32 required_size;
  124.     MMI_BOOL require_full_pool;
  125.     /* Used inside mmi_frm_appmem_stop_confirm_screen_yes() */
  126.     mmi_frm_appmem_stop_state_enum stop_state;
  127.     kal_uint32 app_id_confirm_to_stop;
  128.     /* Keep SCR_APPMEM_STOP_PROMPT & SCR_APPMEM_STOP_PROGRESSING in history 
  129.        at its exit function. */
  130.     MMI_BOOL keep_prompt_screen_in_history;
  131.     void (*success_callback)(void);
  132. } mmi_frm_appmem_prompt_struct;
  133. typedef struct 
  134. {
  135.     /* Number of applications using App-based ASM */
  136.     S32 num_app;
  137.     /* Detailed information */
  138.     applib_mem_ap_usage_struct applist[MMI_FRM_APPMEM_MAX_APP_PROMPTED];
  139. } mmi_frm_appmem_prompt_screen_struct;
  140. /*****************************************************************************              
  141.  * Local Variable                                                                    
  142.  *****************************************************************************/
  143. /* Life cycle: prompt user to stop applications -> successfully get the memory or cancelled */
  144. static mmi_frm_appmem_prompt_struct g_mmi_frm_appmem_prompt;
  145. /* Life cycle: inside the prompt screen only (SCR_APPMEM_STOP_PROMPT) */
  146. static mmi_frm_appmem_prompt_screen_struct *g_mmi_frm_appmem_prompt_screen = NULL;
  147. /*****************************************************************************              
  148.  * Local Function                                                                    
  149.  *****************************************************************************/
  150. /* Prototype declarations: */
  151. static void mmi_frm_appmem_entry_prompt_screen(void);
  152. static void mmi_frm_appmem_entry_progressing_screen(void);
  153. static void mmi_frm_appmem_invoke_release_mem_success_callback(void);
  154. static void mmi_frm_appmem_stop_finished_handler(kal_uint32 app_id, kal_uint32 string_id, kal_bool result);
  155. /*****************************************************************************
  156.  * FUNCTION
  157.  *  mmi_frm_appmem_is_in_prompt_screens
  158.  * DESCRIPTION
  159.  *  Whether MMI is inside prompt screens (SCR_APPMEM_STOP_PROMPT and SCR_APPMEM_STOP_PROGRESSING)
  160.  * PARAMETERS
  161.  *  void
  162.  * RETURNS
  163.  *  MMI_BOOL
  164.  *****************************************************************************/
  165. static MMI_BOOL mmi_frm_appmem_is_in_prompt_screens(void)
  166. {
  167.     /*----------------------------------------------------------------*/
  168.     /* Local Variables                                                */
  169.     /*----------------------------------------------------------------*/
  170.     U16 curr_scr_id;
  171.     /*----------------------------------------------------------------*/
  172.     /* Code Body                                                      */
  173.     /*----------------------------------------------------------------*/
  174.     curr_scr_id = GetExitScrnID();
  175.     return curr_scr_id == SCR_APPMEM_STOP_PROMPT || 
  176.            curr_scr_id == SCR_APPMEM_STOP_PROGRESSING;
  177. }
  178. /*****************************************************************************
  179.  * FUNCTION
  180.  *  mmi_frm_appmem_is_prompt_screens_in_history
  181.  * DESCRIPTION
  182.  *  Whether a prompt screen (SCR_APPMEM_STOP_PROMPT or SCR_APPMEM_STOP_PROGRESSING)
  183.  *  is inside history.
  184.  * PARAMETERS
  185.  *  void
  186.  * RETURNS
  187.  *  MMI_BOOL
  188.  *****************************************************************************/
  189. static MMI_BOOL mmi_frm_appmem_is_prompt_screens_in_history(void)
  190. {
  191.     /*----------------------------------------------------------------*/
  192.     /* Local Variables                                                */
  193.     /*----------------------------------------------------------------*/
  194.     /*----------------------------------------------------------------*/
  195.     /* Code Body                                                      */
  196.     /*----------------------------------------------------------------*/
  197.     return IsScreenPresent(SCR_APPMEM_STOP_PROMPT) || 
  198.            IsScreenPresent(SCR_APPMEM_STOP_PROGRESSING);
  199. }
  200. /*****************************************************************************
  201.  * FUNCTION
  202.  *  mmi_frm_appmem_jump_to_prompt_screen
  203.  * DESCRIPTION
  204.  *  Go back to the prompt screen
  205.  * PARAMETERS
  206.  *  void
  207.  * RETURNS
  208.  *  void
  209.  *****************************************************************************/
  210. static void mmi_frm_appmem_jump_to_prompt_screen(void)
  211. {
  212.     /*----------------------------------------------------------------*/
  213.     /* Local Variables                                                */
  214.     /*----------------------------------------------------------------*/
  215.     /*----------------------------------------------------------------*/
  216.     /* Code Body                                                      */
  217.     /*----------------------------------------------------------------*/
  218.     MMI_ASSERT(IsScreenPresent(SCR_APPMEM_STOP_PROMPT));
  219.     GoBackToHistory(SCR_APPMEM_STOP_PROMPT);
  220. }
  221.  
  222. /*****************************************************************************
  223.  * FUNCTION
  224.  *  mmi_frm_appmem_exit_progressing_screen
  225.  * DESCRIPTION
  226.  *  Exit function of progressing screen 
  227.  * PARAMETERS
  228.  *  void
  229.  * RETURNS
  230.  *  void
  231.  *****************************************************************************/
  232. static void mmi_frm_appmem_exit_progressing_screen(void)
  233. {
  234.     /*----------------------------------------------------------------*/
  235.     /* Local Variables                                                */
  236.     /*----------------------------------------------------------------*/
  237.     history h;
  238.     /*----------------------------------------------------------------*/
  239.     /* Code Body                                                      */
  240.     /*----------------------------------------------------------------*/    
  241.     if (g_mmi_frm_appmem_prompt.keep_prompt_screen_in_history)
  242.     {
  243.         h.scrnID = SCR_APPMEM_STOP_PROGRESSING;
  244.         h.entryFuncPtr = mmi_frm_appmem_entry_progressing_screen;
  245.         pfnUnicodeStrcpy((S8*) h.inputBuffer, (S8*) L"");
  246.         GetCategoryHistory(h.guiBuffer);
  247.     
  248.         AddHistory(h);
  249.     }
  250. }
  251. /*****************************************************************************
  252.  * FUNCTION
  253.  *  mmi_frm_appmem_check_enough_memory
  254.  * DESCRIPTION
  255.  *  Check if the free memory is enough
  256.  * PARAMETERS
  257.  *  void
  258.  * RETURNS
  259.  *  MMI_BOOL
  260.  *****************************************************************************/
  261. static MMI_BOOL mmi_frm_appmem_check_enough_memory(void)
  262. {
  263.     /*----------------------------------------------------------------*/
  264.     /* Local Variables                                                */
  265.     /*----------------------------------------------------------------*/
  266.     /*----------------------------------------------------------------*/
  267.     /* Code Body                                                      */
  268.     /*----------------------------------------------------------------*/
  269.     if (g_mmi_frm_appmem_prompt.require_full_pool && applib_mem_ap_get_alloc_count() == 0)
  270.     {
  271.         return MMI_TRUE;
  272.     }
  273.     if (!g_mmi_frm_appmem_prompt.require_full_pool && 
  274.          applib_mem_ap_get_max_alloc_size() >= g_mmi_frm_appmem_prompt.required_size)
  275.     {
  276.         return MMI_TRUE;
  277.     }
  278.     return MMI_FALSE;
  279. }
  280. /*****************************************************************************
  281.  * FUNCTION
  282.  *  mmi_frm_appmem_entry_progressing_screen
  283.  * DESCRIPTION
  284.  *  Entry function of progressing screen when an application has not finished stopping
  285.  * PARAMETERS
  286.  *  void
  287.  * RETURNS
  288.  *  void
  289.  *****************************************************************************/
  290. static void mmi_frm_appmem_entry_progressing_screen(void)
  291. {
  292.     /*----------------------------------------------------------------*/
  293.     /* Local Variables                                                */
  294.     /*----------------------------------------------------------------*/
  295.     U8 *guiBuffer;
  296.     /*----------------------------------------------------------------*/
  297.     /* Code Body                                                      */
  298.     /*----------------------------------------------------------------*/
  299.     if (mmi_frm_appmem_check_enough_memory() &&
  300.         !is_redrawing_old_screens())    /* not redrawn by small screen */
  301.     {
  302.         DisplayPopupCallBack(
  303.             (U8*) GetString(STR_ID_APPMEM_NOTIFY_STARTING_NEW), 
  304.             IMG_GLOBAL_INFO, 
  305.             0, 
  306.             UI_POPUP_NOTIFYDURATION_TIME_2000, 
  307.             SUCCESS_TONE, 
  308.             mmi_frm_appmem_invoke_release_mem_success_callback);
  309.     }
  310.     else
  311.     {    
  312.         EntryNewScreen(SCR_APPMEM_STOP_PROGRESSING, mmi_frm_appmem_exit_progressing_screen, NULL, NULL);     
  313.         guiBuffer = GetCurrGuiBuffer(SCR_APPMEM_STOP_PROGRESSING);
  314.         ShowCategory8Screen(
  315.             0,
  316.             0,
  317.             0,                                  /* Left softkey */
  318.             0,
  319.             STR_GLOBAL_BACK,                    /* Right softkey */
  320.             0,
  321.             STR_ID_APPMEM_WAITING_FOR_STOP,     /* Message */
  322.             IMG_GLOBAL_PROGRESS,
  323.             guiBuffer);
  324.  
  325.         SetRightSoftkeyFunction(mmi_frm_appmem_jump_to_prompt_screen, KEY_EVENT_UP);
  326.     }
  327. }
  328. /*****************************************************************************
  329.  * FUNCTION
  330.  *  mmi_frm_appmem_stop_confirm_screen_yes
  331.  * DESCRIPTION
  332.  *  User confirm to stop an application
  333.  * PARAMETERS
  334.  *  void
  335.  * RETURNS
  336.  *  void
  337.  *****************************************************************************/
  338. static void mmi_frm_appmem_stop_confirm_screen_yes(void)
  339. {
  340.     /*----------------------------------------------------------------*/
  341.     /* Local Variables                                                */
  342.     /*----------------------------------------------------------------*/
  343.     /*----------------------------------------------------------------*/
  344.     /* Code Body                                                      */
  345.     /*----------------------------------------------------------------*/
  346.     g_mmi_frm_appmem_prompt.stop_state = MMI_FRM_APPMEM_STOP_STATE_WAITING;
  347.     applib_mem_ap_stop_app_by_MMI_task(g_mmi_frm_appmem_prompt.app_id_confirm_to_stop);
  348.     
  349.     if (g_mmi_frm_appmem_prompt.stop_state != MMI_FRM_APPMEM_STOP_STATE_FINISHED)
  350.     {
  351.         /* mmi_frm_appmem_stop_finished_handler() was not invoked inside
  352.            applib_mem_ap_stop_app_by_MMI_task(), we display progressing
  353.            screen to wait for mmi_frm_appmem_stop_finished_handler() */ 
  354.         mmi_frm_appmem_entry_progressing_screen();
  355.     }
  356.     g_mmi_frm_appmem_prompt.stop_state = MMI_FRM_APPMEM_STOP_STATE_IGNORE;    
  357. }
  358. /*****************************************************************************
  359.  * FUNCTION
  360.  *  mmi_frm_appmem_entry_stop_confirm_screen
  361.  * DESCRIPTION
  362.  *  Ask user to confirm to stop application
  363.  * PARAMETERS
  364.  *  void
  365.  * RETURNS
  366.  *  void
  367.  *****************************************************************************/
  368. static void mmi_frm_appmem_entry_stop_confirm_screen(void)
  369. {
  370.     /*----------------------------------------------------------------*/
  371.     /* Local Variables                                                */
  372.     /*----------------------------------------------------------------*/
  373.     /*----------------------------------------------------------------*/
  374.     /* Code Body                                                      */
  375.     /*----------------------------------------------------------------*/
  376.     DisplayConfirm(
  377.         STR_GLOBAL_YES,
  378.         IMG_GLOBAL_YES,
  379.         STR_GLOBAL_NO,
  380.         IMG_GLOBAL_NO,
  381.         (UI_string_type) GetString(STR_ID_APPMEM_STOP),
  382.         IMG_GLOBAL_QUESTION,
  383.         WARNING_TONE);
  384.     SetLeftSoftkeyFunction(mmi_frm_appmem_stop_confirm_screen_yes, KEY_EVENT_UP);
  385.     SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);
  386. }
  387. /*****************************************************************************
  388.  * FUNCTION
  389.  *  mmi_frm_appmem_prompt_screen_get_item
  390.  * DESCRIPTION
  391.  *  Callback function to draw the info box of prompt screen
  392.  * PARAMETERS
  393.  *  item_index      [IN]        Index of menu item
  394.  *  str_buff        [OUT]       Output text buffer
  395.  *  img_buff_p      [OUT]       Output image buffer
  396.  *  str_img_mask    [OUT]       (deprecated)
  397.  * RETURNS
  398.  *  Success or not
  399.  *****************************************************************************/
  400. static pBOOL mmi_frm_appmem_prompt_screen_get_item(
  401.                 S32 item_index, 
  402.                 UI_string_type str_buff, 
  403.                 PU8 *img_buff_p, 
  404.                 U8 str_img_mask)
  405. {
  406.     /*----------------------------------------------------------------*/
  407.     /* Local Variables                                                */
  408.     /*----------------------------------------------------------------*/
  409.     applib_mem_ap_usage_struct *applist;
  410.     S8 size_str[32];
  411.     S8 size_str_ucs2[32 * 2];
  412.     S32 app_name_len;
  413.     /*----------------------------------------------------------------*/
  414.     /* Code Body                                                      */
  415.     /*----------------------------------------------------------------*/
  416.     MMI_ASSERT(g_mmi_frm_appmem_prompt_screen &&
  417.                item_index < g_mmi_frm_appmem_prompt_screen->num_app);
  418.     applist = g_mmi_frm_appmem_prompt_screen->applist;
  419.     
  420.     UCS2Strncpy((S8*) str_buff, GetString((U16) applist[item_index].string_id), (MAX_SUB_MENU_SIZE / 2));
  421.     app_name_len = UCS2Strlen((S8*) str_buff);
  422.     if (applist[item_index].is_stopping)
  423.     {
  424.         UCS2Strncpy(
  425.             (S8*) str_buff + (app_name_len * 2), 
  426.             (S8*) GetString(STR_ID_APPMEM_ATTR_STOPPING),
  427.             (MAX_SUB_MENU_SIZE / 2) - app_name_len);
  428.     }
  429.     else
  430.     {
  431.         sprintf((char*) size_str, " (%d K)", applist[item_index].mem_size / 1024);
  432.         AnsiiToUnicodeString(size_str_ucs2, size_str);
  433.         
  434.         UCS2Strncpy(
  435.             (S8*) str_buff + (app_name_len * 2), 
  436.             size_str_ucs2, 
  437.             (MAX_SUB_MENU_SIZE / 2) - app_name_len);
  438.     }
  439.     
  440.     *img_buff_p = (PU8) GetImage((U16) applist[item_index].icon_id);
  441.     return (pBOOL) MMI_TRUE;
  442. }
  443. /*****************************************************************************
  444.  * FUNCTION
  445.  *  mmi_frm_appmem_prompt_screen_init_info
  446.  * DESCRIPTION
  447.  *  Initialize the info box
  448.  * PARAMETERS
  449.  *  void
  450.  * RETURNS
  451.  *  The minimum height of info box
  452.  *****************************************************************************/
  453. static S32 mmi_frm_appmem_prompt_screen_init_info(void)
  454. {
  455.     /*----------------------------------------------------------------*/
  456.     /* Local Variables                                                */
  457.     /*----------------------------------------------------------------*/
  458.     S32 msg_length;
  459.     UI_string_type msg;
  460.     /*----------------------------------------------------------------*/
  461.     /* Code Body                                                      */
  462.     /*----------------------------------------------------------------*/
  463.     msg = (UI_string_type) GetString(STR_ID_APPMEM_PROMPT_STOP_APP);
  464.     msg_length = gui_strlen(msg);
  465.     create_multiline_inputbox_set_buffer(msg, msg_length, msg_length, 0);
  466.     resize_multiline_inputbox(UI_device_width, UI_device_height);
  467.     
  468.     MMI_multiline_inputbox.flags |= UI_MULTI_LINE_INPUT_BOX_DISABLE_CURSOR_DRAW | 
  469.         UI_MULTI_LINE_INPUT_BOX_VIEW_MODE | UI_MULTI_LINE_INPUT_BOX_DISABLE_SCROLLBAR;
  470.     show_multiline_inputbox_no_draw();
  471.     /* Add (get_multiline_inputbox_line_height() / 4) for extra spacing */
  472.     return get_multiline_inputbox_line_height() * MMI_multiline_inputbox.n_lines +
  473.            MMI_multiline_inputbox.text_y + (get_multiline_inputbox_line_height() / 4);
  474. }
  475. /*****************************************************************************
  476.  * FUNCTION
  477.  *  mmi_frm_appmem_prompt_screen_draw_info
  478.  * DESCRIPTION
  479.  *  Callback function to draw the info box of prompt screen
  480.  * PARAMETERS
  481.  *  idx     [IN]    menu item index
  482.  *  x1      [IN]    x of left-top point
  483.  *  y1      [IN]    y of left-top point
  484.  *  x2      [IN]    x of right-bottom point
  485.  *  y2      [IN]    y of right-bottom point
  486.  * RETURNS
  487.  *  void
  488.  *****************************************************************************/
  489. static void mmi_frm_appmem_prompt_screen_draw_info(S32 idx, S32 x1, S32 y1, S32 x2, S32 y2)
  490. {
  491.     /*----------------------------------------------------------------*/
  492.     /* Local Variables                                                */
  493.     /*----------------------------------------------------------------*/
  494.     
  495.     /*----------------------------------------------------------------*/
  496.     /* Code Body                                                      */
  497.     /*----------------------------------------------------------------*/
  498.     move_multiline_inputbox(x1, y1);
  499.     resize_multiline_inputbox(x2 - x1 + 1, y2 - y1 + 1);
  500.     show_multiline_inputbox_no_draw();
  501.     
  502.     show_multiline_inputbox();
  503. }
  504. /*****************************************************************************
  505.  * FUNCTION
  506.  *  mmi_frm_appmem_prompt_screen_hide_info
  507.  * DESCRIPTION
  508.  *  Callback function to hide the info box of prompt screen
  509.  * PARAMETERS
  510.  *  void
  511.  * RETURNS
  512.  *  void
  513.  *****************************************************************************/
  514. static void mmi_frm_appmem_prompt_screen_hide_info(void)
  515. {
  516.     /*----------------------------------------------------------------*/
  517.     /* Local Variables                                                */
  518.     /*----------------------------------------------------------------*/
  519.     /*----------------------------------------------------------------*/
  520.     /* Code Body                                                      */
  521.     /*----------------------------------------------------------------*/
  522.     
  523. }
  524. /*****************************************************************************
  525.  * FUNCTION
  526.  *  mmi_frm_appmem_prompt_screen_stop_highlighted
  527.  * DESCRIPTION
  528.  *  Stop the current highlighted application
  529.  * PARAMETERS
  530.  *  void
  531.  * RETURNS
  532.  *  void
  533.  *****************************************************************************/
  534. static void mmi_frm_appmem_prompt_screen_stop_highlighted(void)
  535. {
  536.     /*----------------------------------------------------------------*/
  537.     /* Local Variables                                                */
  538.     /*----------------------------------------------------------------*/
  539.     S32 highlited_item;
  540.     /*----------------------------------------------------------------*/
  541.     /* Code Body                                                      */
  542.     /*----------------------------------------------------------------*/    
  543.     MMI_ASSERT(GetExitScrnID() == SCR_APPMEM_STOP_PROMPT &&
  544.                g_mmi_frm_appmem_prompt_screen);
  545.     highlited_item = GetHighlightedItem();
  546.     MMI_ASSERT(highlited_item < g_mmi_frm_appmem_prompt_screen->num_app);
  547.     g_mmi_frm_appmem_prompt.app_id_confirm_to_stop = 
  548.         g_mmi_frm_appmem_prompt_screen->applist[highlited_item].app_id;
  549.     mmi_frm_appmem_entry_stop_confirm_screen();
  550. }
  551. /*****************************************************************************
  552.  * FUNCTION
  553.  *  mmi_frm_appmem_prompt_screen_wait_highlighted
  554.  * DESCRIPTION
  555.  *  Wait a stopping application
  556.  * PARAMETERS
  557.  *  void
  558.  * RETURNS
  559.  *  void
  560.  *****************************************************************************/
  561. static void mmi_frm_appmem_prompt_screen_wait_highlighted(void)
  562. {
  563.     /*----------------------------------------------------------------*/
  564.     /* Local Variables                                                */
  565.     /*----------------------------------------------------------------*/
  566.     /*----------------------------------------------------------------*/
  567.     /* Code Body                                                      */
  568.     /*----------------------------------------------------------------*/    
  569.     mmi_frm_appmem_entry_progressing_screen();
  570. }
  571. /*****************************************************************************
  572.  * FUNCTION
  573.  *  mmi_frm_appmem_prompt_screen_highlight_hdlr
  574.  * DESCRIPTION
  575.  *  Highlight handler of prompt screen
  576.  * PARAMETERS
  577.  *  item_index  [IN]    currently highlighted menu index
  578.  * RETURNS
  579.  *  void
  580.  *****************************************************************************/
  581. static void mmi_frm_appmem_prompt_screen_highlight_hdlr(S32 item_index)
  582. {
  583.     /*----------------------------------------------------------------*/
  584.     /* Local Variables                                                */
  585.     /*----------------------------------------------------------------*/
  586.     /*----------------------------------------------------------------*/
  587.     /* Code Body                                                      */
  588.     /*----------------------------------------------------------------*/    
  589.     if (g_mmi_frm_appmem_prompt_screen->num_app == 0)
  590.     {
  591.         return;
  592.     }
  593.     
  594.     MMI_ASSERT(g_mmi_frm_appmem_prompt_screen && 
  595.                item_index < g_mmi_frm_appmem_prompt_screen->num_app);
  596.     
  597.     if (!g_mmi_frm_appmem_prompt_screen->applist[item_index].is_stopping)
  598.     {
  599.         ChangeLeftSoftkey(STR_ID_APPMEM_STOP, 0);
  600.         SetLeftSoftkeyFunction(mmi_frm_appmem_prompt_screen_stop_highlighted, KEY_EVENT_UP);
  601.     }
  602.     else
  603.     {
  604.         ChangeLeftSoftkey(STR_ID_APPMEM_WAIT, 0);
  605.         SetLeftSoftkeyFunction(mmi_frm_appmem_prompt_screen_wait_highlighted, KEY_EVENT_UP);
  606.     }
  607. }
  608. /*****************************************************************************
  609.  * FUNCTION
  610.  *  mmi_frm_appmem_exit_prompt_screen
  611.  * DESCRIPTION
  612.  *  Exit function of prompt screen 
  613.  * PARAMETERS
  614.  *  void
  615.  * RETURNS
  616.  *  void
  617.  *****************************************************************************/
  618. static void mmi_frm_appmem_exit_prompt_screen(void)
  619. {
  620.     /*----------------------------------------------------------------*/
  621.     /* Local Variables                                                */
  622.     /*----------------------------------------------------------------*/
  623.     history h;
  624.     /*----------------------------------------------------------------*/
  625.     /* Code Body                                                      */
  626.     /*----------------------------------------------------------------*/    
  627.     if (g_mmi_frm_appmem_prompt.keep_prompt_screen_in_history)
  628.     {
  629.         h.scrnID = SCR_APPMEM_STOP_PROMPT;
  630.         h.entryFuncPtr = mmi_frm_appmem_entry_prompt_screen;
  631.         pfnUnicodeStrcpy((S8*) h.inputBuffer, (S8*) L"");
  632.         GetCategoryHistory(h.guiBuffer);
  633.     
  634.         AddHistory(h);
  635.     }
  636.     OslMfree(g_mmi_frm_appmem_prompt_screen);
  637.     g_mmi_frm_appmem_prompt_screen = NULL;
  638. }
  639. /*****************************************************************************
  640.  * FUNCTION
  641.  *  mmi_frm_appmem_entry_prompt_screen
  642.  * DESCRIPTION
  643.  *  Entry function of prompt screen to stop other applications
  644.  * PARAMETERS
  645.  *  void
  646.  * RETURNS
  647.  *  void
  648.  *****************************************************************************/
  649. static void mmi_frm_appmem_entry_prompt_screen(void)
  650. {
  651.     /*----------------------------------------------------------------*/
  652.     /* Local Variables                                                */
  653.     /*----------------------------------------------------------------*/
  654.     U8 *guiBuffer;
  655.     S32 info_box_height;
  656.     /*----------------------------------------------------------------*/
  657.     /* Code Body                                                      */
  658.     /*----------------------------------------------------------------*/
  659.     MMI_ASSERT(g_mmi_frm_appmem_prompt_screen == NULL);
  660.     
  661.     if (mmi_frm_appmem_check_enough_memory() && 
  662.         !is_redrawing_old_screens())    /* not redrawn by small screen */
  663.     {
  664.         DisplayPopupCallBack(
  665.             (U8*) GetString(STR_ID_APPMEM_NOTIFY_STARTING_NEW), 
  666.             IMG_GLOBAL_INFO, 
  667.             0, 
  668.             UI_POPUP_NOTIFYDURATION_TIME_2000, 
  669.             SUCCESS_TONE, 
  670.             mmi_frm_appmem_invoke_release_mem_success_callback);
  671.     }
  672.     else
  673.     {
  674.         EntryNewScreen(SCR_APPMEM_STOP_PROMPT, mmi_frm_appmem_exit_prompt_screen, NULL, NULL);     
  675.         guiBuffer = GetCurrGuiBuffer(SCR_APPMEM_STOP_PROMPT);
  676.         g_mmi_frm_appmem_prompt_screen = 
  677.             (mmi_frm_appmem_prompt_screen_struct*) OslMalloc(sizeof(mmi_frm_appmem_prompt_screen_struct));
  678.         g_mmi_frm_appmem_prompt_screen->num_app = 
  679.             (S32) applib_mem_ap_get_current_usage(
  680.                     g_mmi_frm_appmem_prompt_screen->applist,
  681.                     MMI_FRM_APPMEM_MAX_APP_PROMPTED);
  682.         info_box_height = mmi_frm_appmem_prompt_screen_init_info();        
  683.         RegisterHighlightHandler(mmi_frm_appmem_prompt_screen_highlight_hdlr);
  684.         
  685.         ShowCategory211Screen(
  686.             STR_ID_APPMEM_OUT_OF_MEMORY,
  687.             0, 
  688.             STR_ID_APPMEM_STOP,
  689.             0, 
  690.             STR_GLOBAL_BACK, 
  691.             0, 
  692.             g_mmi_frm_appmem_prompt_screen->num_app, 
  693.             mmi_frm_appmem_prompt_screen_get_item, 
  694.             NULL, 
  695.             mmi_frm_appmem_prompt_screen_draw_info,
  696.             mmi_frm_appmem_prompt_screen_hide_info,
  697.             info_box_height,            /* Info box height */
  698.             0,                          /* Background image of info box */
  699.             0,                          /* Highlight item index */
  700.             guiBuffer);
  701.        SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);
  702.     }
  703. }
  704. /*****************************************************************************
  705.  * FUNCTION
  706.  *  mmi_frm_appmem_invoke_release_mem_success_callback
  707.  * DESCRIPTION
  708.  *  Invoke the calling application there is enough memory
  709.  * PARAMETERS
  710.  *  void
  711.  * RETURNS
  712.  *  void
  713.  *****************************************************************************/
  714. static void mmi_frm_appmem_invoke_release_mem_success_callback(void)
  715. {
  716.     /*----------------------------------------------------------------*/
  717.     /* Local Variables                                                */
  718.     /*----------------------------------------------------------------*/
  719.     /*----------------------------------------------------------------*/
  720.     /* Code Body                                                      */
  721.     /*----------------------------------------------------------------*/
  722.     MMI_ASSERT(mmi_frm_appmem_check_enough_memory());
  723. #ifndef MMI_ON_WIN32
  724.  MMI_ASSERT(mmiapi_is_MMI_task());
  725. #endif
  726.     
  727.     DeleteScreenIfPresent(SCR_APPMEM_STOP_PROMPT);
  728.     DeleteScreenIfPresent(SCR_APPMEM_STOP_PROGRESSING);
  729.     /* Enter another screen by registered callback function */
  730.     g_mmi_frm_appmem_prompt.keep_prompt_screen_in_history = MMI_FALSE;
  731.     g_mmi_frm_appmem_prompt.success_callback();
  732.     /* The callback function must enter new MMI screen */
  733.     MMI_ASSERT(!mmi_frm_appmem_is_in_prompt_screens() &&
  734.                !mmi_frm_appmem_is_prompt_screens_in_history());
  735. }
  736. /*****************************************************************************
  737.  * FUNCTION
  738.  *  mmi_frm_appmem_stop_finished_handler
  739.  * DESCRIPTION
  740.  *  Callback handler when an application finished stop operation
  741.  * PARAMETERS
  742.  *  app_id      [IN]    ID of the application to be stopped
  743.  *  string_id   [IN]    Name of the application to be stopped
  744.  *  result      [IN]    whether the stop operation suceeds or not
  745.  * RETURNS
  746.  *  void
  747.  *****************************************************************************/
  748. static void mmi_frm_appmem_stop_finished_handler(kal_uint32 app_id, kal_uint32 string_id, kal_bool result)
  749. {
  750.     /*----------------------------------------------------------------*/
  751.     /* Local Variables                                                */
  752.     /*----------------------------------------------------------------*/
  753.     /*----------------------------------------------------------------*/
  754.     /* Code Body                                                      */
  755.     /*----------------------------------------------------------------*/
  756.     if (mmi_frm_appmem_is_in_prompt_screens() ||
  757.         g_mmi_frm_appmem_prompt.stop_state == MMI_FRM_APPMEM_STOP_STATE_WAITING) /* In confirm screen */
  758.     {
  759.         U8 *msg;
  760.         U16 icon;
  761.         U8 tone;
  762.         FuncPtr callback;
  763.         if (mmi_frm_appmem_check_enough_memory())
  764.         {
  765.             msg = (U8*) GetString(STR_ID_APPMEM_NOTIFY_STARTING_NEW);
  766.             icon = IMG_GLOBAL_INFO;
  767.             tone = SUCCESS_TONE;        
  768.             callback = mmi_frm_appmem_invoke_release_mem_success_callback;
  769.         }
  770.         else if (result) 
  771.         {
  772.             msg = (U8*) GetString(STR_ID_APPMEM_NOTIFY_STOPPED);
  773.             icon = IMG_GLOBAL_INFO;
  774.             tone = SUCCESS_TONE;
  775.             callback = mmi_frm_appmem_jump_to_prompt_screen;
  776.         }
  777.         else
  778.         {
  779.             msg = (U8*) GetString(STR_ID_APPMEM_NOTIFY_STOP_FAILED);
  780.             icon = IMG_GLOBAL_WARNING;
  781.             tone = ERROR_TONE;
  782.             callback = mmi_frm_appmem_jump_to_prompt_screen;
  783.         }
  784.     
  785.         DisplayPopupCallBack(msg, icon, 0, UI_POPUP_NOTIFYDURATION_TIME_2000, tone, callback);
  786.         if (g_mmi_frm_appmem_prompt.stop_state == MMI_FRM_APPMEM_STOP_STATE_WAITING)
  787.         {
  788.             g_mmi_frm_appmem_prompt.stop_state = MMI_FRM_APPMEM_STOP_STATE_FINISHED;
  789.         }
  790.     }
  791.     else 
  792.     {
  793.         /* 
  794.          * 1. If prompt screen is in history - it will be processed after goback to prompt screen 
  795.          * 2. If prompt screen is not in history - silently ignore the result 
  796.          *    (Maybe it goes back to idle screen)
  797.          */
  798.     }
  799. }
  800. /*****************************************************************************              
  801.  * Extern Global Variable                                                                    
  802.  *****************************************************************************/
  803. /*****************************************************************************              
  804.  * Extern Global Function                                                                    
  805.  *****************************************************************************/
  806. /*****************************************************************************
  807.  * FUNCTION
  808.  *  mmi_frm_appmem_prompt_to_release_mem
  809.  * DESCRIPTION
  810.  *  Show "out-of-memory" screen and prompt user to stop other applications in order 
  811.  *  to release more memory.
  812.  *
  813.  *  Remark: 
  814.  *  1. This function is not suggested to be used in "interrupt screens" 
  815.  *     (e.g. MT call or USB plug-in indication)
  816.  *     Otherwise, it might re-enter the prompt screen and confuse user.
  817.  *  2. 'success_callback' needs to enter a new MMI screen!
  818.  *  3. There is no "failure callback" due to complex interaction of MMI screens.
  819.  *     Applications that use this function should never adopt any explicit or 
  820.  *     implicit "waiting" state for the result.
  821.  *
  822.  *     For example, user may press End key in the "out-of-memory" screen and
  823.  *     try to enter the application again. 
  824.  *
  825.  *  Warning: This function can only be used in MMI task.
  826.  * PARAMETERS
  827.  *  app_name            [IN]    String ID of the application 
  828.  *  app_icon            [IN]    Image ID of the application 
  829.  *  required_size       [IN]    Minimum required memory for the application
  830.  *  success_callback    [IN]    Callback function when getting memory successfully 
  831.  * RETURNS
  832.  *  void
  833.  *****************************************************************************/
  834. void mmi_frm_appmem_prompt_to_release_mem(
  835.         MMI_ID_TYPE app_name, 
  836.         MMI_ID_TYPE app_icon, 
  837.         U32 required_size,
  838.         void (*success_callback)(void))
  839. {
  840.     /*----------------------------------------------------------------*/
  841.     /* Local Variables                                                */
  842.     /*----------------------------------------------------------------*/
  843.     /*----------------------------------------------------------------*/
  844.     /* Code Body                                                      */
  845.     /*----------------------------------------------------------------*/
  846.     MMI_ASSERT(required_size <= applib_mem_ap_get_pool_size());
  847.     /* Avoid reentrance of the prompt screen.
  848.        Application developers should be careful on designing the screen flow */
  849.     MMI_ASSERT(!mmi_frm_appmem_is_in_prompt_screens() && 
  850.                !mmi_frm_appmem_is_prompt_screens_in_history());
  851.     /* Note the context g_mmi_frm_appmem_prompt is not reset  */
  852.     g_mmi_frm_appmem_prompt.app_name = app_name;
  853.     g_mmi_frm_appmem_prompt.app_icon = app_icon;
  854.     g_mmi_frm_appmem_prompt.required_size = required_size;
  855.     g_mmi_frm_appmem_prompt.require_full_pool = MMI_FALSE;    
  856.     g_mmi_frm_appmem_prompt.stop_state = MMI_FRM_APPMEM_STOP_STATE_IGNORE;
  857.     g_mmi_frm_appmem_prompt.keep_prompt_screen_in_history = MMI_TRUE;
  858.     g_mmi_frm_appmem_prompt.success_callback = success_callback;
  859.     mmi_frm_appmem_entry_prompt_screen();
  860. }
  861. /*****************************************************************************
  862.  * FUNCTION
  863.  *  mmi_frm_appmem_prompt_to_release_full_pool
  864.  * DESCRIPTION
  865.  *  Show "out-of-memory" screen and prompt user to stop other applications in order 
  866.  *  to release more memory.
  867.  *  
  868.  *  This function is similar to mmi_frm_appmem_prompt_to_release_mem
  869.  *  but it requires the whole pool.
  870.  *
  871.  *  Warning: This function can only be used in MMI task.
  872.  * PARAMETERS
  873.  *  app_name            [IN]    String ID of the application 
  874.  *  app_icon            [IN]    Image ID of the application 
  875.  *  success_callback    [IN]    Callback function when getting memory successfully 
  876.  * RETURNS
  877.  *  void
  878.  *****************************************************************************/
  879. void mmi_frm_appmem_prompt_to_release_full_pool(
  880.         MMI_ID_TYPE app_name, 
  881.         MMI_ID_TYPE app_icon, 
  882.         void (*success_callback)(void))
  883. {
  884.     /*----------------------------------------------------------------*/
  885.     /* Local Variables                                                */
  886.     /*----------------------------------------------------------------*/
  887.     /*----------------------------------------------------------------*/
  888.     /* Code Body                                                      */
  889.     /*----------------------------------------------------------------*/
  890.     /* Avoid reentrance of the prompt screen.
  891.        Application developers should be careful on designing the screen flow */
  892.     MMI_ASSERT(!mmi_frm_appmem_is_in_prompt_screens() && 
  893.                !mmi_frm_appmem_is_prompt_screens_in_history());
  894.     /* Note the context g_mmi_frm_appmem_prompt is not reset  */
  895.     g_mmi_frm_appmem_prompt.app_name = app_name;
  896.     g_mmi_frm_appmem_prompt.app_icon = app_icon;
  897.     g_mmi_frm_appmem_prompt.required_size = 0;
  898.     g_mmi_frm_appmem_prompt.require_full_pool = MMI_TRUE;
  899.     g_mmi_frm_appmem_prompt.stop_state = MMI_FRM_APPMEM_STOP_STATE_IGNORE;
  900.     g_mmi_frm_appmem_prompt.keep_prompt_screen_in_history = MMI_TRUE;
  901.     g_mmi_frm_appmem_prompt.success_callback = success_callback;
  902.     mmi_frm_appmem_entry_prompt_screen();
  903. }
  904. /*****************************************************************************
  905.  * FUNCTION
  906.  *  mmi_frm_appmem_init
  907.  * DESCRIPTION
  908.  *  Initialize app-based ASM.
  909.  * PARAMETERS
  910.  *  void
  911.  * RETURNS
  912.  *  void
  913.  *****************************************************************************/
  914. void mmi_frm_appmem_init(void)
  915. {
  916.     /*----------------------------------------------------------------*/
  917.     /* Local Variables                                                */
  918.     /*----------------------------------------------------------------*/
  919.     /*----------------------------------------------------------------*/
  920.     /* Code Body                                                      */
  921.     /*----------------------------------------------------------------*/
  922.     applib_mem_ap_init(mmi_frm_appmem_stop_finished_handler);
  923. }
  924. /***************************************************************************** 
  925.  * Unit Test 
  926.  *****************************************************************************/
  927. #ifdef APPLIB_MEM_UNIT_TEST
  928. #define MMI_FRM_APPMEM_TEST1_SIZE   (60*1024)
  929. #define MMI_FRM_APPMEM_TEST2_SIZE   (30*1024)
  930. #define MMI_FRM_APPMEM_TEST3_SIZE   (100*1024)
  931. #define MMI_FRM_APPMEM_TEST4_SIZE   (25*1024)
  932. #define MMI_FRM_APPMEM_TEST5_SIZE   (40*1024)
  933. #include "stdlib.h"
  934. /*****************************************************************************
  935.  * FUNCTION
  936.  *  mmi_frm_appmem_unit_test_stress_alloc
  937.  * DESCRIPTION
  938.  *  Unit test routine
  939.  * PARAMETERS
  940.  *  void
  941.  * RETURNS
  942.  *  void
  943.  *****************************************************************************/
  944. static void mmi_frm_appmem_unit_test_stress_alloc(void)
  945. {
  946.     /*----------------------------------------------------------------*/
  947.     /* Local Variables                                                */
  948.     /*----------------------------------------------------------------*/
  949.     kal_uint32 *ptr_list[100];
  950.     kal_int32 i, count, free_space_original, tmp;
  951.     /*----------------------------------------------------------------*/
  952.     /* Code Body                                                      */
  953.     /*----------------------------------------------------------------*/
  954.     applib_mem_ap_free_all(APPLIB_MEM_AP_ID_TEST1);
  955.     applib_mem_ap_free_all(APPLIB_MEM_AP_ID_TEST2);
  956.     applib_mem_ap_free_all(APPLIB_MEM_AP_ID_TEST3);
  957.     applib_mem_ap_free_all(APPLIB_MEM_AP_ID_TEST4);
  958.     applib_mem_ap_free_all(APPLIB_MEM_AP_ID_TEST5);    
  959.     free_space_original = applib_mem_ap_get_total_left_size();
  960. #define ALLOC_SIZE(_idx)    ((_idx) + (_idx) * 256)
  961. #define PTR_LIST_SIZE       (sizeof(ptr_list) / sizeof(void*))
  962.     for (i = 0; i < PTR_LIST_SIZE; i++)
  963.     {
  964.         if ((ptr_list[i] = applib_mem_ap_alloc(APPLIB_MEM_AP_ID_TEST1, ALLOC_SIZE(i))) == NULL)
  965.         {
  966.             MMI_ASSERT(applib_mem_ap_get_max_alloc_size() < ALLOC_SIZE(i));
  967.             break;
  968.         }
  969.     }
  970.     count = i;
  971.     MMI_ASSERT(count > 0);
  972.     MMI_ASSERT(applib_mem_ap_get_alloc_count() == count);
  973.     applib_mem_ap_check_integrity();
  974.     tmp = 0;
  975.     for (i = 0; i < count; i += 2)
  976.     {
  977.         tmp += ALLOC_SIZE(i);
  978.         applib_mem_ap_free(ptr_list[i]);
  979.     }
  980. #ifndef MMI_ON_WIN32
  981.     MMI_ASSERT(tmp < free_space_original - applib_mem_ap_get_total_left_size());
  982. #endif
  983.     applib_mem_ap_check_integrity();
  984.     if (count >= 2)
  985.     {
  986.         for (i = 1; i < count; i += 2)
  987.         {
  988.             MMI_ASSERT(applib_mem_ap_is_valid_block(ptr_list[i]));
  989.         }
  990.     }
  991.     applib_mem_ap_free_all(APPLIB_MEM_AP_ID_TEST2);
  992.     applib_mem_ap_check_integrity();
  993.     applib_mem_ap_free_all(APPLIB_MEM_AP_ID_TEST1);
  994.     applib_mem_ap_check_integrity();
  995.     MMI_ASSERT(free_space_original == applib_mem_ap_get_total_left_size());
  996.     MMI_ASSERT(applib_mem_ap_get_alloc_count() == 0);    
  997.     /* Random alloc/free */
  998.     for (i = 0; i < PTR_LIST_SIZE; i++)
  999.     {
  1000.         ptr_list[i] = NULL;
  1001.     }
  1002.     for (;;)
  1003.     {
  1004.         kal_int32 key = rand();
  1005.         if ((key % 1000) == 0)
  1006.         {
  1007.             break;
  1008.         }
  1009.         
  1010.         if (applib_mem_ap_get_alloc_count() == PTR_LIST_SIZE || key % 3 == 0)
  1011.         {
  1012.             i = ((key + 1) % PTR_LIST_SIZE);
  1013.             for (; i != key % PTR_LIST_SIZE; i++, i %= PTR_LIST_SIZE)
  1014.             {
  1015.                 if (ptr_list[i])
  1016.                 {
  1017.                     applib_mem_ap_free(ptr_list[i]);
  1018.                     ptr_list[i] = NULL;
  1019.                     break;
  1020.                 }
  1021.             }
  1022.         }
  1023.         else
  1024.         {
  1025.             for (i = 0; i < PTR_LIST_SIZE; i++)
  1026.             {
  1027.                 if (!ptr_list[i])
  1028.                 {
  1029.                     kal_uint32 size = key % applib_mem_ap_get_pool_size();
  1030.                     ptr_list[i] = applib_mem_ap_alloc(APPLIB_MEM_AP_ID_TEST1 + (key % 5), size);
  1031.                     /* Test memory corruption detection */
  1032.                     // ptr_list[i][size/4] = 1;
  1033.                     // ptr_list[i][size/4 + 1] = 1;
  1034.                     // ptr_list[i][size/4 + 2] = 1;
  1035.                     break;
  1036.                 }
  1037.             }
  1038.         }
  1039.         applib_mem_ap_check_integrity();
  1040.     }
  1041.     applib_mem_ap_free_all(APPLIB_MEM_AP_ID_TEST1);
  1042.     applib_mem_ap_free_all(APPLIB_MEM_AP_ID_TEST2);
  1043.     applib_mem_ap_free_all(APPLIB_MEM_AP_ID_TEST3);
  1044.     applib_mem_ap_free_all(APPLIB_MEM_AP_ID_TEST4);
  1045.     applib_mem_ap_free_all(APPLIB_MEM_AP_ID_TEST5);    
  1046.     MMI_ASSERT(free_space_original == applib_mem_ap_get_total_left_size());
  1047.     MMI_ASSERT(applib_mem_ap_get_alloc_count() == 0);
  1048. }
  1049. static void mmi_frm_appmem_unit_test_stop_1_timer_hdlr(void)
  1050. {
  1051.     /*----------------------------------------------------------------*/
  1052.     /* Local Variables                                                */
  1053.     /*----------------------------------------------------------------*/
  1054.     /*----------------------------------------------------------------*/
  1055.     /* Code Body                                                      */
  1056.     /*----------------------------------------------------------------*/
  1057.     applib_mem_ap_free_all(APPLIB_MEM_AP_ID_TEST1);    
  1058.     applib_mem_ap_notify_stop_finished(APPLIB_MEM_AP_ID_TEST1, KAL_TRUE);
  1059. }
  1060. static void mmi_frm_appmem_unit_test_stop_1(void)
  1061. {
  1062.     /*----------------------------------------------------------------*/
  1063.     /* Local Variables                                                */
  1064.     /*----------------------------------------------------------------*/
  1065.     /*----------------------------------------------------------------*/
  1066.     /* Code Body                                                      */
  1067.     /*----------------------------------------------------------------*/
  1068.     StartTimer(FM_AUTO_TEST_COMMNON_TIMER, 10000, mmi_frm_appmem_unit_test_stop_1_timer_hdlr);
  1069. }
  1070. static void mmi_frm_appmem_unit_test_stop_2_timer_hdlr(void)
  1071. {
  1072.     /*----------------------------------------------------------------*/
  1073.     /* Local Variables                                                */
  1074.     /*----------------------------------------------------------------*/
  1075.     /*----------------------------------------------------------------*/
  1076.     /* Code Body                                                      */
  1077.     /*----------------------------------------------------------------*/
  1078.     applib_mem_ap_free_all(APPLIB_MEM_AP_ID_TEST2);
  1079.     applib_mem_ap_notify_stop_finished(APPLIB_MEM_AP_ID_TEST2, KAL_TRUE);
  1080. }
  1081. static void mmi_frm_appmem_unit_test_stop_2(void)
  1082. {
  1083.     /*----------------------------------------------------------------*/
  1084.     /* Local Variables                                                */
  1085.     /*----------------------------------------------------------------*/
  1086.     /*----------------------------------------------------------------*/
  1087.     /* Code Body                                                      */
  1088.     /*----------------------------------------------------------------*/
  1089.     StartTimer(FM_AUTO_TEST_COMMNON_TIMER, 300, mmi_frm_appmem_unit_test_stop_2_timer_hdlr);
  1090. }
  1091. static void mmi_frm_appmem_unit_test_stop_3_timer_hdlr(void)
  1092. {
  1093.     /*----------------------------------------------------------------*/
  1094.     /* Local Variables                                                */
  1095.     /*----------------------------------------------------------------*/
  1096.     /*----------------------------------------------------------------*/
  1097.     /* Code Body                                                      */
  1098.     /*----------------------------------------------------------------*/
  1099.     applib_mem_ap_free_all(APPLIB_MEM_AP_ID_TEST3);    
  1100.     applib_mem_ap_notify_stop_finished(APPLIB_MEM_AP_ID_TEST3, KAL_TRUE);
  1101. }
  1102. static void mmi_frm_appmem_unit_test_stop_3(void)
  1103. {
  1104.     /*----------------------------------------------------------------*/
  1105.     /* Local Variables                                                */
  1106.     /*----------------------------------------------------------------*/
  1107.     /*----------------------------------------------------------------*/
  1108.     /* Code Body                                                      */
  1109.     /*----------------------------------------------------------------*/
  1110.     StartTimer(FM_AUTO_TEST_COMMNON_TIMER, 3000, mmi_frm_appmem_unit_test_stop_3_timer_hdlr);
  1111. }
  1112. static void mmi_frm_appmem_unit_test_stop_4(void)
  1113. {
  1114.     /*----------------------------------------------------------------*/
  1115.     /* Local Variables                                                */
  1116.     /*----------------------------------------------------------------*/
  1117.     /*----------------------------------------------------------------*/
  1118.     /* Code Body                                                      */
  1119.     /*----------------------------------------------------------------*/
  1120.     applib_mem_ap_free_all(APPLIB_MEM_AP_ID_TEST4);        
  1121.     /* Return immediately */   
  1122.     applib_mem_ap_notify_stop_finished(APPLIB_MEM_AP_ID_TEST4, KAL_TRUE);
  1123. }
  1124. static void mmi_frm_appmem_unit_test_stop_5(void)
  1125. {
  1126.     /*----------------------------------------------------------------*/
  1127.     /* Local Variables                                                */
  1128.     /*----------------------------------------------------------------*/
  1129.     /*----------------------------------------------------------------*/
  1130.     /* Code Body                                                      */
  1131.     /*----------------------------------------------------------------*/
  1132.     /* Return immediately */
  1133.     applib_mem_ap_notify_stop_finished(APPLIB_MEM_AP_ID_TEST5, KAL_FALSE); /* Fail */
  1134. }
  1135. /*****************************************************************************
  1136.  * FUNCTION
  1137.  *  mmi_frm_appmem_unit_test_success_callback1
  1138.  * DESCRIPTION
  1139.  *  Callback function when memory was released.
  1140.  * PARAMETERS
  1141.  *  void
  1142.  * RETURNS
  1143.  *  void
  1144.  *****************************************************************************/
  1145. static void (*g_mmi_frm_appmem_unit_test_run_callback1)(void);
  1146. static void mmi_frm_appmem_unit_test_success_callback1(void)
  1147. {
  1148.     void *ptr = applib_mem_ap_alloc(APPLIB_MEM_AP_ID_TEST1, MMI_FRM_APPMEM_TEST1_SIZE);
  1149.     MMI_ASSERT(ptr);
  1150.     g_mmi_frm_appmem_unit_test_run_callback1();
  1151. }
  1152. /*****************************************************************************
  1153.  * FUNCTION
  1154.  *  mmi_frm_appmem_unit_test_prepare_to_enter_test1
  1155.  * DESCRIPTION
  1156.  *  Simple program to test screen-based ASM
  1157.  * PARAMETERS
  1158.  *  void
  1159.  * RETURNS
  1160.  *  void
  1161.  *****************************************************************************/
  1162. void mmi_frm_appmem_unit_test_prepare_to_enter_test1(void (*run_callback)(void))
  1163. {
  1164.     /*----------------------------------------------------------------*/
  1165.     /* Local Variables                                                */
  1166.     /*----------------------------------------------------------------*/
  1167.     
  1168.     /*----------------------------------------------------------------*/
  1169.     /* Code Body                                                      */
  1170.     /*----------------------------------------------------------------*/
  1171.     applib_mem_ap_free_all(APPLIB_MEM_AP_ID_TEST1);
  1172.     g_mmi_frm_appmem_unit_test_run_callback1 = run_callback;
  1173.     if (applib_mem_ap_alloc(APPLIB_MEM_AP_ID_TEST1, MMI_FRM_APPMEM_TEST1_SIZE))
  1174.     {
  1175.         run_callback();
  1176.     }
  1177.     else
  1178.     {
  1179.         mmi_frm_appmem_prompt_to_release_mem(
  1180.             APPLIB_MEM_AP_ID_TEST1,
  1181.             IMG_GLOBAL_L1,
  1182.             MMI_FRM_APPMEM_TEST1_SIZE,
  1183.             mmi_frm_appmem_unit_test_success_callback1);
  1184.     }
  1185. }
  1186. /*****************************************************************************
  1187.  * FUNCTION
  1188.  *  mmi_frm_appmem_unit_test_success_callback2
  1189.  * DESCRIPTION
  1190.  *  Callback function when memory was released.
  1191.  * PARAMETERS
  1192.  *  void
  1193.  * RETURNS
  1194.  *  void
  1195.  *****************************************************************************/
  1196. static void (*g_mmi_frm_appmem_unit_test_run_callback2)(void);
  1197. static void mmi_frm_appmem_unit_test_success_callback2(void)
  1198. {
  1199.     void *ptr = applib_mem_ap_alloc(APPLIB_MEM_AP_ID_TEST2, MMI_FRM_APPMEM_TEST2_SIZE);
  1200.     MMI_ASSERT(ptr);
  1201.     g_mmi_frm_appmem_unit_test_run_callback2();
  1202. }
  1203. /*****************************************************************************
  1204.  * FUNCTION
  1205.  *  mmi_frm_appmem_unit_test_prepare_to_enter_test2
  1206.  * DESCRIPTION
  1207.  *  Simple program to test screen-based ASM
  1208.  * PARAMETERS
  1209.  *  void
  1210.  * RETURNS
  1211.  *  void
  1212.  *****************************************************************************/
  1213. void mmi_frm_appmem_unit_test_prepare_to_enter_test2(void (*run_callback)(void))
  1214. {
  1215.     /*----------------------------------------------------------------*/
  1216.     /* Local Variables                                                */
  1217.     /*----------------------------------------------------------------*/
  1218.     
  1219.     /*----------------------------------------------------------------*/
  1220.     /* Code Body                                                      */
  1221.     /*----------------------------------------------------------------*/
  1222.     applib_mem_ap_free_all(APPLIB_MEM_AP_ID_TEST2);
  1223.     g_mmi_frm_appmem_unit_test_run_callback2 = run_callback;    
  1224.     if (applib_mem_ap_alloc(APPLIB_MEM_AP_ID_TEST2, MMI_FRM_APPMEM_TEST2_SIZE))
  1225.     {
  1226.         run_callback();
  1227.     }
  1228.     else
  1229.     {
  1230.         mmi_frm_appmem_prompt_to_release_mem(
  1231.             APPLIB_MEM_AP_ID_TEST2,
  1232.             IMG_GLOBAL_L2,
  1233.             MMI_FRM_APPMEM_TEST2_SIZE,
  1234.             mmi_frm_appmem_unit_test_success_callback2);
  1235.     }
  1236. }
  1237. /*****************************************************************************
  1238.  * FUNCTION
  1239.  *  mmi_frm_appmem_unit_test_success_callback3
  1240.  * DESCRIPTION
  1241.  *  Callback function when memory was released.
  1242.  * PARAMETERS
  1243.  *  void
  1244.  * RETURNS
  1245.  *  void
  1246.  *****************************************************************************/
  1247. static void (*g_mmi_frm_appmem_unit_test_run_callback3)(void);
  1248. static void mmi_frm_appmem_unit_test_success_callback3(void)
  1249. {
  1250.     void *ptr = applib_mem_ap_alloc(APPLIB_MEM_AP_ID_TEST3, MMI_FRM_APPMEM_TEST3_SIZE);
  1251.     MMI_ASSERT(ptr);
  1252.     g_mmi_frm_appmem_unit_test_run_callback3();
  1253. }
  1254. /*****************************************************************************
  1255.  * FUNCTION
  1256.  *  mmi_frm_appmem_unit_test_prepare_to_enter_test3
  1257.  * DESCRIPTION
  1258.  *  Simple program to test screen-based ASM
  1259.  * PARAMETERS
  1260.  *  void
  1261.  * RETURNS
  1262.  *  void
  1263.  *****************************************************************************/
  1264. void mmi_frm_appmem_unit_test_prepare_to_enter_test3(void (*run_callback)(void))
  1265. {
  1266.     /*----------------------------------------------------------------*/
  1267.     /* Local Variables                                                */
  1268.     /*----------------------------------------------------------------*/
  1269.     
  1270.     /*----------------------------------------------------------------*/
  1271.     /* Code Body                                                      */
  1272.     /*----------------------------------------------------------------*/
  1273.     applib_mem_ap_free_all(APPLIB_MEM_AP_ID_TEST3);
  1274.     g_mmi_frm_appmem_unit_test_run_callback3 = run_callback;    
  1275.     if (applib_mem_ap_alloc(APPLIB_MEM_AP_ID_TEST3, MMI_FRM_APPMEM_TEST3_SIZE))
  1276.     {
  1277.         run_callback();
  1278.     }
  1279.     else
  1280.     {
  1281.         mmi_frm_appmem_prompt_to_release_mem(
  1282.             APPLIB_MEM_AP_ID_TEST3,
  1283.             IMG_GLOBAL_L3,
  1284.             MMI_FRM_APPMEM_TEST3_SIZE,
  1285.             mmi_frm_appmem_unit_test_success_callback3);
  1286.     }
  1287. }
  1288. /*****************************************************************************
  1289.  * FUNCTION
  1290.  *  mmi_frm_appmem_unit_test_success_callback4
  1291.  * DESCRIPTION
  1292.  *  Callback function when memory was released.
  1293.  * PARAMETERS
  1294.  *  void
  1295.  * RETURNS
  1296.  *  void
  1297.  *****************************************************************************/
  1298. static void (*g_mmi_frm_appmem_unit_test_run_callback4)(void);
  1299. static void mmi_frm_appmem_unit_test_success_callback4(void)
  1300. {
  1301.     void *ptr = applib_mem_ap_alloc(APPLIB_MEM_AP_ID_TEST4, MMI_FRM_APPMEM_TEST4_SIZE);
  1302.     MMI_ASSERT(ptr);
  1303.     g_mmi_frm_appmem_unit_test_run_callback4();
  1304. }
  1305. /*****************************************************************************
  1306.  * FUNCTION
  1307.  *  mmi_frm_appmem_unit_test_prepare_to_enter_test4
  1308.  * DESCRIPTION
  1309.  *  Simple program to test screen-based ASM
  1310.  * PARAMETERS
  1311.  *  void
  1312.  * RETURNS
  1313.  *  void
  1314.  *****************************************************************************/
  1315. void mmi_frm_appmem_unit_test_prepare_to_enter_test4(void (*run_callback)(void))
  1316. {
  1317.     /*----------------------------------------------------------------*/
  1318.     /* Local Variables                                                */
  1319.     /*----------------------------------------------------------------*/
  1320.     
  1321.     /*----------------------------------------------------------------*/
  1322.     /* Code Body                                                      */
  1323.     /*----------------------------------------------------------------*/
  1324.     applib_mem_ap_free_all(APPLIB_MEM_AP_ID_TEST4);
  1325.     g_mmi_frm_appmem_unit_test_run_callback4 = run_callback;    
  1326.     if (applib_mem_ap_alloc(APPLIB_MEM_AP_ID_TEST4, MMI_FRM_APPMEM_TEST4_SIZE))
  1327.     {
  1328.         run_callback();
  1329.     }
  1330.     else
  1331.     {
  1332.         mmi_frm_appmem_prompt_to_release_mem(
  1333.             APPLIB_MEM_AP_ID_TEST4,
  1334.             IMG_GLOBAL_L4,
  1335.             MMI_FRM_APPMEM_TEST4_SIZE,
  1336.             mmi_frm_appmem_unit_test_success_callback4);
  1337.     }
  1338. }
  1339. /*****************************************************************************
  1340.  * FUNCTION
  1341.  *  mmi_frm_appmem_unit_test_success_callback5
  1342.  * DESCRIPTION
  1343.  *  Callback function when memory was released.
  1344.  * PARAMETERS
  1345.  *  void
  1346.  * RETURNS
  1347.  *  void
  1348.  *****************************************************************************/
  1349. static void (*g_mmi_frm_appmem_unit_test_run_callback5)(void);
  1350. static void mmi_frm_appmem_unit_test_success_callback5(void)
  1351. {
  1352.     void *ptr = applib_mem_ap_alloc(APPLIB_MEM_AP_ID_TEST5, MMI_FRM_APPMEM_TEST5_SIZE);
  1353.     MMI_ASSERT(ptr);
  1354.     g_mmi_frm_appmem_unit_test_run_callback5();
  1355. }
  1356. /*****************************************************************************
  1357.  * FUNCTION
  1358.  *  mmi_frm_appmem_unit_test_prepare_to_enter_test5
  1359.  * DESCRIPTION
  1360.  *  Simple program to test screen-based ASM
  1361.  * PARAMETERS
  1362.  *  void
  1363.  * RETURNS
  1364.  *  void
  1365.  *****************************************************************************/
  1366. void mmi_frm_appmem_unit_test_prepare_to_enter_test5(void (*run_callback)(void))
  1367. {
  1368.     /*----------------------------------------------------------------*/
  1369.     /* Local Variables                                                */
  1370.     /*----------------------------------------------------------------*/
  1371.     
  1372.     /*----------------------------------------------------------------*/
  1373.     /* Code Body                                                      */
  1374.     /*----------------------------------------------------------------*/
  1375.     applib_mem_ap_free_all(APPLIB_MEM_AP_ID_TEST5);
  1376.     g_mmi_frm_appmem_unit_test_run_callback5 = run_callback;    
  1377.     if (applib_mem_ap_alloc(APPLIB_MEM_AP_ID_TEST5, MMI_FRM_APPMEM_TEST5_SIZE))
  1378.     {
  1379.         run_callback();
  1380.     }
  1381.     else
  1382.     {
  1383.         mmi_frm_appmem_prompt_to_release_mem(
  1384.             APPLIB_MEM_AP_ID_TEST5,
  1385.             IMG_GLOBAL_L5,
  1386.             MMI_FRM_APPMEM_TEST5_SIZE,
  1387.             mmi_frm_appmem_unit_test_success_callback5);
  1388.     }
  1389. }
  1390. /*****************************************************************************
  1391.  * FUNCTION
  1392.  *  mmi_frm_appmem_unit_test_setup_environment
  1393.  * DESCRIPTION
  1394.  *  Setup environment for unit test of app-based ASM 
  1395.  * PARAMETERS
  1396.  *  void
  1397.  * RETURNS
  1398.  *  void
  1399.  *****************************************************************************/
  1400. void mmi_frm_appmem_unit_test_setup_environment(void)
  1401. {
  1402.     /*----------------------------------------------------------------*/
  1403.     /* Local Variables                                                */
  1404.     /*----------------------------------------------------------------*/
  1405.     static MMI_BOOL first_time = MMI_TRUE;
  1406.     
  1407.     /*----------------------------------------------------------------*/
  1408.     /* Code Body                                                      */
  1409.     /*----------------------------------------------------------------*/
  1410.     if (first_time)
  1411.     {
  1412.         first_time = MMI_FALSE;
  1413.         
  1414.         applib_mem_ap_register(
  1415.             APPLIB_MEM_AP_ID_TEST1, 
  1416.             STR_GLOBAL_1, 
  1417.             IMG_GLOBAL_L1, 
  1418.             mmi_frm_appmem_unit_test_stop_1);
  1419.         applib_mem_ap_register(
  1420.             APPLIB_MEM_AP_ID_TEST2, 
  1421.             STR_GLOBAL_2, 
  1422.             IMG_GLOBAL_L2, 
  1423.             mmi_frm_appmem_unit_test_stop_2);
  1424.         applib_mem_ap_register(
  1425.             APPLIB_MEM_AP_ID_TEST3, 
  1426.             STR_GLOBAL_3, 
  1427.             IMG_GLOBAL_L3, 
  1428.             mmi_frm_appmem_unit_test_stop_3);
  1429.         applib_mem_ap_register(
  1430.             APPLIB_MEM_AP_ID_TEST4, 
  1431.             STR_GLOBAL_4, 
  1432.             IMG_GLOBAL_L4, 
  1433.             mmi_frm_appmem_unit_test_stop_4);
  1434.         applib_mem_ap_register(
  1435.             APPLIB_MEM_AP_ID_TEST5, 
  1436.             STR_GLOBAL_5, 
  1437.             IMG_GLOBAL_L5, 
  1438.             mmi_frm_appmem_unit_test_stop_5);
  1439.     }
  1440.     mmi_frm_appmem_unit_test_stress_alloc();
  1441. }
  1442. #endif /* APPLIB_MEM_UNIT_TEST */